peer_connection.h 9.7 KB

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