peer_connection.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. #include "pch.h"
  2. #include <memory>
  3. #include "../common/comm.h"
  4. #include "api.h"
  5. #include "callback.h"
  6. #include "data_channel_observer.h"
  7. #include "video_frame_observer.h"
  8. #include "audio_frame_observer.h"
  9. #include "peer_connection.h"
  10. #ifdef WEBRTC_LINUX
  11. #include "capture_op.h"
  12. #endif
  13. struct SessionDescObserver : public webrtc::SetSessionDescriptionObserver {
  14. public:
  15. SessionDescObserver() = default;
  16. template <typename Closure>
  17. SessionDescObserver(Closure&& callback)
  18. : callback_(std::forward<Closure>(callback)) {}
  19. void OnSuccess() override {
  20. if (callback_)
  21. callback_();
  22. }
  23. void OnFailure(webrtc::RTCError error) override
  24. {
  25. }
  26. protected:
  27. std::function<void()> callback_;
  28. ~SessionDescObserver() override = default;
  29. };
  30. struct SetRemoteSessionDescObserver
  31. : public webrtc::SetRemoteDescriptionObserverInterface {
  32. public:
  33. void OnSetRemoteDescriptionComplete(webrtc::RTCError error) override {}
  34. };
  35. const std::string kAudioVideoStreamId("local_av_stream");
  36. void ensureNullTerminatedCString(std::string& str) {
  37. if (str.empty() || (str.back() != '\0')) {
  38. str.push_back('\0');
  39. }
  40. }
  41. PeerConnection::PeerConnection() = default;
  42. PeerConnection::~PeerConnection() {
  43. // Ensure that observers (sinks) are removed, otherwise the media pipelines
  44. // will continue to try to feed them with data after they're destroyed, or
  45. // try to notify of some incoming data on data tracks.
  46. RemoveLocalVideoTrack();
  47. RemoveLocalAudioTrack();
  48. for (auto stream : remote_streams_) {
  49. if (auto* sink = remote_video_observer_.get()) {
  50. for (auto&& video_track : stream->GetVideoTracks()) {
  51. video_track->RemoveSink(sink);
  52. }
  53. }
  54. if (auto* sink = remote_audio_observer_.get()) {
  55. for (auto&& audio_track : stream->GetAudioTracks()) {
  56. audio_track->RemoveSink(sink);
  57. }
  58. }
  59. }
  60. //RemoveAllDataTracks();
  61. }
  62. void PeerConnection::SetPeerImpl(
  63. rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer) {
  64. peer_ = std::move(peer);
  65. local_video_observer_.reset(new VideoFrameObserver());
  66. remote_video_observer_.reset(new VideoFrameObserver());
  67. local_audio_observer_.reset(new AudioFrameObserver());
  68. remote_audio_observer_.reset(new AudioFrameObserver());
  69. }
  70. bool PeerConnection::AddLocalVideoTrack(
  71. rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track,const std::string& stream) {
  72. if (local_video_track_) {
  73. return false;
  74. }
  75. auto result = peer_->AddTrack(video_track, { stream });
  76. if (result.ok()) {
  77. if (local_video_observer_) {
  78. rtc::VideoSinkWants sink_settings{};
  79. sink_settings.rotation_applied = true;
  80. video_track->AddOrUpdateSink(local_video_observer_.get(), sink_settings);
  81. }
  82. local_video_sender_ = result.value();
  83. local_video_track_ = std::move(video_track);
  84. return true;
  85. }
  86. return false;
  87. }
  88. void PeerConnection::RemoveLocalVideoTrack() {
  89. if (!local_video_track_)
  90. return;
  91. if (auto* sink = local_video_observer_.get()) {
  92. local_video_track_->RemoveSink(sink);
  93. }
  94. peer_->RemoveTrack(local_video_sender_);
  95. local_video_track_ = nullptr;
  96. local_video_sender_ = nullptr;
  97. }
  98. bool PeerConnection::AddLocalAudioTrack(
  99. rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track) {
  100. if (local_audio_track_) {
  101. return false;
  102. }
  103. auto result = peer_->AddTrack(audio_track, {kAudioVideoStreamId});
  104. if (result.ok()) {
  105. if (auto* sink = local_audio_observer_.get()) {
  106. audio_track->AddSink(sink);
  107. }
  108. local_audio_sender_ = result.value();
  109. local_audio_track_ = std::move(audio_track);
  110. return true;
  111. }
  112. return false;
  113. }
  114. void PeerConnection::RemoveLocalAudioTrack() {
  115. if (!local_audio_track_)
  116. return;
  117. if (auto* sink = local_audio_observer_.get()) {
  118. local_audio_track_->RemoveSink(sink);
  119. }
  120. peer_->RemoveTrack(local_audio_sender_);
  121. local_audio_track_ = nullptr;
  122. local_audio_sender_ = nullptr;
  123. }
  124. void PeerConnection::RegisterDataChannelCallback(
  125. DataChannelMessageCallback message_callback,
  126. DataChannelBufferingCallback buffering_callback,
  127. DataChannelStateCallback state_callback) {
  128. data_channel_state_callback_ = state_callback;
  129. data_channel_buffering_callback_ = buffering_callback;
  130. data_channel_message_callback_ = message_callback;
  131. }
  132. mrsResult PeerConnection::AddDataChannel(
  133. const char* label,
  134. bool ordered,
  135. bool reliable//,
  136. // DataChannelMessageCallback message_callback,
  137. // DataChannelBufferingCallback buffering_callback,
  138. // DataChannelStateCallback state_callback
  139. ) {
  140. webrtc::DataChannelInit config{};
  141. config.ordered = ordered;
  142. config.reliable = reliable;
  143. config.id = -1;
  144. if (!sctp_negotiated_) {
  145. // Don't try to create a data channel without SCTP negotiation, it will get
  146. // stuck in the kConnecting state forever.
  147. return MRS_E_SCTP_NOT_NEGOTIATED;
  148. }
  149. std::string labelString = label;
  150. rtc::scoped_refptr<webrtc::DataChannelInterface> dataChannel =
  151. peer_->CreateDataChannel(labelString, &config);
  152. if (dataChannel) {
  153. DataChannelObserver* observer{
  154. new DataChannelObserver(dataChannel)};
  155. observer->SetMessageCallback(data_channel_message_callback_);
  156. observer->SetBufferingCallback(data_channel_buffering_callback_);
  157. observer->SetStateCallback(data_channel_state_callback_);
  158. dataChannel->RegisterObserver(observer);
  159. channel_ob_server.reset(std::move(observer));
  160. // if (!labelString.empty()) {
  161. // data_channel_from_label_.emplace(
  162. // std::make_pair(std::move(labelString), observer));
  163. // }
  164. // if (config.id >= 0) {
  165. // data_channel_from_id_.try_emplace(config.id, std::move(observer));
  166. // }
  167. return MRS_SUCCESS;
  168. }
  169. return MRS_E_UNKNOWN;
  170. }
  171. bool PeerConnection::RemoveDataChannel() {
  172. auto* data_channel = channel_ob_server->data_channel();
  173. data_channel->UnregisterObserver();
  174. data_channel->Close();
  175. return true;
  176. }
  177. bool PeerConnection::SendDataChannelMessage(const void* data,
  178. uint64_t size) {
  179. if (!channel_ob_server)
  180. return false;
  181. auto* data_channel = channel_ob_server->data_channel();
  182. if (data_channel->buffered_amount() + size > 0x1000000uLL) {
  183. return false;
  184. }
  185. rtc::CopyOnWriteBuffer bufferStorage((const char*)data, (size_t)size);
  186. webrtc::DataBuffer buffer(bufferStorage, false); // always binary
  187. return data_channel->Send(buffer);
  188. }
  189. bool PeerConnection::AddIceCandidate(const char* sdp_mid,
  190. const int sdp_mline_index,
  191. const char* candidate) {
  192. if (!peer_)
  193. return false;
  194. webrtc::SdpParseError error;
  195. std::unique_ptr<webrtc::IceCandidateInterface> ice_candidate(
  196. webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, candidate, &error));
  197. if (!ice_candidate)
  198. return false;
  199. if (!peer_->AddIceCandidate(ice_candidate.get()))
  200. return false;
  201. return true;
  202. }
  203. bool PeerConnection::CreateOffer() {
  204. if (!peer_)
  205. return false;
  206. webrtc::PeerConnectionInterface::RTCOfferAnswerOptions options;
  207. /*if (mandatory_receive_)*/ { //< TODO - This is legacy, should use
  208. // transceivers
  209. #ifdef WIN32
  210. options.offer_to_receive_audio = true;
  211. options.offer_to_receive_video = true;
  212. #else
  213. options.offer_to_receive_audio = true;
  214. options.offer_to_receive_video = true;
  215. #endif
  216. }
  217. // if (data_channel_from_id_.empty()) {
  218. // sctp_negotiated_ = false;
  219. // }
  220. peer_->CreateOffer(this, options);
  221. return true;
  222. }
  223. bool PeerConnection::CreateAnswer() {
  224. if (!peer_)
  225. return false;
  226. webrtc::PeerConnectionInterface::RTCOfferAnswerOptions options;
  227. /*if (mandatory_receive_)*/ { //< TODO - This is legacy, should use
  228. // transceivers
  229. options.offer_to_receive_audio = true;
  230. options.offer_to_receive_video = true;
  231. }
  232. peer_->CreateAnswer(this, options);
  233. return true;
  234. }
  235. bool PeerConnection::SetRemoteDescription(const char* type,
  236. const char* sdp) {
  237. if (!peer_)
  238. return false;
  239. // if (data_channel_from_id_.empty()) {
  240. // sctp_negotiated_ = false;
  241. // }
  242. std::string sdp_type_str(type);
  243. auto sdp_type = webrtc::SdpTypeFromString(sdp_type_str);
  244. if (!sdp_type.has_value())
  245. return false;
  246. std::string remote_desc(sdp);
  247. webrtc::SdpParseError error;
  248. std::unique_ptr<webrtc::SessionDescriptionInterface> session_description(
  249. webrtc::CreateSessionDescription(sdp_type.value(), remote_desc, &error));
  250. if (!session_description)
  251. return false;
  252. rtc::scoped_refptr<webrtc::SetRemoteDescriptionObserverInterface> observer =
  253. new rtc::RefCountedObject<SetRemoteSessionDescObserver>();
  254. peer_->SetRemoteDescription(std::move(session_description),
  255. std::move(observer));
  256. return true;
  257. }
  258. void PeerConnection::OnSignalingChange(
  259. webrtc::PeerConnectionInterface::SignalingState new_state) {
  260. // See https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum
  261. switch (new_state) {
  262. case webrtc::PeerConnectionInterface::kStable:
  263. // Transitioning *to* stable means final answer received.
  264. // Otherwise the only possible way to be in the stable state is at start,
  265. // but this callback would not be invoked then because there's no
  266. // transition.
  267. {
  268. std::lock_guard<std::mutex> lock{connected_callback_mutex_};
  269. connected_callback_();
  270. }
  271. break;
  272. case webrtc::PeerConnectionInterface::kHaveLocalOffer:
  273. break;
  274. case webrtc::PeerConnectionInterface::kHaveLocalPrAnswer:
  275. break;
  276. case webrtc::PeerConnectionInterface::kHaveRemoteOffer:
  277. break;
  278. case webrtc::PeerConnectionInterface::kHaveRemotePrAnswer:
  279. break;
  280. }
  281. }
  282. void PeerConnection::OnAddStream(
  283. rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
  284. remote_streams_.push_back(stream);
  285. if (auto* sink = remote_video_observer_.get()) {
  286. rtc::VideoSinkWants sink_settings{};
  287. sink_settings.rotation_applied =
  288. true; // no exposed API for caller to handle rotation
  289. for (auto&& video_track : stream->GetVideoTracks()) {
  290. video_track->AddOrUpdateSink(sink, sink_settings);
  291. }
  292. }
  293. if (auto* sink = remote_audio_observer_.get()) {
  294. for (auto&& audio_track : stream->GetAudioTracks()) {
  295. audio_track->AddSink(sink);
  296. }
  297. }
  298. }
  299. void PeerConnection::OnRemoveStream(
  300. rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
  301. auto it = std::find(remote_streams_.begin(), remote_streams_.end(), stream);
  302. if (it == remote_streams_.end())
  303. return;
  304. if (auto* sink = remote_video_observer_.get()) {
  305. for (auto&& video_track : stream->GetVideoTracks()) {
  306. video_track->RemoveSink(sink);
  307. }
  308. }
  309. if (auto* sink = remote_audio_observer_.get()) {
  310. for (auto&& audio_track : stream->GetAudioTracks()) {
  311. audio_track->RemoveSink(sink);
  312. }
  313. }
  314. remote_streams_.erase(it);
  315. }
  316. void PeerConnection::OnDataChannel(
  317. rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel)
  318. #if defined(WINUWP)
  319. (false)
  320. #else
  321. #endif
  322. {
  323. // If receiving a new data channel, then obviously SCTP has been negotiated so
  324. // it is safe to create other ones.
  325. sctp_negotiated_ = true;
  326. std::string label = data_channel->label();
  327. DataChannelObserver* observer{
  328. new DataChannelObserver(data_channel)};
  329. //< TODO - Need to register a message callback!!
  330. observer->SetMessageCallback(data_channel_message_callback_);
  331. observer->SetBufferingCallback(data_channel_buffering_callback_);
  332. observer->SetStateCallback(data_channel_state_callback_);
  333. data_channel->RegisterObserver(observer);
  334. channel_ob_server.reset(observer);
  335. } // namespace webrtc_impl
  336. void PeerConnection::OnRenegotiationNeeded() {
  337. std::lock_guard<std::mutex> lock{renegotiation_needed_callback_mutex_};
  338. auto cb = renegotiation_needed_callback_;
  339. if (cb) {
  340. cb();
  341. }
  342. }
  343. void PeerConnection::OnIceCandidate(
  344. const webrtc::IceCandidateInterface* candidate) {
  345. std::lock_guard<std::mutex> lock{ice_candidate_ready_to_send_callback_mutex_};
  346. auto cb = ice_candidate_ready_to_send_callback_;
  347. if (cb) {
  348. std::string sdp;
  349. if (!candidate->ToString(&sdp))
  350. return;
  351. ensureNullTerminatedCString(sdp);
  352. std::string sdp_mid = candidate->sdp_mid();
  353. ensureNullTerminatedCString(sdp_mid);
  354. cb(cb.peer,cb.index,sdp.c_str(), candidate->sdp_mline_index(), sdp_mid.c_str());
  355. }
  356. }
  357. void PeerConnection::OnAddTrack(
  358. rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
  359. const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
  360. /*streams*/) {
  361. std::lock_guard<std::mutex> lock{track_added_callback_mutex_};
  362. auto cb = track_added_callback_;
  363. if (cb) {
  364. cb();
  365. }
  366. }
  367. void PeerConnection::OnRemoveTrack(
  368. rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver) {
  369. std::lock_guard<std::mutex> lock{track_removed_callback_mutex_};
  370. auto cb = track_removed_callback_;
  371. if (cb) {
  372. cb();
  373. }
  374. }
  375. void PeerConnection::OnSuccess(
  376. webrtc::SessionDescriptionInterface* desc) {
  377. std::lock_guard<std::mutex> lock{local_sdp_ready_to_send_callback_mutex_};
  378. auto cb = local_sdp_ready_to_send_callback_;
  379. rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer;
  380. if (cb) {
  381. std::string type{SdpTypeToString(desc->GetType())};
  382. ensureNullTerminatedCString(type);
  383. std::string sdp;
  384. desc->ToString(&sdp);
  385. ensureNullTerminatedCString(sdp);
  386. observer = new rtc::RefCountedObject<SessionDescObserver>(
  387. [cb, type = std::move(type), sdp = std::move(sdp)] {
  388. cb(cb.peer,cb.index,type.c_str(), sdp.c_str());
  389. });
  390. }
  391. else {
  392. observer = new rtc::RefCountedObject<SessionDescObserver>();
  393. }
  394. // SetLocalDescription will invoke observer.OnSuccess() once done, which
  395. // will in turn invoke the |local_sdp_ready_to_send_callback_| registered if
  396. // any, or do nothing otherwise. The observer is a mandatory parameter.
  397. peer_->SetLocalDescription(observer, desc);
  398. }
  399. #ifdef WEBRTC_LINUX
  400. void PeerConnection::RegisterCaptureOp(std::unique_ptr<CaptureOp>& ptr)
  401. {
  402. _capture=std::move(ptr);
  403. }
  404. void PeerConnection::SwitchCapture(bool front)
  405. {
  406. _capture->SetForward(front);
  407. }
  408. void * PeerConnection::GetCurrentCtx()
  409. {
  410. return _capture->_ctx0;
  411. }
  412. void PeerConnection::SetOtherCtx(void * data)
  413. {
  414. _capture->_ctx1=(context_t *) data;
  415. }
  416. #endif