-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from Uscreen-video/support-multi-audio-tracks
Support multi audio tracks
- Loading branch information
Showing
8 changed files
with
213 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { VideoContainer } from "../Video-container.component"; | ||
import type Hls from "hls.js"; | ||
import type { MediaPlaylist } from "hls.js"; | ||
import { dispatch, Types } from "../../../state"; | ||
import type { AudiosController } from "./types"; | ||
import _debug from "debug"; | ||
|
||
const audiosDebug = _debug("player:audios"); | ||
|
||
const buildHlsAudioTrackId = (track: MediaPlaylist) => `${track.name}-${track.lang}` | ||
|
||
export const hlsController = (host: VideoContainer, hls: Hls, activeAudioTrackId?: string): AudiosController => { | ||
const enableAudioTrack = (id: string) => { | ||
const newHlsTrackId = hls.audioTracks.find(t => buildHlsAudioTrackId(t) === id)?.id | ||
if (typeof newHlsTrackId === 'number') { | ||
hls.audioTrack = newHlsTrackId | ||
dispatch(host, Types.Action.update, { | ||
activeAudioTrackId: id | ||
}) | ||
audiosDebug('AUDIO TRACK ENABLED', id) | ||
} | ||
} | ||
|
||
if (hls.audioTracks.length > 0) { | ||
dispatch(host, Types.Action.update, { | ||
audioTracks: hls.audioTracks.map((track) => ({ | ||
label: track.name, | ||
lang: track.lang, | ||
id: buildHlsAudioTrackId(track) | ||
})), | ||
activeAudioTrackId: buildHlsAudioTrackId(hls.audioTracks.find(t => t.id === hls.audioTrack) || hls.audioTracks[0]) | ||
}) | ||
|
||
hls.audioTracks.forEach(t => { | ||
audiosDebug('AUDIO TRACK ADDED', t.id, t.name, t.lang) | ||
}) | ||
|
||
if (activeAudioTrackId) { | ||
enableAudioTrack(activeAudioTrackId) | ||
} | ||
} | ||
|
||
return { | ||
enableAudioTrack | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { nativeController } from "./native"; | ||
import { hlsController } from "./hls"; | ||
import type { AudiosController, VideoElementWithAudioTracks } from "./types"; | ||
import type { VideoContainer } from "../Video-container.component"; | ||
import type Hls from "hls.js"; | ||
|
||
export const audiosController = (host: VideoContainer, video: HTMLVideoElement, hls?: Hls, activeAudioTrackId?: string): AudiosController => { | ||
if (hls) { | ||
return hlsController(host, hls, activeAudioTrackId) | ||
} else { | ||
return nativeController(host, video as VideoElementWithAudioTracks, activeAudioTrackId) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { VideoContainer } from "../Video-container.component"; | ||
import debounce from "../../../helpers/debounce" | ||
import { dispatch, Types } from "../../../state" | ||
import type { AudiosController, AudioTrack, VideoElementWithAudioTracks } from "./types" | ||
import _debug from "debug"; | ||
|
||
const audiosDebug = _debug("player:audios"); | ||
|
||
const buildNativeAudioTrackId = (track: AudioTrack) => `${track.label}-${track.language}` | ||
|
||
export const nativeController = (host: VideoContainer, video: VideoElementWithAudioTracks, activeAudioTrackId?: string): AudiosController => { | ||
const enableAudioTrack = (id: string) => { | ||
const newActiveTrack = Array.from(video.audioTracks).find(t => buildNativeAudioTrackId(t) === id) | ||
|
||
if (newActiveTrack) { | ||
for (let i = 0; i < video.audioTracks.length; i++) { | ||
video.audioTracks[i].enabled = buildNativeAudioTrackId(video.audioTracks[i]) === id | ||
} | ||
audiosDebug('AUDIO TRACK ENABLED', id) | ||
} | ||
|
||
} | ||
|
||
const onTrackAdded = debounce(() => { | ||
const audioTracks = Array.from(video.audioTracks) | ||
|
||
dispatch(host, Types.Action.update, { | ||
audioTracks: audioTracks.map((track) => ({ | ||
label: track.label, | ||
lang: track.language, | ||
id: buildNativeAudioTrackId(track) | ||
})), | ||
activeAudioTrackId: buildNativeAudioTrackId(audioTracks.find(t => t.enabled) || audioTracks[0]) | ||
}) | ||
|
||
audioTracks.forEach(t => { | ||
audiosDebug('AUDIO TRACK ADDED', t.label, t.language, t.enabled) | ||
}) | ||
|
||
if (activeAudioTrackId) { | ||
enableAudioTrack(activeAudioTrackId) | ||
} | ||
}, 100) | ||
|
||
const onTracksChanged = debounce(() => { | ||
const audioTracks = Array.from(video.audioTracks) | ||
dispatch(host, Types.Action.update, { | ||
activeAudioTrackId: buildNativeAudioTrackId(audioTracks.find(t => t.enabled) || audioTracks[0]) | ||
}) | ||
}, 100) | ||
|
||
video.audioTracks.addEventListener('addtrack', onTrackAdded) | ||
video.audioTracks.addEventListener('change', onTracksChanged) | ||
|
||
return { | ||
enableAudioTrack | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export type AudiosController = { | ||
enableAudioTrack: (id: string) => void; | ||
} | ||
|
||
export type AudioTrack = { | ||
id: string; | ||
label: string; | ||
language: string; | ||
enabled: boolean; | ||
} | ||
|
||
export type VideoElementWithAudioTracks = HTMLVideoElement & { audioTracks: AudioTrack[] & { addEventListener: (event: string, listener: () => void) => void } } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters