From d59c8a9e3369ff38a1f2daa24bd2e325283d836d Mon Sep 17 00:00:00 2001 From: SergeantPanda Date: Sun, 27 Apr 2025 15:52:10 -0500 Subject: [PATCH] Properly track current stream id during stream switches. --- apps/proxy/ts_proxy/services/channel_service.py | 5 ++++- apps/proxy/ts_proxy/stream_manager.py | 14 +++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/apps/proxy/ts_proxy/services/channel_service.py b/apps/proxy/ts_proxy/services/channel_service.py index 9aa8c66b..bd1f2f81 100644 --- a/apps/proxy/ts_proxy/services/channel_service.py +++ b/apps/proxy/ts_proxy/services/channel_service.py @@ -181,7 +181,7 @@ class ChannelService: old_url = manager.url # Update the stream - success = manager.update_url(new_url) + success = manager.update_url(new_url, stream_id) logger.info(f"Stream URL changed from {old_url} to {new_url}, result: {success}") result.update({ @@ -440,6 +440,9 @@ class ChannelService: if m3u_profile_id: metadata[ChannelMetadataField.M3U_PROFILE] = str(m3u_profile_id) + # Also update the stream switch time field + metadata[ChannelMetadataField.STREAM_SWITCH_TIME] = str(time.time()) + # Use the appropriate method based on the key type if key_type == 'hash': proxy_server.redis_client.hset(metadata_key, mapping=metadata) diff --git a/apps/proxy/ts_proxy/stream_manager.py b/apps/proxy/ts_proxy/stream_manager.py index 9d2847c1..2615758f 100644 --- a/apps/proxy/ts_proxy/stream_manager.py +++ b/apps/proxy/ts_proxy/stream_manager.py @@ -544,7 +544,7 @@ class StreamManager: # Set running to false to ensure thread exits self.running = False - def update_url(self, new_url): + def update_url(self, new_url, stream_id=None): """Update stream URL and reconnect with proper cleanup for both HTTP and transcode sessions""" if new_url == self.url: logger.info(f"URL unchanged: {new_url}") @@ -568,6 +568,14 @@ class StreamManager: self.url = new_url self.connected = False + # Update stream ID if provided + if stream_id: + old_stream_id = self.current_stream_id + self.current_stream_id = stream_id + # Add stream ID to tried streams for proper tracking + self.tried_stream_ids.add(stream_id) + logger.info(f"Updated stream ID from {old_stream_id} to {stream_id} for channel {self.buffer.channel_id}") + # Reset retry counter to allow immediate reconnect self.retry_count = 0 @@ -1005,7 +1013,7 @@ class StreamManager: logger.info(f"Stream metadata updated for channel {self.channel_id} to stream ID {stream_id}") # IMPORTANT: Just update the URL, don't stop the channel or release resources - switch_result = self.update_url(new_url) + switch_result = self.update_url(new_url, stream_id) if not switch_result: logger.error(f"Failed to update URL for stream ID {stream_id}") return False @@ -1015,4 +1023,4 @@ class StreamManager: except Exception as e: logger.error(f"Error trying next stream for channel {self.channel_id}: {e}", exc_info=True) - return False + return False \ No newline at end of file