Reoo88 commited on
Commit
c201b90
·
verified ·
1 Parent(s): 9d0318c

Keyframe-gated resume + fill-credit write deadline (v2.34-keyframe-gate)

Browse files
Files changed (5) hide show
  1. flv_bridge.py +26 -0
  2. ingest.py +1 -1
  3. relay.py +9 -3
  4. test_flv_bridge.py +36 -8
  5. test_smart_mode.py +8 -0
flv_bridge.py CHANGED
@@ -79,6 +79,8 @@ class FLVBridge:
79
  self._video_samples=deque();self._bitrate_kbps=None;self._bitrate_check_ts=None
80
  self._last_keyframe_ts=None;self._keyframe_interval=None;self._keyframe_event_ts=None
81
  self._last_keyframe_tag=None # آخر وسم إطار مفتاحي مُرسل كاملًا: مادة حشو الفجوات
 
 
82
  def _event(self,event,details):
83
  try:self.event_logger(self.user,event,details)
84
  except Exception:pass
@@ -117,6 +119,10 @@ class FLVBridge:
117
  self.skip_script=True
118
  self.skip_same_cfg=True
119
  self.ts_bias_pending=self.last_out_ts is not None
 
 
 
 
120
  self._event('RECONNECT',{'scope':'tiktok_http','outbound_rtmp_reused':True,
121
  'last_output_dts_ms':self.last_out_ts})
122
  def freeze_bytes(self,step_ms=2000):
@@ -192,6 +198,10 @@ class FLVBridge:
192
  self._event('STREAM_CONFIG',{'codec':codec,'width':new_size[0] if new_size else None,
193
  'height':new_size[1] if new_size else None,'config_bytes':len(signature)-4})
194
  self.video_cfg=signature;self.video_codec=codec;self.video_size=new_size
 
 
 
 
195
  # Only the first identical record after reconnect is redundant. A changed
196
  # record is emitted now, before any dependent coded frame in this input.
197
  if self.skip_same_cfg:
@@ -214,6 +224,22 @@ class FLVBridge:
214
  coded=(enhanced and packet in (1,3)) or (not enhanced and codec_id==7 and packet==1)
215
  is_keyframe=coded and ((body[0]>>4)&7)==1
216
  if coded:self._observe_frame(out_ts,len(body),is_keyframe)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  header[1:4]=len(body).to_bytes(3,'big')
218
  tag_bytes=bytes(header)+body+(11+len(body)).to_bytes(4,'big')
219
  if is_keyframe:self._last_keyframe_tag=tag_bytes
 
79
  self._video_samples=deque();self._bitrate_kbps=None;self._bitrate_check_ts=None
80
  self._last_keyframe_ts=None;self._keyframe_interval=None;self._keyframe_event_ts=None
81
  self._last_keyframe_tag=None # آخر وسم إطار مفتاحي مُرسل كاملًا: مادة حشو الفجوات
82
+ self._await_keyframe=False # بوّابة الاستئناف: لا إطارات مرجعية حتى أول مفتاحي نظيف
83
+ self._gate_dropped=0
84
  def _event(self,event,details):
85
  try:self.event_logger(self.user,event,details)
86
  except Exception:pass
 
119
  self.skip_script=True
120
  self.skip_same_cfg=True
121
  self.ts_bias_pending=self.last_out_ts is not None
122
+ # الخياطة الجديدة قد تبدأ من منتصف GOP: إطاراتها المرجعية تشير إلى صور لم
123
+ # تصل يوتيوب/المُرمِّز قط، وتمريرها يرسم رصاصيًا وألوان تشويش عند المشاهدين
124
+ # حتى أول مفتاحي. البوّابة تسقطها وتبقي الصورة المجمّدة/الأخيرة حتى مفتاحي نظيف.
125
+ self._await_keyframe=True;self._gate_dropped=0
126
  self._event('RECONNECT',{'scope':'tiktok_http','outbound_rtmp_reused':True,
127
  'last_output_dts_ms':self.last_out_ts})
128
  def freeze_bytes(self,step_ms=2000):
 
198
  self._event('STREAM_CONFIG',{'codec':codec,'width':new_size[0] if new_size else None,
199
  'height':new_size[1] if new_size else None,'config_bytes':len(signature)-4})
200
  self.video_cfg=signature;self.video_codec=codec;self.video_size=new_size
201
+ if changed:
202
+ # إعداد فكّ جديد من نفس الجلسة: الإطارات التالية قد ترجع إلى صور
203
+ # مشفّرة بالإعداد القديم — نفس بوّابة المفتاحي حتى IDR نظيف.
204
+ self._await_keyframe=True;self._gate_dropped=0
205
  # Only the first identical record after reconnect is redundant. A changed
206
  # record is emitted now, before any dependent coded frame in this input.
207
  if self.skip_same_cfg:
 
224
  coded=(enhanced and packet in (1,3)) or (not enhanced and codec_id==7 and packet==1)
225
  is_keyframe=coded and ((body[0]>>4)&7)==1
226
  if coded:self._observe_frame(out_ts,len(body),is_keyframe)
227
+ if self._await_keyframe and kind==9 and coded:
228
+ if is_keyframe:
229
+ self._await_keyframe=False
230
+ self._event('KEYFRAME_RESYNC',{'dropped_tags':self._gate_dropped,'capped':False})
231
+ self._gate_dropped=0
232
+ else:
233
+ self._gate_dropped+=1
234
+ if self._gate_dropped>=300:
235
+ # مصدر شاذ بلا مفتاحي قادم: لا نجاعة في تجويع الفيديو أكثر من
236
+ # ~10 ثوانٍ؛ نفتح ونترك أول مفتاحي حقيقي ينظّف الصورة.
237
+ self._await_keyframe=False
238
+ self._event('KEYFRAME_RESYNC',{'dropped_tags':self._gate_dropped,'capped':True})
239
+ self._gate_dropped=0
240
+ else:
241
+ offset+=total
242
+ continue
243
  header[1:4]=len(body).to_bytes(3,'big')
244
  tag_bytes=bytes(header)+body+(11+len(body)).to_bytes(4,'big')
245
  if is_keyframe:self._last_keyframe_tag=tag_bytes
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.33-format-lock-fillguard'
10
 
11
 
12
  def env_rtmp_destination():
 
6
  from datetime import datetime
7
  from zoneinfo import ZoneInfo
8
 
9
+ APP_VERSION = 'relay-v2.34-keyframe-gate'
10
 
11
 
12
  def env_rtmp_destination():
relay.py CHANGED
@@ -711,6 +711,12 @@ class Sender:
711
  base=float(sess.get('last') or 0)
712
  if sess.get('filling') or sess.get('swapping'):
713
  base=max(base,float(sess.get('ff_last') or 0))
 
 
 
 
 
 
714
  return base
715
 
716
  def _stream_tuning(self):
@@ -781,7 +787,7 @@ class Sender:
781
  stdin، ويوتيوب يبقى مستقبِلًا على آخر ما وصل — لذا يُقاس تدفق البيانات لا حالة العملية.
782
  """
783
  sess.setdefault('stdin_lock', threading.Lock())
784
- sess.setdefault('filling', False); sess.setdefault('filling_at', 0.0)
785
  it = response.iter_content(None).__iter__()
786
  deadline = time.monotonic() + sess['max_seconds']
787
  chunk_wait = min(read_timeout, max(1.0, self._stall_seconds() / 3.0)) if self._stall_seconds() else read_timeout
@@ -1070,7 +1076,7 @@ class Sender:
1070
  'stall_reason': '', 'spawned': False, 'spawn_error': '',
1071
  'writing': False, 'output_failure': False, 'connection_event': False,
1072
  'ff_last': time.monotonic(),
1073
- 'stdin_lock': threading.Lock(), 'filling': False, 'filling_at': 0.0}
1074
  self.set(status='الاتصال بيوتيوب', source=candidate['label'], output_seconds=0,
1075
  last_output_at=None, stalled=False, last_source_at=sess['last'], source_bytes=0,
1076
  rtmp_phase='')
@@ -1285,7 +1291,7 @@ class Sender:
1285
  view=view[n:]
1286
  except (BrokenPipeError,OSError,ValueError):
1287
  return
1288
- fill_state['emitted']+=1;fill_state['last_emit']=time.monotonic()
1289
  except Exception:
1290
  sess['filling']=False
1291
  return
 
711
  base=float(sess.get('last') or 0)
712
  if sess.get('filling') or sess.get('swapping'):
713
  base=max(base,float(sess.get('ff_last') or 0))
714
+ if sess.get('filling'):
715
+ # كتابة حشو ناجحة دليل مباشر أن FFmpeg يبتلع مدخله الآن (المُرمِّز
716
+ # يطحن قفزة/تبديلًا لكنه حيّ): تُحتسب مهلة المضخّة منها أيضًا فلا
717
+ # تموت الجلسة (مقيس: موتان 10:24:57 و10:25:40 أثناء حشو+تبديل).
718
+ # خرج محجوب حقًا يُجمّد كتابات الحشو نفسها فتمضي المهلة كالعادة.
719
+ base=max(base,float(sess.get('fill_emit_at') or 0))
720
  return base
721
 
722
  def _stream_tuning(self):
 
787
  stdin، ويوتيوب يبقى مستقبِلًا على آخر ما وصل — لذا يُقاس تدفق البيانات لا حالة العملية.
788
  """
789
  sess.setdefault('stdin_lock', threading.Lock())
790
+ sess.setdefault('filling', False); sess.setdefault('filling_at', 0.0); sess.setdefault('fill_emit_at', 0.0)
791
  it = response.iter_content(None).__iter__()
792
  deadline = time.monotonic() + sess['max_seconds']
793
  chunk_wait = min(read_timeout, max(1.0, self._stall_seconds() / 3.0)) if self._stall_seconds() else read_timeout
 
1076
  'stall_reason': '', 'spawned': False, 'spawn_error': '',
1077
  'writing': False, 'output_failure': False, 'connection_event': False,
1078
  'ff_last': time.monotonic(),
1079
+ 'stdin_lock': threading.Lock(), 'filling': False, 'filling_at': 0.0, 'fill_emit_at': 0.0}
1080
  self.set(status='الاتصال بيوتيوب', source=candidate['label'], output_seconds=0,
1081
  last_output_at=None, stalled=False, last_source_at=sess['last'], source_bytes=0,
1082
  rtmp_phase='')
 
1291
  view=view[n:]
1292
  except (BrokenPipeError,OSError,ValueError):
1293
  return
1294
+ fill_state['emitted']+=1;fill_state['last_emit']=time.monotonic();sess['fill_emit_at']=fill_state['last_emit']
1295
  except Exception:
1296
  sess['filling']=False
1297
  return
test_flv_bridge.py CHANGED
@@ -75,7 +75,8 @@ events=[]
75
  b=FLVBridge(detect_format_change=True,allow_raise=lambda:True,user='alice',
76
  event_logger=lambda user,event,details:events.append((user,event,details)))
77
  avc_input=flv(tag(9,0,seq('avc1',AVC_A)),tag(9,0,coded('avc1',VIDEO_A,b'\xff\xff\xfe')),
78
- tag(9,40,seq('avc1',AVC_B)),tag(9,40,coded('avc1',VIDEO_A)))
 
79
  avc_out=chunked(b,avc_input);avc_tags=parse(avc_out)
80
  check('AVC configuration replacement stays in one bridge session',b.format_changes==1)
81
  check('AVC replacement is not classified as a codec switch',b.codec_changes==0 and b.video_codec=='avc1')
@@ -83,11 +84,13 @@ check('both AVC configuration records are emitted in order',
83
  [x[2][5:] for x in avc_tags if x[0]==9 and x[2][1:2]==b'\x00']==[AVC_A,AVC_B])
84
  check('AVC coded bytes and signed CTS pass unchanged',
85
  [x[2] for x in avc_tags if x[0]==9 and x[2][1:2]==b'\x01']==
86
- [coded('avc1',VIDEO_A,b'\xff\xff\xfe'),coded('avc1',VIDEO_A)])
87
- check('continuous-source DTS is preserved exactly',[x[1] for x in avc_tags]==[0,0,40,40])
 
 
88
  check('AVC config and resolution changes are recorded with broadcaster identity',
89
- [x[1] for x in events]==['STREAM_CONFIG','DECODER_CONFIG_CHANGE','RESOLUTION_CHANGE'] and
90
- events[-1][0]=='alice' and events[-1][2]['new_width']==640)
91
 
92
  # A complete avcC may itself contain accumulated arrays. Preserve the record as a unit rather
93
  # than trying to append old records with potentially conflicting parameter-set IDs.
@@ -121,11 +124,11 @@ reconnect_events=[]
121
  br=FLVBridge(user='alice',event_logger=lambda user,event,details:reconnect_events.append((event,details)))
122
  first=br.feed(flv(tag(9,100,seq('avc1',AVC_A)),tag(9,140,coded('avc1',VIDEO_A))))
123
  last=br.last_out_ts;br.reconnect()
124
- second=br.feed(flv(tag(18,0,b'metadata'),tag(9,0,seq('avc1',AVC_A)),tag(9,40,coded('avc1',VIDEO_A))))
125
  joined=parse(first+second)
126
  check('reattach drops second FLV header and startup metadata',len(second)>0 and not second.startswith(b'FLV') and all(x[0]!=18 for x in joined))
127
  check('source reattach is recorded without claiming RTMP closure',
128
- reconnect_events[-1][0]=='RECONNECT' and reconnect_events[-1][1]['outbound_rtmp_reused'] is True)
129
  check('reattach drops only identical startup decoder record',sum(1 for x in joined if x[0]==9 and x[2][1:2]==b'\x00')==1)
130
  check('reattach shifts DTS once while preserving valid equal timestamps',
131
  br.last_out_ts>last and all(a[1]<=z[1] for a,z in zip(joined,joined[1:])))
@@ -146,7 +149,7 @@ bs=FLVBridge(detect_format_change=True,user='alice',
146
  switch=bs.feed(flv(tag(9,0,seq('avc1',AVC_A)),tag(9,1,coded('avc1',VIDEO_A)),
147
  tag(9,40,seq('hvc1',HEVC_A,legacy=False)),tag(9,41,coded('hvc1',VIDEO_H,legacy=False))))
148
  switch_tags=parse(switch)
149
- check('AVC to HEVC transition does not raise or truncate bridge output',len(switch_tags)==4)
150
  check('codec transition is separately observable',bs.format_changes==1 and bs.codec_changes==1 and bs.video_codec=='hvc1')
151
  check('codec transition writes CODEC_CHANGE without stopping the bridge','CODEC_CHANGE' in switch_events)
152
 
@@ -219,6 +222,31 @@ if ff and pathlib.Path(ff).is_file():
219
  else:
220
  print('SKIP FFmpeg decoder-record remux checks (ffmpeg unavailable)')
221
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
  # --- Gap filler: frozen keyframes keep the timeline (and YouTube leg) alive ---
223
  b2=FLVBridge(user='alice',event_logger=lambda *a:None)
224
  check('no filler before any keyframe was emitted',b2.freeze_bytes() is None)
 
75
  b=FLVBridge(detect_format_change=True,allow_raise=lambda:True,user='alice',
76
  event_logger=lambda user,event,details:events.append((user,event,details)))
77
  avc_input=flv(tag(9,0,seq('avc1',AVC_A)),tag(9,0,coded('avc1',VIDEO_A,b'\xff\xff\xfe')),
78
+ tag(9,40,seq('avc1',AVC_B)),tag(9,40,coded('avc1',VIDEO_A)),
79
+ tag(9,80,coded('avc1',VIDEO_A,frame=1)))
80
  avc_out=chunked(b,avc_input);avc_tags=parse(avc_out)
81
  check('AVC configuration replacement stays in one bridge session',b.format_changes==1)
82
  check('AVC replacement is not classified as a codec switch',b.codec_changes==0 and b.video_codec=='avc1')
 
84
  [x[2][5:] for x in avc_tags if x[0]==9 and x[2][1:2]==b'\x00']==[AVC_A,AVC_B])
85
  check('AVC coded bytes and signed CTS pass unchanged',
86
  [x[2] for x in avc_tags if x[0]==9 and x[2][1:2]==b'\x01']==
87
+ [coded('avc1',VIDEO_A,b'\xff\xff\xfe'),coded('avc1',VIDEO_A,frame=1)])
88
+ check('dependent frame after a changed record is held for a clean keyframe',
89
+ any(x[1]=='KEYFRAME_RESYNC' and x[2].get('dropped_tags')==1 for x in events),str(events))
90
+ check('continuous-source DTS is preserved exactly',[x[1] for x in avc_tags]==[0,0,40,80])
91
  check('AVC config and resolution changes are recorded with broadcaster identity',
92
+ [x[1] for x in events]==['STREAM_CONFIG','DECODER_CONFIG_CHANGE','RESOLUTION_CHANGE','KEYFRAME_RESYNC'] and
93
+ events[2][0]=='alice' and events[2][2]['new_width']==640)
94
 
95
  # A complete avcC may itself contain accumulated arrays. Preserve the record as a unit rather
96
  # than trying to append old records with potentially conflicting parameter-set IDs.
 
124
  br=FLVBridge(user='alice',event_logger=lambda user,event,details:reconnect_events.append((event,details)))
125
  first=br.feed(flv(tag(9,100,seq('avc1',AVC_A)),tag(9,140,coded('avc1',VIDEO_A))))
126
  last=br.last_out_ts;br.reconnect()
127
+ second=br.feed(flv(tag(18,0,b'metadata'),tag(9,0,seq('avc1',AVC_A)),tag(9,40,coded('avc1',VIDEO_A,frame=1))))
128
  joined=parse(first+second)
129
  check('reattach drops second FLV header and startup metadata',len(second)>0 and not second.startswith(b'FLV') and all(x[0]!=18 for x in joined))
130
  check('source reattach is recorded without claiming RTMP closure',
131
+ any(e[0]=='RECONNECT' and e[1]['outbound_rtmp_reused'] is True for e in reconnect_events))
132
  check('reattach drops only identical startup decoder record',sum(1 for x in joined if x[0]==9 and x[2][1:2]==b'\x00')==1)
133
  check('reattach shifts DTS once while preserving valid equal timestamps',
134
  br.last_out_ts>last and all(a[1]<=z[1] for a,z in zip(joined,joined[1:])))
 
149
  switch=bs.feed(flv(tag(9,0,seq('avc1',AVC_A)),tag(9,1,coded('avc1',VIDEO_A)),
150
  tag(9,40,seq('hvc1',HEVC_A,legacy=False)),tag(9,41,coded('hvc1',VIDEO_H,legacy=False))))
151
  switch_tags=parse(switch)
152
+ check('AVC to HEVC transition holds dependents for a keyframe without raising',len(switch_tags)==3)
153
  check('codec transition is separately observable',bs.format_changes==1 and bs.codec_changes==1 and bs.video_codec=='hvc1')
154
  check('codec transition writes CODEC_CHANGE without stopping the bridge','CODEC_CHANGE' in switch_events)
155
 
 
222
  else:
223
  print('SKIP FFmpeg decoder-record remux checks (ffmpeg unavailable)')
224
 
225
+ # --- Keyframe gate: splices/config changes resume only on a clean IDR, so the
226
+ # viewer sees the held picture instead of gray/colour-static error concealment ---
227
+ gev=[];bg=FLVBridge(user='alice',event_logger=lambda user,event,details:gev.append((event,details)))
228
+ bg.feed(flv(tag(9,0,seq('avc1',AVC_A)),tag(9,0,coded('avc1',VIDEO_A,frame=1)),
229
+ tag(9,40,coded('avc1',VIDEO_H,frame=2))))
230
+ bg.reconnect()
231
+ seg2=flv(tag(9,100,seq('avc1',AVC_A)),tag(9,100,coded('avc1',VIDEO_H,frame=2)),
232
+ tag(8,120,b'\xaf\x01\x11\x22'),
233
+ tag(9,140,coded('avc1',VIDEO_H,frame=2)),tag(9,180,coded('avc1',VIDEO_A,frame=1)),
234
+ tag(9,220,coded('avc1',VIDEO_H,frame=2)))
235
+ out_g=parse(b'FLV\x01\x05\x00\x00\x00\x09\x00\x00\x00\x00'+bg.feed(seg2))
236
+ check('post-reconnect inter frames are held until a keyframe',
237
+ [t[2] for t in out_g if t[0]==9]==[coded('avc1',VIDEO_A,frame=1),coded('avc1',VIDEO_H,frame=2)],
238
+ str(out_g))
239
+ check('audio flows while the video gate is closed',any(t[0]==8 for t in out_g),str(out_g))
240
+ check('keyframe resync is recorded with the dropped count',
241
+ ('KEYFRAME_RESYNC',{'dropped_tags':2,'capped':False}) in gev,str(gev))
242
+ gev2=[];bg2=FLVBridge(user='alice',event_logger=lambda u,e,d:gev2.append((e,d)))
243
+ bg2.feed(flv(tag(9,0,seq('avc1',AVC_A)),tag(9,0,coded('avc1',VIDEO_A,frame=1))))
244
+ out_c=parse(b'FLV\x01\x05\x00\x00\x00\x09\x00\x00\x00\x00'+bg2.feed(
245
+ tag(9,40,seq('avc1',AVC_B))+tag(9,40,coded('avc1',VIDEO_H,frame=2))+
246
+ tag(9,80,coded('avc1',VIDEO_A,frame=1))))
247
+ check('config change holds dependents until the next keyframe',
248
+ [t[2] for t in out_c if t[0]==9]==[seq('avc1',AVC_B),coded('avc1',VIDEO_A,frame=1)],str(out_c))
249
+
250
  # --- Gap filler: frozen keyframes keep the timeline (and YouTube leg) alive ---
251
  b2=FLVBridge(user='alice',event_logger=lambda *a:None)
252
  check('no filler before any keyframe was emitted',b2.freeze_bytes() is None)
test_smart_mode.py CHANGED
@@ -274,6 +274,14 @@ class WriteDeadlineTests(unittest.TestCase):
274
  self.assertEqual(Sender._deadline_base(
275
  {'last': 100.0, 'ff_last': 150.0, 'filling': True}), 150.0)
276
 
 
 
 
 
 
 
 
 
277
  def test_swapping_base_never_moves_backwards(self):
278
  self.assertEqual(Sender._deadline_base(
279
  {'last': 200.0, 'ff_last': 150.0, 'swapping': True}), 200.0)
 
274
  self.assertEqual(Sender._deadline_base(
275
  {'last': 100.0, 'ff_last': 150.0, 'filling': True}), 150.0)
276
 
277
+ def test_filling_base_counts_successful_fill_writes(self):
278
+ self.assertEqual(Sender._deadline_base(
279
+ {'last': 100.0, 'ff_last': 120.0, 'fill_emit_at': 150.0, 'filling': True}), 150.0)
280
+
281
+ def test_fill_credit_does_not_apply_while_swapping(self):
282
+ self.assertEqual(Sender._deadline_base(
283
+ {'last': 100.0, 'ff_last': 90.0, 'fill_emit_at': 150.0, 'swapping': True}), 100.0)
284
+
285
  def test_swapping_base_never_moves_backwards(self):
286
  self.assertEqual(Sender._deadline_base(
287
  {'last': 200.0, 'ff_last': 150.0, 'swapping': True}), 200.0)