-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathpushtotalk2.cpp
379 lines (298 loc) · 10.2 KB
/
pushtotalk2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
* \file feedbackexample.cpp
*/
#include <mipconfig.h>
#if(defined(MIPCONFIG_SUPPORT_OSS) || defined(MIPCONFIG_SUPPORT_WINMM) || defined(MIPCONFIG_SUPPORT_PORTAUDIO))
#include <mipcomponentchain.h>
#include <mipcomponent.h>
#include <miptime.h>
#include <mipaveragetimer.h>
#include <mipwavinput.h>
#include <mipsamplingrateconverter.h>
#include <mipsampleencoder.h>
#include <mipulawencoder.h>
#include <miprtpulawencoder.h>
#include <miprtpcomponent.h>
#include <miprtpdecoder.h>
#include <miprtpulawdecoder.h>
#include <mipulawdecoder.h>
#include <mipaudiomixer.h>
#include <miprawaudiomessage.h> // Needed for MIPRAWAUDIOMESSAGE_TYPE_S16LE etc
#include <mipcomponentalias.h>
#ifdef MIPCONFIG_SUPPORT_WINMM
#include <mipwinmmoutput.h>
#else
#ifdef MIPCONFIG_SUPPORT_PORTAUDIO
#include <mippainputoutput.h>
#define NEED_PA_INIT
#else
#include <mipossinputoutput.h>
#endif
#endif
#include <jrtplib3/rtpsession.h>
#include <jrtplib3/rtpsessionparams.h>
#include <jrtplib3/rtpipv4address.h>
#include <jrtplib3/rtpudpv4transmitter.h>
#include <jrtplib3/rtperrors.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace jrtplib;
void checkError(bool returnValue, const MIPComponent &component)
{
if (returnValue == true)
return;
std::cerr << "An error occured in component: " << component.getComponentName() << std::endl;
std::cerr << "Error description: " << component.getErrorString() << std::endl;
exit(-1);
}
void checkError(bool returnValue, const MIPComponentChain &chain)
{
if (returnValue == true)
return;
std::cerr << "An error occured in chain: " << chain.getName() << std::endl;
std::cerr << "Error description: " << chain.getErrorString() << std::endl;
exit(-1);
}
// We'll be using an RTPSession instance from the JRTPLIB library. The following
// function checks the JRTPLIB error code.
void checkError(int status)
{
if (status >= 0)
return;
std::cerr << "An error occured in the RTP component: " << std::endl;
std::cerr << "Error description: " << RTPGetErrorString(status) << std::endl;
exit(-1);
}
class MyChain : public MIPComponentChain
{
public:
MyChain(const std::string &chainName) : MIPComponentChain(chainName)
{
}
private:
void onThreadExit(bool error, const std::string &errorComponent, const std::string &errorDescription)
{
if (!error)
return;
std::cerr << "An error occured in the background thread." << std::endl;
std::cerr << " Component: " << errorComponent << std::endl;
std::cerr << " Error description: " << errorDescription << std::endl;
}
};
class ToggleOutputComponent : public MIPComponent
{
public:
ToggleOutputComponent(MIPComponent *pRealComponent) : MIPComponent(std::string("ToggleOutputComponent[") + pRealComponent->getComponentName() + "]")
{
m_pRealComponent = pRealComponent;
m_enabled = true;
}
bool push(const MIPComponentChain &chain, int64_t iteration, MIPMessage *pMsg)
{
return m_pRealComponent->push(chain, iteration, pMsg);
}
bool pull(const MIPComponentChain &chain, int64_t iteration, MIPMessage **pMsg)
{
if (m_enabled)
return m_pRealComponent->pull(chain, iteration, pMsg);
*pMsg = 0; // don't output messages
return true;
}
void setEnabled(bool f)
{
m_enabled = f;
}
private:
MIPComponent *m_pRealComponent;
bool m_enabled;
};
int main(void)
{
#ifdef NEED_PA_INIT
std::string errStr;
if (!MIPPAInputOutput::initializePortAudio(errStr))
{
std::cerr << "Can't initialize PortAudio: " << errStr << std::endl;
return -1;
}
#endif // NEED_PA_INIT
#ifdef WIN32
WSADATA dat;
WSAStartup(MAKEWORD(2,2),&dat);
#endif // WIN32
MIPTime interval(0.020); // We'll use 20 millisecond intervals.
MIPAverageTimer timer(interval);
MIPWAVInput sndFileInput;
MIPSamplingRateConverter sampConv, sampConv2;
MIPSampleEncoder sampEnc, sampEnc2, sampEnc3;
MIPULawEncoder uLawEnc;
MIPRTPULawEncoder rtpEnc;
MIPRTPComponent rtpComp;
MIPRTPDecoder rtpDec;
MIPRTPULawDecoder rtpULawDec;
MIPULawDecoder uLawDec;
MIPAudioMixer mixer;
MIPComponentAlias rtpCompAlias(&rtpComp);
ToggleOutputComponent sndToggleComponent(&sndFileInput);
#ifdef MIPCONFIG_SUPPORT_WINMM
MIPWinMMOutput sndCardOutput;
#else
#ifdef MIPCONFIG_SUPPORT_PORTAUDIO
MIPPAInputOutput sndCardOutput;
#else
MIPOSSInputOutput sndCardOutput;
#endif
#endif
MyChain chain("Sound file player");
RTPSession rtpSession;
bool returnValue;
// We'll open the file 'soundfile.wav'.
returnValue = sndFileInput.open("soundfile.wav", interval);
checkError(returnValue, sndFileInput);
// We'll convert to a sampling rate of 8000Hz and mono sound.
int samplingRate = 8000;
int numChannels = 1;
returnValue = sampConv.init(samplingRate, numChannels);
checkError(returnValue, sampConv);
// Initialize the sample encoder: the RTP U-law audio encoder
// expects native endian signed 16 bit samples.
returnValue = sampEnc.init(MIPRAWAUDIOMESSAGE_TYPE_S16);
checkError(returnValue, sampEnc);
// Convert samples to U-law encoding
returnValue = uLawEnc.init();
checkError(returnValue, uLawEnc);
// Initialize the RTP audio encoder: this component will create
// RTP messages which can be sent to the RTP component.
returnValue = rtpEnc.init();
checkError(returnValue, rtpEnc);
// We'll initialize the RTPSession object which is needed by the
// RTP component.
RTPUDPv4TransmissionParams transmissionParams;
RTPSessionParams sessionParams;
int portBase = 60000;
int status;
transmissionParams.SetPortbase(portBase);
sessionParams.SetOwnTimestampUnit(1.0/((double)samplingRate));
sessionParams.SetMaximumPacketSize(64000);
sessionParams.SetAcceptOwnPackets(true);
status = rtpSession.Create(sessionParams,&transmissionParams);
checkError(status);
// Instruct the RTP session to send data to ourselves.
status = rtpSession.AddDestination(RTPIPv4Address(ntohl(inet_addr("127.0.0.1")),portBase));
checkError(status);
// Tell the RTP component to use this RTPSession object.
returnValue = rtpComp.init(&rtpSession, 160); // 20ms at 8000Hz = 160 samples per RTP packet
checkError(returnValue, rtpComp);
// Initialize the RTP audio decoder.
returnValue = rtpDec.init(true, 0, &rtpSession);
checkError(returnValue, rtpDec);
// Register the U-law decoder for payload type 0
returnValue = rtpDec.setPacketDecoder(0,&rtpULawDec);
checkError(returnValue, rtpDec);
// Convert U-law encoded samples to linear encoded samples
returnValue = uLawDec.init();
checkError(returnValue, uLawDec);
// Transform the received audio data to floating point format.
returnValue = sampEnc2.init(MIPRAWAUDIOMESSAGE_TYPE_FLOAT);
checkError(returnValue, sampEnc2);
// We'll make sure that received audio frames are converted to the right
// sampling rate.
returnValue = sampConv2.init(samplingRate, numChannels);
checkError(returnValue, sampConv2);
// Initialize the mixer.
returnValue = mixer.init(samplingRate, numChannels, interval);
checkError(returnValue, mixer);
// Initialize the soundcard output.
returnValue = sndCardOutput.open(samplingRate, numChannels, interval);
checkError(returnValue, sndCardOutput);
#ifdef MIPCONFIG_SUPPORT_WINMM
// The WinMM output component uses signed little endian 16 bit samples.
returnValue = sampEnc3.init(MIPRAWAUDIOMESSAGE_TYPE_S16LE);
#else
#ifdef MIPCONFIG_SUPPORT_PORTAUDIO
// The PortAudio output component uses signed 16 bit samples
returnValue = sampEnc3.init(MIPRAWAUDIOMESSAGE_TYPE_S16);
#else
// The OSS component can use several encoding types. We'll ask
// the component to which format samples should be converted.
returnValue = sampEnc3.init(sndCardOutput.getRawAudioSubtype());
#endif
#endif
checkError(returnValue, sampEnc3);
// Next, we'll create the chain
returnValue = chain.setChainStart(&timer);
checkError(returnValue, chain);
returnValue = chain.addConnection(&timer, &sndToggleComponent);
checkError(returnValue, chain);
returnValue = chain.addConnection(&sndToggleComponent, &sampConv);
checkError(returnValue, chain);
returnValue = chain.addConnection(&sampConv, &sampEnc);
checkError(returnValue, chain);
returnValue = chain.addConnection(&sampEnc, &uLawEnc);
checkError(returnValue, chain);
returnValue = chain.addConnection(&uLawEnc, &rtpEnc);
checkError(returnValue, chain);
returnValue = chain.addConnection(&rtpEnc, &rtpComp);
checkError(returnValue, chain);
returnValue = chain.addConnection(&timer, &rtpCompAlias);
checkError(returnValue, chain);
returnValue = chain.addConnection(&rtpCompAlias, &rtpDec);
checkError(returnValue, chain);
// This is where the feedback chain is specified: we want
// feedback from the mixer to reach the RTP audio decoder,
// so we'll specify that over the links in between, feedback
// should be transferred.
returnValue = chain.addConnection(&rtpDec, &uLawDec, true);
checkError(returnValue, chain);
returnValue = chain.addConnection(&uLawDec, &sampEnc2, true);
checkError(returnValue, chain);
returnValue = chain.addConnection(&sampEnc2, &sampConv2, true);
checkError(returnValue, chain);
returnValue = chain.addConnection(&sampConv2, &mixer, true);
checkError(returnValue, chain);
returnValue = chain.addConnection(&mixer, &sampEnc3);
checkError(returnValue, chain);
returnValue = chain.addConnection(&sampEnc3, &sndCardOutput);
checkError(returnValue, chain);
// Start the chain
returnValue = chain.start();
checkError(returnValue, chain);
// We'll wait until enter is pressed
int num = 10;
for (int i = 0 ; i < num ; i++)
{
std::cout << "iteration " << (i+1) << "/" << num << std::endl;
std::cout << "Press enter for silence" << std::endl;
getc(stdin);
sndToggleComponent.lock();
sndToggleComponent.setEnabled(false);
sndToggleComponent.unlock();
std::cout << "Press enter for sound" << std::endl;
getc(stdin);
sndToggleComponent.lock();
sndToggleComponent.setEnabled(true);
sndToggleComponent.unlock();
}
returnValue = chain.stop();
checkError(returnValue, chain);
rtpSession.Destroy();
// We'll let the destructors of the other components take care
// of their de-initialization.
sndCardOutput.close(); // In case we're using PortAudio
#ifdef NEED_PA_INIT
MIPPAInputOutput::terminatePortAudio();
#endif // NEED_PA_INIT
#ifdef WIN32
WSACleanup();
#endif
return 0;
}
#else
#include <iostream>
int main(void)
{
std::cerr << "Not all necessary components are available to run this example." << std::endl;
return 0;
}
#endif // WIN32 || _WIN32_WCE || MIPCONFIG_SUPPORT_OSS