-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLive555Client.cpp
More file actions
1306 lines (1098 loc) · 39.7 KB
/
Copy pathLive555Client.cpp
File metadata and controls
1306 lines (1098 loc) · 39.7 KB
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 "live555client.h"
#include <UsageEnvironment.hh>
#include <BasicUsageEnvironment.hh>
#include <GroupsockHelper.hh>
#include <liveMedia.hh>
#include <liveMedia_version.hh>
#include <Base64.hh>
#include <RTSPCommon.hh>
#include <assert.h>
#if defined(_WIN32) || defined(WIN32)
#pragma warning(disable:4996)
#endif
using namespace std;
#define DEFAULT_WAIT_TIME (800) //默认超时时间,毫秒
#define HTTP_OK (0)
//这个错误号表明虽然live555没返回http错误。但我们处理RTSP过程中碰到了错误
#define HTTP_ERR_USR (99)
#define HTTP_TIMEOUT (180)
#define HTTP_ERR_EOF (502)
#define HTTP_AUTH_ERR (401)
#define HTTP_STREAM_NOT_FOUND (404)
#define HTTP_SESSION_NOT_FOUND (454)
#define HTTP_UNSUPPORTED_TRANSPOR (461)
/* All timestamp below or equal to this define are invalid/unset
* XXX the numerical value is 0 because of historical reason and will change.*/
#define VLC_TS_INVALID INT64_C(0)
#define VLC_TS_0 INT64_C(1)
#define CLOCK_FREQ INT64_C(1000000)
int HttpErrToRtspErr(int http);
unsigned char* parseH264ConfigStr(char const* configStr,
unsigned int& configSize);
uint8_t * parseVorbisConfigStr(char const* configStr,
unsigned int& configSize);
static /* Base64 decoding */
size_t vlc_b64_decode_binary_to_buffer(uint8_t *p_dst, size_t i_dst, const char *p_src)
{
static const int b64[256] = {
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
};
uint8_t *p_start = p_dst;
uint8_t *p = (uint8_t *)p_src;
int i_level;
int i_last;
for (i_level = 0, i_last = 0; (size_t)(p_dst - p_start) < i_dst && *p != '\0'; p++)
{
const int c = b64[(unsigned int)*p];
if (c == -1)
break;
switch (i_level)
{
case 0:
i_level++;
break;
case 1:
*p_dst++ = (i_last << 2) | ((c >> 4) & 0x03);
i_level++;
break;
case 2:
*p_dst++ = ((i_last << 4) & 0xf0) | ((c >> 2) & 0x0f);
i_level++;
break;
case 3:
*p_dst++ = ((i_last & 0x03) << 6) | c;
i_level = 0;
}
i_last = c;
}
return p_dst - p_start;
}
static inline Boolean toBool(bool b) { return b ? True : False; } // silly, no?
class MyRTSPClient : public RTSPClient
{
protected:
Live555Client* parent;
Boolean fSupportsGetParameter;
string destination;
int destPort = 0;
PresentationTimeSessionNormalizer* PtsSessionNormalizer = nullptr;
public:
MyRTSPClient( UsageEnvironment& env, char const* rtspURL, int verbosityLevel,
char const* applicationName, portNumBits tunnelOverHTTPPortNum,
Live555Client *p_sys)
: RTSPClient( env, rtspURL, verbosityLevel, applicationName,
tunnelOverHTTPPortNum
#if LIVEMEDIA_LIBRARY_VERSION_INT >= 1373932800
, -1
#endif
)
, parent (p_sys)
, fSupportsGetParameter(False)
, PtsSessionNormalizer(new PresentationTimeSessionNormalizer(env))
{
}
~MyRTSPClient()
{
if (PtsSessionNormalizer) {
Medium::close(PtsSessionNormalizer);
}
}
virtual Boolean setRequestFields(RequestRecord* request,
char*& cmdURL, Boolean& cmdURLWasAllocated,
char const*& protocolStr,
char*& extraHeaders, Boolean& extraHeadersWereAllocated);
void setDestination(string Addr, int DstPort);
PresentationTimeSubsessionNormalizer*
createNewPresentationTimeSubsessionNormalizer(FramedSource* inputSource,
RTPSource* rtpSource,
char const* codecName);
static void continueAfterDESCRIBE(RTSPClient* client, int result_code, char* result_string);
static void continueAfterOPTIONS(RTSPClient* client, int result_code, char* result_string);
static void default_live555_callback(RTSPClient* client, int result_code, char* result_string);
void setSupportsGetParameter(Boolean val) { fSupportsGetParameter = val; }
Boolean isSupportsGetParameter() { return fSupportsGetParameter; }
};
void MyRTSPClient::setDestination(string Addr, int DstPort)
{
destination = Addr;
destPort = DstPort;
}
PresentationTimeSubsessionNormalizer*
MyRTSPClient::createNewPresentationTimeSubsessionNormalizer(FramedSource* inputSource,
RTPSource* rtpSource,
char const* codecName)
{
if (PtsSessionNormalizer) {
return PtsSessionNormalizer->createNewPresentationTimeSubsessionNormalizer(inputSource, rtpSource, codecName);
}
return nullptr;
}
Boolean MyRTSPClient::setRequestFields(RTSPClient::RequestRecord* request,
char*& cmdURL, Boolean& cmdURLWasAllocated,
char const*& protocolStr,
char*& extraHeaders, Boolean& extraHeadersWereAllocated) {
if ((destination.size() > 0) && (strcmp(request->commandName(), "SETUP") == 0)) { //只覆盖SETUP消息,其它消息仍然使用RTSPClient::setRequestFields()
extraHeaders = new char[256];
extraHeadersWereAllocated = True;
Boolean streamUsingTCP = (request->booleanFlags() & 0x1) != 0;
if (streamUsingTCP) {
if (strcmp(request->subsession()->protocolName(), "UDP") == 0)
sprintf(extraHeaders, "Transport: RAW/RAW/UDP;unicast;interleaved=0-1;destination=%s;client_port=%d-%d\r\n",
destination.c_str(), destPort, destPort + 1);
else
sprintf(extraHeaders, "Transport: RTP/AVP;unicast;interleaved=0-1;destination=%s;client_port=%d-%d\r\n",
destination.c_str(), destPort, destPort + 1);
}
else {
sprintf(extraHeaders, "Transport: RTP/TCP;unicast;interleaved=0-1;destination=%s;client_port=%d-%d\r\n",
destination.c_str(), destPort, destPort + 1);
}
return True;
}
return RTSPClient::setRequestFields(request, cmdURL, cmdURLWasAllocated, protocolStr, extraHeaders, extraHeadersWereAllocated);
}
void MyRTSPClient::continueAfterDESCRIBE(RTSPClient* client, int result_code, char* result_string)
{
MyRTSPClient* pThis = static_cast<MyRTSPClient*>(client);
pThis->parent->continueAfterDESCRIBE(result_code, result_string);
delete[] result_string;
}
void MyRTSPClient::continueAfterOPTIONS(RTSPClient* client, int result_code, char* result_string)
{
MyRTSPClient* pThis = static_cast<MyRTSPClient*>(client);
Boolean serverSupportsGetParameter = RTSPOptionIsSupported("GET_PARAMETER", result_string);
pThis->setSupportsGetParameter(serverSupportsGetParameter);
pThis->parent->continueAfterOPTIONS(result_code, result_string);
delete[] result_string;
}
void MyRTSPClient::default_live555_callback(RTSPClient* client, int result_code, char* result_string)
{
MyRTSPClient* pThis = static_cast<MyRTSPClient*>(client);
delete[]result_string;
pThis->parent->live555Callback(result_code);
//这里好好处理一下转换成我们的错误值
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
Live555Client::LiveTrack::LiveTrack(Live555Client* p_sys, void* sub, int buffer_size)
: parent(p_sys)
, media_sub_session(sub)
, i_buffer(buffer_size)
{
b_quicktime = false;
b_asf = false;
b_muxed = false;
b_discard_trunc = false;
waiting = 0;
b_rtcp_sync = false;
i_pts = VLC_TS_INVALID;
f_npt = 0.;
}
Live555Client::LiveTrack::~LiveTrack()
{
if (p_buffer) {
delete[] p_buffer;
p_buffer = NULL;
}
}
int Live555Client::LiveTrack::init()
{
MediaSubsession* sub = static_cast<MediaSubsession*>(media_sub_session);
p_buffer = new uint8_t [i_buffer + 4];
if (!p_buffer)
return RTSP_ERR;
fmt.type = sub->mediumName();
fmt.codec = sub->codecName();
if (fmt.type == "audio") {
fmt.audio.i_channels = sub->numChannels();
fmt.audio.i_rate = sub->rtpTimestampFrequency();
if ((fmt.codec == "MPA") ||
(fmt.codec == "MPA-ROBUST") ||
(fmt.codec == "X-MP3-DRAFT-00") ||
(fmt.codec == "AC3")) {
fmt.audio.i_rate = 0;
}
else if (fmt.codec == "L16"){
fmt.audio.i_bitspersample = 16;
}
else if (fmt.codec == "L20"){
fmt.audio.i_bitspersample = 20;
}
else if (fmt.codec == "L24"){
fmt.audio.i_bitspersample = 24;
}
else if (fmt.codec == "L8"){
fmt.audio.i_bitspersample = 8;
}
else if (fmt.codec == "DAT12"){
fmt.audio.i_bitspersample = 12;
}
else if (fmt.codec == "PCMU"){
fmt.audio.i_bitspersample = 8;
}
else if (fmt.codec == "PCMA"){
fmt.audio.i_bitspersample = 8;
}
else if (fmt.codec == "G726") {
fmt.audio.i_rate = 8000;
fmt.audio.i_channels = 1;
if (!strcmp(sub->codecName() + 5, "40"))
fmt.i_bitrate = 40000;
else if (!strcmp(sub->codecName() + 5, "32"))
fmt.i_bitrate = 32000;
else if (!strcmp(sub->codecName() + 5, "24"))
fmt.i_bitrate = 24000;
else if (!strcmp(sub->codecName() + 5, "16"))
fmt.i_bitrate = 16000;
}
else if (fmt.codec == "MP4A-LATM") {
unsigned int i_extra;
uint8_t *p_extra;
if ((p_extra = parseStreamMuxConfigStr(sub->fmtp_config(),
i_extra)))
{
fmt.extra = string((char*)p_extra, i_extra);
delete[] p_extra;
}
/* Because the "faad" decoder does not handle the LATM
* data length field at the start of each returned LATM
* frame, tell the RTP source to omit. */
((MPEG4LATMAudioRTPSource*)sub->rtpSource())->omitLATMDataLengthField();
}
else if (fmt.codec == "MPEG4-GENERIC") {
unsigned int i_extra;
uint8_t *p_extra;
if ((p_extra = parseGeneralConfigStr(sub->fmtp_config(),
i_extra)))
{
fmt.extra = string((char*)p_extra, i_extra);
delete[] p_extra;
}
}
else if (fmt.codec == "SPEEX") {
if (fmt.audio.i_rate == 0)
{
onDebug("Using 8kHz as default sample rate.");
fmt.audio.i_rate = 8000;
}
}
else if (fmt.codec == "VORBIS") {
unsigned int i_extra;
unsigned char *p_extra;
if ((p_extra = parseVorbisConfigStr(sub->fmtp_config(),
i_extra)))
{
fmt.extra = string((char*)p_extra, i_extra);
delete[] p_extra;
}
}
}
else if (fmt.type == "video") {
if (fmt.codec == "MPV")
{
fmt.b_packetized = false;
}
else if (fmt.codec == "H264")
{
unsigned int i_extra = 0;
uint8_t *p_extra = NULL;
fmt.b_packetized = false;
if ((p_extra = parseH264ConfigStr(sub->fmtp_spropparametersets(),
i_extra)))
{
fmt.extra = string((char*)p_extra, i_extra);
delete[] p_extra;
}
}
#if LIVEMEDIA_LIBRARY_VERSION_INT >= 1393372800 // 2014.02.26
else if (fmt.codec == "H265")
{
unsigned int i_extra1 = 0, i_extra2 = 0, i_extra3 = 0, i_extraTot;
uint8_t *p_extra1 = NULL, *p_extra2 = NULL, *p_extra3 = NULL;
fmt.b_packetized = false;
p_extra1 = parseH264ConfigStr(sub->fmtp_spropvps(), i_extra1);
p_extra2 = parseH264ConfigStr(sub->fmtp_spropsps(), i_extra2);
p_extra3 = parseH264ConfigStr(sub->fmtp_sproppps(), i_extra3);
i_extraTot = i_extra1 + i_extra2 + i_extra3;
if (i_extraTot > 0)
{
if (p_extra1)
{
fmt.extra = string((char*)p_extra1, i_extra1);
delete[] p_extra1;
}
if (p_extra2)
{
fmt.extra += string((char*)p_extra2, i_extra2);
delete[] p_extra2;
}
if (p_extra3)
{
fmt.extra += string((char*)p_extra3, i_extra3);
delete[] p_extra3;
}
}
}
#endif
else if (fmt.codec == "MP4V-ES")
{
unsigned int i_extra;
uint8_t *p_extra;
if ((p_extra = parseGeneralConfigStr(sub->fmtp_config(),
i_extra)))
{
fmt.extra = string((char*)p_extra, i_extra);
delete[] p_extra;
}
}
else if ((fmt.codec == "X-QT") ||
(fmt.codec == "X-QUICKTIME") ||
(fmt.codec == "X-QDM") ||
(fmt.codec == "X-SV3V-ES") ||
(fmt.codec == "X-SORENSONVIDEO"))
{
b_quicktime = true;
}
else if (fmt.codec == "DV")
{
b_discard_trunc = true;
}
else if (fmt.codec == "THEORA")
{
unsigned int i_extra;
uint8_t *p_extra;
if ((p_extra = parseVorbisConfigStr(sub->fmtp_config(),
i_extra)))
{
fmt.extra = string((char*)p_extra, i_extra);
delete[] p_extra;
}
else
onDebug("Missing or unsupported theora header.");
}
}
/* Try and parse a=lang: attribute */
const char* p_lang = strstr( sub->savedSDPLines(), "a=lang:" );
if( p_lang )
{
unsigned i_lang_len;
p_lang += 7;
i_lang_len = strcspn( p_lang, " \r\n" );
fmt.text.psz_language = string( p_lang, i_lang_len );
}
if( sub->rtcpInstance() != NULL ){
sub->rtcpInstance()->setByeHandler( Live555Client::LiveTrack::streamClose, this );
}
return RTSP_OK;
}
const char* Live555Client::LiveTrack::getSessionId() const
{
MediaSubsession* sub = static_cast<MediaSubsession*>(media_sub_session);
if (!sub)
return "";
return sub->sessionId();
}
const char* Live555Client::LiveTrack::getSessionName() const
{
MediaSubsession* sub = static_cast<MediaSubsession*>(media_sub_session);
if (!sub)
return "";
return sub->controlPath();
}
void Live555Client::LiveTrack::streamRead(void *opaque, unsigned int i_size,
unsigned int i_truncated_bytes, struct timeval pts,
unsigned int duration)
{
Live555Client::LiveTrack* pThis = static_cast<Live555Client::LiveTrack*>(opaque);
pThis->parent->onStreamRead(pThis, i_size, i_truncated_bytes, pts, duration);
}
void Live555Client::LiveTrack::streamClose(void* opaque)
{
Live555Client::LiveTrack* pThis = static_cast<Live555Client::LiveTrack*>(opaque);
pThis->parent->onStreamClose(pThis);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
void Live555Client::taskInterruptData( void *opaque )
{
Live555Client *pThis = static_cast<Live555Client*>(opaque);
pThis->i_no_data_ti++;
/* Avoid lock */
pThis->event_data = (char)0xff;
}
void Live555Client::taskInterruptRTSP( void *opaque )
{
Live555Client *pThis = static_cast<Live555Client*>(opaque);
pThis->live555ResultCode = HTTP_TIMEOUT;
/* Avoid lock */
pThis->event_rtsp = (char)0xff;
}
void Live555Client::taskInterrupKeepAlive(void *opaque)
{
Live555Client *pThis = static_cast<Live555Client*>(opaque);
if (!pThis) return;
MyRTSPClient *pRtsp = pThis->rtsp;
if (!pRtsp) return;
MediaSession *pMedia = pThis->m_pMediaSession;
if (!pMedia) return;
TaskScheduler *sch = static_cast<TaskScheduler*>(pThis->scheduler);
if (!sch) return;
char *psz_bye = NULL;
if (pRtsp->isSupportsGetParameter())
pRtsp->sendGetParameterCommand(*pMedia, NULL, psz_bye);
else {
if (pThis->user_name.length() > 0 && (pThis->password.length()) > 0) {
Authenticator authenticator;
authenticator.setUsernameAndPassword(pThis->user_name.c_str(), pThis->password.c_str());
pRtsp->sendOptionsCommand(NULL, &authenticator);
}
else {
pRtsp->sendOptionsCommand(NULL, NULL);
}
}
sch->rescheduleDelayedTask(pThis->taskKeepAlive, 3000000, (TaskFunc*)taskInterrupKeepAlive, opaque);
}
int Live555Client::waitLive555Response( int i_timeout /* ms */ )
{
TaskToken task = nullptr;
BasicTaskScheduler* sch = (BasicTaskScheduler*)scheduler;
event_rtsp = 0;
if( i_timeout > 0 )
{
/* Create a task that will be called if we wait more than timeout ms */
task = sch->scheduleDelayedTask( i_timeout*1000,
taskInterruptRTSP,
this );
}
event_rtsp = 0;
live555ResultCode = HTTP_OK;
sch->doEventLoop( &event_rtsp );
//here, if b_error is true and i_live555_ret = 0 we didn't receive a response
if(task)
{
/* remove the task */
sch->unscheduleDelayedTask( task );
}
return HttpErrToRtspErr(live555ResultCode);
}
#define DEFAULT_FRAME_BUFFER_SIZE 500000
//这个接口到底危险不危险?
void Live555Client::controlPauseState()
{
RTSPClient* client = static_cast<RTSPClient*>(rtsp);
b_is_paused = !b_is_paused;
if (b_is_paused) {
client->sendPauseCommand( *m_pMediaSession, MyRTSPClient::default_live555_callback );
}
else {
client->sendPlayCommand( *m_pMediaSession, MyRTSPClient::default_live555_callback, -1.0f, -1.0f, m_pMediaSession->scale() );
}
waitLive555Response(DEFAULT_WAIT_TIME);
}
void Live555Client::controlSeek()
{
RTSPClient* client = static_cast<RTSPClient*>(rtsp);
client->sendPauseCommand( *m_pMediaSession, MyRTSPClient::default_live555_callback );
waitLive555Response(DEFAULT_WAIT_TIME);
client->sendPlayCommand( *m_pMediaSession, MyRTSPClient::default_live555_callback, f_seekTime, -1.0f, m_pMediaSession->scale() );
waitLive555Response(DEFAULT_WAIT_TIME);
f_seekTime = -1.0;
/* Retrieve the starttime if possible */
f_npt = f_npt_start = m_pMediaSession->playStartTime();
/* Retrieve the duration if possible */
if(m_pMediaSession->playEndTime() > 0 )
f_npt_length = m_pMediaSession->playEndTime();
}
int Live555Client::demux(void)
{
TaskToken task;
MyRTSPClient* client = static_cast<MyRTSPClient*>(rtsp);
TaskScheduler* sch = static_cast<TaskScheduler*>(scheduler);
bool b_send_pcr = true;
if (b_is_paused)
return RTSP_OK;
/* First warn we want to read data */
event_data = 0;
for (auto it = listTracks.begin(); it != listTracks.end(); ++it)
{
LiveTrack *tk = *it;
MediaSubsession* sub = static_cast<MediaSubsession*>(tk->getMediaSubsession());
uint8_t* p_buffer = tk->buffer();
if( tk->getFormat().codec == "AMR" ||
tk->getFormat().codec == "AMR-WB" )
{
p_buffer++;
}
else if( tk->getFormat().codec == "H261" ||
tk->getFormat().codec == "H264" ||
tk->getFormat().codec == "H265" )
{
p_buffer += 4;
}
if( !tk->isWaiting() )
{
tk->doWaiting(1);
sub->readSource()->getNextFrame( p_buffer, tk->buffer_size(),
Live555Client::LiveTrack::streamRead, tk, Live555Client::LiveTrack::streamClose, tk );
}
}
/* Create a task that will be called if we wait more than 300ms */
task = sch->scheduleDelayedTask( 300000, taskInterruptData, this );
/* Do the read */
sch->doEventLoop( &event_data );
/* remove the task */
sch->unscheduleDelayedTask( task );
/* Check for gap in pts value */
for (auto it = listTracks.begin(); it != listTracks.end(); ++it)
{
LiveTrack *tk = *it;
MediaSubsession* sub = static_cast<MediaSubsession*>(tk->getMediaSubsession());
if( !tk->b_muxed && !tk->b_rtcp_sync && sub->rtpSource() && sub->rtpSource()->hasBeenSynchronizedUsingRTCP() )
{
onDebug("tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
onResetPcr();
tk->b_rtcp_sync = true;
/* reset PCR */
tk->i_pts = VLC_TS_INVALID;
tk->f_npt = 0.;
i_pcr = 0;
f_npt = 0.;
}
}
if( b_multicast && ( i_no_data_ti > 120 ) )
{
onDebug( "no multicast data received in 36s, aborting" );
return RTSP_TIMEOUT;
}
else if( !b_multicast && !b_paused && ( i_no_data_ti > 34 ) )
{
onDebug( "no data received in 10s, aborting" );
return RTSP_TIMEOUT;
}
if( i_no_data_ti > 33 || live555ResultCode == HTTP_SESSION_NOT_FOUND) { //no data received in 10s, eof ?
return RTSP_TIMEOUT;
}
return RTSP_OK;
}
int Live555Client::demux_loop()
{
demuxLoopFlag = true;
while (demuxLoopFlag)
{
if (b_do_control_pause_state) {
if (f_seekTime >= 0)
controlSeek();
else
controlPauseState();
b_do_control_pause_state = false;
}
int r = demux();
if (r != RTSP_OK){
return r;
}
if (live555ResultCode != 0) {
r = HttpErrToRtspErr(live555ResultCode);
return r;
}
}
//用户主动关闭
return RTSP_USR_STOP;
}
Live555Client::Live555Client(void)
: env(NULL)
, scheduler(NULL)
, rtsp(NULL)
, m_pMediaSession(NULL)
, event_rtsp(0)
, event_data(0)
, b_get_param(false)
, live555ResultCode(0)
, i_timeout(60)
, taskKeepAlive(NULL)
, i_pcr(VLC_TS_0)
, f_seekTime(-1.0)
, f_npt(0)
, f_npt_length(0)
, f_npt_start(0)
, i_no_data_ti(0)
, b_is_paused(false)
, b_do_control_pause_state(false)
, u_port_begin(0)
, user_agent("RTSPClient/1.0")
, demuxLoopFlag(false)
, b_rtsp_tcp(true)
{
scheduler = BasicTaskScheduler::createNew();
env = BasicUsageEnvironment::createNew(*(TaskScheduler*)scheduler);
}
Live555Client::~Live555Client(void)
{
StopRtsp();
UsageEnvironment* environment = static_cast<UsageEnvironment*>(env);
TaskScheduler* sch = static_cast<TaskScheduler*>(scheduler);
environment->reclaim();
delete sch;
sch = NULL;
}
int Live555Client::PlayRtsp(string Uri)
{
if (Uri.length() == 0) {
return RTSP_ERR;
}
int Status = RTSP_OK;
Authenticator authenticator;
UsageEnvironment* environment = static_cast<UsageEnvironment*>(env);
TaskScheduler* sch = static_cast<TaskScheduler*>(scheduler);
rtsp = new MyRTSPClient(*environment, Uri.c_str(), 0, user_agent.c_str(), 0, this);
if (!rtsp) {
return RTSP_ERR;
}
if (user_name.length() != 0 && password.length() != 0) {
authenticator.setUsernameAndPassword(user_name.c_str(), password.c_str());
}
rtsp->sendOptionsCommand(&MyRTSPClient::continueAfterOPTIONS, &authenticator);
Status = waitLive555Response(DEFAULT_WAIT_TIME);
if (Status != RTSP_OK){
goto quit;
}
f_npt_start = 0;
/* The PLAY */
rtsp->sendPlayCommand(*m_pMediaSession, MyRTSPClient::default_live555_callback, f_npt_start, -1, 1);
Status = waitLive555Response(DEFAULT_WAIT_TIME);
if (Status != RTSP_OK) {
goto quit;
}
/* Retrieve the timeout value and set up a timeout prevention thread */
i_timeout = rtsp->sessionTimeoutParameter();
if (i_timeout <= 0)
i_timeout = 60; /* default value from RFC2326 */
i_pcr = 0;
/* Retrieve the starttime if possible */
f_npt_start = m_pMediaSession->playStartTime();
if (m_pMediaSession->playEndTime() > 0)
f_npt_length = m_pMediaSession->playEndTime();
// now create thread for get data
b_is_paused = false;
b_do_control_pause_state = false;
taskKeepAlive = sch->scheduleDelayedTask(3000000, (TaskFunc*)taskInterrupKeepAlive, this);
Status = demux_loop();
quit:
if (taskKeepAlive) {
sch->unscheduleDelayedTask(taskKeepAlive);
taskKeepAlive = NULL;
}
if (rtsp && m_pMediaSession)
rtsp->sendTeardownCommand(*m_pMediaSession, NULL);
if (m_pMediaSession) {
Medium::close(m_pMediaSession);
m_pMediaSession = NULL;
}
if (rtsp) {
RTSPClient::close(rtsp);
rtsp = NULL;
}
for (size_t i = 0; i < listTracks.size(); i++) {
delete listTracks[i];
}
listTracks.clear();
u_port_begin = 0;
return Status;
}
void Live555Client::togglePause()
{
b_do_control_pause_state = true;
}
int Live555Client::seek(double f_time)
{
if (f_npt_length <= 0)
return -1; // unsupported
if (f_time <= 0)
f_time = 0;
if (f_time > f_npt_length)
f_time = f_npt_length;
f_seekTime = f_time;
b_do_control_pause_state = true;
return 0;
}
void Live555Client::StopRtsp()
{
demuxLoopFlag = false;
}
void Live555Client::setUser(const char* user_name, const char* password)
{
this->user_name = user_name;
this->password = password;
}
void Live555Client::setDestination(string Addr, int DstPort)
{
MyRTSPClient* client = static_cast<MyRTSPClient*>(rtsp);
client->setDestination(Addr, DstPort);
}
void Live555Client::continueAfterDESCRIBE( int result_code, char* sdp)
{
live555ResultCode = result_code;
if ( result_code != 0 ){
return;
}
if ((sdp == nullptr) || strlen(sdp) < 5) {
live555ResultCode = HTTP_STREAM_NOT_FOUND;
return;
}
MediaSubsessionIterator *iter = NULL;
MediaSubsession *sub = NULL;
MyRTSPClient* client = static_cast<MyRTSPClient*>(rtsp);
UsageEnvironment* environment = static_cast<UsageEnvironment*>(env);
int i_client_port;
int i_return = 0;
unsigned int i_receive_buffer = 0;
int i_frame_buffer = DEFAULT_FRAME_BUFFER_SIZE;
unsigned const thresh = 200000; /* RTP reorder threshold .2 second (default .1) */
i_client_port = u_port_begin; //var_InheritInteger( p_demux, "rtp-client-port" );
/* here print sdp on debug */
printf("SDP content:\n%s", sdp);
/* Create the session from the SDP */
m_pMediaSession = MediaSession::createNew(*environment, sdp);
iter = new MediaSubsessionIterator(*m_pMediaSession);
while ((sub = iter->next()) != NULL)
{
bool b_init;
LiveTrack* tk;
/* Value taken from mplayer */
if (!strcmp(sub->mediumName(), "audio"))
i_receive_buffer = 200000;
else if (!strcmp(sub->mediumName(), "video"))
i_receive_buffer = 2000000;
else if (!strcmp(sub->mediumName(), "text"))
;
else continue;
if (i_client_port != -1)
{
sub->setClientPortNum(i_client_port);
i_client_port += 2;
}
if (!strcmp(sub->codecName(), "X-ASF-PF"))
b_init = sub->initiate(0);
else
b_init = sub->initiate();
if (b_init)
{
FramedFilter* normalizerFilter = client->createNewPresentationTimeSubsessionNormalizer(
sub->readSource(), sub->rtpSource(),
sub->codecName());
sub->addFilter(normalizerFilter);
if (sub->rtpSource() != NULL)
{
int fd = sub->rtpSource()->RTPgs()->socketNum();
/* Increase the buffer size */
if (i_receive_buffer > 0)
increaseReceiveBufferTo(*environment, fd, i_receive_buffer);
/* Increase the RTP reorder timebuffer just a bit */
sub->rtpSource()->setPacketReorderingThresholdTime(thresh);
}
/* Issue the SETUP */
if (client)
{
client->sendSetupCommand(*sub, MyRTSPClient::default_live555_callback, False,
toBool(b_rtsp_tcp),
False/*toBool( p_sys->b_force_mcast && !b_rtsp_tcp )*/);
if (waitLive555Response(DEFAULT_WAIT_TIME) != RTSP_OK)
{
/* if we get an unsupported transport error, toggle TCP
* use and try again */
if (live555ResultCode != HTTP_UNSUPPORTED_TRANSPOR) {
break;
}
client->sendSetupCommand(*sub, MyRTSPClient::default_live555_callback, False,
!toBool(b_rtsp_tcp), False);
if (waitLive555Response(DEFAULT_WAIT_TIME) != RTSP_OK){
onDebug("SETUP failed!");
break;
}
else{
b_rtsp_tcp = true;
}
}
}
/* Check if we will receive data from this subsession for
* this track */
if (sub->readSource() == NULL) continue;
if (!b_multicast){
/* We need different rollover behaviour for multicast */
b_multicast = IsMulticastAddress(sub->connectionEndpointAddress());
}
tk = new LiveTrack(this, sub, i_frame_buffer);
if (tk->init() != RTSP_OK){
delete tk;
break;
}
else {
listTracks.push_back(tk);