Notice
Recent Posts
Recent Comments
Link
투케이2K
113. (Aws/Amazon) [Aws Kinesis Video Streams] Aws KVS HLS 옵션 지정 FragmentSelectorType 종류 정리 본문
Aws (Amazon)
113. (Aws/Amazon) [Aws Kinesis Video Streams] Aws KVS HLS 옵션 지정 FragmentSelectorType 종류 정리
투케이2K 2025. 9. 15. 21:33728x90
[개발 환경 설정]
개발 환경 : Aws / Amazon Web Services

[설명 정리]
// --------------------------------------------------------------------------------------
[개발 및 환경]
// --------------------------------------------------------------------------------------
- 인프라 : Aws / Amazon Web Services
- 기술 구분 : Aws / Aws Kinesis Video Streams / HLS / FragmentSelectorType
- 사전) 비디오 스트림 설명 :
>> 비디오 스트림은 라이브 비디오 및 기타 시간이 인코딩된 데이터를 캡처하고, 선택적으로 저장하고, 실시간, 배치 혹은 애드혹 형식으로 데이터의 소비를 가능하게 할 수 있도록 해 주는 리소스입니다
>> 일반적인 구성에서는 Kinesis 비디오 스트림은 데이터를 푸시해 주는 생산자가 하나만 있습니다 (실시간 스트리밍 데이터를 밀어 넣어주는 하드웨어 기기)
- 사전) KVS 개념 설명 :
>> KVS 는 ML (기계 학습), 재생 및 기타 처리를 위해 커넥티드 디바이스에서 AWS로 비디오를 쉽고 안전하게 스트리밍할 수 있는 기술입니다
- 사전) HLS 개념 설명 :
>> HTTP 라이브 스트리밍으로 인터넷을 통해 소비자에게 미디어 콘텐츠를 제공하는 데 사용되는 스트리밍 프로토콜입니다
>> HLS 는 Apple 장치에서 지원하는 유일한 형식입니다
>> 미디어는 h.264 또는 h.265 인코딩된 비디오를 포함해야 하며 AAC 인코딩된 오디오는 선택 사항입니다
>> 미디어 타입 유효한 값의 예로는 "video/h264" 및 "video/h264,audio/aac" 가 있습니다
// --------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------
[설 명]
// --------------------------------------------------------------------------------------
1. FragmentSelectorType 은 AWS Kinesis Video Streams 에서 HLS (HTTP Live Streaming) 를 사용할 때, 어떤 기준의 타임스탬프를 기반으로 미디어 조각 (fragment) 을 선택 할지를 지정하는 설정입니다
2. GetHLSStreamingSessionURL API 호출 시 사용 되는 Request 전문 : 재생 되고 있는 HLS 스트리밍 URL 확인
POST /getHLSStreamingSessionURL HTTP/1.1
Content-type: application/json
{
"ContainerFormat": "string",
"DiscontinuityMode": "string",
"DisplayFragmentTimestamp": "string",
"Expires": number,
"HLSFragmentSelector": {
"FragmentSelectorType": "string",
"TimestampRange": {
"EndTimestamp": number,
"StartTimestamp": number
}
},
"MaxMediaPlaylistFragmentResults": number,
"PlaybackMode": "string",
"StreamARN": "string",
"StreamName": "string"
}
3. FragmentSelectorType의 종류
>> PRODUCER_TIMESTAMP :
- 비디오를 생성한 장치 (Producer) 가 각 프래그먼트에 부여한 타임스탬프를 기준으로 프래그먼트를 선택합니다
- 해당 옵션은 주로 온디맨드(ON_DEMAND) 또는 라이브 리플레이(LIVE_REPLAY) 모드에서 사용됩니다
- 해당 옵션 사용 시 지정된 TimestampRange 내에서 가장 먼저 수집된 프래그먼트부터 시작하여, 해당 범위 내의 프래그먼트들을 포함합니다
- 해당 옵션 사용 시 중복된 타임스탬프를 가진 프래그먼트는 중복 제거 (deduplication) 처리됩니다
- LIVE 모드에서는 producer timestamp 가 MP4 프래그먼트에 사용되지만, 실제로는 서버 타임스탬프 기준으로 가장 최근에 수집된 프래그먼트가 HLS playlist 에 포함됩니다
>> SERVER_TIMESTAMP :
- AWS 서버가 프래그먼트를 수신한 시점의 타임스탬프를 기준으로 프래그먼트를 선택합니다
- 해당 옵션은 기본값 (default) 으로 설정되어 있으며, 일반적으로 LIVE 스트리밍에 적합합니다
- 해당 옵션은 서버 타임 기준이므로, producer 가 과거 타임스탬프를 부여한 프래그먼트는 포함되지 않을 수 있습니다
4. 자바스크립트 소스 기준 KinesisVideoArchivedMedia 사용 예시 코드 :
// -----------------------------------------
// [엔드포인트를 기반으로 HLS 세션 URL 가져오기]
// -----------------------------------------
const kinesisVideoArchivedMedia = new AWS.KinesisVideoArchivedMedia({
region: region,
endpoint: endpoint,
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey
});
const startTimestamp = new Date(Date.now() - 30 * 1000); // 과거 : 30초전
//const startTimestamp = new Date(Date.now() - 60 * 1000); // 과거 : 1분전
//const endTimestamp = new Date(); // 현재
//const endTimestamp = new Date(Date.now() + 60 * 1000); // 미래 : 1분후
kinesisVideoArchivedMedia.getHLSStreamingSessionURL({
StreamName: streamName,
//PlaybackMode: 'LIVE', // 가장 최신만 재생 (지연 최소, 끊김 위험 높음)
PlaybackMode: 'LIVE_REPLAY', // 이전 데이터 부터 재생을 하므로 끊김 최소화 : AWS 콘솔 대시보드도 동일 설정
HLSFragmentSelector: {
FragmentSelectorType: 'SERVER_TIMESTAMP', // 또는 PRODUCER_TIMESTAMP
TimestampRange: {
StartTimestamp: startTimestamp,
//EndTimestamp: endTimestamp // 디바이스가 올린 영상 종료까지 출력 위해 주석 : 주석하지 않으면 시작 ~ 종료 범위까지의 영상만 재생 됨
}
}
}, function(err, response){
if (err){
console.error("[getHLSStreamingSessionURL] : [ERROR] : ", JSON.stringify(err.message));
return;
}
console.log("[getHLSStreamingSessionURL] : [response] : ", JSON.stringify(response));
const hlsUrl = response.HLSStreamingSessionURL; // parsing hls stream url
if (hlsUrl != null && hlsUrl != '' && hlsUrl != 'undefined'){
Hls.DefaultConfig.debug = true; // 디버깅 로그 출력
if (Hls.isSupported()){ // Hls 지원 여부 확인
const hls = new Hls(); // Default
hls.loadSource(hlsUrl);
hls.attachMedia(remoteView);
// [비디오 AutoPlay 설정으로 주석 처리]
/*
hls.on(Hls.Events.MANIFEST_PARSED, function(){
remoteView.play();
console.log("[getHLSStreamingSessionURL] : Hls 스트리밍 재생 수행]");
});
// */
hls.on(Hls.Events.ERROR, function(event, data){
console.error("[getHLSStreamingSessionURL] : Hls.Events.ERROR : ", JSON.stringify(data));
// [에러 분기 처리 시 사용]
// if (data.details === 'bufferStalledError'){ }
// if (data.details === 'bufferStalledError'){ }
});
}
else {
console.error("[getHLSStreamingSessionURL] : [ERROR] : Hls.isSupported False");
}
}
else {
console.error("[getHLSStreamingSessionURL] : [ERROR] : HLSStreamingSessionURL Is Null");
}
})
5. 참고 사항 정리 :
>> PlaybackMode LIVE 모드에서는 TimestampRange 를 사용하지 않아야 합니다.
>> MaxMediaPlaylistFragmentResults 값을 통해 playlist에 포함될 최대 프래그먼트 수를 제한할 수 있습니다
// --------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------
[참고 사이트]
// --------------------------------------------------------------------------------------
[AWS 사이트 : GetHLSStreamingSessionURL 설명]
https://docs.aws.amazon.com/ko_kr/kinesisvideostreams/latest/dg/API_reader_GetHLSStreamingSessionURL.html#API_reader_GetHLSStreamingSessionURL_RequestSyntax
[업무 이슈] android 안드로이드 aws kvs hls 스트리밍 동영상 업로드 이슈 - mp4 파일 ffmpeg 트랜스코딩 mkv 파일 변환
https://blog.naver.com/kkh0977/223878649342?trackingCode=blog_bloghome_searchlist
[안드로이드 Raw 폴더에 저장 된 MKV 형식 파일 Aws Kvs HLS 비디오 스트리밍 업로드 수행]
https://blog.naver.com/kkh0977/223874458019?trackingCode=blog_bloghome_searchlist
[Web/JavaScript] AWS Kvs HLS 뷰어 Viewer 비디오 스트림 영상 재생 수행
https://blog.naver.com/kkh0977/224004858035
// --------------------------------------------------------------------------------------
728x90
반응형
'Aws (Amazon)' 카테고리의 다른 글
Comments
