peer_connection.h 10 KB

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