-
Notifications
You must be signed in to change notification settings - Fork 1k
/
agoraobject.cpp
565 lines (484 loc) · 16.7 KB
/
agoraobject.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#include "agoraobject.h"
#include <qmessagebox.h>
#include <cassert>
#include <qprocess>
CAgoraConfig gAgoraConfig;
class AgoraRtcEngineEvent : public agora::rtc::IRtcEngineEventHandler
{
CAgoraObject& m_pInstance;
public:
AgoraRtcEngineEvent(CAgoraObject& engine)
:m_pInstance(engine)
{}
virtual void onVideoStopped() override
{
emit m_pInstance.sender_videoStopped();
}
virtual void onJoinChannelSuccess(const char* channel, uid_t uid, int elapsed) override
{
emit m_pInstance.sender_joinedChannelSuccess(QString(channel),uid,elapsed);
}
virtual void onUserJoined(uid_t uid, int elapsed) override
{
//qDebug("%s %u",__FUNCTION__,uid);
emit m_pInstance.sender_userJoined(uid, elapsed);
}
virtual void onUserOffline(uid_t uid, USER_OFFLINE_REASON_TYPE reason) override
{
emit m_pInstance.sender_userOffline(uid, reason);
}
virtual void onFirstLocalVideoFrame(int width, int height, int elapsed) override
{
emit m_pInstance.sender_firstLocalVideoFrame(width, height, elapsed);
}
virtual void onFirstRemoteVideoDecoded(uid_t uid, int width, int height, int elapsed) override
{
emit m_pInstance.sender_firstRemoteVideoDecoded(uid, width, height, elapsed);
}
virtual void onFirstRemoteVideoFrame(uid_t uid, int width, int height, int elapsed) override
{
emit m_pInstance.sender_firstRemoteVideoFrameDrawn(uid, width, height, elapsed);
}
virtual void onLocalVideoStats(const LocalVideoStats &stats) override
{
emit m_pInstance.sender_localVideoStats(stats);
}
virtual void onRemoteVideoStats(const RemoteVideoStats &stats) override
{
emit m_pInstance.sender_remoteVideoStats(stats);
}
virtual void onRtcStats(const RtcStats &stats) override
{
emit m_pInstance.sender_rtcStats(stats);
}
virtual void onLastmileQuality(int quality) override
{
emit m_pInstance.sender_lastmileQuality(quality);
}
virtual void onLastmileProbeResult(const LastmileProbeResult &result) override
{
emit m_pInstance.sender_lastmileProbeResult(result);
}
};
CAgoraObject* CAgoraObject::getInstance(QObject *parent)
{
std::lock_guard<std::mutex> autoLock(m_mutex);
if(nullptr == m_pInstance)
m_pInstance = new CAgoraObject(parent);
return m_pInstance;
}
CAgoraObject* CAgoraObject::m_pInstance = nullptr;
std::mutex CAgoraObject::m_mutex;
CAgoraObject::CAgoraObject(QObject *parent):
QObject(parent),
m_rtcEngine(createAgoraRtcEngine()),
m_eventHandler(new AgoraRtcEngineEvent(*this))
{
// Declare a RTC engine context
agora::rtc::RtcEngineContext context;
// Retrieve event handler
context.eventHandler = m_eventHandler.get();
QByteArray temp;
// Pass appId to the context
if(strlen(APPID))
context.appId = APPID;
else {
QString strAppId = gAgoraConfig.getAppId();
if(strAppId.length()== 0){
gAgoraConfig.setAppId(QString(""));
}
temp = strAppId.toUtf8();
context.appId = const_cast<const char*>(temp.data());
}
if (*context.appId == '\0')
{
QMessageBox::critical(nullptr, ("AgoraOpenLive"),
("You must specify APP ID before using the demo"));
QProcess process;
process.startDetached("notepad.exe", {"AgoraConfigOpenVideoCall.ini"}, "");
ExitProcess(0);
}
// initialize the RtcEngine with the context
m_rtcEngine->initialize(context);
}
CAgoraObject::~CAgoraObject()
{
// release the RTC engine inside the destructor
if(m_rtcEngine)
m_rtcEngine->release();
}
// Join Channel method joins the given channel.
int CAgoraObject::joinChannel(const QString& key, const QString& channel, uint uid)
{
if (channel.isEmpty()) {
QMessageBox::warning(nullptr,("AgoraHighSound"),("channelname is empty"));
return -1;
}
// Strarts local video preview
m_rtcEngine->startPreview();
// Parameters are: token channelId, optionalInfo, uid
// https://docs.agora.io/en/Video/API%20Reference/cpp/classagora_1_1rtc_1_1_i_rtc_engine.html#a3eb5ee494ce124b34609c593719c89ab
int r = m_rtcEngine->joinChannel(key.toUtf8().data(), channel.toUtf8().data(), nullptr, uid);
return r;
}
int CAgoraObject::leaveChannel()
{
// Stops the local video preview
m_rtcEngine->stopPreview();
// Leaves the channel
int r = m_rtcEngine->leaveChannel();
return r;
}
int CAgoraObject::muteLocalAudioStream(bool muted)
{
RtcEngineParameters rep(*m_rtcEngine);
// mutes or unmutes baed on the bpassed in muted boolean
return rep.muteLocalAudioStream(muted);
}
// Plays the local video
BOOL CAgoraObject::LocalVideoPreview(HWND hVideoWnd, BOOL bPreviewOn, RENDER_MODE_TYPE renderType/* = RENDER_MODE_TYPE::RENDER_MODE_FIT*/)
{
int nRet = 0;
if (bPreviewOn) {
VideoCanvas vc;
vc.uid = 0;
vc.view = hVideoWnd;
vc.renderMode = renderType;
m_rtcEngine->setupLocalVideo(vc);
nRet = m_rtcEngine->startPreview();
}
else
nRet = m_rtcEngine->stopPreview();
return nRet == 0 ? TRUE : FALSE;
}
// Plays the remote video
BOOL CAgoraObject::RemoteVideoRender(uid_t uid, HWND hVideoWnd, RENDER_MODE_TYPE renderType/* = RENDER_MODE_TYPE::RENDER_MODE_HIDDEN*/)
{
int nRet = 0;
VideoCanvas vc;
vc.uid = uid;
vc.view = hVideoWnd;
vc.renderMode = renderType;
m_rtcEngine->setupRemoteVideo(vc);
return nRet == 0 ? TRUE : FALSE;
}
// Measues the network statistics
BOOL CAgoraObject::startLastMileProbeTest(bool enable)
{
int nres = 0;
if(enable) {
LastmileProbeConfig lp;
lp.probeUplink =true;
lp.probeDownlink = true;
lp.expectedDownlinkBitrate = 5000;
lp.expectedUplinkBitrate = 5000;
nres = m_rtcEngine->startLastmileProbeTest(lp);
}
else {
nres = m_rtcEngine->stopLastmileProbeTest();
}
return nres == 0? TRUE : FALSE;
}
// Enables video functionalities
int CAgoraObject::enableVideo(bool enabled)
{
return enabled ? m_rtcEngine->enableVideo() : m_rtcEngine->disableVideo();
}
// Enables audio functionalities
int CAgoraObject::enableAudio(bool enabled)
{
return enabled ? m_rtcEngine->enableAudio() : m_rtcEngine->disableAudio();
}
// Sets the directory for storing the logs
BOOL CAgoraObject::setLogPath(const QString &strDir)
{
int ret = 0;
RtcEngineParameters rep(*m_rtcEngine);
ret = rep.setLogFile(strDir.toUtf8().data());
return ret == 0 ? TRUE : FALSE;
}
// Sets the profile for the channel based on the passed in string
// More info here https://docs.agora.io/en/Video/API%20Reference/cpp/classagora_1_1rtc_1_1_i_rtc_engine.html#aab53788c74da25080bad61f0525d12ae
BOOL CAgoraObject::SetChannelProfile(CHANNEL_PROFILE_TYPE channelType)
{
int ret = 0;
ret = m_rtcEngine->setChannelProfile(channelType);
return ret == 0 ? TRUE : FALSE;
}
// Set the client role Broadcaster or Audience
BOOL CAgoraObject::SetClientRole(CLIENT_ROLE_TYPE roleType)
{
int ret = 0;
ret = m_rtcEngine->setClientRole(roleType);
return ret == 0 ? TRUE : FALSE;
}
// This method enables the native application to send video to/ recieve video from Web SDK
BOOL CAgoraObject::EnableWebSdkInteroperability(BOOL bEnable)
{
RtcEngineParameters rep(*m_rtcEngine);
int nRet = rep.enableWebSdkInteroperability(static_cast<bool>(bEnable));
return nRet == 0 ? TRUE : FALSE;
}
// Generates the list of recording devices available on the computer
qSSMap CAgoraObject::getRecordingDeviceList()
{
qSSMap devices;
AAudioDeviceManager audioDeviceManager(m_rtcEngine);
if (!audioDeviceManager)
return devices;
agora::util::AutoPtr<IAudioDeviceCollection> spCollection(audioDeviceManager->enumerateRecordingDevices());
if (!spCollection)
return devices;
char name[MAX_DEVICE_ID_LENGTH], guid[MAX_DEVICE_ID_LENGTH];
int count = spCollection->getCount();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
if (!spCollection->getDevice(i, name, guid))
{
devices[name] = guid;
}
}
}
return devices;
}
// Generate the list of playout devices available in the computer (like speaker)
qSSMap CAgoraObject::getPlayoutDeviceList()
{
qSSMap devices;
AAudioDeviceManager audioDeviceManager(m_rtcEngine);
if (!audioDeviceManager)
return devices;
agora::util::AutoPtr<IAudioDeviceCollection> spCollection(audioDeviceManager->enumeratePlaybackDevices());
if (!spCollection)
return devices;
char name[MAX_DEVICE_ID_LENGTH], guid[MAX_DEVICE_ID_LENGTH];
int count = spCollection->getCount();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
if (!spCollection->getDevice(i, name, guid))
{
devices[name] = guid;
}
}
}
return devices;
}
// Get the list of all video devices available in the computer (like webcams)
qSSMap CAgoraObject::getVideoDeviceList()
{
m_rtcEngine->enableVideo();
qSSMap devices;
AVideoDeviceManager videoDeviceManager(m_rtcEngine);
if (!videoDeviceManager)
return devices;
agora::util::AutoPtr<IVideoDeviceCollection> spCollection(videoDeviceManager->enumerateVideoDevices());
if (!spCollection)
return devices;
char name[MAX_DEVICE_ID_LENGTH], guid[MAX_DEVICE_ID_LENGTH];
int count = spCollection->getCount();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
if (!spCollection->getDevice(i, name, guid))
{
devices[name] = guid;
}
}
}
return devices;
}
// This method sets the Recording device (mic) to the one passed in the parameter
int CAgoraObject::setRecordingDevice(const QString& guid)
{
if (guid.isEmpty())
return -1;
AAudioDeviceManager audioDeviceManager(m_rtcEngine);
if (!audioDeviceManager)
return -1;
return audioDeviceManager->setRecordingDevice(guid.toUtf8().data());
}
// This method sets the Playout device (speaker) to the one passed in the parameter
int CAgoraObject::setPlayoutDevice(const QString& guid)
{
if (guid.isEmpty())
return -1;
AAudioDeviceManager audioDeviceManager(m_rtcEngine);
if (!audioDeviceManager)
return -1;
return audioDeviceManager->setPlaybackDevice(guid.toUtf8().data());
}
// This method sets the Video device (webcam) to the one passed in the parameter
int CAgoraObject::setVideoDevice(const QString& guid)
{
if (guid.isEmpty())
return -1;
AVideoDeviceManager videoDeviceManager(m_rtcEngine);
if (!videoDeviceManager)
return -1;
return videoDeviceManager->setDevice(guid.toUtf8().data());
}
BOOL CAgoraObject::setVideoProfile(int nWidth,int nHeight, FRAME_RATE fps, int bitrate)
{
int res = 0;
VideoEncoderConfiguration vec;
if(gAgoraConfig.isCustomFPS())
fps = (FRAME_RATE)gAgoraConfig.getFPS();
if(gAgoraConfig.isCustomBitrate())
bitrate = gAgoraConfig.getBitrate();
if(gAgoraConfig.isCustomResolution())
gAgoraConfig.getVideoResolution(nWidth, nHeight);
vec = VideoEncoderConfiguration(nWidth,nHeight,FRAME_RATE_FPS_15,bitrate,ORIENTATION_MODE_FIXED_LANDSCAPE);
res = m_rtcEngine->setVideoEncoderConfiguration(vec);
return res ==0 ? TRUE : FALSE;
}
BOOL CAgoraObject::setRecordingIndex(int nIndex)
{
int res = 0;
AAudioDeviceManager audioDeviceManager(m_rtcEngine);
if (!audioDeviceManager)
return FALSE;
agora::util::AutoPtr<IAudioDeviceCollection> spCollection(audioDeviceManager->enumerateRecordingDevices());
if (!spCollection)
return FALSE;
char name[MAX_DEVICE_ID_LENGTH], guid[MAX_DEVICE_ID_LENGTH];
int count = spCollection->getCount();
assert(res < count);
spCollection->getDevice(nIndex,name,guid);
res = spCollection->setDevice(guid);
return res ==0 ? TRUE:FALSE;
}
BOOL CAgoraObject::setPlayoutIndex(int nIndex)
{
int res = 0;
AAudioDeviceManager audioDeviceManager(m_rtcEngine);
if (!audioDeviceManager)
return FALSE;
agora::util::AutoPtr<IAudioDeviceCollection> spCollection(audioDeviceManager->enumeratePlaybackDevices());
if (!spCollection)
return FALSE;
char name[MAX_DEVICE_ID_LENGTH], guid[MAX_DEVICE_ID_LENGTH];
int count = spCollection->getCount();
assert(res < count);
spCollection->getDevice(nIndex,name,guid);
res = spCollection->setDevice(guid);
return res ==0 ? TRUE:FALSE;
}
BOOL CAgoraObject::setVideoIndex(int nIndex)
{
int res = 0;
AVideoDeviceManager videoDeviceManager(m_rtcEngine);
if (!videoDeviceManager)
return FALSE;
agora::util::AutoPtr<IVideoDeviceCollection> spCollection(videoDeviceManager->enumerateVideoDevices());
if (!spCollection)
return FALSE;
char name[MAX_DEVICE_ID_LENGTH], guid[MAX_DEVICE_ID_LENGTH];
int count = spCollection->getCount();
assert(nIndex < count);
spCollection->getDevice(nIndex,name,guid);
res = spCollection->setDevice(guid);
return res == 0 ? TRUE : FALSE;
}
// Mutes the local video stream of the client
BOOL CAgoraObject::MuteLocalVideo(BOOL bMute)
{
int nRet = 0;
RtcEngineParameters rep(*m_rtcEngine);
nRet = rep.enableLocalVideo(!bMute);
return nRet == 0 ? TRUE : FALSE;
}
// Mutes the local audio Stream of the client
BOOL CAgoraObject::MuteLocalAudio(BOOL bMute)
{
int nRet = 0;
RtcEngineParameters rep(*m_rtcEngine);
nRet = m_rtcEngine->enableLocalAudio(!bMute);
return nRet == 0 ? TRUE : FALSE;
}
// Sets the encryption mode for the packets of video data aes-128-xts, aes-128-ecb or aes-256-xts
BOOL CAgoraObject::SetEncryptionMode(const char* secret,const char* secretMode)
{
int nRet = 0;
if(secret != nullptr && strlen(secret) > 0 && secretMode != nullptr) {
nRet = m_rtcEngine->setEncryptionSecret(secret);
nRet = m_rtcEngine->setEncryptionMode(secretMode);
}
return nRet == 0 ? TRUE : FALSE;
}
BOOL CAgoraObject::EnableEncryption(bool enabled, const EncryptionConfig & config)
{
int nRet = m_rtcEngine->enableEncryption(enabled, config);
return nRet == 0 ? TRUE : FALSE;
}
// Sets the type of log that are allowed [Debug, Info, warning, error, critical]
BOOL CAgoraObject::SetLogFilter(LOG_FILTER_TYPE logFilterType, LPCTSTR lpLogPath)
{
int nRet = 0;
RtcEngineParameters rep(*m_rtcEngine);
nRet = rep.setLogFilter(logFilterType);
if (lpLogPath != Q_NULLPTR) {
#ifdef UNICODE
CHAR szFilePath[MAX_PATH];
::WideCharToMultiByte(CP_ACP, 0, lpLogPath, -1, szFilePath, MAX_PATH, nullptr, nullptr);
nRet |= rep.setLogFile(szFilePath);
#else
nRet |= rep.setLogFile(lpLogPath);
#endif
}
return nRet == 0 ? TRUE : FALSE;
}
// sets the beauty options like contrast, brightness, smoothness, redness
bool CAgoraObject::setBeautyEffectOptions(bool enabled, BeautyOptions& options)
{
int nRet = 0;
nRet = m_rtcEngine->setBeautyEffectOptions(enabled, options);
return nRet == 0 ? true : false;
}
bool CAgoraObject::SetCustomVideoProfile()
{
FRAME_RATE customFPS = FRAME_RATE_FPS_15;
int customBitrate = STANDARD_BITRATE;
int nWidth = 640, nHeight = 360;
return setVideoProfile(nWidth, nHeight, customFPS, customBitrate);
}
void CAgoraObject::CAgoraObject::SetDefaultParameters()
{
std::map<std::string, std::string> mapStringParamsters;
std::map<std::string, bool> mapBoolParameters;
std::map<std::string, int> mapIntParameters;
std::map<std::string, std::string> mapObjectParamsters;
if(m_agoraJson.GetParameters(mapStringParamsters,
mapBoolParameters,
mapIntParameters,
mapObjectParamsters)){
AParameter apm(m_rtcEngine);
for (auto iter = mapBoolParameters.begin();
iter != mapBoolParameters.end(); ++iter) {
apm->setBool(iter->first.c_str(), iter->second);
}
for (auto iter = mapStringParamsters.begin();
iter != mapStringParamsters.end(); ++iter) {
apm->setString(iter->first.c_str(), iter->second.c_str());
}
for (auto iter = mapIntParameters.begin();
iter != mapIntParameters.end(); ++iter) {
apm->setInt(iter->first.c_str(), iter->second);
}
for (auto iter = mapObjectParamsters.begin();
iter != mapObjectParamsters.end(); ++iter) {
apm->setObject(iter->first.c_str(), iter->second.c_str());
}
}
}
QString CAgoraObject::GetAppToken()
{
QString strAppToken = APP_TOKEN;
if(!strAppToken.isEmpty())
return strAppToken;
return gAgoraConfig.getAppToken();
}