Skip to content

Commit

Permalink
Audio/video device (Windows) handling consistent with the internal API
Browse files Browse the repository at this point in the history
  • Loading branch information
devopvoid committed Jul 18, 2021
1 parent 6550c3c commit 702e58c
Show file tree
Hide file tree
Showing 21 changed files with 253 additions and 731 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ bool ExceptionCheck(JNIEnv * env)

void ThrowCxxJavaException(JNIEnv * env)
{
std::exception_ptr eptr = std::current_exception();

if (!eptr) {
return;
}

try {
std::rethrow_exception(std::current_exception());
std::rethrow_exception(eptr);
}
catch (const jni::JavaWrappedException & e) {
env->Throw(e.exception().get());
Expand All @@ -48,12 +54,7 @@ void ThrowCxxJavaException(JNIEnv * env)
env->Throw(jni::JavaError(env, e.what()));
}
catch (...) {
try {
std::rethrow_exception(std::current_exception());
}
catch (const std::exception & e) {
env->Throw(jni::JavaError(env, e.what()));
}
env->Throw(jni::JavaError(env, "Unhandled Exception"));
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ namespace jni
{
namespace avdev
{
class MFAudioDeviceManager : public AudioDeviceManager, IMMNotificationClient
class WindowsAudioDeviceManager : public AudioDeviceManager, IMMNotificationClient
{
public:
MFAudioDeviceManager();
~MFAudioDeviceManager();
WindowsAudioDeviceManager();
~WindowsAudioDeviceManager();

std::set<AudioDevicePtr> getAudioCaptureDevices() override;
std::set<AudioDevicePtr> getAudioPlaybackDevices() override;
Expand Down
7 changes: 5 additions & 2 deletions webrtc-jni/src/main/cpp/include/media/video/VideoDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ namespace jni
namespace avdev
{
class VideoDevice : public Device
{
{
public:
VideoDevice(std::string name, std::string descriptor);
virtual ~VideoDevice() {};
};


using VideoDevicePtr = std::shared_ptr<VideoDevice>;
}

namespace VideoDevice
Expand All @@ -48,7 +51,7 @@ namespace jni
jfieldID descriptor;
};

JavaLocalRef<jobject> toJavaVideoDevice(JNIEnv * env, avdev::DevicePtr device);
JavaLocalRef<jobject> toJavaVideoDevice(JNIEnv * env, const avdev::VideoDevice & device);
avdev::VideoDevice toNativeVideoDevice(JNIEnv * env, const JavaRef<jobject> & javaType);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "modules/video_capture/video_capture.h"
#include "modules/video_capture/video_capture_defines.h"

#include "media/video/VideoDevice.h"

namespace jni
{
class VideoTrackDeviceSource : public webrtc::VideoTrackSource, public rtc::VideoSinkInterface<webrtc::VideoFrame>
Expand All @@ -34,7 +36,7 @@ namespace jni
VideoTrackDeviceSource();
~VideoTrackDeviceSource();

void setDeviceUid(const std::string & deviceUid);
void setVideoDevice(const avdev::VideoDevicePtr & device);
void setVideoCaptureCapability(const webrtc::VideoCaptureCapability & capability);

void start();
Expand All @@ -56,7 +58,7 @@ namespace jni
void destroy();

private:
std::string deviceUid;
avdev::VideoDevicePtr device;
webrtc::VideoCaptureCapability capability;

rtc::scoped_refptr<webrtc::VideoCaptureModule> captureModule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ namespace jni
{
namespace avdev
{
class MFVideoDeviceManager : public VideoDeviceManager, WinHotplugNotifier
class WindowsVideoDeviceManager : public VideoDeviceManager, WinHotplugNotifier
{
public:
MFVideoDeviceManager();
~MFVideoDeviceManager() = default;
WindowsVideoDeviceManager();
~WindowsVideoDeviceManager() = default;

std::set<VideoDevicePtr> getVideoCaptureDevices() override;
std::set<VideoCaptureCapability> getVideoCaptureCapabilities(const VideoDevice & device) override;
Expand Down
4 changes: 0 additions & 4 deletions webrtc-jni/src/main/cpp/src/JNI_AudioDeviceModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_audio_AudioDeviceModule_setR
jni::JavaObject obj(env, jni::JavaLocalRef<jobject>(env, device));

const auto javaClass = jni::JavaClasses::get<jni::AudioDevice::JavaAudioDeviceClass>(env);
const std::string devName = jni::JavaString::toNative(env, obj.getString(javaClass->name));
const std::string devGuid = jni::JavaString::toNative(env, obj.getString(javaClass->descriptor));

uint16_t index = 0;
Expand All @@ -162,9 +161,6 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_audio_AudioDeviceModule_setR

for (int i = 0; i < deviceCount; ++i) {
if ((audioModule->RecordingDeviceName(i, name, guid) == 0) && devGuid == std::string(guid)) {
printf("selected audio device: %s - %s\n", name, guid);
fflush(NULL);

index = i;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion webrtc-jni/src/main/cpp/src/JNI_MediaDevices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ JNIEXPORT jobject JNICALL Java_dev_onvoid_webrtc_media_MediaDevices_getVideoCapt
jni::JavaArrayList deviceList(env, devices.size());

for (const auto & device : devices) {
deviceList.add(jni::VideoDevice::toJavaVideoDevice(env, device));
deviceList.add(jni::VideoDevice::toJavaVideoDevice(env, *device.get()));
}

return deviceList.listObject().release();
Expand Down
1 change: 0 additions & 1 deletion webrtc-jni/src/main/cpp/src/JNI_VideoCapture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoCapture_setVideoC
jni::VideoCapture * videoSource = GetHandle<jni::VideoCapture>(env, caller);
CHECK_HANDLE(videoSource);

const auto javaClass = jni::JavaClasses::get<jni::VideoDevice::JavaVideoDeviceClass>(env);
const auto dev = jni::VideoDevice::toNativeVideoDevice(env, jni::JavaLocalRef<jobject>(env, device));

videoSource->setDevice(std::make_shared<jni::avdev::VideoDevice>(dev.getName(), dev.getDescriptor()));
Expand Down
7 changes: 2 additions & 5 deletions webrtc-jni/src/main/cpp/src/JNI_VideoDeviceSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoDeviceSource_setV
jni::VideoTrackDeviceSource * videoSource = GetHandle<jni::VideoTrackDeviceSource>(env, caller);
CHECK_HANDLE(videoSource);

jni::JavaObject obj(env, jni::JavaLocalRef<jobject>(env, device));
const auto dev = jni::VideoDevice::toNativeVideoDevice(env, jni::JavaLocalRef<jobject>(env, device));

const auto javaClass = jni::JavaClasses::get<jni::VideoDevice::JavaVideoDeviceClass>(env);
const std::string guid = jni::JavaString::toNative(env, obj.getString(javaClass->descriptor));

videoSource->setDeviceUid(guid);
videoSource->setVideoDevice(std::make_shared<jni::avdev::VideoDevice>(dev.getName(), dev.getDescriptor()));
}

JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoDeviceSource_setVideoCaptureCapability
Expand Down
8 changes: 4 additions & 4 deletions webrtc-jni/src/main/cpp/src/WebRTCContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

#ifdef _WIN32
#include <windows.h>
#include "media/audio/windows/MFAudioDeviceManager.h"
#include "media/video/windows/MFVideoDeviceManager.h"
#include "media/audio/windows/WindowsAudioDeviceManager.h"
#include "media/video/windows/WindowsVideoDeviceManager.h"
#endif
#ifdef __linux__
#include "media/audio/linux/PulseAudioDeviceManager.h"
Expand Down Expand Up @@ -123,8 +123,8 @@ namespace jni
void WebRTCContext::initDeviceManagers()
{
#ifdef _WIN32
audioDevManager = std::make_unique<avdev::MFAudioDeviceManager>();
videoDevManager = std::make_unique<avdev::MFVideoDeviceManager>();
audioDevManager = std::make_unique<avdev::WindowsAudioDeviceManager>();
videoDevManager = std::make_unique<avdev::WindowsVideoDeviceManager>();
#endif
#ifdef __linux__
audioDevManager = std::make_unique<avdev::PulseAudioDeviceManager>();
Expand Down
6 changes: 4 additions & 2 deletions webrtc-jni/src/main/cpp/src/media/DeviceChangeListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ namespace jni
jdevice = AudioDevice::toJavaAudioDevice(env, device);
}
else if (dynamic_cast<jni::avdev::VideoDevice *>(device.get())) {
jdevice = VideoDevice::toJavaVideoDevice(env, device);
const auto dev = dynamic_cast<jni::avdev::VideoDevice*>(device.get());
jdevice = VideoDevice::toJavaVideoDevice(env, *dev);
}

if (jdevice) {
Expand All @@ -56,7 +57,8 @@ namespace jni
jdevice = AudioDevice::toJavaAudioDevice(env, device);
}
else if (dynamic_cast<jni::avdev::VideoDevice *>(device.get())) {
jdevice = VideoDevice::toJavaVideoDevice(env, device);
const auto dev = dynamic_cast<jni::avdev::VideoDevice *>(device.get());
jdevice = VideoDevice::toJavaVideoDevice(env, *dev);
}

if (jdevice) {
Expand Down

0 comments on commit 702e58c

Please sign in to comment.