audiortp.h 4.44 KB
Newer Older
jpbl's avatar
jpbl committed
1
/*
2
 *  Copyright (C) 2004-2007 Savoir-Faire Linux inc.
3
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
4
 *  Author: Alexandre Bourget <alexandre.bourget@savoirfairelinux.com>
jpbl's avatar
jpbl committed
5
6
7
8
9
 *  Author: Yan Morin <yan.morin@savoirfairelinux.com>
 *  Author: Laurielle Lea <laurielle.lea@savoirfairelinux.com>
 * 
 *  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
10
 *  the Free Software Foundation; either version 3 of the License, or
jpbl's avatar
jpbl committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *  (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
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef __AUDIO_RTP_H__
#define __AUDIO_RTP_H__

#include <cstdio>
#include <cstdlib>
27
28
#include <fstream> // fstream + iostream for _fstream debugging...
#include <iostream>
jpbl's avatar
jpbl committed
29
30
31
32

#include <ccrtp/rtp.h>
#include <cc++/numbers.h>

33
34
#include <samplerate.h>

35
#include "../global.h"
Alexandre Bourget's avatar
Alexandre Bourget committed
36

37
38
39
40
#define UP_SAMPLING 0
#define DOWN_SAMPLING 1


yanmorin's avatar
   
yanmorin committed
41
class SIPCall;
jpbl's avatar
jpbl committed
42
43
44
45
46

///////////////////////////////////////////////////////////////////////////////
// Two pair of sockets
///////////////////////////////////////////////////////////////////////////////
class AudioRtpRTX : public ost::Thread, public ost::TimerPort {
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
	public:
		AudioRtpRTX (SIPCall *, bool);
		~AudioRtpRTX();

		ost::Time *time; 	// For incoming call notification 
		virtual void run ();

	private:
		SIPCall* _ca;
		ost::RTPSession *_sessionSend;
		ost::RTPSession *_sessionRecv;
		ost::SymmetricRTPSession *_session;
		ost::Semaphore _start;
		bool _sym;

		/** When we receive data, we decode it inside this buffer */
		int16* _receiveDataDecoded;
		/** Buffers used for send data from the mic */
		unsigned char* _sendDataEncoded;
		int16* _intBufferDown;

		/** After that we send the data inside this buffer if there is a format conversion or rate conversion */
		/** Also use for getting mic-ringbuffer data */
		SFLDataFormat* _dataAudioLayer;

		/** Buffers used for sample rate conversion */
		float32* _floatBufferDown;
		float32* _floatBufferUp;

		/** Debugging output file */
		//std::ofstream _fstream;

		/** libsamplerate converter for incoming voice */
		SRC_STATE*    _src_state_spkr;
		/** libsamplerate converter for outgoing voice */
		SRC_STATE*    _src_state_mic;
		/** libsamplerate error */
		int           _src_err;

		// Variables to process audio stream
		int _layerSampleRate;  // sample rate for playing sound (typically 44100HZ)
		int _codecSampleRate; // sample rate of the codec we use to encode and decode (most of time 8000HZ)
		int _layerFrameSize; // length of the sound frame we capture in ms(typically 20ms)

		void initAudioRtpSession(void);
		/**
 		 * Get the data from the mic, encode it and send it through the RTP session
 		 */ 		 	
		void sendSessionFromMic(int);
		/**
 		 * Get the data from the RTP packets, decode it and send it to the sound card
 		 */		 	
		void receiveSessionForSpkr(int&);
		/**
 		 * Init the buffers used for processing sound data
 		 */ 
		void initBuffers(void);
		/**
 		 * Call the appropriate function, up or downsampling
 		 */ 
		int reSampleData(int, int ,int);
		/**
 		 * Upsample the data from the clock rate of the codec to the sample rate of the layer
 		 * @param int The clock rate of the codec
 		 * @param int The number of samples we have in the buffer
 		 * @return int The number of samples after the operation
 		 */
		int upSampleData(int, int);
		/**
 		 * Downsample the data from the sample rate of the layer to the clock rate of the codec
 		 *  @param int The clock rate of the codec
 		 *  @param int The number of samples we have in the buffer 
 		 *  @return int The number of samples after the operation
 		 */
		int downSampleData(int, int);
jpbl's avatar
jpbl committed
122
123
124
125
126
127
};

///////////////////////////////////////////////////////////////////////////////
// Main class rtp
///////////////////////////////////////////////////////////////////////////////
class AudioRtp {
128
129
130
	public:
		AudioRtp();
		~AudioRtp();
jpbl's avatar
jpbl committed
131

132
133
		int 			createNewSession (SIPCall *);
		void			closeRtpSession	 ();
jpbl's avatar
jpbl committed
134

135
136
137
138
	private:
		AudioRtpRTX*	        _RTXThread;
		bool			_symmetric;
		ost::Mutex            _threadMutex;
jpbl's avatar
jpbl committed
139
140
141
};

#endif // __AUDIO_RTP_H__