diff --git a/src/controller/buffer-controller.ts b/src/controller/buffer-controller.ts index d3e32f34fbf..358186f8f1e 100644 --- a/src/controller/buffer-controller.ts +++ b/src/controller/buffer-controller.ts @@ -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' @@ -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 { @@ -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++; diff --git a/tests/unit/controller/buffer-controller.js b/tests/unit/controller/buffer-controller.js index f8f42dc2436..bed804dd31b 100644 --- a/tests/unit/controller/buffer-controller.js +++ b/tests/unit/controller/buffer-controller.js @@ -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, + }); + }); + }); });