-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathRtspConnection.c
1221 lines (1024 loc) · 39.6 KB
/
RtspConnection.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 "Limelight-internal.h"
#include "Rtsp.h"
#define RTSP_CONNECT_TIMEOUT_SEC 10
#define RTSP_RECEIVE_TIMEOUT_SEC 15
#define RTSP_RETRY_DELAY_MS 500
static int currentSeqNumber;
static char rtspTargetUrl[256];
static char* sessionIdString;
static bool hasSessionId;
static int rtspClientVersion;
static char urlAddr[URLSAFESTRING_LEN];
static bool useEnet;
static char* controlStreamId;
static SOCKET sock = INVALID_SOCKET;
static ENetHost* client;
static ENetPeer* peer;
#define CHAR_TO_INT(x) ((x) - '0')
#define CHAR_IS_DIGIT(x) ((x) >= '0' && (x) <= '9')
// Create RTSP Option
static POPTION_ITEM createOptionItem(char* option, char* content)
{
POPTION_ITEM item = malloc(sizeof(*item));
if (item == NULL) {
return NULL;
}
item->option = strdup(option);
if (item->option == NULL) {
free(item);
return NULL;
}
item->content = strdup(content);
if (item->content == NULL) {
free(item->option);
free(item);
return NULL;
}
item->next = NULL;
item->flags = FLAG_ALLOCATED_OPTION_FIELDS;
return item;
}
// Add an option to the RTSP Message
static bool addOption(PRTSP_MESSAGE msg, char* option, char* content)
{
POPTION_ITEM item = createOptionItem(option, content);
if (item == NULL) {
return false;
}
insertOption(&msg->options, item);
msg->flags |= FLAG_ALLOCATED_OPTION_ITEMS;
return true;
}
// Create an RTSP Request
static bool initializeRtspRequest(PRTSP_MESSAGE msg, char* command, char* target)
{
char sequenceNumberStr[16];
char clientVersionStr[16];
// FIXME: Hacked CSeq attribute due to RTSP parser bug
createRtspRequest(msg, NULL, 0, command, target, "RTSP/1.0",
0, NULL, NULL, 0);
sprintf(sequenceNumberStr, "%d", currentSeqNumber++);
sprintf(clientVersionStr, "%d", rtspClientVersion);
if (!addOption(msg, "CSeq", sequenceNumberStr) ||
!addOption(msg, "X-GS-ClientVersion", clientVersionStr) ||
(!useEnet && !addOption(msg, "Host", urlAddr))) {
freeMessage(msg);
return false;
}
return true;
}
// Send RTSP message and get response over ENet
static bool transactRtspMessageEnet(PRTSP_MESSAGE request, PRTSP_MESSAGE response, bool expectingPayload, int* error) {
ENetEvent event;
char* serializedMessage;
int messageLen;
int offset;
ENetPacket* packet;
char* payload;
int payloadLength;
bool ret;
char* responseBuffer;
*error = -1;
ret = false;
responseBuffer = NULL;
// We're going to handle the payload separately, so temporarily set the payload to NULL
payload = request->payload;
payloadLength = request->payloadLength;
request->payload = NULL;
request->payloadLength = 0;
// Serialize the RTSP message into a message buffer
serializedMessage = serializeRtspMessage(request, &messageLen);
if (serializedMessage == NULL) {
goto Exit;
}
// Create the reliable packet that describes our outgoing message
packet = enet_packet_create(serializedMessage, messageLen, ENET_PACKET_FLAG_RELIABLE);
if (packet == NULL) {
goto Exit;
}
// Send the message
if (enet_peer_send(peer, 0, packet) < 0) {
enet_packet_destroy(packet);
goto Exit;
}
enet_host_flush(client);
// If we have a payload to send, we'll need to send that separately
if (payload != NULL) {
packet = enet_packet_create(payload, payloadLength, ENET_PACKET_FLAG_RELIABLE);
if (packet == NULL) {
goto Exit;
}
// Send the payload
if (enet_peer_send(peer, 0, packet) < 0) {
enet_packet_destroy(packet);
goto Exit;
}
enet_host_flush(client);
}
// Wait for a reply
if (serviceEnetHost(client, &event, RTSP_RECEIVE_TIMEOUT_SEC * 1000) <= 0 ||
event.type != ENET_EVENT_TYPE_RECEIVE) {
Limelog("Failed to receive RTSP reply\n");
goto Exit;
}
responseBuffer = malloc(event.packet->dataLength);
if (responseBuffer == NULL) {
Limelog("Failed to allocate RTSP response buffer\n");
enet_packet_destroy(event.packet);
goto Exit;
}
// Copy the data out and destroy the packet
memcpy(responseBuffer, event.packet->data, event.packet->dataLength);
offset = (int) event.packet->dataLength;
enet_packet_destroy(event.packet);
// Wait for the payload if we're expecting some
if (expectingPayload) {
// The payload comes in a second packet
if (serviceEnetHost(client, &event, RTSP_RECEIVE_TIMEOUT_SEC * 1000) <= 0 ||
event.type != ENET_EVENT_TYPE_RECEIVE) {
Limelog("Failed to receive RTSP reply payload\n");
goto Exit;
}
responseBuffer = extendBuffer(responseBuffer, event.packet->dataLength + offset);
if (responseBuffer == NULL) {
Limelog("Failed to extend RTSP response buffer\n");
enet_packet_destroy(event.packet);
goto Exit;
}
// Copy the payload out to the end of the response buffer and destroy the packet
memcpy(&responseBuffer[offset], event.packet->data, event.packet->dataLength);
offset += (int) event.packet->dataLength;
enet_packet_destroy(event.packet);
}
if (parseRtspMessage(response, responseBuffer, offset) == RTSP_ERROR_SUCCESS) {
// Successfully parsed response
ret = true;
}
else {
Limelog("Failed to parse RTSP response\n");
}
Exit:
// Swap back the payload pointer to avoid leaking memory later
request->payload = payload;
request->payloadLength = payloadLength;
// Free the serialized buffer
if (serializedMessage != NULL) {
free(serializedMessage);
}
// Free the response buffer
if (responseBuffer != NULL) {
free(responseBuffer);
}
return ret;
}
// Send RTSP message and get response over TCP
static bool transactRtspMessageTcp(PRTSP_MESSAGE request, PRTSP_MESSAGE response, int* error) {
SOCK_RET err;
bool ret;
int offset;
char* serializedMessage = NULL;
int messageLen;
char* responseBuffer;
int responseBufferSize;
int connectRetries;
*error = -1;
ret = false;
responseBuffer = NULL;
connectRetries = 0;
// Retry up to 10 seconds if we receive ECONNREFUSED errors from the host PC.
// This can happen with GFE 3.22 when initially launching a session because it
// returns HTTP 200 OK for the /launch request before the RTSP handshake port
// is listening.
do {
sock = connectTcpSocket(&RemoteAddr, RemoteAddrLen, RtspPortNumber, RTSP_CONNECT_TIMEOUT_SEC);
if (sock == INVALID_SOCKET) {
*error = LastSocketError();
if (*error == ECONNREFUSED) {
// Try again after 500 ms on ECONNREFUSED
PltSleepMs(RTSP_RETRY_DELAY_MS);
}
else {
// Fail if we get some other error
break;
}
}
else {
// We successfully connected
break;
}
} while (connectRetries++ < (RTSP_CONNECT_TIMEOUT_SEC * 1000) / RTSP_RETRY_DELAY_MS && !ConnectionInterrupted);
if (sock == INVALID_SOCKET) {
return ret;
}
serializedMessage = serializeRtspMessage(request, &messageLen);
if (serializedMessage == NULL) {
closeSocket(sock);
sock = INVALID_SOCKET;
return ret;
}
// Send our message split into smaller chunks to avoid MTU issues.
// enableNoDelay() must have been called for sendMtuSafe() to work.
enableNoDelay(sock);
err = sendMtuSafe(sock, serializedMessage, messageLen);
if (err == SOCKET_ERROR) {
*error = LastSocketError();
Limelog("Failed to send RTSP message: %d\n", *error);
goto Exit;
}
// Read the response until the server closes the connection
offset = 0;
responseBufferSize = 0;
for (;;) {
struct pollfd pfd;
if (offset >= responseBufferSize) {
responseBufferSize = offset + 16384;
responseBuffer = extendBuffer(responseBuffer, responseBufferSize);
if (responseBuffer == NULL) {
Limelog("Failed to allocate RTSP response buffer\n");
goto Exit;
}
}
pfd.fd = sock;
pfd.events = POLLIN;
err = pollSockets(&pfd, 1, RTSP_RECEIVE_TIMEOUT_SEC * 1000);
if (err == 0) {
*error = ETIMEDOUT;
Limelog("RTSP request timed out\n");
goto Exit;
}
else if (err < 0) {
*error = LastSocketError();
Limelog("Failed to wait for RTSP response: %d\n", *error);
goto Exit;
}
err = recv(sock, &responseBuffer[offset], responseBufferSize - offset, 0);
if (err < 0) {
// Error reading
*error = LastSocketError();
Limelog("Failed to read RTSP response: %d\n", *error);
goto Exit;
}
else if (err == 0) {
// Done reading
break;
}
else {
offset += err;
}
}
if (parseRtspMessage(response, responseBuffer, offset) == RTSP_ERROR_SUCCESS) {
// Successfully parsed response
ret = true;
}
else {
Limelog("Failed to parse RTSP response\n");
}
Exit:
if (serializedMessage != NULL) {
free(serializedMessage);
}
if (responseBuffer != NULL) {
free(responseBuffer);
}
closeSocket(sock);
sock = INVALID_SOCKET;
return ret;
}
static bool transactRtspMessage(PRTSP_MESSAGE request, PRTSP_MESSAGE response, bool expectingPayload, int* error) {
if (ConnectionInterrupted) {
*error = -1;
return false;
}
if (useEnet) {
return transactRtspMessageEnet(request, response, expectingPayload, error);
}
else {
return transactRtspMessageTcp(request, response, error);
}
}
// Send RTSP OPTIONS request
static bool requestOptions(PRTSP_MESSAGE response, int* error) {
RTSP_MESSAGE request;
bool ret;
*error = -1;
ret = initializeRtspRequest(&request, "OPTIONS", rtspTargetUrl);
if (ret) {
ret = transactRtspMessage(&request, response, false, error);
freeMessage(&request);
}
return ret;
}
// Send RTSP DESCRIBE request
static bool requestDescribe(PRTSP_MESSAGE response, int* error) {
RTSP_MESSAGE request;
bool ret;
*error = -1;
ret = initializeRtspRequest(&request, "DESCRIBE", rtspTargetUrl);
if (ret) {
if (addOption(&request, "Accept",
"application/sdp") &&
addOption(&request, "If-Modified-Since",
"Thu, 01 Jan 1970 00:00:00 GMT")) {
ret = transactRtspMessage(&request, response, true, error);
}
else {
ret = false;
}
freeMessage(&request);
}
return ret;
}
// Send RTSP SETUP request
static bool setupStream(PRTSP_MESSAGE response, char* target, int* error) {
RTSP_MESSAGE request;
bool ret;
char* transportValue;
*error = -1;
ret = initializeRtspRequest(&request, "SETUP", target);
if (ret) {
if (hasSessionId) {
if (!addOption(&request, "Session", sessionIdString)) {
ret = false;
goto FreeMessage;
}
}
if (AppVersionQuad[0] >= 6) {
// It looks like GFE doesn't care what we say our port is but
// we need to give it some port to successfully complete the
// handshake process.
transportValue = "unicast;X-GS-ClientPort=50000-50001";
}
else {
transportValue = " ";
}
if (addOption(&request, "Transport", transportValue) &&
addOption(&request, "If-Modified-Since",
"Thu, 01 Jan 1970 00:00:00 GMT")) {
ret = transactRtspMessage(&request, response, false, error);
}
else {
ret = false;
}
FreeMessage:
freeMessage(&request);
}
return ret;
}
// Send RTSP PLAY request
static bool playStream(PRTSP_MESSAGE response, char* target, int* error) {
RTSP_MESSAGE request;
bool ret;
*error = -1;
ret = initializeRtspRequest(&request, "PLAY", target);
if (ret != 0) {
if (addOption(&request, "Session", sessionIdString)) {
ret = transactRtspMessage(&request, response, false, error);
}
else {
ret = false;
}
freeMessage(&request);
}
return ret;
}
// Send RTSP ANNOUNCE message
static bool sendVideoAnnounce(PRTSP_MESSAGE response, int* error) {
RTSP_MESSAGE request;
bool ret;
int payloadLength;
char payloadLengthStr[16];
*error = -1;
ret = initializeRtspRequest(&request, "ANNOUNCE",
APP_VERSION_AT_LEAST(7, 1, 431) ? controlStreamId : "streamid=video");
if (ret) {
ret = false;
if (!addOption(&request, "Session", sessionIdString) ||
!addOption(&request, "Content-type", "application/sdp")) {
goto FreeMessage;
}
request.payload = getSdpPayloadForStreamConfig(rtspClientVersion, &payloadLength);
if (request.payload == NULL) {
goto FreeMessage;
}
request.flags |= FLAG_ALLOCATED_PAYLOAD;
request.payloadLength = payloadLength;
sprintf(payloadLengthStr, "%d", payloadLength);
if (!addOption(&request, "Content-length", payloadLengthStr)) {
goto FreeMessage;
}
ret = transactRtspMessage(&request, response, false, error);
FreeMessage:
freeMessage(&request);
}
return ret;
}
static int parseOpusConfigFromParamString(char* paramStr, int channelCount, POPUS_MULTISTREAM_CONFIGURATION opusConfig) {
int i;
// Set channel count (included in the prefix, so not parsed below)
opusConfig->channelCount = channelCount;
// Parse the remaining data from the surround-params value
if (!CHAR_IS_DIGIT(*paramStr)) {
Limelog("Invalid stream count: %c\n", *paramStr);
return -1;
}
opusConfig->streams = CHAR_TO_INT(*paramStr);
paramStr++;
if (!CHAR_IS_DIGIT(*paramStr)) {
Limelog("Invalid coupled stream count: %c\n", *paramStr);
return -2;
}
opusConfig->coupledStreams = CHAR_TO_INT(*paramStr);
paramStr++;
for (i = 0; i < opusConfig->channelCount; i++) {
if (!CHAR_IS_DIGIT(*paramStr)) {
Limelog("Invalid mapping value at %d: %c\n", i, *paramStr);
return -3;
}
opusConfig->mapping[i] = CHAR_TO_INT(*paramStr);
paramStr++;
}
return 0;
}
// Parse the server port from the Transport header
// Example: unicast;server_port=48000-48001;source=192.168.35.177
static bool parseServerPortFromTransport(PRTSP_MESSAGE response, uint16_t* port) {
char* transport;
char* portStart;
transport = getOptionContent(response->options, "Transport");
if (transport == NULL) {
return false;
}
// Look for the server_port= entry in the Transport option
portStart = strstr(transport, "server_port=");
if (portStart == NULL) {
return false;
}
// Skip the prefix
portStart += strlen("server_port=");
// Validate the port number
long int rawPort = strtol(portStart, NULL, 10);
if (rawPort <= 0 || rawPort > 65535) {
return false;
}
*port = (uint16_t)rawPort;
return true;
}
// Parses the Opus configuration from an RTSP DESCRIBE response
static int parseOpusConfigurations(PRTSP_MESSAGE response) {
HighQualitySurroundSupported = false;
memset(&NormalQualityOpusConfig, 0, sizeof(NormalQualityOpusConfig));
memset(&HighQualityOpusConfig, 0, sizeof(HighQualityOpusConfig));
// Sample rate is always 48 KHz
HighQualityOpusConfig.sampleRate = NormalQualityOpusConfig.sampleRate = 48000;
// Stereo doesn't have any surround-params elements in the RTSP data
if (CHANNEL_COUNT_FROM_AUDIO_CONFIGURATION(StreamConfig.audioConfiguration) == 2) {
NormalQualityOpusConfig.channelCount = 2;
NormalQualityOpusConfig.streams = 1;
NormalQualityOpusConfig.coupledStreams = 1;
NormalQualityOpusConfig.mapping[0] = 0;
NormalQualityOpusConfig.mapping[1] = 1;
}
else {
char paramsPrefix[128];
char* paramStart;
int err;
int channelCount;
channelCount = CHANNEL_COUNT_FROM_AUDIO_CONFIGURATION(StreamConfig.audioConfiguration);
// Find the correct audio parameter value
sprintf(paramsPrefix, "a=fmtp:97 surround-params=%d", channelCount);
paramStart = strstr(response->payload, paramsPrefix);
if (paramStart) {
// Skip the prefix
paramStart += strlen(paramsPrefix);
// Parse the normal quality Opus config
err = parseOpusConfigFromParamString(paramStart, channelCount, &NormalQualityOpusConfig);
if (err != 0) {
return err;
}
// GFE's normal-quality channel mapping differs from the one our clients use.
// They use FL FR C RL RR SL SR LFE, but we use FL FR C LFE RL RR SL SR. We'll need
// to swap the mappings to match the expected values.
if (channelCount == 6 || channelCount == 8) {
OPUS_MULTISTREAM_CONFIGURATION originalMapping = NormalQualityOpusConfig;
// LFE comes after C
NormalQualityOpusConfig.mapping[3] = originalMapping.mapping[channelCount - 1];
// Slide everything else up
memcpy(&NormalQualityOpusConfig.mapping[4],
&originalMapping.mapping[3],
channelCount - 4);
}
// If this configuration is compatible with high quality mode, we may have another
// matching surround-params value for high quality mode.
paramStart = strstr(paramStart, paramsPrefix);
if (paramStart) {
// Skip the prefix
paramStart += strlen(paramsPrefix);
// Parse the high quality Opus config
err = parseOpusConfigFromParamString(paramStart, channelCount, &HighQualityOpusConfig);
if (err != 0) {
return err;
}
// We can request high quality audio
HighQualitySurroundSupported = true;
}
}
else {
Limelog("No surround parameters found for channel count: %d\n", channelCount);
// It's unknown whether all GFE versions that supported surround sound included these
// surround sound parameters. In case they didn't, we'll specifically handle 5.1 surround
// sound using a hardcoded configuration like we used to before this parsing code existed.
//
// It is not necessary to provide HighQualityOpusConfig here because high quality mode
// can only be enabled from seeing the required "surround-params=" value, so there's no
// chance it could regress from implementing this parser.
if (channelCount == 6) {
NormalQualityOpusConfig.channelCount = 6;
NormalQualityOpusConfig.streams = 4;
NormalQualityOpusConfig.coupledStreams = 2;
NormalQualityOpusConfig.mapping[0] = 0;
NormalQualityOpusConfig.mapping[1] = 4;
NormalQualityOpusConfig.mapping[2] = 1;
NormalQualityOpusConfig.mapping[3] = 5;
NormalQualityOpusConfig.mapping[4] = 2;
NormalQualityOpusConfig.mapping[5] = 3;
}
else {
// We don't have a hardcoded fallback mapping, so we have no choice but to fail.
return -4;
}
}
}
return 0;
}
static bool parseUrlAddrFromRtspUrlString(const char* rtspUrlString, char* destination) {
char* rtspUrlScratchBuffer;
char* portSeparator;
char* v6EscapeEndChar;
char* urlPathSeparator;
int prefixLen;
// Create a copy that we can modify
rtspUrlScratchBuffer = strdup(rtspUrlString);
if (rtspUrlScratchBuffer == NULL) {
return false;
}
// If we have a v6 address, we want to stop one character after the closing ]
// If we have a v4 address, we want to stop at the port separator
portSeparator = strrchr(rtspUrlScratchBuffer, ':');
v6EscapeEndChar = strchr(rtspUrlScratchBuffer, ']');
// Count the prefix length to skip past the initial rtsp:// or rtspru:// part
for (prefixLen = 2; rtspUrlScratchBuffer[prefixLen - 2] != 0 && (rtspUrlScratchBuffer[prefixLen - 2] != '/' || rtspUrlScratchBuffer[prefixLen - 1] != '/'); prefixLen++);
// If we hit the end of the string prior to parsing the prefix, we cannot proceed
if (rtspUrlScratchBuffer[prefixLen - 2] == 0) {
free(rtspUrlScratchBuffer);
return false;
}
// Look for a slash at the end of the host portion of the URL (may not be present)
urlPathSeparator = strchr(rtspUrlScratchBuffer + prefixLen, '/');
// Check for a v6 address first since they also have colons
if (v6EscapeEndChar) {
// Terminate the string at the next character
*(v6EscapeEndChar + 1) = 0;
}
else if (portSeparator) {
// Terminate the string prior to the port separator
*portSeparator = 0;
}
else if (urlPathSeparator) {
// Terminate the string prior to the path separator
*urlPathSeparator = 0;
}
strcpy(destination, rtspUrlScratchBuffer + prefixLen);
free(rtspUrlScratchBuffer);
return true;
}
// SDP attributes are in the form:
// a=x-nv-bwe.bwuSafeZoneLowLimit:70\r\n
bool parseSdpAttributeToUInt(const char* payload, const char* name, unsigned int* val) {
// Find the entry for the specified attribute name
char* attribute = strstr(payload, name);
if (!attribute) {
return false;
}
// Locate the start of the value
char* valst = strstr(attribute, ":");
if (!valst) {
return false;
}
// Locate the end of the value
char* valend;
if (!(valend = strstr(valst, "\r")) && !(valend = strstr(valst, "\n"))) {
return false;
}
// Swap the end character for a null terminator, read the integer, then swap it back
char valendchar = *valend;
*valend = 0;
*val = strtoul(valst + 1, NULL, 0);
*valend = valendchar;
return true;
}
bool parseSdpAttributeToInt(const char* payload, const char* name, int* val) {
// Find the entry for the specified attribute name
char* attribute = strstr(payload, name);
if (!attribute) {
return false;
}
// Locate the start of the value
char* valst = strstr(attribute, ":");
if (!valst) {
return false;
}
// Locate the end of the value
char* valend;
if (!(valend = strstr(valst, "\r")) && !(valend = strstr(valst, "\n"))) {
return false;
}
// Swap the end character for a null terminator, read the integer, then swap it back
char valendchar = *valend;
*valend = 0;
*val = strtol(valst + 1, NULL, 0);
*valend = valendchar;
return true;
}
// Perform RTSP Handshake with the streaming server machine as part of the connection process
int performRtspHandshake(PSERVER_INFORMATION serverInfo) {
int ret;
LC_ASSERT(RtspPortNumber != 0);
// Initialize global state
useEnet = (AppVersionQuad[0] >= 5) && (AppVersionQuad[0] <= 7) && (AppVersionQuad[2] < 404);
currentSeqNumber = 1;
hasSessionId = false;
controlStreamId = APP_VERSION_AT_LEAST(7, 1, 431) ? "streamid=control/13/0" : "streamid=control/1/0";
AudioEncryptionEnabled = false;
// HACK: In order to get GFE to respect our request for a lower audio bitrate, we must
// fake our target address so it doesn't match any of the PC's local interfaces. It seems
// that the only way to get it to give you "low quality" stereo audio nowadays is if it
// thinks you are remote (target address != any local address).
//
// We will enable high quality audio if the following are all true:
// 1. Video bitrate is higher than 15 Mbps (to ensure most bandwidth is reserved for video)
// 2. The audio decoder has not declared that it is slow
// 3. The stream is either local or not surround sound (to prevent MTU issues over the Internet)
LC_ASSERT(StreamConfig.streamingRemotely != STREAM_CFG_AUTO);
if (OriginalVideoBitrate >= HIGH_AUDIO_BITRATE_THRESHOLD &&
(AudioCallbacks.capabilities & CAPABILITY_SLOW_OPUS_DECODER) == 0 &&
(StreamConfig.streamingRemotely != STREAM_CFG_REMOTE || CHANNEL_COUNT_FROM_AUDIO_CONFIGURATION(StreamConfig.audioConfiguration) <= 2)) {
// If we have an RTSP URL string and it was successfully parsed, use that string
if (serverInfo->rtspSessionUrl != NULL && parseUrlAddrFromRtspUrlString(serverInfo->rtspSessionUrl, urlAddr)) {
strcpy(rtspTargetUrl, serverInfo->rtspSessionUrl);
}
else {
// If an RTSP URL string was not provided or failed to parse, we will construct one now as best we can.
//
// NB: If the remote address is not a LAN address, the host will likely not enable high quality
// audio since it only does that for local streaming normally. We can avoid this limitation,
// but only if the caller gave us the RTSP session URL that it received from the host during launch.
addrToUrlSafeString(&RemoteAddr, urlAddr);
sprintf(rtspTargetUrl, "rtsp%s://%s:%u", useEnet ? "ru" : "", urlAddr, RtspPortNumber);
}
}
else {
strcpy(urlAddr, "0.0.0.0");
sprintf(rtspTargetUrl, "rtsp%s://%s:%u", useEnet ? "ru" : "", urlAddr, RtspPortNumber);
}
switch (AppVersionQuad[0]) {
case 3:
rtspClientVersion = 10;
break;
case 4:
rtspClientVersion = 11;
break;
case 5:
rtspClientVersion = 12;
break;
case 6:
// Gen 6 has never been seen in the wild
rtspClientVersion = 13;
break;
case 7:
default:
rtspClientVersion = 14;
break;
}
// Setup ENet if required by this GFE version
if (useEnet) {
ENetAddress address;
ENetEvent event;
enet_address_set_address(&address, (struct sockaddr *)&RemoteAddr, RemoteAddrLen);
enet_address_set_port(&address, RtspPortNumber);
// Create a client that can use 1 outgoing connection and 1 channel
client = enet_host_create(RemoteAddr.ss_family, NULL, 1, 1, 0, 0);
if (client == NULL) {
return -1;
}
// Connect to the host
peer = enet_host_connect(client, &address, 1, 0);
if (peer == NULL) {
enet_host_destroy(client);
client = NULL;
return -1;
}
// Wait for the connect to complete
if (serviceEnetHost(client, &event, RTSP_CONNECT_TIMEOUT_SEC * 1000) <= 0 ||
event.type != ENET_EVENT_TYPE_CONNECT) {
Limelog("RTSP: Failed to connect to UDP port %u\n", RtspPortNumber);
enet_peer_reset(peer);
peer = NULL;
enet_host_destroy(client);
client = NULL;
return -1;
}
// Ensure the connect verify ACK is sent immediately
enet_host_flush(client);
}
{
RTSP_MESSAGE response;
int error = -1;
if (!requestOptions(&response, &error)) {
Limelog("RTSP OPTIONS request failed: %d\n", error);
ret = error;
goto Exit;
}
if (response.message.response.statusCode != 200) {
Limelog("RTSP OPTIONS request failed: %d\n",
response.message.response.statusCode);
ret = response.message.response.statusCode;
goto Exit;
}
freeMessage(&response);
}
{
RTSP_MESSAGE response;
int error = -1;
if (!requestDescribe(&response, &error)) {
Limelog("RTSP DESCRIBE request failed: %d\n", error);
ret = error;
goto Exit;
}
if (response.message.response.statusCode != 200) {
Limelog("RTSP DESCRIBE request failed: %d\n",
response.message.response.statusCode);
ret = response.message.response.statusCode;
goto Exit;
}
if ((StreamConfig.supportedVideoFormats & VIDEO_FORMAT_MASK_AV1) && strstr(response.payload, "AV1/90000")) {
if ((serverInfo->serverCodecModeSupport & SCM_AV1_MAIN10) && (StreamConfig.supportedVideoFormats & VIDEO_FORMAT_AV1_MAIN10)) {
NegotiatedVideoFormat = VIDEO_FORMAT_AV1_MAIN10;
}
else {
NegotiatedVideoFormat = VIDEO_FORMAT_AV1_MAIN8;
// Apply bitrate adjustment for SDR AV1 if the client requested one
if (StreamConfig.av1BitratePercentageMultiplier != 0) {
StreamConfig.bitrate *= StreamConfig.av1BitratePercentageMultiplier;
StreamConfig.bitrate /= 100;
}
}
}
else if ((StreamConfig.supportedVideoFormats & VIDEO_FORMAT_MASK_H265) && strstr(response.payload, "sprop-parameter-sets=AAAAAU")) {
// The RTSP DESCRIBE reply will contain a collection of SDP media attributes that
// describe the various supported video stream formats and include the SPS, PPS,
// and VPS (if applicable). We will use this information to determine whether the
// server can support HEVC. For some reason, they still set the MIME type of the HEVC
// format to H264, so we can't just look for the HEVC MIME type. What we'll do instead is
// look for the base 64 encoded VPS NALU prefix that is unique to the HEVC bitstream.
if ((serverInfo->serverCodecModeSupport & SCM_HEVC_MAIN10) && (StreamConfig.supportedVideoFormats & VIDEO_FORMAT_H265_MAIN10)) {
NegotiatedVideoFormat = VIDEO_FORMAT_H265_MAIN10;
}
else {
NegotiatedVideoFormat = VIDEO_FORMAT_H265;
// Apply bitrate adjustment for SDR HEVC if the client requested one
if (StreamConfig.hevcBitratePercentageMultiplier != 0) {
StreamConfig.bitrate *= StreamConfig.hevcBitratePercentageMultiplier;
StreamConfig.bitrate /= 100;
}
}
}
else {
NegotiatedVideoFormat = VIDEO_FORMAT_H264;
// Dimensions over 4096 are only supported with HEVC on NVENC
if (StreamConfig.width > 4096 || StreamConfig.height > 4096) {
Limelog("WARNING: Host PC doesn't support HEVC. Streaming at resolutions above 4K using H.264 will likely fail!\n");
}
}
// Look for the SDP attribute that indicates we're dealing with a server that supports RFI
ReferenceFrameInvalidationSupported = strstr(response.payload, "x-nv-video[0].refPicInvalidation") != NULL;
if (!ReferenceFrameInvalidationSupported) {
Limelog("Reference frame invalidation is not supported by this host\n");
}
// Look for the Sunshine feature flags in the SDP attributes
if (!parseSdpAttributeToUInt(response.payload, "x-ss-general.featureFlags", &SunshineFeatureFlags)) {
SunshineFeatureFlags = 0;
}
// Parse the Opus surround parameters out of the RTSP DESCRIBE response.
ret = parseOpusConfigurations(&response);
if (ret != 0) {
goto Exit;
}
freeMessage(&response);
}
{
RTSP_MESSAGE response;
char* sessionId;
char* pingPayload;
int error = -1;
if (!setupStream(&response,
AppVersionQuad[0] >= 5 ? "streamid=audio/0/0" : "streamid=audio",
&error)) {
Limelog("RTSP SETUP streamid=audio request failed: %d\n", error);
ret = error;
goto Exit;
}
if (response.message.response.statusCode != 200) {
Limelog("RTSP SETUP streamid=audio request failed: %d\n",
response.message.response.statusCode);
ret = response.message.response.statusCode;
goto Exit;
}
// Parse the audio port out of the RTSP SETUP response
LC_ASSERT(AudioPortNumber == 0);
if (!parseServerPortFromTransport(&response, &AudioPortNumber)) {
// Use the well known port if parsing fails
AudioPortNumber = 48000;
Limelog("Audio port: %u (RTSP parsing failed)\n", AudioPortNumber);
}
else {
Limelog("Audio port: %u\n", AudioPortNumber);