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
2 changes: 1 addition & 1 deletion docs/guide/client-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ The new camera keeps the resolution and frame rate of the old one, as far as it

### Devices

`media.devices()` returns the cameras, microphones and speakers. The first call reads them from the system; after that, the lists are snapshots kept current in the background, so reading them is cheap on any thread. Where the system reports no device changes, as on a Linux machine without PulseAudio, they are read on each call instead. `screens()` and `windows()` ask the system on each call. A kind of device the system cannot list, such as audio devices on a machine without an audio system, comes back as an empty list; a query that fails otherwise throws a `MediaException`, so an empty list means there are no such devices.
`media.devices()` returns the cameras, microphones and speakers. The first call reads them from the system; after that, the lists are snapshots kept current in the background, so reading them is cheap on any thread. Where the system reports no device changes, as on a Linux machine without PulseAudio, they are read on each call instead. `screens()` and `windows()` ask the system on each call. A query that fails, including an unavailable audio backend, throws a `MediaException`, so an empty list means there are no such devices. Its cause is an `AudioBackendUnavailableException` when the audio backend is missing or unreachable, or a `DeviceException` for other native device failures.

```java
Devices devices = media.devices();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2019 Alex Andres
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef JNI_AUDIO_BACKEND_UNAVAILABLE_EXCEPTION_H_
#define JNI_AUDIO_BACKEND_UNAVAILABLE_EXCEPTION_H_

#include <stdexcept>

namespace jni {
// Expected backend absence, distinct from other device initialization failures.
class AudioBackendUnavailableException : public std::runtime_error {
public:
using std::runtime_error::runtime_error;
};
}

#endif
76 changes: 76 additions & 0 deletions webrtc-jni/src/main/cpp/src/JNI_MediaDevices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#include "JavaArrayList.h"
#include "JavaContext.h"
#include "JavaError.h"
#include "JavaUtils.h"
#include "Exception.h"
#include "media/AudioBackendUnavailableException.h"
#include <new>
#include "media/DeviceChangeListener.h"
#include "media/audio/AudioDevice.h"
#include "media/audio/AudioDeviceManager.h"
Expand All @@ -40,6 +44,15 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_MediaDevices_addDeviceChange

javaContext->addNativeRef(env, jni::JavaLocalRef<jobject>(env, jListener), listenerPtr);
}
catch (const jni::AudioBackendUnavailableException & e) {
ThrowJavaException(env, "dev/onvoid/webrtc/media/AudioBackendUnavailableException", e.what());
}
catch (const std::bad_alloc &) {
ThrowJavaException(env, "java/lang/OutOfMemoryError", "Allocating native device resources failed");
}
catch (const jni::Exception & e) {
ThrowJavaException(env, "dev/onvoid/webrtc/media/DeviceException", e.what());
}
catch (...) {
ThrowCxxJavaException(env);
}
Expand All @@ -55,6 +68,15 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_MediaDevices_removeDeviceCha
context->getAudioDeviceManager()->detachHotplugListener(listener);
context->getVideoDeviceManager()->detachHotplugListener(listener);
}
catch (const jni::AudioBackendUnavailableException & e) {
ThrowJavaException(env, "dev/onvoid/webrtc/media/AudioBackendUnavailableException", e.what());
}
catch (const std::bad_alloc &) {
ThrowJavaException(env, "java/lang/OutOfMemoryError", "Allocating native device resources failed");
}
catch (const jni::Exception & e) {
ThrowJavaException(env, "dev/onvoid/webrtc/media/DeviceException", e.what());
}
catch (...) {
ThrowCxxJavaException(env);
}
Expand All @@ -74,6 +96,15 @@ JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_media_MediaDevices_getDefaultAu

return jni::AudioDevice::toJavaAudioDevice(env, device).release();
}
catch (const jni::AudioBackendUnavailableException & e) {
ThrowJavaException(env, "dev/onvoid/webrtc/media/AudioBackendUnavailableException", e.what());
}
catch (const std::bad_alloc &) {
ThrowJavaException(env, "java/lang/OutOfMemoryError", "Allocating native device resources failed");
}
catch (const jni::Exception & e) {
ThrowJavaException(env, "dev/onvoid/webrtc/media/DeviceException", e.what());
}
catch (...) {
ThrowCxxJavaException(env);
}
Expand All @@ -95,6 +126,15 @@ JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_media_MediaDevices_getDefaultAu

return jni::AudioDevice::toJavaAudioDevice(env, device).release();
}
catch (const jni::AudioBackendUnavailableException & e) {
ThrowJavaException(env, "dev/onvoid/webrtc/media/AudioBackendUnavailableException", e.what());
}
catch (const std::bad_alloc &) {
ThrowJavaException(env, "java/lang/OutOfMemoryError", "Allocating native device resources failed");
}
catch (const jni::Exception & e) {
ThrowJavaException(env, "dev/onvoid/webrtc/media/DeviceException", e.what());
}
catch (...) {
ThrowCxxJavaException(env);
}
Expand All @@ -118,6 +158,15 @@ JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_media_MediaDevices_getAudioRend

return deviceList.listObject().release();
}
catch (const jni::AudioBackendUnavailableException & e) {
ThrowJavaException(env, "dev/onvoid/webrtc/media/AudioBackendUnavailableException", e.what());
}
catch (const std::bad_alloc &) {
ThrowJavaException(env, "java/lang/OutOfMemoryError", "Allocating native device resources failed");
}
catch (const jni::Exception & e) {
ThrowJavaException(env, "dev/onvoid/webrtc/media/DeviceException", e.what());
}
catch (...) {
ThrowCxxJavaException(env);
}
Expand All @@ -141,6 +190,15 @@ JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_media_MediaDevices_getAudioCapt

return deviceList.listObject().release();
}
catch (const jni::AudioBackendUnavailableException & e) {
ThrowJavaException(env, "dev/onvoid/webrtc/media/AudioBackendUnavailableException", e.what());
}
catch (const std::bad_alloc &) {
ThrowJavaException(env, "java/lang/OutOfMemoryError", "Allocating native device resources failed");
}
catch (const jni::Exception & e) {
ThrowJavaException(env, "dev/onvoid/webrtc/media/DeviceException", e.what());
}
catch (...) {
ThrowCxxJavaException(env);
}
Expand All @@ -164,6 +222,15 @@ JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_media_MediaDevices_getVideoCapt

return deviceList.listObject().release();
}
catch (const jni::AudioBackendUnavailableException & e) {
ThrowJavaException(env, "dev/onvoid/webrtc/media/AudioBackendUnavailableException", e.what());
}
catch (const std::bad_alloc &) {
ThrowJavaException(env, "java/lang/OutOfMemoryError", "Allocating native device resources failed");
}
catch (const jni::Exception & e) {
ThrowJavaException(env, "dev/onvoid/webrtc/media/DeviceException", e.what());
}
catch (...) {
ThrowCxxJavaException(env);
}
Expand Down Expand Up @@ -193,6 +260,15 @@ JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_media_MediaDevices_getVideoCapt

return capabilityList.listObject().release();
}
catch (const jni::AudioBackendUnavailableException & e) {
ThrowJavaException(env, "dev/onvoid/webrtc/media/AudioBackendUnavailableException", e.what());
}
catch (const std::bad_alloc &) {
ThrowJavaException(env, "java/lang/OutOfMemoryError", "Allocating native device resources failed");
}
catch (const jni::Exception & e) {
ThrowJavaException(env, "dev/onvoid/webrtc/media/DeviceException", e.what());
}
catch (...) {
ThrowCxxJavaException(env);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "media/audio/linux/PulseAudioDeviceManager.h"
#include "media/audio/linux/PulseAudioLoader.h"
#include "Exception.h"
#include "media/AudioBackendUnavailableException.h"

namespace jni
{
Expand All @@ -26,7 +27,7 @@ namespace jni
{
// Try to load PulseAudio symbols dynamically
if (!PulseAudioLoader::instance().load()) {
throw Exception("PulseAudio: Library not found on system.");
throw AudioBackendUnavailableException("PulseAudio: Library not found on system.");
}

// Use the loader instance for function calls
Expand All @@ -48,7 +49,7 @@ namespace jni
context = pa.pa_context_new(mainloopApi, "MediaDevices");

if (!context) {
pa.pa_threaded_mainloop_free(mainloop);
dispose();

throw Exception("PulseAudio: Could not create context");
}
Expand All @@ -59,18 +60,19 @@ namespace jni
pa.pa_threaded_mainloop_lock(mainloop);

if (pa.pa_context_connect(context, nullptr, PA_CONTEXT_NOFLAGS, nullptr) < 0) {
pa.pa_context_unref(context);
pa.pa_threaded_mainloop_free(mainloop);
pa.pa_threaded_mainloop_unlock(mainloop);
dispose();

throw Exception("PulseAudio: Could not connect to the context");
throw AudioBackendUnavailableException("PulseAudio: Could not connect to the context");
}

while (true) {
pa_context_state_t contextState = pa.pa_context_get_state(context);

if (contextState == PA_CONTEXT_FAILED || contextState == PA_CONTEXT_TERMINATED) {
pa.pa_threaded_mainloop_unlock(mainloop);
dispose();
return;
throw AudioBackendUnavailableException("PulseAudio: Context connection failed");
}
if (contextState == PA_CONTEXT_READY) {
break;
Expand All @@ -87,6 +89,7 @@ namespace jni
pa.pa_threaded_mainloop_unlock(mainloop);

if (!op) {
dispose();
throw Exception("PulseAudio: Failed to subscribe context");
}
pa.pa_operation_unref(op);
Expand Down Expand Up @@ -114,6 +117,7 @@ namespace jni
context = nullptr;
}

pa.pa_threaded_mainloop_unlock(mainloop);
pa.pa_threaded_mainloop_stop(mainloop);
pa.pa_threaded_mainloop_free(mainloop);

Expand Down
28 changes: 17 additions & 11 deletions webrtc/src/main/java/dev/onvoid/webrtc/client/CodecPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,34 @@ void apply() {
return;
}

for (RTCRtpTransceiver transceiver : peer.connection().getTransceivers()) {
try {
apply(peer.connection().getTransceivers(), codecs);
}

/** Takes ownership of every wrapper, including those not reached on failure. */
static void apply(RTCRtpTransceiver[] transceivers, Map<MediaKind, List<RTCRtpCodecCapability>> codecs) {
try {
for (RTCRtpTransceiver transceiver : transceivers) {
if (transceiver.stopped()) {
continue;
}

RTCRtpReceiver receiver = transceiver.getReceiver();
MediaStreamTrack track = receiver.getTrack();
MediaKind kind = MediaStreamTrack.AUDIO_TRACK_KIND.equals(track.getKind())
? MediaKind.AUDIO
: MediaKind.VIDEO;

track.dispose();
receiver.dispose();
MediaKind kind;
try (RTCRtpReceiver receiver = transceiver.getReceiver();
MediaStreamTrack track = receiver.getTrack()) {
kind = MediaStreamTrack.AUDIO_TRACK_KIND.equals(track.getKind())
? MediaKind.AUDIO
: MediaKind.VIDEO;
}

List<RTCRtpCodecCapability> preferred = codecs.get(kind);

if (preferred != null) {
transceiver.setCodecPreferences(preferred);
}
}
finally {
}
finally {
for (RTCRtpTransceiver transceiver : transceivers) {
transceiver.dispose();
}
}
Expand Down
6 changes: 2 additions & 4 deletions webrtc/src/main/java/dev/onvoid/webrtc/client/Devices.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@
* The media devices of the system. The lists are snapshots, kept current in
* the background, so reading them is cheap on any thread. Where the system
* reports no device changes, as a Linux machine without PulseAudio, they are
* read on each call. A kind of device the system cannot list, such as audio
* devices without an audio system, comes back as an empty list. A query that
* fails otherwise throws a {@link MediaException}, so that an empty list means
* that there are no such devices.
* read on each call. A query that fails, including an unavailable audio backend,
* throws a {@link MediaException}; an empty list means there are no such devices.
*/
public interface Devices {

Expand Down
22 changes: 4 additions & 18 deletions webrtc/src/main/java/dev/onvoid/webrtc/client/DevicesImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.function.Consumer;
import java.util.function.Supplier;

import dev.onvoid.webrtc.media.AudioBackendUnavailableException;
import dev.onvoid.webrtc.media.Device;
import dev.onvoid.webrtc.media.DeviceChangeListener;
import dev.onvoid.webrtc.media.MediaDevices;
Expand All @@ -45,9 +46,8 @@
* reports no changes, as a Linux machine without PulseAudio, the lists are
* read on each call instead.
* <p>
* A list the platform cannot read is empty: a machine without an audio system
* has no microphones to offer. A query that fails otherwise throws a {@link
* MediaException} when the list is read, and errors of the JVM pass through.
* A query that fails, including an unavailable audio backend, throws a
* {@link MediaException} when the list is read. Errors of the JVM pass through.
*/
final class DevicesImpl implements Devices {

Expand Down Expand Up @@ -278,16 +278,6 @@ static void switchDevice(Runnable select, Runnable stop, Runnable start) {
}
}

/**
* Tells whether a query failed in the platform, which the native layer
* reports as a plain {@code Error} with the message of the C++ exception,
* as when there is no audio system. Its subclasses are errors of the JVM,
* such as an {@code OutOfMemoryError} or an {@code UnsatisfiedLinkError}.
*/
static boolean platformFailure(Throwable e) {
return e.getClass() == Error.class;
}

/** Called on a thread of the system, so it only posts. */
private void changed() {
media.executor.execute(() -> {
Expand Down Expand Up @@ -315,10 +305,6 @@ private static <T> Snapshot<T> read(String kind, Supplier<List<T>> query) {
: new Snapshot<>(Collections.unmodifiableList(new ArrayList<>(devices)), kind, null);
}
catch (Throwable e) {
if (platformFailure(e)) {
// No device of the kind, such as without an audio system.
return Snapshot.empty();
}
if (e instanceof Error) {
throw (Error) e;
}
Expand All @@ -333,7 +319,7 @@ private boolean watch() {
return true;
}
catch (Throwable e) {
if (platformFailure(e)) {
if (e instanceof AudioBackendUnavailableException) {
// The system reports no changes, as Linux without PulseAudio.
return false;
}
Expand Down
12 changes: 6 additions & 6 deletions webrtc/src/main/java/dev/onvoid/webrtc/client/Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ public Builder maxBitrate(int bitsPerSecond) {
}

/**
* @param framesPerSecond the highest frame rate of a video track.
* @param framesPerSecond the finite, positive highest frame rate of a video track.
*
* @return this builder.
*/
public Builder maxFrameRate(double framesPerSecond) {
if (framesPerSecond <= 0) {
throw new IllegalArgumentException("The frame rate must be positive, got " + framesPerSecond);
if (!Double.isFinite(framesPerSecond) || framesPerSecond <= 0) {
throw new IllegalArgumentException("The frame rate must be finite and positive, got " + framesPerSecond);
}

this.maxFrameRate = framesPerSecond;
Expand All @@ -105,13 +105,13 @@ public Builder maxFrameRate(double framesPerSecond) {

/**
* @param factor how much smaller than captured a video track is sent,
* 1 or more.
* finite and 1 or more.
*
* @return this builder.
*/
public Builder scaleDownBy(double factor) {
if (factor < 1) {
throw new IllegalArgumentException("The factor must be 1 or more, got " + factor);
if (!Double.isFinite(factor) || factor < 1) {
throw new IllegalArgumentException("The factor must be finite and 1 or more, got " + factor);
}

this.scaleDownBy = factor;
Expand Down
Loading
Loading