~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to daemon/src/audio/audiortp/audio_rtp_stream.h

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2015-01-07 14:51:16 UTC
  • mfrom: (4.3.5 sid)
  • Revision ID: package-import@ubuntu.com-20150107145116-yxnafinf4lrdvrmx
Tags: 1.4.1-0.1ubuntu1
* Merge with Debian, remaining changes:
 - Drop soprano, nepomuk build-dep
* Drop ubuntu patches, now upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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>
 
5
 *
 
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.
 
14
 *
 
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.
 
18
 *
 
19
 *  Additional permission under GNU GPL version 3 section 7:
 
20
 *
 
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.
 
29
 */
 
30
 
 
31
#ifndef AUDIO_RTP_STREAM_H__
 
32
#define AUDIO_RTP_STREAM_H__
 
33
 
 
34
#ifdef HAVE_CONFIG_H
 
35
#include "config.h"
 
36
#endif
 
37
 
 
38
#include "audio/codecs/audiocodec.h"
 
39
#include "audio/audiobuffer.h"
 
40
#include "noncopyable.h"
 
41
 
 
42
#include <pjmedia/plc.h>
 
43
#include <pj/pool.h>
 
44
 
 
45
#include <array>
 
46
#include <mutex>
 
47
#include <memory>
 
48
#include <atomic>
 
49
#include <cstddef>
 
50
 
 
51
class Resampler;
 
52
class DSP;
 
53
 
 
54
namespace sfl {
 
55
 
 
56
class AudioRtpContext {
 
57
    public:
 
58
    AudioRtpContext(AudioFormat f);
 
59
    ~AudioRtpContext();
 
60
 
 
61
    /**
 
62
     * Ramp in audio data to avoid audio click from peer
 
63
     */
 
64
    void fadeIn(AudioBuffer& buf);
 
65
 
 
66
    private:
 
67
    NON_COPYABLE(AudioRtpContext);
 
68
    static constexpr double FADEIN_STEP_SIZE = 4.0;
 
69
 
 
70
    void resetResampler();
 
71
    double fadeFactor_;
 
72
    int payloadType_;
 
73
    int frameSize_;
 
74
    AudioFormat format_;
 
75
    AudioBuffer resampledData_;
 
76
    std::unique_ptr<Resampler> resampler_;
 
77
#if HAVE_SPEEXDSP
 
78
    void resetDSP();
 
79
    void applyDSP(AudioBuffer &rawBuffer);
 
80
    std::mutex dspMutex_;
 
81
    std::unique_ptr<DSP> dsp_;
 
82
#endif
 
83
 
 
84
    friend class AudioRtpStream;
 
85
};
 
86
 
 
87
/**
 
88
 * Class meant to store internal data in order to encode/decode,
 
89
 * resample, process, and packetize audio streams.
 
90
 */
 
91
class AudioRtpStream {
 
92
    public:
 
93
        AudioRtpStream(const std::string &id);
 
94
        virtual ~AudioRtpStream();
 
95
        void initBuffers();
 
96
 
 
97
        /**
 
98
         * Set the list of codecs supported by this stream.
 
99
         * The codec used for encoding must be first.
 
100
         */
 
101
        void setRtpMedia(const std::vector<AudioCodec*> &codecs);
 
102
 
 
103
        /**
 
104
         * Decode audio data received from peer
 
105
         */
 
106
        void processDataDecode(uint8_t *spkrData, size_t size, int payloadType);
 
107
 
 
108
        /**
 
109
         * Wait for available data to be encoded from mainbuffer.
 
110
         */
 
111
        bool waitForDataEncode(const std::chrono::milliseconds& max_wait) const;
 
112
 
 
113
        /**
 
114
         * Encode audio data from mainbuffer
 
115
         * @return size of encoded data, in bytes.
 
116
         */
 
117
        size_t processDataEncode();
 
118
 
 
119
        bool hasDynamicPayload() const {
 
120
            return hasDynamicPayloadType_;
 
121
        }
 
122
 
 
123
        const uint8_t *getMicDataEncoded() const {
 
124
            return encodedData_.data();
 
125
        }
 
126
 
 
127
        int getEncoderPayloadType() const;
 
128
        int getEncoderFrameSize() const;
 
129
        AudioFormat getEncoderFormat() const { return encoder_.format_; }
 
130
 
 
131
#if HAVE_SPEEXDSP
 
132
        void initDSP();
 
133
        void resetDSP();
 
134
#endif
 
135
        bool codecsDiffer(const std::vector<AudioCodec*> &codecs) const;
 
136
        int getTransportRate() const;
 
137
 
 
138
    private:
 
139
        const std::string id_;
 
140
 
 
141
        AudioRtpContext encoder_;
 
142
        AudioRtpContext decoder_;
 
143
 
 
144
        void deleteCodecs();
 
145
        sfl::AudioCodec* getCurrentEncoder() const;
 
146
        sfl::AudioCodec* getCurrentDecoder() const;
 
147
 
 
148
        // Decoder-specific methods
 
149
        bool tryToSwitchDecoder(int newPt);
 
150
        void resetDecoderPLC(const sfl::AudioCodec *);
 
151
 
 
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_;
 
161
 
 
162
        bool isDead();
 
163
 
 
164
        NON_COPYABLE(AudioRtpStream);
 
165
        std::atomic<bool> dead_;
 
166
        size_t currentEncoderIndex_;
 
167
        size_t currentDecoderIndex_;
 
168
        int warningInterval_;
 
169
 
 
170
        pj_caching_pool plcCachePool_;
 
171
        pj_pool_t * plcPool_;
 
172
        // PLC instances, one per channel
 
173
        std::vector<pjmedia_plc*> plcDec_;
 
174
 
 
175
};
 
176
}
 
177
 
 
178
#endif // AUDIO_RTP_STREAM_H__