-
Notifications
You must be signed in to change notification settings - Fork 199
/
Context.jslib
265 lines (211 loc) · 10.5 KB
/
Context.jslib
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
var UnityWebRTCContext = {
GetHardwareEncoderSupport: function () {
return true;
},
ContextCreate__deps: ['$UWEncoderType'],
ContextCreate: function (uid, encodeType) {
var context = {
id: uid,
encodeType: UWEncoderType[encodeType]
};
uwcom_addManageObj(context);
return context.managePtr;
},
ContextDestroy: function (uid) {
var contextPtrs = Object.keys(UWManaged).filter(function (contextPtr) {
if ('id' in UWManaged[contextPtr]) {
return UWManaged[contextPtr].id === uid;
} else
return false;
});
if (contextPtrs.length > 1) {
console.error('ContextDestroy: multiple Contexts with the same id');
} else if (!contextPtrs.length) {
console.error('ContextDestroy: There is no context with id = ' + uid.toString());
}
contextPtrs.forEach(function (contextPtr) {
delete UWManaged[contextPtr];
});
},
// TODO
ContextAddRefPtr: function (ptr){
},
// TODO
ContextDeleteRefPtr: function(ptr){
},
// TODO
ContextCreateAudioTrackSource: function(contextPtr){
if (!uwcom_existsCheck(contextPtr, "ContextCreateAudioTrackSource", "context")) return;
const audioTrackSource = {};
uwcom_addManageObj(audioTrackSource);
return audioTrackSource.managePtr;
},
// TODO
ContextCreateVideoTrackSource: function(contextPtr){
if (!uwcom_existsCheck(contextPtr, "ContextCreateVideoTrackSource", "context")) return;
const videoTrackSource = {};
uwcom_addManageObj(videoTrackSource);
return videoTrackSource.managePtr;
},
ContextGetEncoderType: function (contextPtr) {
if (!uwcom_existsCheck(contextPtr, 'ContextGetEncoderType', 'context')) return;
var context = UWManaged[contextPtr];
var encodeTypeIdx = UWEncoderType.indexOf(context.encodeType);
return encodeTypeIdx;
},
ContextCreatePeerConnection: function (contextPtr, conf) {
if (!uwcom_existsCheck(contextPtr, 'ContextCreatePeerConnection', 'context')) return;
return _CreatePeerConnection(conf);
},
ContextCreatePeerConnectionWithConfig: function (contextPtr, confPtr) {
if (!uwcom_existsCheck(contextPtr, 'ContextCreatePeerConnectionWithConfig', 'context')) return;
return _CreatePeerConnectionWithConfig(confPtr);
},
ContextDeletePeerConnection: function (contextPtr, peerPtr) {
//if (!uwcom_existsCheck(contextPtr, 'ContextDeletePeerConnection', 'context')) return;
if (!uwcom_existsCheck(peerPtr, 'ContextDeletePeerConnection', 'peer')) return;
var peer = UWManaged[peerPtr];
if (peer.readyState !== 'closed' || peer.signalingState !== 'closed')
peer.close();
delete UWManaged[peerPtr];
},
PeerConnectionSetLocalDescription: function (contextPtr, peerPtr, typeIdx, sdpPtr) {
if (!uwcom_existsCheck(contextPtr, 'PeerConnectionSetLocalDescription', 'context')) return 11; // OperationErrorWithData
if (!uwcom_existsCheck(peerPtr, 'PeerConnectionSetLocalDescription', 'peer')) return 11; // OperationErrorWithData
return _PeerConnectionSetDescription(peerPtr, typeIdx, sdpPtr, 'Local');
},
PeerConnectionSetRemoteDescription: function (contextPtr, peerPtr, typeIdx, sdpPtr) {
if (!uwcom_existsCheck(contextPtr, 'PeerConnectionSetRemoteDescription', 'context')) return 11; // OperationErrorWithData
if (!uwcom_existsCheck(peerPtr, 'PeerConnectionSetRemoteDescription', 'peer')) return 11; // OperationErrorWithData
return _PeerConnectionSetDescription(peerPtr, typeIdx, sdpPtr, 'Remote');
},
PeerConnectionSetLocalDescriptionWithoutDescription: function (contextPtr, peerPtr) {
if (!uwcom_existsCheck(contextPtr, 'PeerConnectionSetLocalDescriptionWithoutDescription', 'context')) return 11; // OperationErrorWithData
if (!uwcom_existsCheck(peerPtr, 'PeerConnectionSetLocalDescriptionWithoutDescription', 'peer')) return 11; // OperationErrorWithData
return _PeerConnectionSetDescriptionWithoutDescription(peerPtr);
},
PeerConnectionRegisterOnSetSessionDescSuccess: function (contextPtr, peerPtr, OnSetSessionDescSuccess) {
if (!uwcom_existsCheck(contextPtr, 'PeerConnectionRegisterOnSetSessionDescSuccess', 'context')) return;
if (!uwcom_existsCheck(peerPtr, 'PeerConnectionRegisterOnSetSessionDescSuccess', 'peer')) return;
uwevt_OnSetSessionDescSuccess = OnSetSessionDescSuccess;
},
PeerConnectionRegisterOnSetSessionDescFailure: function (contextPtr, peerPtr, OnSetSessionDescFailure) {
if (!uwcom_existsCheck(contextPtr, 'PeerConnectionRegisterOnSetSessionDescFailure', 'context')) return;
if (!uwcom_existsCheck(peerPtr, 'PeerConnectionRegisterOnSetSessionDescFailure', 'peer')) return;
uwevt_OnSetSessionDescFailure = OnSetSessionDescFailure;
},
ContextCreateDataChannel: function (contextPtr, peerPtr, labelPtr, optionsJsonPtr) {
if (!uwcom_existsCheck(contextPtr, 'ContextCreateDataChannel', 'context')) return;
if (!uwcom_existsCheck(peerPtr, 'ContextCreateDataChannel', 'peer')) return;
return _CreateDataChannel(peerPtr, labelPtr, optionsJsonPtr);
},
ContextDeleteDataChannel: function (contextPtr, dataChannelPtr) {
//if (!uwcom_existsCheck(contextPtr, 'ContextDeleteDataChannel', 'context')) return;
if (!uwcom_existsCheck(dataChannelPtr, 'ContextDeleteDataChannel', 'dataChannel')) return;
delete UWManaged[dataChannelPtr];
},
ContextCreateMediaStream: function (contextPtr, labelPtr) {
if (!uwcom_existsCheck(contextPtr, 'ContextCreateMediaStream', 'context')) return;
return _CreateMediaStream(labelPtr);
},
ContextDeleteMediaStream: function (contextPtr, streamPtr) {
//if (!uwcom_existsCheck(contextPtr, 'ContextDeleteMediaStream', 'context')) return;
if (!uwcom_existsCheck(streamPtr, 'ContextDeleteMediaStream', 'stream')) return;
_DeleteMediaStream(streamPtr);
},
ContextRegisterMediaStreamObserver: function (contextPtr, streamPtr) {
if (!uwcom_existsCheck(contextPtr, 'MediaStreamRegisterOnAddTrack', 'context')) return;
if (!uwcom_existsCheck(streamPtr, 'MediaStreamRegisterOnAddTrack', 'stream')) return;
var stream = UWManaged[streamPtr];
stream.onaddtrack = (function(evt) {
uwcom_addManageObj(evt.track);
Module.dynCall_vii(uwevt_MSOnAddTrack, stream.managePtr, evt.track.managePtr);
});
stream.onremovetrack = (function(evt) {
if (!uwcom_existsCheck(evt.track.managePtr, "stream.onremovetrack", "track")) return;
Module.dynCall_vii(uwevt_MSOnRemoveTrack, stream.managePtr, evt.track.managePtr);
});
},
ContextUnRegisterMediaStreamObserver: function (contextPtr, streamPtr) {
},
MediaStreamRegisterOnAddTrack: function (contextPtr, streamPtr, MediaStreamOnAddTrack) {
if (!uwcom_existsCheck(contextPtr, 'MediaStreamRegisterOnAddTrack', 'context')) return;
if (!uwcom_existsCheck(streamPtr, 'MediaStreamRegisterOnAddTrack', 'stream')) return;
uwevt_MSOnAddTrack = MediaStreamOnAddTrack;
},
MediaStreamRegisterOnRemoveTrack: function (contextPtr, streamPtr, MediaStreamOnRemoveTrack) {
if (!uwcom_existsCheck(contextPtr, 'MediaStreamRegisterOnRemoveTrack', 'context')) return;
if (!uwcom_existsCheck(streamPtr, 'MediaStreamRegisterOnRemoveTrack', 'stream')) return;
uwevt_MSOnRemoveTrack = MediaStreamOnRemoveTrack;
},
GetRenderEventFunc: function (contextPtr) {
},
GetUpdateTextureFunc: function (contextPtr) {
},
ContextCreateAudioTrack: function (contextPtr, labelPtr, sourcePtr) {
if (!uwcom_existsCheck(contextPtr, 'ContextCreateAudioTrack', 'context')) return;
return _CreateAudioTrack(labelPtr, sourcePtr);
},
ContextCreateVideoTrack: function (contextPtr, srcTexturePtr, dstTexturePtr, width, height) {
if (!uwcom_existsCheck(contextPtr, 'ContextCreateVideoTrack', 'context')) return;
return _CreateVideoTrack(srcTexturePtr, dstTexturePtr, width, height);
},
ContextStopMediaStreamTrack: function (contextPtr, trackPtr) {
if (!uwcom_existsCheck(contextPtr, 'ContextStopMediaStreamTrack', 'context')) return;
if (!uwcom_existsCheck(trackPtr, 'ContextStopMediaStreamTrack', 'track')) return;
var track = UWManaged[trackPtr];
track.stop();
},
ContextDeleteMediaStreamTrack: function (contextPtr, trackPtr) {
if (!uwcom_existsCheck(trackPtr, 'ContextDeleteMediaStreamTrack', 'track')) return;
var track = UWManaged[trackPtr];
// Not sure how js garbage collection works, remove/disable/stop all attributes inside the track object?
if(track.kind === "video"){
if(uwcom_localVideoTracks[trackPtr]){
delete uwcom_localVideoTracks[trackPtr];
}
if(uwcom_remoteVideoTracks[trackPtr]){
uwcom_remoteVideoTracks[trackPtr].video.remove();
uwcom_remoteVideoTracks[trackPtr].track.stop();
delete uwcom_remoteVideoTracks[trackPtr];
}
}
delete UWManaged[trackPtr];
},
ContextRegisterAudioReceiveCallback: function (contextPtr, trackPtr, AudioTrackOnReceive){
},
ContextUnregisterAudioReceiveCallback: function (contextPtr, trackPtr){
},
// CreateVideoRenderer: function(contextPtr) {
// },
// DeleteVideoRenderer: function(contextPtr, sinkPtr) {
// },
ContextDeleteStatsReport: function (contextPtr, reportPtr) {
//if (!uwcom_existsCheck(contextPtr, 'ContextDeleteStatsReport', 'context')) return;
if (!uwcom_existsCheck(reportPtr, 'ContextDeleteStatsReport', 'report')) return;
delete UWManaged[reportPtr];
},
// ContextSetVideoEncoderParameter: function(trackPtr, width, height, format, texturePtr) {
// },
// GetInitializationResult: function(contextPtr, trackPtr) {
// },
$UWContextGetCapabilities: function (senderReceiver, kindIdx) {
var kind = UWMediaStreamTrackKind[kindIdx];
var capabilities = {codecs:[], headerExtensions: []};
const supportsSetCodecPreferences = window.RTCRtpTransceiver && 'setCodecPreferences' in window.RTCRtpTransceiver.prototype;
if(supportsSetCodecPreferences) capabilities = senderReceiver.getCapabilities(kind);
var capabilitiesJson = JSON.stringify(capabilities);
var capabilitiesJsonPtr = uwcom_strToPtr(capabilitiesJson);
return capabilitiesJsonPtr;
},
ContextGetSenderCapabilities: function (contextPtr, kindIdx) {
if (!uwcom_existsCheck(contextPtr, 'ContextGetSenderCapabilities', 'context')) return;
return UWContextGetCapabilities(RTCRtpSender, kindIdx);
},
ContextGetReceiverCapabilities: function (contextPtr, kindIdx) {
if (!uwcom_existsCheck(contextPtr, 'ContextGetReceiverCapabilities', 'context')) return;
return UWContextGetCapabilities(RTCRtpReceiver, kindIdx);
}
};
autoAddDeps(UnityWebRTCContext, '$UWContextGetCapabilities');
mergeInto(LibraryManager.library, UnityWebRTCContext);