aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/SideChains/WebRtc.NET/src/conductor.h
blob: c002bc76021d0da7f89aad4d3bcf7edec51cda7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#ifndef WEBRTC_NET_CONDUCTOR_H_
#define WEBRTC_NET_CONDUCTOR_H_
#pragma once

#include "webrtc/api/mediastreaminterface.h"
#include "webrtc/api/peerconnectioninterface.h"

#include "internals.h"

namespace cricket
{
	class TurnServer;
	class StunServer;
}

namespace Native
{
	typedef void(__stdcall *OnErrorCallbackNative)();
	typedef void(__stdcall *OnSuccessCallbackNative)(const char * type, const char * sdp);
	typedef void(__stdcall *OnFailureCallbackNative)(const char * error);
	typedef void(__stdcall *OnIceStateChangedCallbackNative)(webrtc::PeerConnectionInterface::IceConnectionState state);
	typedef void(__stdcall *OnIceCandidateCallbackNative)(const char * sdp_mid, int sdp_mline_index, const char * sdp);
	typedef void(__stdcall *OnRenderCallbackNative)(uint8_t * frame_buffer, uint32_t w, uint32_t h);
	typedef void(__stdcall *OnDataMessageCallbackNative)(const char * msg);
	typedef void(__stdcall *OnDataBinaryMessageCallbackNative)(const uint8_t * msg, uint32_t size);

	class Conductor : public webrtc::PeerConnectionObserver,
		public webrtc::CreateSessionDescriptionObserver,
		public webrtc::SetSessionDescriptionObserver,
		public webrtc::DataChannelObserver
	{
	public:

		Conductor();
		~Conductor();

		bool InitializePeerConnection();
		void Dispose();
		void CreateOffer();
		void OnOfferReply(std::string type, std::string sdp);
		void OnOfferRequest(std::string sdp);
		bool AddIceCandidate(std::string sdp_mid, int sdp_mlineindex, std::string sdp);

		bool ProcessMessages(int delay)
		{
			return rtc::Thread::Current()->ProcessMessages(delay);
		}

		static std::vector<std::string> GetVideoDevices();
		bool OpenVideoCaptureDevice(std::string & name);
		void AddServerConfig(std::string uri, std::string username, std::string password);

		uint8_t * VideoCapturerI420Buffer()
		{
			if (capturer)
			{
				return (uint8_t*)capturer->video_buffer->DataY();
			}
			return nullptr;
		}

		void PushFrame()
		{
			if (capturer)
			{
				capturer->PushFrame();
			}
		}

#if DESKTOP_CAPTURE
		void DesktopCapturerSize(int & w, int & h)
		{
			if (capturer && capturer->desktop_frame)
			{
				webrtc::DesktopSize s = capturer->desktop_frame->size();
				w = s.width();
				h = s.height();
			}
		}

		uint8_t * DesktopCapturerRGBAbuffer()
		{
			if (capturer && capturer->desktop_frame)
			{
				return capturer->desktop_frame->data();
			}
			return nullptr;
		}

		void CaptureFrame()
		{
			if (capturer)
			{
				capturer->CaptureFrame();
			}
		}
#endif
		void CreateDataChannel(const std::string & label);
		void DataChannelSendText(const std::string & text);
		void DataChannelSendData(const webrtc::DataBuffer & data);

		OnErrorCallbackNative onError;
		OnSuccessCallbackNative onSuccess;
		OnIceStateChangedCallbackNative onIceStateChanged;
		OnFailureCallbackNative onFailure;
		OnIceCandidateCallbackNative onIceCandidate;
		OnRenderCallbackNative onRenderLocal;
		OnRenderCallbackNative onRenderRemote;
		OnDataMessageCallbackNative onDataMessage;
		OnDataBinaryMessageCallbackNative onDataBinaryMessage;

		bool RunStunServer(const std::string & bindIp);
		bool RunTurnServer(const std::string & bindIp, const std::string & ip,
						   const std::string & realm, const std::string & authFile);

	protected:

#pragma region -- SetSessionDescriptionObserver --

		virtual void webrtc::SetSessionDescriptionObserver::OnSuccess()
		{
			LOG(INFO) << __FUNCTION__;
		}

#pragma endregion
		
#pragma region -- CreateSessionDescriptionObserver --

		virtual void webrtc::CreateSessionDescriptionObserver::OnSuccess(webrtc::SessionDescriptionInterface* desc);
		virtual void OnFailure(const std::string& error);

#pragma endregion

#pragma region -- PeerConnectionObserver --

		virtual void OnAddStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream);
		virtual void OnRemoveStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream);
		virtual void OnError();
		virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate);

		virtual void OnIceChange()
		{
			LOG(INFO) << __FUNCTION__ << " ";
		}
		
		virtual void OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState state)
		{
			LOG(INFO) << __FUNCTION__ << " " << state;
		}

		virtual void OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState state)
		{
			LOG(INFO) << __FUNCTION__ << " " << state;

			if (onIceStateChanged != nullptr)
			{
				onIceStateChanged(state);
			}
		}

		virtual void OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState state)
		{
			LOG(INFO) << __FUNCTION__ << " " << state;
		}

		virtual void OnStateChange(webrtc::PeerConnectionObserver::StateType state_changed)
		{
			LOG(INFO) << __FUNCTION__ << " " << state_changed;
		}

		virtual void OnRenegotiationNeeded()
		{
			LOG(INFO) << __FUNCTION__ << " ";
		}

#pragma endregion
		
#pragma region -- DataChannelObserver --
		
		virtual void OnDataChannel(rtc::scoped_refptr<webrtc::DataChannelInterface> channel);

		// The data channel state have changed.
		virtual void OnStateChange()
		{
			LOG(INFO) << __FUNCTION__;
		}

		//  A data buffer was successfully received.
		virtual void OnMessage(const webrtc::DataBuffer& buffer);

		// The data channel's buffered_amount has changed.
		virtual void OnBufferedAmountChange(uint64_t previous_amount)
		{
			LOG(INFO) << __FUNCTION__;
		}

#pragma endregion

		int AddRef() const
		{
			return 0;
		};
		int Release() const
		{
			return 0;
		};

	private:

		bool _disposed;
		bool CreatePeerConnection(bool dtls);
		void DeletePeerConnection();
		void AddStreams();		

		rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
		rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
		std::map<std::string, rtc::scoped_refptr<webrtc::MediaStreamInterface>> active_streams_;
		rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel;
		std::vector<webrtc::PeerConnectionInterface::IceServer> serverConfigs;

		YuvFramesCapturer2 * capturer;
		cricket::VideoCapturer * capturer_internal;

		std::unique_ptr<VideoRenderer> local_video;
		std::unique_ptr<VideoRenderer> remote_video;
		std::unique_ptr<AudioRenderer> remote_audio;

		std::unique_ptr<cricket::TurnServer> turnServer;
		std::unique_ptr<cricket::StunServer> stunServer;

	public:
		int caputureFps;
		bool audioEnabled;
		bool barcodeEnabled;

		int width_;
		int height_;
	};
}
#endif  // WEBRTC_NET_CONDUCTOR_H_