/
RtcCall.cpp
152 lines (133 loc) · 4.04 KB
/
RtcCall.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
/*
* Copyright (c) 2020 The anyRTC project authors. All Rights Reserved.
*
* Website: https://www.anyrtc.io for more details.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "RtcCall.h"
#include <assert.h>
static const size_t kMaxDataSizeSamples = 3840;
RtcCall::RtcCall()
: rtc_chan_(NULL)
, b_join_chan_(false)
, aud_rtc_2_sip_buffer_(NULL)
, aud_sip_sample_hz_(16000)
, aud_sip_channels_(1)
, aud_sip_cur_len_(0)
, aud_sip_req_len_(0)
, aud_enc_(NULL)
, aud_rtc_sample_hz_(48000)
, aud_rtc_channels_(2)
{
aud_rtc_2_sip_buffer_ = new char[kMaxDataSizeSamples];
rtc_chan_ = createARtSEngine();
aud_enc_ = createRtcAudEncoder(*this);
aud_sip_req_len_ = (aud_sip_sample_hz_ * aud_sip_channels_ * sizeof(int16_t)) / 50;
}
RtcCall::~RtcCall(void)
{
assert(!b_join_chan_);
if (aud_rtc_2_sip_buffer_ != NULL) {
delete[] aud_rtc_2_sip_buffer_;
aud_rtc_2_sip_buffer_ = NULL;
}
if (aud_enc_ != NULL) {
aud_enc_->DeInit();
delete aud_enc_;
aud_enc_ = NULL;
}
if (rtc_chan_ != NULL)
{
delete rtc_chan_;
rtc_chan_ = NULL;
}
}
void RtcCall::JoinRtc(const std::string&strAppId, const std::string&strChanId, const std::string&strUId)
{
if (!b_join_chan_) {
b_join_chan_ = true;
rtc_chan_->init(strAppId.c_str(), strUId.c_str(), this, NULL);
char strParams[256];
sprintf(strParams, "{\"Cmd\":\"SetStreamTypes\", \"StreamTypes\": %d}", 0/*0:audio 1:video 2:audio&video*/);
rtc_chan_->set_parameters(strParams);
rtc_chan_->set_parameters("{\"Cmd\":\"SetHost\", \"Host\": true}");
rtc_chan_->mute_local_video(true); // ²»°lËÍÒ•îl”µ“þ
rtc_chan_->join_channel(strChanId.c_str(), NULL, 0);
Timer10Ms::Inst().RegisterTimer(this, this);
}
}
void RtcCall::LeaveRtc()
{
if (b_join_chan_) {
b_join_chan_ = false;
Timer10Ms::Inst().UnRegisterTimer(this);
rtc_chan_->leave_channel();
}
}
//* For
void RtcCall::on_user_joined(const char *channel, const char * uid, int elapsed)
{
XAutoLock l(cs_aud_dec_);
if (map_aud_dec_.find(uid) == map_aud_dec_.end()) {
NeteqDecoder* audDec = createNeteqDecoder(uid, *this);
map_aud_dec_[uid] = audDec;
}
}
void RtcCall::on_user_offline(const char *channel, const char * uid, int reason)
{
XAutoLock l(cs_aud_dec_);
if (map_aud_dec_.find(uid) != map_aud_dec_.end()) {
NeteqDecoder* audDec = map_aud_dec_[uid];
map_aud_dec_.erase(uid);
delete audDec;
audDec = NULL;
}
}
void RtcCall::on_audio_data(const char *channel, const char * uid, uint32_t sent_ts, uint16_t seqn, uint8_t codec, const void *data, size_t len)
{
XAutoLock l(cs_aud_dec_);
if (map_aud_dec_.find(uid) != map_aud_dec_.end()) {
NeteqDecoder* audDec = map_aud_dec_[uid];
audDec->SetAudEncData((char*)data, len, seqn, sent_ts);
}
}
//* For Timer10MsEvent
void RtcCall::OnTimer10Ms(int64_t curTime64)
{
bool bMix = false;
if (aud_sip_cur_len_ == 0) {
memset(aud_rtc_2_sip_buffer_, 0, kMaxDataSizeSamples);
}
{
XAutoLock l(cs_aud_dec_);
MapNeteqDecoder::iterator itnr = map_aud_dec_.begin();
while (itnr != map_aud_dec_.end()) {
NeteqDecoder* audDec = itnr->second;
if (audDec->MixAudPcmData(bMix, aud_rtc_2_sip_buffer_ + aud_sip_cur_len_, aud_sip_sample_hz_, aud_sip_channels_) > 0) {
bMix = true;
}
itnr++;
}
}
aud_sip_cur_len_ += aud_sip_sample_hz_ * sizeof(int16_t) * aud_sip_channels_ / 100;
if (aud_sip_cur_len_ >= aud_sip_req_len_) {
OnRtcCallAudioMixData(aud_rtc_2_sip_buffer_, aud_sip_cur_len_, aud_sip_sample_hz_, aud_sip_channels_, XGetTimestamp());
aud_sip_cur_len_ = 0;
}
}
//* For RtcAudEncoderEvent
void RtcCall::OnAudioEncoderData(const char*pData, int nLen)
{
if (rtc_chan_ != NULL) {
rtc_chan_->send_audio_data(0, pData, nLen);
}
}
//* For AudNeqDecoderEvent
void RtcCall::OnAudNeqDecoderData(const char*strIdd, void* audioSamples, uint32_t samplesPerSec, size_t nChannels)
{//* Do nothing
}