-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.cc
256 lines (210 loc) · 6.79 KB
/
server.cc
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
// Copyright (C) 2008 Simon Pantzare
// See COPYING for details.
#include "server.h"
#include "liveMedia.hh"
#include "BasicUsageEnvironment.hh"
#include <pthread.h>
StreamServer::StreamServer(int port) throw(StreamServerError)
: port(port), notRunning(~0), thread(NULL)
{
TaskScheduler* scheduler = BasicTaskScheduler::createNew();
env = BasicUsageEnvironment::createNew(*scheduler);
rtsp = RTSPServer::createNew(*env, port, NULL, 45);
if (rtsp == NULL)
{
throw StreamServerError();
}
}
StreamServer::~StreamServer()
{
Medium::close(rtsp);
env->reclaim();
}
typedef MPEG1or2VideoFileServerMediaSubsession MPEGVidOnly;
typedef MPEG1or2FileServerDemux MPEG;
typedef MP3AudioFileServerMediaSubsession MP3;
#define DO_ADD_CHECKS() \
if (name == NULL) \
{ \
name = file; \
} \
ServerMediaSession* look = rtsp->lookupServerMediaSession(name); \
if (isRunning() && look != NULL) \
{ \
throw StreamServerRunError(); \
} \
else \
{ \
remove(name); \
}
void StreamServer::addMP3(char const* file, char const* name = NULL)
throw(StreamServerRunError)
{
DO_ADD_CHECKS();
unsigned char interleaveCycle[] = {0,2,1,3};
unsigned const interleaveCycleSize =
sizeof(interleaveCycle)/sizeof(unsigned char);
Interleaving* interleaving;
interleaving = new Interleaving(interleaveCycleSize, interleaveCycle);
ServerMediaSession* sms = NULL;
sms = ServerMediaSession::createNew(*env, name, name);
sms->addSubsession(MP3::createNew(*env, file, False, False,
interleaving));
rtsp->addServerMediaSession(sms);
}
void StreamServer::addMPEGVideo(char const* file, char const* name)
throw(StreamServerRunError)
{
DO_ADD_CHECKS();
ServerMediaSession *sms = NULL;
sms = ServerMediaSession::createNew(*env, name, name);
sms->addSubsession(MPEGVidOnly::createNew(*env, file, False, False));
rtsp->addServerMediaSession(sms);
}
void StreamServer::addMPEG(char const* file, char const* name)
throw(StreamServerRunError)
{
DO_ADD_CHECKS();
ServerMediaSession *sms = NULL;
sms = ServerMediaSession::createNew(*env, name, name);
MPEG* demux = MPEG::createNew(*env, file, False);
sms->addSubsession(demux->newVideoServerMediaSubsession());
sms->addSubsession(demux->newAudioServerMediaSubsession());
rtsp->addServerMediaSession(sms);
}
void StreamServer::remove(char const* name) throw(StreamServerRunError)
{
ServerMediaSession* sms = NULL;
sms = rtsp->lookupServerMediaSession(name);
if (sms != NULL)
{
if (isRunning())
{
throw StreamServerRunError();
}
else
{
rtsp->removeServerMediaSession(sms);
}
}
}
void* StreamServer::listenClose(void* instance) // Will run in its own thread.
{
StreamServer* inst = (StreamServer*)instance;
inst->env->taskScheduler().doEventLoop(&(inst->notRunning));
return NULL;
}
void StreamServer::run() throw(StreamServerRunError)
{
if (isRunning() == false)
{
notRunning = 0;
pthread_create(&thread, NULL, StreamServer::listenClose,
(void*)this);
}
else
{
throw StreamServerRunError();
}
}
void StreamServer::stop() throw(StreamServerRunError)
{
if (isRunning() == false)
{
throw StreamServerRunError();
}
else
{
notRunning = ~0;
// TODO: This join does not seem to finish if there is no
// client activity.
//pthread_join(thread, NULL);
}
}
bool StreamServer::isRunning() const
{
return ~notRunning;
}
const char* StreamServer::getURL(char const* name) const
throw(StreamServerNameError)
{
ServerMediaSession* sms = NULL;
sms = rtsp->lookupServerMediaSession(name);
if (sms == NULL)
{
throw StreamServerNameError();
}
return rtsp->rtspURL(sms);
}
///////////////////////////////////////////////////////////////////////////////
// TESTS
int main()
{
StreamServer stream;
int retval = 0;
// This is how we add files to the streaming server. Both these files
// should exist in our working directory.
stream.addMP3("m1.mp3", "test_mp3");
stream.addMP3("m2.mp3", "test_mp3_2");
stream.addMPEGVideo("test.mpg", "test_mpg");
// Just add and remove a stream. Behaviour when trying to stream
// nonexisting files is undefined.
stream.addMP3("", "badmp3");
stream.remove("badmp3");
// This should work.
stream.getURL("test_mp3");
stream.getURL("test_mpg");
stream.run();
stream.stop();
// Try to remove a stream that does not exist. Nothing should happen
// even if we are running or not.
stream.remove("bad");
stream.run();
stream.remove("bad");
// However, trying to remove a stream that exists while the server is
// running is forbidden.
try
{
stream.remove("test_mp3_2");
}
catch (StreamServerRunError) { }
stream.stop();
// Now we can remove it.
stream.remove("test_mp3_2");
// We can not add a new stream that has a name that already exists in
// our database when the server is running.
stream.run();
try
{
stream.addMP3("m2.mp3", "test_mp3");
}
catch (StreamServerRunError) { }
// If the name does not exist in the database, it is OK even if the
// server is running.
stream.addMP3("m1.mp3", "test_mp3_3");
// When the server is not running we can overwrite old stream names.
stream.stop();
stream.addMP3("m2.mp3", "test_mp3");
// A StreamServerNameError exception should be thrown when trying to
// get the URL for streams that has not been added.
try
{
stream.getURL("bad");
}
catch (StreamServerNameError) { }
// We can not start the server more than once in a row (same when
// stopping).
stream.run();
try
{
stream.run();
}
catch (StreamServerRunError) { }
stream.stop();
try
{
stream.stop();
}
catch (StreamServerRunError) { }
return 0;
}