From 51e2046dbcbbb0375c383594aa4f77aa8ed67b06 Mon Sep 17 00:00:00 2001 From: Qiang Chen Date: Tue, 5 Dec 2017 11:11:21 -0800 Subject: [PATCH] Bug Fix: WebRTC Unity Plugin Audio One Way When audio_only is on for the webrtc unity plugin, there is a bug that the audio from hologram cannot be heard at the remote side. Actually we found the audio is transmitted to the remote side, but the remote side wants video data also to playout everything. So without video data, the remote side will drop all the audio data. Thus, on the hologram (using webrtc unity plugin) side, we should not hook up a dummy camera, but instead we should use media constraint to request the remote side to send video data. This CL fixes the bug. Bug: webrtc:8555 Change-Id: I21ddda65185b645088aa4ac15f47b3f8ffad1873 Reviewed-on: https://webrtc-review.googlesource.com/24680 Commit-Queue: Qiang Chen Reviewed-by: George Zhou Cr-Commit-Position: refs/heads/master@{#21094} --- examples/BUILD.gn | 2 ++ .../unityplugin/simple_peer_connection.cc | 36 +++++++++++-------- examples/unityplugin/simple_peer_connection.h | 4 +-- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/examples/BUILD.gn b/examples/BUILD.gn index eef20d4bb8..70d3600cea 100644 --- a/examples/BUILD.gn +++ b/examples/BUILD.gn @@ -657,6 +657,8 @@ if (is_win || is_android) { deps = [ "../api:libjingle_peerconnection_test_api", "../api:video_frame_api", + "../api/audio_codecs:builtin_audio_decoder_factory", + "../api/audio_codecs:builtin_audio_encoder_factory", "../media:rtc_media", "../media:rtc_media_base", "../modules/video_capture:video_capture_module", diff --git a/examples/unityplugin/simple_peer_connection.cc b/examples/unityplugin/simple_peer_connection.cc index 9332a83a27..2ea8227b12 100644 --- a/examples/unityplugin/simple_peer_connection.cc +++ b/examples/unityplugin/simple_peer_connection.cc @@ -12,6 +12,8 @@ #include +#include "api/audio_codecs/builtin_audio_decoder_factory.h" +#include "api/audio_codecs/builtin_audio_encoder_factory.h" #include "api/test/fakeconstraints.h" #include "api/videosourceproxy.h" #include "media/engine/webrtcvideocapturerfactory.h" @@ -91,7 +93,8 @@ bool SimplePeerConnection::InitializePeerConnection(const char** turn_urls, g_peer_connection_factory = webrtc::CreatePeerConnectionFactory( g_worker_thread.get(), g_worker_thread.get(), g_signaling_thread.get(), - nullptr, nullptr, nullptr); + nullptr, webrtc::CreateBuiltinAudioEncoderFactory(), + webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr); } if (!g_peer_connection_factory.get()) { DeletePeerConnection(); @@ -99,19 +102,19 @@ bool SimplePeerConnection::InitializePeerConnection(const char** turn_urls, } g_peer_count++; - if (!CreatePeerConnection(turn_urls, no_of_urls, username, credential, - is_receiver)) { + if (!CreatePeerConnection(turn_urls, no_of_urls, username, credential)) { DeletePeerConnection(); return false; } + + mandatory_receive_ = is_receiver; return peer_connection_.get() != nullptr; } bool SimplePeerConnection::CreatePeerConnection(const char** turn_urls, const int no_of_urls, const char* username, - const char* credential, - bool is_receiver) { + const char* credential) { RTC_DCHECK(g_peer_connection_factory.get() != nullptr); RTC_DCHECK(peer_connection_.get() == nullptr); @@ -148,11 +151,6 @@ bool SimplePeerConnection::CreatePeerConnection(const char** turn_urls, webrtc::FakeConstraints constraints; constraints.SetAllowDtlsSctpDataChannels(); - if (is_receiver) { - constraints.SetMandatoryReceiveAudio(true); - constraints.SetMandatoryReceiveVideo(true); - } - peer_connection_ = g_peer_connection_factory->CreatePeerConnection( config_, &constraints, nullptr, nullptr, this); @@ -192,7 +190,12 @@ bool SimplePeerConnection::CreateOffer() { if (!peer_connection_.get()) return false; - peer_connection_->CreateOffer(this, nullptr); + webrtc::FakeConstraints constraints; + if (mandatory_receive_) { + constraints.SetMandatoryReceiveAudio(true); + constraints.SetMandatoryReceiveVideo(true); + } + peer_connection_->CreateOffer(this, &constraints); return true; } @@ -200,7 +203,12 @@ bool SimplePeerConnection::CreateAnswer() { if (!peer_connection_.get()) return false; - peer_connection_->CreateAnswer(this, nullptr); + webrtc::FakeConstraints constraints; + if (mandatory_receive_) { + constraints.SetMandatoryReceiveAudio(true); + constraints.SetMandatoryReceiveVideo(true); + } + peer_connection_->CreateAnswer(this, &constraints); return true; } @@ -421,8 +429,8 @@ void SimplePeerConnection::AddStreams(bool audio_only) { RTC_DCHECK(texture_helper != nullptr) << "Cannot get the Surface Texture Helper."; - rtc::scoped_refptr source( - new rtc::RefCountedObject( + rtc::scoped_refptr source( + new rtc::RefCountedObject( g_signaling_thread.get(), env, texture_helper, false)); rtc::scoped_refptr proxy_source = webrtc::VideoTrackSourceProxy::Create(g_signaling_thread.get(), diff --git a/examples/unityplugin/simple_peer_connection.h b/examples/unityplugin/simple_peer_connection.h index 0c7a2615d7..0c490253ee 100644 --- a/examples/unityplugin/simple_peer_connection.h +++ b/examples/unityplugin/simple_peer_connection.h @@ -64,8 +64,7 @@ class SimplePeerConnection : public webrtc::PeerConnectionObserver, bool CreatePeerConnection(const char** turn_urls, const int no_of_urls, const char* username, - const char* credential, - bool is_receiver); + const char* credential); void CloseDataChannel(); std::unique_ptr OpenVideoCaptureDevice(); void SetAudioControl(); @@ -127,6 +126,7 @@ class SimplePeerConnection : public webrtc::PeerConnectionObserver, bool is_mute_audio_ = false; bool is_record_audio_ = false; + bool mandatory_receive_ = false; // disallow copy-and-assign SimplePeerConnection(const SimplePeerConnection&) = delete;