Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 69efd83

Browse files
lastpeonylastpeony
andauthored
p2p default audio device is speaker, switch to bluetooth device if its (ant-media#102)
available Co-authored-by: lastpeony <[email protected]>
1 parent a71346b commit 69efd83

File tree

6 files changed

+43
-11
lines changed

6 files changed

+43
-11
lines changed

webrtc-android-framework/src/main/java/io/antmedia/webrtcandroidframework/core/WebRTCClient.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,10 @@ public void join(String streamId, String token) {
10891089
peers.put(streamId, peerInfo);
10901090

10911091
init();
1092+
1093+
initializeAudioManager();
1094+
1095+
10921096
wsHandler.joinToPeer(streamId, token);
10931097
}
10941098

@@ -1109,16 +1113,31 @@ private void callConnected(String streamId) {
11091113
enableStatsEvents(streamId, true, STAT_CALLBACK_PERIOD);
11101114
}
11111115

1112-
// This method is called when the audio manager reports audio device change,
1113-
// e.g. from wired headset to speakerphone.
1116+
/**
1117+
* Handles changes in audio devices reported by the audio manager, such as
1118+
* switching from a wired headset to a speakerphone.
1119+
*
1120+
* @param device The currently selected audio device.
1121+
* @param availableDevices A set of available audio devices.
1122+
*/
11141123
public void onAudioManagerDevicesChanged(
11151124
final AppRTCAudioManager.AudioDevice device, final Set<AppRTCAudioManager.AudioDevice> availableDevices) {
1116-
Log.d(TAG, "onAudioManagerDevicesChanged: " + availableDevices + ", "
1117-
+ "selected: " + device);
1118-
// TODO(henrika): add callback handler.
1119-
if (audioManager != null) {
1120-
audioManager.selectAudioDevice(device);
1125+
if(audioManager == null){
1126+
return;
1127+
}
1128+
Log.i(TAG, "Audio devices changed. Available: " + availableDevices + ", Selected: " + device);
1129+
1130+
// Prioritize Bluetooth if available
1131+
if (availableDevices.contains(AppRTCAudioManager.AudioDevice.BLUETOOTH)) {
1132+
Log.i(TAG, "Bluetooth device found, switching to Bluetooth.");
1133+
audioManager.selectAudioDevice(AppRTCAudioManager.AudioDevice.BLUETOOTH);
1134+
return;
11211135
}
1136+
1137+
// Fallback to the current selected device
1138+
Log.i(TAG, "Switching to the selected device: " + device);
1139+
audioManager.selectAudioDevice(device);
1140+
11221141
}
11231142

11241143
// Disconnect from remote resources, dispose of local resources, and exit.

webrtc-android-framework/src/test/java/io/antmedia/webrtcandroidframework/WebRTCClientTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,13 @@ public void testJoinParameters() {
261261

262262
when(context.getString(anyInt(), Matchers.<Object>anyVararg())).thenReturn("asas");
263263
doNothing().when(webRTCClient).init();
264+
doNothing().when(webRTCClient).initializeAudioManager();
264265
doReturn(true).when(wsHandler).isConnected();
265266

266267

267268
webRTCClient.join(streamId, token);
268269

270+
verify(webRTCClient, times(1)).initializeAudioManager();
269271
verify(wsHandler, times(1)).joinToPeer(streamId, token);
270272

271273
ArgumentCaptor<String> jsonCaptor = ArgumentCaptor.forClass(String.class);
@@ -411,6 +413,14 @@ public void testOnAudioManagerDevicesChanged() {
411413

412414
// Verify that the audio device is selected using the AudioManager
413415
Mockito.verify(audioManager).selectAudioDevice(device);
416+
417+
availableDevices.clear();
418+
419+
availableDevices.add(AppRTCAudioManager.AudioDevice.SPEAKER_PHONE);
420+
AppRTCAudioManager.AudioDevice speakerDevice = AppRTCAudioManager.AudioDevice.SPEAKER_PHONE;
421+
422+
webRTCClient.onAudioManagerDevicesChanged(speakerDevice, availableDevices);
423+
Mockito.verify(audioManager).selectAudioDevice(speakerDevice);
414424
}
415425

416426
@Test

webrtc-android-sample-app/src/main/java/io/antmedia/webrtc_android_sample_app/MainActivity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte
5151
@Override
5252
protected void onCreate(Bundle savedInstanceState) {
5353
super.onCreate(savedInstanceState);
54+
getSupportActionBar().hide();
55+
5456
setContentView(R.layout.activity_main);
5557

5658
list = findViewById(R.id.list);

webrtc-android-sample-app/src/main/java/io/antmedia/webrtc_android_sample_app/TestableActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
4040
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
4141
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
4242
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
43-
//getWindow().getDecorView().setSystemUiVisibility(getSystemUiVisibility());
43+
getSupportActionBar().hide();
4444

4545
sharedPreferences =
4646
PreferenceManager.getDefaultSharedPreferences(this /* Activity context */);

webrtc-android-sample-app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
android:id="@+id/list"
1010
android:layout_width="match_parent"
1111
android:layout_height="match_parent"
12-
android:layout_marginTop="120dp"
13-
android:layout_marginBottom="42dp"
12+
1413
android:columnWidth="40dp"
1514
android:gravity="center"
1615
android:horizontalSpacing="21dp"

webrtc-android-sample-app/src/main/res/values/styles.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<resources>
22

33
<!-- Base application theme. -->
4-
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
4+
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
55
<!-- Customize your theme here. -->
66
<item name="colorPrimary">@color/colorPrimary</item>
7+
<item name="windowNoTitle">true</item>
8+
<item name="android:windowNoTitle">true</item>
79
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
810
<item name="colorAccent">@color/colorAccent</item>
911
</style>

0 commit comments

Comments
 (0)