gyzhou | ad7cad8 | 2017-05-11 16:10:03 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef EXAMPLES_UNITYPLUGIN_SIMPLE_PEER_CONNECTION_H_ |
| 12 | #define EXAMPLES_UNITYPLUGIN_SIMPLE_PEER_CONNECTION_H_ |
gyzhou | ad7cad8 | 2017-05-11 16:10:03 -0700 | [diff] [blame] | 13 | |
| 14 | #include <map> |
| 15 | #include <memory> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 19 | #include "api/data_channel_interface.h" |
| 20 | #include "api/media_stream_interface.h" |
| 21 | #include "api/peer_connection_interface.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "examples/unityplugin/unity_plugin_apis.h" |
| 23 | #include "examples/unityplugin/video_observer.h" |
gyzhou | ad7cad8 | 2017-05-11 16:10:03 -0700 | [diff] [blame] | 24 | |
| 25 | class SimplePeerConnection : public webrtc::PeerConnectionObserver, |
| 26 | public webrtc::CreateSessionDescriptionObserver, |
| 27 | public webrtc::DataChannelObserver, |
| 28 | public webrtc::AudioTrackSinkInterface { |
| 29 | public: |
| 30 | SimplePeerConnection() {} |
| 31 | ~SimplePeerConnection() {} |
| 32 | |
gyzhou | b38f386 | 2017-07-25 16:04:31 -0700 | [diff] [blame] | 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); |
gyzhou | ad7cad8 | 2017-05-11 16:10:03 -0700 | [diff] [blame] | 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. |
gyzhou | b38f386 | 2017-07-25 16:04:31 -0700 | [diff] [blame] | 47 | void RegisterOnLocalI420FrameReady(I420FRAMEREADY_CALLBACK callback); |
| 48 | void RegisterOnRemoteI420FrameReady(I420FRAMEREADY_CALLBACK callback); |
gyzhou | ad7cad8 | 2017-05-11 16:10:03 -0700 | [diff] [blame] | 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); |
gyzhou | b38f386 | 2017-07-25 16:04:31 -0700 | [diff] [blame] | 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); |
gyzhou | ad7cad8 | 2017-05-11 16:10:03 -0700 | [diff] [blame] | 61 | |
| 62 | protected: |
gyzhou | b38f386 | 2017-07-25 16:04:31 -0700 | [diff] [blame] | 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, |
Qiang Chen | 51e2046 | 2017-12-05 11:11:21 -0800 | [diff] [blame] | 67 | const char* credential); |
gyzhou | ad7cad8 | 2017-05-11 16:10:03 -0700 | [diff] [blame] | 68 | void CloseDataChannel(); |
gyzhou | ad7cad8 | 2017-05-11 16:10:03 -0700 | [diff] [blame] | 69 | void SetAudioControl(); |
| 70 | |
| 71 | // PeerConnectionObserver implementation. |
| 72 | void OnSignalingChange( |
| 73 | webrtc::PeerConnectionInterface::SignalingState new_state) override {} |
| 74 | void OnAddStream( |
| 75 | rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override; |
| 76 | void OnRemoveStream( |
| 77 | rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override {} |
| 78 | void OnDataChannel( |
| 79 | rtc::scoped_refptr<webrtc::DataChannelInterface> channel) override; |
| 80 | void OnRenegotiationNeeded() override {} |
| 81 | void OnIceConnectionChange( |
| 82 | webrtc::PeerConnectionInterface::IceConnectionState new_state) override {} |
| 83 | void OnIceGatheringChange( |
| 84 | webrtc::PeerConnectionInterface::IceGatheringState new_state) override {} |
| 85 | void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override; |
| 86 | void OnIceConnectionReceivingChange(bool receiving) override {} |
| 87 | |
| 88 | // CreateSessionDescriptionObserver implementation. |
| 89 | void OnSuccess(webrtc::SessionDescriptionInterface* desc) override; |
Harald Alvestrand | 73771a8 | 2018-05-24 10:53:49 +0200 | [diff] [blame] | 90 | void OnFailure(webrtc::RTCError error) override; |
gyzhou | ad7cad8 | 2017-05-11 16:10:03 -0700 | [diff] [blame] | 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 | |
gyzhou | b38f386 | 2017-07-25 16:04:31 -0700 | [diff] [blame] | 112 | std::unique_ptr<VideoObserver> local_video_observer_; |
| 113 | std::unique_ptr<VideoObserver> remote_video_observer_; |
gyzhou | ad7cad8 | 2017-05-11 16:10:03 -0700 | [diff] [blame] | 114 | |
gyzhou | b38f386 | 2017-07-25 16:04:31 -0700 | [diff] [blame] | 115 | webrtc::MediaStreamInterface* remote_stream_ = nullptr; |
| 116 | webrtc::PeerConnectionInterface::RTCConfiguration config_; |
| 117 | |
gyzhou | ad7cad8 | 2017-05-11 16:10:03 -0700 | [diff] [blame] | 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; |
Qiang Chen | 51e2046 | 2017-12-05 11:11:21 -0800 | [diff] [blame] | 128 | bool mandatory_receive_ = false; |
gyzhou | ad7cad8 | 2017-05-11 16:10:03 -0700 | [diff] [blame] | 129 | |
| 130 | // disallow copy-and-assign |
| 131 | SimplePeerConnection(const SimplePeerConnection&) = delete; |
| 132 | SimplePeerConnection& operator=(const SimplePeerConnection&) = delete; |
| 133 | }; |
| 134 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 135 | #endif // EXAMPLES_UNITYPLUGIN_SIMPLE_PEER_CONNECTION_H_ |