Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Support MultiTrack Audio #199

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions d.ts/src/player/mse-player.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ declare class MSEPlayer {
unload(): void;
play(): Promise<void>;
pause(): void;
selectAudioTrack(index: number): void;
get type(): string;
get buffered(): TimeRanges;
get duration(): number;
Expand Down
1 change: 1 addition & 0 deletions d.ts/src/player/player-engine-dedicated-thread.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ declare class PlayerEngineDedicatedThread implements PlayerEngine {
play(): Promise<void>;
pause(): void;
seek(seconds: number): void;
selectAudioTrack(track: number): void;
get mediaInfo(): MediaInfo;
get statisticsInfo(): any;
_onLoggingConfigChanged(config: any): void;
Expand Down
1 change: 1 addition & 0 deletions d.ts/src/player/player-engine-main-thread.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ declare class PlayerEngineMainThread implements PlayerEngine {
play(): Promise<void>;
pause(): void;
seek(seconds: number): void;
selectAudioTrack(track: number): void;
get mediaInfo(): MediaInfo;
get statisticsInfo(): any;
private _onMSESourceOpen;
Expand Down
1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ interface Player {
unload(): void;
play(): Promise<void>;
pause(): void;
selectAudioTrack(track: number): void;
type: string;
buffered: TimeRanges;
duration: number;
Expand Down
1 change: 1 addition & 0 deletions src/core/mse-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ class MSEController {
return;
}
} else {
this._sourceBuffers[is.type].changeType(mimeType);
Log.v(this.TAG, `Notice: ${is.type} mimeType changed, origin: ${this._mimeTypes[is.type]}, target: ${mimeType}`);
}
this._mimeTypes[is.type] = mimeType;
Expand Down
8 changes: 8 additions & 0 deletions src/core/transmuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ class Transmuxer {
}
}

selectAudioTrack(track) {
if (this._worker) {
this._worker.postMessage({cmd: 'select_audio_track', param: track});
} else {
this._controller.selectAudioTrack(track);
}
}

_onInitSegment(type, initSegment) {
// do async invoke
Promise.resolve().then(() => {
Expand Down
31 changes: 26 additions & 5 deletions src/core/transmuxing-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ class TransmuxingController {
this._emitter.removeListener(event, listener);
}

start() {
this._loadSegment(0);
start(optionalFrom) {
this._loadSegment(0, optionalFrom);
this._enableStatisticsReporter();
}

Expand All @@ -134,10 +134,10 @@ class TransmuxingController {
ioctl.onRedirect = this._onIORedirect.bind(this);
ioctl.onRecoveredEarlyEof = this._onIORecoveredEarlyEof.bind(this);

if (optionalFrom) {
this._demuxer.bindDataSource(this._ioctl);
} else {
if (typeof optionalFrom === 'undefined') {
ioctl.onDataArrival = this._onInitChunkArrival.bind(this);
} else {
ioctl.onDataArrival = this._onExistsChunkArrival.bind(this);
}

ioctl.open(optionalFrom);
Expand Down Expand Up @@ -220,6 +220,19 @@ class TransmuxingController {
this._enableStatisticsReporter();
}

selectAudioTrack(track) {
if (this._config.isLive) {
this._demuxer.selectAudioTrack(track);
} else { // FIXME: Seek needed?
this.stop();
this._remuxer.insertDiscontinuity();
this._demuxer.resetMediaInfo();
this._demuxer._firstParse = true;
this._demuxer.selectAudioTrack(track);
this.start(0);
}
}

_searchSegmentIndexContains(milliseconds) {
let segments = this._mediaDataSource.segments;
let idx = segments.length - 1;
Expand All @@ -233,6 +246,14 @@ class TransmuxingController {
return idx;
}

_onExistsChunkArrival(data, byteStart) {
// IOController seeked immediately after opened, byteStart > 0 callback may received
this._demuxer.bindDataSource(this._ioctl);
this._demuxer.timestampBase = this._mediaDataSource.segments[this._currentSegmentIndex].timestampBase;

return this._demuxer.parseChunks(data, byteStart);
}

_onInitChunkArrival(data, byteStart) {
let consumed = 0;

Expand Down
5 changes: 4 additions & 1 deletion src/core/transmuxing-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ let TransmuxingWorker = function (self) {
self.addEventListener('message', function (e) {
switch (e.data.cmd) {
case 'init':
controller = new TransmuxingController(e.data.param[0], e.data.param[1]);
controller = new TransmuxingController(e.data.param[0], e.data.param[1], e.data.param[2]);
controller.on(TransmuxingEvents.IO_ERROR, onIOError.bind(this));
controller.on(TransmuxingEvents.DEMUX_ERROR, onDemuxError.bind(this));
controller.on(TransmuxingEvents.INIT_SEGMENT, onInitSegment.bind(this));
Expand Down Expand Up @@ -88,6 +88,9 @@ let TransmuxingWorker = function (self) {
case 'resume':
controller.resume();
break;
case 'select_audio_track':
controller.selectAudioTrack(e.data.param);
break;
case 'logging_config': {
let config = e.data.param;
LoggingControl.applyConfig(config);
Expand Down
Loading