forked from rgaufman/live555
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestWAVAudioStreamer.cpp
239 lines (209 loc) · 10.1 KB
/
testWAVAudioStreamer.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
/**********
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
This library 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 Lesser General Public License for
more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**********/
// Copyright (c) 1996-2019, Live Networks, Inc. All rights reserved
// A test program that streams a WAV audio file via RTP/RTCP
// main program
#include "liveMedia.hh"
#include "GroupsockHelper.hh"
#include "BasicUsageEnvironment.hh"
// To convert 16-bit samples to 8-bit u-law ("u" is the Greek letter "mu")
// encoding, before streaming, uncomment the following line:
//#define CONVERT_TO_ULAW 1
UsageEnvironment* env;
void play(); // forward
int main(int argc, char** argv) {
// Begin by setting up our usage environment:
TaskScheduler* scheduler = BasicTaskScheduler::createNew();
env = BasicUsageEnvironment::createNew(*scheduler);
play();
env->taskScheduler().doEventLoop(); // does not return
return 0; // only to prevent compiler warnings
}
char const* inputFileName = "test.wav";
void afterPlaying(void* clientData); // forward
// A structure to hold the state of the current session.
// It is used in the "afterPlaying()" function to clean up the session.
struct sessionState_t {
FramedSource* source;
RTPSink* sink;
RTCPInstance* rtcpInstance;
Groupsock* rtpGroupsock;
Groupsock* rtcpGroupsock;
RTSPServer* rtspServer;
} sessionState;
void play() {
// Open the file as a 'WAV' file:
WAVAudioFileSource* wavSource = WAVAudioFileSource::createNew(*env, inputFileName);
if (wavSource == NULL) {
*env << "Unable to open file \"" << inputFileName
<< "\" as a WAV audio file source: "
<< env->getResultMsg() << "\n";
exit(1);
}
// Get attributes of the audio source:
unsigned char audioFormat = wavSource->getAudioFormat();
unsigned char const bitsPerSample = wavSource->bitsPerSample();
// We handle only 4,8,16,20,24 bits-per-sample audio:
if (bitsPerSample%4 != 0 || bitsPerSample < 4 || bitsPerSample > 24 || bitsPerSample == 12) {
*env << "The input file contains " << bitsPerSample << " bit-per-sample audio, which we don't handle\n";
exit(1);
}
unsigned const samplingFrequency = wavSource->samplingFrequency();
unsigned char const numChannels = wavSource->numChannels();
unsigned bitsPerSecond = samplingFrequency*bitsPerSample*numChannels;
*env << "Audio source parameters:\n\t" << samplingFrequency << " Hz, ";
*env << bitsPerSample << " bits-per-sample, ";
*env << numChannels << " channels => ";
*env << bitsPerSecond << " bits-per-second\n";
char const* mimeType;
unsigned char payloadFormatCode = 96; // by default, unless a static RTP payload type can be used
// Add in any filter necessary to transform the data prior to streaming.
// (This is where any audio compression would get added.)
sessionState.source = wavSource; // by default
if (audioFormat == WA_PCM) {
if (bitsPerSample == 16) {
// Note that samples in the WAV audio file are in little-endian order.
#ifdef CONVERT_TO_ULAW
// Add a filter that converts from raw 16-bit PCM audio (in little-endian order) to 8-bit u-law audio:
sessionState.source = uLawFromPCMAudioSource::createNew(*env, wavSource, 1/*little-endian*/);
if (sessionState.source == NULL) {
*env << "Unable to create a u-law filter from the PCM audio source: " << env->getResultMsg() << "\n";
exit(1);
}
bitsPerSecond /= 2;
*env << "Converting to 8-bit u-law audio for streaming => " << bitsPerSecond << " bits-per-second\n";
mimeType = "PCMU";
if (samplingFrequency == 8000 && numChannels == 1) {
payloadFormatCode = 0; // a static RTP payload type
}
#else
// Add a filter that converts from little-endian to network (big-endian) order:
sessionState.source = EndianSwap16::createNew(*env, wavSource);
if (sessionState.source == NULL) {
*env << "Unable to create a little->bit-endian order filter from the PCM audio source: " << env->getResultMsg() << "\n";
exit(1);
}
*env << "Converting to network byte order for streaming\n";
mimeType = "L16";
if (samplingFrequency == 44100 && numChannels == 2) {
payloadFormatCode = 10; // a static RTP payload type
} else if (samplingFrequency == 44100 && numChannels == 1) {
payloadFormatCode = 11; // a static RTP payload type
}
#endif
} else if (bitsPerSample == 20 || bitsPerSample == 24) {
// Add a filter that converts from little-endian to network (big-endian) order:
sessionState.source = EndianSwap24::createNew(*env, wavSource);
if (sessionState.source == NULL) {
*env << "Unable to create a little->bit-endian order filter from the PCM audio source: " << env->getResultMsg() << "\n";
exit(1);
}
*env << "Converting to network byte order for streaming\n";
mimeType = bitsPerSample == 20 ? "L20" : "L24";
} else { // bitsPerSample == 8 (we assume that bitsPerSample == 4 is only for WA_IMA_ADPCM)
// Don't do any transformation; send the 8-bit PCM data 'as is':
mimeType = "L8";
}
} else if (audioFormat == WA_PCMU) {
mimeType = "PCMU";
if (samplingFrequency == 8000 && numChannels == 1) {
payloadFormatCode = 0; // a static RTP payload type
}
} else if (audioFormat == WA_PCMA) {
mimeType = "PCMA";
if (samplingFrequency == 8000 && numChannels == 1) {
payloadFormatCode = 8; // a static RTP payload type
}
} else if (audioFormat == WA_IMA_ADPCM) {
mimeType = "DVI4";
// Use a static payload type, if one is defined:
if (numChannels == 1) {
if (samplingFrequency == 8000) {
payloadFormatCode = 5; // a static RTP payload type
} else if (samplingFrequency == 16000) {
payloadFormatCode = 6; // a static RTP payload type
} else if (samplingFrequency == 11025) {
payloadFormatCode = 16; // a static RTP payload type
} else if (samplingFrequency == 22050) {
payloadFormatCode = 17; // a static RTP payload type
}
}
} else { //unknown format
*env << "Unknown audio format code \"" << audioFormat << "\" in WAV file header\n";
exit(1);
}
// Create 'groupsocks' for RTP and RTCP:
struct in_addr destinationAddress;
destinationAddress.s_addr = chooseRandomIPv4SSMAddress(*env);
// Note: This is a multicast address. If you wish instead to stream
// using unicast, then you should use the "testOnDemandRTSPServer" demo application,
// or the "LIVE555 Media Server" - not this application - as a model.
const unsigned short rtpPortNum = 2222;
const unsigned short rtcpPortNum = rtpPortNum+1;
const unsigned char ttl = 255;
const Port rtpPort(rtpPortNum);
const Port rtcpPort(rtcpPortNum);
sessionState.rtpGroupsock
= new Groupsock(*env, destinationAddress, rtpPort, ttl);
sessionState.rtpGroupsock->multicastSendOnly(); // we're a SSM source
sessionState.rtcpGroupsock
= new Groupsock(*env, destinationAddress, rtcpPort, ttl);
sessionState.rtcpGroupsock->multicastSendOnly(); // we're a SSM source
// Create an appropriate audio RTP sink (using "SimpleRTPSink") from the RTP 'groupsock':
sessionState.sink
= SimpleRTPSink::createNew(*env, sessionState.rtpGroupsock,
payloadFormatCode, samplingFrequency,
"audio", mimeType, numChannels);
// Create (and start) a 'RTCP instance' for this RTP sink:
const unsigned estimatedSessionBandwidth = (bitsPerSecond + 500)/1000; // in kbps; for RTCP b/w share
const unsigned maxCNAMElen = 100;
unsigned char CNAME[maxCNAMElen+1];
gethostname((char*)CNAME, maxCNAMElen);
CNAME[maxCNAMElen] = '\0'; // just in case
sessionState.rtcpInstance
= RTCPInstance::createNew(*env, sessionState.rtcpGroupsock,
estimatedSessionBandwidth, CNAME,
sessionState.sink, NULL /* we're a server */,
True /* we're a SSM source*/);
// Note: This starts RTCP running automatically
// Create and start a RTSP server to serve this stream:
sessionState.rtspServer = RTSPServer::createNew(*env, 8554);
if (sessionState.rtspServer == NULL) {
*env << "Failed to create RTSP server: " << env->getResultMsg() << "\n";
exit(1);
}
ServerMediaSession* sms
= ServerMediaSession::createNew(*env, "testStream", inputFileName,
"Session streamed by \"testWAVAudiotreamer\"", True/*SSM*/);
sms->addSubsession(PassiveServerMediaSubsession::createNew(*sessionState.sink, sessionState.rtcpInstance));
sessionState.rtspServer->addServerMediaSession(sms);
char* url = sessionState.rtspServer->rtspURL(sms);
*env << "Play this stream using the URL \"" << url << "\"\n";
delete[] url;
// Finally, start the streaming:
*env << "Beginning streaming...\n";
sessionState.sink->startPlaying(*sessionState.source, afterPlaying, NULL);
}
void afterPlaying(void* /*clientData*/) {
*env << "...done streaming\n";
// End by closing the media:
Medium::close(sessionState.rtspServer);
Medium::close(sessionState.rtcpInstance);
Medium::close(sessionState.sink);
delete sessionState.rtpGroupsock;
Medium::close(sessionState.source);
delete sessionState.rtcpGroupsock;
// We're done:
exit(0);
}