Skip to content

Commit 432a519

Browse files
committed
enhancement #9: microphone support
1 parent e29ead3 commit 432a519

File tree

10 files changed

+43
-51
lines changed

10 files changed

+43
-51
lines changed

Plugin/android/Android.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ include $(CLEAR_VARS)
66
#SYSROOT := $(abspath $(LOCAL_PATH))
77

88
LOCAL_MODULE := UnityGStreamerPlugin
9-
LOCAL_SRC_FILES := ../src/DebugLog.cpp ../src/RenderingPlugin.cpp ../src/GstDataPipeline.cpp ../src/GstBasePipeline.cpp ../src/GstAVPipeline.cpp ../src/GstAVPipelineOpenGLES.cpp
9+
LOCAL_SRC_FILES := ../src/DebugLog.cpp ../src/RenderingPlugin.cpp ../src/GstDataPipeline.cpp ../src/GstBasePipeline.cpp ../src/GstAVPipeline.cpp ../src/GstAVPipelineOpenGLES.cpp ../src/GstMicPipeline.cpp
1010

1111
LOCAL_SHARED_LIBRARIES := gstreamer_android
1212
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv2

Plugin/src/GstMicPipeline.cpp

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,35 @@
22
This source code is licensed under the license found in the
33
LICENSE file in the root directory of this source tree. */
44

5-
#include "DebugLog.h"
65
#include "GstMicPipeline.h"
6+
#include "DebugLog.h"
7+
#include "Unity/PlatformBase.h"
78

89
GstMicPipeline::GstMicPipeline() : GstBasePipeline("MicPipeline") {}
910

10-
void GstMicPipeline::CreatePipeline(const char* uri, const char* remote_peer_id)
11+
void GstMicPipeline::CreatePipeline(const char* uri, const char* remote_peer_id)
1112
{
1213
Debug::Log("GstMicPipeline create pipeline", Level::Info);
1314
Debug::Log(uri, Level::Info);
1415
Debug::Log(remote_peer_id, Level::Info);
1516

1617
GstBasePipeline::CreatePipeline();
1718

18-
GstElement* wasapi2src = add_wasapi2src(pipeline_);
19+
#if UNITY_WIN
20+
GstElement* audiosrc = add_wasapi2src(pipeline_);
21+
#elif UNITY_ANDROID
22+
GstElement* audiosrc = add_by_name(pipeline_, "openslessrc");
23+
#endif
1924
GstElement* webrtcdsp = add_webrtcdsp(pipeline_);
20-
GstElement* audioconvert = add_audioconvert(pipeline_);
21-
GstElement* queue = add_queue(pipeline_);
25+
GstElement* audioconvert = add_by_name(pipeline_, "audioconvert");
26+
GstElement* audioresample = add_by_name(pipeline_, "audioresample");
27+
GstElement* queue = add_by_name(pipeline_, "queue");
2228
GstElement* opusenc = add_opusenc(pipeline_);
2329
GstElement* audio_caps_capsfilter = add_audio_caps_capsfilter(pipeline_);
2430
GstElement* webrtcsink = add_webrtcsink(pipeline_, uri);
2531

26-
if (!gst_element_link_many(wasapi2src, queue, audioconvert, webrtcdsp, opusenc, audio_caps_capsfilter, webrtcsink, nullptr))
32+
if (!gst_element_link_many(audiosrc, queue, audioconvert, audioresample, webrtcdsp, opusenc, audio_caps_capsfilter,
33+
webrtcsink, nullptr))
2734
{
2835
Debug::Log("Audio sending elements could not be linked.", Level::Error);
2936
}
@@ -45,32 +52,6 @@ GstElement* GstMicPipeline::add_wasapi2src(GstElement* pipeline)
4552
return wasapi2src;
4653
}
4754

48-
GstElement* GstMicPipeline::add_queue(GstElement* pipeline)
49-
{
50-
GstElement* queue = gst_element_factory_make("queue", nullptr);
51-
if (!queue)
52-
{
53-
Debug::Log("Failed to create queue", Level::Error);
54-
return nullptr;
55-
}
56-
57-
gst_bin_add(GST_BIN(pipeline), queue);
58-
return queue;
59-
}
60-
61-
GstElement* GstMicPipeline::add_audioconvert(GstElement* pipeline)
62-
{
63-
GstElement* audioconvert = gst_element_factory_make("audioconvert", nullptr);
64-
if (!audioconvert)
65-
{
66-
Debug::Log("Failed to create audioconvert", Level::Error);
67-
return nullptr;
68-
}
69-
70-
gst_bin_add(GST_BIN(pipeline), audioconvert);
71-
return audioconvert;
72-
}
73-
7455
GstElement* GstMicPipeline::add_opusenc(GstElement* pipeline)
7556
{
7657
GstElement* opusenc = gst_element_factory_make("opusenc", nullptr);
@@ -80,7 +61,7 @@ GstElement* GstMicPipeline::add_opusenc(GstElement* pipeline)
8061
return nullptr;
8162
}
8263

83-
g_object_set(opusenc, "audio-type", "restricted-lowdelay", "frame-size", 10, nullptr);
64+
g_object_set(opusenc, "audio-type", /*"restricted-lowdelay"*/ 2051, "frame-size", 10, nullptr);
8465

8566
gst_bin_add(GST_BIN(pipeline), opusenc);
8667
return opusenc;
@@ -134,7 +115,7 @@ GstElement* GstMicPipeline::add_webrtcsink(GstElement* pipeline, const std::stri
134115
gst_structure_free(meta_structure);
135116
g_value_unset(&meta_value);
136117

137-
g_object_set(webrtcsink, "stun-server", nullptr, "do-restransmission", false, nullptr);
118+
g_object_set(webrtcsink, "stun-server", nullptr, "do-retransmission", false, nullptr);
138119

139120
g_signal_connect(webrtcsink, "consumer-added", G_CALLBACK(consumer_added_callback), nullptr);
140121

Plugin/src/GstMicPipeline.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ class GstMicPipeline : public GstBasePipeline
1313

1414
void CreatePipeline(const char* uri, const char* remote_peer_id);
1515

16-
private:
16+
private:
1717
static void consumer_added_callback(GstElement* consumer_id, gchararray webrtcbin, GstElement* arg1, gpointer udata);
18-
static GstElement* add_queue(GstElement* pipeline);
19-
static GstElement* add_audioconvert(GstElement* pipeline);
2018
static GstElement* add_wasapi2src(GstElement* pipeline);
2119
static GstElement* add_opusenc(GstElement* pipeline);
2220
static GstElement* add_audio_caps_capsfilter(GstElement* pipeline);

Plugin/src/RenderingPlugin.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
LICENSE file in the root directory of this source tree. */
44

55
#include "GstDataPipeline.h"
6+
#include "GstMicPipeline.h"
67
#include "Unity/IUnityGraphics.h"
78
#include "Unity/PlatformBase.h"
8-
//#include <assert.h>
9+
// #include <assert.h>
910

1011
#if UNITY_WIN
1112
#include "GstAVPipelineD3D11.h"
12-
#include "GstMicPipeline.h"
1313

14-
static std::unique_ptr<GstMicPipeline> gstMicPipeline = nullptr;
1514
static std::unique_ptr<GstAVPipelineD3D11> gstAVPipeline = nullptr;
1615

1716
#elif UNITY_ANDROID
@@ -33,6 +32,7 @@ static jobject surface_plugin = nullptr;
3332

3433
#endif
3534

35+
static std::unique_ptr<GstMicPipeline> gstMicPipeline = nullptr;
3636
static std::unique_ptr<GstDataPipeline> gstDataPipeline = nullptr;
3737

3838
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API CreateDevice()
@@ -55,9 +55,7 @@ extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API SetSurface(jobject su
5555
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API CreatePipeline(const char* uri, const char* remote_peer_id)
5656
{
5757
gstAVPipeline->CreatePipeline(uri, remote_peer_id);
58-
#if UNITY_WIN
5958
gstMicPipeline->CreatePipeline(uri, remote_peer_id);
60-
#endif
6159
}
6260

6361
extern "C" UNITY_INTERFACE_EXPORT void* UNITY_INTERFACE_API CreateTexture(unsigned int width, unsigned int height, bool left)
@@ -76,9 +74,7 @@ extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API ReleaseTexture(void*
7674
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API DestroyPipeline()
7775
{
7876
gstAVPipeline->DestroyPipeline();
79-
#if UNITY_WIN
8077
gstMicPipeline->DestroyPipeline();
81-
#endif
8278
}
8379

8480
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API DestroyDataPipeline() { gstDataPipeline->DestroyPipeline(); }
@@ -127,12 +123,11 @@ extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnit
127123
#if UNITY_WIN
128124
gst_init(nullptr, nullptr);
129125
gstAVPipeline = std::make_unique<GstAVPipelineD3D11>(unityInterfaces);
130-
gstMicPipeline = std::make_unique<GstMicPipeline>();
131126
#elif UNITY_ANDROID
132127
// gst_init done in the java side
133128
gstAVPipeline = std::make_unique<GstAVPipelineOpenGLES>(unityInterfaces);
134129
#endif
135-
130+
gstMicPipeline = std::make_unique<GstMicPipeline>();
136131
gstDataPipeline = std::make_unique<GstDataPipeline>();
137132
}
138133

UnityProject/Assets/Plugins/Android/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
</intent-filter>
1313
</activity>
1414
</application>
15+
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
1516
</manifest>

UnityProject/Packages/com.pollenrobotics.gstreamerwebrtc/Runtime/Java/src/com/pollenrobotics/gstreamer/RenderingCallbackManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.pollenrobotics.gstreamer;
22

33
import java.util.ArrayList;
4+
import android.util.Log;
45

56
public class RenderingCallbackManager {
67
@SuppressWarnings("JniMissingFunction")

UnityProject/Packages/com.pollenrobotics.gstreamerwebrtc/Runtime/Java/src/com/pollenrobotics/gstreamer/UnityRenderTexture.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public Surface GetSurface(){
8181
@Override
8282
public void onFrameAvailable(SurfaceTexture surfaceTexture) {
8383
RenderingCallbackManager.Instance().SubscribeOneShot(eventCode->{
84-
surfaceTexture.updateTexImage();
84+
surfaceTexture.updateTexImage();
8585
});
8686
}
8787
}

UnityProject/Packages/com.pollenrobotics.gstreamerwebrtc/Runtime/Plugins/Android/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
</intent-filter>
1313
</activity>
1414
</application>
15+
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
1516
</manifest>
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:f49406b7de9ab8a4283dd32b397d7a6aaac6fa296fcfff7bb40aed887fc926f0
3-
size 95624
2+
oid sha256:478baa7e5a9c46f36421df63c64c809406bbe7baeec0487568625df2fe7c0ff0
3+
size 105560

UnityProject/Packages/com.pollenrobotics.gstreamerwebrtc/Runtime/Scripts/GstreamerRenderingPlugin.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LICENSE file in the root directory of this source tree. */
88
using UnityEngine.Rendering;
99
using UnityEngine.Events;
1010
using System.Collections;
11-
11+
using UnityEngine.Android;
1212

1313
namespace GstreamerWebRTC
1414
{
@@ -96,6 +96,8 @@ public GStreamerRenderingPlugin(string ip_address, ref Texture leftTexture, ref
9696
_command = new CommandBuffer();
9797

9898
#if UNITY_ANDROID
99+
CheckMicPermission();
100+
99101
_command.IssuePluginEvent(GetRenderEventFunc(), 1);
100102
Camera.main.AddCommandBuffer(CameraEvent.BeforeSkybox, _command);
101103

@@ -122,6 +124,19 @@ public GStreamerRenderingPlugin(string ip_address, ref Texture leftTexture, ref
122124
}
123125

124126
#if UNITY_ANDROID
127+
private void CheckMicPermission()
128+
{
129+
if (Permission.HasUserAuthorizedPermission(Permission.Microphone))
130+
{
131+
Debug.Log("Microphone permission already granted");
132+
}
133+
else
134+
{
135+
Permission.RequestUserPermission(Permission.Microphone);
136+
}
137+
138+
}
139+
125140
public bool IsNativePtrSet()
126141
{
127142
return nativeTexPtrSet;

0 commit comments

Comments
 (0)