-
Notifications
You must be signed in to change notification settings - Fork 0
/
satip_rtsp.c
734 lines (539 loc) · 17 KB
/
satip_rtsp.c
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/*
* satip: RTSP processing
*
* Copyright (C) 2014 [email protected]
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <poll.h>
#include "satip_config.h"
#include "satip_rtsp.h"
#include "polltimer.h"
#include "log.h"
extern int enabled_workarounds;
typedef enum
{
RTSP_NOCONFIG = 0,
RTSP_CONNECTING,
RTSP_ESTABLISHING, /* try to get stream ID */
RTSP_READY, /* connected with stream ID and no request pending */
RTSP_WAITING, /* waiting for response */
} t_rtsp_state;
typedef enum
{
RTSP_REQ_OPTIONS = 0,
RTSP_REQ_SETUP,
RTSP_REQ_PLAY,
RTSP_REQ_TEARDOWN,
RTSP_REQ_NONE
} t_rtsp_request;
#define MAX_BUF 1024
#define MAX_SESSION 50
typedef struct satip_rtsp
{
t_rtsp_state status;
int sockfd;
char* host;
char* port;
int rtp_port;
struct addrinfo* addrinfo;
t_satip_config* satip_config;
struct polltimer** timer_queue;
struct polltimer* timer;
t_rtsp_request request;
int cseq;
int streamid;
char session[MAX_SESSION];
int timeout;
char txbuf[MAX_BUF];
char rxbuf[MAX_BUF];
int rxbuf_pos;
} t_satip_rtsp;
static int handle_response_options(t_satip_rtsp* rtsp);
static int handle_response_setup(t_satip_rtsp* rtsp);
static int handle_response_play(t_satip_rtsp* rtsp);
static int handle_response_teardown(t_satip_rtsp* rtsp);
int(*handle_response[])(t_satip_rtsp*)=
{
handle_response_options,
handle_response_setup,
handle_response_play,
handle_response_teardown
};
static void restart_connection(t_satip_rtsp* rtsp,int now);
static void reset_connection(t_satip_rtsp* rtsp)
{
rtsp->status = RTSP_NOCONFIG;
rtsp->cseq = 1;
rtsp->request = RTSP_REQ_NONE;
rtsp->timeout = 30;
rtsp->session[0] = 0;
rtsp->txbuf[0]=0;
rtsp->rxbuf_pos=0;
rtsp->rxbuf[0]=0;
if (rtsp->timer != NULL)
{
polltimer_cancel(rtsp->timer_queue,rtsp->timer);
rtsp->timer=NULL;
}
if (rtsp->sockfd>=0)
{
DEBUG(MSG_NET,"closing socket\n");
close(rtsp->sockfd);
rtsp->sockfd=-1;
}
}
static void timeout_reconnect(void* param)
{
t_satip_rtsp* rtsp=(t_satip_rtsp*)param;
DEBUG(MSG_NET,"timeout\n");
/* timer expired, clear it */
rtsp->timer = NULL;
restart_connection(rtsp,1);
}
t_satip_rtsp* satip_rtsp_new(t_satip_config* satip_config,
struct polltimer** timer_queue,
const char* host,
const char* port,
int rtp_port)
{
t_satip_rtsp* rtsp;
rtsp=(t_satip_rtsp*)malloc(sizeof(t_satip_rtsp));
rtsp->host=strdup(host);
rtsp->port=strdup(port);
rtsp->rtp_port=rtp_port;
rtsp->satip_config= satip_config;
rtsp->timer_queue = timer_queue;
rtsp->timer = NULL;
rtsp->sockfd = -1;
/* reset dynamic parts*/
reset_connection(rtsp);
return rtsp;
}
static void restart_connection(t_satip_rtsp* rtsp,int now)
{
reset_connection(rtsp);
if ( now )
{
satip_rtsp_check_update(rtsp);
}
else
{
/* attempt again after some time*/
rtsp->timer = polltimer_start( rtsp->timer_queue,
timeout_reconnect,
2000,(void*)rtsp);
}
}
static int read_response(t_satip_rtsp* rtsp)
{
int rec;
rec=recv(rtsp->sockfd,
&(rtsp->rxbuf[rtsp->rxbuf_pos]),
MAX_BUF-rtsp->rxbuf_pos, 0);
if ( rec==0 )
return SATIP_RTSP_ERROR;
rtsp->rxbuf_pos += rec;
rtsp->rxbuf[rtsp->rxbuf_pos]=0;
DEBUG(MSG_NET,"rxbuf:\n%s\n<<\n",rtsp->rxbuf);
if (strstr(rtsp->rxbuf,"\r\n\r\n") == NULL )
{
return SATIP_RTSP_OK;
}
else
{
/* reset buffer index for next response */
rtsp->rxbuf_pos=0;
/* check basic status code */
int ret=0;
sscanf(rtsp->rxbuf,"RTSP/%*s %d",&ret);
if (ret!=200)
return SATIP_RTSP_ERROR;
/* request specific evaluation of response */
return (*handle_response[rtsp->request])(rtsp);
}
}
static int handle_response_options(t_satip_rtsp* rtsp)
{
return SATIP_RTSP_COMPLETE;
}
static int handle_response_setup(t_satip_rtsp* rtsp)
{
char* str;
str=strstr(rtsp->rxbuf,"\ncom.ses.streamID:");
if ( str==NULL || sscanf(str,"\ncom.ses.streamID: %d",&rtsp->streamid) != 1 )
return SATIP_RTSP_ERROR;
DEBUG(MSG_NET,"streamid %d\n",rtsp->streamid);
str=strstr(rtsp->rxbuf,"\nSession:");
if ( str==NULL || sscanf(str,"\nSession: %s",rtsp->session) !=1 )
return SATIP_RTSP_ERROR;
str=strstr(rtsp->session,";timeout=");
if ( str!=NULL )
{
if ( sscanf(str,";timeout= %d",&rtsp->timeout) != 1 )
return SATIP_RTSP_ERROR;
/* terminate session at ";" */
*str=0;
DEBUG(MSG_NET,"timeout: %d\n",rtsp->timeout);
}
DEBUG(MSG_NET,"Session: %s\n",rtsp->session);
return SATIP_RTSP_COMPLETE;
}
static int handle_response_play(t_satip_rtsp* rtsp)
{
return SATIP_RTSP_COMPLETE;
}
static int handle_response_teardown(t_satip_rtsp* rtsp)
{
reset_connection(rtsp);
return SATIP_RTSP_OK;
}
static int send_options(t_satip_rtsp* rtsp)
{
int printed;
printed =
snprintf(rtsp->txbuf,MAX_BUF,"OPTIONS rtsp://%s:%s/ RTSP/1.0\r\n"
"CSeq: %d\r\n"
"%s%s%s",
rtsp->host, rtsp->port,
rtsp->cseq++,
rtsp->session[0] ? "Session: " : "",
rtsp->session ,
rtsp->session[0] ? "\r\n\r\n" : "\r\n"
);
if ( printed >= MAX_BUF )
return SATIP_RTSP_ERROR;
DEBUG(MSG_NET,">>txbuf:\n%s\n<<\n",rtsp->txbuf);
if ( send(rtsp->sockfd,rtsp->txbuf,printed,0) != printed )
return SATIP_RTSP_ERROR;
return SATIP_RTSP_OK;
}
static int send_teardown(t_satip_rtsp* rtsp)
{
int printed;
printed =
snprintf(rtsp->txbuf,MAX_BUF,"TEARDOWN rtsp://%s/stream=%d RTSP/1.0\r\n"
"CSeq: %d\r\n"
"Session: %s\r\n\r\n",
rtsp->host,
rtsp->streamid,
rtsp->cseq++,
rtsp->session);
if ( printed >= MAX_BUF )
return SATIP_RTSP_ERROR;
DEBUG(MSG_NET,">>txbuf:\n%s\n<<\n",rtsp->txbuf);
if ( send(rtsp->sockfd,rtsp->txbuf,printed,0) != printed )
return SATIP_RTSP_ERROR;
return SATIP_RTSP_OK;
}
static int send_setup(t_satip_rtsp* rtsp)
{
char* buf=rtsp->txbuf;
int printed;
int remain=MAX_BUF;
printed = snprintf(buf,remain,"SETUP rtsp://%s/?", rtsp->host);
if ( printed >= remain )
return SATIP_RTSP_ERROR;
printed += satip_prepare_tuning(rtsp->satip_config,buf+printed,remain-printed);
if ( printed >= remain )
return SATIP_RTSP_ERROR;
printed += snprintf(buf+printed,remain-printed,"&");
if ( printed >= remain )
return SATIP_RTSP_ERROR;
#if 1
printed += satip_prepare_pids(rtsp->satip_config,buf+printed,remain-printed,0);
if ( printed >= remain )
return SATIP_RTSP_ERROR;
satip_settle_config(rtsp->satip_config);
#endif
#if 1
printed += snprintf(buf+printed,remain-printed," RTSP/1.0\r\n"
"CSeq: %d\r\n"
"Transport: RTP/AVP;unicast;client_port=%d-%d\r\n\r\n",
rtsp->cseq++,rtsp->rtp_port,rtsp->rtp_port+1);
#else
printed += snprintf(buf+printed,remain-printed," RTSP/1.0\r\n"
"CSeq: %d\r\n"
"Transport: RTP/AVP;multicast;destination=224.16.16.1;port=%d-%d\r\n\r\n",
rtsp->cseq++,rtsp->rtp_port,rtsp->rtp_port+1);
#endif
if ( printed >= remain )
return SATIP_RTSP_ERROR;
DEBUG(MSG_NET,">>txbuf:\n%s\n<<\n",rtsp->txbuf);
if ( send(rtsp->sockfd,rtsp->txbuf,printed,0) != printed )
return SATIP_RTSP_ERROR ;
return SATIP_RTSP_OK;
}
static int send_play(t_satip_rtsp* rtsp)
{
char* buf=rtsp->txbuf;
int printed;
int remain=MAX_BUF;
int tuning,pid_update;
tuning = satip_tuning_required(rtsp->satip_config);
pid_update = satip_pid_update_required(rtsp->satip_config);
printed = snprintf(buf,remain,"PLAY rtsp://%s/stream=%d%s",
rtsp->host,rtsp->streamid,
(tuning || pid_update) ? "?" : "");
if ( printed>=remain )
return SATIP_RTSP_ERROR;
#if 1
if ( tuning )
{
printed += satip_prepare_tuning(rtsp->satip_config, buf+printed, remain-printed);
if ( printed>=remain )
return SATIP_RTSP_ERROR;
printed += snprintf(buf+printed,remain-printed,"&");
if ( printed >= remain )
return SATIP_RTSP_ERROR;
printed += satip_prepare_pids(rtsp->satip_config, buf+printed, remain-printed,0);
if ( printed>=remain )
return SATIP_RTSP_ERROR;
}
else if ( pid_update )
{
// Add workaround for Fritzdvbc quirk: use full pid updates
if (enabled_workarounds & 0x02)
printed += satip_prepare_pids(rtsp->satip_config, buf+printed, remain-printed,0);
else
printed += satip_prepare_pids(rtsp->satip_config, buf+printed, remain-printed,1);
if ( printed>=remain )
return SATIP_RTSP_ERROR;
}
#else
printed += snprintf(buf+printed,remain-printed,"pids=all");
if ( printed>=remain )
return SATIP_RTSP_ERROR;
#endif
satip_settle_config(rtsp->satip_config);
printed += snprintf(buf+printed,remain-printed," RTSP/1.0\r\n"
"CSeq: %d\r\n"
"%s%s%s",
rtsp->cseq++,
rtsp->session[0] ? "Session: " : "",
rtsp->session,
rtsp->session[0] ? "\r\n\r\n" : "\r\n"
);
if ( printed >= remain )
return SATIP_RTSP_ERROR;
DEBUG(MSG_NET,">>play:\n%s\n<<\n",rtsp->txbuf);
if ( send(rtsp->sockfd,rtsp->txbuf,printed,0) != printed )
return SATIP_RTSP_ERROR ;
return SATIP_RTSP_OK;
}
static int connect_server(t_satip_rtsp* rtsp )
{
int sockfd,s;
int flags;
struct addrinfo hints;
struct addrinfo *result;
struct addrinfo *rp;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* IPv4 or IPv6 */
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
hints.ai_protocol = 0;
s = getaddrinfo(rtsp->host, rtsp->port, &hints, &result);
if (s != 0)
{
ERROR(MSG_NET, "getaddrinfo: %s\n", gai_strerror(s));
return(SATIP_RTSP_ERROR);
}
for (rp = result; rp != NULL; rp = rp->ai_next)
{
sockfd = socket(rp->ai_family, rp->ai_socktype,
rp->ai_protocol);
if (sockfd == -1)
continue;
flags=fcntl(sockfd,F_GETFL,0);
fcntl(sockfd,F_SETFL,flags | O_NONBLOCK);
if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) == -1 &&
errno == EINPROGRESS )
break;
close(sockfd);
}
if (rp == NULL)
{
DEBUG(MSG_NET, "Could not connect\n");
freeaddrinfo(result);
return(SATIP_RTSP_ERROR);
}
rtsp->sockfd = sockfd;
rtsp->addrinfo = rp;
return(SATIP_RTSP_OK);
}
int satip_rtsp_socket(t_satip_rtsp* rtsp)
{
return rtsp->sockfd;
}
static void send_request(t_satip_rtsp* rtsp,
t_rtsp_state newstate,
t_rtsp_request request,
int(*sendfunc)(t_satip_rtsp*))
{
/* stop supervision timer*/
polltimer_cancel(rtsp->timer_queue, rtsp->timer);
rtsp->request = request;
rtsp->status = newstate;
/* send request and start timer */
if ( (*sendfunc)(rtsp) == SATIP_RTSP_OK )
rtsp->timer = polltimer_start( rtsp->timer_queue,
timeout_reconnect,
2000,(void*)rtsp);
else
restart_connection(rtsp,0);
}
static void timeout_keep_alive(void* param)
{
t_satip_rtsp* rtsp=(t_satip_rtsp*)param;
DEBUG(MSG_NET,"keep_alive\n");
/* timer expired, clear it */
rtsp->timer = NULL;
send_request(rtsp, RTSP_READY, RTSP_REQ_OPTIONS, send_options);
}
void satip_rtsp_pollevents(t_satip_rtsp* rtsp, short events)
{
if ( events & POLLHUP )
{
/* connection rejected (port closed) */
DEBUG(MSG_NET,"connection rejected\n");
restart_connection(rtsp,0);
return;
}
switch ( rtsp->status )
{
case RTSP_NOCONFIG:
break;
case RTSP_CONNECTING:
if ( events & POLLOUT )
{
DEBUG(MSG_NET,"connected -> establishing\n");
send_request(rtsp, RTSP_ESTABLISHING, RTSP_REQ_OPTIONS, send_options);
}
break;
case RTSP_ESTABLISHING:
if ( events & POLLIN )
{
int ret=read_response(rtsp);
if ( ret==SATIP_RTSP_ERROR )
{
DEBUG(MSG_NET,"peer closed\n");
restart_connection(rtsp,0);
}
else if ( ret==SATIP_RTSP_COMPLETE )
{
DEBUG(MSG_NET,"response complete\n");
if ( rtsp->request == RTSP_REQ_OPTIONS )
send_request(rtsp, RTSP_ESTABLISHING, RTSP_REQ_SETUP, send_setup);
else if (rtsp->request == RTSP_REQ_SETUP )
send_request(rtsp, RTSP_READY, RTSP_REQ_PLAY, send_play);
else
{
DEBUG(MSG_NET,"bug..\n");
restart_connection(rtsp,0);
}
}
/* SATIP_RTSP_OK: response not yet complete, wait for more data.. */
}
break;
case RTSP_READY:
if ( events & POLLIN )
{
int ret=read_response(rtsp);
if ( ret==SATIP_RTSP_ERROR )
{
restart_connection(rtsp,0);
}
else if ( ret==SATIP_RTSP_COMPLETE )
{
polltimer_cancel(rtsp->timer_queue,rtsp->timer);
rtsp->request=RTSP_REQ_NONE;
satip_rtsp_check_update(rtsp);
if ( rtsp->request == RTSP_REQ_NONE )
rtsp->timer = polltimer_start( rtsp->timer_queue,
timeout_keep_alive,
(rtsp->timeout-5)*1000,(void*)rtsp);
}
/* SATIP_RTSP_OK: response not yet complete, wait for more data.. */
}
break;
default:
break;
}
}
short satip_rtsp_pollflags(t_satip_rtsp* rtsp)
{
short flags=0;
switch ( rtsp->status )
{
case RTSP_NOCONFIG:
break;
case RTSP_CONNECTING:
flags = POLLHUP | POLLOUT;
break;
case RTSP_ESTABLISHING:
case RTSP_READY:
flags = POLLHUP | POLLIN;
break;
default:
break;
}
return flags;
}
void satip_rtsp_check_update(struct satip_rtsp* rtsp)
{
switch ( rtsp->status )
{
case RTSP_NOCONFIG:
if ( satip_valid_config(rtsp->satip_config) &&
rtsp->timer == NULL )
{
DEBUG(MSG_NET,"connecting...\n");
rtsp->timer = polltimer_start( rtsp->timer_queue,
timeout_reconnect,
5000,(void*)rtsp);
if ( connect_server(rtsp) == SATIP_RTSP_ERROR )
/* network down, DNS error,... */
DEBUG(MSG_NET,"connect failed, waiting...\n");
else
rtsp->status = RTSP_CONNECTING;
}
break;
case RTSP_READY:
if ( rtsp->request == RTSP_REQ_NONE )
{
if ( satip_tuning_required(rtsp->satip_config) ||
satip_pid_update_required(rtsp->satip_config))
send_request(rtsp, RTSP_READY, RTSP_REQ_PLAY, send_play);
else if ( !satip_valid_config(rtsp->satip_config) )
send_request(rtsp, RTSP_READY, RTSP_REQ_TEARDOWN, send_teardown);
}
break;
case RTSP_CONNECTING:
case RTSP_ESTABLISHING:
break;
default:
break;
}
}