Skip to content

Commit fb98ecc

Browse files
authored
Merge pull request #288 from boostcampwm-2024/feature-FE-##287-Host_use_SessionKey
[FEAT] 404 에러 처리 및 세션키로 변경
2 parents d37e827 + d8d524d commit fb98ecc

File tree

2 files changed

+100
-6
lines changed

2 files changed

+100
-6
lines changed

frontend/src/hooks/usePlayer.ts

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,100 @@
1-
import Hls from 'hls.js';
1+
import Hls, {
2+
Loader,
3+
LoaderCallbacks,
4+
LoaderConfiguration,
5+
LoaderContext,
6+
LoaderResponse,
7+
LoaderStats,
8+
HlsConfig
9+
} from 'hls.js';
210
import { useEffect, useRef } from 'react';
311

12+
class CustomLoader implements Loader<LoaderContext> {
13+
private loader: Loader<LoaderContext>;
14+
15+
constructor(config: HlsConfig) {
16+
const DefaultLoader = Hls.DefaultConfig.loader as new (config: HlsConfig) => Loader<LoaderContext>;
17+
this.loader = new DefaultLoader(config);
18+
}
19+
20+
load(context: LoaderContext, config: LoaderConfiguration, callbacks: LoaderCallbacks<LoaderContext>) {
21+
const { onSuccess, onError, onTimeout, onAbort, onProgress } = callbacks;
22+
23+
const newCallbacks: LoaderCallbacks<LoaderContext> = {
24+
onSuccess: (response: LoaderResponse, stats: LoaderStats, context: LoaderContext, networkDetails: any = null) => {
25+
onSuccess(response, stats, context, networkDetails);
26+
},
27+
onError: (
28+
error: { code: number; text: string },
29+
context: LoaderContext,
30+
networkDetails: any = null,
31+
stats: LoaderStats
32+
) => {
33+
if (error.code === 404) {
34+
const emptyData = new ArrayBuffer(0);
35+
onSuccess(
36+
{
37+
url: context.url,
38+
data: emptyData
39+
},
40+
{
41+
trequest: performance.now(),
42+
tfirst: performance.now(),
43+
tload: performance.now(),
44+
loaded: 0,
45+
total: 0
46+
} as unknown as LoaderStats,
47+
context,
48+
networkDetails
49+
);
50+
} else {
51+
if (onError) {
52+
onError(error, context, networkDetails, stats);
53+
}
54+
}
55+
},
56+
onTimeout: (stats: LoaderStats, context: LoaderContext, networkDetails: any = null) => {
57+
if (onTimeout) {
58+
onTimeout(stats, context, networkDetails);
59+
}
60+
},
61+
onAbort: (stats: LoaderStats, context: LoaderContext, networkDetails: any = null) => {
62+
if (onAbort) {
63+
onAbort(stats, context, networkDetails);
64+
}
65+
},
66+
onProgress: (
67+
stats: LoaderStats,
68+
context: LoaderContext,
69+
data: string | ArrayBuffer,
70+
networkDetails: any = null
71+
) => {
72+
if (onProgress) {
73+
onProgress(stats, context, data, networkDetails);
74+
}
75+
}
76+
};
77+
78+
this.loader.load(context, config, newCallbacks);
79+
}
80+
81+
abort() {
82+
this.loader.abort();
83+
}
84+
85+
destroy() {
86+
this.loader.destroy();
87+
}
88+
89+
get stats() {
90+
return this.loader.stats;
91+
}
92+
93+
get context() {
94+
return this.loader.context;
95+
}
96+
}
97+
498
export default function usePlayer(url: string) {
599
const videoRef = useRef<HTMLVideoElement | null>(null);
6100

@@ -19,7 +113,8 @@ export default function usePlayer(url: string) {
19113

20114
const hls = new Hls({
21115
enableWorker: true,
22-
lowLatencyMode: true
116+
lowLatencyMode: true,
117+
loader: CustomLoader
23118
});
24119

25120
hls.loadSource(url);

frontend/src/utils/hostURL.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { BASE_URL, RTMP_HTTP_PORT } from '@apis/index';
2-
import { getStreamKey } from './streamKey';
1+
import { getSessionKey } from './streamKey';
32

43
export function getHostURL() {
5-
const streamKey = getStreamKey();
4+
const sessionKey = getSessionKey();
65

7-
return `${BASE_URL}:${RTMP_HTTP_PORT}/live/${streamKey}/index.m3u8`;
6+
return `https://kr.object.ncloudstorage.com/web22/live/${sessionKey}/index.m3u8`;
87
}

0 commit comments

Comments
 (0)