blob: 0c490253ee86cd6c4656f988ff8bb5bc0a57bb37 [file] [log] [blame]
gyzhouad7cad82017-05-11 16:10:03 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef EXAMPLES_UNITYPLUGIN_SIMPLE_PEER_CONNECTION_H_
12#define EXAMPLES_UNITYPLUGIN_SIMPLE_PEER_CONNECTION_H_
gyzhouad7cad82017-05-11 16:10:03 -070013
14#include <map>
15#include <memory>
16#include <string>
17#include <vector>
18
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "api/datachannelinterface.h"
20#include "api/mediastreaminterface.h"
21#include "api/peerconnectioninterface.h"
22#include "examples/unityplugin/unity_plugin_apis.h"
23#include "examples/unityplugin/video_observer.h"
gyzhouad7cad82017-05-11 16:10:03 -070024
25class SimplePeerConnection : public webrtc::PeerConnectionObserver,
26 public webrtc::CreateSessionDescriptionObserver,
27 public webrtc::DataChannelObserver,
28 public webrtc::AudioTrackSinkInterface {
29 public:
30 SimplePeerConnection() {}
31 ~SimplePeerConnection() {}
32
gyzhoub38f3862017-07-25 16:04:31 -070033 bool InitializePeerConnection(const char** turn_urls,
34 const int no_of_urls,
35 const char* username,
36 const char* credential,
37 bool is_receiver);
gyzhouad7cad82017-05-11 16:10:03 -070038 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.
gyzhoub38f3862017-07-25 16:04:31 -070047 void RegisterOnLocalI420FrameReady(I420FRAMEREADY_CALLBACK callback);
48 void RegisterOnRemoteI420FrameReady(I420FRAMEREADY_CALLBACK callback);
gyzhouad7cad82017-05-11 16:10:03 -070049 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);
gyzhoub38f3862017-07-25 16:04:31 -070057 bool SetRemoteDescription(const char* type, const char* sdp);
58 bool AddIceCandidate(const char* sdp,
59 const int sdp_mlineindex,
60 const char* sdp_mid);
gyzhouad7cad82017-05-11 16:10:03 -070061
62 protected:
gyzhoub38f3862017-07-25 16:04:31 -070063 // 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 Chen51e20462017-12-05 11:11:21 -080067 const char* credential);
gyzhouad7cad82017-05-11 16:10:03 -070068 void CloseDataChannel();
69 std::unique_ptr<cricket::VideoCapturer> OpenVideoCaptureDevice();
70 void SetAudioControl();
71
72 // PeerConnectionObserver implementation.
73 void OnSignalingChange(
74 webrtc::PeerConnectionInterface::SignalingState new_state) override {}
75 void OnAddStream(
76 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override;
77 void OnRemoveStream(
78 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override {}
79 void OnDataChannel(
80 rtc::scoped_refptr<webrtc::DataChannelInterface> channel) override;
81 void OnRenegotiationNeeded() override {}
82 void OnIceConnectionChange(
83 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {}
84 void OnIceGatheringChange(
85 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {}
86 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override;
87 void OnIceConnectionReceivingChange(bool receiving) override {}
88
89 // CreateSessionDescriptionObserver implementation.
90 void OnSuccess(webrtc::SessionDescriptionInterface* desc) override;
91 void OnFailure(const std::string& error) override;
92
93 // DataChannelObserver implementation.
94 void OnStateChange() override;
95 void OnMessage(const webrtc::DataBuffer& buffer) override;
96
97 // AudioTrackSinkInterface implementation.
98 void OnData(const void* audio_data,
99 int bits_per_sample,
100 int sample_rate,
101 size_t number_of_channels,
102 size_t number_of_frames) override;
103
104 // Get remote audio tracks ssrcs.
105 std::vector<uint32_t> GetRemoteAudioTrackSsrcs();
106
107 private:
108 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
109 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel_;
110 std::map<std::string, rtc::scoped_refptr<webrtc::MediaStreamInterface> >
111 active_streams_;
112
gyzhoub38f3862017-07-25 16:04:31 -0700113 std::unique_ptr<VideoObserver> local_video_observer_;
114 std::unique_ptr<VideoObserver> remote_video_observer_;
gyzhouad7cad82017-05-11 16:10:03 -0700115
gyzhoub38f3862017-07-25 16:04:31 -0700116 webrtc::MediaStreamInterface* remote_stream_ = nullptr;
117 webrtc::PeerConnectionInterface::RTCConfiguration config_;
118
gyzhouad7cad82017-05-11 16:10:03 -0700119 LOCALDATACHANNELREADY_CALLBACK OnLocalDataChannelReady = nullptr;
120 DATAFROMEDATECHANNELREADY_CALLBACK OnDataFromDataChannelReady = nullptr;
121 FAILURE_CALLBACK OnFailureMessage = nullptr;
122 AUDIOBUSREADY_CALLBACK OnAudioReady = nullptr;
123
124 LOCALSDPREADYTOSEND_CALLBACK OnLocalSdpReady = nullptr;
125 ICECANDIDATEREADYTOSEND_CALLBACK OnIceCandiateReady = nullptr;
126
127 bool is_mute_audio_ = false;
128 bool is_record_audio_ = false;
Qiang Chen51e20462017-12-05 11:11:21 -0800129 bool mandatory_receive_ = false;
gyzhouad7cad82017-05-11 16:10:03 -0700130
131 // disallow copy-and-assign
132 SimplePeerConnection(const SimplePeerConnection&) = delete;
133 SimplePeerConnection& operator=(const SimplePeerConnection&) = delete;
134};
135
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200136#endif // EXAMPLES_UNITYPLUGIN_SIMPLE_PEER_CONNECTION_H_