-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtspservice.c
executable file
·2049 lines (1825 loc) · 55.9 KB
/
rtspservice.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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <pthread.h>
#include <errno.h>
#include "rtsputils.h"
#include "rtspservice.h"
#include "rtputils.h"
#include "ringfifo.h"
struct profileid_sps_pps{
char base64profileid[10];
char base64sps[524];
char base64pps[524];
};
pthread_mutex_t mut;
#define MAX_SIZE 1024*1024*200
#define SDP_EL "\r\n"
#define RTSP_RTP_AVP "RTP/AVP"
struct profileid_sps_pps psp; //存base64编码的profileid sps pps
StServPrefs stPrefs;
extern int num_conn;
int g_s32Maxfd = 0;//最大轮询id号
int g_s32DoPlay = 0;
uint32_t s_u32StartPort=RTP_DEFAULT_PORT;
uint32_t s_uPortPool[MAX_CONNECTION];//RTP端口
extern int stop_schedule;
int g_s32Quit = 0;//退出全局变量
void RTP_port_pool_init(int port);
int UpdateSpsOrPps(unsigned char *data,int frame_type,int len);
/**************************************************************************************************
**
**
**
**************************************************************************************************/
void PrefsInit()
{
int l;
//设置服务器信息全局变量
stPrefs.port = SERVER_RTSP_PORT_DEFAULT;
gethostname(stPrefs.hostname,sizeof(stPrefs.hostname));
l=strlen(stPrefs.hostname);
if (getdomainname(stPrefs.hostname+l+1,sizeof(stPrefs.hostname)-l)!=0)
{
stPrefs.hostname[l]='.';
}
#ifdef RTSP_DEBUG
printf("\n");
printf("\thostname is: %s\n", stPrefs.hostname);
printf("\trtsp listening port is: %d\n", stPrefs.port);
printf("\tInput rtsp://hostIP:%d/test.264 to play this\n",stPrefs.port);
printf("\n");
#endif
}
/**************************************************************************************************
**
**
**
**************************************************************************************************/
//为缓冲分配空间
void RTSP_initserver(RTSP_buffer *rtsp, int fd)
{
rtsp->fd = fd;
rtsp->session_list = (RTSP_session *) calloc(1, sizeof(RTSP_session));
rtsp->session_list->session_id = -1;
}
/**************************************************************************************************
**
**
**
**************************************************************************************************/
//为RTP准备两个端口
int RTP_get_port_pair(port_pair *pair)
{
int i;
for (i=0; i<MAX_CONNECTION; ++i)
{
if (s_uPortPool[i]!=0)
{
pair->RTP=(s_uPortPool[i]-s_u32StartPort)*2+s_u32StartPort;
pair->RTCP=pair->RTP+1;
s_uPortPool[i]=0;
return ERR_NOERROR;
}
}
return ERR_GENERIC;
}
/**************************************************************************************************
**
**
**
**************************************************************************************************/
void AddClient(RTSP_buffer **ppRtspList, int fd)
{
RTSP_buffer *pRtsp=NULL,*pRtspNew=NULL;
#ifdef RTSP_DEBUG
fprintf(stderr, "%s, %d\n", __FUNCTION__, __LINE__);
#endif
//在链表头部插入第一个元素
if (*ppRtspList==NULL)
{
/*分配空间*/
if ( !(*ppRtspList=(RTSP_buffer*)calloc(1,sizeof(RTSP_buffer)) ) )
{
fprintf(stderr,"alloc memory error %s,%i\n", __FILE__, __LINE__);
return;
}
pRtsp = *ppRtspList;
}
else
{
//向链表中插入新的元素
for (pRtsp=*ppRtspList; pRtsp!=NULL; pRtsp=pRtsp->next)
{
pRtspNew=pRtsp;
}
/*在链表尾部插入*/
if (pRtspNew!=NULL)
{
if ( !(pRtspNew->next=(RTSP_buffer *)calloc(1,sizeof(RTSP_buffer)) ) )
{
fprintf(stderr, "error calloc %s,%i\n", __FILE__, __LINE__);
return;
}
pRtsp=pRtspNew->next;
pRtsp->next=NULL;
}
}
//设置最大轮询id号
if(g_s32Maxfd < fd)
{
g_s32Maxfd = fd;
}
/*初始化新添加的客户端*/
RTSP_initserver(pRtsp,fd);
fprintf(stderr,"Incoming RTSP connection accepted on socket: %d\n",pRtsp->fd);
}
/**************************************************************************************************
**
**
**
**************************************************************************************************/
/*根据缓冲区的内容,填充后边两个长度数据,检查缓冲区中消息的完整性
* return -1 on ERROR
* return RTSP_not_full (0) if a full RTSP message is NOT present in the in_buffer yet.
* return RTSP_method_rcvd (1) if a full RTSP message is present in the in_buffer and is
* ready to be handled.
* return RTSP_interlvd_rcvd (2) if a complete RTP/RTCP interleaved packet is present.
* terminate on really ugly cases.
*/
int RTSP_full_msg_rcvd(RTSP_buffer *rtsp, int *hdr_len, int *body_len)
{
int eomh; /* end of message header found */
int mb; /* message body exists */
int tc; /* terminator count */
int ws; /* white space */
unsigned int ml; /* total message length including any message body */
int bl; /* message body length */
char c; /* character */
int control;
char *p;
/*是否存在交叉存取的二进制rtp/rtcp数据包,参考RFC2326-10.12*/
if (rtsp->in_buffer[0] == '$')
{
uint16_t *intlvd_len = (uint16_t *)&rtsp->in_buffer[2]; /*跳过通道标志符*/
/*转化为主机字节序,因为长度是网络字节序*/
if ( (bl = ntohs(*intlvd_len)) <= rtsp->in_size)
{
fprintf(stderr,"Interleaved RTP or RTCP packet arrived (len: %hu).\n", bl);
if (hdr_len)
*hdr_len = 4;
if (body_len)
*body_len = bl;
return RTSP_interlvd_rcvd;
}
else
{
/*缓冲区不能完全存放数据*/
fprintf(stderr,"Non-complete Interleaved RTP or RTCP packet arrived.\n");
return RTSP_not_full;
}
}
eomh = mb = ml = bl = 0;
while (ml <= rtsp->in_size)
{
/* look for eol. */
/*计算不包含回车、换行在内的所有字符数*/
control = strcspn(&(rtsp->in_buffer[ml]), "\r\n");
if(control > 0)
ml += control;
else
return ERR_GENERIC;
/* haven't received the entire message yet. */
if (ml > rtsp->in_size)
return RTSP_not_full;
/* 处理终结符,判读是否是消息头的结束*/
tc = ws = 0;
while (!eomh && ((ml + tc + ws) < rtsp->in_size))
{
c = rtsp->in_buffer[ml + tc + ws];
/*统计回车换行*/
if (c == '\r' || c == '\n')
tc++;
else if ((tc < 3) && ((c == ' ') || (c == '\t')))
{
ws++; /*回车、换行之间的空格或者TAB,也是可以接受的 */
}
else
{
break;
}
}
/*
*一对回车、换行符仅仅被统计为一个行终结符
* 双行可以被接受,并将其认为是消息头的结束标识
* 这与RFC2068中的描述一致,参考rfc2068 19.3
*否则,对所有的HTTP/1.1兼容协议消息元素来说,
*回车、换行被认为是合法的行终结符
*/
/* must be the end of the message header */
if ((tc > 2) || ((tc == 2) && (rtsp->in_buffer[ml] == rtsp->in_buffer[ml + 1])))
eomh = 1;
ml += tc + ws;
if (eomh)
{
ml += bl; /* 加入消息体长度 */
if (ml <= rtsp->in_size)
break; /* all done finding the end of the message. */
}
if (ml >= rtsp->in_size)
return RTSP_not_full; /* 还没有完全接收消息 */
/*检查每一行的第一个记号,确定是否有消息体存在 */
if (!mb)
{
/* content length token not yet encountered. */
if (!strncmp(&(rtsp->in_buffer[ml]), HDR_CONTENTLENGTH, strlen(HDR_CONTENTLENGTH)))
{
mb = 1; /* 存在消息体. */
ml += strlen(HDR_CONTENTLENGTH);
/*跳过:和空格,找到长度字段*/
while (ml < rtsp->in_size)
{
c = rtsp->in_buffer[ml];
if ((c == ':') || (c == ' '))
ml++;
else
break;
}
//Content-Length:后面是消息体长度值
if (sscanf(&(rtsp->in_buffer[ml]), "%d", &bl) != 1)
{
fprintf(stderr,"RTSP_full_msg_rcvd(): Invalid ContentLength encountered in message.\n");
return ERR_GENERIC;
}
}
}
}
if (hdr_len)
*hdr_len = ml - bl;
if (body_len)
{
/*
* go through any trailing nulls. Some servers send null terminated strings
* following the body part of the message. It is probably not strictly
* legal when the null byte is not included in the Content-Length count.
* However, it is tolerated here.
* 减去可能存在的\0,它没有被计算在Content-Length中
*/
for (tc = rtsp->in_size - ml, p = &(rtsp->in_buffer[ml]); tc && (*p == '\0'); p++, bl++, tc--);
*body_len = bl;
}
return RTSP_method_rcvd;
}
/**************************************************************************************************
**
**
**
**************************************************************************************************/
/*
* return 0 是客户端发送的请求
* 1 是服务器返回的响应
*/
int RTSP_valid_response_msg(unsigned short *status, RTSP_buffer * rtsp)
{
char ver[32], trash[15];
unsigned int stat;
unsigned int seq;
int pcnt; /* parameter count */
/* assuming "stat" may not be zero (probably faulty) */
stat = 0;
/*从消息中填充数据*/
pcnt = sscanf(rtsp->in_buffer, " %31s %u %s %s %u\n%*255s ", ver, &stat, trash, trash, &seq);
/* 通过起始字符,检查信息是客户端发送的请求还是服务器做出的响应*/
/* C->S CMD rtsp://IP:port/suffix RTSP/1.0\r\n |head
* CSeq: 1 \r\n |
* Content_Length:** |body
* S->C RTSP/1.0 200 OK\r\n
* CSeq: 1\r\n
* Date:....
*/
if (strncmp(ver, "RTSP/", 5))
return 0; /*不是响应消息,是客户端请求消息,返回*/
/*确信至少存在版本、状态码、序列号*/
if (pcnt < 3 || stat == 0)
return 0; /* 表示不是一个响应消息 */
/*如果版本不兼容,在此处增加码来拒绝该消息*/
/*检查回复消息中的序列号是否合法*/
if (rtsp->rtsp_cseq != seq + 1)
{
fprintf(stderr,"Invalid sequence number returned in response.\n");
return ERR_GENERIC; /*序列号错误,返回*/
}
*status = stat;
return 1;
}
/**************************************************************************************************
**
**
**
**************************************************************************************************/
//返回请求方法类型,出错返回-1
int RTSP_validate_method(RTSP_buffer * pRtsp)
{
char method[32], hdr[16];
char object[256];
char ver[32];
unsigned int seq;
int pcnt; /* parameter count */
int mid = ERR_GENERIC;
char *p; //=======增加
char trash[255]; //===增加
*method = *object = '\0';
seq = 0;
printf("");
/*按照请求消息的格式解析消息的第一行*/
// if ( (pcnt = sscanf(pRtsp->in_buffer, " %31s %255s %31s\n%15s", method, object, ver, hdr, &seq)) != 5){
if ( (pcnt = sscanf(pRtsp->in_buffer, " %31s %255s %31s\n%15s", method, object, ver, hdr)) != 4){
printf("========\n%s\n==========\n",pRtsp->in_buffer);
printf("%s ",method);
printf("%s ",object);
printf("%s ",ver);
printf("hdr:%s\n",hdr);
return ERR_GENERIC;
}
/*如果没有头标记,则错误*/
/*
if ( !strstr(hdr, HDR_CSEQ) ){
printf("no HDR_CSEQ err_generic");
return ERR_GENERIC;
}
*/
//===========加
if ((p = strstr(pRtsp->in_buffer, "CSeq")) == NULL) {
return ERR_GENERIC;
}else {
if(sscanf(p,"%254s %d",trash,&seq)!=2){
return ERR_GENERIC;
}
}
//==========
/*根据不同的方法,返回响应的方法ID*/
if (strcmp(method, RTSP_METHOD_DESCRIBE) == 0) {
mid = RTSP_ID_DESCRIBE;
}
if (strcmp(method, RTSP_METHOD_ANNOUNCE) == 0) {
mid = RTSP_ID_ANNOUNCE;
}
if (strcmp(method, RTSP_METHOD_GET_PARAMETERS) == 0) {
mid = RTSP_ID_GET_PARAMETERS;
}
if (strcmp(method, RTSP_METHOD_OPTIONS) == 0) {
mid = RTSP_ID_OPTIONS;
}
if (strcmp(method, RTSP_METHOD_PAUSE) == 0) {
mid = RTSP_ID_PAUSE;
}
if (strcmp(method, RTSP_METHOD_PLAY) == 0) {
mid = RTSP_ID_PLAY;
}
if (strcmp(method, RTSP_METHOD_RECORD) == 0) {
mid = RTSP_ID_RECORD;
}
if (strcmp(method, RTSP_METHOD_REDIRECT) == 0) {
mid = RTSP_ID_REDIRECT;
}
if (strcmp(method, RTSP_METHOD_SETUP) == 0) {
mid = RTSP_ID_SETUP;
}
if (strcmp(method, RTSP_METHOD_SET_PARAMETER) == 0) {
mid = RTSP_ID_SET_PARAMETER;
}
if (strcmp(method, RTSP_METHOD_TEARDOWN) == 0) {
mid = RTSP_ID_TEARDOWN;
}
/*设置当前方法的请求序列号*/
pRtsp->rtsp_cseq = seq;
return mid;
}
/**************************************************************************************************
**
**
**
**************************************************************************************************/
//解析URL中的port端口和文件名称
int ParseUrl(const char *pUrl, char *pServer, unsigned short *port, char *pFileName, size_t FileNameLen)
{
/* expects format [rtsp://server[:port/]]filename RTSP/1.0*/
int s32NoValUrl;
/*拷贝URL */
char *pFull = (char *)malloc(strlen(pUrl) + 1);
strcpy(pFull, pUrl);
/*检查前缀是否正确*/
if (strncmp(pFull, "rtsp://", 7) == 0)
{
char *pSuffix;
//找到/ 它之后是文件名
if((pSuffix = strchr(&pFull[7], '/')) != NULL)
{
char *pPort;
char pSubPort[128];
//判断是否有端口
pPort=strchr(&pFull[7], ':');
if(pPort != NULL)
{
strncpy(pServer,&pFull[7],pPort-pFull-7);
printf("server:%s\n",pServer);
strncpy(pSubPort, pPort+1, pSuffix-pPort-1);
pSubPort[pSuffix-pPort-1] = '\0';
*port = (unsigned short) atol(pSubPort);
printf("port:%d\n",port);
}
else
{
*port = SERVER_RTSP_PORT_DEFAULT;
}
pSuffix++;
//跳过空格或者制表符
while(*pSuffix == ' '||*pSuffix == '\t')
{
pSuffix++;
}
//拷贝文件名
strcpy(pFileName, pSuffix);
s32NoValUrl = 0;
}
else
{
*port = SERVER_RTSP_PORT_DEFAULT;
*pFileName = '\0';
s32NoValUrl = 1;
}
}else
{
*pFileName = '\0';
s32NoValUrl = 1;
}
//释放空间
free(pFull);
return s32NoValUrl;
}
/**************************************************************************************************
**
**
**
**************************************************************************************************/
//把当前时间作为session号
char *GetSdpId(char *buffer)
{
time_t t;
buffer[0]='\0';
t = time(NULL);
sprintf(buffer,"%.0f",(float)t+2208988800U); /*获得NPT时间*/
return buffer;
}
/**************************************************************************************************
**
**
**
**************************************************************************************************/
void GetSdpDescr(RTSP_buffer * pRtsp, char *pDescr, char *s8Str)
{
/*/=====================================
char const* const SdpPrefixFmt =
"v=0\r\n" //版本信息
"o=- %s %s IN IP4 %s\r\n" //<用户名><会话id><版本>//<网络类型><地址类型><地址>
"c=IN IP4 %s\r\n" //c=<网络信息><地址信息><连接地址>对ip4为0.0.0.0 here!
"s=RTSP Session\r\n" //会话名session id
"i=N/A\r\n" //会话信息
"t=0 0\r\n" //<开始时间><结束时间>
"a=recvonly\r\n"
"m=video %s RTP/AVP 96\r\n\r\n"; //<媒体格式><端口><传送><格式列表,即媒体净荷类型> m=video 5004 RTP/AVP 96
struct ifreq stIfr;
char pSdpId[128];
//获取本机地址
strcpy(stIfr.ifr_name, "eth0");
if(ioctl(pRtsp->fd, SIOCGIFADDR, &stIfr) < 0)
{
//printf("Failed to get host eth0 ip\n");
strcpy(stIfr.ifr_name, "wlan0");
if(ioctl(pRtsp->fd, SIOCGIFADDR, &stIfr) < 0)
{
printf("Failed to get host eth0 or wlan0 ip\n");
}
}
sock_ntop_host(&stIfr.ifr_addr, sizeof(struct sockaddr), s8Str, 128);
GetSdpId(pSdpId);
sprintf(pDescr, SdpPrefixFmt, pSdpId, pSdpId, s8Str, inet_ntoa(((struct sockaddr_in *)(&pRtsp->stClientAddr))->sin_addr), "5006", "H264");
"b=RR:0\r\n"
//按spydroid改
"a=rtpmap:96 %s/90000\r\n" //a=rtpmap:<净荷类型><编码名>/<时钟速率> a=rtpmap:96 H264/90000
"a=fmtp:96 packetization-mode=1;profile-level-id=1EE042;sprop-parameter-sets=QuAe2gLASRA=,zjCkgA==\r\n"
"a=control:trackID=0\r\n";
#ifdef RTSP_DEBUG
// printf("SDP:\n%s\n", pDescr);
#endif
*/
struct ifreq stIfr;
char pSdpId[128];
char rtp_port[5];
strcpy(stIfr.ifr_name, "eth0");
if(ioctl(pRtsp->fd, SIOCGIFADDR, &stIfr) < 0)
{
//printf("Failed to get host eth0 ip\n");
strcpy(stIfr.ifr_name, "wlan0");
if(ioctl(pRtsp->fd, SIOCGIFADDR, &stIfr) < 0)
{
printf("Failed to get host eth0 or wlan0 ip\n");
}
}
sock_ntop_host(&stIfr.ifr_addr, sizeof(struct sockaddr), s8Str, 128);
GetSdpId(pSdpId);
strcpy(pDescr, "v=0\r\n");
strcat(pDescr, "o=-");
strcat(pDescr, pSdpId);
strcat(pDescr," ");
strcat(pDescr, pSdpId);
strcat(pDescr," IN IP4 ");
strcat(pDescr, s8Str);
strcat(pDescr, "\r\n");
strcat(pDescr, "s=Unnamed\r\n");
strcat(pDescr, "i=N/A\r\n");
strcat(pDescr, "c=");
strcat(pDescr, "IN "); /* Network type: Internet. */
strcat(pDescr, "IP4 "); /* Address type: IP4. */
//strcat(pDescr, get_address());
strcat(pDescr, inet_ntoa(((struct sockaddr_in *)(&pRtsp->stClientAddr))->sin_addr));
strcat(pDescr, "\r\n");
strcat(pDescr, "t=0 0\r\n");
strcat(pDescr, "a=recvonly\r\n");
/**** media specific ****/
strcat(pDescr,"m=");
strcat(pDescr,"video ");
sprintf(rtp_port,"%d",s_u32StartPort);
strcat(pDescr, rtp_port);
strcat(pDescr," RTP/AVP "); /* Use UDP */
strcat(pDescr,"96\r\n");
//strcat(pDescr, "\r\n");
strcat(pDescr,"b=RR:0\r\n");
/**** Dynamically defined payload ****/
strcat(pDescr,"a=rtpmap:96");
strcat(pDescr," ");
strcat(pDescr,"H264/90000");
strcat(pDescr, "\r\n");
strcat(pDescr,"a=fmtp:96 packetization-mode=1;");
strcat(pDescr,"profile-level-id=");
strcat(pDescr,psp.base64profileid);
strcat(pDescr,";sprop-parameter-sets=");
strcat(pDescr,psp.base64sps);
strcat(pDescr,",");
strcat(pDescr,psp.base64pps);
strcat(pDescr,";");
strcat(pDescr, "\r\n");
strcat(pDescr,"a=control:trackID=0");
strcat(pDescr, "\r\n");
printf("\n\n%s,%d===>psp.base64profileid=%s,psp.base64sps=%s,psp.base64pps=%s\n\n",__FUNCTION__,__LINE__,psp.base64profileid,psp.base64sps,psp.base64pps);
/*
strcat(pDescr, "m=audio ");
strcat(pDescr,"5004");
strcat(pDescr," RTP/AVP 96\r\n");
strcat(pDescr, "b=AS:128\r\n");
strcat(pDescr, "b=RR:0\r\n");
strcat(pDescr, "a=rtpmap:96 AMR/8000\r\n");
strcat(pDescr, "a=fmtp:96 octrt-align=1;\r\n");
strcat(pDescr,"a=control:trackID=1");
strcat(pDescr, "\r\n");
*/
//strcat(pDescr, "\r\n");
//printf("0\r\n");
}
/**************************************************************************************************
**
**
**
**************************************************************************************************/
/*添加时间戳*/
void add_time_stamp(char *b, int crlf)
{
struct tm *t;
time_t now;
/*
* concatenates a null terminated string with a
* time stamp in the format of "Date: 23 Jan 1997 15:35:06 GMT"
*/
now = time(NULL);
t = gmtime(&now);
//输出时间格式:Date: Fri, 15 Jul 2011 09:23:26 GMT
strftime(b + strlen(b), 38, "Date: %a, %d %b %Y %H:%M:%S GMT"RTSP_EL, t);
//是否是消息结束,添加回车换行符
if (crlf)
strcat(b, "\r\n"); /* add a message header terminator (CRLF) */
}
/**************************************************************************************************
**
**
**
**************************************************************************************************/
int SendDescribeReply(RTSP_buffer * rtsp, char *object, char *descr, char *s8Str)
{
char *pMsgBuf; /* 用于获取响应缓冲指针*/
int s32MbLen;
/* 分配空间,处理内部错误*/
s32MbLen = 2048;
pMsgBuf = (char *)malloc(s32MbLen);
if (!pMsgBuf)
{
fprintf(stderr,"send_describe_reply(): unable to allocate memory\n");
send_reply(500, 0, rtsp); /* internal server error */
if (pMsgBuf)
{
free(pMsgBuf);
}
return ERR_ALLOC;
}
/*构造describe消息串*/
sprintf(pMsgBuf, "%s %d %s"RTSP_EL"CSeq: %d"RTSP_EL"Server: %s/%s"RTSP_EL, RTSP_VER, 200, get_stat(200), rtsp->rtsp_cseq, PACKAGE, VERSION);
add_time_stamp(pMsgBuf, 0); /*添加时间戳*/
strcat(pMsgBuf, "Content-Type: application/sdp"RTSP_EL); /*实体头,表示实体类型*/
/*用于解析实体内相对url的 绝对url*/
sprintf(pMsgBuf + strlen(pMsgBuf), "Content-Base: rtsp://%s/%s/"RTSP_EL, s8Str, object);
sprintf(pMsgBuf + strlen(pMsgBuf), "Content-Length: %d"RTSP_EL, strlen(descr)); /*消息体的长度*/
strcat(pMsgBuf, RTSP_EL);
/*消息头结束*/
/*加上消息体*/
strcat(pMsgBuf, descr); /*describe消息*/
/*向缓冲区中填充数据*/
bwrite(pMsgBuf, (unsigned short) strlen(pMsgBuf), rtsp);
free(pMsgBuf);
return ERR_NOERROR;
}
/**************************************************************************************************
**
**
**
**************************************************************************************************/
//describe处理
int RTSP_describe(RTSP_buffer * pRtsp)
{
char object[255], trash[255];
char *p;
unsigned short port;
char s8Url[255];
char s8Descr[MAX_DESCR_LENGTH];
char server[128];
char s8Str[128];
/*根据收到的请求请求消息,跳过方法名,分离出URL*/
if (!sscanf(pRtsp->in_buffer, " %*s %254s ", s8Url))
{
fprintf(stderr, "Error %s,%i\n", __FILE__, __LINE__);
send_reply(400, 0, pRtsp); /* bad request */
printf("get URL error");
return ERR_NOERROR;
}
/*验证URL */
switch (ParseUrl(s8Url, server, &port, object, sizeof(object)))
{
case 1: /*请求错误*/
fprintf(stderr, "Error %s,%i\n", __FILE__, __LINE__);
send_reply(400, 0, pRtsp);
printf("url request error");
return ERR_NOERROR;
break;
case -1: /*内部错误*/
fprintf(stderr,"url error while parsing !\n");
send_reply(500, 0, pRtsp);
printf("inner error");
return ERR_NOERROR;
break;
default:
break;
}
/*取得序列号,并且必须有这个选项*/
if ((p = strstr(pRtsp->in_buffer, HDR_CSEQ)) == NULL)
{
fprintf(stderr, "Error %s,%i\n", __FILE__, __LINE__);
send_reply(400, 0, pRtsp); /* Bad Request */
printf("get serial num error");
return ERR_NOERROR;
}
else
{
if (sscanf(p, "%254s %d", trash, &(pRtsp->rtsp_cseq)) != 2)
{
fprintf(stderr, "Error %s,%i\n", __FILE__, __LINE__);
send_reply(400, 0, pRtsp); /*请求错误*/
printf("get serial num 2 error");
return ERR_NOERROR;
}
}
//获取SDP内容
GetSdpDescr(pRtsp, s8Descr, s8Str);
//发送Describe响应
//printf("----------------1\r\n");
SendDescribeReply(pRtsp, object, s8Descr, s8Str);
//printf("2\r\n");
return ERR_NOERROR;
}
/**************************************************************************************************
**
**
**
**************************************************************************************************/
//发送options处理后的响应
int send_options_reply(RTSP_buffer * pRtsp, long cseq)
{
char r[1024];
sprintf(r, "%s %d %s"RTSP_EL"CSeq: %ld"RTSP_EL, RTSP_VER, 200, get_stat(200), cseq);
strcat(r, "Public: OPTIONS,DESCRIBE,SETUP,PLAY,PAUSE,TEARDOWN"RTSP_EL);
strcat(r, RTSP_EL);
bwrite(r, (unsigned short) strlen(r), pRtsp);
#ifdef RTSP_DEBUG
// fprintf(stderr ,"SERVER SEND Option Replay: %s\n", r);
#endif
return ERR_NOERROR;
}
/**************************************************************************************************
**
**
**
**************************************************************************************************/
//options处理
int RTSP_options(RTSP_buffer * pRtsp)
{
char *p;
char trash[255];
unsigned int cseq;
char method[255], url[255], ver[255];
#ifdef RTSP_DEBUG
// trace_point();
#endif
/*序列号*/
if ((p = strstr(pRtsp->in_buffer, HDR_CSEQ)) == NULL)
{
fprintf(stderr, "Error %s,%i\n", __FILE__, __LINE__);
send_reply(400, 0, pRtsp);/* Bad Request */
printf("serial num error");
return ERR_NOERROR;
}
else
{
if (sscanf(p, "%254s %d", trash, &(pRtsp->rtsp_cseq)) != 2)
{
fprintf(stderr, "Error %s,%i\n", __FILE__, __LINE__);
send_reply(400, 0, pRtsp);/* Bad Request */
printf("serial num 2 error");
return ERR_NOERROR;
}
}
cseq = pRtsp->rtsp_cseq;
#ifdef RTSP_DEBUG
sscanf(pRtsp->in_buffer, " %31s %255s %31s ", method, url, ver);
fprintf(stderr,"%s %s %s \n",method,url,ver);
#endif
//发送option处理后的消息
send_options_reply(pRtsp, cseq);
return ERR_NOERROR;
}
/**************************************************************************************************
**
**
**
**************************************************************************************************/
int send_setup_reply(RTSP_buffer *pRtsp, RTSP_session *pSession, RTP_session *pRtpSes)
{
char s8Str[1024];
sprintf(s8Str, "%s %d %s"RTSP_EL"CSeq: %ld"RTSP_EL"Server: %s/%s"RTSP_EL, RTSP_VER,\
200, get_stat(200), (long int)pRtsp->rtsp_cseq, PACKAGE, VERSION);
add_time_stamp(s8Str, 0);
sprintf(s8Str + strlen(s8Str), "Session: %d"RTSP_EL"Transport: ", (pSession->session_id));
switch (pRtpSes->transport.type)
{
case RTP_rtp_avp:
if (pRtpSes->transport.u.udp.is_multicast)
{
// sprintf(s8Str + strlen(s8Str), "RTP/AVP;multicast;ttl=%d;destination=%s;port=", (int)DEFAULT_TTL, descr->multicast);
}
else
{
sprintf(s8Str + strlen(s8Str), "RTP/AVP;unicast;client_port=%d-%d;destination=192.168.245.65;source=%s;server_port=", \
pRtpSes->transport.u.udp.cli_ports.RTP, pRtpSes->transport.u.udp.cli_ports.RTCP,"192.168.245.96");
}
sprintf(s8Str + strlen(s8Str), "%d-%d"RTSP_EL, pRtpSes->transport.u.udp.ser_ports.RTP, pRtpSes->transport.u.udp.ser_ports.RTCP);
break;
case RTP_rtp_avp_tcp:
sprintf(s8Str + strlen(s8Str), "RTP/AVP/TCP;interleaved=%d-%d"RTSP_EL, \
pRtpSes->transport.u.tcp.interleaved.RTP, pRtpSes->transport.u.tcp.interleaved.RTCP);
break;
default:
break;
}
strcat(s8Str, RTSP_EL);
bwrite(s8Str, (unsigned short) strlen(s8Str), pRtsp);
return ERR_NOERROR;
}
/**************************************************************************************************
**
**
**
**************************************************************************************************/
int RTSP_setup(RTSP_buffer * pRtsp)
{
char s8TranStr[128], *s8Str;
char *pStr;
RTP_transport Transport;
int s32SessionID=0;
RTP_session *rtp_s, *rtp_s_prec;
RTSP_session *rtsp_s;
if ((s8Str = strstr(pRtsp->in_buffer, HDR_TRANSPORT)) == NULL)
{
fprintf(stderr, "Error %s,%i\n", __FILE__, __LINE__);
send_reply(406, 0, pRtsp); // Not Acceptable
printf("not acceptable");
return ERR_NOERROR;
}
//检查传输层子串是否正确
if (sscanf(s8Str, "%*10s %255s", s8TranStr) != 1)
{
fprintf(stderr,"SETUP request malformed: Transport string is empty\n");
send_reply(400, 0, pRtsp); // Bad Request
printf("check transport 400 bad request");
return ERR_NOERROR;
}
fprintf(stderr,"*** transport: %s ***\n", s8TranStr);
//如果需要增加一个会话
if ( !pRtsp->session_list )
{
pRtsp->session_list = (RTSP_session *) calloc(1, sizeof(RTSP_session));
}
rtsp_s = pRtsp->session_list;
//建立一个新会话,插入到链表中
if (pRtsp->session_list->rtp_session == NULL)
{
pRtsp->session_list->rtp_session = (RTP_session *) calloc(1, sizeof(RTP_session));
rtp_s = pRtsp->session_list->rtp_session;
}
else
{
for (rtp_s = rtsp_s->rtp_session; rtp_s != NULL; rtp_s = rtp_s->next)
{
rtp_s_prec = rtp_s;
}
rtp_s_prec->next = (RTP_session *) calloc(1, sizeof(RTP_session));
rtp_s = rtp_s_prec->next;
}
/*
printf("\tFile Name %s\n", Object);
if((rtp_s->file_id = open(Object, O_RDONLY)) < 0)
{
printf("Open File error %s, %d\n", __FILE__, __LINE__);
}
*/
//起始状态为暂停
rtp_s->pause = 1;
rtp_s->hndRtp = NULL;
Transport.type = RTP_no_transport;
if((pStr = strstr(s8TranStr, RTSP_RTP_AVP)))
{
//Transport: RTP/AVP
pStr += strlen(RTSP_RTP_AVP);
if ( !*pStr || (*pStr == ';') || (*pStr == ' '))
{
//单播