@@ -270,15 +270,15 @@ private void addDefaultAudioConstraints(MediaConstraints audioConstraints) {
270
270
* if not matched camera with specified facing mode.
271
271
*/
272
272
private Map <String , VideoCapturer > createVideoCapturer (
273
- CameraEnumerator enumerator , boolean isFacing , String sourceId ) {
273
+ CameraEnumerator enumerator , boolean isFacing , String sourceId , CameraEventsHandler cameraEventsHandler ) {
274
274
VideoCapturer videoCapturer = null ;
275
275
Map <String , VideoCapturer > result = new HashMap <String , VideoCapturer >();
276
276
// if sourceId given, use specified sourceId first
277
277
final String [] deviceNames = enumerator .getDeviceNames ();
278
278
if (sourceId != null && !sourceId .equals ("" )) {
279
279
for (String name : deviceNames ) {
280
280
if (name .equals (sourceId )) {
281
- videoCapturer = enumerator .createCapturer (name , new CameraEventsHandler () );
281
+ videoCapturer = enumerator .createCapturer (name , cameraEventsHandler );
282
282
if (videoCapturer != null ) {
283
283
Log .d (TAG , "create user specified camera " + name + " succeeded" );
284
284
result .put (name , videoCapturer );
@@ -295,10 +295,9 @@ private Map<String, VideoCapturer> createVideoCapturer(
295
295
String facingStr = isFacing ? "front" : "back" ;
296
296
for (String name : deviceNames ) {
297
297
if (enumerator .isFrontFacing (name ) == isFacing ) {
298
- videoCapturer = enumerator .createCapturer (name , new CameraEventsHandler () );
298
+ videoCapturer = enumerator .createCapturer (name , cameraEventsHandler );
299
299
if (videoCapturer != null ) {
300
300
Log .d (TAG , "Create " + facingStr + " camera " + name + " succeeded" );
301
-
302
301
result .put (name , videoCapturer );
303
302
return result ;
304
303
} else {
@@ -309,7 +308,7 @@ private Map<String, VideoCapturer> createVideoCapturer(
309
308
310
309
// falling back to the first available camera
311
310
if (videoCapturer == null && deviceNames .length > 0 ) {
312
- videoCapturer = enumerator .createCapturer (deviceNames [0 ], new CameraEventsHandler () );
311
+ videoCapturer = enumerator .createCapturer (deviceNames [0 ], cameraEventsHandler );
313
312
Log .d (TAG , "Falling back to the first available camera" );
314
313
result .put (deviceNames [0 ], videoCapturer );
315
314
}
@@ -741,15 +740,19 @@ private ConstraintsMap getUserVideo(ConstraintsMap constraints, MediaStream medi
741
740
String facingMode = getFacingMode (videoConstraintsMap );
742
741
isFacing = facingMode == null || !facingMode .equals ("environment" );
743
742
String deviceId = getSourceIdConstraint (videoConstraintsMap );
744
-
745
- Map <String , VideoCapturer > result = createVideoCapturer (cameraEnumerator , isFacing , deviceId );
743
+ CameraEventsHandler cameraEventsHandler = new CameraEventsHandler ();
744
+ Map <String , VideoCapturer > result = createVideoCapturer (cameraEnumerator , isFacing , deviceId , cameraEventsHandler );
746
745
747
746
if (result == null ) {
748
747
return null ;
749
748
}
750
749
751
750
if (deviceId == null ) {
752
- deviceId = result .keySet ().iterator ().next ();
751
+ if (!result .keySet ().isEmpty ()) {
752
+ deviceId = result .keySet ().iterator ().next ();
753
+ } else {
754
+ return null ;
755
+ }
753
756
}
754
757
755
758
VideoCapturer videoCapturer = result .get (deviceId );
@@ -785,8 +788,11 @@ private ConstraintsMap getUserVideo(ConstraintsMap constraints, MediaStream medi
785
788
? videoConstraintsMandatory .getInt ("minFrameRate" )
786
789
: DEFAULT_FPS ;
787
790
info .capturer = videoCapturer ;
791
+ info .cameraEventsHandler = cameraEventsHandler ;
788
792
videoCapturer .startCapture (info .width , info .height , info .fps );
789
793
794
+ cameraEventsHandler .waitForCameraOpen ();
795
+
790
796
String trackId = stateProvider .getNextTrackUUID ();
791
797
mVideoCapturers .put (trackId , info );
792
798
mSurfaceTextureHelpers .put (trackId , surfaceTextureHelper );
@@ -819,34 +825,29 @@ private ConstraintsMap getUserVideo(ConstraintsMap constraints, MediaStream medi
819
825
return trackParams ;
820
826
}
821
827
822
- void removeVideoCapturerSync (String id ) {
823
- synchronized (mVideoCapturers ) {
824
- VideoCapturerInfo info = mVideoCapturers .get (id );
825
- if (info != null ) {
826
- try {
827
- info .capturer .stopCapture ();
828
- } catch (InterruptedException e ) {
829
- Log .e (TAG , "removeVideoCapturer() Failed to stop video capturer" );
830
- } finally {
831
- info .capturer .dispose ();
832
- mVideoCapturers .remove (id );
833
- SurfaceTextureHelper helper = mSurfaceTextureHelpers .get (id );
834
- if (helper != null ) {
835
- helper .stopListening ();
836
- helper .dispose ();
837
- mSurfaceTextureHelpers .remove (id );
838
- }
828
+ void removeVideoCapturer (String id ) {
829
+ VideoCapturerInfo info = mVideoCapturers .get (id );
830
+ if (info != null ) {
831
+ try {
832
+ info .capturer .stopCapture ();
833
+ if (info .cameraEventsHandler != null ) {
834
+ info .cameraEventsHandler .waitForCameraClosed ();
835
+ }
836
+ } catch (InterruptedException e ) {
837
+ Log .e (TAG , "removeVideoCapturer() Failed to stop video capturer" );
838
+ } finally {
839
+ info .capturer .dispose ();
840
+ mVideoCapturers .remove (id );
841
+ SurfaceTextureHelper helper = mSurfaceTextureHelpers .get (id );
842
+ if (helper != null ) {
843
+ helper .stopListening ();
844
+ helper .dispose ();
845
+ mSurfaceTextureHelpers .remove (id );
839
846
}
840
847
}
841
848
}
842
849
}
843
850
844
- void removeVideoCapturer (String id ) {
845
- new Thread (() -> {
846
- removeVideoCapturerSync (id );
847
- }).start ();
848
- }
849
-
850
851
@ RequiresApi (api = VERSION_CODES .M )
851
852
private void requestPermissions (
852
853
final ArrayList <String > permissions ,
@@ -1289,6 +1290,7 @@ public class VideoCapturerInfo {
1289
1290
int height ;
1290
1291
int fps ;
1291
1292
boolean isScreenCapture = false ;
1293
+ CameraEventsHandler cameraEventsHandler ;
1292
1294
}
1293
1295
1294
1296
@ RequiresApi (api = VERSION_CODES .M )
0 commit comments