Simultaneous mic usage - CameraX Video Capture & 3rd Party SDK

40 views
Skip to first unread message

Aubrey Haskett

unread,
Jul 10, 2024, 12:44:05 PM (5 days ago) Jul 10
to Android CameraX Discussion Group
Hi CameraX folks - wondering if anyone has come across this or knows a way to make this work:

I am using CameraX's VideoCapture use case to record video. At the same time, I am streaming to a 3rd party via an SDK. I have the view working and sending info to both places, but not the audio. My audio is only going to VideoCapture, not my 3rd party SDK.

How I do the view:
  1. I create a custom surfaceProvider. The surface given to that surfaceProvider is generated by the 3rd party SDK.
  2. I then use my surfaceProvider to build a preview, which I give to CameraX as the preview use case: 
val surfaceProvider = cameraState.customSurfaceProvider
val preview = Preview.Builder().build().also { it.setSurfaceProvider(surfaceProvider) }

That's all great and works perfectly. But the audio isn't working for the stream, only the recording.

How I do the audio:
  • The 3rd party SDK has an area where you can attach items like a camera or mic. I'm attaching the device's mic.
  • This works and provides the streamed video with audio when I am NOT using the VideoCapture use case at the same time. As soon as a start using VideoCapture alongside the stream, the audio only goes to the recording, not the stream.
  • I queried the SDK and it says that it has properly attached the mic and all should be well. So I believe VideoCapture is consuming the audio and the stream is failing silently. 

Has anyone faced this before? I see a convo in here from last year that might help me but want to get more recent opinions. I really want to continue using CameraX instead of Camera2. 

Thanks. 

Aubrey Haskett

unread,
Jul 11, 2024, 2:59:40 PM (4 days ago) Jul 11
to Android CameraX Discussion Group, Aubrey Haskett
I have a suspicion that the stream via the SDK is receiving silence because two threads can't access the same mic. I am not 100% sure which mic(s) the SDK is trying to use versus CameraX's videoCapture, because both obscure that code. CameraX source code shows a whole lotta of checks to find the right mic(s). So I haven't confirmed this theory, but I think it's the case. 

I am thinking I may have to not use CameraX for VideoCapture. I'll have to set up MediaRecorder with a custom surface (image input I'll send to the SDK's stream, the recording mp4, and the user's device preview), along with an audio stream (goes to recording and SDK's stream). :( Sad because there are still a ton of freebies I want to use from CameraX but have to keep customizing it. 

tonyt...@google.com

unread,
Jul 11, 2024, 11:51:55 PM (4 days ago) Jul 11
to Android CameraX Discussion Group, aubrey....@gc.com
Yes, it depends on the device implementation, but on most devices, the same audio source can only be accessed by one client at a time. CameraX uses CAMCORDER by default. If the SDK tries to access the same source simultaneously, it could receive silence (see the behavior). With that, there's a chance that the audio only goes to one of the clients even if you switch the implementation to MediaRecorder for video recording.

Leo Huang

unread,
Jul 12, 2024, 12:57:32 AM (4 days ago) Jul 12
to tonyt...@google.com, Android CameraX Discussion Group, aubrey....@gc.com
One thing to add. I did some tests and found it behaves on different SDK levels.

Is your test device pre-Android 10? If yes, you could run into the behavior as mentioned in behavior "Pre-Android 10 behavior". Then you can try on newer SDK devices.
```
One more change was added in Android 9: only apps running in the foreground (or a foreground service) could capture the audio input. When an app without a foreground service or foreground UI component started to capture, the app continued running but received silence, even if it was the only app capturing audio at the time.
```

Although the doc of behavior doesn't mention the behavior "within the same application on Android 10+", my test result shows that two AudioRecords instances can receive audio data simultaneously. (Ps. CameraX VideoCapture and MediaRecorder both use AudioRecord under the hook.) Even if two AudioRecords have different AudioSource, sample rate, encoding settings and running on different processes.


'tonyt...@google.com' via Android CameraX Discussion Group <camerax-d...@android.com> 於 2024年7月12日 週五 上午11:52寫道:
--
You received this message because you are subscribed to the Google Groups "Android CameraX Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to camerax-develop...@android.com.
To view this discussion on the web visit https://groups.google.com/a/android.com/d/msgid/camerax-developers/ef26531d-ce72-483c-aa9f-02b7451f9d3fn%40android.com.
Reply all
Reply to author
Forward
0 new messages