simple_peer_connection.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef EXAMPLES_UNITYPLUGIN_SIMPLE_PEER_CONNECTION_H_
  11. #define EXAMPLES_UNITYPLUGIN_SIMPLE_PEER_CONNECTION_H_
  12. #include <map>
  13. #include <memory>
  14. #include <string>
  15. #include <vector>
  16. #include "api/data_channel_interface.h"
  17. #include "api/media_stream_interface.h"
  18. #include "api/peer_connection_interface.h"
  19. #include "examples/unityplugin/unity_plugin_apis.h"
  20. #include "examples/unityplugin/video_observer.h"
  21. class SimplePeerConnection : public webrtc::PeerConnectionObserver,
  22. public webrtc::CreateSessionDescriptionObserver,
  23. public webrtc::DataChannelObserver,
  24. public webrtc::AudioTrackSinkInterface {
  25. public:
  26. SimplePeerConnection() {}
  27. ~SimplePeerConnection() {}
  28. bool InitializePeerConnection(const char** turn_urls,
  29. const int no_of_urls,
  30. const char* username,
  31. const char* credential,
  32. bool is_receiver);
  33. void DeletePeerConnection();
  34. void AddStreams(bool audio_only);
  35. bool CreateDataChannel();
  36. bool CreateOffer();
  37. bool CreateAnswer();
  38. bool SendDataViaDataChannel(const std::string& data);
  39. void SetAudioControl(bool is_mute, bool is_record);
  40. // Register callback functions.
  41. void RegisterOnLocalI420FrameReady(I420FRAMEREADY_CALLBACK callback);
  42. void RegisterOnRemoteI420FrameReady(I420FRAMEREADY_CALLBACK callback);
  43. void RegisterOnLocalDataChannelReady(LOCALDATACHANNELREADY_CALLBACK callback);
  44. void RegisterOnDataFromDataChannelReady(
  45. DATAFROMEDATECHANNELREADY_CALLBACK callback);
  46. void RegisterOnFailure(FAILURE_CALLBACK callback);
  47. void RegisterOnAudioBusReady(AUDIOBUSREADY_CALLBACK callback);
  48. void RegisterOnLocalSdpReadytoSend(LOCALSDPREADYTOSEND_CALLBACK callback);
  49. void RegisterOnIceCandiateReadytoSend(
  50. ICECANDIDATEREADYTOSEND_CALLBACK callback);
  51. bool SetRemoteDescription(const char* type, const char* sdp);
  52. bool AddIceCandidate(const char* sdp,
  53. const int sdp_mlineindex,
  54. const char* sdp_mid);
  55. protected:
  56. // create a peerconneciton and add the turn servers info to the configuration.
  57. bool CreatePeerConnection(const char** turn_urls,
  58. const int no_of_urls,
  59. const char* username,
  60. const char* credential);
  61. void CloseDataChannel();
  62. void SetAudioControl();
  63. // PeerConnectionObserver implementation.
  64. void OnSignalingChange(
  65. webrtc::PeerConnectionInterface::SignalingState new_state) override {}
  66. void OnAddStream(
  67. rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override;
  68. void OnRemoveStream(
  69. rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override {}
  70. void OnDataChannel(
  71. rtc::scoped_refptr<webrtc::DataChannelInterface> channel) override;
  72. void OnRenegotiationNeeded() override {}
  73. void OnIceConnectionChange(
  74. webrtc::PeerConnectionInterface::IceConnectionState new_state) override {}
  75. void OnIceGatheringChange(
  76. webrtc::PeerConnectionInterface::IceGatheringState new_state) override {}
  77. void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override;
  78. void OnIceConnectionReceivingChange(bool receiving) override {}
  79. // CreateSessionDescriptionObserver implementation.
  80. void OnSuccess(webrtc::SessionDescriptionInterface* desc) override;
  81. void OnFailure(webrtc::RTCError error) override;
  82. // DataChannelObserver implementation.
  83. void OnStateChange() override;
  84. void OnMessage(const webrtc::DataBuffer& buffer) override;
  85. // AudioTrackSinkInterface implementation.
  86. void OnData(const void* audio_data,
  87. int bits_per_sample,
  88. int sample_rate,
  89. size_t number_of_channels,
  90. size_t number_of_frames) override;
  91. // Get remote audio tracks ssrcs.
  92. std::vector<uint32_t> GetRemoteAudioTrackSsrcs();
  93. private:
  94. rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
  95. rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel_;
  96. std::map<std::string, rtc::scoped_refptr<webrtc::MediaStreamInterface> >
  97. active_streams_;
  98. std::unique_ptr<VideoObserver> local_video_observer_;
  99. std::unique_ptr<VideoObserver> remote_video_observer_;
  100. webrtc::MediaStreamInterface* remote_stream_ = nullptr;
  101. webrtc::PeerConnectionInterface::RTCConfiguration config_;
  102. LOCALDATACHANNELREADY_CALLBACK OnLocalDataChannelReady = nullptr;
  103. DATAFROMEDATECHANNELREADY_CALLBACK OnDataFromDataChannelReady = nullptr;
  104. FAILURE_CALLBACK OnFailureMessage = nullptr;
  105. AUDIOBUSREADY_CALLBACK OnAudioReady = nullptr;
  106. LOCALSDPREADYTOSEND_CALLBACK OnLocalSdpReady = nullptr;
  107. ICECANDIDATEREADYTOSEND_CALLBACK OnIceCandiateReady = nullptr;
  108. bool is_mute_audio_ = false;
  109. bool is_record_audio_ = false;
  110. bool mandatory_receive_ = false;
  111. // disallow copy-and-assign
  112. SimplePeerConnection(const SimplePeerConnection&) = delete;
  113. SimplePeerConnection& operator=(const SimplePeerConnection&) = delete;
  114. };
  115. #endif // EXAMPLES_UNITYPLUGIN_SIMPLE_PEER_CONNECTION_H_