audio_rtp_record_handler.h 6.18 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
3
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
Adrien Béraud's avatar
Adrien Béraud committed
4
 *  Author: Adrien Beraud <adrien.beraud@wisdomvibes.com>
5 6 7 8 9 10 11 12 13 14 15 16
 *
 *  This program 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.
 *  This program 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 this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
 */

31 32
#ifndef AUDIO_RTP_RECORD_HANDLER_H__
#define AUDIO_RTP_RECORD_HANDLER_H__
33 34 35 36 37

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

38 39 40 41 42
#include <cstddef>

using std::ptrdiff_t;

#include <ccrtp/rtp.h>
43
#include <tr1/array>
44
#include <list>
45
#include <mutex>
46

47
#include "noncopyable.h"
48 49 50
#include "audio/codecs/audiocodec.h"
#include "audio/samplerateconverter.h"
#include "audio/noisesuppress.h"
51
#include "audio/gaincontrol.h"
52

53 54
class SIPCall;

55
namespace sfl {
56

Alexandre Savard's avatar
Alexandre Savard committed
57 58 59 60
// G.722 VoIP is typically carried in RTP payload type 9.[2] Note that IANA records the clock rate for type 9 G.722 as 8 kHz
// (instead of 16 kHz), RFC3551[3]  clarifies that this is due to a historical error and is retained in order to maintain backward
// compatibility. Consequently correct implementations represent the value 8,000 where required but encode and decode audio at 16 kHz.

61
inline uint32
62
timeval2microtimeout(const timeval& t)
Alexandre Savard's avatar
Alexandre Savard committed
63
{
64
    return ((t.tv_sec * 1000000ul) + t.tv_usec);
Alexandre Savard's avatar
Alexandre Savard committed
65
}
66

67
struct DTMFEvent {
68
    DTMFEvent(char digit);
69 70 71 72 73
    ost::RTPPacket::RFC2833Payload payload;
    bool newevent;
    int length;
};

74 75 76
/**
 * Class meant to store internal data in order to encode/decode,
 * resample, process, and packetize audio streams. This class should not be
77
 * handled directly. Use AudioRtpRecordHandler
78
 */
79
class AudioRtpRecord {
Alexandre Savard's avatar
Alexandre Savard committed
80
    public:
81
        AudioRtpRecord();
82
        ~AudioRtpRecord();
83
        void deleteCodecs();
84 85
        bool tryToSwitchPayloadTypes(int newPt);
        sfl::AudioCodec* getCurrentCodec() const;
86 87 88
        std::string callId_;
        int codecSampleRate_;
        std::list<DTMFEvent> dtmfQueue_;
Alexandre Savard's avatar
Alexandre Savard committed
89

90
    private:
91
        std::vector<AudioCodec*> audioCodecs_;
92
        std::mutex audioCodecMutex_;
93 94 95 96
        // these will have the same value unless we are sending
        // a different codec than we are receiving (asymmetric RTP)
        int encoderPayloadType_;
        int decoderPayloadType_;
97
        bool hasDynamicPayloadType_;
98 99
        AudioBuffer decData_;
        AudioBuffer resampledData_;
100
        std::tr1::array<unsigned char, DEC_BUFFER_SIZE> encodedData_;
101 102
        SamplerateConverter *converterEncode_;
        SamplerateConverter *converterDecode_;
103 104
        int codecFrameSize_;
        int converterSamplingRate_;
105
        double fadeFactor_;
106 107

#if HAVE_SPEEXDSP
108
        NoiseSuppress *noiseSuppressEncode_;
109
        NoiseSuppress *noiseSuppressDecode_;
110
        std::mutex audioProcessMutex_;
111 112
#endif

113
        unsigned int dtmfPayloadType_;
114

115
        bool isDead();
116 117 118 119
        friend class AudioRtpRecordHandler;
        /**
        * Ramp In audio data to avoid audio click from peer
        */
120
        void fadeInDecodedData();//size_t size);
121
        NON_COPYABLE(AudioRtpRecord);
122 123 124 125 126
#ifdef CCPP_PREFIX
        ost::AtomicCounter dead_;
#else
        ucommon::atomic::counter dead_;
#endif
127
        size_t currentCodecIndex_;
128 129 130
};


131
class AudioRtpRecordHandler {
Alexandre Savard's avatar
Alexandre Savard committed
132
    public:
133
        AudioRtpRecordHandler(SIPCall &);
134
        virtual ~AudioRtpRecordHandler();
135

Alexandre Savard's avatar
Alexandre Savard committed
136 137 138
        /**
         *  Set rtp media for this session
         */
139
        void setRtpMedia(const std::vector<AudioCodec*> &audioCodec);
140

141
        AudioCodec *getAudioCodec() const {
142
            return audioRtpRecord_.audioCodecs_[0];
Alexandre Savard's avatar
Alexandre Savard committed
143
        }
144

145 146
        int getEncoderPayloadType() const {
            return audioRtpRecord_.encoderPayloadType_;
Alexandre Savard's avatar
Alexandre Savard committed
147
        }
148

149 150
        int getCodecSampleRate() const {
            return audioRtpRecord_.codecSampleRate_;
Alexandre Savard's avatar
Alexandre Savard committed
151
        }
152

153 154
        int getCodecFrameSize() const {
            return audioRtpRecord_.codecFrameSize_;
Alexandre Savard's avatar
Alexandre Savard committed
155
        }
156

157 158
        bool getHasDynamicPayload() const {
            return audioRtpRecord_.hasDynamicPayloadType_;
Alexandre Savard's avatar
Alexandre Savard committed
159
        }
160

161
        bool hasDTMFPending() const {
162
            return not audioRtpRecord_.dtmfQueue_.empty();
Alexandre Savard's avatar
Alexandre Savard committed
163
        }
164

165
        const unsigned char *getMicDataEncoded() const {
166
            return audioRtpRecord_.encodedData_.data();
Alexandre Savard's avatar
Alexandre Savard committed
167
        }
168

169
        void initBuffers();
170

171
#if HAVE_SPEEXDSP
172
        void initNoiseSuppress();
173
#endif
174

Alexandre Savard's avatar
Alexandre Savard committed
175 176 177
        /**
         * Encode audio data from mainbuffer
         */
178
        int processDataEncode();
179

Alexandre Savard's avatar
Alexandre Savard committed
180 181 182
        /**
         * Decode audio data received from peer
         */
Tristan Matthews's avatar
Tristan Matthews committed
183
        void processDataDecode(unsigned char * spkrData, size_t size, int payloadType);
184

185
        void setDtmfPayloadType(unsigned int payloadType) {
186
            audioRtpRecord_.dtmfPayloadType_ = payloadType;
187 188
        }

189 190
        unsigned int getDtmfPayloadType() const {
            return audioRtpRecord_.dtmfPayloadType_;
191 192
        }

193
        void putDtmfEvent(char digit);
194

195 196
        std::string getCurrentAudioCodecNames();

197
    protected:
198
        bool codecsDiffer(const std::vector<AudioCodec*> &codecs) const;
Tristan Matthews's avatar
Tristan Matthews committed
199
        AudioRtpRecord audioRtpRecord_;
200

201
    private:
202
        const std::string id_;
Tristan Matthews's avatar
Tristan Matthews committed
203
        GainControl gainController_;
204
        int warningInterval_;
205 206 207
};
}

208
#endif // AUDIO_RTP_RECORD_HANDLER_H__