@@ -158,6 +158,42 @@ private void onAudioManagerDevicesChanged(
158
158
// TODO(henrika): add callback handler.
159
159
}
160
160
161
+ private void startAudioManager () {
162
+ if (rtcAudioManager != null )
163
+ return ;
164
+
165
+ rtcAudioManager = RTCAudioManager .create (registrar .context ());
166
+ // Store existing audio settings and change audio mode to
167
+ // MODE_IN_COMMUNICATION for best possible VoIP performance.
168
+ Log .d (TAG , "Starting the audio manager..." );
169
+ rtcAudioManager .start (new RTCAudioManager .AudioManagerEvents () {
170
+ // This method will be called each time the number of available audio
171
+ // devices has changed.
172
+ @ Override
173
+ public void onAudioDeviceChanged (
174
+ RTCAudioManager .AudioDevice audioDevice , Set <RTCAudioManager .AudioDevice > availableAudioDevices ) {
175
+ onAudioManagerDevicesChanged (audioDevice , availableAudioDevices );
176
+ }
177
+ });
178
+ }
179
+
180
+ private void stopAudioManager () {
181
+ if (rtcAudioManager != null ) {
182
+ Log .d (TAG , "Stoping the audio manager..." );
183
+ rtcAudioManager .stop ();
184
+ rtcAudioManager = null ;
185
+ }
186
+ }
187
+
188
+ // This method is called when the audio manager reports audio device change,
189
+ // e.g. from wired headset to speakerphone.
190
+ private void onAudioManagerDevicesChanged (
191
+ final RTCAudioManager .AudioDevice device , final Set <RTCAudioManager .AudioDevice > availableDevices ) {
192
+ Log .d (TAG , "onAudioManagerDevicesChanged: " + availableDevices + ", "
193
+ + "selected: " + device );
194
+ // TODO(henrika): add callback handler.
195
+ }
196
+
161
197
@ Override
162
198
public void onMethodCall (MethodCall call , Result notSafeResult ) {
163
199
final AnyThreadResult result = new AnyThreadResult (notSafeResult );
@@ -297,7 +333,7 @@ public void onMethodCall(MethodCall call, Result notSafeResult) {
297
333
result .success (null );
298
334
} else if (call .method .equals ("peerConnectionDispose" )){
299
335
String peerConnectionId = call .argument ("peerConnectionId" );
300
- peerConnectionClose (peerConnectionId );
336
+ peerConnectionDispose (peerConnectionId );
301
337
result .success (null );
302
338
}else if (call .method .equals ("createVideoRenderer" )) {
303
339
TextureRegistry .SurfaceTextureEntry entry = textures .createSurfaceTexture ();
@@ -362,6 +398,9 @@ public void onMethodCall(MethodCall call, Result notSafeResult) {
362
398
result .success (null );
363
399
} else if (call .method .equals ("enableSpeakerphone" )) {
364
400
boolean enable = call .argument ("enable" );
401
+ if (rtcAudioManager == null ){
402
+ startAudioManager ();
403
+ }
365
404
rtcAudioManager .setSpeakerphoneOn (enable );
366
405
result .success (null );
367
406
} else if (call .method .equals ("getDisplayMedia" )) {
@@ -723,6 +762,9 @@ public String peerConnectionInit(
723
762
parseMediaConstraints (constraints ),
724
763
observer );
725
764
observer .setPeerConnection (peerConnection );
765
+ if (mPeerConnectionObservers .size () == 0 ) {
766
+ startAudioManager ();
767
+ }
726
768
mPeerConnectionObservers .put (peerConnectionId , observer );
727
769
return peerConnectionId ;
728
770
}
@@ -1285,6 +1327,9 @@ public void peerConnectionDispose(final String id) {
1285
1327
pco .dispose ();
1286
1328
mPeerConnectionObservers .remove (id );
1287
1329
}
1330
+ if (mPeerConnectionObservers .size () == 0 ) {
1331
+ stopAudioManager ();
1332
+ }
1288
1333
}
1289
1334
1290
1335
public void mediaStreamRelease (final String id ) {
0 commit comments