Skip to content

Commit

Permalink
HLS: (#3249)
Browse files Browse the repository at this point in the history
* Don't mark metadata as to be inserted if they match the last inserted ones.
* Log segment reopen reasons.
  • Loading branch information
toots authored Jul 21, 2023
1 parent ee0d7c5 commit 172baa2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion doc/content/hls_output.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ streams = [("aac_lofi",aac_lofi),
("aac_hifi", aac_hifi)]
def segment_name(~position,~extname,stream_name) =
timestamp = int_of_float(gettimeofday())
timestamp = int_of_float(time())
duration = 2
"#{stream_name}_#{duration}_#{timestamp}_#{position}.#{extname}"
end
Expand Down
1 change: 1 addition & 0 deletions src/core/encoder/formats/meta_format.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ let export_metadata m =
ret

let to_metadata m = m
let to_metadata_list m = Utils.list_of_metadata m
let empty_metadata = Hashtbl.create 0
let is_empty m = Hashtbl.length m == 0

Expand Down
1 change: 1 addition & 0 deletions src/core/encoder/formats/meta_format.mli
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type export_metadata

val export_metadata : Frame.metadata -> export_metadata
val to_metadata : export_metadata -> Frame.metadata
val to_metadata_list : export_metadata -> (string * string) list
val empty_metadata : export_metadata
val is_empty : export_metadata -> bool
val to_string : export_metadata -> string
35 changes: 29 additions & 6 deletions src/core/outputs/hls_output.ml
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,25 @@ class hls_output p =
s.init_state <- `Has_init init_filename
| Some _ -> raise Encoder.Not_enough_data

method private should_reopen ~segment ~len s =
if s.id3_enabled && pending_metadata s.metadata then (
self#log#info
"Terminating current segment on stream %s to insert new metadata"
s.name;
true)
else if Atomic.get s.pending_extra_tags <> [] then (
self#log#info
"Terminating current segment on stream %s to insert pending extra \
tags"
s.name;
true)
else if segment.len + len > segment_main_duration then (
self#log#debug
"Terminating current segment on stream %s to make expected length"
s.name;
true)
else false

method encode frame ofs len =
let frame_pos, samples_pos = current_position in
let frame_size = Lazy.force Frame.size in
Expand All @@ -892,11 +911,7 @@ class hls_output p =
self#process_init ~init ~segment s;
(None, encoded)
with Encoder.Not_enough_data -> (None, Strings.empty))
else if
(s.id3_enabled && pending_metadata s.metadata)
|| Atomic.get s.pending_extra_tags <> []
|| segment.len + len > segment_main_duration
then (
else if self#should_reopen ~segment ~len s then (
match Encoder.(s.encoder.hls.split_encode frame ofs len) with
| `Ok (flushed, encoded) -> (Some flushed, encoded)
| `Nope encoded -> (None, encoded))
Expand All @@ -922,7 +937,15 @@ class hls_output p =
method send b = List.iter2 self#write_pipe streams b

method insert_metadata m =
List.iter (fun s -> s.metadata <- `Todo m) streams
List.iter
(fun s ->
match s.metadata with
| `Sent m'
when Meta_format.to_metadata_list m
= Meta_format.to_metadata_list m' ->
()
| _ -> s.metadata <- `Todo m)
streams
end

let stream_t kind =
Expand Down
2 changes: 1 addition & 1 deletion src/core/tools/utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ let hashtbl_of_list l =

let list_of_metadata m =
let f x y l = (x, y) :: l in
Hashtbl.fold f m []
List.sort (fun (k, _) (k', _) -> Stdlib.compare k k') (Hashtbl.fold f m [])

let hashtbl_get : ('a, 'b) Hashtbl.t -> 'a -> 'b option =
fun h k -> try Some (Hashtbl.find h k) with Not_found -> None
Expand Down

0 comments on commit 172baa2

Please sign in to comment.