@@ -2,7 +2,7 @@ import { Deferred } from "@scrypted/common/src/deferred";
2
2
import { listenZeroSingleClient } from "@scrypted/common/src/listen-cluster" ;
3
3
import { getNaluTypesInNalu , RtspServer } from "@scrypted/common/src/rtsp-server" ;
4
4
import { createSdpInput , parseSdp } from '@scrypted/common/src/sdp-utils' ;
5
- import sdk , { FFmpegInput , Intercom , MediaObject , MediaStreamUrl , ResponseMediaStreamOptions , RTCAVSignalingSetup , RTCSessionControl , RTCSignalingChannel , RTCSignalingOptions , RTCSignalingSendIceCandidate , RTCSignalingSession , ScryptedMimeTypes } from "@scrypted/sdk" ;
5
+ import sdk , { FFmpegInput , Intercom , MediaObject , MediaStreamUrl , ResponseMediaStreamOptions , RTCAVSignalingSetup , RTCSessionControl , RTCSignalingChannel , RTCSignalingOptions , RTCSignalingSendIceCandidate , RTCSignalingSession , ScryptedMimeTypes , ScryptedNativeId } from "@scrypted/sdk" ;
6
6
import { FullIntraRequest } from "../../../external/werift/packages/rtp/src/rtcp/psfb/fullIntraRequest" ;
7
7
import { logConnectionState , waitClosed , waitConnected , waitIceConnected } from "./peerconnection-util" ;
8
8
import { startRtpForwarderProcess } from "./rtp-forwarders" ;
@@ -13,9 +13,10 @@ import { createRawResponse, getWeriftIceServers, isPeerConnectionAlive, logIsLoc
13
13
const { mediaManager } = sdk ;
14
14
15
15
export interface RTCPeerConnectionPipe {
16
+ __json_copy_serialize_children : true ,
16
17
mediaObject : MediaObject ;
17
- intercom : Promise < Intercom > ;
18
- pcClose : Promise < unknown > ;
18
+ getIntercom ( ) : Promise < Intercom > ;
19
+ pcClose ( ) : Promise < unknown > ;
19
20
}
20
21
21
22
function ignoreDeferred ( ...d : Deferred < any > [ ] ) {
@@ -27,12 +28,14 @@ function ignorePromise(...p: Promise<any>[]) {
27
28
}
28
29
29
30
export async function createRTCPeerConnectionSource ( options : {
30
- console : Console ,
31
+ mixinId : string ,
32
+ nativeId : ScryptedNativeId ,
31
33
mediaStreamOptions : ResponseMediaStreamOptions ,
32
- channel : RTCSignalingChannel ,
34
+ startRTCSignalingSession : ( session : RTCSignalingSession ) => Promise < RTCSessionControl | undefined > ,
33
35
maximumCompatibilityMode : boolean ,
34
36
} ) : Promise < RTCPeerConnectionPipe > {
35
- const { mediaStreamOptions, channel, console, maximumCompatibilityMode } = options ;
37
+ const { mediaStreamOptions, startRTCSignalingSession, mixinId, nativeId, maximumCompatibilityMode } = options ;
38
+ const console = mixinId ? sdk . deviceManager . getMixinConsole ( mixinId , nativeId ) : sdk . deviceManager . getDeviceConsole ( nativeId ) ;
36
39
37
40
const { clientPromise, port } = await listenZeroSingleClient ( ) ;
38
41
@@ -45,9 +48,9 @@ export async function createRTCPeerConnectionSource(options: {
45
48
46
49
const cleanup = ( ) => {
47
50
console . log ( 'webrtc/rtsp cleaning up' ) ;
48
- clientPromise . then ( client => client . destroy ( ) ) . catch ( ( ) => { } ) ;
49
- sessionControl . promise . then ( sc => sc . endSession ( ) ) . catch ( ( ) => { } ) ;
50
- peerConnection . promise . then ( pc => pc . close ( ) ) . catch ( ( ) => { } ) ;
51
+ clientPromise . then ( client => client . destroy ( ) ) . catch ( ( ) => { } ) ;
52
+ sessionControl . promise . then ( sc => sc . endSession ( ) ) . catch ( ( ) => { } ) ;
53
+ peerConnection . promise . then ( pc => pc . close ( ) ) . catch ( ( ) => { } ) ;
51
54
ignorePromise ( intercom . promise . then ( intercom => intercom . stopIntercom ( ) ) ) ;
52
55
} ;
53
56
@@ -74,6 +77,8 @@ export async function createRTCPeerConnectionSource(options: {
74
77
iceServers : getWeriftIceServers ( setup . configuration ) ,
75
78
} ) ;
76
79
80
+ waitClosed ( ret ) . then ( ( ) => cleanup ( ) ) ;
81
+
77
82
logConnectionState ( console , ret ) ;
78
83
peerConnection . resolve ( ret ) ;
79
84
@@ -101,6 +106,8 @@ export async function createRTCPeerConnectionSource(options: {
101
106
let gotVideo = false ;
102
107
103
108
const pc = await peerConnection . promise ;
109
+ const timeout = setTimeout ( ( ) => cleanup ( ) , 2 * 60 * 1000 ) ;
110
+ waitClosed ( pc ) . then ( ( ) => clearTimeout ( timeout ) ) ;
104
111
105
112
const setupAudioTranscevier = ( transciever : RTCRtpTransceiver ) => {
106
113
audioTransceiver = transciever ;
@@ -135,6 +142,7 @@ export async function createRTCPeerConnectionSource(options: {
135
142
track . onReceiveRtcp . subscribe ( rtp => rtspServer . sendTrack ( videoTrack , rtp . serialize ( ) , true ) ) ;
136
143
137
144
track . onReceiveRtp . once ( ( ) => {
145
+ clearTimeout ( timeout ) ;
138
146
let firSequenceNumber = 0 ;
139
147
const pictureLossInterval = setInterval ( ( ) => {
140
148
// i think this is necessary for older clients like ring
@@ -266,7 +274,7 @@ export async function createRTCPeerConnectionSource(options: {
266
274
}
267
275
268
276
const session = new SignalingSession ( ) ;
269
- const sc = await channel . startRTCSignalingSession ( session ) ;
277
+ const sc = await startRTCSignalingSession ( session ) ;
270
278
sessionControl . resolve ( sc ) ;
271
279
console . log ( 'waiting for peer connection' ) ;
272
280
const pc = await peerConnection . promise ;
@@ -351,9 +359,10 @@ export async function createRTCPeerConnectionSource(options: {
351
359
} ;
352
360
353
361
return {
362
+ __json_copy_serialize_children : true ,
354
363
mediaObject : await mediaManager . createMediaObject ( mediaStreamUrl , ScryptedMimeTypes . MediaStreamUrl ) ,
355
- intercom : intercom . promise ,
356
- pcClose,
364
+ getIntercom : ( ) => intercom . promise ,
365
+ pcClose : ( ) => pcClose ,
357
366
} ;
358
367
}
359
368
0 commit comments