Skip to content

Commit

Permalink
Merge pull request video-dev#4308 from albertdaurell/bugfix/ensure-ch…
Browse files Browse the repository at this point in the history
…angetype-called-when-required

Bugfix: Store track info when buffer codec switched
  • Loading branch information
robwalch authored Jan 18, 2022
2 parents 38b5c1d + 1de9ccd commit 9781184
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/controller/buffer-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ export default class BufferController implements ComponentAPI {
// check if SourceBuffer codec needs to change
const track = this.tracks[trackName];
if (track && typeof track.buffer.changeType === 'function') {
const { codec, levelCodec, container } = data[trackName];
const { id, codec, levelCodec, container, metadata } =
data[trackName];
const currentCodec = (track.levelCodec || track.codec).replace(
VIDEO_CODEC_PROFILE_REPACE,
'$1'
Expand All @@ -255,6 +256,17 @@ export default class BufferController implements ComponentAPI {
if (currentCodec !== nextCodec) {
const mimeType = `${container};codecs=${levelCodec || codec}`;
this.appendChangeType(trackName, mimeType);
logger.log(
`[buffer-controller]: switching codec ${currentCodec} to ${nextCodec}`
);
this.tracks[trackName] = {
buffer: track.buffer,
codec,
container,
levelCodec,
metadata,
id,
};
}
}
} else {
Expand Down Expand Up @@ -714,6 +726,7 @@ export default class BufferController implements ComponentAPI {
codec: codec,
container: track.container,
levelCodec: track.levelCodec,
metadata: track.metadata,
id: track.id,
};
tracksCreated++;
Expand Down
66 changes: 66 additions & 0 deletions tests/unit/controller/buffer-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,70 @@ describe('BufferController tests', function () {
expect(createSbStub).to.have.been.calledWith({ audio: {}, video: {} });
});
});

describe('onBufferCodecs', function () {
it('calls changeType if needed and stores current track info', function () {
const getSourceBufferTypes = sandbox
.stub(bufferController, 'getSourceBufferTypes')
.returns(['audio', 'video']);
const appendChangeType = sandbox.stub(
bufferController,
'appendChangeType'
);
const buffer = {
changeType: sandbox.stub(),
};
const originalAudioTrack = {
id: 'main',
codec: 'mp4a.40.2',
levelCodec: undefined,
container: 'audio/mp4',
metadata: {
channelCount: 1,
},
};
const newAudioTrack = {
id: 'main',
codec: 'mp4a.40.5',
levelCodec: undefined,
container: 'audio/mp4',
metadata: {
channelCount: 1,
},
};
bufferController.tracks = {
audio: {
...originalAudioTrack,
buffer,
},
};
bufferController.onBufferCodecs(Events.BUFFER_CODECS, {
audio: newAudioTrack,
});
expect(getSourceBufferTypes).to.have.been.calledOnce;
expect(bufferController.appendChangeType).to.have.been.calledOnce;
expect(bufferController.appendChangeType).to.have.been.calledWith(
'audio',
'audio/mp4;codecs=mp4a.40.5'
);
expect(bufferController.tracks.audio).to.deep.equal({
buffer,
...newAudioTrack,
});

bufferController.onBufferCodecs(Events.BUFFER_CODECS, {
audio: originalAudioTrack,
});
expect(getSourceBufferTypes).to.have.been.calledTwice;
expect(bufferController.appendChangeType).to.have.been.calledTwice;
expect(bufferController.appendChangeType).to.have.been.calledWith(
'audio',
'audio/mp4;codecs=mp4a.40.2'
);
expect(bufferController.tracks.audio).to.deep.equal({
buffer,
...originalAudioTrack,
});
});
});
});

0 comments on commit 9781184

Please sign in to comment.