diff --git a/src/AudioStream.c b/src/AudioStream.c index 4cd18f1..b4b2a9c 100644 --- a/src/AudioStream.c +++ b/src/AudioStream.c @@ -45,6 +45,8 @@ static void AudioPingThreadProc(void* context) { char pingData[] = { 0x50, 0x49, 0x4E, 0x47 }; LC_SOCKADDR saddr; + LC_ASSERT(AudioPortNumber != 0); + memcpy(&saddr, &RemoteAddr, sizeof(saddr)); SET_PORT(&saddr, AudioPortNumber); @@ -94,11 +96,12 @@ int initializeAudioStream(void) { return 0; } -// This is called when the RTSP DESCRIBE message is parsed and the audio port +// This is called when the RTSP SETUP message is parsed and the audio port // number is parsed out of it. Alternatively, it's also called if parsing fails // and will use the well known audio port instead. int notifyAudioPortNegotiationComplete(void) { LC_ASSERT(!pingThreadStarted); + LC_ASSERT(AudioPortNumber != 0); // We may receive audio before our threads are started, but that's okay. We'll // drop the first 1 second of audio packets to catch up with the backlog. diff --git a/src/Connection.c b/src/Connection.c index e0213c1..430a543 100644 --- a/src/Connection.c +++ b/src/Connection.c @@ -238,11 +238,10 @@ int LiStartConnection(PSERVER_INFORMATION serverInfo, PSTREAM_CONFIGURATION stre OriginalVideoBitrate = streamConfig->bitrate; RemoteAddrString = strdup(serverInfo->address); - // Initialize port numbers to defaults. The values in RTSP SETUP (if valid) - // will override these to allow dynamic port selection. - VideoPortNumber = 47998; - ControlPortNumber = 47999; - AudioPortNumber = 48000; + // The values in RTSP SETUP will be used to populate these. + VideoPortNumber = 0; + ControlPortNumber = 0; + AudioPortNumber = 0; // Parse RTSP port number from RTSP session URL if (!parseRtspPortNumberFromUrl(serverInfo->rtspSessionUrl, &RtspPortNumber)) { diff --git a/src/ControlStream.c b/src/ControlStream.c index 02fe462..18055ad 100644 --- a/src/ControlStream.c +++ b/src/ControlStream.c @@ -1142,6 +1142,8 @@ int startControlStream(void) { ENetAddress address; ENetEvent event; + LC_ASSERT(ControlPortNumber != 0); + enet_address_set_address(&address, (struct sockaddr *)&RemoteAddr, RemoteAddrLen); enet_address_set_port(&address, ControlPortNumber); @@ -1183,6 +1185,7 @@ int startControlStream(void) { } else { // NB: Do NOT use ControlPortNumber here. 47995 is correct for these old versions. + LC_ASSERT(ControlPortNumber == 0); ctlSock = connectTcpSocket(&RemoteAddr, RemoteAddrLen, 47995, CONTROL_STREAM_TIMEOUT_SEC); if (ctlSock == INVALID_SOCKET) { diff --git a/src/RtspConnection.c b/src/RtspConnection.c index 3e36fcc..4083286 100644 --- a/src/RtspConnection.c +++ b/src/RtspConnection.c @@ -672,6 +672,8 @@ int performRtspHandshake(void) { strcpy(urlAddr, "0.0.0.0"); } + LC_ASSERT(RtspPortNumber != 0); + // Initialize global state useEnet = (AppVersionQuad[0] >= 5) && (AppVersionQuad[0] <= 7) && (AppVersionQuad[2] < 404); sprintf(rtspTargetUrl, "rtsp%s://%s:%u", useEnet ? "ru" : "", urlAddr, RtspPortNumber); @@ -833,7 +835,11 @@ int performRtspHandshake(void) { } // 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 { @@ -890,7 +896,11 @@ int performRtspHandshake(void) { } // Parse the video port out of the RTSP SETUP response + LC_ASSERT(VideoPortNumber == 0); if (!parseServerPortFromTransport(&response, &VideoPortNumber)) { + // Use the well known port if parsing fails + VideoPortNumber = 47998; + Limelog("Video port: %u (RTSP parsing failed)\n", VideoPortNumber); } else { @@ -920,7 +930,11 @@ int performRtspHandshake(void) { } // Parse the control port out of the RTSP SETUP response + LC_ASSERT(ControlPortNumber == 0); if (!parseServerPortFromTransport(&response, &ControlPortNumber)) { + // Use the well known port if parsing fails + ControlPortNumber = 47999; + Limelog("Control port: %u (RTSP parsing failed)\n", ControlPortNumber); } else { diff --git a/src/SdpGenerator.c b/src/SdpGenerator.c index b0f1190..e9c301a 100644 --- a/src/SdpGenerator.c +++ b/src/SdpGenerator.c @@ -133,6 +133,7 @@ static int addGen4Options(PSDP_OPTION* head, char* addrStr) { char payloadStr[92]; int err = 0; + LC_ASSERT(RtspPortNumber != 0); sprintf(payloadStr, "rtsp://%s:%u", addrStr, RtspPortNumber); err |= addAttributeString(head, "x-nv-general.serverAddress", payloadStr); @@ -461,6 +462,7 @@ static int fillSdpHeader(char* buffer, int rtspClientVersion, char*urlSafeAddr) // Populate the SDP tail with required information static int fillSdpTail(char* buffer) { + LC_ASSERT(VideoPortNumber != 0); return sprintf(buffer, "t=0 0\r\n" "m=video %d \r\n", diff --git a/src/VideoStream.c b/src/VideoStream.c index 8244ad0..eac29a3 100644 --- a/src/VideoStream.c +++ b/src/VideoStream.c @@ -46,6 +46,8 @@ static void VideoPingThreadProc(void* context) { char pingData[] = { 0x50, 0x49, 0x4E, 0x47 }; LC_SOCKADDR saddr; + LC_ASSERT(VideoPortNumber != 0); + memcpy(&saddr, &RemoteAddr, sizeof(saddr)); SET_PORT(&saddr, VideoPortNumber);