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 11 #ifndef EXAMPLES_UNITYPLUGIN_SIMPLE_PEER_CONNECTION_H_ 12 #define EXAMPLES_UNITYPLUGIN_SIMPLE_PEER_CONNECTION_H_ 13 14 #include <map> 15 #include <memory> 16 #include <string> 17 #include <vector> 18 19 #include "api/data_channel_interface.h" 20 #include "api/media_stream_interface.h" 21 #include "api/peer_connection_interface.h" 22 #include "examples/unityplugin/unity_plugin_apis.h" 23 #include "examples/unityplugin/video_observer.h" 24 25 class SimplePeerConnection : public webrtc::PeerConnectionObserver, 26 public webrtc::CreateSessionDescriptionObserver, 27 public webrtc::DataChannelObserver, 28 public webrtc::AudioTrackSinkInterface { 29 public: SimplePeerConnection()30 SimplePeerConnection() {} ~SimplePeerConnection()31 ~SimplePeerConnection() {} 32 33 bool InitializePeerConnection(const char** turn_urls, 34 const int no_of_urls, 35 const char* username, 36 const char* credential, 37 bool is_receiver); 38 void DeletePeerConnection(); 39 void AddStreams(bool audio_only); 40 bool CreateDataChannel(); 41 bool CreateOffer(); 42 bool CreateAnswer(); 43 bool SendDataViaDataChannel(const std::string& data); 44 void SetAudioControl(bool is_mute, bool is_record); 45 46 // Register callback functions. 47 void RegisterOnLocalI420FrameReady(I420FRAMEREADY_CALLBACK callback); 48 void RegisterOnRemoteI420FrameReady(I420FRAMEREADY_CALLBACK callback); 49 void RegisterOnLocalDataChannelReady(LOCALDATACHANNELREADY_CALLBACK callback); 50 void RegisterOnDataFromDataChannelReady( 51 DATAFROMEDATECHANNELREADY_CALLBACK callback); 52 void RegisterOnFailure(FAILURE_CALLBACK callback); 53 void RegisterOnAudioBusReady(AUDIOBUSREADY_CALLBACK callback); 54 void RegisterOnLocalSdpReadytoSend(LOCALSDPREADYTOSEND_CALLBACK callback); 55 void RegisterOnIceCandiateReadytoSend( 56 ICECANDIDATEREADYTOSEND_CALLBACK callback); 57 bool SetRemoteDescription(const char* type, const char* sdp); 58 bool AddIceCandidate(const char* sdp, 59 const int sdp_mlineindex, 60 const char* sdp_mid); 61 62 protected: 63 // create a peerconneciton and add the turn servers info to the configuration. 64 bool CreatePeerConnection(const char** turn_urls, 65 const int no_of_urls, 66 const char* username, 67 const char* credential); 68 void CloseDataChannel(); 69 void SetAudioControl(); 70 71 // PeerConnectionObserver implementation. OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState new_state)72 void OnSignalingChange( 73 webrtc::PeerConnectionInterface::SignalingState new_state) override {} 74 void OnAddStream( 75 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override; OnRemoveStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream)76 void OnRemoveStream( 77 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override {} 78 void OnDataChannel( 79 rtc::scoped_refptr<webrtc::DataChannelInterface> channel) override; OnRenegotiationNeeded()80 void OnRenegotiationNeeded() override {} OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state)81 void OnIceConnectionChange( 82 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {} OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state)83 void OnIceGatheringChange( 84 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {} 85 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override; OnIceConnectionReceivingChange(bool receiving)86 void OnIceConnectionReceivingChange(bool receiving) override {} 87 88 // CreateSessionDescriptionObserver implementation. 89 void OnSuccess(webrtc::SessionDescriptionInterface* desc) override; 90 void OnFailure(webrtc::RTCError error) override; 91 92 // DataChannelObserver implementation. 93 void OnStateChange() override; 94 void OnMessage(const webrtc::DataBuffer& buffer) override; 95 96 // AudioTrackSinkInterface implementation. 97 void OnData(const void* audio_data, 98 int bits_per_sample, 99 int sample_rate, 100 size_t number_of_channels, 101 size_t number_of_frames) override; 102 103 // Get remote audio tracks ssrcs. 104 std::vector<uint32_t> GetRemoteAudioTrackSsrcs(); 105 106 private: 107 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_; 108 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel_; 109 std::map<std::string, rtc::scoped_refptr<webrtc::MediaStreamInterface> > 110 active_streams_; 111 112 std::unique_ptr<VideoObserver> local_video_observer_; 113 std::unique_ptr<VideoObserver> remote_video_observer_; 114 115 webrtc::MediaStreamInterface* remote_stream_ = nullptr; 116 webrtc::PeerConnectionInterface::RTCConfiguration config_; 117 118 LOCALDATACHANNELREADY_CALLBACK OnLocalDataChannelReady = nullptr; 119 DATAFROMEDATECHANNELREADY_CALLBACK OnDataFromDataChannelReady = nullptr; 120 FAILURE_CALLBACK OnFailureMessage = nullptr; 121 AUDIOBUSREADY_CALLBACK OnAudioReady = nullptr; 122 123 LOCALSDPREADYTOSEND_CALLBACK OnLocalSdpReady = nullptr; 124 ICECANDIDATEREADYTOSEND_CALLBACK OnIceCandiateReady = nullptr; 125 126 bool is_mute_audio_ = false; 127 bool is_record_audio_ = false; 128 bool mandatory_receive_ = false; 129 130 // disallow copy-and-assign 131 SimplePeerConnection(const SimplePeerConnection&) = delete; 132 SimplePeerConnection& operator=(const SimplePeerConnection&) = delete; 133 }; 134 135 #endif // EXAMPLES_UNITYPLUGIN_SIMPLE_PEER_CONNECTION_H_ 136