twilio / video-quickstart-android

Twilio Video Quickstart for Android
MIT License
213 stars 160 forks source link

Automatic detection of connecting/disconnecting a headset #76

Closed KarimAbdo closed 5 years ago

KarimAbdo commented 7 years ago

This only worked on the nexus devices. One HTC, Sony and samsung it kept using the loudspeaker.

i solved it by adding a BroadcastReceiver that detects if the headphones get connected/disconnected

private class MusicIntentReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
                int state = intent.getIntExtra("state", -1);
                switch (state) {
                    case 0:
                        audioManager.setSpeakerphoneOn(true);
                        break;
                    case 1:
                        audioManager.setSpeakerphoneOn(false);
                        break;
                    default:
                        audioManager.setSpeakerphoneOn(true);
                }
            }
        }
    }
aaalaniz commented 7 years ago

Thanks @KarimAbdo

Do you find that there is a need for an AudioManager utility that handles common events such as bluetooth connection, headset connect/disconnect or proximity sensor detection? If so, we can discuss in our next planning meeting.

KarimAbdo commented 7 years ago

I think it will be very helpful.

Also i would think maybe an integrated speed test would be very helpful, if you could integrate it within the call. so for example if the internet got weak you can either send lower quality instead of the slow motion one that is already there, or if it is too weak maybe freeze the video with a message, or an event.

mcorner commented 7 years ago

It would be great it if it just did the sensible thing by default (prioritize headsets over the speaker). What is is set to do by default now? We are tracking down some audio problems in our app and this might be the cause.

aaalaniz commented 7 years ago

Thanks for the feedback everyone. We added a few tasks to our backlog to accommodate this feature. I will update the ticket when we start the work.

Thanks!

mcorner commented 7 years ago

Any update on this? We are running into some volume issues on wireless headphones. (and when there are no headphones)

aaalaniz commented 7 years ago

No update @mcorner

The resources for this feature have not been allocated. Can you elaborate on the volume issues you are experiencing? Seems worthy of a separate issue.

mcorner commented 7 years ago

Unfortunately, my info is pretty sketchy as it came from users. Basically sometimes the audio is super low through the speakers and they can't hear anything, then they switched to wireless headset and it still was quiet, then they switched to wired and it was fine.

We are using this: setAudioFocus(true); when stating a call, and: setAudioFocus(false); on onDisconnected

  private void setAudioFocus(boolean focus) {
    if (focus) {
      previousAudioMode = audioManager.getMode();
      // Request audio focus before making any device switch.
      audioManager.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL,
      AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
      /*
      * Use MODE_IN_COMMUNICATION as the default audio mode. It is required
      * to be in this mode when playout and/or recording starts for the best
      * possible VoIP performance. Some devices have difficulties with
      * speaker mode if this is not set.
      */
      audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
      /*
      * Always disable microphone mute during a WebRTC call.
      */
      previousMicrophoneMute = audioManager.isMicrophoneMute();
      audioManager.setMicrophoneMute(false);
    } else {
      audioManager.setMode(previousAudioMode);
      audioManager.abandonAudioFocus(null);
      audioManager.setMicrophoneMute(previousMicrophoneMute);
    }
  }
aaalaniz commented 7 years ago

Hey @mcorner

Do you have device info from these customers? Also I highly recommend our Troubleshooting Audio Guide for optimal audio quality.

mcorner commented 7 years ago

I am able to reproduce this on my own Nexus 5x. The call connects fine (the other end is an iOS device). The audio coming out of the nexus is way too low in volume, even if I turn it up to max. It is exactly like the difference between having a phone call volume vs speaker call volume (if I put the phone to my head it is the right volume). If I put in wired headphones it works just fine on Android. I am using the code copied right out of the quickstart for the audioManager.

aaalaniz commented 7 years ago

Thanks @mcorner

I'll have a look.

mcorner commented 7 years ago

I will try experimenting with: setSpeakerphoneOn(true); to see if that will just always route it to the speakerphone at the right volume.

However, looking at other code, it seems this problem might not be as simple as trusting Android to do the "right thing"..... For instance:

https://webrtc.googlesource.com/src/+/master/examples/androidapp/src/org/appspot/apprtc/AppRTCAudioManager.java

It basically routes audio between the speaker, headset, and Bluetooth based on what is plugged in and when.

mcorner commented 7 years ago

Ok, I did some experimentation. Setting the speakerPhone to true fixes the issue. Of course that means that when you plug in a wired headset it still comes out of the speakerphone. Wireless (bluetooth) headsets don't seem to work at all. We could write a bunch of code (like what appears in AppRTCAudioManager above), but that starts to seem like we are doing too much work to get something simple to work. Any hope that Twilio can solve this for us?

aaalaniz commented 7 years ago

Hey @mcorner

What you are suggesting is basically the scope of this issue. We would like to ship something either with the SDK or separately that properly routes audio for developers as opposed to having all our customer's implement this themselves. We have this story in our backlog, but have not allocated a resource to implement it yet.

Apologies for the delay on getting to this feature.

Thanks!

seanadkinson commented 6 years ago

@aaalaniz Any update on Android bluetooth support?

We saw this error in our logs:

java.lang.SecurityException: Need BLUETOOTH permission: Neither user 10107 nor current process has android.permission.BLUETOOTH.

Do we just need to add the <uses-permission android:name="android.permission.BLUETOOTH" /> to our manifest?

aaalaniz commented 6 years ago

Hey @seanadkinson

No resources have been allocated to this story and yes for this error you just need BLUETOOTH permission.

Thanks!

wellington-brito commented 5 years ago

Hello! I would like to ask if, apart from this logic that you have created, it is possible for me to be able to simulate the connection of a headset ... for example: to make the "think" device that has a headset connected. sorry for my english from google translator :D

zackm0571 commented 5 years ago

Hey @PequenoAprendiz,

From doing some research it seems like there isn't an official Android way to do this, but I did find a helper library that allows you to create some mock Bluetooth devices. Hope this helps!

zackm0571 commented 5 years ago

Hey @KarimAbdo,

Thanks for bringing this to our attention. At this time we don't have this on our roadmap. Perhaps in the future we may consider this as an external project.

Regards,

Zack