@@ -71,17 +71,23 @@ function deviceDetailsHidden(devices: MediaDeviceInfo[], kind: RealtimeTalkDevic
7171 return inputs . length === 0 || inputs . some ( ( device ) => ! device . deviceId || ! device . label ) ;
7272}
7373
74- const deviceIssueByDomErrorName : Record < string , RealtimeTalkDeviceIssue > = {
74+ const deviceIssueByMediaErrorName : Record < string , RealtimeTalkDeviceIssue > = {
7575 NotAllowedError : "permission-blocked" ,
7676 NotFoundError : "none-found" ,
7777 NotReadableError : "busy" ,
7878 InvalidStateError : "page-inactive" ,
7979} ;
8080
81+ function mediaDeviceErrorName ( error : unknown ) : string | undefined {
82+ // WebKit shipped OverconstrainedError as Error instead of DOMException.
83+ if ( error instanceof DOMException ) {
84+ return error . name ;
85+ }
86+ return error instanceof Error && error . name === "OverconstrainedError" ? error . name : undefined ;
87+ }
88+
8189function deviceIssueFromError ( error : unknown ) : RealtimeTalkDeviceIssue {
82- return (
83- ( error instanceof DOMException ? deviceIssueByDomErrorName [ error . name ] : undefined ) ?? "failed"
84- ) ;
90+ return deviceIssueByMediaErrorName [ mediaDeviceErrorName ( error ) ?? "" ] ?? "failed" ;
8591}
8692
8793export function realtimeTalkDeviceIssueMessage (
@@ -233,18 +239,16 @@ export async function openRealtimeTalkInput(
233239 ) ,
234240 } ;
235241 } catch ( error ) {
236- if (
237- inputDeviceId ?. trim ( ) &&
238- error instanceof DOMException &&
239- error . name === "OverconstrainedError"
240- ) {
241- throw new Error ( t ( "chat.composer.selectedMicrophoneUnavailable" ) , { cause : error } ) ;
242- }
243- if ( error instanceof DOMException && error . name !== "AbortError" ) {
244- acquisition = { failure : describeRealtimeTalkInputError ( error ) } ;
245- } else {
242+ const errorName = mediaDeviceErrorName ( error ) ;
243+ if ( ! errorName || errorName === "AbortError" ) {
246244 throw error ;
247245 }
246+ acquisition = {
247+ failure :
248+ inputDeviceId ?. trim ( ) && errorName === "OverconstrainedError"
249+ ? t ( "chat.composer.selectedMicrophoneUnavailable" )
250+ : describeRealtimeTalkInputError ( error ) ,
251+ } ;
248252 }
249253 if ( "failure" in acquisition ) {
250254 throw new Error ( acquisition . failure ) ;
@@ -266,36 +270,31 @@ export async function openRealtimeTalkCamera(
266270 throw new Error ( t ( "chat.composer.cameraAccessFailed" ) ) ;
267271 }
268272 const deviceId = videoDeviceId ?. trim ( ) ;
269- let camera : MediaStream ;
273+ let acquisition : { stream : MediaStream } | { failure : string } ;
270274 try {
271- camera = await awaitRealtimeTalkMediaRequest (
272- ( ) =>
273- devices . getUserMedia ( {
274- video : deviceId ? { deviceId : { exact : deviceId } } : true ,
275- } ) ,
275+ const stream = await awaitRealtimeTalkMediaRequest (
276+ ( ) => devices . getUserMedia ( { video : deviceId ? { deviceId : { exact : deviceId } } : true } ) ,
276277 options . signal ,
277278 ) ;
278279 if ( options . signal ?. aborted ) {
279- camera . getTracks ( ) . forEach ( ( track ) => track . stop ( ) ) ;
280+ stream . getTracks ( ) . forEach ( ( track ) => track . stop ( ) ) ;
280281 throw realtimeTalkAbortReason ( options . signal ) ;
281282 }
282- return camera ;
283+ acquisition = { stream } ;
283284 } catch ( error ) {
284285 if ( options . signal ?. aborted ) {
285286 throw realtimeTalkAbortReason ( options . signal ) ;
286287 }
287- if ( deviceId && error instanceof DOMException && error . name === "OverconstrainedError" ) {
288- throw new Error ( t ( "chat.composer.selectedCameraUnavailable" ) , { cause : error } ) ;
289- }
290- if ( error instanceof DOMException && error . name === "NotAllowedError" ) {
291- throw new Error ( t ( "chat.composer.cameraPermissionBlocked" ) , { cause : error } ) ;
292- }
293- if ( error instanceof DOMException && error . name === "NotFoundError" ) {
294- throw new Error ( t ( "chat.composer.cameraNoneFound" ) , { cause : error } ) ;
295- }
296- if ( error instanceof DOMException && error . name === "NotReadableError" ) {
297- throw new Error ( t ( "chat.composer.cameraBusy" ) , { cause : error } ) ;
298- }
299- throw new Error ( t ( "chat.composer.cameraAccessFailed" ) , { cause : error } ) ;
288+ const errorName = mediaDeviceErrorName ( error ) ;
289+ acquisition = {
290+ failure :
291+ deviceId && errorName === "OverconstrainedError"
292+ ? t ( "chat.composer.selectedCameraUnavailable" )
293+ : realtimeTalkDeviceIssueMessage ( deviceIssueFromError ( error ) , "videoinput" ) ,
294+ } ;
295+ }
296+ if ( "failure" in acquisition ) {
297+ throw new Error ( acquisition . failure ) ;
300298 }
299+ return acquisition . stream ;
301300}
0 commit comments