@@ -32,14 +32,20 @@ const initialRtcConfiguration = {
32
32
iceCandidatePoolSize : 1 ,
33
33
} as RTCConfiguration ;
34
34
35
- export type CallState = "connecting" | "ringing" | "in-call" ;
35
+ export type CallState =
36
+ | "answerIncomingCallPrompt"
37
+ | "connecting"
38
+ | "ringing"
39
+ | "in-call" ;
36
40
37
41
export class CallsManager {
38
42
private peerConnection : RTCPeerConnection ;
39
43
private setIceServersPromise : Promise < void > ;
40
44
private state : CallState ;
41
45
static initialState = "connecting" as const ;
42
46
47
+ resolveCallAcceptedPromise ?: ( accepted : boolean ) => void ;
48
+
43
49
private iceTricklingDataChannel : RTCDataChannel ;
44
50
/**
45
51
* Stores local ICE candidates to be sent to the remote peer
@@ -142,6 +148,28 @@ export class CallsManager {
142
148
if ( hash === "startCall" ) {
143
149
console . log ( "URL hash CMD: " , hash ) ;
144
150
await this . startCall ( ) ;
151
+ } else if ( hash . startsWith ( "promptThenAcceptCall=" ) ) {
152
+ this . state = "answerIncomingCallPrompt" ;
153
+ this . onStateChanged ( this . state ) ;
154
+
155
+ const offer = window . atob ( hash . split ( "promptThenAcceptCall=" , 2 ) [ 1 ] ) ;
156
+ console . log ( "URL hash CMD: promptThenAcceptCall:" , offer ) ;
157
+
158
+ const accepted = await new Promise < boolean > ( ( r ) => {
159
+ this . resolveCallAcceptedPromise = r ;
160
+ } ) ;
161
+ console . log (
162
+ "Accept call prompt resolved, " +
163
+ ( accepted ? "accepted" : "rejected" ) ,
164
+ ) ;
165
+ if ( ! accepted ) {
166
+ this . endCall ( ) ;
167
+ } else {
168
+ this . state = "connecting" ;
169
+ this . onStateChanged ( this . state ) ;
170
+
171
+ await onIncomingCall ( offer ) ;
172
+ }
145
173
} else if ( hash . startsWith ( "acceptCall=" ) ) {
146
174
const offer = window . atob ( hash . substring ( 11 ) ) ;
147
175
console . log ( "URL hash CMD: acceptCall:" , offer ) ;
@@ -178,9 +206,18 @@ export class CallsManager {
178
206
this . onStateChanged ( this . state ) ;
179
207
}
180
208
209
+ // TODO maybe just make this `undefined` if there is no need to accept it?
210
+ acceptCall ( ) {
211
+ this . resolveCallAcceptedPromise ?.( true ) ;
212
+ this . resolveCallAcceptedPromise = undefined ;
213
+ }
214
+
181
215
async endCall ( ) : Promise < void > {
182
216
this . peerConnection . close ( ) ;
183
217
window . calls . endCall ( ) ;
218
+
219
+ this . resolveCallAcceptedPromise ?.( false ) ;
220
+ this . resolveCallAcceptedPromise = undefined ;
184
221
}
185
222
186
223
getState ( ) {
0 commit comments