-
Notifications
You must be signed in to change notification settings - Fork 582
/
liveaudiosource.h
202 lines (176 loc) · 7.32 KB
/
liveaudiosource.h
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
/* ---------------------------------------------------------------------------
** This software is in the public domain, furnished "as is", without technical
** support, and with no warranty, express or implied, as to its usefulness for
** any purpose.
**
** liveaudiosource.h
**
** -------------------------------------------------------------------------*/
#pragma once
#ifdef WIN32
#include "base/win/wincrypt_shim.h"
#endif
#include <iostream>
#include <thread>
#include <mutex>
#include <queue>
#include <cctype>
#include "environment.h"
#include "pc/local_audio_source.h"
#include "api/audio_codecs/builtin_audio_decoder_factory.h"
template <typename T>
class LiveAudioSource : public webrtc::Notifier<webrtc::AudioSourceInterface>, public T::Callback
{
public:
SourceState state() const override { return kLive; }
bool remote() const override { return true; }
virtual void AddSink(webrtc::AudioTrackSinkInterface *sink) override
{
RTC_LOG(LS_INFO) << "LiveAudioSource::AddSink ";
std::lock_guard<std::mutex> lock(m_sink_lock);
m_sinks.push_back(sink);
}
virtual void RemoveSink(webrtc::AudioTrackSinkInterface *sink) override
{
RTC_LOG(LS_INFO) << "LiveAudioSource::RemoveSink ";
std::lock_guard<std::mutex> lock(m_sink_lock);
m_sinks.remove(sink);
}
void CaptureThread()
{
m_env.mainloop();
}
// overide RTSPConnection::Callback
virtual bool onNewSession(const char *id, const char *media, const char *codec, const char *sdp, unsigned int rtpfrequency, unsigned int channels) override
{
bool success = false;
if (strcmp(media, "audio") == 0)
{
RTC_LOG(LS_INFO) << "LiveAudioSource::onNewSession " << media << "/" << codec << " " << sdp;
m_freq = rtpfrequency;
m_channel = channels;
RTC_LOG(LS_INFO) << "LiveAudioSource::onNewSession codec:" << " freq:" << m_freq << " channel:" << m_channel;
std::map<std::string, std::string> params;
if (m_channel == 2)
{
params["stereo"] = "1";
}
webrtc::SdpAudioFormat format = webrtc::SdpAudioFormat(codec, m_freq, m_channel, std::move(params));
if (m_factory->IsSupportedDecoder(format))
{
m_decoder = m_factory->MakeAudioDecoder(format, absl::optional<webrtc::AudioCodecPairId>());
m_codec[id] = codec;
success = true;
}
else
{
RTC_LOG(LS_ERROR) << "LiveAudioSource::onNewSession not support codec" << sdp;
}
}
return success;
}
virtual bool onData(const char *id, unsigned char *buffer, ssize_t size, struct timeval presentationTime) override
{
bool success = false;
int segmentLength = m_freq / 100;
if (m_codec.find(id) != m_codec.end())
{
int64_t sourcets = presentationTime.tv_sec;
sourcets = sourcets * 1000 + presentationTime.tv_usec / 1000;
int64_t ts = std::chrono::high_resolution_clock::now().time_since_epoch().count() / 1000 / 1000;
RTC_LOG(LS_VERBOSE) << "LiveAudioSource::onData decode ts:" << ts
<< " source ts:" << sourcets;
if (m_decoder.get() != NULL)
{
// waiting
if ((m_wait) && (m_prevts != 0))
{
int64_t periodSource = sourcets - m_previmagets;
int64_t periodDecode = ts - m_prevts;
RTC_LOG(LS_VERBOSE) << "LiveAudioSource::onData interframe decode:" << periodDecode << " source:" << periodSource;
int64_t delayms = periodSource - periodDecode;
if ((delayms > 0) && (delayms < 1000))
{
std::this_thread::sleep_for(std::chrono::milliseconds(delayms));
}
}
int maxDecodedBufferSize = m_decoder->PacketDuration(buffer, size) * m_channel * sizeof(int16_t);
int16_t *decoded = new int16_t[maxDecodedBufferSize];
webrtc::AudioDecoder::SpeechType speech_type;
int decodedBufferSize = m_decoder->Decode(buffer, size, m_freq, maxDecodedBufferSize, decoded, &speech_type);
RTC_LOG(LS_VERBOSE) << "LiveAudioSource::onData size:" << size << " decodedBufferSize:" << decodedBufferSize << " maxDecodedBufferSize: " << maxDecodedBufferSize << " channels: " << m_channel;
if (decodedBufferSize > 0)
{
for (int i = 0; i < decodedBufferSize; ++i)
{
m_buffer.push(decoded[i]);
}
}
else
{
RTC_LOG(LS_ERROR) << "LiveAudioSource::onData error:Decode Audio failed";
}
delete[] decoded;
while (m_buffer.size() > segmentLength * m_channel)
{
int16_t *outbuffer = new int16_t[segmentLength * m_channel];
for (int i = 0; i < segmentLength * m_channel; ++i)
{
uint16_t value = m_buffer.front();
outbuffer[i] = value;
m_buffer.pop();
}
std::lock_guard<std::mutex> lock(m_sink_lock);
for (auto *sink : m_sinks)
{
sink->OnData(outbuffer, 16, m_freq, m_channel, segmentLength);
}
delete[] outbuffer;
}
m_previmagets = sourcets;
m_prevts = std::chrono::high_resolution_clock::now().time_since_epoch().count() / 1000 / 1000;
success = true;
}
else
{
RTC_LOG(LS_VERBOSE) << "LiveAudioSource::onData error:No Audio decoder";
}
}
return success;
}
protected:
LiveAudioSource(rtc::scoped_refptr<webrtc::AudioDecoderFactory> audioDecoderFactory, const std::string &uri, const std::map<std::string, std::string> &opts, bool wait)
: m_env(m_stop)
, m_connection(m_env, this, uri.c_str(), opts, rtc::LogMessage::GetLogToDebug() <= 2)
, m_factory(audioDecoderFactory)
, m_freq(8000)
, m_channel(1)
, m_wait(wait)
, m_previmagets(0)
, m_prevts(0)
{
m_capturethread = std::thread(&LiveAudioSource::CaptureThread, this);
}
virtual ~LiveAudioSource()
{
m_env.stop();
m_capturethread.join();
}
private:
char m_stop;
Environment m_env;
private:
T m_connection;
std::thread m_capturethread;
rtc::scoped_refptr<webrtc::AudioDecoderFactory> m_factory;
std::unique_ptr<webrtc::AudioDecoder> m_decoder;
int m_freq;
int m_channel;
std::queue<uint16_t> m_buffer;
std::list<webrtc::AudioTrackSinkInterface *> m_sinks;
std::mutex m_sink_lock;
std::map<std::string, std::string> m_codec;
bool m_wait;
int64_t m_previmagets;
int64_t m_prevts;
};