Get alternative streams for a channel when the current stream fails. Args: channel_id: The UUID of the channel current_stream_id: The currently failing stream ID to exclude Returns: List[dict]: List of stream information dictionaries with stream_id and profile_
(channel_id: str, current_stream_id: Optional[int] = None)
| 307 | close_old_connections() |
| 308 | |
| 309 | def get_alternate_streams(channel_id: str, current_stream_id: Optional[int] = None) -> List[dict]: |
| 310 | """ |
| 311 | Get alternative streams for a channel when the current stream fails. |
| 312 | |
| 313 | Args: |
| 314 | channel_id: The UUID of the channel |
| 315 | current_stream_id: The currently failing stream ID to exclude |
| 316 | |
| 317 | Returns: |
| 318 | List[dict]: List of stream information dictionaries with stream_id and profile_id |
| 319 | """ |
| 320 | try: |
| 321 | from core.utils import RedisClient |
| 322 | |
| 323 | # Get channel object |
| 324 | channel = get_stream_object(channel_id) |
| 325 | if isinstance(channel, Stream): |
| 326 | logger.error(f"Stream is not a channel") |
| 327 | return [] |
| 328 | |
| 329 | redis_client = RedisClient.get_client() |
| 330 | logger.debug(f"Looking for alternate streams for channel {channel_id}, current stream ID: {current_stream_id}") |
| 331 | |
| 332 | # Get all assigned streams for this channel using the correct ordering |
| 333 | streams = channel.streams.all().order_by('channelstream__order') |
| 334 | logger.debug(f"Channel {channel_id} has {streams.count()} total assigned streams") |
| 335 | |
| 336 | if not streams.exists(): |
| 337 | logger.warning(f"No streams assigned to channel {channel_id}") |
| 338 | return [] |
| 339 | |
| 340 | alternate_streams = [] |
| 341 | |
| 342 | # Process each stream in the user-defined order |
| 343 | for stream in streams: |
| 344 | logger.debug(f"Checking stream ID {stream.id} ({stream.name}) for channel {channel_id}") |
| 345 | |
| 346 | # Skip the current failing stream |
| 347 | if current_stream_id and stream.id == current_stream_id: |
| 348 | logger.debug(f"Skipping current stream ID {current_stream_id}") |
| 349 | continue |
| 350 | |
| 351 | # Find compatible profiles for this stream with connection checking |
| 352 | try: |
| 353 | m3u_account = stream.m3u_account |
| 354 | if not m3u_account: |
| 355 | logger.debug(f"Stream {stream.id} has no M3U account") |
| 356 | continue |
| 357 | if m3u_account.is_active == False: |
| 358 | logger.debug(f"M3U account {m3u_account.id} is inactive, skipping.") |
| 359 | continue |
| 360 | m3u_profiles = m3u_account.profiles.filter(is_active=True) |
| 361 | default_profile = next((obj for obj in m3u_profiles if obj.is_default), None) |
| 362 | |
| 363 | if not default_profile: |
| 364 | logger.debug(f"M3U account {m3u_account.id} has no default profile") |
| 365 | continue |
| 366 |