3
3
// SPDX-License-Identifier: Apache-2.0
4
4
5
5
/* eslint-disable require-jsdoc */
6
- /* global Promise, Map, QuicTransport, Uint8Array, TextEncoder */
6
+ /* global Promise, Map, QuicTransport, Uint8Array, Uint32Array, TextEncoder */
7
7
8
8
'use strict' ;
9
9
10
10
import Logger from '../base/logger.js' ;
11
11
import { EventDispatcher } from '../base/event.js' ;
12
12
import { Publication } from '../base/publication.js' ;
13
13
import { Subscription } from './subscription.js' ;
14
+ import { Base64 } from '../base/base64.js' ;
14
15
15
16
/**
16
17
* @class QuicConnection
@@ -20,15 +21,19 @@ import {Subscription} from './subscription.js';
20
21
* @private
21
22
*/
22
23
export class QuicConnection extends EventDispatcher {
23
- constructor ( url , signaling ) {
24
+ // `tokenString` is a base64 string of the token object. It's in the return
25
+ // value of `ConferenceClient.join`.
26
+ constructor ( url , tokenString , signaling ) {
24
27
super ( ) ;
28
+ this . _token = JSON . parse ( Base64 . decodeBase64 ( tokenString ) ) ;
25
29
this . _signaling = signaling ;
26
30
this . _ended = false ;
27
31
this . _quicStreams = new Map ( ) ; // Key is publication or subscription ID.
28
32
this . _quicTransport = new QuicTransport ( url ) ;
29
33
this . _subscribePromises = new Map ( ) ; // Key is subscription ID.
30
- this . _transportId = undefined ;
34
+ this . _transportId = this . _token . transportId ;
31
35
this . _init ( ) ;
36
+ this . _authenticate ( tokenString ) ;
32
37
}
33
38
34
39
/**
@@ -106,10 +111,14 @@ export class QuicConnection extends EventDispatcher {
106
111
const quicStream = await this . _quicTransport . createSendStream ( ) ;
107
112
const writer = quicStream . writable . getWriter ( ) ;
108
113
await writer . ready ;
114
+ // 128 bit of zero indicates this is a stream for signaling.
109
115
writer . write ( new Uint8Array ( 16 ) ) ;
116
+ // Send token as described in
117
+ // https://github.com/open-webrtc-toolkit/owt-server/blob/20e8aad216cc446095f63c409339c34c7ee770ee/doc/design/quic-transport-payload-format.md.
110
118
const encoder = new TextEncoder ( ) ;
111
- writer . write ( encoder . encode ( token ) ) ;
112
- Logger . info ( 'End of auth.' ) ;
119
+ const encodedToken = encoder . encode ( token ) ;
120
+ writer . write ( Uint32Array . of ( encodedToken . length ) ) ;
121
+ writer . write ( encodedToken ) ;
113
122
}
114
123
115
124
async createSendStream ( ) {
@@ -214,27 +223,14 @@ export class QuicConnection extends EventDispatcher {
214
223
} ) ;
215
224
}
216
225
217
- async _updateTransportId ( id ) {
218
- if ( this . _transportId ) {
219
- Logger . error ( 'Update transport ID is not supported.' ) ;
220
- return ;
221
- }
222
- this . _transportId = id ;
223
- await this . _authenticate ( id ) ;
224
- }
225
-
226
226
async _initiatePublication ( ) {
227
227
const data = await this . _signaling . sendSignalingMessage ( 'publish' , {
228
228
media : null ,
229
229
data : true ,
230
230
transport : { type : 'quic' , id : this . _transportId } ,
231
231
} ) ;
232
- if ( data . transportId ) {
233
- await this . _updateTransportId ( data . transportId ) ;
234
- } else {
235
- if ( this . _transportId !== data . transportId ) {
236
- throw new Error ( 'Transport ID not match.' ) ;
237
- }
232
+ if ( this . _transportId !== data . transportId ) {
233
+ throw new Error ( 'Transport ID not match.' ) ;
238
234
}
239
235
return data . id ;
240
236
}
0 commit comments