-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMediaSoupMailbox.cpp
215 lines (161 loc) · 6.89 KB
/
MediaSoupMailbox.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
#ifndef _DEBUG
#include "MediaSoupMailbox.h"
#include "common_audio/include/audio_util.h"
/**
* MediaSoupMailbox
*/
MediaSoupMailbox::~MediaSoupMailbox()
{
if (m_to_float_resampler != nullptr)
audio_resampler_destroy(m_to_float_resampler);
if (m_from_float_to_mediasoup_resampler != nullptr)
audio_resampler_destroy(m_from_float_to_mediasoup_resampler);
if (m_to_mediasoup_resampler != nullptr)
audio_resampler_destroy(m_to_mediasoup_resampler);
}
rtc::scoped_refptr<webrtc::I420Buffer> MediaSoupMailbox::getProducerFrameBuffer(const int width, const int height)
{
if (m_producerFrameBuffer == nullptr || m_producerFrameBuffer->width() != width || m_producerFrameBuffer->height() != height)
m_producerFrameBuffer = webrtc::I420Buffer::Create(width, height);
return m_producerFrameBuffer;
}
void MediaSoupMailbox::push_received_videoFrame(std::unique_ptr<webrtc::VideoFrame> ptr)
{
std::lock_guard<std::mutex> grd(m_mtx_received_video);
m_received_video_frame = std::move(ptr);
}
void MediaSoupMailbox::pop_receieved_videoFrames(std::unique_ptr<webrtc::VideoFrame> &output)
{
std::lock_guard<std::mutex> grd(m_mtx_received_video);
output = std::move(m_received_video_frame);
}
void MediaSoupMailbox::assignOutgoingAudioParams(const audio_format audioformat, const speaker_layout speakerLayout, const int bytesPerSample,
const int numChannels, const int samples_per_sec)
{
if (m_obs_bytesPerSample == bytesPerSample && m_obs_numChannels == numChannels && m_obs_samples_per_sec == samples_per_sec &&
m_obs_audioformat == audioformat && m_obs_speakerLayout == speakerLayout)
return;
m_outgoing_audio_data.clear();
m_outgoing_audio_data.resize(numChannels);
m_obs_numFrames = 0;
m_obs_bytesPerSample = bytesPerSample;
m_obs_numChannels = numChannels;
m_obs_samples_per_sec = samples_per_sec;
m_obs_audioformat = audioformat;
m_obs_speakerLayout = speakerLayout;
if (m_to_float_resampler != nullptr)
audio_resampler_destroy(m_to_float_resampler);
if (m_from_float_to_mediasoup_resampler != nullptr)
audio_resampler_destroy(m_from_float_to_mediasoup_resampler);
if (m_to_mediasoup_resampler != nullptr)
audio_resampler_destroy(m_to_mediasoup_resampler);
struct resample_info from;
struct resample_info to;
// Format -> Float
from.samples_per_sec = m_obs_samples_per_sec;
from.speakers = speakerLayout;
from.format = audioformat;
to.samples_per_sec = m_obs_samples_per_sec;
to.speakers = speakerLayout;
to.format = AUDIO_FORMAT_FLOAT;
m_to_float_resampler = audio_resampler_create(&to, &from);
// Float -> GetDefaultAudioFormat
from.samples_per_sec = m_obs_samples_per_sec;
from.speakers = speakerLayout;
from.format = AUDIO_FORMAT_FLOAT;
to.samples_per_sec = m_obs_samples_per_sec;
to.speakers = speakerLayout;
to.format = MediaSoupTransceiver::GetDefaultAudioFormat();
m_from_float_to_mediasoup_resampler = audio_resampler_create(&to, &from);
// Format -> GetDefaultAudioFormat
from.samples_per_sec = m_obs_samples_per_sec;
from.speakers = speakerLayout;
from.format = audioformat;
to.samples_per_sec = m_obs_samples_per_sec;
to.speakers = speakerLayout;
to.format = MediaSoupTransceiver::GetDefaultAudioFormat();
m_to_mediasoup_resampler = audio_resampler_create(&to, &from);
}
void MediaSoupMailbox::push_outgoing_audioFrame(const uint8_t **data, const int frames)
{
std::lock_guard<std::mutex> grd(m_mtx_outgoing_audio);
const int framesPer10ms = m_obs_samples_per_sec / 100;
// Overflow?
if (m_obs_numFrames > framesPer10ms * 256) {
m_obs_numFrames = 0;
for (auto &itr : m_outgoing_audio_data)
itr.clear();
}
m_obs_numFrames += frames;
int channelBufferSize = m_obs_bytesPerSample * frames;
for (int channel = 0; channel < m_obs_numChannels; ++channel)
m_outgoing_audio_data[channel].append((char *)data[channel], channelBufferSize);
}
void MediaSoupMailbox::pop_outgoing_audioFrames(std::vector<std::unique_ptr<SoupSendAudioFrame>> &output)
{
std::lock_guard<std::mutex> grd(m_mtx_outgoing_audio);
if (m_obs_bytesPerSample == 0 || m_obs_numChannels == 0 || m_obs_samples_per_sec == 0 || m_obs_numFrames == 0)
return;
const int framesPer10ms = m_obs_samples_per_sec / 100;
while (m_obs_numFrames > framesPer10ms) {
m_obs_numFrames -= framesPer10ms;
std::unique_ptr<SoupSendAudioFrame> ptr = std::make_unique<SoupSendAudioFrame>();
ptr->numFrames = framesPer10ms;
ptr->numChannels = m_obs_numChannels;
ptr->samples_per_sec = m_obs_samples_per_sec;
ptr->bytesPerSample = sizeof(int16_t);
// Pluck from the audio buffer and also convert to desired format
uint8_t *array2d_float_planar_raw[MAX_AV_PLANES];
uint8_t *array2d_int16_raw[MAX_AV_PLANES];
uint8_t *array2d_float_raw[MAX_AV_PLANES];
for (int channel = 0; channel < m_obs_numChannels; ++channel) {
const int bytesFromFloatBuffer = m_obs_bytesPerSample * framesPer10ms;
array2d_float_planar_raw[channel] = new uint8_t[bytesFromFloatBuffer];
auto &channelBuffer_Float = m_outgoing_audio_data[channel];
memcpy(array2d_float_planar_raw[channel], channelBuffer_Float.data(), bytesFromFloatBuffer);
channelBuffer_Float.erase(channelBuffer_Float.begin(), channelBuffer_Float.begin() + bytesFromFloatBuffer);
}
uint32_t numFrames = 0;
uint64_t tOffset = 0;
// Avoid redundant work
if (m_volume == 1.f) {
// Format -> Mediasoup
if (audio_resampler_resample(m_to_mediasoup_resampler, array2d_int16_raw, &numFrames, &tOffset, array2d_float_planar_raw,
framesPer10ms)) {
ptr->audio_data.resize(framesPer10ms * m_obs_numChannels);
webrtc::Interleave((int16_t **)array2d_int16_raw, ptr->numFrames, ptr->numChannels, ptr->audio_data.data());
}
}
// Planar -> Float
else if (audio_resampler_resample(m_to_float_resampler, array2d_float_raw, &numFrames, &tOffset, array2d_float_planar_raw, framesPer10ms)) {
// Apply volume
float *cur = (float *)array2d_float_raw[0];
float *end = cur + numFrames * m_obs_numChannels;
while (cur < end)
*(cur++) *= m_volume;
// Float -> Mediasoup
if (audio_resampler_resample(m_from_float_to_mediasoup_resampler, array2d_int16_raw, &numFrames, &tOffset, array2d_float_raw,
framesPer10ms)) {
ptr->audio_data.resize(framesPer10ms * m_obs_numChannels);
webrtc::Interleave((int16_t **)array2d_int16_raw, ptr->numFrames, ptr->numChannels, ptr->audio_data.data());
}
}
for (int channel = 0; channel < m_obs_numChannels; ++channel)
delete[] array2d_float_planar_raw[channel];
output.push_back(std::move(ptr));
}
}
void MediaSoupMailbox::push_outgoing_videoFrame(rtc::scoped_refptr<webrtc::I420Buffer> ptr)
{
std::lock_guard<std::mutex> grd(m_mtx_outgoing_video);
// Overflow?
if (m_outgoing_video_data.size() > 30)
m_outgoing_video_data.clear();
m_outgoing_video_data.push_back(ptr);
}
void MediaSoupMailbox::pop_outgoing_videoFrames(std::vector<rtc::scoped_refptr<webrtc::I420Buffer>> &output)
{
std::lock_guard<std::mutex> grd(m_mtx_outgoing_video);
m_outgoing_video_data.swap(output);
}
#endif