forked from EpicGamesExt/PixelStreamingInfrastructure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamController.ts
More file actions
79 lines (70 loc) · 3.04 KB
/
StreamController.ts
File metadata and controls
79 lines (70 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright Epic Games, Inc. All Rights Reserved.
import { Logger } from '@epicgames-ps/lib-pixelstreamingcommon-ue5.5';
import { VideoPlayer } from './VideoPlayer';
/**
* Video Player Controller handles the creation of the video HTML element and all handlers
*/
export class StreamController {
videoElementProvider: VideoPlayer;
audioElement: HTMLAudioElement;
/**
* @param videoElementProvider Video Player instance
*/
constructor(videoElementProvider: VideoPlayer) {
this.videoElementProvider = videoElementProvider;
this.audioElement = document.createElement('Audio') as HTMLAudioElement;
this.videoElementProvider.setAudioElement(this.audioElement);
}
/**
* Handles when the Peer connection has a track event
* @param rtcTrackEvent - RTC Track Event
*/
handleOnTrack(rtcTrackEvent: RTCTrackEvent) {
Logger.Info('handleOnTrack ' + JSON.stringify(rtcTrackEvent.streams));
// Do not add the track if the ID is `probator` as this is special track created by mediasoup for bitrate probing.
// Refer to https://github.com/EpicGamesExt/PixelStreamingInfrastructure/pull/86 for more details.
if (rtcTrackEvent.streams.length < 1 || rtcTrackEvent.streams[0].id == 'probator') {
return;
}
const videoElement = this.videoElementProvider.getVideoElement();
if (rtcTrackEvent.track) {
Logger.Info(
'Got track - ' +
rtcTrackEvent.track.kind +
' id=' +
rtcTrackEvent.track.id +
' readyState=' +
rtcTrackEvent.track.readyState
);
}
if (rtcTrackEvent.track.kind == 'audio') {
this.CreateAudioTrack(rtcTrackEvent.streams[0]);
return;
} else if (
rtcTrackEvent.track.kind == 'video' &&
videoElement.srcObject !== rtcTrackEvent.streams[0]
) {
videoElement.srcObject = rtcTrackEvent.streams[0];
this.videoElementProvider.switchToWebRTCStream();
Logger.Info('Set video source from video track ontrack.');
return;
}
}
/**
* Creates the audio device when receiving an RTCTrackEvent with the kind of "audio"
* @param audioMediaStream - Audio Media stream track
*/
CreateAudioTrack(audioMediaStream: MediaStream) {
const videoElement = this.videoElementProvider.getVideoElement();
// do nothing the video has the same media stream as the audio track we have here (they are linked)
if (videoElement.srcObject == audioMediaStream) {
return;
}
// video element has some other media stream that is not associated with this audio track
else if (videoElement.srcObject && videoElement.srcObject !== audioMediaStream) {
// create a new audio element
this.audioElement.srcObject = audioMediaStream;
Logger.Info('Created new audio element to play separate audio stream.');
}
}
}