From 1a1c5dea9eea33553a3727db4e69f537759235e6 Mon Sep 17 00:00:00 2001 From: SergeantPanda Date: Thu, 24 Apr 2025 20:07:36 -0500 Subject: [PATCH] Uses correct api to slected specific stream id. --- apps/proxy/ts_proxy/views.py | 54 +++++++++++++++++++++++------------- frontend/src/api.js | 14 ++++++++++ frontend/src/pages/Stats.jsx | 33 ++++++++++++++++++++-- 3 files changed, 78 insertions(+), 23 deletions(-) diff --git a/apps/proxy/ts_proxy/views.py b/apps/proxy/ts_proxy/views.py index a924cd75..d71da8d4 100644 --- a/apps/proxy/ts_proxy/views.py +++ b/apps/proxy/ts_proxy/views.py @@ -335,14 +335,31 @@ def change_stream(request, channel_id): data = json.loads(request.body) new_url = data.get('url') user_agent = data.get('user_agent') + stream_id = data.get('stream_id') - if not new_url: - return JsonResponse({'error': 'No URL provided'}, status=400) + # If stream_id is provided, get the URL and user_agent from it + if stream_id: + logger.info(f"Stream ID {stream_id} provided, looking up stream info for channel {channel_id}") + stream_info = get_stream_info_for_switch(channel_id, stream_id) - logger.info(f"Attempting to change stream URL for channel {channel_id} to {new_url}") + if 'error' in stream_info: + return JsonResponse({ + 'error': stream_info['error'], + 'stream_id': stream_id + }, status=404) + + # Use the info from the stream + new_url = stream_info['url'] + user_agent = stream_info['user_agent'] + # Stream ID will be passed to change_stream_url later + elif not new_url: + return JsonResponse({'error': 'Either url or stream_id must be provided'}, status=400) + + logger.info(f"Attempting to change stream for channel {channel_id} to {new_url}") # Use the service layer instead of direct implementation - result = ChannelService.change_stream_url(channel_id, new_url, user_agent) + # Pass stream_id to ensure proper connection tracking + result = ChannelService.change_stream_url(channel_id, new_url, user_agent, stream_id) # Get the stream manager before updating URL stream_manager = proxy_server.stream_managers.get(channel_id) @@ -360,22 +377,19 @@ def change_stream(request, channel_id): }, status=404) # Format response based on whether it was a direct update or event-based - if result.get('direct_update'): - return JsonResponse({ - 'message': 'Stream URL updated', - 'channel': channel_id, - 'url': new_url, - 'owner': True, - 'worker_id': proxy_server.worker_id - }) - else: - return JsonResponse({ - 'message': 'Stream URL change requested', - 'channel': channel_id, - 'url': new_url, - 'owner': False, - 'worker_id': proxy_server.worker_id - }) + response_data = { + 'message': 'Stream changed successfully', + 'channel': channel_id, + 'url': new_url, + 'owner': result.get('direct_update', False), + 'worker_id': proxy_server.worker_id + } + + # Include stream_id in response if it was used + if stream_id: + response_data['stream_id'] = stream_id + + return JsonResponse(response_data) except json.JSONDecodeError: return JsonResponse({'error': 'Invalid JSON'}, status=400) diff --git a/frontend/src/api.js b/frontend/src/api.js index b0e2c5d9..38aac846 100644 --- a/frontend/src/api.js +++ b/frontend/src/api.js @@ -1240,6 +1240,20 @@ export default class API { } static async switchStream(channelId, streamId) { + try { + const response = await request(`${host}/proxy/ts/change_stream/${channelId}`, { + method: 'POST', + body: { stream_id: streamId }, + }); + + return response; + } catch (e) { + errorNotification('Failed to switch stream', e); + throw e; + } + } + + static async nextStream(channelId, streamId) { try { const response = await request(`${host}/proxy/ts/next_stream/${channelId}`, { method: 'POST', diff --git a/frontend/src/pages/Stats.jsx b/frontend/src/pages/Stats.jsx index 7d346916..e8f1b7e8 100644 --- a/frontend/src/pages/Stats.jsx +++ b/frontend/src/pages/Stats.jsx @@ -130,13 +130,40 @@ const ChannelCard = ({ channel, clients, stopClient, stopChannel, logos, channel // Handle stream switching const handleStreamChange = async (streamId) => { try { - await API.switchStream(channel.channel_id, streamId); + console.log("Switching to stream ID:", streamId); + // Find the selected stream in availableStreams for debugging + const selectedStream = availableStreams.find(s => s.id.toString() === streamId); + console.log("Selected stream details:", selectedStream); + + // Make sure we're passing the correct ID to the API + const response = await API.switchStream(channel.channel_id, streamId); + console.log("Stream switch API response:", response); + + // Update the local active stream ID immediately + setActiveStreamId(streamId); + + // Show detailed notification with stream name notifications.show({ title: 'Stream switching', - message: `Switching stream for ${channel.name}`, + message: `Switching to "${selectedStream?.name}" for ${channel.name}`, color: 'blue.5', }); + + // After a short delay, fetch streams again to confirm the switch + setTimeout(async () => { + try { + const channelId = channelsByUUID[channel.channel_id]; + if (channelId) { + const updatedStreamData = await API.getChannelStreams(channelId); + console.log("Channel streams after switch:", updatedStreamData); + } + } catch (error) { + console.error("Error checking streams after switch:", error); + } + }, 2000); + } catch (error) { + console.error("Stream switch error:", error); notifications.show({ title: 'Error switching stream', message: error.toString(), @@ -232,7 +259,7 @@ const ChannelCard = ({ channel, clients, stopClient, stopChannel, logos, channel // Create select options for available streams const streamOptions = availableStreams.map(stream => ({ value: stream.id.toString(), - label: stream.name || `Stream #${stream.id}` + label: `${stream.name || `Stream #${stream.id}`}`, // Make sure stream name is clear })); // Debug logging to see what stream_id values we have