peer_connection.cpp 14 KB

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