-
Notifications
You must be signed in to change notification settings - Fork 689
Expand file tree
/
Copy pathStartStreamingButton.tsx
More file actions
348 lines (308 loc) · 12.3 KB
/
Copy pathStartStreamingButton.tsx
File metadata and controls
348 lines (308 loc) · 12.3 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import React, { useEffect, useState, useCallback, useMemo } from 'react';
import cx from 'classnames';
import { EStreamingState } from 'services/streaming';
import { EGlobalSyncStatus } from 'services/media-backup';
import { $t } from 'services/i18n';
import { useVuex } from '../hooks';
import { Services } from '../service-provider';
import * as remote from '@electron/remote';
import { TStreamShiftStatus } from 'services/restream';
import { promptAction } from 'components-react/modals';
import { IStreamShiftRequested, IStreamShiftActionCompleted } from 'services/websocket';
export default function StartStreamingButton(p: { disabled?: boolean }) {
const {
StreamingService,
StreamSettingsService,
UserService,
CustomizationService,
MediaBackupService,
SourcesService,
RestreamService,
UsageStatisticsService,
} = Services;
const {
streamingStatus,
delayEnabled,
delaySeconds,
streamShiftStatus,
isDualOutputMode,
isLoggedIn,
isPrime,
primaryPlatform,
isMultiplatformMode,
updateStreamInfoOnLive,
} = useVuex(() => ({
streamingStatus: StreamingService.state.streamingStatus,
delayEnabled: StreamingService.views.delayEnabled,
delaySeconds: StreamingService.views.delaySeconds,
streamShiftStatus: RestreamService.state.streamShiftStatus,
isDualOutputMode: StreamingService.views.isDualOutputMode,
isLoggedIn: UserService.isLoggedIn,
isPrime: UserService.state.isPrime,
primaryPlatform: UserService.state.auth?.primaryPlatform,
isMultiplatformMode: StreamingService.views.isMultiplatformMode,
updateStreamInfoOnLive: CustomizationService.state.updateStreamInfoOnLive,
}));
const [delaySecondsRemaining, setDelayTick] = useState(delaySeconds);
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
setDelayTick(delaySeconds);
}, [streamingStatus]);
useEffect(() => {
if (
delayEnabled &&
delaySecondsRemaining > 0 &&
(streamingStatus === EStreamingState.Starting || streamingStatus === EStreamingState.Ending)
) {
const interval = window.setTimeout(() => {
setDelayTick(delaySecondsRemaining - 1);
}, 1000);
return () => {
clearTimeout(interval);
};
}
}, [delaySecondsRemaining, streamingStatus, delayEnabled]);
useEffect(() => {
if (!isDualOutputMode && isPrime && streamingStatus === EStreamingState.Offline) {
fetchStreamShiftStatus();
}
const streamShiftEvent = StreamingService.streamShiftEvent.subscribe(
(event: IStreamShiftRequested | IStreamShiftActionCompleted) => {
const { streamShiftStreamId } = RestreamService.state;
console.debug('Event ID: ' + event.data.identifier, '\n Stream ID: ' + streamShiftStreamId);
const isFromOtherDevice =
streamShiftStreamId && event.data.identifier !== streamShiftStreamId;
const isMobileRemote = isFromOtherDevice ? /[A-Z]/.test(event.data.identifier) : false;
const remoteDeviceType = isMobileRemote ? 'mobile' : 'desktop';
// Note: because the event's stream id is from the device that requested the switch,
// it is not possible to know what type of device the stream will be switching from.
// We can only identify the type of device the stream is switching to.
const switchType = `desktop-${remoteDeviceType}`;
if (event.type === 'streamSwitchRequest') {
if (!isFromOtherDevice) {
// Don't record the request from this device because the other device will record it
RestreamService.actions.confirmStreamShift('approved');
} else {
UsageStatisticsService.recordAnalyticsEvent('StreamShift', {
stream: switchType,
action: 'request',
});
}
}
if (event.type === 'switchActionComplete') {
// End the stream on this device if switching the stream to another device
// Only record analytics if the stream was switched from this device to a different one
if (isFromOtherDevice) {
Services.RestreamService.actions.endStreamShiftStream(event.data.identifier);
UsageStatisticsService.recordAnalyticsEvent('StreamShift', {
stream: switchType,
action: 'complete',
});
}
// Notify the user
const message = isFromOtherDevice
? $t(
'Your stream has been successfully switched to Streamlabs Desktop. Enjoy your stream!',
)
: $t(
'Your stream has been switched to Streamlabs Desktop from another device. Enjoy your stream!',
);
promptAction({
title: $t('Stream successfully switched'),
message,
btnText: $t('Close'),
btnType: 'default',
cancelBtnPosition: 'none',
});
}
},
);
return () => {
streamShiftEvent.unsubscribe();
};
}, []);
const toggleStreaming = useCallback(async () => {
if (StreamingService.isStreaming) {
StreamingService.toggleStreaming();
} else {
// Check if the scene collection has completed loading and syncing
if (MediaBackupService.views.globalSyncStatus === EGlobalSyncStatus.Syncing) {
const goLive = await remote.dialog
.showMessageBox(remote.getCurrentWindow(), {
title: $t('Cloud Backup'),
type: 'warning',
message:
$t('Your media files are currently being synced with the cloud. ') +
$t('It is recommended that you wait until this finishes before going live.'),
buttons: [$t('Wait'), $t('Go Live Anyway')],
})
.then(({ response }) => !!response);
if (!goLive) return;
}
const needToShowNoSourcesWarning =
StreamSettingsService.settings.warnNoVideoSources &&
SourcesService.views.getSources().filter(source => source.type !== 'scene' && source.video)
.length === 0;
if (needToShowNoSourcesWarning) {
const goLive = await remote.dialog
.showMessageBox(remote.getCurrentWindow(), {
title: $t('No Sources'),
type: 'warning',
message:
// tslint:disable-next-line prefer-template
$t(
"It looks like you haven't added any video sources yet, so you will only be outputting a black screen.",
) +
' ' +
$t('Are you sure you want to do this?') +
'\n\n' +
$t('You can add sources by clicking the + icon near the Sources box at any time'),
buttons: [$t('Cancel'), $t('Go Live Anyway')],
})
.then(({ response }) => !!response);
if (!goLive) return;
}
// Only check for Stream Shift for ultra users
if (isLoggedIn && isPrime) {
setIsLoading(true);
const isLive = await fetchStreamShiftStatus();
setIsLoading(false);
const message = isDualOutputMode
? $t(
'A stream on another device has been detected. Would you like to switch your stream to Streamlabs Desktop? If you do not wish to continue this stream, please end it from the current streaming source. Dual Output will be disabled since not supported in this mode. If you\'re sure you\'re not live and it has been incorrectly detected, choose "Force Start" below.',
)
: $t(
'A stream on another device has been detected. Would you like to switch your stream to Streamlabs Desktop? If you do not wish to continue this stream, please end it from the current streaming source. If you\'re sure you\'re not live and it has been incorrectly detected, choose "Force Start" below.',
);
if (isLive) {
const { streamShiftForceGoLive } = RestreamService.state;
let shouldForceGoLive = streamShiftForceGoLive;
await promptAction({
title: $t('Another stream detected'),
message,
btnText: $t('Switch to Streamlabs Desktop'),
fn: startStreamShift,
cancelBtnText: $t('Cancel'),
cancelBtnPosition: 'left',
secondaryActionText: $t('Force Start'),
secondaryActionFn: async () => {
// FIXME: this should actually do something server-side
RestreamService.actions.return.forceStreamShiftGoLive(true);
shouldForceGoLive = true;
},
});
if (!shouldForceGoLive) {
return;
}
}
}
if (shouldShowGoLiveWindow()) {
if (!StreamingService.views.hasPendingChecks()) {
StreamingService.actions.resetInfo();
}
StreamingService.actions.showGoLiveWindow();
} else {
StreamingService.actions.goLive();
}
}
}, []);
const getIsRedButton = useMemo(
() => streamingStatus !== EStreamingState.Offline && streamShiftStatus !== 'pending',
[streamingStatus],
);
const isDisabled = useMemo(() => {
return (
p.disabled ||
(streamingStatus === EStreamingState.Starting && delaySecondsRemaining === 0) ||
(streamingStatus === EStreamingState.Ending && delaySecondsRemaining === 0)
);
}, [p.disabled, streamingStatus, delaySecondsRemaining]);
const fetchStreamShiftStatus = useCallback(async () => {
try {
const isLive = await RestreamService.checkIsLive();
return isLive;
} catch (e: unknown) {
console.error('Error checking stream shift status', e);
setIsLoading(false);
return false;
}
}, []);
const startStreamShift = useCallback(() => {
if (isDualOutputMode) {
Services.DualOutputService.actions.toggleDisplay(false, 'vertical');
}
StreamingService.actions.goLive();
}, [isDualOutputMode]);
const shouldShowGoLiveWindow = useCallback(() => {
if (!UserService.isLoggedIn) return false;
const primaryPlatform = UserService.state.auth?.primaryPlatform;
const updateStreamInfoOnLive = CustomizationService.state.updateStreamInfoOnLive;
if (!primaryPlatform) return false;
if (StreamingService.views.isDualOutputMode) {
return true;
}
if (
!!UserService.state.auth?.platforms &&
StreamingService.views.isMultiplatformMode &&
Object.keys(UserService.state.auth?.platforms).length > 1
) {
return true;
}
if (primaryPlatform === 'twitch') {
// For Twitch, we can show the Go Live window even with protected mode off
// This is mainly for legacy reasons.
return StreamingService.views.isMultiplatformMode || updateStreamInfoOnLive;
} else {
return (
StreamSettingsService.state.protectedModeEnabled &&
StreamSettingsService.isSafeToModifyStreamKey()
);
}
}, [primaryPlatform, isMultiplatformMode, updateStreamInfoOnLive]);
return (
<button
style={{ minWidth: '130px' }}
className={cx('button button--action', { 'button--soft-warning': getIsRedButton })}
disabled={isDisabled}
onClick={toggleStreaming}
data-name="StartStreamingButton"
>
{isLoading ? (
<i className="fa fa-spinner fa-pulse" />
) : (
<StreamButtonLabel
streamingStatus={streamingStatus}
delayEnabled={delayEnabled}
delaySecondsRemaining={delaySecondsRemaining}
streamShiftStatus={streamShiftStatus}
/>
)}
</button>
);
}
function StreamButtonLabel(p: {
streamingStatus: EStreamingState;
streamShiftStatus: TStreamShiftStatus;
delaySecondsRemaining: number;
delayEnabled: boolean;
}) {
const label = useMemo(() => {
if (p.streamShiftStatus === 'pending') {
return $t('Claim Stream');
}
switch (p.streamingStatus) {
case EStreamingState.Live:
return $t('End Stream');
case EStreamingState.Starting:
return p.delayEnabled ? `Starting ${p.delaySecondsRemaining}s` : $t('Starting');
case EStreamingState.Ending:
return p.delayEnabled ? `Discard ${p.delaySecondsRemaining}s` : $t('Ending');
case EStreamingState.Reconnecting:
return $t('Reconnecting');
case EStreamingState.Offline:
default:
return $t('Go Live');
}
}, [p.streamShiftStatus, p.streamingStatus, p.delayEnabled, p.delaySecondsRemaining]);
return <>{label}</>;
}