Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #273 from jibranabsarulislam/jibranabsarulislam/ad…
Browse files Browse the repository at this point in the history
…d-CRD-events

Include create, reconnect, and update events (with associated tests)
  • Loading branch information
mcottontensor committed Jun 21, 2023
2 parents ab4bd67 + fd41a63 commit f1edea5
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Frontend/library/src/PixelStreaming/PixelStreaming.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,4 +572,51 @@ describe('PixelStreaming', () => {
expect(responseListenerSpy).toHaveBeenCalledWith(testMessageContents);
});

it('should emit StreamConnectEvent when streamer connects', () => {
const config = new Config({ initialSettings: {ss: mockSignallingUrl}});
const streamConnectSpy = jest.fn();
const pixelStreaming = new PixelStreaming(config);
pixelStreaming.addEventListener("streamConnect", streamConnectSpy);
pixelStreaming.connect();

establishMockedPixelStreamingConnection();

expect(streamConnectSpy).toHaveBeenCalled();
});

it('should emit StreamDisconnectEvent when streamer disconnects', () => {
const config = new Config({ initialSettings: {ss: mockSignallingUrl}});
const streamDisconnectSpy = jest.fn();
const pixelStreaming = new PixelStreaming(config);
pixelStreaming.addEventListener("streamDisconnect", streamDisconnectSpy);
pixelStreaming.connect();

establishMockedPixelStreamingConnection();

expect(streamDisconnectSpy).not.toHaveBeenCalled();

pixelStreaming.disconnect();

expect(streamDisconnectSpy).toHaveBeenCalled();
});

it('should emit StreamReconnectEvent when streamer reconnects', () => {
const config = new Config({ initialSettings: {ss: mockSignallingUrl}});
const streamReconnectSpy = jest.fn();
const pixelStreaming = new PixelStreaming(config);
pixelStreaming.addEventListener("streamReconnect", streamReconnectSpy);
pixelStreaming.connect();

establishMockedPixelStreamingConnection();

expect(streamReconnectSpy).not.toHaveBeenCalled();

pixelStreaming.reconnect();

expect(streamReconnectSpy).toHaveBeenCalled();

pixelStreaming.disconnect();

expect(streamReconnectSpy).toHaveBeenCalledTimes(1);
});
});
6 changes: 6 additions & 0 deletions Frontend/library/src/PixelStreaming/PixelStreaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import {
PixelStreamingEvent,
StatsReceivedEvent,
StreamLoadingEvent,
StreamPreConnectEvent,
StreamReconnectEvent,
StreamPreDisconnectEvent,
VideoEncoderAvgQPEvent,
VideoInitializedEvent,
WebRtcAutoConnectEvent,
Expand Down Expand Up @@ -325,6 +328,7 @@ export class PixelStreaming {
* Connect to signaling server.
*/
public connect() {
this._eventEmitter.dispatchEvent(new StreamPreConnectEvent());
this._webRtcController.connectToSignallingServer();
}

Expand All @@ -333,13 +337,15 @@ export class PixelStreaming {
* before establishing a new connection
*/
public reconnect() {
this._eventEmitter.dispatchEvent(new StreamReconnectEvent());
this._webRtcController.restartStreamAutomatically();
}

/**
* Disconnect from the signaling server and close open peer connections.
*/
public disconnect() {
this._eventEmitter.dispatchEvent(new StreamPreDisconnectEvent());
this._webRtcController.close();
}

Expand Down
33 changes: 33 additions & 0 deletions Frontend/library/src/Util/EventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,36 @@ export class StreamLoadingEvent extends Event {
}
}

/**
* An event that is emitted when video stream loading has finished.
*/
export class StreamPreConnectEvent extends Event {
readonly type: 'streamConnect';
constructor() {
super('streamConnect');
}
}

/**
* An event that is emitted when video stream has stopped.
*/
export class StreamPreDisconnectEvent extends Event {
readonly type: 'streamDisconnect';
constructor() {
super('streamDisconnect');
}
}

/**
* An event that is emitted when video stream is reconnecting.
*/
export class StreamReconnectEvent extends Event {
readonly type: 'streamReconnect';
constructor() {
super('streamReconnect');
}
}

/**
* An event that is emitted if there are errors loading the video stream.
*/
Expand Down Expand Up @@ -457,6 +487,9 @@ export type PixelStreamingEvent =
| DataChannelErrorEvent
| VideoInitializedEvent
| StreamLoadingEvent
| StreamPreConnectEvent
| StreamReconnectEvent
| StreamPreDisconnectEvent
| PlayStreamErrorEvent
| PlayStreamEvent
| PlayStreamRejectedEvent
Expand Down

0 comments on commit f1edea5

Please sign in to comment.