blob: b99bde930ef1200af55417e2c748c3a5290224c5 [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
Steve Anton10542f22019-01-11 09:11:00 -080019#include "api/data_channel_interface.h"
20#include "api/media_stream_interface.h"
21#include "api/peer_connection_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#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();
gyzhouad7cad82017-05-11 16:10:03 -070069 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 Alvestrand73771a82018-05-24 10:53:49 +020090 void OnFailure(webrtc::RTCError error) override;
gyzhouad7cad82017-05-11 16:10:03 -070091
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
gyzhoub38f3862017-07-25 16:04:31 -0700112 std::unique_ptr<VideoObserver> local_video_observer_;
113 std::unique_ptr<VideoObserver> remote_video_observer_;
gyzhouad7cad82017-05-11 16:10:03 -0700114
gyzhoub38f3862017-07-25 16:04:31 -0700115 webrtc::MediaStreamInterface* remote_stream_ = nullptr;
116 webrtc::PeerConnectionInterface::RTCConfiguration config_;
117
gyzhouad7cad82017-05-11 16:10:03 -0700118 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 Chen51e20462017-12-05 11:11:21 -0800128 bool mandatory_receive_ = false;
gyzhouad7cad82017-05-11 16:10:03 -0700129
130 // disallow copy-and-assign
131 SimplePeerConnection(const SimplePeerConnection&) = delete;
132 SimplePeerConnection& operator=(const SimplePeerConnection&) = delete;
133};
134
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200135#endif // EXAMPLES_UNITYPLUGIN_SIMPLE_PEER_CONNECTION_H_