Skip to content

Commit 0e7af72

Browse files
committed
Make it work with discontinuities.
1 parent 2a95c68 commit 0e7af72

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

src/outputs/hls_output.ml

+41-15
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ type segment = {
129129
discontinuous : bool;
130130
current_discontinuity : int;
131131
filename : string;
132+
mutable init_filename : string option;
132133
mutable out_channel : out_channel option;
133134
mutable len : int;
134135
}
@@ -158,6 +159,7 @@ type stream = {
158159
codecs : string Lazy.t; (** codecs (see RFC 6381) *)
159160
extname : string;
160161
mutable init_state : init_state;
162+
mutable init_position : int;
161163
mutable position : int;
162164
mutable current_segment : segment option;
163165
mutable discontinuity_count : int;
@@ -346,7 +348,8 @@ class hls_output p =
346348
video_size;
347349
extname;
348350
init_state = `Todo;
349-
position = 0;
351+
init_position = 0;
352+
position = 1;
350353
current_segment = None;
351354
discontinuity_count = 0;
352355
}
@@ -358,7 +361,10 @@ class hls_output p =
358361
lazy
359362
( if
360363
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)
362368
streams
363369
<> None
364370
then 7
@@ -423,7 +429,19 @@ class hls_output p =
423429
push_segment segment segments;
424430
if List.length !segments >= max_segments then (
425431
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) ))
427445
s.current_segment);
428446
s.current_segment <- None;
429447
self#write_playlist s;
@@ -450,6 +468,8 @@ class hls_output p =
450468
current_discontinuity = s.discontinuity_count;
451469
len = 0;
452470
filename;
471+
init_filename =
472+
(match s.init_state with `Has_init f -> Some f | _ -> None);
453473
out_channel = Some out_channel;
454474
}
455475
in
@@ -468,12 +488,14 @@ class hls_output p =
468488
(fun segment ->
469489
self#close_out ~filename:segment.filename
470490
(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);
471496
self#unlink segment.filename)
472497
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)
477499
streams
478500

479501
method private playlist_name s = directory ^^ s.name ^ ".m3u8"
@@ -512,10 +534,10 @@ class hls_output p =
512534
if segment.discontinuous then
513535
output_string oc "#EXT-X-DISCONTINUITY\r\n";
514536
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 ->
517539
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)
519541
| _ -> () );
520542
output_string oc
521543
(Printf.sprintf "#EXTINF:%.03f,\r\n"
@@ -607,19 +629,23 @@ class hls_output p =
607629
(fun stream (name, pos, discontinuity_count) ->
608630
assert (name = stream.name);
609631
stream.discontinuity_count <- discontinuity_count;
610-
stream.position <- pos)
632+
stream.init_position <- pos;
633+
stream.position <- pos + 1)
611634
streams p;
612635
segments <- s
613636

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) =
615639
match init with
616640
| None -> s.init_state <- `No_init
617641
| 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
619645
let oc = self#open_out init_filename in
620646
Strings.iter (output_substring oc) data;
621647
self#close_out ~filename:init_filename oc;
622-
s.position <- s.position + 1;
648+
segment.init_filename <- Some init_filename;
623649
s.init_state <- `Has_init init_filename
624650

625651
method encode frame ofs len =
@@ -631,7 +657,7 @@ class hls_output p =
631657
let init, encoded =
632658
Encoder.(s.encoder.hls.init_encode frame ofs len)
633659
in
634-
self#process_init ~init s;
660+
self#process_init ~init ~segment s;
635661
(None, encoded) )
636662
else if segment.len + len > segment_master_duration then (
637663
match Encoder.(s.encoder.hls.split_encode frame ofs len) with

0 commit comments

Comments
 (0)