diff --git a/src/media/remote-stream.spec.ts b/src/media/remote-stream.spec.ts index d61785c..8400d10 100644 --- a/src/media/remote-stream.spec.ts +++ b/src/media/remote-stream.spec.ts @@ -1,5 +1,5 @@ import { createMockedStream } from '../util/test-utils'; -import { RemoteStream } from './remote-stream'; +import { RemoteMediaState, RemoteStream, RemoteStreamEventNames } from './remote-stream'; describe('RemoteStream', () => { const mockStream = createMockedStream(); @@ -63,6 +63,46 @@ describe('RemoteStream', () => { expect(addEventListenerSpy).toHaveBeenCalledWith('mute', expect.anything()); expect(addEventListenerSpy).toHaveBeenCalledWith('unmute', expect.anything()); }); + + it('should emit MediaStateChange event if new track is muted but old track is not', () => { + expect.assertions(2); + + // Set old track to be unmuted. + Object.defineProperty(mockStream.getTracks()[0], 'muted', { + value: false, + configurable: true, + }); + + // Set new track to be muted. + const newTrack = new MediaStreamTrack(); + Object.defineProperty(newTrack, 'muted', { value: true }); + + const emitSpy = jest.spyOn(remoteStream[RemoteStreamEventNames.MediaStateChange], 'emit'); + remoteStream.replaceTrack(newTrack); + + expect(emitSpy).toHaveBeenCalledTimes(1); + expect(emitSpy).toHaveBeenCalledWith(RemoteMediaState.Stopped); + }); + + it('should emit MediaStateChange event if old track is muted but new track is not', () => { + expect.assertions(2); + + // Set old track to be muted. + Object.defineProperty(mockStream.getTracks()[0], 'muted', { + value: true, + configurable: true, + }); + + // Set new track to be unmuted. + const newTrack = new MediaStreamTrack(); + Object.defineProperty(newTrack, 'muted', { value: false }); + + const emitSpy = jest.spyOn(remoteStream[RemoteStreamEventNames.MediaStateChange], 'emit'); + remoteStream.replaceTrack(newTrack); + + expect(emitSpy).toHaveBeenCalledTimes(1); + expect(emitSpy).toHaveBeenCalledWith(RemoteMediaState.Started); + }); }); describe('stop', () => { diff --git a/src/media/remote-stream.ts b/src/media/remote-stream.ts index c8be98b..23f78f2 100644 --- a/src/media/remote-stream.ts +++ b/src/media/remote-stream.ts @@ -104,6 +104,14 @@ class _RemoteStream extends Stream { this.outputStream.addTrack(newTrack); this.addTrackHandlers(newTrack); + if (oldTrack.muted !== newTrack.muted) { + if (newTrack.muted) { + this.handleMediaStopped(); + } else { + this.handleMediaStarted(); + } + } + // TODO: Chrome/React may not automatically refresh the media element with the new track when // the output track has changed, so we may need to emit an event here if this is the case. // this[StreamEventNames.OutputTrackChange].emit(newTrack);