MCPcopy Create free account
hub / github.com/Dispatcharr/Dispatcharr / stream_endpoint

Method stream_endpoint

apps/proxy/hls_proxy/server.py:876–959  ·  view source on GitHub ↗
(self, channel_id: str)

Source from the content-addressed store, hash-verified

874
875 # Update methods to return data instead of Flask Response objects
876 def stream_endpoint(self, channel_id: str):
877 if channel_id not in self.stream_managers:
878 return 'Channel not found', 404
879
880 manager = self.stream_managers[channel_id]
881
882 # Wait for initial buffer
883 if not manager.buffer_ready.wait(Config.BUFFER_READY_TIMEOUT):
884 logging.error(f"Timeout waiting for initial buffer for channel {channel_id}")
885 return 'Initial buffer not ready', 503
886
887 try:
888 if (channel_id not in self.stream_managers) or (not self.stream_managers[channel_id].running):
889 return 'Channel not found', 404
890
891 manager = self.stream_managers[channel_id]
892 buffer = self.stream_buffers[channel_id]
893
894 # Record client activity and enable cleanup
895 client_ip = request.remote_addr
896 manager.enable_cleanup()
897 self.client_managers[channel_id].record_activity(client_ip)
898
899 # Wait for first segment with timeout
900 start_time = time.time()
901 while True:
902 with buffer.lock:
903 available = sorted(buffer.keys())
904 if available:
905 break
906
907 if time.time() - start_time > Config.FIRST_SEGMENT_TIMEOUT:
908 logging.warning(f"Timeout waiting for first segment for channel {channel_id}")
909 return 'No segments available', 503
910
911 time.sleep(0.1) # Short sleep to prevent CPU spinning
912
913 # Rest of manifest generation code...
914 with buffer.lock:
915 max_seq = max(available)
916 # Find the first segment after any discontinuity
917 discontinuity_start = min(available)
918 for seq in available:
919 if seq in manager.source_changes:
920 discontinuity_start = seq
921 break
922
923 # Calculate window bounds starting from discontinuity
924 if len(available) <= Config.INITIAL_SEGMENTS:
925 min_seq = discontinuity_start
926 else:
927 min_seq = max(
928 discontinuity_start,
929 max_seq - Config.WINDOW_SIZE + 1
930 )
931
932 # Build manifest with proper tags
933 new_manifest = ['#EXTM3U']

Callers 1

stream_endpointFunction · 0.80

Calls 5

enable_cleanupMethod · 0.80
record_activityMethod · 0.80
keysMethod · 0.80
waitMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected