Skip to content

Commit

Permalink
MediaDevice change listener implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
devopvoid committed Mar 12, 2020
1 parent f14ec93 commit 1029fe4
Show file tree
Hide file tree
Showing 61 changed files with 4,752 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import dev.onvoid.webrtc.demo.model.Contact;
import dev.onvoid.webrtc.demo.service.PeerConnectionService;
import dev.onvoid.webrtc.demo.view.MainView;
import dev.onvoid.webrtc.media.Device;
import dev.onvoid.webrtc.media.DeviceChangeListener;
import dev.onvoid.webrtc.media.MediaDevices;

import java.lang.System.Logger;
import java.lang.System.Logger.Level;
Expand Down Expand Up @@ -81,6 +84,21 @@ public void initialize() {

view.setOnClose(this::onClose);

DeviceChangeListener listener = new DeviceChangeListener() {

@Override
public void deviceConnected(Device device) {
LOGGER.log(Level.INFO, "Connected: " + device);
}

@Override
public void deviceDisconnected(Device device) {
LOGGER.log(Level.INFO, "Disconnected: " + device);
}
};

MediaDevices.addDeviceChangeListener(listener);

showStart();
}

Expand Down
44 changes: 39 additions & 5 deletions webrtc-jni/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,45 @@ if(UNIX AND NOT APPLE)
endif()

if(APPLE)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -s")
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -s")
set(SOURCE_TARGET macos)
elseif(LINUX)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -s")
set(SOURCE_TARGET linux)
elseif(WIN32)
set(CMAKE_CXX_FLAGS_RELEASE "/MT")
set(CMAKE_CXX_FLAGS_DEBUG "/MTd")
set(SOURCE_TARGET windows)
endif()

add_subdirectory(dependencies/webrtc)
add_subdirectory(dependencies/jni-voithos)

file(GLOB_RECURSE SOURCES "src/*.cpp")
file(GLOB SOURCES_ROOT "src/*.cpp")
file(GLOB SOURCES_API "src/api/*.cpp")
file(GLOB SOURCES_MEDIA "src/media/*.cpp")
file(GLOB SOURCES_MEDIA_AUDIO "src/media/audio/*.cpp")
file(GLOB SOURCES_MEDIA_AUDIO_OS "src/media/audio/${SOURCE_TARGET}/*.cpp")
file(GLOB SOURCES_MEDIA_VIDEO "src/media/video/*.cpp")
file(GLOB SOURCES_MEDIA_VIDEO_DESKTOP "src/media/video/desktop/*.cpp")
file(GLOB SOURCES_MEDIA_VIDEO_OS "src/media/video/${SOURCE_TARGET}/*.cpp")
file(GLOB SOURCES_PLATFORM "src/platform/${SOURCE_TARGET}/*.cpp")
file(GLOB SOURCES_RTC "src/rtc/*.cpp")

list(APPEND SOURCES
${SOURCES_ROOT}
${SOURCES_API}
${SOURCES_MEDIA}
${SOURCES_MEDIA_AUDIO}
${SOURCES_MEDIA_AUDIO_OS}
${SOURCES_MEDIA_VIDEO}
${SOURCES_MEDIA_VIDEO_DESKTOP}
${SOURCES_MEDIA_VIDEO_OS}
${SOURCES_PLATFORM}
${SOURCES_RTC}
)

add_library(${PROJECT_NAME} SHARED ${SOURCES})

Expand All @@ -40,7 +65,16 @@ set_target_properties(${PROJECT_NAME} PROPERTIES
target_link_libraries(${PROJECT_NAME} jni-voithos)
target_link_libraries(${PROJECT_NAME} webrtc)

if(APPLE)
set_source_files_properties(${SOURCES} PROPERTIES COMPILE_FLAGS "-x objective-c++")
target_link_libraries(${PROJECT_NAME} "-framework Foundation" "-framework AVFoundation" "-framework CoreMedia" "-framework CoreAudio")
elseif(LINUX)
target_link_libraries(${PROJECT_NAME} asound pulse udev v4l2)
elseif(WIN32)
target_link_libraries(${PROJECT_NAME} mf.lib mfreadwrite.lib mfplat.lib mfuuid.lib)
endif()

install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX} COMPONENT Runtime
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX} COMPONENT Runtime
)
)
16 changes: 16 additions & 0 deletions webrtc-jni/src/main/cpp/include/JNI_MediaDevices.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions webrtc-jni/src/main/cpp/include/WebRTCContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
#define JNI_WEBRTC_CONTEXT_H_

#include "JavaContext.h"
#include "media/audio/AudioDeviceManager.h"
#include "media/video/VideoDeviceManager.h"

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

namespace jni
{
Expand All @@ -31,6 +34,16 @@ namespace jni

void initialize(JNIEnv * env) override;
void destroy(JNIEnv * env) override;

avdev::AudioDeviceManager * getAudioDeviceManager();
avdev::VideoDeviceManager * getVideoDeviceManager();

private:
void initDeviceManagers();

private:
std::unique_ptr<avdev::AudioDeviceManager> audioDevManager;
std::unique_ptr<avdev::VideoDeviceManager> videoDevManager;
};
}

Expand Down
41 changes: 29 additions & 12 deletions webrtc-jni/src/main/cpp/include/media/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,51 @@
#include "JavaRef.h"

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

namespace jni
{
namespace Device
namespace avdev
{
class JavaAudioDeviceClass : public JavaClass
class Device
{
public:
explicit JavaAudioDeviceClass(JNIEnv * env);
virtual ~Device() {};

jclass cls;
jmethodID ctor;
jfieldID guid;
virtual bool operator==(const Device & other);
virtual bool operator!=(const Device & other);
virtual bool operator<(const Device & other);

std::string getName() const;
std::string getDescriptor() const;

protected:
Device(std::string name, std::string descriptor);

private:
const std::string name;
const std::string descriptor;
};

class JavaVideoDeviceClass : public JavaClass

using DevicePtr = std::shared_ptr<Device>;
}

namespace Device
{
class JavaDeviceClass : public JavaClass
{
public:
explicit JavaVideoDeviceClass(JNIEnv * env);
explicit JavaDeviceClass(JNIEnv * env);

jclass cls;
jmethodID ctor;
jfieldID guid;
jfieldID name;
jfieldID descriptor;
};

JavaLocalRef<jobject> toJavaAudioDevice(JNIEnv * env, std::string name, std::string guid);

JavaLocalRef<jobject> toJavaVideoDevice(JNIEnv * env, std::string name, std::string guid);
JavaLocalRef<jobject> toJavaDevice(JNIEnv * env, avdev::DevicePtr device);
}
}

Expand Down
58 changes: 58 additions & 0 deletions webrtc-jni/src/main/cpp/include/media/DeviceChangeListener.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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_WEBRTC_MEDIA_DEVICE_CHANGE_LISTENER_H_
#define JNI_WEBRTC_MEDIA_DEVICE_CHANGE_LISTENER_H_

#include "JavaClass.h"
#include "JavaRef.h"

#include "media/DeviceHotplugListener.h"

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

namespace jni
{

class DeviceChangeListener : public avdev::DeviceHotplugListener
{
public:
explicit DeviceChangeListener(JNIEnv * env, const JavaGlobalRef<jobject> & listener);
~DeviceChangeListener() = default;

// DeviceHotplugListener implementation.
void deviceConnected(avdev::DevicePtr device) override;
void deviceDisconnected(avdev::DevicePtr device) override;

private:
class JavaDeviceChangeListenerClass : public JavaClass
{
public:
explicit JavaDeviceChangeListenerClass(JNIEnv * env);

jmethodID deviceConnected;
jmethodID deviceDisconnected;
};

private:
JavaGlobalRef<jobject> listener;

const std::shared_ptr<JavaDeviceChangeListenerClass> javaClass;
};
}

#endif
42 changes: 42 additions & 0 deletions webrtc-jni/src/main/cpp/include/media/DeviceHotplugListener.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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_WEBRTC_MEDIA_DEVICE_HOTPLUG_LISTENER_H_
#define JNI_WEBRTC_MEDIA_DEVICE_HOTPLUG_LISTENER_H_

#include "media/Device.h"

#include <memory>

namespace jni
{
namespace avdev
{
class DeviceHotplugListener
{
public:
virtual ~DeviceHotplugListener() {};

virtual void deviceConnected(DevicePtr device) = 0;
virtual void deviceDisconnected(DevicePtr device) = 0;
};


using PDeviceHotplugListener = std::shared_ptr<DeviceHotplugListener>;
}
}

#endif

0 comments on commit 1029fe4

Please sign in to comment.