-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
270 lines (240 loc) · 8.77 KB
/
main.cpp
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <OpenteraWebrtcNativeClient/StreamClient.h>
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <cmath>
#include <iostream>
#include <thread>
#include <vector>
#include <cstdlib>
using namespace opentera;
using namespace std;
class CvVideoCaptureVideoSource : public VideoSource
{
atomic_bool m_stopped;
thread m_thread;
string m_path;
public:
CvVideoCaptureVideoSource(string path)
: VideoSource(VideoSourceConfiguration::create(false, true)),
m_stopped(false),
m_thread(&CvVideoCaptureVideoSource::run, this),
m_path(move(path))
{
}
~CvVideoCaptureVideoSource() override
{
m_stopped.store(true);
m_thread.join();
}
private:
void run()
{
cv::VideoCapture cap;
cv::Mat bgrImg;
while (!m_stopped.load())
{
cap.open(m_path);
if (!cap.isOpened())
{
cerr << "Invalid video file" << endl;
exit(EXIT_FAILURE);
}
auto frameDuration = chrono::microseconds(static_cast<int>(1e6 / cap.get(cv::CAP_PROP_FPS)));
auto frameTime = chrono::steady_clock::now();
while (!m_stopped.load())
{
cap.read(bgrImg);
if (bgrImg.empty())
{
break;
}
int64_t timestampUs =
chrono::duration_cast<chrono::microseconds>(chrono::steady_clock::now().time_since_epoch()).count();
sendFrame(bgrImg, timestampUs);
frameTime += frameDuration;
this_thread::sleep_until(frameTime);
}
}
}
};
constexpr uint32_t SoundCardTotalDelayMs = 0;
constexpr int BitsPerSample = 16;
constexpr int SampleRate = 48000;
constexpr size_t NumberOfChannels = 1;
constexpr chrono::milliseconds SinAudioSourceFrameDuration = 10ms;
constexpr chrono::milliseconds SinAudioSourceSleepBuffer = 2ms;
constexpr int16_t SinAudioSourceAmplitude = 15000;
class SinAudioSource : public AudioSource
{
atomic_bool m_stopped;
thread m_thread;
public:
SinAudioSource()
: AudioSource(
AudioSourceConfiguration::create(SoundCardTotalDelayMs),
BitsPerSample,
SampleRate,
NumberOfChannels),
m_stopped(false),
m_thread(&SinAudioSource::run, this)
{
}
~SinAudioSource() override
{
m_stopped.store(true);
m_thread.join();
}
private:
void run()
{
vector<int16_t> data(SinAudioSourceFrameDuration.count() * SampleRate / 1000, 0);
double t = 0;
for (size_t i = 0; i < data.size(); i++)
{
data[i] = static_cast<int16_t>(SinAudioSourceAmplitude * sin(t));
t += 2 * M_PI / static_cast<double>(data.size());
}
while (!m_stopped.load())
{
sendFrame(data.data(), data.size() * sizeof(int16_t) / bytesPerFrame());
auto start = chrono::steady_clock::now();
this_thread::sleep_for(SinAudioSourceFrameDuration - SinAudioSourceSleepBuffer);
while ((chrono::steady_clock::now() - start) < SinAudioSourceFrameDuration);
}
}
};
int main(int argc, char* argv[])
{
if (argc != 2)
{
cout << "Usage: CppStreamClient video_path" << endl;
return EXIT_FAILURE;
}
vector<IceServer> iceServers;
if (!IceServer::fetchFromServer("http://localhost:8080/iceservers", "abc", iceServers))
{
cout << "IceServer::fetchFromServer failed" << endl;
iceServers.clear();
}
auto signalingServerConfiguration =
SignalingServerConfiguration::create("ws://localhost:8080/signaling", "C++", "chat", "abc");
auto webrtcConfiguration = WebrtcConfiguration::create(iceServers);
auto videoStreamConfiguration = VideoStreamConfiguration::create();
auto videoSource = make_shared<CvVideoCaptureVideoSource>(argv[1]);
auto audioSource = make_shared<SinAudioSource>();
StreamClient
client(signalingServerConfiguration, webrtcConfiguration, videoStreamConfiguration, videoSource, audioSource);
client.setOnSignalingConnectionOpened(
[]()
{
// This callback is called from the internal client thread.
cout << "OnSignalingConnectionOpened" << endl;
});
client.setOnSignalingConnectionClosed(
[]()
{
// This callback is called from the internal client thread.
cout << "OnSignalingConnectionClosed" << endl;
});
client.setOnSignalingConnectionError(
[](const string& error)
{
// This callback is called from the internal client thread.
cout << "OnSignalingConnectionError:" << endl << "\t" << error;
});
client.setOnRoomClientsChanged(
[](const vector<RoomClient>& roomClients)
{
// This callback is called from the internal client thread.
cout << "OnRoomClientsChanged:" << endl;
for (const auto& c : roomClients)
{
cout << "\tid=" << c.id() << ", name=" << c.name() << ", isConnected=" << c.isConnected() << endl;
}
});
client.setOnClientConnected(
[](const Client& client)
{
// This callback is called from the internal client thread.
cout << "OnClientConnected:" << endl;
cout << "\tid=" << client.id() << ", name=" << client.name() << endl;
cv::namedWindow(client.id(), cv::WINDOW_AUTOSIZE);
});
client.setOnClientDisconnected(
[](const Client& client)
{
// This callback is called from the internal client thread.
cout << "OnClientDisconnected:" << endl;
cout << "\tid=" << client.id() << ", name=" << client.name() << endl;
cv::destroyWindow(client.id());
});
client.setOnClientConnectionFailed(
[](const Client& client)
{
// This callback is called from the internal client thread.
cout << "OnClientConnectionFailed:" << endl;
cout << "\tid=" << client.id() << ", name=" << client.name() << endl;
});
client.setOnError(
[](const string& error)
{
// This callback is called from the internal client thread.
cout << "error:" << endl;
cout << "\t" << error << endl;
});
client.setLogger(
[](const string& message)
{
// This callback is called from the internal client thread.
cout << "log:" << endl;
cout << "\t" << message << endl;
});
client.setOnAddRemoteStream(
[](const Client& client)
{
// This callback is called from the internal client thread.
cout << "OnAddRemoteStream:" << endl;
cout << "\tid=" << client.id() << ", name=" << client.name() << endl;
});
client.setOnRemoveRemoteStream(
[](const Client& client)
{
// This callback is called from the internal client thread.
cout << "OnRemoveRemoteStream:" << endl;
cout << "\tid=" << client.id() << ", name=" << client.name() << endl;
});
client.setOnVideoFrameReceived(
[](const Client& client, const cv::Mat& bgrImg, uint64_t timestampUs)
{
// This callback is called from a WebRTC processing thread.
cout << "OnVideoFrameReceived:" << endl;
cv::imshow(client.id(), bgrImg);
cv::waitKey(1);
});
client.setOnAudioFrameReceived(
[](const Client& client,
const void* audioData,
int bitsPerSample,
int sampleRate,
size_t numberOfChannels,
size_t numberOfFrames)
{
// This callback is called from a WebRTC processing thread.
cout << "OnAudioFrameReceived:" << endl;
cout << "\tid=" << client.id() << ", name=" << client.name() << endl;
cout << "\tbitsPerSample=" << bitsPerSample << ", sampleRate = " << sampleRate;
cout << ", numberOfChannels = " << numberOfChannels << ", numberOfFrames=" << numberOfFrames << endl;
});
client.setOnMixedAudioFrameReceived(
[](const void* audioData, int bitsPerSample, int sampleRate, size_t numberOfChannels, size_t numberOfFrames)
{
// This callback is called from the audio device module thread.
cout << "OnMixedAudioFrameReceived:" << endl;
cout << "\tbitsPerSample=" << bitsPerSample << ", sampleRate=" << sampleRate;
cout << ", numberOfChannels=" << numberOfChannels << ", numberOfFrames=" << numberOfFrames << endl;
});
client.connect();
cin.get();
return 0;
}