Skip to content

Commit 1029fe4

Browse files
committed
MediaDevice change listener implementation
1 parent f14ec93 commit 1029fe4

File tree

61 files changed

+4752
-176
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+4752
-176
lines changed

webrtc-demo/webrtc-demo-api/src/main/java/dev/onvoid/webrtc/demo/presenter/MainPresenter.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
import dev.onvoid.webrtc.demo.model.Contact;
2828
import dev.onvoid.webrtc.demo.service.PeerConnectionService;
2929
import dev.onvoid.webrtc.demo.view.MainView;
30+
import dev.onvoid.webrtc.media.Device;
31+
import dev.onvoid.webrtc.media.DeviceChangeListener;
32+
import dev.onvoid.webrtc.media.MediaDevices;
3033

3134
import java.lang.System.Logger;
3235
import java.lang.System.Logger.Level;
@@ -81,6 +84,21 @@ public void initialize() {
8184

8285
view.setOnClose(this::onClose);
8386

87+
DeviceChangeListener listener = new DeviceChangeListener() {
88+
89+
@Override
90+
public void deviceConnected(Device device) {
91+
LOGGER.log(Level.INFO, "Connected: " + device);
92+
}
93+
94+
@Override
95+
public void deviceDisconnected(Device device) {
96+
LOGGER.log(Level.INFO, "Disconnected: " + device);
97+
}
98+
};
99+
100+
MediaDevices.addDeviceChangeListener(listener);
101+
84102
showStart();
85103
}
86104

webrtc-jni/src/main/cpp/CMakeLists.txt

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,45 @@ if(UNIX AND NOT APPLE)
66
endif()
77

88
if(APPLE)
9-
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
10-
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -s")
9+
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
10+
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -s")
11+
set(SOURCE_TARGET macos)
1112
elseif(LINUX)
12-
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
13+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
1314
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -s")
15+
set(SOURCE_TARGET linux)
1416
elseif(WIN32)
1517
set(CMAKE_CXX_FLAGS_RELEASE "/MT")
1618
set(CMAKE_CXX_FLAGS_DEBUG "/MTd")
19+
set(SOURCE_TARGET windows)
1720
endif()
1821

1922
add_subdirectory(dependencies/webrtc)
2023
add_subdirectory(dependencies/jni-voithos)
2124

22-
file(GLOB_RECURSE SOURCES "src/*.cpp")
25+
file(GLOB SOURCES_ROOT "src/*.cpp")
26+
file(GLOB SOURCES_API "src/api/*.cpp")
27+
file(GLOB SOURCES_MEDIA "src/media/*.cpp")
28+
file(GLOB SOURCES_MEDIA_AUDIO "src/media/audio/*.cpp")
29+
file(GLOB SOURCES_MEDIA_AUDIO_OS "src/media/audio/${SOURCE_TARGET}/*.cpp")
30+
file(GLOB SOURCES_MEDIA_VIDEO "src/media/video/*.cpp")
31+
file(GLOB SOURCES_MEDIA_VIDEO_DESKTOP "src/media/video/desktop/*.cpp")
32+
file(GLOB SOURCES_MEDIA_VIDEO_OS "src/media/video/${SOURCE_TARGET}/*.cpp")
33+
file(GLOB SOURCES_PLATFORM "src/platform/${SOURCE_TARGET}/*.cpp")
34+
file(GLOB SOURCES_RTC "src/rtc/*.cpp")
35+
36+
list(APPEND SOURCES
37+
${SOURCES_ROOT}
38+
${SOURCES_API}
39+
${SOURCES_MEDIA}
40+
${SOURCES_MEDIA_AUDIO}
41+
${SOURCES_MEDIA_AUDIO_OS}
42+
${SOURCES_MEDIA_VIDEO}
43+
${SOURCES_MEDIA_VIDEO_DESKTOP}
44+
${SOURCES_MEDIA_VIDEO_OS}
45+
${SOURCES_PLATFORM}
46+
${SOURCES_RTC}
47+
)
2348

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

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

68+
if(APPLE)
69+
set_source_files_properties(${SOURCES} PROPERTIES COMPILE_FLAGS "-x objective-c++")
70+
target_link_libraries(${PROJECT_NAME} "-framework Foundation" "-framework AVFoundation" "-framework CoreMedia" "-framework CoreAudio")
71+
elseif(LINUX)
72+
target_link_libraries(${PROJECT_NAME} asound pulse udev v4l2)
73+
elseif(WIN32)
74+
target_link_libraries(${PROJECT_NAME} mf.lib mfreadwrite.lib mfplat.lib mfuuid.lib)
75+
endif()
76+
4377
install(TARGETS ${PROJECT_NAME}
4478
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX} COMPONENT Runtime
4579
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX} COMPONENT Runtime
46-
)
80+
)

webrtc-jni/src/main/cpp/include/JNI_MediaDevices.h

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webrtc-jni/src/main/cpp/include/WebRTCContext.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
#define JNI_WEBRTC_CONTEXT_H_
1919

2020
#include "JavaContext.h"
21+
#include "media/audio/AudioDeviceManager.h"
22+
#include "media/video/VideoDeviceManager.h"
2123

2224
#include <jni.h>
25+
#include <memory>
2326

2427
namespace jni
2528
{
@@ -31,6 +34,16 @@ namespace jni
3134

3235
void initialize(JNIEnv * env) override;
3336
void destroy(JNIEnv * env) override;
37+
38+
avdev::AudioDeviceManager * getAudioDeviceManager();
39+
avdev::VideoDeviceManager * getVideoDeviceManager();
40+
41+
private:
42+
void initDeviceManagers();
43+
44+
private:
45+
std::unique_ptr<avdev::AudioDeviceManager> audioDevManager;
46+
std::unique_ptr<avdev::VideoDeviceManager> videoDevManager;
3447
};
3548
}
3649

webrtc-jni/src/main/cpp/include/media/Device.h

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,51 @@
2121
#include "JavaRef.h"
2222

2323
#include <jni.h>
24+
#include <string>
25+
#include <memory>
2426

2527
namespace jni
2628
{
27-
namespace Device
29+
namespace avdev
2830
{
29-
class JavaAudioDeviceClass : public JavaClass
31+
class Device
3032
{
3133
public:
32-
explicit JavaAudioDeviceClass(JNIEnv * env);
34+
virtual ~Device() {};
3335

34-
jclass cls;
35-
jmethodID ctor;
36-
jfieldID guid;
36+
virtual bool operator==(const Device & other);
37+
virtual bool operator!=(const Device & other);
38+
virtual bool operator<(const Device & other);
39+
40+
std::string getName() const;
41+
std::string getDescriptor() const;
42+
43+
protected:
44+
Device(std::string name, std::string descriptor);
45+
46+
private:
47+
const std::string name;
48+
const std::string descriptor;
3749
};
3850

39-
class JavaVideoDeviceClass : public JavaClass
51+
52+
using DevicePtr = std::shared_ptr<Device>;
53+
}
54+
55+
namespace Device
56+
{
57+
class JavaDeviceClass : public JavaClass
4058
{
4159
public:
42-
explicit JavaVideoDeviceClass(JNIEnv * env);
60+
explicit JavaDeviceClass(JNIEnv * env);
4361

4462
jclass cls;
4563
jmethodID ctor;
46-
jfieldID guid;
64+
jfieldID name;
65+
jfieldID descriptor;
4766
};
4867

49-
JavaLocalRef<jobject> toJavaAudioDevice(JNIEnv * env, std::string name, std::string guid);
50-
51-
JavaLocalRef<jobject> toJavaVideoDevice(JNIEnv * env, std::string name, std::string guid);
68+
JavaLocalRef<jobject> toJavaDevice(JNIEnv * env, avdev::DevicePtr device);
5269
}
5370
}
5471

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2019 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef JNI_WEBRTC_MEDIA_DEVICE_CHANGE_LISTENER_H_
18+
#define JNI_WEBRTC_MEDIA_DEVICE_CHANGE_LISTENER_H_
19+
20+
#include "JavaClass.h"
21+
#include "JavaRef.h"
22+
23+
#include "media/DeviceHotplugListener.h"
24+
25+
#include <jni.h>
26+
#include <memory>
27+
28+
namespace jni
29+
{
30+
31+
class DeviceChangeListener : public avdev::DeviceHotplugListener
32+
{
33+
public:
34+
explicit DeviceChangeListener(JNIEnv * env, const JavaGlobalRef<jobject> & listener);
35+
~DeviceChangeListener() = default;
36+
37+
// DeviceHotplugListener implementation.
38+
void deviceConnected(avdev::DevicePtr device) override;
39+
void deviceDisconnected(avdev::DevicePtr device) override;
40+
41+
private:
42+
class JavaDeviceChangeListenerClass : public JavaClass
43+
{
44+
public:
45+
explicit JavaDeviceChangeListenerClass(JNIEnv * env);
46+
47+
jmethodID deviceConnected;
48+
jmethodID deviceDisconnected;
49+
};
50+
51+
private:
52+
JavaGlobalRef<jobject> listener;
53+
54+
const std::shared_ptr<JavaDeviceChangeListenerClass> javaClass;
55+
};
56+
}
57+
58+
#endif
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2019 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef JNI_WEBRTC_MEDIA_DEVICE_HOTPLUG_LISTENER_H_
18+
#define JNI_WEBRTC_MEDIA_DEVICE_HOTPLUG_LISTENER_H_
19+
20+
#include "media/Device.h"
21+
22+
#include <memory>
23+
24+
namespace jni
25+
{
26+
namespace avdev
27+
{
28+
class DeviceHotplugListener
29+
{
30+
public:
31+
virtual ~DeviceHotplugListener() {};
32+
33+
virtual void deviceConnected(DevicePtr device) = 0;
34+
virtual void deviceDisconnected(DevicePtr device) = 0;
35+
};
36+
37+
38+
using PDeviceHotplugListener = std::shared_ptr<DeviceHotplugListener>;
39+
}
40+
}
41+
42+
#endif

0 commit comments

Comments
 (0)