Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions .github/actions/test-natives/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ runs:
name: natives-media-${{ inputs.platform-name }}
path: natives

# PulseAudio, libudev and libdbus are deliberately not installed here: the
# natives open them at runtime, and the armhf lane, whose image has none of
# them, proves that they load without them.
# PulseAudio, libudev and libdbus are deliberately not installed as runtime
# libraries: the natives open them at runtime, and the armhf lane, whose
# image has none of them, proves that they load without them.
- name: Install 32-bit runtime libraries
if: runner.os == 'Linux' && inputs.java-architecture == 'armv7'
run: |
Expand All @@ -75,6 +75,18 @@ runs:
sudo apt-get install -y libx11-6 libxext6 libxfixes3 libxdamage1 libxtst6 libxrandr2 libxcomposite1 libglib2.0-0 libgbm1 libdrm2
shell: bash

# Not a runtime library either: DeviceBackendTests starts a PulseAudio
# server of its own, with virtual devices only, to remove a default device
# while the natives use it. The armhf lane keeps running without it. No
# autospawn, so that the other tests still find no server running.
- name: Install a PulseAudio server for the device tests
if: runner.os == 'Linux' && inputs.java-architecture != 'armv7'
run: |
sudo apt-get install -y --no-install-recommends pulseaudio pulseaudio-utils
mkdir -p ~/.config/pulse
echo "autospawn = no" > ~/.config/pulse/client.conf
shell: bash

- name: Set up JDK ${{ inputs.java-version }}
uses: actions/setup-java@v6
with:
Expand Down
4 changes: 3 additions & 1 deletion docs/guide/media/media-devices.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ for (VideoCaptureCapability capability : capabilities) {

## Hotplug Events

You can listen for device hotplug events to be notified when devices are connected or disconnected:
You can listen for device hotplug events to be notified when devices are connected or disconnected.
Listeners are called on a thread of the library, one change at a time and in order, so a listener may
query `MediaDevices` again, for example for the new default device:

```java
// Import required classes
Expand Down
12 changes: 10 additions & 2 deletions webrtc-jni/src/main/cpp/include/api/JavaCallbackThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,20 @@ namespace jni
// their media. So these observers are queued and called here instead, one
// at a time and in the order they were posted.
//
// The thread starts on first use and is attached to the JVM as a daemon,
// so that it never holds up the JVM's exit.
// Device change listeners have a thread of their own, for the same reason:
// the operating system notifies them on its own thread, a PulseAudio main
// loop or an MMDevice notification thread, which the device queries they
// make wait for. A separate one, so that a listener waiting for an observer
// does not wait for itself.
//
// The threads start on first use and are attached to the JVM as daemons,
// so that they never hold up the JVM's exit.
class JavaCallbackThread
{
public:
static void Post(absl::AnyInvocable<void(JNIEnv *) &&> callback);

static void PostDeviceChange(absl::AnyInvocable<void(JNIEnv *) &&> callback);
};
}

Expand Down
13 changes: 12 additions & 1 deletion webrtc-jni/src/main/cpp/include/media/DeviceChangeListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@

#include "media/DeviceHotplugListener.h"

#include <atomic>
#include <jni.h>
#include <memory>

namespace jni
{

class DeviceChangeListener : public avdev::DeviceHotplugListener
// Calls a Java DeviceChangeListener on the device change thread of
// JavaCallbackThread, not on the thread of the operating system that
// reports the change.
class DeviceChangeListener : public avdev::DeviceHotplugListener, public std::enable_shared_from_this<DeviceChangeListener>
{
public:
explicit DeviceChangeListener(JNIEnv * env, const JavaGlobalRef<jobject> & listener);
Expand All @@ -38,8 +42,13 @@ namespace jni
void deviceConnected(avdev::DevicePtr device) override;
void deviceDisconnected(avdev::DevicePtr device) override;

// Drops the changes that are still queued for the Java listener,
// once it has been removed.
void detach();

private:
void notify(const avdev::DevicePtr & device, jmethodID method);
void call(JNIEnv * env, const avdev::DevicePtr & device, jmethodID method);

private:
class JavaDeviceChangeListenerClass : public JavaClass
Expand All @@ -54,6 +63,8 @@ namespace jni
private:
JavaGlobalRef<jobject> listener;

std::atomic<bool> detached;

const std::shared_ptr<JavaDeviceChangeListenerClass> javaClass;
};
}
Expand Down
4 changes: 4 additions & 0 deletions webrtc-jni/src/main/cpp/src/JNI_MediaDevices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_MediaDevices_removeDeviceCha
jni::WebRTCContext * context = static_cast<jni::WebRTCContext *>(javaContext);
auto listener = javaContext->removeNativeRef<jni::DeviceChangeListener>(env, jni::JavaLocalRef<jobject>(env, jListener));

if (listener) {
listener->detach();
}

try {
context->getAudioDeviceManager()->detachHotplugListener(listener);
context->getVideoDeviceManager()->detachHotplugListener(listener);
Expand Down
42 changes: 26 additions & 16 deletions webrtc-jni/src/main/cpp/src/api/JavaCallbackThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace jni
{
struct Queue
{
const char * name;
std::mutex mutex;
std::condition_variable condition;
std::deque<absl::AnyInvocable<void(JNIEnv *) &&>> callbacks;
Expand All @@ -43,7 +44,7 @@ namespace jni

JavaVMAttachArgs args;
args.version = JNI_VERSION_1_6;
args.name = const_cast<char *>("WebRTC-Callbacks");
args.name = const_cast<char *>(queue->name);
args.group = nullptr;

if (vm->AttachCurrentThreadAsDaemon(reinterpret_cast<void **>(&env), &args) != JNI_OK) {
Expand Down Expand Up @@ -77,32 +78,41 @@ namespace jni
}
}

Queue & GetQueue()
// Never destroyed: its thread waits on it for as long as the process
// runs.
Queue * StartQueue(const char * name)
{
// Never destroyed: its thread waits on it for as long as the
// process runs.
static Queue * queue = [] {
Queue * created = new Queue();
Queue * queue = new Queue();
queue->name = name;

std::thread(Run, created).detach();
std::thread(Run, queue).detach();

return created;
}();
return queue;
}

void Enqueue(Queue & queue, absl::AnyInvocable<void(JNIEnv *) &&> callback)
{
{
std::lock_guard<std::mutex> lock(queue.mutex);

queue.callbacks.push_back(std::move(callback));
}

return *queue;
queue.condition.notify_one();
}
}

void JavaCallbackThread::Post(absl::AnyInvocable<void(JNIEnv *) &&> callback)
{
Queue & queue = GetQueue();
static Queue * queue = StartQueue("WebRTC-Callbacks");

{
std::lock_guard<std::mutex> lock(queue.mutex);
Enqueue(*queue, std::move(callback));
}

queue.callbacks.push_back(std::move(callback));
}
void JavaCallbackThread::PostDeviceChange(absl::AnyInvocable<void(JNIEnv *) &&> callback)
{
static Queue * queue = StartQueue("WebRTC-DeviceChanges");

queue.condition.notify_one();
Enqueue(*queue, std::move(callback));
}
}
38 changes: 31 additions & 7 deletions webrtc-jni/src/main/cpp/src/media/DeviceChangeListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "media/DeviceChangeListener.h"
#include "api/JavaCallbackThread.h"
#include "api/WebRTCUtils.h"
#include "media/audio/AudioDevice.h"
#include "media/video/VideoDevice.h"
Expand All @@ -31,6 +32,7 @@ namespace jni
{
DeviceChangeListener::DeviceChangeListener(JNIEnv * env, const JavaGlobalRef<jobject> & listener) :
listener(listener),
detached(false),
javaClass(JavaClasses::get<JavaDeviceChangeListenerClass>(env))
{
}
Expand All @@ -45,19 +47,41 @@ namespace jni
notify(device, javaClass->deviceDisconnected);
}

void DeviceChangeListener::detach()
{
detached = true;
}

void DeviceChangeListener::notify(const avdev::DevicePtr & device, jmethodID method)
{
JNIEnv * env = AttachCurrentThread();
// This runs on a thread the operating system owns (a CoreAudio queue, the MMDevice
// notification thread, the PulseAudio main loop, ...), which has no handler for a C++
// exception. A Java listener called here would also hold up the next change, and a device
// query it makes would wait for the very thread it runs on: the PulseAudio main loop
// answers only once the listener has returned. Attached to the JVM, the thread would
// moreover keep it from ending, as these threads run as long as the process does.
std::shared_ptr<DeviceChangeListener> self = weak_from_this().lock();

if (env == nullptr) {
if (!self || detached) {
return;
}

// This runs on a thread the operating system owns (a CoreAudio queue, the MMDevice
// notification thread, the PulseAudio main loop, ...), which has no handler for a C++
// exception and never returns to Java. So nothing may be thrown from here, and what the
// Java listener throws is reported and cleared: left pending, it would be there for the
// next JNI call on this thread, which may be the next listener or the next event.
try {
JavaCallbackThread::PostDeviceChange([self, device, method](JNIEnv * env) {
if (!self->detached) {
self->call(env, device, method);
}
});
}
catch (...) {
RTC_LOG(LS_ERROR) << "Device change notification failed";
}
}

void DeviceChangeListener::call(JNIEnv * env, const avdev::DevicePtr & device, jmethodID method)
{
// What the Java listener throws is reported and cleared: left pending, it would be there
// for the next JNI call on this thread, which may be the next listener or the next event.
try {
JavaLocalRef<jobject> jdevice = nullptr;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,24 @@ namespace jni
if (!pa.pa_threaded_mainloop_in_thread(mainloop))
iterate(mainloop, op);

op = pa.pa_context_get_source_info_by_name(context, defaultCaptureName.c_str(), getSourceInfoCallback, this);
// Without a source the server names no default, and an empty name is no query at all.
if (defaultCaptureName.empty()) {
defaultCaptureDescName.clear();
}
else {
op = pa.pa_context_get_source_info_by_name(context, defaultCaptureName.c_str(), getSourceInfoCallback, this);

if (!pa.pa_threaded_mainloop_in_thread(mainloop))
iterate(mainloop, op);
if (!pa.pa_threaded_mainloop_in_thread(mainloop))
iterate(mainloop, op);
}

if (!pa.pa_threaded_mainloop_in_thread(mainloop))
pa.pa_threaded_mainloop_unlock(mainloop);

if (defaultCaptureName.empty() || defaultCaptureDescName.empty()) {
return nullptr;
}

AudioDevicePtr defaultDevice = std::make_shared<AudioDevice>(defaultCaptureDescName, defaultCaptureName);
defaultDevice->directionType = AudioDeviceDirectionType::adtCapture;
return defaultDevice;
Expand Down Expand Up @@ -191,14 +201,24 @@ namespace jni
if (!pa.pa_threaded_mainloop_in_thread(mainloop))
iterate(mainloop, op);

op = pa.pa_context_get_sink_info_by_name(context, defaultPlaybackName.c_str(), getSinkInfoCallback, this);
// Without a sink the server names no default, and an empty name is no query at all.
if (defaultPlaybackName.empty()) {
defaultPlaybackDescName.clear();
}
else {
op = pa.pa_context_get_sink_info_by_name(context, defaultPlaybackName.c_str(), getSinkInfoCallback, this);

if (!pa.pa_threaded_mainloop_in_thread(mainloop))
iterate(mainloop, op);
if (!pa.pa_threaded_mainloop_in_thread(mainloop))
iterate(mainloop, op);
}

if (!pa.pa_threaded_mainloop_in_thread(mainloop))
pa.pa_threaded_mainloop_unlock(mainloop);

if (defaultPlaybackName.empty() || defaultPlaybackDescName.empty()) {
return nullptr;
}

AudioDevicePtr defaultDevice = std::make_shared<AudioDevice>(defaultPlaybackDescName, defaultPlaybackName);
defaultDevice->directionType = AudioDeviceDirectionType::adtRender;
return defaultDevice;
Expand Down Expand Up @@ -226,12 +246,17 @@ namespace jni

auto& pa = PulseAudioLoader::instance();

if (last > 0) {
pa.pa_threaded_mainloop_signal(engine->mainloop, 0);
// A failed query, e.g. for a source removed after the server named it the default,
// ends with last < 0 and no info, and nothing follows it.
if (last < 0) {
engine->defaultCaptureDescName.clear();
}
else if (last == 0 && info) {
engine->defaultCaptureDescName = info->description ? info->description : "";
return;
}

engine->defaultCaptureDescName = info->description;
pa.pa_threaded_mainloop_signal(engine->mainloop, 0);
}

void PulseAudioDeviceManager::getSourceCallback(pa_context * ctx, const pa_source_info * info, int last, void * userdata)
Expand Down Expand Up @@ -266,13 +291,18 @@ namespace jni
{
PulseAudioDeviceManager * engine = reinterpret_cast<PulseAudioDeviceManager *>(userdata);

if (last > 0) {
auto& pa = PulseAudioLoader::instance();
pa.pa_threaded_mainloop_signal(engine->mainloop, 0);
// A failed query, e.g. for a sink removed after the server named it the default,
// ends with last < 0 and no info, and nothing follows it.
if (last < 0) {
engine->defaultPlaybackDescName.clear();
}
else if (last == 0 && info) {
engine->defaultPlaybackDescName = info->description ? info->description : "";
return;
}

engine->defaultPlaybackDescName = info->description;
auto& pa = PulseAudioLoader::instance();
pa.pa_threaded_mainloop_signal(engine->mainloop, 0);
}

void PulseAudioDeviceManager::getSinkCallback(pa_context * ctx, const pa_sink_info * info, int last, void * userdata)
Expand Down Expand Up @@ -350,8 +380,9 @@ namespace jni
auto& pa = PulseAudioLoader::instance();

PulseAudioDeviceManager * engine = reinterpret_cast<PulseAudioDeviceManager *>(userdata);
engine->defaultCaptureName = info->default_source_name;
engine->defaultPlaybackName = info->default_sink_name;
// A failed query has no info, and a server without sources or sinks names no default.
engine->defaultCaptureName = info && info->default_source_name ? info->default_source_name : "";
engine->defaultPlaybackName = info && info->default_sink_name ? info->default_sink_name : "";

pa.pa_threaded_mainloop_signal(engine->mainloop, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
/**
* A device change listener is notified whenever a media device such as a
* camera, microphone, or speaker is connected to or removed from the system.
* <p>
* Listeners are called on a thread of the library, not on the one the system
* reports the change on, one change at a time and in the order they were
* reported. A listener may query {@link MediaDevices}, which then reflects the
* change. Once a listener has been removed, it gets no more changes, apart from
* one it may be handling at that moment.
*
* @author Alex Andres
*/
Expand Down
Loading
Loading