-
Notifications
You must be signed in to change notification settings - Fork 14
/
AppStream.tsx
258 lines (230 loc) · 9 KB
/
AppStream.tsx
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: LicenseRef-NvidiaProprietary
*
* NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
* property and proprietary rights in and to this material, related
* documentation and any modifications thereto. Any use, reproduction,
* disclosure or distribution of this material and related documentation
* without an express license agreement from NVIDIA CORPORATION or
* its affiliates is strictly prohibited.
*/
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { AppStreamer, StreamEvent, StreamProps, RagnarokConfig, GFNConfig } from '@nvidia/omniverse-webrtc-streaming-library';
import StreamConfig from '../stream.config.json';
interface AppStreamProps {
sessionId: string
backendUrl: string
signalingserver: string
signalingport: number
mediaserver: string
mediaport: number
accessToken: string
style?: React.CSSProperties;
onStarted: () => void;
onLoggedIn: (userId: string) => void;
handleCustomEvent: (event: any) => void;
onFocus: () => void;
onBlur: () => void;
}
interface AppStreamState {
streamReady: boolean;
}
export default class AppStream extends Component<AppStreamProps, AppStreamState> {
private _requested: boolean;
static defaultProps = {
style: {}
};
static propTypes = {
onStarted: PropTypes.func.isRequired,
handleCustomEvent: PropTypes.func.isRequired,
style: PropTypes.object
};
constructor(props: AppStreamProps) {
super(props);
this._requested = false;
this.state = {
streamReady: false
};
}
componentDidMount() {
if (!this._requested) {
this._requested = true;
let streamProps: StreamProps;
let streamConfig: RagnarokConfig | GFNConfig;
let streamSource: 'gfn' | 'direct';
if (StreamConfig.source === 'gfn') {
streamSource = 'gfn'
streamConfig = {
//@ts-ignore
GFN : GFN,
catalogClientId : StreamConfig.gfn.catalogClientId,
clientId : StreamConfig.gfn.clientId,
cmsId : StreamConfig.gfn.cmsId,
onUpdate : (message: StreamEvent) => this._onUpdate(message),
onStart : (message: StreamEvent) => this._onStart(message),
onCustomEvent : (message: any) => this._onCustomEvent(message)
}
}
else if (StreamConfig.source === 'local') {
streamSource = 'direct';
streamConfig = {
videoElementId: 'remote-video',
audioElementId: 'remote-audio',
authenticate: false,
maxReconnects: 20,
server: StreamConfig.local.server,
nativeTouchEvents: true,
width: 1920,
height: 1080,
fps: 60,
onUpdate: (message: StreamEvent) => this._onUpdate(message),
onStart: (message: StreamEvent) => this._onStart(message),
onCustomEvent: (message: any) => this._onCustomEvent(message),
onStop: (message: StreamEvent) => { console.log(message) },
onTerminate: (message: StreamEvent) => { console.log(message) }
};
}
else if (StreamConfig.source === 'stream') {
streamSource = 'direct'
streamConfig = {
signalingserver: this.props.signalingserver,
signalingport: this.props.signalingport,
mediaserver: this.props.mediaserver,
mediaport: this.props.mediaport,
backendurl: this.props.backendUrl,
sessionid: this.props.sessionId,
autolaunch: true,
cursor: 'free',
mic: false,
videoElementId: 'remote-video',
audioElementId: 'remote-audio',
authenticate: false,
maxReconnects: 20,
nativeTouchEvents: true,
width: 1920,
height: 1080,
fps: 60,
onUpdate: (message: StreamEvent) => this._onUpdate(message),
onStart: (message: StreamEvent) => this._onStart(message),
onCustomEvent: (message: any) => this._onCustomEvent(message),
onStop: (message: StreamEvent) => { console.log(message) },
onTerminate: (message: StreamEvent) => { console.log(message) },
};
}
else {
console.error(`Unknown stream source: ${StreamConfig.source}`);
return
}
try {
streamProps = {streamConfig, streamSource}
AppStreamer.connect(streamProps)
.then((result: StreamEvent) => {
console.info(result);
})
.catch((error: StreamEvent) => {
console.error(error);
});
}
catch (error) {
console.error(error);
}
}
}
componentDidUpdate(_prevProps: AppStreamProps, prevState: AppStreamState, _snapshot: any) {
if (prevState.streamReady === false && this.state.streamReady === true) {
const player = document.getElementById("gfn-stream-player-video") as HTMLVideoElement;
if (player) {
if (StreamConfig.source === "gfn")
{
player.style.position = "relative";
const container = document.getElementById("gfn-stream-player-video-container") as HTMLVideoElement;
container.style.background = "white";
}
player.tabIndex = -1;
player.playsInline = true;
player.muted = true;
player.play();
}
}
}
static sendMessage(message: any) {
AppStreamer.sendMessage(message);
}
static stop() {
AppStreamer.stop();
(AppStreamer as any)._stream = null; // Accessing a private member
}
_onStart(message: any) {
if (message.action === 'start' && message.status === 'success' && !this.state.streamReady) {
console.info('streamReady');
this.setState({ streamReady: true });
this.props.onStarted();
}
}
_onUpdate(message: any) {
try {
if (message.action === 'authUser' && message.status === 'success') {
this.props.onLoggedIn(message.info);
}
} catch (error) {
console.error(message);
}
}
_onCustomEvent(message: any) {
this.props.handleCustomEvent(message);
}
_onStop(message: any) {
console.info('Stream stopped', message);
}
_onTerminate(message: any) {
console.info('Stream terminated', message);
}
render() {
const source = StreamConfig.source;
if (source === 'gfn') {
return (
<div
id="view"
style={{
backgroundColor: this.state.streamReady ? 'white': '#dddddd',
display: 'flex', justifyContent: 'space-between',
height: "100%",
width: "100%",
...this.props.style
}}
/>
);
} else if (source === 'local' || source === 'stream') {
return (
<div
key={'stream-canvas'}
id={'main-div'}
style={{
backgroundColor:this.state.streamReady ? 'white': '#dddddd',
visibility: this.state.streamReady ? 'visible' : 'hidden',
...this.props.style
}}
>
<video
key={'video-canvas'}
id={'remote-video'}
style={{
left: 0,
top: 0,
width: '100%',
height: '100%',
}}
tabIndex={-1}
playsInline muted
autoPlay
/>
<audio id="remote-audio" muted></audio>
<h3 style={{ visibility: 'hidden' }} id="message-display">...</h3>
</div>
);
}
return null;
}
}