Reoo88 commited on
Commit
b1e5f46
·
verified ·
1 Parent(s): 5efbe58

relay-v2.40-avfill-splice trial: fix splice bias and fill shielding (On only)

Browse files

Sequence headers stamped 0 no longer consume the reconnect/fill splice bias (they are pinned at the splice point; first media tag sets the bias), bridge feed/freeze/reconnect are serialized, fill writes use the bounded write deadline and mark output failure, and the watchdog no longer shields a fill whose emissions stall or whose output seconds leap far ahead of wall time.

Files changed (5) hide show
  1. flv_bridge.py +30 -5
  2. ingest.py +1 -1
  3. relay.py +43 -7
  4. test_flv_bridge.py +23 -0
  5. test_smart_mode.py +20 -0
flv_bridge.py CHANGED
@@ -26,6 +26,7 @@ References:
26
  - GStreamer bugs 797255/797256 (stream headers/caps changes in streamable FLV)
27
  """
28
  import os
 
29
  from collections import deque
30
  from stream_monitor import log_event, video_dimensions
31
 
@@ -141,6 +142,7 @@ class FLVBridge:
141
  self.last_out_ts=None
142
  self.ts_bias=0
143
  self.ts_bias_pending=False
 
144
  self._video_samples=deque();self._bitrate_kbps=None;self._bitrate_check_ts=None
145
  self._last_keyframe_ts=None;self._keyframe_interval=None;self._keyframe_event_ts=None
146
  self._last_keyframe_tag=None # آخر وسم إطار مفتاحي مُرسل كاملًا: مادة حشو الفجوات
@@ -177,6 +179,9 @@ class FLVBridge:
177
  self._keyframe_interval=interval if old is None else old*0.7+interval*0.3
178
  self._last_keyframe_ts=ts
179
  def reconnect(self):
 
 
 
180
  """Prepare to splice a new TikTok FLV into the same FFmpeg stdin.
181
 
182
  ``video_cfg`` stays live so an identical startup record can be omitted while a changed
@@ -196,6 +201,9 @@ class FLVBridge:
196
  self._event('RECONNECT',{'scope':'tiktok_http','outbound_rtmp_reused':True,
197
  'last_output_dts_ms':self.last_out_ts})
198
  def freeze_bytes(self,step_ms=2000):
 
 
 
199
  """وسم الإطار المفتاحي الأخير مكرّرًا بطابع زمني متقدّم: صورة مجمّدة صحيحة
200
  التشكيل تُبقي الخطّ الزمني (ووصلة يوتيوب) حيًّا أثناء صمت المصدر، بدل صمتٍ
201
  تقفل بعده حافة يوتيوب النشر (~14-20 ث موثّق). أول وسم حقيقي بعد الحشو يأخذ
@@ -226,6 +234,9 @@ class FLVBridge:
226
  return bytes([self._audio_byte0,0])+value.to_bytes(2,'big')
227
 
228
  def freeze_span_bytes(self,step_ms=2000):
 
 
 
229
  """Frozen keyframe for one fill step plus silent AAC covering the span.
230
 
231
  TikTok audio is HE-AAC: a bare AAC-LC silent frame under an HE header
@@ -263,6 +274,9 @@ class FLVBridge:
263
  return b''.join(parts)+video
264
 
265
  def feed(self,chunk):
 
 
 
266
  self.buffer.extend(chunk);output=bytearray()
267
  if not self.header_done:
268
  if len(self.buffer)<9:return b''
@@ -279,7 +293,7 @@ class FLVBridge:
279
  if len(self.buffer)-offset<total:break
280
  body=bytes(self.buffer[offset+11:offset+11+size]);tag_type=header[0]
281
  if tag_type & 0x20:raise ValueError('Filtered/encrypted FLV tag is unsupported')
282
- kind = tag_type & 31;raw_ts=_tag_ts(header);audio_prepend=False
283
  if kind==18 and self.skip_script:
284
  offset+=total
285
  continue
@@ -297,6 +311,7 @@ class FLVBridge:
297
  if kind==9:
298
  signature=_video_signature(body)
299
  if signature is not None:
 
300
  previous=self.video_cfg;old_size=self.video_size
301
  changed=previous is not None and signature!=previous
302
  codec=signature[:4].decode('ascii','replace')
@@ -336,6 +351,7 @@ class FLVBridge:
336
  if kind==8 and len(body)>=2 and (body[0]>>4)==10: # AAC audio
337
  self._audio_byte0=body[0]
338
  if body[1]==0: # sequence header: learn the source audio config
 
339
  self.audio_cfg=body[2:]
340
  self._audio_seq_payload=bytes(body)
341
  parsed=_asc_info(body[2:])
@@ -353,10 +369,19 @@ class FLVBridge:
353
  audio_prepend=True
354
  self._fill_declared_lc=False
355
  self._fill_cursor=None
356
- if self.ts_bias_pending:
357
- self.ts_bias=(self.last_out_ts or 0)+40-raw_ts
358
- self.ts_bias_pending=False
359
- out_ts=raw_ts+self.ts_bias
 
 
 
 
 
 
 
 
 
360
  _set_tag_ts(header, out_ts)
361
  # Equal DTS across audio/video (or config/frame) is valid and must not be rewritten.
362
  # Only reconnect() adds one constant bias; last_out_ts tracks the emitted maximum.
 
26
  - GStreamer bugs 797255/797256 (stream headers/caps changes in streamable FLV)
27
  """
28
  import os
29
+ import threading
30
  from collections import deque
31
  from stream_monitor import log_event, video_dimensions
32
 
 
142
  self.last_out_ts=None
143
  self.ts_bias=0
144
  self.ts_bias_pending=False
145
+ self._lock=threading.RLock()
146
  self._video_samples=deque();self._bitrate_kbps=None;self._bitrate_check_ts=None
147
  self._last_keyframe_ts=None;self._keyframe_interval=None;self._keyframe_event_ts=None
148
  self._last_keyframe_tag=None # آخر وسم إطار مفتاحي مُرسل كاملًا: مادة حشو الفجوات
 
179
  self._keyframe_interval=interval if old is None else old*0.7+interval*0.3
180
  self._last_keyframe_ts=ts
181
  def reconnect(self):
182
+ with self._lock:
183
+ return self._reconnect_locked()
184
+ def _reconnect_locked(self):
185
  """Prepare to splice a new TikTok FLV into the same FFmpeg stdin.
186
 
187
  ``video_cfg`` stays live so an identical startup record can be omitted while a changed
 
201
  self._event('RECONNECT',{'scope':'tiktok_http','outbound_rtmp_reused':True,
202
  'last_output_dts_ms':self.last_out_ts})
203
  def freeze_bytes(self,step_ms=2000):
204
+ with self._lock:
205
+ return self._freeze_bytes_locked(step_ms)
206
+ def _freeze_bytes_locked(self,step_ms=2000):
207
  """وسم الإطار المفتاحي الأخير مكرّرًا بطابع زمني متقدّم: صورة مجمّدة صحيحة
208
  التشكيل تُبقي الخطّ الزمني (ووصلة يوتيوب) حيًّا أثناء صمت المصدر، بدل صمتٍ
209
  تقفل بعده حافة يوتيوب النشر (~14-20 ث موثّق). أول وسم حقيقي بعد الحشو يأخذ
 
234
  return bytes([self._audio_byte0,0])+value.to_bytes(2,'big')
235
 
236
  def freeze_span_bytes(self,step_ms=2000):
237
+ with self._lock:
238
+ return self._freeze_span_bytes_locked(step_ms)
239
+ def _freeze_span_bytes_locked(self,step_ms=2000):
240
  """Frozen keyframe for one fill step plus silent AAC covering the span.
241
 
242
  TikTok audio is HE-AAC: a bare AAC-LC silent frame under an HE header
 
274
  return b''.join(parts)+video
275
 
276
  def feed(self,chunk):
277
+ with self._lock:
278
+ return self._feed_locked(chunk)
279
+ def _feed_locked(self,chunk):
280
  self.buffer.extend(chunk);output=bytearray()
281
  if not self.header_done:
282
  if len(self.buffer)<9:return b''
 
293
  if len(self.buffer)-offset<total:break
294
  body=bytes(self.buffer[offset+11:offset+11+size]);tag_type=header[0]
295
  if tag_type & 0x20:raise ValueError('Filtered/encrypted FLV tag is unsupported')
296
+ kind = tag_type & 31;raw_ts=_tag_ts(header);audio_prepend=False;tag_is_seq=False
297
  if kind==18 and self.skip_script:
298
  offset+=total
299
  continue
 
311
  if kind==9:
312
  signature=_video_signature(body)
313
  if signature is not None:
314
+ tag_is_seq=True
315
  previous=self.video_cfg;old_size=self.video_size
316
  changed=previous is not None and signature!=previous
317
  codec=signature[:4].decode('ascii','replace')
 
351
  if kind==8 and len(body)>=2 and (body[0]>>4)==10: # AAC audio
352
  self._audio_byte0=body[0]
353
  if body[1]==0: # sequence header: learn the source audio config
354
+ tag_is_seq=True
355
  self.audio_cfg=body[2:]
356
  self._audio_seq_payload=bytes(body)
357
  parsed=_asc_info(body[2:])
 
369
  audio_prepend=True
370
  self._fill_declared_lc=False
371
  self._fill_cursor=None
372
+ if self.ts_bias_pending and tag_is_seq and self.last_out_ts is not None:
373
+ # ترويسات الإعداد (AVC/AAC SequenceStart) كثيرًا ما تحمل طابع 0 أو
374
+ # طابعًا لا يمثّل أول عيّنة وسائط. لا تدعها تضع انحياز الخياطة:
375
+ # لو استهلكته بطابع 0 صار الانحياز ‏last_out+40، ثم قفز أول إطار
376
+ # حقيقي (DTS بعيد) ملايين الميلي ثانية إلى المستقبل فينهار خطّ
377
+ # الإخراج الزمني ويُهمل يوتيوب التدفق رغم أن FFmpeg «يتقدّم».
378
+ # نُلصق الترويسة عند نقطة الخياطة، ويبقى الانحياز لأول وسم وسائط.
379
+ out_ts=self.last_out_ts+40
380
+ else:
381
+ if self.ts_bias_pending:
382
+ self.ts_bias=(self.last_out_ts or 0)+40-raw_ts
383
+ self.ts_bias_pending=False
384
+ out_ts=raw_ts+self.ts_bias
385
  _set_tag_ts(header, out_ts)
386
  # Equal DTS across audio/video (or config/frame) is valid and must not be rewritten.
387
  # Only reconnect() adds one constant bias; last_out_ts tracks the emitted maximum.
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.39-avfill-fastout'
10
 
11
 
12
  def env_rtmp_destination():
 
6
  from datetime import datetime
7
  from zoneinfo import ZoneInfo
8
 
9
+ APP_VERSION = 'relay-v2.40-avfill-splice'
10
 
11
 
12
  def env_rtmp_destination():
relay.py CHANGED
@@ -723,7 +723,22 @@ class Sender:
723
  return False
724
  since = float(sess.get('fill_since') or 0)
725
  ff_last = float(sess.get('ff_last') or 0)
726
- return bool(since and ff_last and now - since >= grace and now - ff_last > grace)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
727
 
728
  @staticmethod
729
  def _fill_write_renew_allowed(sess):
@@ -734,8 +749,14 @@ class Sender:
734
  until = float(sess.get('fill_until') or 0)
735
  if until and now >= until:
736
  return False
 
737
  ff_last = float(sess.get('ff_last') or 0)
738
- return not ff_last or now - ff_last <= Sender._fill_output_grace()
 
 
 
 
 
739
 
740
  @staticmethod
741
  def _deadline_base(sess):
@@ -1323,6 +1344,7 @@ class Sender:
1323
  if fill_state['started'] is None:
1324
  fill_state['started']=now_f;fill_state['last_emit']=0.0
1325
  sess['fill_since']=now_f;sess['fill_until']=now_f+fill_max+25.0
 
1326
  self.log(user,'[FILL] صمت المصدر %.0f ث؛ حشو بإطار مجمّد حتى لا يرى يوتيوب فجوة (سقف %.0f ث)'%(silent_for,fill_max))
1327
  log_event(user,'GAP_FILL_START',{'silent_seconds':round(silent_for,1),'cap_seconds':fill_max,'silent_audio':bridge.fill_audio_ready()})
1328
  telemetry_event(tele_key,user,'GAP_FILL_START',{'silent_seconds':round(silent_for,1),'cap_seconds':fill_max,'silent_audio':bridge.fill_audio_ready()})
@@ -1335,11 +1357,25 @@ class Sender:
1335
  try:
1336
  with sess['stdin_lock']:
1337
  if time.monotonic()-float(sess.get('last') or 0)<fill_after:continue
1338
- view=memoryview(data)
1339
- while view:
1340
- n=p.stdin.write(view)
1341
- if not n:raise BrokenPipeError()
1342
- view=view[n:]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1343
  except (BrokenPipeError,OSError,ValueError):
1344
  return
1345
  fill_state['emitted']+=1;fill_state['last_emit']=time.monotonic();sess['fill_emit_at']=fill_state['last_emit']
 
723
  return False
724
  since = float(sess.get('fill_since') or 0)
725
  ff_last = float(sess.get('ff_last') or 0)
726
+ if not since or not ff_last or now - since < grace:
727
+ return False
728
+ if now - ff_last > grace:
729
+ return True
730
+ # كاتب الحشو نفسه شاهد: لو لم تُكتب قطعة حشو كاملة منذ السماحية فالأنبوب
731
+ # محجوب/الخيط ميت، حتى لو بدت «ثواني» FFmpeg حديثة بسبب قفزة طوابع.
732
+ emit = float(sess.get('fill_emit_at') or 0)
733
+ if emit and now - emit > grace:
734
+ return True
735
+ # تقدّم خرج أسرع بكثير من الزمن الحقيقي أثناء الحشو ليس تسليمًا حيًا:
736
+ # إنه قفزة خطّ زمني (خياطة فاسدة) يتجاهلها يوتيوب كـ noData بينما يبدو
737
+ # العدّاد المحلي سعيدًا. لا نمنحه حصانة الحشو.
738
+ start = sess.get('fill_out_start')
739
+ if start is not None and float(output_seconds) - float(start) > (now - since) + 15.0:
740
+ return True
741
+ return False
742
 
743
  @staticmethod
744
  def _fill_write_renew_allowed(sess):
 
749
  until = float(sess.get('fill_until') or 0)
750
  if until and now >= until:
751
  return False
752
+ grace = Sender._fill_output_grace()
753
  ff_last = float(sess.get('ff_last') or 0)
754
+ if ff_last and now - ff_last > grace:
755
+ return False
756
+ emit = float(sess.get('fill_emit_at') or 0)
757
+ if emit and now - emit > grace:
758
+ return False
759
+ return True
760
 
761
  @staticmethod
762
  def _deadline_base(sess):
 
1344
  if fill_state['started'] is None:
1345
  fill_state['started']=now_f;fill_state['last_emit']=0.0
1346
  sess['fill_since']=now_f;sess['fill_until']=now_f+fill_max+25.0
1347
+ sess['fill_out_start']=max(0.0,float(progress['seconds']))
1348
  self.log(user,'[FILL] صمت المصدر %.0f ث؛ حشو بإطار مجمّد حتى لا يرى يوتيوب فجوة (سقف %.0f ث)'%(silent_for,fill_max))
1349
  log_event(user,'GAP_FILL_START',{'silent_seconds':round(silent_for,1),'cap_seconds':fill_max,'silent_audio':bridge.fill_audio_ready()})
1350
  telemetry_event(tele_key,user,'GAP_FILL_START',{'silent_seconds':round(silent_for,1),'cap_seconds':fill_max,'silent_audio':bridge.fill_audio_ready()})
 
1357
  try:
1358
  with sess['stdin_lock']:
1359
  if time.monotonic()-float(sess.get('last') or 0)<fill_after:continue
1360
+ # لا كتابة خام محظورة هنا: أنبوب ممتلئ كان يُبقي خيط
1361
+ # الحشو (وقفل stdin معه) معلّقًا بلا مهلة، فيتجمّد
1362
+ # الانبعاث ولا يرى الحارس إلا عدّادات مضلّلة. نفس
1363
+ # مهلة المضخّة: تجديد فقط أثناء تقدّم خرج حقيقي.
1364
+ fill_limit=self._stall_seconds()
1365
+ if fill_limit:
1366
+ self._write_deadline(p.stdin,data,
1367
+ self._deadline_base(sess)+fill_limit,sess)
1368
+ else:
1369
+ view=memoryview(data)
1370
+ while view:
1371
+ n=p.stdin.write(view)
1372
+ if not n:raise BrokenPipeError()
1373
+ view=view[n:]
1374
+ except SourceStalled as e:
1375
+ sess['output_failure']=True
1376
+ sess['error']=str(e)
1377
+ sess['filling']=False
1378
+ return
1379
  except (BrokenPipeError,OSError,ValueError):
1380
  return
1381
  fill_state['emitted']+=1;fill_state['last_emit']=time.monotonic();sess['fill_emit_at']=fill_state['last_emit']
test_flv_bridge.py CHANGED
@@ -140,6 +140,29 @@ check('reattach never suppresses a changed decoder record',
140
  [x[2][5:] for x in changed if x[0]==9 and x[2][1:2]==b'\x00']==[AVC_A,AVC_B])
141
  check('changed reattach remains on one FLV byte stream',not part2.startswith(b'FLV') and bc.format_changes==1)
142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
  # The byte bridge can represent a FourCC transition without raising or closing. FFmpeg 7 creates
144
  # a new late AVStream for such a transition, so production source selection still pins one codec;
145
  # same-codec decoder-record replacement above is the fully verified path.
 
140
  [x[2][5:] for x in changed if x[0]==9 and x[2][1:2]==b'\x00']==[AVC_A,AVC_B])
141
  check('changed reattach remains on one FLV byte stream',not part2.startswith(b'FLV') and bc.format_changes==1)
142
 
143
+ # A sequence header stamped 0 must not consume the splice bias: the first real
144
+ # frame of a refreshed TikTok FLV can carry a far-away room DTS, and letting a
145
+ # ts=0 header set the bias makes that frame leap millions of ms into the future
146
+ # (the output timeline collapses and YouTube discards the leg as noData).
147
+ bh0=FLVBridge(user='alice',event_logger=lambda *a:None)
148
+ bh0.feed(flv(tag(9,1000,seq('avc1',AVC_A)),tag(9,1000,coded('avc1',VIDEO_A,frame=1)),
149
+ tag(9,1040,coded('avc1',VIDEO_H,frame=2))))
150
+ bh0.freeze_span_bytes(2000);bh0.reconnect()
151
+ out_h0=parse(b'FLV\x01\x05\x00\x00\x00\x09\x00\x00\x00\x00'+bh0.feed(flv(
152
+ tag(9,0,seq('avc1',AVC_B)),tag(9,500000,coded('avc1',VIDEO_A,frame=1)),
153
+ tag(9,500040,coded('avc1',VIDEO_H,frame=2)))))
154
+ check('ts=0 changed video header does not consume splice bias',
155
+ [t[1] for t in out_h0]==[3080,3120,3160],str([t[1] for t in out_h0]))
156
+ ba0=FLVBridge(user='alice',event_logger=lambda *a:None)
157
+ ba0.feed(flv(tag(9,1000,seq('avc1',AVC_A)),tag(9,1000,coded('avc1',VIDEO_A,frame=1)),
158
+ tag(9,1040,coded('avc1',VIDEO_H,frame=2))))
159
+ ba0.freeze_span_bytes(2000);ba0.reconnect()
160
+ out_a0=parse(b'FLV\x01\x05\x00\x00\x00\x09\x00\x00\x00\x00'+ba0.feed(flv(
161
+ tag(8,0,b'\xaf\x00\x12\x10'),tag(9,0,seq('avc1',AVC_A)),
162
+ tag(9,500000,coded('avc1',VIDEO_A,frame=1)))))
163
+ check('ts=0 audio header does not consume splice bias',
164
+ [t[1] for t in out_a0]==[3080,3120],str([t[1] for t in out_a0]))
165
+
166
  # The byte bridge can represent a FourCC transition without raising or closing. FFmpeg 7 creates
167
  # a new late AVStream for such a transition, so production source selection still pins one codec;
168
  # same-codec decoder-record replacement above is the fully verified path.
test_smart_mode.py CHANGED
@@ -224,6 +224,26 @@ class WriteDeadlineRenewalTests(unittest.TestCase):
224
  self.assertFalse(Sender._fill_write_renew_allowed(stale))
225
  self.assertFalse(Sender._fill_write_renew_allowed(expired))
226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
 
228
  class FormatLockTests(unittest.TestCase):
229
  """مصدر بفاصل إطارات مفتاحية مضطرب (قياس الجسر) لا يُنسخ: يُطبَّع حتى نهاية
 
224
  self.assertFalse(Sender._fill_write_renew_allowed(stale))
225
  self.assertFalse(Sender._fill_write_renew_allowed(expired))
226
 
227
+ def test_fill_output_stale_detects_blocked_emission_and_inflated_progress(self):
228
+ # FFmpeg's counter looks fresh, but no fill chunk has completed writing:
229
+ # the pipe/filler is blocked, so the fill must not shield the session.
230
+ sess = {'filling': True, 'fill_since': 900.0, 'ff_last': 990.0,
231
+ 'fill_emit_at': 900.0}
232
+ self.assertTrue(Sender._fill_output_stale(sess, 1000.0, 25.0, 10.0))
233
+ # Output seconds racing far ahead of wall time during a fill is a
234
+ # timestamp leap (bad splice), not live delivery YouTube will play.
235
+ sess = {'filling': True, 'fill_since': 900.0, 'ff_last': 990.0,
236
+ 'fill_emit_at': 990.0, 'fill_out_start': 100.0}
237
+ self.assertTrue(Sender._fill_output_stale(sess, 1000.0, 25.0, 1000.0))
238
+ sess['fill_out_start'] = 905.0
239
+ self.assertFalse(Sender._fill_output_stale(sess, 1000.0, 25.0, 1000.0))
240
+
241
+ def test_fill_write_renewal_requires_fresh_emission(self):
242
+ now = time.monotonic()
243
+ sess = {'filling': True, 'ff_last': now, 'fill_until': now + 90,
244
+ 'fill_emit_at': now - 60}
245
+ self.assertFalse(Sender._fill_write_renew_allowed(sess))
246
+
247
 
248
  class FormatLockTests(unittest.TestCase):
249
  """مصدر بفاصل إطارات مفتاحية مضطرب (قياس الجسر) لا يُنسخ: يُطبَّع حتى نهاية