3
3
#include < time.h>
4
4
5
5
CRtspSession::CRtspSession (WiFiClient& aClient, CStreamer* aStreamer)
6
- : LinkedListElement(aStreamer->getClientsListHead ()),
7
- m_Client(aClient),
8
- m_Streamer(aStreamer) {
6
+ : LinkedListElement(aStreamer->getClientsListHead ()), m_Client(aClient), m_Streamer(aStreamer) {
9
7
printf (" Creating RTSP session\n " );
10
8
Init ();
11
9
@@ -37,8 +35,7 @@ void CRtspSession::Init() {
37
35
m_ContentLength = 0 ;
38
36
};
39
37
40
- bool CRtspSession::ParseRtspRequest (char const * aRequest,
41
- unsigned aRequestSize) {
38
+ bool CRtspSession::ParseRtspRequest (char const * aRequest, unsigned aRequestSize) {
42
39
char CmdName[RTSP_PARAM_STRING_MAX];
43
40
static char CurRequest[RTSP_BUFFER_SIZE]; // Note: we assume single
44
41
// threaded, this large buf we
@@ -118,26 +115,20 @@ bool CRtspSession::ParseRtspRequest(char const* aRequest,
118
115
119
116
// Skip over the prefix of any "rtsp://" or "rtsp:/" URL that follows:
120
117
unsigned j = i + 1 ;
121
- while (j < CurRequestSize &&
122
- (CurRequest[j] == ' ' || CurRequest[j] == ' \t ' ))
118
+ while (j < CurRequestSize && (CurRequest[j] == ' ' || CurRequest[j] == ' \t ' ))
123
119
++j; // skip over any additional white space
124
120
for (; (int )j < (int )(CurRequestSize - 8 ); ++j) {
125
- if ((CurRequest[j] == ' r' || CurRequest[j] == ' R' ) &&
126
- (CurRequest[j + 1 ] == ' t' || CurRequest[j + 1 ] == ' T' ) &&
121
+ if ((CurRequest[j] == ' r' || CurRequest[j] == ' R' ) && (CurRequest[j + 1 ] == ' t' || CurRequest[j + 1 ] == ' T' ) &&
127
122
(CurRequest[j + 2 ] == ' s' || CurRequest[j + 2 ] == ' S' ) &&
128
- (CurRequest[j + 3 ] == ' p' || CurRequest[j + 3 ] == ' P' ) &&
129
- CurRequest[j + 4 ] == ' : ' && CurRequest[j + 5 ] == ' /' ) {
123
+ (CurRequest[j + 3 ] == ' p' || CurRequest[j + 3 ] == ' P' ) && CurRequest[j + 4 ] == ' : ' &&
124
+ CurRequest[j + 5 ] == ' /' ) {
130
125
j += 6 ;
131
126
if (CurRequest[j] == ' /' ) { // This is a "rtsp://" URL; skip over
132
127
// the host:port part that follows:
133
128
++j;
134
129
unsigned uidx = 0 ;
135
- while (
136
- j < CurRequestSize && CurRequest[j] != ' /' &&
137
- CurRequest[j] != ' ' &&
138
- uidx <
139
- sizeof (m_URLHostPort) -
140
- 1 ) { // extract the host:port part of the URL here
130
+ while (j < CurRequestSize && CurRequest[j] != ' /' && CurRequest[j] != ' ' &&
131
+ uidx < sizeof (m_URLHostPort) - 1 ) { // extract the host:port part of the URL here
141
132
m_URLHostPort[uidx] = CurRequest[j];
142
133
uidx++;
143
134
++j;
@@ -152,8 +143,7 @@ bool CRtspSession::ParseRtspRequest(char const* aRequest,
152
143
// Look for the URL suffix (before the following "RTSP/"):
153
144
parseSucceeded = false ;
154
145
for (unsigned k = i + 1 ; (int )k < (int )(CurRequestSize - 5 ); ++k) {
155
- if (CurRequest[k] == ' R' && CurRequest[k + 1 ] == ' T' &&
156
- CurRequest[k + 2 ] == ' S' && CurRequest[k + 3 ] == ' P' &&
146
+ if (CurRequest[k] == ' R' && CurRequest[k + 1 ] == ' T' && CurRequest[k + 2 ] == ' S' && CurRequest[k + 3 ] == ' P' &&
157
147
CurRequest[k + 4 ] == ' /' ) {
158
148
while (--k >= i && CurRequest[k] == ' ' ) {
159
149
}
@@ -181,16 +171,12 @@ bool CRtspSession::ParseRtspRequest(char const* aRequest,
181
171
// or \n as 'CSeq':
182
172
parseSucceeded = false ;
183
173
for (j = i; (int )j < (int )(CurRequestSize - 5 ); ++j) {
184
- if (CurRequest[j] == ' C' && CurRequest[j + 1 ] == ' S' &&
185
- CurRequest[j + 2 ] == ' e' && CurRequest[j + 3 ] == ' q' &&
174
+ if (CurRequest[j] == ' C' && CurRequest[j + 1 ] == ' S' && CurRequest[j + 2 ] == ' e' && CurRequest[j + 3 ] == ' q' &&
186
175
CurRequest[j + 4 ] == ' :' ) {
187
176
j += 5 ;
188
- while (j < CurRequestSize &&
189
- (CurRequest[j] == ' ' || CurRequest[j] == ' \t ' ))
190
- ++j;
177
+ while (j < CurRequestSize && (CurRequest[j] == ' ' || CurRequest[j] == ' \t ' )) ++j;
191
178
unsigned n;
192
- for (n = 0 ; n < sizeof (m_CSeq) - 1 && j < CurRequestSize;
193
- ++n, ++j) {
179
+ for (n = 0 ; n < sizeof (m_CSeq) - 1 && j < CurRequestSize; ++n, ++j) {
194
180
char c = CurRequest[j];
195
181
if (c == ' \r ' || c == ' \n ' ) {
196
182
parseSucceeded = true ;
@@ -206,27 +192,21 @@ bool CRtspSession::ParseRtspRequest(char const* aRequest,
206
192
207
193
// Also: Look for "Content-Length:" (optional)
208
194
for (j = i; (int )j < (int )(CurRequestSize - 15 ); ++j) {
209
- if (CurRequest[j] == ' C' && CurRequest[j + 1 ] == ' o' &&
210
- CurRequest[j + 2 ] == ' n' && CurRequest[j + 3 ] == ' t' &&
211
- CurRequest[j + 4 ] == ' e' && CurRequest[j + 5 ] == ' n' &&
212
- CurRequest[j + 6 ] == ' t' && CurRequest[j + 7 ] == ' -' &&
213
- (CurRequest[j + 8 ] == ' L' || CurRequest[j + 8 ] == ' l' ) &&
214
- CurRequest[j + 9 ] == ' e' && CurRequest[j + 10 ] == ' n' &&
215
- CurRequest[j + 11 ] == ' g' && CurRequest[j + 12 ] == ' t' &&
216
- CurRequest[j + 13 ] == ' h' && CurRequest[j + 14 ] == ' :' ) {
195
+ if (CurRequest[j] == ' C' && CurRequest[j + 1 ] == ' o' && CurRequest[j + 2 ] == ' n' && CurRequest[j + 3 ] == ' t' &&
196
+ CurRequest[j + 4 ] == ' e' && CurRequest[j + 5 ] == ' n' && CurRequest[j + 6 ] == ' t' &&
197
+ CurRequest[j + 7 ] == ' -' && (CurRequest[j + 8 ] == ' L' || CurRequest[j + 8 ] == ' l' ) &&
198
+ CurRequest[j + 9 ] == ' e' && CurRequest[j + 10 ] == ' n' && CurRequest[j + 11 ] == ' g' &&
199
+ CurRequest[j + 12 ] == ' t' && CurRequest[j + 13 ] == ' h' && CurRequest[j + 14 ] == ' :' ) {
217
200
j += 15 ;
218
- while (j < CurRequestSize &&
219
- (CurRequest[j] == ' ' || CurRequest[j] == ' \t ' ))
220
- ++j;
201
+ while (j < CurRequestSize && (CurRequest[j] == ' ' || CurRequest[j] == ' \t ' )) ++j;
221
202
unsigned num;
222
203
if (sscanf (&CurRequest[j], " %u" , &num) == 1 ) m_ContentLength = num;
223
204
}
224
205
}
225
206
return true ;
226
207
};
227
208
228
- RTSP_CMD_TYPES CRtspSession::Handle_RtspRequest (char const * aRequest,
229
- unsigned aRequestSize) {
209
+ RTSP_CMD_TYPES CRtspSession::Handle_RtspRequest (char const * aRequest, unsigned aRequestSize) {
230
210
if (ParseRtspRequest (aRequest, aRequestSize)) {
231
211
switch (m_RtspCmdType) {
232
212
case RTSP_OPTIONS: {
@@ -272,15 +252,12 @@ void CRtspSession::Handle_RtspDESCRIBE() {
272
252
273
253
// check whether we know a stream with the URL which is requested
274
254
m_StreamID = -1 ; // invalid URL
275
- if ((strcmp (m_URLPreSuffix, " mjpeg" ) == 0 ) &&
276
- (strcmp (m_URLSuffix, " 1" ) == 0 ))
255
+ if ((strcmp (m_URLPreSuffix, " mjpeg" ) == 0 ) && (strcmp (m_URLSuffix, " 1" ) == 0 ))
277
256
m_StreamID = 0 ;
278
- else if ((strcmp (m_URLPreSuffix, " mjpeg" ) == 0 ) &&
279
- (strcmp (m_URLSuffix, " 2" ) == 0 ))
257
+ else if ((strcmp (m_URLPreSuffix, " mjpeg" ) == 0 ) && (strcmp (m_URLSuffix, " 2" ) == 0 ))
280
258
m_StreamID = 1 ;
281
259
if (m_StreamID == -1 ) { // Stream not available
282
- snprintf (Response, sizeof (Response),
283
- " RTSP/1.0 404 Stream Not Found\r\n CSeq: %s\r\n %s\r\n " , m_CSeq,
260
+ snprintf (Response, sizeof (Response), " RTSP/1.0 404 Stream Not Found\r\n CSeq: %s\r\n %s\r\n " , m_CSeq,
284
261
DateHeader ());
285
262
286
263
socketsend (m_RtspClient, Response, strlen (Response));
@@ -294,16 +271,15 @@ void CRtspSession::Handle_RtspDESCRIBE() {
294
271
ColonPtr = strstr (OBuf, " :" );
295
272
if (ColonPtr != nullptr ) ColonPtr[0 ] = 0x00 ;
296
273
297
- snprintf (
298
- SDPBuf, sizeof (SDPBuf),
299
- " v=0\r\n "
300
- " o=- %d 1 IN IP4 %s\r\n "
301
- " s=\r\n "
302
- " t=0 0\r\n " // start / stop - 0 -> unbounded and permanent session
303
- " m=video 0 RTP/AVP 26\r\n " // currently we just handle UDP sessions
304
- // "a=x-dimensions: 640,480\r\n"
305
- " c=IN IP4 0.0.0.0\r\n " ,
306
- rand (), OBuf);
274
+ snprintf (SDPBuf, sizeof (SDPBuf),
275
+ " v=0\r\n "
276
+ " o=- %d 1 IN IP4 %s\r\n "
277
+ " s=\r\n "
278
+ " t=0 0\r\n " // start / stop - 0 -> unbounded and permanent session
279
+ " m=video 0 RTP/AVP 26\r\n " // currently we just handle UDP sessions
280
+ // "a=x-dimensions: 640,480\r\n"
281
+ " c=IN IP4 0.0.0.0\r\n " ,
282
+ rand (), OBuf);
307
283
char StreamName[64 ];
308
284
switch (m_StreamID) {
309
285
case 0 :
@@ -345,16 +321,13 @@ void CRtspSession::Handle_RtspSETUP() {
345
321
346
322
// simulate SETUP server response
347
323
if (m_TcpTransport)
348
- snprintf (Transport, sizeof (Transport),
349
- " RTP/AVP/TCP;unicast;interleaved=0-1" );
324
+ snprintf (Transport, sizeof (Transport), " RTP/AVP/TCP;unicast;interleaved=0-1" );
350
325
else
351
326
snprintf (Transport, sizeof (Transport),
352
327
" RTP/"
353
328
" AVP;unicast;destination=127.0.0.1;source=127.0.0.1;client_"
354
329
" port=%i-%i;server_port=%i-%i" ,
355
- m_ClientRTPPort, m_ClientRTCPPort,
356
- m_Streamer->GetRtpServerPort (),
357
- m_Streamer->GetRtcpServerPort ());
330
+ m_ClientRTPPort, m_ClientRTCPPort, m_Streamer->GetRtpServerPort (), m_Streamer->GetRtcpServerPort ());
358
331
snprintf (Response, sizeof (Response),
359
332
" RTSP/1.0 200 OK\r\n CSeq: %s\r\n "
360
333
" %s\r\n "
@@ -397,17 +370,16 @@ int CRtspSession::GetStreamID() {
397
370
bool CRtspSession::handleRequests (uint32_t readTimeoutMs) {
398
371
if (m_stopped) return false ; // Already closed down
399
372
400
- static char
401
- RecvBuf[RTSP_BUFFER_SIZE]; // Note: we assume single threaded, this
402
- // large buf we keep off of the tiny stack
373
+ static char RecvBuf[RTSP_BUFFER_SIZE]; // Note: we assume single threaded, this
374
+ // large buf we keep off of the tiny stack
403
375
404
376
memset (RecvBuf, 0x00 , sizeof (RecvBuf));
405
377
int res = socketread (m_RtspClient, RecvBuf, sizeof (RecvBuf), readTimeoutMs);
406
378
if (res > 0 ) {
407
379
// we filter away everything which seems not to be an RTSP command:
408
380
// O-ption, D-escribe, S-etup, P-lay, T-eardown
409
- if ((RecvBuf[0 ] == ' O' ) || (RecvBuf[0 ] == ' D' ) || (RecvBuf[0 ] == ' S' ) ||
410
- (RecvBuf[0 ] == ' P ' ) || (RecvBuf[ 0 ] == ' T' )) {
381
+ if ((RecvBuf[0 ] == ' O' ) || (RecvBuf[0 ] == ' D' ) || (RecvBuf[0 ] == ' S' ) || (RecvBuf[ 0 ] == ' P ' ) ||
382
+ (RecvBuf[0 ] == ' T' )) {
411
383
RTSP_CMD_TYPES C = Handle_RtspRequest (RecvBuf, res);
412
384
if (C == RTSP_PLAY)
413
385
m_streaming = true ;
0 commit comments