forked from AgoraIO/Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolem.cpp
332 lines (271 loc) · 9.77 KB
/
golem.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <sys/time.h>
#include <sys/timerfd.h>
#include <poll.h>
#include <signal.h>
#include <cstring>
#include <cmath>
#include <cinttypes>
#include <iostream>
#include "golem.h"
#include "base/safe_log.h"
#include "base/time_util.h"
namespace agora {
using namespace rtc;
using namespace base;
namespace pstn {
std::atomic<bool> golem::term_sig;
golem::golem(settings_t settings):settings_(settings)
{
dump_settings();
rtc_engine = NULL;
joined_ = false;
}
golem::~golem()
{
if (rtc_engine)
cleanup();
}
void golem::dump_settings()
{
std::cout << std::endl;
std::cout << " -- key : " << settings_.key << std::endl;
std::cout << " -- channel name : " << settings_.channel_name << std::endl;
std::cout << " -- uid : " << settings_.uid << std::endl;
std::cout << " -- mode : " << (settings_.mode == MODE_BROADCAST ? "broadcast" : "communicat") << std::endl;
std::cout << " -- role : " << (settings_.role == ROLE_BROADCASTER ? "broadcaster" : "audience") << std::endl;
std::cout << " -- dual stream : " << (settings_.dual_stream == 1 ? "true" : "false") << std::endl;
std::cout << " -- web interoperability : " << (settings_.web_interoperability == 1 ? "true" : "false") << std::endl;
std::cout << " -- video profile : " << settings_.video_profile << std::endl;
std::cout << " -- video profile ex : " << "width:" << settings_.video_profile_ex.width << ", height:" << settings_.video_profile_ex.height << ", fps:" << settings_.video_profile_ex.fps << ", kbps:" << settings_.video_profile_ex.kbps << std::endl;
std::cout << " -- audio codec : " << settings_.audio_codec << std::endl;
std::cout << " -- mute : " << "local video:"<< settings_.mute.local_video << ", local audio:" << settings_.mute.local_audio << ", remote video:" << settings_.mute.remote_video << ", remote audio:" << settings_.mute.remote_audio << std::endl;
std::cout << " -- video source : " << settings_.video_file << std::endl;
std::cout << " -- audio source : " << settings_.audio_file << std::endl;
for (auto iter = settings_.parameters.begin(); iter != settings_.parameters.end(); iter++) {
std::cout << " -- parameters : " << *iter << std::endl;
}
std::cout << std::endl;
}
void golem::cleanup()
{
LOG(INFO, "Leaving channel and ready to cleanup");
rtc_engine->leaveChannel();
rtc_engine->release();
rtc_engine = NULL;
fs_audio.close();
fs_video.close();
sleep(1);
}
int golem::run()
{
int ret = 0;
fs_audio.open(settings_.audio_file.c_str(), std::fstream::in|std::fstream::binary);
fs_video.open(settings_.video_file.c_str(), std::fstream::in|std::fstream::binary);
term_sig = false;
rtc_engine = dynamic_cast<rtc::IRtcEngine*>(createAgoraRtcEngine());
if (rtc_engine == NULL) {
SAFE_LOG(FATAL) << "Failed to create an Agora Rtc Engine!";
return -1;
}
RtcEngineContext context;
context.eventHandler = this;
context.appId = settings_.key.c_str();
ret = rtc_engine->initialize(context);
if(ret != 0) {
return -1;
}
agora::media::IMediaEngine* media_engine = nullptr;
rtc_engine->queryInterface(agora::AGORA_IID_MEDIA_ENGINE, (void**)&media_engine);
agora::rtc::IRtcEngineParameter* rtc_parameters = nullptr;
rtc_engine->queryInterface(agora::AGORA_IID_RTC_ENGINE_PARAMETER, (void**)&rtc_parameters);
// mode & role
if (settings_.mode == MODE_BROADCAST) {
rtc_engine->setChannelProfile(rtc::CHANNEL_PROFILE_LIVE_BROADCASTING);
if(settings_.role == ROLE_BROADCASTER) {
rtc_engine->setClientRole(rtc::CLIENT_ROLE_BROADCASTER);
} else {
rtc_engine->setClientRole(rtc::CLIENT_ROLE_AUDIENCE);
}
} else {
rtc_engine->setChannelProfile(rtc::CHANNEL_PROFILE_COMMUNICATION);
}
// video enable
rtc_engine->enableVideo();
// dual stream
if (settings_.dual_stream) {
rtc::RtcEngineParameters param(*rtc_engine);
param.enableDualStreamMode(true);
}
// video profile
if(settings_.video_profile >= 0) {
rtc_engine->setVideoProfile((VIDEO_PROFILE_TYPE)settings_.video_profile, false);
} else {
VideoEncoderConfiguration cfg(settings_.video_profile_ex.width,
settings_.video_profile_ex.height,
(agora::rtc::FRAME_RATE)settings_.video_profile_ex.fps,
settings_.video_profile_ex.kbps,
ORIENTATION_MODE_ADAPTIVE);
rtc_engine->setVideoEncoderConfiguration(cfg);
}
//set parameters
auto iter = settings_.parameters.begin();
for(; iter != settings_.parameters.end(); iter++) {
ret = rtc_parameters->setParameters(iter->c_str());
if(ret != 0) {
std::cout << "error:setParameters: " << iter->c_str() << std::endl;
return -1;
}
}
// web interoperability
if(settings_.web_interoperability) {
rtc::RtcEngineParameters param(*rtc_engine);
param.enableWebSdkInteroperability(true);
rtc_parameters->setParameters("{\"rtc.video.web_h264_interop_enable\":true,\"che.video.web_h264_interop_enable\":true}");
} else {
rtc::RtcEngineParameters param(*rtc_engine);
param.enableWebSdkInteroperability(false);
rtc_parameters->setParameters("{\"rtc.video.web_h264_interop_enable\":false,\"che.video.web_h264_interop_enable\":false}");
}
last_active_ts_ = now_ts();
max_users_ = 0;
// setup signal handler
signal(SIGPIPE, term_handler);
signal(SIGINT, term_handler);
signal(SIGTERM, term_handler);
media_engine->registerAudioFrameObserver(this);
media_engine->registerVideoFrameObserver(this);
int fs = 48000;
agora::rtc::AParameter msp(*rtc_engine);
// audio
if(msp) {
msp->setBool("che.audio.external_device", true);
msp->setInt("che.audio.audioSampleRate", fs);
msp->setInt("che.video.local.camera_index", 1024);
if(!settings_.audio_codec.empty()) {
msp->setString("che.audio.specify.codec", settings_.audio_codec.c_str());
}
} else {
std::cout << "msp is nullptr" << std::endl;
}
rtc::RtcEngineParameters parameter(*rtc_engine);
parameter.setRecordingAudioFrameParameters(fs, 1, RAW_AUDIO_FRAME_OP_MODE_WRITE_ONLY, fs/100);
parameter.setPlaybackAudioFrameParameters(fs, 1, RAW_AUDIO_FRAME_OP_MODE_READ_ONLY, fs/100);
// mute
parameter.muteLocalAudioStream(settings_.mute.local_audio);
parameter.muteLocalVideoStream(settings_.mute.local_video);
parameter.muteAllRemoteAudioStreams(settings_.mute.remote_audio);
parameter.muteAllRemoteVideoStreams(settings_.mute.remote_video);
// Join channel
if (rtc_engine->joinChannel(settings_.key.c_str(), settings_.channel_name.c_str(), NULL, settings_.uid) < 0) {
SAFE_LOG(ERROR) << "Failed to create the channel " << settings_.channel_name;
return -2;
}
return run_interactive();
}
int golem::run_interactive()
{
while (!term_sig) {
sleep(1);
}
cleanup();
return 0;
}
void golem::term_handler(int sig_no)
{
(void)sig_no;
term_sig = true;
}
void golem::onError(int rescode, const char *msg)
{
}
void golem::onJoinChannelSuccess(const char *channel, uid_t uid, int ts)
{
SAFE_LOG(INFO) << uid << " logined successfully in " << channel
<< ", elapsed: " << ts << " ms";
joined_ = true;
LOG(INFO, "User %u join channel %s success", uid, channel);
std::cout << "User " << uid << " join channel " << channel << " success" << std::endl << std::endl;
}
void golem::onRejoinChannelSuccess(const char *channel, uid_t uid, int elapsed)
{
SAFE_LOG(INFO) << uid << " rejoin to channel: " << channel << ", time offset "
<< elapsed << " ms";
std::cout << uid << " rejoin to channel: " << channel << ", time offset "
<< elapsed << " ms" << std::endl;
}
void golem::onWarning(int warn, const char *msg)
{
SAFE_LOG(WARN) << "code: " << warn << ", " << msg;
}
void golem::onUserJoined(uid_t uid, int elapsed)
{
SAFE_LOG(INFO) << "offset " << elapsed << " ms: " << uid
<< " joined the channel";
std::cout << "offset " << elapsed << " ms: " << uid
<< " joined the channel" << std::endl;
}
void golem::onUserOffline(uid_t uid, USER_OFFLINE_REASON_TYPE reason)
{
const char *detail = reason == USER_OFFLINE_QUIT ? "quit" : "dropped";
SAFE_LOG(INFO) << "User " << uid << " " << detail;
std::cout << "User " << uid << " " << detail << std::endl;
}
void golem::onRtcStats(const rtc::RtcStats &stats)
{
}
void golem::onLogEvent(int level, const char *msg, int length)
{
(void)length;
LOG(INFO, "level %d: %s", level, msg);
std::cout << "level " << level << ": " << msg << std::endl;
}
bool golem::onRecordAudioFrame(AudioFrame& audioFrame)
{
size_t bytes = audioFrame.samples * audioFrame.bytesPerSample * audioFrame.channels;
if (fs_audio.is_open()) {
fs_audio.read((char *)audioFrame.buffer, bytes);
if (fs_audio.eof()) {
fs_audio.clear();
fs_audio.seekg(0);
}
}
return true;
}
bool golem::onMixedAudioFrame(AudioFrame& audioFrame)
{
return true;
}
bool golem::onPlaybackAudioFrame(AudioFrame& audioFrame)
{
return true;
}
bool golem::onPlaybackAudioFrameBeforeMixing(unsigned int uid, AudioFrame& audioFrame)
{
return true;
}
bool golem::onCaptureVideoFrame(VideoFrame& videoFrame)
{
int h = videoFrame.height;
int yStride = videoFrame.yStride;
int uStride = videoFrame.uStride;
int vStride = videoFrame.vStride;
char *yBuf = (char *)videoFrame.yBuffer;
char *uBuf = (char *)videoFrame.uBuffer;
char *vBuf = (char *)videoFrame.vBuffer;
if (fs_video.is_open()) {
fs_video.read(yBuf, yStride*h);
fs_video.read(uBuf, uStride*h/2);
fs_video.read(vBuf, vStride*h/2);
if (fs_video.eof()) {
fs_video.clear();
fs_video.seekg(0);
}
}
return true;
}
bool golem::onRenderVideoFrame(unsigned int uid, VideoFrame& videoFrame)
{
return true;
}
}
}