@@ -50,16 +50,19 @@ angular.module('mm.core.emulator')
5050 function captureMedia ( type , successCallback , errorCallback , options ) {
5151 options = options || { } ;
5252
53+ var loadingModal ;
54+
5355 try {
5456 var scope = $rootScope . $new ( ) ,
55- loadingModal = $mmUtil . showModalLoading ( ) ,
5657 facingMode = 'environment' ,
5758 mimetype ,
5859 extension ,
5960 quality = 0.92 , // Image only.
6061 returnData = false , // Image only.
6162 isCaptureImage = false ; // To identify if it's capturing an image using media capture plugin (instead of camera).
6263
64+ loadingModal = $mmUtil . showModalLoading ( ) ;
65+
6366 if ( type == 'captureimage' ) {
6467 isCaptureImage = true ;
6568 type = 'image' ;
@@ -117,7 +120,7 @@ angular.module('mm.core.emulator')
117120 audio : ! scope . isImage
118121 } ;
119122
120- navigator . mediaDevices . getUserMedia ( constraints ) . then ( function ( localMediaStream ) {
123+ return navigator . mediaDevices . getUserMedia ( constraints ) . then ( function ( localMediaStream ) {
121124 var streamVideo ,
122125 previewMedia ,
123126 canvas ,
@@ -159,17 +162,37 @@ angular.module('mm.core.emulator')
159162 }
160163
161164 if ( scope . isImage || scope . isVideo ) {
165+ var hasLoaded = false ,
166+ waitTimeout ;
167+
162168 // Set the stream as the source of the video.
163169 streamVideo = modal . modalEl . querySelector ( 'video.mm-webcam-stream' ) ;
164170 streamVideo . src = $window . URL . createObjectURL ( localMediaStream ) ;
165171
166172 // Stream ready, show modal.
167173 streamVideo . onloadedmetadata = function ( ) {
174+ if ( hasLoaded ) {
175+ // Already loaded or timeout triggered, stop.
176+ return ;
177+ }
178+
179+ hasLoaded = true ;
180+ $timeout . cancel ( waitTimeout ) ;
168181 loadingModal . dismiss ( ) ;
169182 modal . show ( ) ;
170183 scope . readyToCapture = true ;
171184 streamVideo . onloadedmetadata = null ;
172185 } ;
186+
187+ // If stream isn't ready in a while, show error.
188+ waitTimeout = $timeout ( function ( ) {
189+ if ( ! hasLoaded ) {
190+ // Show error.
191+ hasLoaded = true ;
192+ loadingModal . dismiss ( ) ;
193+ errorCallback && errorCallback ( { code : - 1 , message : 'Cannot connect to webcam.' } ) ;
194+ }
195+ } , 10000 ) ;
173196 } else {
174197 // No need to wait to show the modal.
175198 loadingModal . dismiss ( ) ;
@@ -304,10 +327,14 @@ angular.module('mm.core.emulator')
304327 scope . $on ( '$destroy' , function ( ) {
305328 scope . modal . remove ( ) ;
306329 } ) ;
307- } ) . catch ( errorCallback ) ;
308- } , errorCallback ) ;
330+ } ) ;
331+ } ) . catch ( function ( err ) {
332+ loadingModal && loadingModal . dismiss ( ) ;
333+ errorCallback && errorCallback ( err ) ;
334+ } ) ;
309335 } catch ( ex ) {
310- errorCallback ( ex . toString ( ) ) ;
336+ loadingModal && loadingModal . dismiss ( ) ;
337+ errorCallback && errorCallback ( ex . toString ( ) ) ;
311338 }
312339 }
313340
0 commit comments