Skip to content

Commit

Permalink
fix: emit MediaStateChange on RemoteStream.replaceTrack
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryce Tham committed Mar 11, 2024
1 parent 87fa5da commit d313be4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/media/remote-stream.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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', () => {
Expand Down
8 changes: 8 additions & 0 deletions src/media/remote-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d313be4

Please sign in to comment.