peer_connection.h 10 KB

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