2
* Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
3
* Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
4
* Author: Adrien Beraud <adrien.beraud@wisdomvibes.com>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 3 of the License, or
9
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
* Additional permission under GNU GPL version 3 section 7:
21
* If you modify this program, or any covered work, by linking or
22
* combining it with the OpenSSL project's OpenSSL library (or a
23
* modified version of that library), containing parts covered by the
24
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25
* grants you additional permission to convey the resulting work.
26
* Corresponding Source for a non-source form of such a combination
27
* shall include the source code for the parts of OpenSSL used as well
28
* as that of the covered work.
31
#ifndef AUDIO_RTP_STREAM_H__
32
#define AUDIO_RTP_STREAM_H__
38
#include "audio/codecs/audiocodec.h"
39
#include "audio/audiobuffer.h"
40
#include "noncopyable.h"
42
#include <pjmedia/plc.h>
56
class AudioRtpContext {
58
AudioRtpContext(AudioFormat f);
62
* Ramp in audio data to avoid audio click from peer
64
void fadeIn(AudioBuffer& buf);
67
NON_COPYABLE(AudioRtpContext);
68
static constexpr double FADEIN_STEP_SIZE = 4.0;
70
void resetResampler();
75
AudioBuffer resampledData_;
76
std::unique_ptr<Resampler> resampler_;
79
void applyDSP(AudioBuffer &rawBuffer);
81
std::unique_ptr<DSP> dsp_;
84
friend class AudioRtpStream;
88
* Class meant to store internal data in order to encode/decode,
89
* resample, process, and packetize audio streams.
91
class AudioRtpStream {
93
AudioRtpStream(const std::string &id);
94
virtual ~AudioRtpStream();
98
* Set the list of codecs supported by this stream.
99
* The codec used for encoding must be first.
101
void setRtpMedia(const std::vector<AudioCodec*> &codecs);
104
* Decode audio data received from peer
106
void processDataDecode(uint8_t *spkrData, size_t size, int payloadType);
109
* Wait for available data to be encoded from mainbuffer.
111
bool waitForDataEncode(const std::chrono::milliseconds& max_wait) const;
114
* Encode audio data from mainbuffer
115
* @return size of encoded data, in bytes.
117
size_t processDataEncode();
119
bool hasDynamicPayload() const {
120
return hasDynamicPayloadType_;
123
const uint8_t *getMicDataEncoded() const {
124
return encodedData_.data();
127
int getEncoderPayloadType() const;
128
int getEncoderFrameSize() const;
129
AudioFormat getEncoderFormat() const { return encoder_.format_; }
135
bool codecsDiffer(const std::vector<AudioCodec*> &codecs) const;
136
int getTransportRate() const;
139
const std::string id_;
141
AudioRtpContext encoder_;
142
AudioRtpContext decoder_;
145
sfl::AudioCodec* getCurrentEncoder() const;
146
sfl::AudioCodec* getCurrentDecoder() const;
148
// Decoder-specific methods
149
bool tryToSwitchDecoder(int newPt);
150
void resetDecoderPLC(const sfl::AudioCodec *);
152
std::vector<AudioCodec*> audioCodecs_;
153
std::mutex codecEncMutex_;
154
std::mutex codecDecMutex_;
155
// these will have the same value unless we are sending
156
// a different codec than we are receiving (asymmetric RTP)
157
bool hasDynamicPayloadType_;
158
// FIXME: probably need one for pre-encoder data, one for post-decoder data
159
AudioBuffer rawBuffer_, micData_;
160
std::array<uint8_t, RAW_BUFFER_SIZE> encodedData_;
164
NON_COPYABLE(AudioRtpStream);
165
std::atomic<bool> dead_;
166
size_t currentEncoderIndex_;
167
size_t currentDecoderIndex_;
168
int warningInterval_;
170
pj_caching_pool plcCachePool_;
171
pj_pool_t * plcPool_;
172
// PLC instances, one per channel
173
std::vector<pjmedia_plc*> plcDec_;
178
#endif // AUDIO_RTP_STREAM_H__