-
Notifications
You must be signed in to change notification settings - Fork 165
/
mp_proxyplugin_consumer_audio.cc
executable file
·177 lines (150 loc) · 4.51 KB
/
mp_proxyplugin_consumer_audio.cc
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
/* Copyright (C) 2012-2015 Doubango Telecom <http://www.doubango.org>
*
* This file is part of Open Source 'webrtc2sip' project
* <http://code.google.com/p/webrtc2sip/>
*
* 'webrtc2sip' is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 'webrtc2sip' is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 'webrtc2sip'.
*/
#include "mp_proxyplugin_consumer_audio.h"
#include "MediaSessionMgr.h"
namespace webrtc2sip {
//
// MPProxyPluginConsumerAudio
//
MPProxyPluginConsumerAudio::MPProxyPluginConsumerAudio(uint64_t nId, const ProxyAudioConsumer* pcConsumer)
: MPProxyPlugin(MPMediaType_Audio, nId, dynamic_cast<const ProxyPlugin*>(pcConsumer)), m_pcWrappedConsumer(pcConsumer),
m_nPtime(MP_AUDIO_PTIME_DEFAULT),
m_nRate(MP_AUDIO_RATE_DEFAULT),
m_nChannels(MP_AUDIO_CHANNELS_DEFAULT)
{
m_nVolumeComputeCount = 0;
m_nVolume = 0.0;
m_nLatency = 0;
m_nLastPullTime = 0;
m_pHeldBufferPtr = NULL;
m_nHeldBufferSize = 0;
m_nHeldBufferPos = 0;
m_nLastRTPPacketTime = 0;
m_bByPassDecoding = MediaSessionMgr::defaultsGetByPassDecoding();
if((m_oCallback = new MPProxyPluginConsumerAudioCallback(this))){
const_cast<ProxyAudioConsumer*>(m_pcWrappedConsumer)->setCallback(*m_oCallback);
}
}
MPProxyPluginConsumerAudio::~MPProxyPluginConsumerAudio()
{
m_pcWrappedConsumer = NULL;
TSK_FREE(m_pHeldBufferPtr);
TSK_DEBUG_INFO("MPProxyPluginConsumerAudio object destroyed");
}
int MPProxyPluginConsumerAudio::prepareCallback(int ptime, int rate, int channels)
{
m_nPtime = ptime;
m_nRate = rate;
m_nChannels = channels;
return MPProxyPlugin::prepare();
}
int MPProxyPluginConsumerAudio::startCallback()
{
// call parent
return MPProxyPlugin::start();
}
int MPProxyPluginConsumerAudio::pauseCallback()
{
// call parent
return MPProxyPlugin::pause();
}
int MPProxyPluginConsumerAudio::stopCallback()
{
// call parent
return MPProxyPlugin::stop();
}
int MPProxyPluginConsumerAudio::consumeCallback(const void* buffer_ptr, tsk_size_t buffer_size, const tsk_object_t* proto_hdr)
{
if(m_oProducerOpposite && m_oProducerOpposite->isValid()){
return m_oProducerOpposite->push(buffer_ptr, buffer_size, m_nPtime, m_nRate, m_nChannels);
}
return 0;
}
unsigned MPProxyPluginConsumerAudio::pullAndHold()
{
#if 0
if(!isPrepared()){
return 0;
}
if(!m_pHeldBufferPtr){
m_nHeldBufferSize = (m_nRate * (MP_AUDIO_BITS_PER_SAMPLE_DEFAULT >> 3) * m_nChannels * m_nPtime)/1000;
assert((m_pHeldBufferPtr = tsk_calloc(m_nHeldBufferSize, sizeof(uint8_t))));
}
bool bReset = false;
if(m_nLastPullTime){
m_nLatency += (MPTime::GetNow() - m_nLastPullTime) - m_nPtime;
m_nLatency = TSK_CLAMP(0, m_nLatency, MP_AUDIO_MAX_LATENCY);
// TSK_DEBUG_INFO("m_nLatency=%lld", m_nLatency);
if(m_nLatency >= MP_AUDIO_MAX_LATENCY){
m_nLatency = 0;
bReset = true;
}
}
m_nHeldBufferPos = Pull(m_pHeldBufferPtr, m_nHeldBufferSize);
if(m_nHeldBufferPos){
m_nVolumeComputeCount+=m_nPtime;
if(m_nVolumeComputeCount >= 1000/* FIXME */){
m_nVolumeComputeCount = 0;
m_nVolume = 0;
}
int16_t* pHeldBufferPtr = (int16_t*)m_pHeldBufferPtr;
double nValue = 0;
for(uint32_t i = 0; i< m_nHeldBufferSize>>1; ++i){
nValue += (pHeldBufferPtr[i] > 0) ? 20.0 * log10((double)TSK_ABS(pHeldBufferPtr[i]) / 32768.0) : 72.f;
}
nValue /= m_nHeldBufferSize>>1;
m_nVolume = (m_nVolume + nValue)/(m_nVolumeComputeCount == 0 ? 1 : 2);
//TSK_DEBUG_INFO("m_nVolume=%f", m_nVolume);
}
else{
m_nVolume /= 2;
//TSK_DEBUG_INFO("No Sound");
}
m_nLastPullTime = MPTime::GetNow();
if(bReset){
TSK_DEBUG_INFO("======BOUUUUH======");
}
if(m_nHeldBufferPos > 0){
m_nLastRTPPacketTime = MPTime::GetNow();
}
return m_nHeldBufferPos;
#else
return 0;
#endif
}
unsigned MPProxyPluginConsumerAudio::copyFromHeldBuffer(void* output, unsigned size)
{
if(!output || !size){
TSK_DEBUG_ERROR("Invalid parameter");
return 0;
}
unsigned nRetSize = 0;
if(m_pHeldBufferPtr){
nRetSize = TSK_MIN(m_nHeldBufferPos, size);
if(nRetSize){
memcpy(output, m_pHeldBufferPtr, nRetSize);
}
}
if(nRetSize < size){
// complete with silence
memset(((uint8_t*)output) + nRetSize, 0, (size - nRetSize));
}
return nRetSize;
}
} // namespace