Custom audio device in android WebRTC using libjingle

2.7k Views Asked by At

I am developing native android WebRTC client that is suppoded to stream audio from custom device (I am getting audio stream via Bluetooth from that device). I am using libjingle library to implement WebRTC and I wonder if and how it is possible to hook up custom audio stream to audio track?

Currently I am adding default audio track like this:

localMS = factory.createLocalMediaStream("ARDAMS");

AudioSource audioSource = factory.createAudioSource(new MediaConstraints());
localMS.addTrack(factory.createAudioTrack("ARDAMSa0", audioSource));

I saw that there is WebRtcAuidioRecord (https://github.com/pristineio/webrtc-android/blob/master/libjingle_peerconnection/src/main/java/org/webrtc/voiceengine/WebRtcAudioRecord.java) - is it possible to override it?

Anybody tried doing something like that?

1

There are 1 best solutions below

0
On

Your post lead me to the below code, I am going to try it and let you know if I get it to work. I am trying to send one audio stream to Watson API and one to WebRTC but Android only lets one InputStream read for the microphone. I will update you if I get it to work.

private org.webrtc.MediaStream createMediaStream() {
    org.webrtc.MediaStream mediaStream = mFactory.createLocalMediaStream(ARDAMS);

    if (mEnableVideo) {
        mVideoCapturer = createVideoCapturer();
        if (mVideoCapturer != null) {
            mediaStream.addTrack(createVideoTrack(mVideoCapturer));
        } else {
            mEnableVideo = false;
        }
    }

    if (mEnableAudio) {
        createAudioCapturer();
        mediaStream.addTrack(mFactory.createAudioTrack(
                AUDIO_TRACK_ID,
                mFactory.createAudioSource(mAudioConstraints)));
    }

    return mediaStream;
}

/**
 * Creates a instance of WebRtcAudioRecord.
 */
private void createAudioCapturer() {
    if (mOption.getAudioType() == PeerOption.AudioType.EXTERNAL_RESOURCE) {
        WebRtcAudioRecord.setAudioRecordModuleFactory(new WebRtcAudioRecordModuleFactory() {
            @Override
            public WebRtcAudioRecordModule create() {
                AudioCapturerExternalResource module = new AudioCapturerExternalResource();
                module.setUri(mOption.getAudioUri());
                module.setSampleRate(mOption.getAudioSampleRate());
                module.setBitDepth(mOption.getAudioBitDepth());
                module.setChannel(mOption.getAudioChannel());
                return module;
            }
        });
    } else {
        WebRtcAudioRecord.setAudioRecordModuleFactory(null);
    }
}

Source: https://www.programcreek.com/java-api-examples/?code=DeviceConnect/DeviceConnect-Android/DeviceConnect-Android-master/dConnectDevicePlugin/dConnectDeviceWebRTC/app/src/main/java/org/deviceconnect/android/deviceplugin/webrtc/core/MediaStream.java