peer_connection.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #pragma once
  2. class CaptureOp;
  3. class PeerConnection : public webrtc::PeerConnectionObserver,
  4. public webrtc::CreateSessionDescriptionObserver {
  5. public:
  6. PeerConnection();
  7. ~PeerConnection() override;
  8. void SetPeerImpl(
  9. rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer) ;
  10. using ConnectedCallback = Callback<>;
  11. void RegisterConnectedCallback(ConnectedCallback&& callback) {
  12. std::lock_guard<std::mutex> lock{connected_callback_mutex_};
  13. connected_callback_ = std::move(callback);
  14. }
  15. using LocalSdpReadytoSendCallback = Callback<int32_t,int32_t,const char*, const char*>;
  16. void RegisterLocalSdpReadytoSendCallback(
  17. LocalSdpReadytoSendCallback&& callback) {
  18. std::lock_guard<std::mutex> lock{local_sdp_ready_to_send_callback_mutex_};
  19. local_sdp_ready_to_send_callback_ = std::move(callback);
  20. }
  21. using IceCandidateReadytoSendCallback =
  22. Callback<int32_t,int32_t,const char*, int, const char*>;
  23. void RegisterIceCandidateReadytoSendCallback(
  24. IceCandidateReadytoSendCallback&& callback) {
  25. std::lock_guard<std::mutex> lock{ice_candidate_ready_to_send_callback_mutex_};
  26. ice_candidate_ready_to_send_callback_ = std::move(callback);
  27. }
  28. using RenegotiationNeededCallback = Callback<>;
  29. void RegisterRenegotiationNeededCallback(
  30. RenegotiationNeededCallback&& callback) {
  31. std::lock_guard<std::mutex> lock{renegotiation_needed_callback_mutex_};
  32. renegotiation_needed_callback_ = std::move(callback);
  33. }
  34. using TrackAddedCallback = Callback<>;
  35. void RegisterTrackAddedCallback(TrackAddedCallback&& callback) {
  36. std::lock_guard<std::mutex> lock{track_added_callback_mutex_};
  37. track_added_callback_ = std::move(callback);
  38. }
  39. using TrackRemovedCallback = Callback<>;
  40. void RegisterTrackRemovedCallback(TrackRemovedCallback&& callback) {
  41. std::lock_guard<std::mutex> lock{track_removed_callback_mutex_};
  42. track_removed_callback_ = std::move(callback);
  43. }
  44. void RegisterLocalVideoFrameCallback(
  45. I420FrameReadyCallback callback) {
  46. if (local_video_observer_) {
  47. local_video_observer_->SetCallback(std::move(callback));
  48. }
  49. }
  50. void RegisterLocalVideoFrameCallback(
  51. ARGBFrameReadyCallback callback) {
  52. if (local_video_observer_) {
  53. local_video_observer_->SetCallback(std::move(callback));
  54. }
  55. }
  56. void RegisterRemoteVideoFrameCallback(
  57. I420FrameReadyCallback callback) {
  58. if (remote_video_observer_) {
  59. remote_video_observer_->SetCallback(std::move(callback));
  60. }
  61. }
  62. void RegisterRemoteVideoFrameCallback(
  63. ARGBFrameReadyCallback callback)
  64. {
  65. //检查 remote_video_observer_ 是否存在
  66. if (remote_video_observer_)
  67. {
  68. remote_video_observer_->SetCallback(std::move(callback));
  69. }
  70. }
  71. void RegisterDataChannelCallback(
  72. DataChannelMessageCallback message_callback,
  73. DataChannelBufferingCallback buffering_callback,
  74. DataChannelStateCallback state_callback);
  75. bool AddLocalVideoTrack(
  76. rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track,const std::string& stream) ;
  77. void RemoveLocalVideoTrack() ;
  78. bool AddLocalAudioTrack(
  79. rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track) ;
  80. void RemoveLocalAudioTrack() ;
  81. mrsResult AddDataChannel(
  82. const char* label,
  83. bool ordered,
  84. bool reliable//,
  85. // DataChannelMessageCallback message_callback,
  86. // DataChannelBufferingCallback buffering_callback,
  87. // DataChannelStateCallback state_callba
  88. ) ;
  89. bool RemoveDataChannel() ;
  90. /* bool RemoveDataChannel(const char* label) ;*/
  91. /// Remove all the existing data tracks from the peer connection.
  92. /* void RemoveAllDataTracks() ;*/
  93. bool SendDataChannelMessage(const void* data, uint64_t size) ;
  94. bool AddIceCandidate(const char* sdp_mid,
  95. const int sdp_mline_index,
  96. const char* candidate) ;
  97. bool CreateOffer() ;
  98. bool CreateAnswer() ;
  99. bool SetRemoteDescription(const char* type, const char* sdp) ;
  100. #ifdef WEBRTC_LINUX
  101. void RegisterCaptureOp(std::unique_ptr<CaptureOp>& op);
  102. void SwitchCapture(bool front);
  103. void SetOtherCtx(void * data);
  104. void* GetCurrentCtx();
  105. #endif // WEBRTC_LINUX
  106. protected:
  107. // PeerConnectionObserver interface
  108. // Triggered when the SignalingState changed.
  109. void OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState
  110. new_state) override;
  111. // Triggered when media is received on a new stream from remote peer.
  112. void OnAddStream(rtc::scoped_refptr<webrtc::MediaStreamInterface>
  113. stream) override;
  114. // Triggered when a remote peer closes a stream.
  115. void OnRemoveStream(rtc::scoped_refptr<webrtc::MediaStreamInterface>
  116. stream) override;
  117. // Triggered when a remote peer opens a data channel.
  118. void OnDataChannel(
  119. rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) override;
  120. // Triggered when renegotiation is needed. For example, an ICE restart
  121. // has begun.
  122. void OnRenegotiationNeeded() override;
  123. // Called any time the IceConnectionState changes.
  124. //
  125. // Note that our ICE states lag behind the standard slightly. The most
  126. // notable differences include the fact that "failed" occurs after 15
  127. // seconds, not 30, and this actually represents a combination ICE + DTLS
  128. // state, so it may be "failed" if DTLS fails while ICE succeeds.
  129. void OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState
  130. /*new_state*/) override {}
  131. // Called any time the IceGatheringState changes.
  132. void OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState
  133. /*new_state*/) override {}
  134. // A new ICE candidate has been gathered.
  135. void OnIceCandidate(
  136. const webrtc::IceCandidateInterface* candidate) override;
  137. // Callback on track added.
  138. void OnAddTrack(
  139. rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
  140. const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
  141. streams) override;
  142. // Callback on track removed.
  143. void OnRemoveTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface>
  144. receiver) override;
  145. protected:
  146. // CreateSessionDescriptionObserver interface
  147. // This callback transfers the ownership of the |desc|.
  148. // TODO(deadbeef): Make this take an std::unique_ptr<> to avoid confusion
  149. // around ownership.
  150. void OnSuccess(webrtc::SessionDescriptionInterface* desc) override;
  151. // The OnFailure callback takes an RTCError, which consists of an
  152. // error code and a string.
  153. // RTCError is non-copyable, so it must be passed using std::move.
  154. // Earlier versions of the API used a string argument. This version
  155. // is deprecated; in order to let clients remove the old version, it has a
  156. // default implementation. If both versions are unimplemented, the
  157. // result will be a runtime error (stack overflow). This is intentional.
  158. void OnFailure(webrtc::RTCError error) override {}
  159. protected:
  160. rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_;
  161. ConnectedCallback connected_callback_;
  162. LocalSdpReadytoSendCallback local_sdp_ready_to_send_callback_;
  163. IceCandidateReadytoSendCallback ice_candidate_ready_to_send_callback_;
  164. RenegotiationNeededCallback renegotiation_needed_callback_;
  165. TrackAddedCallback track_added_callback_;
  166. TrackRemovedCallback track_removed_callback_;
  167. DataChannelMessageCallback data_channel_message_callback_;
  168. DataChannelBufferingCallback data_channel_buffering_callback_;
  169. DataChannelStateCallback data_channel_state_callback_;
  170. std::mutex connected_callback_mutex_;
  171. std::mutex local_sdp_ready_to_send_callback_mutex_;
  172. std::mutex ice_candidate_ready_to_send_callback_mutex_;
  173. std::mutex renegotiation_needed_callback_mutex_;
  174. std::mutex track_added_callback_mutex_;
  175. std::mutex track_removed_callback_mutex_;
  176. rtc::scoped_refptr<webrtc::VideoTrackInterface> local_video_track_;
  177. rtc::scoped_refptr<webrtc::AudioTrackInterface> local_audio_track_;
  178. rtc::scoped_refptr<webrtc::RtpSenderInterface> local_video_sender_;
  179. rtc::scoped_refptr<webrtc::RtpSenderInterface> local_audio_sender_;
  180. std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>> remote_streams_;
  181. #ifdef WEBRTC_LINUX
  182. std::unique_ptr<CaptureOp> _capture;
  183. #endif
  184. /// Collection of data channels from their unique ID.
  185. /// This contains only data channels pre-negotiated or opened by the remote
  186. /// peer, as data channels opened locally won't have immediately a unique ID.
  187. // std::unordered_map<int, std::shared_ptr<DataChannelObserver>>
  188. // data_channel_from_id_;
  189. //
  190. // /// Collection of data channels from their label.
  191. // /// This contains only data channels with a non-empty label.
  192. // std::unordered_multimap<std::string, std::shared_ptr<DataChannelObserver>>
  193. // data_channel_from_label_;
  194. std::unique_ptr<DataChannelObserver> channel_ob_server;
  195. //< TODO - Clarify lifetime of those, for now same as this PeerConnection
  196. std::unique_ptr<AudioFrameObserver> local_audio_observer_;
  197. std::unique_ptr<AudioFrameObserver> remote_audio_observer_;
  198. std::unique_ptr<VideoFrameObserver> local_video_observer_;
  199. std::unique_ptr<VideoFrameObserver> remote_video_observer_; //声明了一个名为 remote_video_observer_ 的std::unique_ptr<VideoFrameObserver>类型的智能指针
  200. /// Flag to indicate if SCTP was negotiated during the initial SDP handshake
  201. /// (m=application), which allows subsequently to use data channels. If this
  202. /// is false then data channels will never connnect. This is set to true if a
  203. /// data channel is created before the connection is established, which will
  204. /// force the connection to negotiate the necessary SCTP information. See
  205. /// https://stackoverflow.com/questions/43788872/how-are-data-channels-negotiated-between-two-peers-with-webrtc
  206. bool sctp_negotiated_ = true;
  207. private:
  208. PeerConnection(const PeerConnection&) = delete;
  209. PeerConnection& operator=(const PeerConnection&) = delete;
  210. };