@@ -129,6 +129,7 @@ type segment = {
129
129
discontinuous : bool ;
130
130
current_discontinuity : int ;
131
131
filename : string ;
132
+ mutable init_filename : string option ;
132
133
mutable out_channel : out_channel option ;
133
134
mutable len : int ;
134
135
}
@@ -158,6 +159,7 @@ type stream = {
158
159
codecs : string Lazy .t ; (* * codecs (see RFC 6381) *)
159
160
extname : string ;
160
161
mutable init_state : init_state ;
162
+ mutable init_position : int ;
161
163
mutable position : int ;
162
164
mutable current_segment : segment option ;
163
165
mutable discontinuity_count : int ;
@@ -346,7 +348,8 @@ class hls_output p =
346
348
video_size;
347
349
extname;
348
350
init_state = `Todo ;
349
- position = 0 ;
351
+ init_position = 0 ;
352
+ position = 1 ;
350
353
current_segment = None ;
351
354
discontinuity_count = 0 ;
352
355
}
@@ -358,7 +361,10 @@ class hls_output p =
358
361
lazy
359
362
( if
360
363
List. find_opt
361
- (fun s -> match s.init_state with `Has_init _ -> true | _ -> false )
364
+ (fun s ->
365
+ match s.current_segment with
366
+ | Some { init_filename = Some _ } -> true
367
+ | _ -> false )
362
368
streams
363
369
<> None
364
370
then 7
@@ -423,7 +429,19 @@ class hls_output p =
423
429
push_segment segment segments;
424
430
if List. length ! segments > = max_segments then (
425
431
let segment = remove_segment segments in
426
- self#unlink segment.filename ))
432
+ self#unlink segment.filename;
433
+ ignore
434
+ (Option. map
435
+ (fun filename ->
436
+ if
437
+ Sys. file_exists filename
438
+ && not
439
+ (List. exists
440
+ (fun s ->
441
+ s.init_filename = segment.init_filename)
442
+ ! segments)
443
+ then self#unlink filename)
444
+ segment.init_filename) ))
427
445
s.current_segment);
428
446
s.current_segment < - None ;
429
447
self#write_playlist s;
@@ -450,6 +468,8 @@ class hls_output p =
450
468
current_discontinuity = s.discontinuity_count;
451
469
len = 0 ;
452
470
filename;
471
+ init_filename =
472
+ (match s.init_state with `Has_init f -> Some f | _ -> None );
453
473
out_channel = Some out_channel;
454
474
}
455
475
in
@@ -468,12 +488,14 @@ class hls_output p =
468
488
(fun segment ->
469
489
self#close_out ~filename: segment.filename
470
490
(Option. get segment.out_channel);
491
+ ignore
492
+ (Option. map
493
+ (fun filename ->
494
+ if Sys. file_exists filename then self#unlink filename)
495
+ segment.init_filename);
471
496
self#unlink segment.filename)
472
497
s.current_segment);
473
- s.current_segment < - None ;
474
- match s.init_state with
475
- | `Has_init filename -> self#unlink filename
476
- | _ -> () )
498
+ s.current_segment < - None )
477
499
streams
478
500
479
501
method private playlist_name s = directory ^^ s.name ^ " .m3u8"
@@ -512,10 +534,10 @@ class hls_output p =
512
534
if segment.discontinuous then
513
535
output_string oc " #EXT-X-DISCONTINUITY\r\n " ;
514
536
if pos = 0 || segment.discontinuous then (
515
- match s.init_state with
516
- | `Has_init init_filename ->
537
+ match segment.init_filename with
538
+ | Some filename ->
517
539
output_string oc
518
- (Printf. sprintf " #EXT-X-MAP:URI=%S\r\n " init_filename )
540
+ (Printf. sprintf " #EXT-X-MAP:URI=%S\r\n " filename )
519
541
| _ -> () );
520
542
output_string oc
521
543
(Printf. sprintf " #EXTINF:%.03f,\r\n "
@@ -607,19 +629,23 @@ class hls_output p =
607
629
(fun stream (name , pos , discontinuity_count ) ->
608
630
assert (name = stream.name);
609
631
stream.discontinuity_count < - discontinuity_count;
610
- stream.position < - pos)
632
+ stream.init_position < - pos;
633
+ stream.position < - pos + 1 )
611
634
streams p;
612
635
segments < - s
613
636
614
- method private process_init ~init ({ extname; name; position } as s ) =
637
+ method private process_init ~init ~segment
638
+ ({ extname; name; init_position } as s ) =
615
639
match init with
616
640
| None -> s.init_state < - `No_init
617
641
| Some data ->
618
- let init_filename = segment_name ~position ~extname name in
642
+ let init_filename =
643
+ segment_name ~position: init_position ~extname name
644
+ in
619
645
let oc = self#open_out init_filename in
620
646
Strings. iter (output_substring oc) data;
621
647
self#close_out ~filename: init_filename oc;
622
- s.position < - s.position + 1 ;
648
+ segment.init_filename < - Some init_filename ;
623
649
s.init_state < - `Has_init init_filename
624
650
625
651
method encode frame ofs len =
@@ -631,7 +657,7 @@ class hls_output p =
631
657
let init, encoded =
632
658
Encoder. (s.encoder.hls.init_encode frame ofs len)
633
659
in
634
- self#process_init ~init s;
660
+ self#process_init ~init ~segment s;
635
661
(None , encoded) )
636
662
else if segment.len + len > segment_master_duration then (
637
663
match Encoder. (s.encoder.hls.split_encode frame ofs len) with
0 commit comments