11import * as sdpTransform from 'sdp-transform' ;
22import type * as SdpTransform from 'sdp-transform' ;
3+ import { EnhancedEventEmitter } from '../enhancedEvents' ;
34import { Logger } from '../Logger' ;
45import * as utils from '../utils' ;
56import * as ortc from '../ortc' ;
@@ -15,18 +16,19 @@ import type { SctpCapabilities, SctpStreamParameters } from '../SctpParameters';
1516import * as sdpCommonUtils from './sdp/commonUtils' ;
1617import * as sdpUnifiedPlanUtils from './sdp/unifiedPlanUtils' ;
1718import * as ortcUtils from './ortc/utils' ;
18- import {
19- type HandlerFactory ,
19+ import type {
20+ HandlerFactory ,
2021 HandlerInterface ,
21- type HandlerOptions ,
22- type HandlerSendOptions ,
23- type HandlerSendResult ,
24- type HandlerReceiveOptions ,
25- type HandlerReceiveResult ,
26- type HandlerSendDataChannelOptions ,
27- type HandlerSendDataChannelResult ,
28- type HandlerReceiveDataChannelOptions ,
29- type HandlerReceiveDataChannelResult ,
22+ HandlerEvents ,
23+ HandlerOptions ,
24+ HandlerSendOptions ,
25+ HandlerSendResult ,
26+ HandlerReceiveOptions ,
27+ HandlerReceiveResult ,
28+ HandlerSendDataChannelOptions ,
29+ HandlerSendDataChannelResult ,
30+ HandlerReceiveDataChannelOptions ,
31+ HandlerReceiveDataChannelResult ,
3032} from './HandlerInterface' ;
3133import { RemoteSdp } from './sdp/RemoteSdp' ;
3234
@@ -35,7 +37,10 @@ const logger = new Logger('Chrome111');
3537const NAME = 'Chrome111' ;
3638const SCTP_NUM_STREAMS = { OS : 1024 , MIS : 1024 } ;
3739
38- export class Chrome111 extends HandlerInterface {
40+ export class Chrome111
41+ extends EnhancedEventEmitter < HandlerEvents >
42+ implements HandlerInterface
43+ {
3944 // Closed flag.
4045 private _closed = false ;
4146 // Handler direction.
@@ -76,7 +81,7 @@ export class Chrome111 extends HandlerInterface {
7681 getNativeRtpCapabilities : async ( ) : Promise < RtpCapabilities > => {
7782 logger . debug ( 'getNativeRtpCapabilities()' ) ;
7883
79- const pc = new RTCPeerConnection ( {
84+ let pc : RTCPeerConnection | undefined = new RTCPeerConnection ( {
8085 iceServers : [ ] ,
8186 iceTransportPolicy : 'all' ,
8287 bundlePolicy : 'max-bundle' ,
@@ -97,6 +102,8 @@ export class Chrome111 extends HandlerInterface {
97102 pc . close ( ) ;
98103 } catch ( error ) { }
99104
105+ pc = undefined ;
106+
100107 const sdpObject = sdpTransform . parse ( offer . sdp ! ) ;
101108 const nativeRtpCapabilities = sdpCommonUtils . extractRtpCapabilities ( {
102109 sdpObject,
@@ -108,9 +115,11 @@ export class Chrome111 extends HandlerInterface {
108115 return nativeRtpCapabilities ;
109116 } catch ( error ) {
110117 try {
111- pc . close ( ) ;
118+ pc ? .close ( ) ;
112119 } catch ( error2 ) { }
113120
121+ pc = undefined ;
122+
114123 throw error ;
115124 }
116125 } ,
@@ -177,68 +186,35 @@ export class Chrome111 extends HandlerInterface {
177186 ...additionalSettings ,
178187 } ) ;
179188
180- this . _pc . addEventListener ( 'icegatheringstatechange' , ( ) => {
181- this . emit ( '@icegatheringstatechange' , this . _pc . iceGatheringState ) ;
182- } ) ;
183-
184189 this . _pc . addEventListener (
185- 'icecandidateerror' ,
186- ( event : RTCPeerConnectionIceErrorEvent ) => {
187- this . emit ( '@icecandidateerror' , event ) ;
188- }
190+ 'icegatheringstatechange' ,
191+ this . onIceGatheringStateChange
189192 ) ;
190193
194+ this . _pc . addEventListener ( 'icecandidateerror' , this . onIceCandidateError ) ;
195+
191196 if ( this . _pc . connectionState ) {
192- this . _pc . addEventListener ( 'connectionstatechange' , ( ) => {
193- this . emit ( '@connectionstatechange' , this . _pc . connectionState ) ;
194- } ) ;
197+ this . _pc . addEventListener (
198+ 'connectionstatechange' ,
199+ this . onConnectionStateChange
200+ ) ;
195201 } else {
196202 logger . warn (
197203 'run() | pc.connectionState not supported, using pc.iceConnectionState'
198204 ) ;
199205
200- this . _pc . addEventListener ( 'iceconnectionstatechange' , ( ) => {
201- switch ( this . _pc . iceConnectionState ) {
202- case 'checking' : {
203- this . emit ( '@connectionstatechange' , 'connecting' ) ;
204-
205- break ;
206- }
207-
208- case 'connected' :
209- case 'completed' : {
210- this . emit ( '@connectionstatechange' , 'connected' ) ;
211-
212- break ;
213- }
214-
215- case 'failed' : {
216- this . emit ( '@connectionstatechange' , 'failed' ) ;
217-
218- break ;
219- }
220-
221- case 'disconnected' : {
222- this . emit ( '@connectionstatechange' , 'disconnected' ) ;
223-
224- break ;
225- }
226-
227- case 'closed' : {
228- this . emit ( '@connectionstatechange' , 'closed' ) ;
229-
230- break ;
231- }
232- }
233- } ) ;
206+ this . _pc . addEventListener (
207+ 'iceconnectionstatechange' ,
208+ this . onIceConnectionStateChange
209+ ) ;
234210 }
235211 }
236212
237213 get name ( ) : string {
238214 return NAME ;
239215 }
240216
241- close ( ) : void {
217+ override close ( ) : void {
242218 logger . debug ( 'close()' ) ;
243219
244220 if ( this . _closed ) {
@@ -248,13 +224,31 @@ export class Chrome111 extends HandlerInterface {
248224 this . _closed = true ;
249225
250226 // Close RTCPeerConnection.
251- if ( this . _pc ) {
252- try {
253- this . _pc . close ( ) ;
254- } catch ( error ) { }
255- }
227+ try {
228+ this . _pc . close ( ) ;
229+ } catch ( error ) { }
230+
231+ this . _pc . removeEventListener (
232+ 'icegatheringstatechange' ,
233+ this . onIceGatheringStateChange
234+ ) ;
235+
236+ this . _pc . removeEventListener ( 'icecandidateerror' , this . onIceCandidateError ) ;
237+
238+ this . _pc . removeEventListener (
239+ 'connectionstatechange' ,
240+ this . onConnectionStateChange
241+ ) ;
242+
243+ this . _pc . removeEventListener (
244+ 'iceconnectionstatechange' ,
245+ this . onIceConnectionStateChange
246+ ) ;
256247
257248 this . emit ( '@close' ) ;
249+
250+ // Invoke close() in EnhancedEventEmitter classes.
251+ super . close ( ) ;
258252 }
259253
260254 async updateIceServers ( iceServers : RTCIceServer [ ] ) : Promise < void > {
@@ -1193,6 +1187,55 @@ export class Chrome111 extends HandlerInterface {
11931187 this . _transportReady = true ;
11941188 }
11951189
1190+ private onIceGatheringStateChange = ( ) : void => {
1191+ this . emit ( '@icegatheringstatechange' , this . _pc . iceGatheringState ) ;
1192+ } ;
1193+
1194+ private onIceCandidateError = (
1195+ event : RTCPeerConnectionIceErrorEvent
1196+ ) : void => {
1197+ this . emit ( '@icecandidateerror' , event ) ;
1198+ } ;
1199+
1200+ private onConnectionStateChange = ( ) : void => {
1201+ this . emit ( '@connectionstatechange' , this . _pc . connectionState ) ;
1202+ } ;
1203+
1204+ private onIceConnectionStateChange = ( ) : void => {
1205+ switch ( this . _pc . iceConnectionState ) {
1206+ case 'checking' : {
1207+ this . emit ( '@connectionstatechange' , 'connecting' ) ;
1208+
1209+ break ;
1210+ }
1211+
1212+ case 'connected' :
1213+ case 'completed' : {
1214+ this . emit ( '@connectionstatechange' , 'connected' ) ;
1215+
1216+ break ;
1217+ }
1218+
1219+ case 'failed' : {
1220+ this . emit ( '@connectionstatechange' , 'failed' ) ;
1221+
1222+ break ;
1223+ }
1224+
1225+ case 'disconnected' : {
1226+ this . emit ( '@connectionstatechange' , 'disconnected' ) ;
1227+
1228+ break ;
1229+ }
1230+
1231+ case 'closed' : {
1232+ this . emit ( '@connectionstatechange' , 'closed' ) ;
1233+
1234+ break ;
1235+ }
1236+ }
1237+ } ;
1238+
11961239 private assertNotClosed ( ) : void {
11971240 if ( this . _closed ) {
11981241 throw new InvalidStateError ( 'method called in a closed handler' ) ;
0 commit comments