~ubuntu-branches/ubuntu/saucy/sflphone/saucy

« back to all changes in this revision

Viewing changes to sflphone-common/src/audio/audiortp/AudioRtpRecordHandler.h

  • Committer: Bazaar Package Importer
  • Author(s): Francois Marier
  • Date: 2010-12-24 16:33:55 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20101224163355-tkvvikqxbrbav6up
Tags: 0.9.11-1
* New upstream release
* Add new build dependencies on libwebkit-dev and libyaml-dev

* Bump Standards-Version up to 3.9.1
* Bump debhelper compatibility to 8
* Patch another typo in the upstream code (lintian notice)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
 
3
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
 
4
 *
 
5
 *  This program is free software; you can redistribute it and/or modify
 
6
 *  it under the terms of the GNU General Public License as published by
 
7
 *  the Free Software Foundation; either version 3 of the License, or
 
8
 *  (at your option) any later version.
 
9
 *  This program is distributed in the hope that it will be useful,
 
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *  GNU General Public License for more details.
 
13
 *
 
14
 *  You should have received a copy of the GNU General Public License
 
15
 *  along with this program; if not, write to the Free Software
 
16
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
 *
 
18
 *  Additional permission under GNU GPL version 3 section 7:
 
19
 *
 
20
 *  If you modify this program, or any covered work, by linking or
 
21
 *  combining it with the OpenSSL project's OpenSSL library (or a
 
22
 *  modified version of that library), containing parts covered by the
 
23
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 
24
 *  grants you additional permission to convey the resulting work.
 
25
 *  Corresponding Source for a non-source form of such a combination
 
26
 *  shall include the source code for the parts of OpenSSL used as well
 
27
 *  as that of the covered work.
 
28
 */
 
29
 
 
30
#ifndef AUDIORTPRECORDHANDLER_H_
 
31
#define AUDIORTPRECORDHANDLER_H_
 
32
 
 
33
#include "sip/sipcall.h"
 
34
#include "audio/codecs/audiocodec.h"
 
35
#include "audio/samplerateconverter.h"
 
36
#include "audio/audioprocessing.h"
 
37
#include "audio/noisesuppress.h"
 
38
#include "managerimpl.h"
 
39
#include <ccrtp/rtp.h>
 
40
 
 
41
namespace sfl
 
42
{
 
43
 
 
44
// Frequency (in packet number)
 
45
#define RTP_TIMESTAMP_RESET_FREQ 100
 
46
 
 
47
// Factor use to increase volume in fade in
 
48
#define FADEIN_STEP_SIZE 4;
 
49
 
 
50
static const int schedulingTimeout = 10000;
 
51
static const int expireTimeout = 100000;
 
52
 
 
53
// 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
 
54
// (instead of 16 kHz), RFC3551[3]  clarifies that this is due to a historical error and is retained in order to maintain backward
 
55
// compatibility. Consequently correct implementations represent the value 8,000 where required but encode and decode audio at 16 kHz.
 
56
static const int g722PayloadType = 9;
 
57
static const int g722RtpClockRate = 8000;
 
58
static const int g722RtpTimeincrement = 160;
 
59
 
 
60
inline uint32
 
61
timeval2microtimeout (const timeval& t)
 
62
{
 
63
    return ( (t.tv_sec * 1000000ul) + t.tv_usec);
 
64
}
 
65
 
 
66
class AudioRtpSessionException: public std::exception
 
67
{
 
68
        virtual const char* what() const throw() {
 
69
            return "AudioRtpSessionException occured";
 
70
        }
 
71
};
 
72
 
 
73
typedef struct DtmfEvent {
 
74
    ost::RTPPacket::RFC2833Payload payload;
 
75
    int length;
 
76
    bool newevent;
 
77
} DtmfEvent;
 
78
 
 
79
typedef list<DtmfEvent *> EventQueue;
 
80
 
 
81
/**
 
82
 * Class meant to store internal data in order to encode/decode,
 
83
 * resample, process, and packetize audio streams. This class should not be
 
84
 * handled directly. Use AudioRtpRecorrdHandeler
 
85
 */
 
86
class AudioRtpRecord
 
87
{
 
88
    public:
 
89
        AudioRtpRecord ();
 
90
        ~AudioRtpRecord();
 
91
 
 
92
        AudioCodec *_audioCodec;
 
93
        int _codecPayloadType;
 
94
        bool _hasDynamicPayloadType;
 
95
        SFLDataFormat *_micData;
 
96
        SFLDataFormat *_micDataConverted;
 
97
        unsigned char *_micDataEncoded;
 
98
        SFLDataFormat *_spkrDataDecoded;
 
99
        SFLDataFormat *_spkrDataConverted;
 
100
        SamplerateConverter *_converter;
 
101
        int _audioLayerSampleRate;
 
102
        int _codecSampleRate;
 
103
        int _audioLayerFrameSize;
 
104
        int _codecFrameSize;
 
105
        int _converterSamplingRate;
 
106
        EventQueue _eventQueue;
 
107
        bool _micFadeInComplete;
 
108
        bool _spkrFadeInComplete;
 
109
        SFLDataFormat _micAmplFactor;
 
110
        SFLDataFormat _spkrAmplFactor;
 
111
        AudioProcessing *_audioProcess;
 
112
        NoiseSuppress *_noiseSuppress;
 
113
 
 
114
};
 
115
 
 
116
 
 
117
class AudioRtpRecordHandler
 
118
{
 
119
    public:
 
120
        AudioRtpRecordHandler (ManagerImpl *manager, SIPCall *ca);
 
121
        ~AudioRtpRecordHandler();
 
122
 
 
123
        /**
 
124
         *  Set rtp media for this session
 
125
         */
 
126
 
 
127
        void setRtpMedia (AudioCodec* audioCodec);
 
128
 
 
129
 
 
130
        AudioCodec *getAudioCodec (void) {
 
131
            return _audioRtpRecord._audioCodec;
 
132
        }
 
133
 
 
134
        int getCodecPayloadType (void) {
 
135
            return _audioRtpRecord._codecPayloadType;
 
136
        }
 
137
 
 
138
        int getCodecSampleRate (void) {
 
139
            return _audioRtpRecord._codecSampleRate;
 
140
        }
 
141
 
 
142
        int getCodecFrameSize (void) {
 
143
            return _audioRtpRecord._codecFrameSize;
 
144
        }
 
145
 
 
146
        int getHasDynamicPayload (void) {
 
147
            return _audioRtpRecord._hasDynamicPayloadType;
 
148
        }
 
149
 
 
150
        int getAudioLayerFrameSize (void) {
 
151
            return _audioRtpRecord._audioLayerFrameSize;
 
152
        }
 
153
 
 
154
        int getAudioLayerSampleRate (void) {
 
155
            return _audioRtpRecord._audioLayerSampleRate;
 
156
        }
 
157
 
 
158
        EventQueue *getEventQueue (void) {
 
159
            return &_audioRtpRecord._eventQueue;
 
160
        }
 
161
 
 
162
        int getEventQueueSize (void) {
 
163
            return _audioRtpRecord._eventQueue.size();
 
164
        }
 
165
 
 
166
        SFLDataFormat *getMicData (void) {
 
167
            return _audioRtpRecord._micData;
 
168
        }
 
169
 
 
170
        SFLDataFormat *getMicDataConverted (void) {
 
171
            return _audioRtpRecord._micDataConverted;
 
172
        }
 
173
 
 
174
        unsigned char *getMicDataEncoded (void) {
 
175
            return _audioRtpRecord._micDataEncoded;
 
176
        }
 
177
 
 
178
 
 
179
        inline float computeCodecFrameSize (int codecSamplePerFrame, int codecClockRate) {
 
180
            return ( (float) codecSamplePerFrame * 1000.0) / (float) codecClockRate;
 
181
        }
 
182
 
 
183
        int computeNbByteAudioLayer (float codecFrameSize) {
 
184
            return (int) ( ( (float) _audioRtpRecord._codecSampleRate * codecFrameSize * sizeof (SFLDataFormat)) / 1000.0);
 
185
        }
 
186
 
 
187
        void init (void);
 
188
 
 
189
        /**
 
190
         * Allocate memory for RTP buffers and fill them with zeros
 
191
         * @prereq Session codec needs to be initialized prior calling this method
 
192
         */
 
193
        void initBuffers (void);
 
194
 
 
195
        void initNoiseSuppress (void);
 
196
 
 
197
        /**
 
198
         * Encode audio data from mainbuffer
 
199
         */
 
200
        int processDataEncode (void);
 
201
 
 
202
        /**
 
203
         * Decode audio data received from peer
 
204
         */
 
205
        void processDataDecode (unsigned char * spkrData, unsigned int size);
 
206
 
 
207
        /**
 
208
        * Ramp In audio data to avoid audio click from peer
 
209
        */
 
210
        bool fadeIn (SFLDataFormat *audio, int size, SFLDataFormat *factor);
 
211
 
 
212
        void putDtmfEvent (int digit);
 
213
 
 
214
    private:
 
215
 
 
216
        AudioRtpRecord  _audioRtpRecord;
 
217
 
 
218
        SIPCall *_ca;
 
219
 
 
220
};
 
221
 
 
222
}
 
223
 
 
224
#endif /* AUDIORTPRECORD_H_ */