Reoo88 commited on
Commit
d40c1ac
·
verified ·
1 Parent(s): 8ede3fd

Recover repeated RTMPS startup failures using safe backup ingest routing

Browse files

Same owned stream and broadcast, primary/backup route recovery with query-safe URL construction. Add redacted handshake diagnostics and prevent debug publish-success messages from being treated as output failures. Preserve credentials, channel, quota and account choices. 376 checks passed.

Files changed (7) hide show
  1. Dockerfile +1 -1
  2. README.md +8 -1
  3. ingest.py +1 -1
  4. relay.py +25 -3
  5. test_ingest_routes.py +76 -0
  6. verify.sh +1 -0
  7. youtube_manager.py +45 -7
Dockerfile CHANGED
@@ -6,7 +6,7 @@ WORKDIR /app
6
  COPY requirements.txt .
7
  RUN pip install --no-cache-dir -U -r requirements.txt
8
  COPY app.py relay.py youtube_api.py youtube_manager.py flv_bridge.py stream_monitor.py tiktok_auth.py ingest.py index.html ./
9
- COPY verify.sh verify_tokens.py probe_live.py test_watchdog.py test_lifecycle.py test_relay.py test_source_reconnect.py test_concurrency.py test_flv_bridge.py test_regressions.py test_completion.py test_channel_switch.py test_dashboard.js ./
10
 
11
  # TikTok Live Monitor → Direct Relay → YouTube. No local video files.
12
  ENV TZ=Asia/Baghdad \
 
6
  COPY requirements.txt .
7
  RUN pip install --no-cache-dir -U -r requirements.txt
8
  COPY app.py relay.py youtube_api.py youtube_manager.py flv_bridge.py stream_monitor.py tiktok_auth.py ingest.py index.html ./
9
+ COPY verify.sh verify_tokens.py probe_live.py test_watchdog.py test_lifecycle.py test_relay.py test_source_reconnect.py test_ingest_routes.py test_concurrency.py test_flv_bridge.py test_regressions.py test_completion.py test_channel_switch.py test_dashboard.js ./
10
 
11
  # TikTok Live Monitor → Direct Relay → YouTube. No local video files.
12
  ENV TZ=Asia/Baghdad \
README.md CHANGED
@@ -8,7 +8,14 @@ pinned: false
8
  app_port: 7860
9
  ---
10
 
11
- # Reoo88/On — relay-v2.23-source-handover
 
 
 
 
 
 
 
12
 
13
  ## إصلاح انقطاع المصدر في v2.23
14
 
 
8
  app_port: 7860
9
  ---
10
 
11
+ # Reoo88/On — relay-v2.24-ingest-recovery
12
+
13
+ ## استعادة استقبال يوتيوب في v2.24
14
+
15
+ عند فشل خرج RTMPS المتكرر تُجرّب نقطة الاستقبال الاحتياطية الآمنة لنفس مصدر يوتيوب، لا قناة أو مفتاحًا أو حدثًا جديدًا. لا يُستعمل مسار محفوظ بعد تبديل القناة أو معرّف المصدر. تبقى query الخاصة بالاحتياطي بعد المفتاح في الرابط الصحيح، وتبقى عناوين الإرسال ومفاتيحها في الذاكرة فقط.
16
+
17
+ فشل بدء الخرج يفعّل تشخيص تفاوض RTMP للمحاولة التالية، مع إخفاء `playpath` من رسالة `Sending publish command` حتى إن لم يظهر كـURL. رسائل النجاح `NetStream.Publish.Start` و`FCPublish` لا تُعتبر رفضًا للنشر. التشخيص لا يُغيّر توكنات أو إعدادات نشر المستخدم.
18
+
19
 
20
  ## إصلاح انقطاع المصدر في v2.23
21
 
ingest.py CHANGED
@@ -6,7 +6,7 @@ import os, threading, time
6
  from datetime import datetime
7
  from zoneinfo import ZoneInfo
8
 
9
- APP_VERSION = 'relay-v2.23-source-handover'
10
 
11
 
12
  def env_rtmp_destination():
 
6
  from datetime import datetime
7
  from zoneinfo import ZoneInfo
8
 
9
+ APP_VERSION = 'relay-v2.24-ingest-recovery'
10
 
11
 
12
  def env_rtmp_destination():
relay.py CHANGED
@@ -73,6 +73,8 @@ def _error_interesting(text):
73
  def classify_ffmpeg_error(text):
74
  """أول وصف ينطبق على سطر من stderr، أو None. تصنيف نصي خالص بلا أي روابط."""
75
  low = (text or '').lower()
 
 
76
  for needle, label in FFMPEG_ERROR_LABELS:
77
  if needle.lower() in low:
78
  return label
@@ -87,14 +89,21 @@ def ffmpeg_output_failure(text):
87
  never refreshes the YouTube destination or normalizes the feed.
88
  """
89
  low=(text or '').lower()
90
- return any(x in low for x in (
 
 
91
  'error opening output', 'error writing', 'broken pipe', 'netstream.publish',
92
- 'publish stream', 'server error, publish', '[out#'))
 
 
93
 
94
 
95
  def redact_stderr(text):
96
  """يُبقي سبب الخطأ ويخفي كل ما لا يجوز قراءته في سجل عام: الروابط والمفاتيح والرموز."""
97
  t = re.sub(r'\b(?:rtmps?|rtsp|srt|https?)://\S+', '«رابط-مخفي»', text or '')
 
 
 
98
  t = re.sub(r'(?i)([?&=]|^|\s)((?:key|token|auth|sessionid|sign)\s*=\s*)[^\s&]+', r'\1\2«مخفي»', t)
99
  return t.strip()[:240]
100
 
@@ -128,6 +137,7 @@ class Sender:
128
  self.last_diagnostics=[] # آخر الأوصاف العربية المصنَّفة، تُعرض في اللوحة كما في السجل
129
  self.last_source_gap=0.0 # صمت المصدر لحظة التفكيك: يفصل جوع المصدر عن رفض الوجهة
130
  self.last_failure_side='' # 'source' or 'output'; stderr/backpressure outranks an aged meter
 
131
  self.active_mode='' # exact FFmpeg mode, safe to expose in operational logs/state
132
  # Timestamps of source resolution changes seen by this sender (anti-flap window).
133
  self.flap=deque()
@@ -543,7 +553,7 @@ class Sender:
543
  else:
544
  output=['-flvflags','no_duration_filesize','-f','flv']
545
  ta=self.input_args(candidate)
546
- return [self.ffmpeg,'-hide_banner','-loglevel',os.getenv('YOUTUBE_FFMPEG_LOGLEVEL','error'),
547
  '-nostats','-progress','pipe:1',
548
  *ta['input'],'-map','0:v:0','-map','0:a:0?',
549
  # TikTok streams HE-AAC (SBR) at 64 kbit/s — measured on the live source. Copied
@@ -946,6 +956,13 @@ class Sender:
946
  try:
947
  for line in iter(p.stderr.readline, b''):
948
  text = line.decode('utf-8', 'replace')
 
 
 
 
 
 
 
949
  if ffmpeg_output_failure(text):
950
  sess['output_failure'] = True
951
  self.last_failure_side = 'output'
@@ -1146,6 +1163,11 @@ class Sender:
1146
  if self.last_failure_side=='output' and not sess.get('connection_event'):
1147
  log_event(user,'CONNECTION_ERROR',{'scope':'youtube_rtmp','reason':_why[:300],
1148
  'ffmpeg_exit_code':p.poll()});sess['connection_event']=True
 
 
 
 
 
1149
  self._teardown(user, p, response, sess, stop, _why)
1150
  if error_reader:
1151
  error_reader.join(timeout=1)
 
73
  def classify_ffmpeg_error(text):
74
  """أول وصف ينطبق على سطر من stderr، أو None. تصنيف نصي خالص بلا أي روابط."""
75
  low = (text or '').lower()
76
+ if 'netstream.publish.start' in low or 'fcpublish stream...' in low:
77
+ return None # Successful/debug publish status is not a rejection.
78
  for needle, label in FFMPEG_ERROR_LABELS:
79
  if needle.lower() in low:
80
  return label
 
89
  never refreshes the YouTube destination or normalizes the feed.
90
  """
91
  low=(text or '').lower()
92
+ if 'netstream.publish.start' in low or 'fcpublish stream...' in low:
93
+ return False
94
+ return (any(x in low for x in (
95
  'error opening output', 'error writing', 'broken pipe', 'netstream.publish',
96
+ 'server error, publish')) or
97
+ (('[out#' in low or 'publish stream' in low) and
98
+ any(x in low for x in ('error', 'failed', 'rejected', 'denied', 'unable', 'broken pipe'))))
99
 
100
 
101
  def redact_stderr(text):
102
  """يُبقي سبب الخطأ ويخفي كل ما لا يجوز قراءته في سجل عام: الروابط والمفاتيح والرموز."""
103
  t = re.sub(r'\b(?:rtmps?|rtsp|srt|https?)://\S+', '«رابط-مخفي»', text or '')
104
+ # FFmpeg debug prints the RTMP playpath WITHOUT its URL scheme.
105
+ t = re.sub(r"(?i)((?:publish|play) command for\s+)'[^']*'", r"\1'«مفتاح-مخفي»'", t)
106
+ t = re.sub(r'(?i)(\b(?:fname|playpath)\s*=\s*)[^,\s]+', r'\1«مفتاح-مخفي»', t)
107
  t = re.sub(r'(?i)([?&=]|^|\s)((?:key|token|auth|sessionid|sign)\s*=\s*)[^\s&]+', r'\1\2«مخفي»', t)
108
  return t.strip()[:240]
109
 
 
137
  self.last_diagnostics=[] # آخر الأوصاف العربية المصنَّفة، تُعرض في اللوحة كما في السجل
138
  self.last_source_gap=0.0 # صمت المصدر لحظة التفكيك: يفصل جوع المصدر عن رفض الوجهة
139
  self.last_failure_side='' # 'source' or 'output'; stderr/backpressure outranks an aged meter
140
+ self.trace_rtmp=False # One failed startup enables redacted handshake diagnostics.
141
  self.active_mode='' # exact FFmpeg mode, safe to expose in operational logs/state
142
  # Timestamps of source resolution changes seen by this sender (anti-flap window).
143
  self.flap=deque()
 
553
  else:
554
  output=['-flvflags','no_duration_filesize','-f','flv']
555
  ta=self.input_args(candidate)
556
+ return [self.ffmpeg,'-hide_banner','-loglevel',('debug' if network and self.trace_rtmp else os.getenv('YOUTUBE_FFMPEG_LOGLEVEL','error')),
557
  '-nostats','-progress','pipe:1',
558
  *ta['input'],'-map','0:v:0','-map','0:a:0?',
559
  # TikTok streams HE-AAC (SBR) at 64 kbit/s — measured on the live source. Copied
 
956
  try:
957
  for line in iter(p.stderr.readline, b''):
958
  text = line.decode('utf-8', 'replace')
959
+ low = text.lower()
960
+ if 'handshak' in low:
961
+ self.set(rtmp_phase='handshake')
962
+ elif 'fcpublish stream' in low or 'sending publish command' in low:
963
+ self.set(rtmp_phase='publish')
964
+ elif 'netstream.publish.start' in low or 'publishing stream at bitrates' in low:
965
+ self.set(rtmp_phase='accepted')
966
  if ffmpeg_output_failure(text):
967
  sess['output_failure'] = True
968
  self.last_failure_side = 'output'
 
1163
  if self.last_failure_side=='output' and not sess.get('connection_event'):
1164
  log_event(user,'CONNECTION_ERROR',{'scope':'youtube_rtmp','reason':_why[:300],
1165
  'ffmpeg_exit_code':p.poll()});sess['connection_event']=True
1166
+ if destination.startswith(('rtmp://','rtmps://')) and progress['seconds'] <= 0:
1167
+ self.log(user, '[RTMP] لم يصل أول إطار؛ مرحلة=%s؛ دخل=%d بايت؛ pipe_busy=%s' %
1168
+ (self.data.get('rtmp_phase') or 'opening', self.data.get('source_bytes',0), bool(sess.get('writing'))))
1169
+ for line in self.last_errors[-8:]:
1170
+ self.log(user, '[RTMP] ' + redact_stderr(line))
1171
  self._teardown(user, p, response, sess, stop, _why)
1172
  if error_reader:
1173
  error_reader.join(timeout=1)
test_ingest_routes.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Secure primary/backup ingest and diagnostic redaction; no real keys or cloud calls."""
3
+ import os
4
+ import pathlib
5
+ import sys
6
+ import tempfile
7
+ import unittest
8
+ from urllib.parse import urlparse
9
+ from unittest.mock import patch
10
+ ROOT=pathlib.Path(sys.argv[1]).resolve() if len(sys.argv)>1 else pathlib.Path(__file__).parent
11
+ sys.path.insert(0,str(ROOT))
12
+ from youtube_manager import ManagedYouTube
13
+ from relay import Sender,redact_stderr,ffmpeg_output_failure,classify_ffmpeg_error
14
+
15
+ class IngestRouteTests(unittest.TestCase):
16
+ def setUp(self):
17
+ self.tmp=tempfile.TemporaryDirectory();self.addCleanup(self.tmp.cleanup)
18
+ self.mgr=ManagedYouTube('/bin/true',lambda u:None,lambda *a:None,lambda:['alice'],
19
+ pathlib.Path(self.tmp.name)/'journal.json')
20
+ self.mgr.journal.change(lambda d:d.update(channel={'id':'CHANNEL'},streams={'alice':{'id':'STREAM'}}))
21
+ self.primary='rtmps://a.rtmps.youtube.com/live2/test-ingest-only'
22
+ self.backup='rtmps://b.rtmps.youtube.com:443/live2/test-ingest-only?backup=1'
23
+ self.mgr._ingest_routes['alice']={'urls':[self.primary,self.backup], 'channel_id':'CHANNEL','stream_id':'STREAM'}
24
+ self.mgr.destinations['alice']=self.primary
25
+
26
+ def test_backup_query_follows_key_not_the_other_way_around(self):
27
+ url=self.mgr.secure_ingest_url('rtmps://b.rtmps.youtube.com:443/live2?backup=1','test-key')
28
+ p=urlparse(url);self.assertEqual(p.path,'/live2/test-key');self.assertEqual(p.query,'backup=1')
29
+
30
+ def test_plaintext_foreign_and_userinfo_addresses_are_rejected(self):
31
+ for address in ('rtmp://a.rtmp.youtube.com/live2','rtmps://attacker.invalid/live2',
32
+ 'rtmps://youtube.com.attacker.invalid/live2','rtmps://user:password@a.rtmps.youtube.com/live2'):
33
+ with self.assertRaises(RuntimeError):self.mgr.secure_ingest_url(address,'test-key')
34
+
35
+ def test_route_switch_preserves_key_event_and_quota_state(self):
36
+ before=self.mgr.journal.read()
37
+ self.assertTrue(self.mgr.switch_ingest_route('alice'))
38
+ self.assertEqual(self.mgr.destinations['alice'],self.backup)
39
+ self.assertEqual(self.mgr.journal.read(),before)
40
+
41
+ def test_route_is_not_reused_after_channel_or_stream_change(self):
42
+ self.mgr.journal.change(lambda d:d.update(channel={'id':'DIFFERENT_CHANNEL'}))
43
+ self.assertFalse(self.mgr.switch_ingest_route('alice'))
44
+ self.mgr.journal.change(lambda d:d.update(channel={'id':'CHANNEL'},streams={'alice':{'id':'DIFFERENT_STREAM'}}))
45
+ self.assertFalse(self.mgr.switch_ingest_route('alice'))
46
+
47
+ def test_no_second_route_means_no_change(self):
48
+ self.mgr._ingest_routes['alice']['urls']=[self.primary]
49
+ self.assertFalse(self.mgr.switch_ingest_route('alice'))
50
+ self.assertEqual(self.mgr.destinations['alice'],self.primary)
51
+
52
+ class DiagnosticTests(unittest.TestCase):
53
+ def test_publish_command_hides_playpath_even_without_url(self):
54
+ for text in ("Sending publish command for 'SECRET-STREAM-NAME'", "Sending play command for 'SECRET-STREAM-NAME'",
55
+ 'Proto=rtmps, fname=SECRET-STREAM-NAME, app=live2'):
56
+ self.assertNotIn('SECRET-STREAM-NAME',redact_stderr(text))
57
+
58
+ def test_debug_success_messages_are_not_output_failures(self):
59
+ for line in ('NetStream.Publish.Start','FCPublish stream...', '[out#0/flv] Output stream created'):
60
+ self.assertFalse(ffmpeg_output_failure(line))
61
+ self.assertIsNone(classify_ffmpeg_error('NetStream.Publish.Start'))
62
+
63
+ def test_real_output_failures_still_classify(self):
64
+ for line in ('NetStream.Publish.BadName','[out#0/flv] Error opening output: End of file',
65
+ 'Broken pipe','Failed to publish stream'):
66
+ self.assertTrue(ffmpeg_output_failure(line))
67
+
68
+ def test_handshake_trace_is_opt_in_and_local_file_stays_normal(self):
69
+ s=Sender('/bin/true',lambda *a:None);s.mode_override='copy';s.trace_rtmp=True
70
+ with patch.dict(os.environ,{'YOUTUBE_FFMPEG_LOGLEVEL':'info'}):
71
+ args=s.command('rtmps://a.rtmps.youtube.com/live2/test-key',{'codec':'h264'})
72
+ self.assertEqual(args[args.index('-loglevel')+1],'debug')
73
+ local=s.command('out.flv',{'codec':'h264'})
74
+ self.assertEqual(local[local.index('-loglevel')+1],'info')
75
+
76
+ if __name__=='__main__':unittest.main(argv=[sys.argv[0]],verbosity=2)
verify.sh CHANGED
@@ -27,6 +27,7 @@ fi
27
  python3 test_regressions.py "$ROOT"
28
  python3 test_relay.py "$ROOT"
29
  python3 test_source_reconnect.py "$ROOT"
 
30
  python3 test_flv_bridge.py "$ROOT"
31
  python3 test_completion.py "$ROOT"
32
  # Optional heavy tests when ffmpeg exists
 
27
  python3 test_regressions.py "$ROOT"
28
  python3 test_relay.py "$ROOT"
29
  python3 test_source_reconnect.py "$ROOT"
30
+ python3 test_ingest_routes.py "$ROOT"
31
  python3 test_flv_bridge.py "$ROOT"
32
  python3 test_completion.py "$ROOT"
33
  # Optional heavy tests when ffmpeg exists
youtube_manager.py CHANGED
@@ -5,7 +5,7 @@ Fail closed on ambiguous remote creates."""
5
  import os, re, subprocess, threading, time, uuid
6
  from datetime import datetime, timezone, timedelta
7
  from zoneinfo import ZoneInfo
8
- from urllib.parse import urlparse
9
  from curl_cffi import requests
10
  from relay import RelayStartError, Sender, SourceStalled, source_http_headers
11
  from stream_monitor import log_event
@@ -31,6 +31,7 @@ class ManagedYouTube:
31
  self.manager_thread=None;self.manager_stop=threading.Event()
32
  self.senders={} # session_key -> {'thread','stop','sender'}
33
  self.destinations={} # username -> rtmps url (in-memory only, never persisted)
 
34
  self.scan=max(8,int(os.getenv('YOUTUBE_SCAN_SECONDS') or os.getenv('CHECK_INTERVAL','20')))
35
  self.end_grace=max(15,int(os.getenv('YOUTUBE_END_GRACE','40')))
36
  self._unstable={}
@@ -348,7 +349,7 @@ class ManagedYouTube:
348
  dd.update(channel=dict(new),enabled=False,events={},streams={})
349
  # One atomic journal replacement; no YouTube event update/delete/transition call.
350
  self.journal.change(change)
351
- self.destinations.clear();self._blocked403.clear();self.retry.clear()
352
  self.clear_pacing(None,'تبديل القناة المفوضة')
353
  self.last_logged=None
354
  self.set_status('القناة الجديدة معتمدة؛ الأتمتة معطلة حتى تفعيلها')
@@ -418,6 +419,7 @@ class ManagedYouTube:
418
  # يظن المشاهد أن البث ميت بينما الجلسة تعمل (ما حدث في تجمّد @viggoo313).
419
  # صمت المصدر لحظة آخر قفل: يشرح في اللوحة سبب 187 (جوع تغذية لا رفض مستقبِل).
420
  'source_age_seconds':s.get('source_age'),'source_gap':s.get('source_gap'),
 
421
  # آخر سطور stderr من FFmpeg بعد إخفاء الرابط والمفتاح: «لماذا أُغلقت الجلسة» لا يُترك تخمينًا
422
  'ffmpeg_errors':list(s.get('ffmpeg_errors') or []),
423
  'ffmpeg_diagnostics':list(s.get('ffmpeg_diagnostics') or []),
@@ -501,6 +503,32 @@ class ManagedYouTube:
501
  markers=[self.marker()+'-STREAM-'+user]
502
  if legacy:markers.append(self.marker()+'-STREAM') # stream migrated from V6/V7
503
  return markers
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
504
  def ensure_stream(self,user):
505
  d=self.journal.read();st=d['streams'].get(user) or {}
506
  markers=self.stream_markers(user,bool(st.get('legacy')))
@@ -538,10 +566,16 @@ class ManagedYouTube:
538
  if not secure:
539
  # YouTube's secure endpoint; streamName remains in memory only.
540
  secure='rtmps://a.rtmps.youtube.com:443/live2'
541
- p=urlparse(secure)
542
- if p.scheme!='rtmps' or not p.hostname or not p.hostname.endswith('.youtube.com') or not key:
543
- raise RuntimeError('لم يُرجع يوتيوب عنوان إرسال آمنًا صالحًا')
544
- self.destinations[user]=secure.rstrip('/')+'/'+key
 
 
 
 
 
 
545
  return stream
546
 
547
  # ---------- events ----------
@@ -1069,6 +1103,7 @@ class ManagedYouTube:
1069
  n = self.hammer.get(user, 0) + 1
1070
  self.hammer[user] = n
1071
  if output_side and n == 1:
 
1072
  # First confirmed output-side failure: re-read the API-owned ingest/key before
1073
  # changing codecs. This is cheap, safe, and rules out stale in-memory routing.
1074
  self.destinations.pop(user, None)
@@ -1077,7 +1112,10 @@ class ManagedYouTube:
1077
  self.log(user, '[WATCHDOG] فشل خرج RTMPS؛ أُعيدت قراءة عنوان الاستقبال ومفتاحه من YouTube API')
1078
  except Exception as e:
1079
  self.log(user, '[WATCHDOG] تعذر تحديث عنوان الاستقبال بعد فشل الخرج: '+self.safe_error(e)[:100])
1080
- if output_side and n == 2:
 
 
 
1081
  try:snap=sender.snapshot() or {}
1082
  except Exception:snap={}
1083
  mode=str(snap.get('send_mode') or sender.send_mode() or '')
 
5
  import os, re, subprocess, threading, time, uuid
6
  from datetime import datetime, timezone, timedelta
7
  from zoneinfo import ZoneInfo
8
+ from urllib.parse import urlparse, urlunparse, quote
9
  from curl_cffi import requests
10
  from relay import RelayStartError, Sender, SourceStalled, source_http_headers
11
  from stream_monitor import log_event
 
31
  self.manager_thread=None;self.manager_stop=threading.Event()
32
  self.senders={} # session_key -> {'thread','stop','sender'}
33
  self.destinations={} # username -> rtmps url (in-memory only, never persisted)
34
+ self._ingest_routes={};self._ingest_preferred={}
35
  self.scan=max(8,int(os.getenv('YOUTUBE_SCAN_SECONDS') or os.getenv('CHECK_INTERVAL','20')))
36
  self.end_grace=max(15,int(os.getenv('YOUTUBE_END_GRACE','40')))
37
  self._unstable={}
 
349
  dd.update(channel=dict(new),enabled=False,events={},streams={})
350
  # One atomic journal replacement; no YouTube event update/delete/transition call.
351
  self.journal.change(change)
352
+ self.destinations.clear();self._ingest_routes.clear();self._ingest_preferred.clear();self._blocked403.clear();self.retry.clear()
353
  self.clear_pacing(None,'تبديل القناة المفوضة')
354
  self.last_logged=None
355
  self.set_status('القناة الجديدة معتمدة؛ الأتمتة معطلة حتى تفعيلها')
 
419
  # يظن المشاهد أن البث ميت بينما الجلسة تعمل (ما حدث في تجمّد @viggoo313).
420
  # صمت المصدر لحظة آخر قفل: يشرح في اللوحة سبب 187 (جوع تغذية لا رفض مستقبِل).
421
  'source_age_seconds':s.get('source_age'),'source_gap':s.get('source_gap'),
422
+ 'rtmp_phase':s.get('rtmp_phase',''),
423
  # آخر سطور stderr من FFmpeg بعد إخفاء الرابط والمفتاح: «لماذا أُغلقت الجلسة» لا يُترك تخمينًا
424
  'ffmpeg_errors':list(s.get('ffmpeg_errors') or []),
425
  'ffmpeg_diagnostics':list(s.get('ffmpeg_diagnostics') or []),
 
503
  markers=[self.marker()+'-STREAM-'+user]
504
  if legacy:markers.append(self.marker()+'-STREAM') # stream migrated from V6/V7
505
  return markers
506
+ @staticmethod
507
+ def secure_ingest_url(address,key):
508
+ p=urlparse(str(address or ''))
509
+ if (p.scheme!='rtmps' or not p.hostname or not p.hostname.endswith('.youtube.com')
510
+ or p.username or p.password or not key):
511
+ raise RuntimeError('لم يُرجع يوتيوب عنوان إرسال آمنًا صالحًا')
512
+ # Official backup addresses carry ?backup=1. The key belongs in the PATH,
513
+ # not after the query string, and neither endpoint nor key is persisted.
514
+ return urlunparse((p.scheme,p.netloc,p.path.rstrip('/')+'/'+quote(str(key),safe=''),'',p.query,''))
515
+
516
+ def switch_ingest_route(self,user):
517
+ routes=self._ingest_routes.get(user) or {}
518
+ d=self.journal.read()
519
+ if (routes.get('channel_id')!=(d.get('channel') or {}).get('id') or
520
+ routes.get('stream_id')!=(d.get('streams',{}).get(user) or {}).get('id')):
521
+ return False # Do not reuse a route across a channel/stream ownership change.
522
+ choices=routes.get('urls') or []
523
+ if len(choices)<2:return False
524
+ current=self.destinations.get(user)
525
+ alt=next((u for u in choices if u!=current),None)
526
+ if not alt:return False
527
+ self._ingest_preferred[user]=choices.index(alt)
528
+ self.destinations[user]=alt
529
+ self.log(user,'[RECOVERY] تجربة مستقبل RTMPS البديل من يوتيوب بنفس المصدر والحدث؛ بلا إنشاء مفتاح جديد')
530
+ return True
531
+
532
  def ensure_stream(self,user):
533
  d=self.journal.read();st=d['streams'].get(user) or {}
534
  markers=self.stream_markers(user,bool(st.get('legacy')))
 
566
  if not secure:
567
  # YouTube's secure endpoint; streamName remains in memory only.
568
  secure='rtmps://a.rtmps.youtube.com:443/live2'
569
+ primary=self.secure_ingest_url(secure,key)
570
+ backup_address=ingest.get('rtmpsBackupIngestionAddress')
571
+ if not backup_address:
572
+ backup_address='rtmps://b.rtmps.youtube.com:443/live2?backup=1'
573
+ backup=self.secure_ingest_url(backup_address,key)
574
+ choices=list(dict.fromkeys((primary,backup)))
575
+ self._ingest_routes[user]={'urls':choices,'stream_id':sid,
576
+ 'channel_id':(dnow.get('channel') or {}).get('id')}
577
+ index=min(int(self._ingest_preferred.get(user,0)),len(choices)-1)
578
+ self.destinations[user]=choices[index]
579
  return stream
580
 
581
  # ---------- events ----------
 
1103
  n = self.hammer.get(user, 0) + 1
1104
  self.hammer[user] = n
1105
  if output_side and n == 1:
1106
+ sender.trace_rtmp=True # Redacted handshake evidence on the next startup.
1107
  # First confirmed output-side failure: re-read the API-owned ingest/key before
1108
  # changing codecs. This is cheap, safe, and rules out stale in-memory routing.
1109
  self.destinations.pop(user, None)
 
1112
  self.log(user, '[WATCHDOG] فشل خرج RTMPS؛ أُعيدت قراءة عنوان الاستقبال ومفتاحه من YouTube API')
1113
  except Exception as e:
1114
  self.log(user, '[WATCHDOG] تعذر تحديث عنوان الاستقبال بعد فشل الخرج: '+self.safe_error(e)[:100])
1115
+ if output_side and n == 2 and self.switch_ingest_route(user):
1116
+ self.retry[user]=self.retry_min
1117
+ return
1118
+ if output_side and (n == 2 or (n == 3 and self._ingest_preferred.get(user))):
1119
  try:snap=sender.snapshot() or {}
1120
  except Exception:snap={}
1121
  mode=str(snap.get('send_mode') or sender.send_mode() or '')