2023-12-01 17:32:19 +00:00
|
|
|
// Copyright 2017 Google LLC. All rights reserved.
|
2017-05-22 20:28:10 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#ifndef PACKAGER_PUBLIC_PACKAGER_H_
|
|
|
|
#define PACKAGER_PUBLIC_PACKAGER_H_
|
2017-05-22 20:28:10 +00:00
|
|
|
|
2023-11-30 06:51:37 +00:00
|
|
|
#include <cstdint>
|
2017-05-22 20:28:10 +00:00
|
|
|
#include <memory>
|
2024-02-14 17:21:11 +00:00
|
|
|
#include <optional>
|
2017-05-22 20:28:10 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/ad_cue_generator_params.h>
|
|
|
|
#include <packager/buffer_callback_params.h>
|
|
|
|
#include <packager/chunking_params.h>
|
|
|
|
#include <packager/crypto_params.h>
|
|
|
|
#include <packager/export.h>
|
|
|
|
#include <packager/hls_params.h>
|
|
|
|
#include <packager/mp4_output_params.h>
|
|
|
|
#include <packager/mpd_params.h>
|
|
|
|
#include <packager/status.h>
|
2017-05-23 02:41:26 +00:00
|
|
|
|
2017-05-22 20:28:10 +00:00
|
|
|
namespace shaka {
|
|
|
|
|
2017-05-23 02:41:26 +00:00
|
|
|
/// Parameters used for testing.
|
|
|
|
struct TestParams {
|
|
|
|
/// Whether to dump input stream info.
|
|
|
|
bool dump_stream_info = false;
|
|
|
|
/// Inject a fake clock which always returns 0. This allows deterministic
|
|
|
|
/// output from packaging.
|
|
|
|
bool inject_fake_clock = false;
|
|
|
|
/// Inject and replace the library version string if specified, which is used
|
|
|
|
/// to populate the version string in the manifests / media files.
|
|
|
|
std::string injected_library_version;
|
|
|
|
};
|
|
|
|
|
2017-05-22 20:28:10 +00:00
|
|
|
/// Packaging parameters.
|
|
|
|
struct PackagingParams {
|
|
|
|
/// Specify temporary directory for intermediate temporary files.
|
|
|
|
std::string temp_dir;
|
|
|
|
/// MP4 (ISO-BMFF) output related parameters.
|
|
|
|
Mp4OutputParams mp4_output_params;
|
2018-07-03 23:52:05 +00:00
|
|
|
/// The offset to be applied to transport stream (e.g. MPEG2-TS, HLS packed
|
|
|
|
/// audio) timestamps to compensate for possible negative timestamps in the
|
|
|
|
/// input.
|
2021-08-04 18:56:44 +00:00
|
|
|
int32_t transport_stream_timestamp_offset_ms = 0;
|
2024-02-08 18:39:50 +00:00
|
|
|
// the threshold used to determine if we should assume that the text stream
|
|
|
|
// actually starts at time zero
|
|
|
|
int32_t default_text_zero_bias_ms = 0;
|
|
|
|
|
2017-05-22 20:28:10 +00:00
|
|
|
/// Chunking (segmentation) related parameters.
|
|
|
|
ChunkingParams chunking_params;
|
|
|
|
|
2017-11-18 01:45:51 +00:00
|
|
|
/// Out of band cuepoint parameters.
|
|
|
|
AdCueGeneratorParams ad_cue_generator_params;
|
|
|
|
|
2017-10-06 18:14:40 +00:00
|
|
|
/// Create a human readable format of MediaInfo. The output file name will be
|
|
|
|
/// the name specified by output flag, suffixed with `.media_info`.
|
2017-05-22 20:28:10 +00:00
|
|
|
bool output_media_info = false;
|
2020-10-20 21:08:46 +00:00
|
|
|
/// Only use a single thread to generate output. This is useful in tests to
|
|
|
|
/// avoid non-deterministic outputs.
|
|
|
|
bool single_threaded = false;
|
2024-02-14 17:21:11 +00:00
|
|
|
|
2017-05-22 20:28:10 +00:00
|
|
|
/// DASH MPD related parameters.
|
|
|
|
MpdParams mpd_params;
|
|
|
|
/// HLS related parameters.
|
|
|
|
HlsParams hls_params;
|
|
|
|
|
|
|
|
/// Encryption and Decryption Parameters.
|
|
|
|
EncryptionParams encryption_params;
|
|
|
|
DecryptionParams decryption_params;
|
2017-05-23 02:41:26 +00:00
|
|
|
|
2017-07-17 18:15:07 +00:00
|
|
|
/// Buffer callback params.
|
|
|
|
BufferCallbackParams buffer_callback_params;
|
|
|
|
|
2017-05-23 02:41:26 +00:00
|
|
|
// Parameters for testing. Do not use in production.
|
|
|
|
TestParams test_params;
|
2017-05-22 20:28:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Defines a single input/output stream.
|
|
|
|
struct StreamDescriptor {
|
2024-02-14 17:21:11 +00:00
|
|
|
/// index of the stream to enforce ordering
|
|
|
|
std::optional<uint32_t> index;
|
|
|
|
|
2017-05-22 20:28:10 +00:00
|
|
|
/// Input/source media file path or network stream URL. Required.
|
|
|
|
std::string input;
|
|
|
|
|
|
|
|
/// Stream selector, can be `audio`, `video`, `text` or a zero based stream
|
|
|
|
/// index. Required.
|
|
|
|
std::string stream_selector;
|
|
|
|
|
|
|
|
/// Specifies output file path or init segment path (if segment template is
|
|
|
|
/// specified). Can be empty for self initialization media segments.
|
|
|
|
std::string output;
|
|
|
|
/// Specifies segment template. Can be empty.
|
|
|
|
std::string segment_template;
|
|
|
|
|
|
|
|
/// Optional value which specifies output container format, e.g. "mp4". If not
|
|
|
|
/// specified, will detect from output / segment template name.
|
|
|
|
std::string output_format;
|
|
|
|
/// If set to true, the stream will not be encrypted. This is useful, e.g. to
|
|
|
|
/// encrypt only video streams.
|
|
|
|
bool skip_encryption = false;
|
2017-09-20 22:49:00 +00:00
|
|
|
/// Specifies a custom DRM stream label, which can be a DRM label defined by
|
|
|
|
/// the DRM system. Typically values include AUDIO, SD, HD, UHD1, UHD2. If not
|
|
|
|
/// provided, the DRM stream label is derived from stream type (video, audio),
|
|
|
|
/// resolutions etc.
|
|
|
|
std::string drm_label;
|
2017-05-22 20:28:10 +00:00
|
|
|
/// If set to a non-zero value, will generate a trick play / trick mode
|
|
|
|
/// stream with frames sampled from the key frames in the original stream.
|
|
|
|
/// `trick_play_factor` defines the sampling rate.
|
|
|
|
uint32_t trick_play_factor = 0;
|
|
|
|
/// Optional user-specified content bit rate for the stream, in bits/sec.
|
|
|
|
/// If specified, this value is propagated to the `$Bandwidth$` template
|
|
|
|
/// parameter for segment names. If not specified, its value may be estimated.
|
|
|
|
uint32_t bandwidth = 0;
|
|
|
|
/// Optional value which contains a user-specified language tag. If specified,
|
|
|
|
/// this value overrides any language metadata in the input stream.
|
|
|
|
std::string language;
|
2020-12-11 20:58:26 +00:00
|
|
|
/// Optional value for the index of the sub-stream to use. For some text
|
|
|
|
/// formats, there are multiple "channels" in a single stream. This allows
|
|
|
|
/// selecting only one channel.
|
|
|
|
int32_t cc_index = -1;
|
2018-10-10 22:30:28 +00:00
|
|
|
|
2017-05-22 20:28:10 +00:00
|
|
|
/// Required for audio when outputting HLS. It defines the name of the output
|
|
|
|
/// stream, which is not necessarily the same as output. This is used as the
|
|
|
|
/// `NAME` attribute for EXT-X-MEDIA.
|
|
|
|
std::string hls_name;
|
|
|
|
/// Required for audio when outputting HLS. It defines the group ID for the
|
|
|
|
/// output stream. This is used as the GROUP-ID attribute for EXT-X-MEDIA.
|
|
|
|
std::string hls_group_id;
|
|
|
|
/// Required for HLS output. It defines the name of the playlist for the
|
|
|
|
/// stream. Usually ends with `.m3u8`.
|
|
|
|
std::string hls_playlist_name;
|
2018-02-01 20:27:30 +00:00
|
|
|
/// Optional for HLS output. It defines the name of the I-Frames only playlist
|
|
|
|
/// for the stream. For Video only. Usually ends with `.m3u8`.
|
|
|
|
std::string hls_iframe_playlist_name;
|
2018-10-10 22:30:28 +00:00
|
|
|
/// Optional for HLS output. It defines the CHARACTERISTICS attribute of the
|
|
|
|
/// stream.
|
|
|
|
std::vector<std::string> hls_characteristics;
|
2019-06-13 06:01:16 +00:00
|
|
|
|
|
|
|
/// Optional for DASH output. It defines Accessibility elements of the stream.
|
|
|
|
std::vector<std::string> dash_accessiblities;
|
|
|
|
/// Optional for DASH output. It defines Role elements of the stream.
|
|
|
|
std::vector<std::string> dash_roles;
|
2020-03-06 18:19:47 +00:00
|
|
|
|
|
|
|
/// Set to true to indicate that the stream is for dash only.
|
|
|
|
bool dash_only = false;
|
|
|
|
/// Set to true to indicate that the stream is for hls only.
|
|
|
|
bool hls_only = false;
|
2024-02-14 18:36:08 +00:00
|
|
|
|
feat: Allow LIVE UDP WebVTT input (#1349)
An updated version of PR #1027
That previous PR was done using 2021 code, and there were many changes
in the codebase from there, so a rebase was needed and also some minor
tweak here and there. But it's the same code, just reimplemented on a
newer codebase.
If you want to take a look at this in action, after building shaka
packager with this PR's code included, try this commands in 3 different
simultaneous bash sessions:
1. Video UDP input: `ffmpeg -f lavfi -re -i
"testsrc=s=320x240:r=30,format=yuv420p" -c:v h264 -sc_threshold 0 -g 30
-keyint_min 30 -r 30 -a53cc 1 -b:v 150k -preset ultrafast -r 30 -f
mpegts "udp://127.0.0.1:10000?pkt_size=1316"`
2. WebVTT UDP input: `for sec in $(seq 0 9999) ; do printf
"%02d:%02d.000 --> %02d:%02d.000\ntest second ${sec}\n\n" "$(( ${sec} /
60 ))" "$(( ${sec} % 60 ))" "$(( (${sec} + 1) / 60 ))" "$(( (${sec} + 1)
% 60 ))" ; sleep 1 ; done > /dev/udp/127.0.0.1/12345`
3. shaka packager command line: `timeout 60
path/to/build/packager/packager
'in=udp://127.0.0.1:10000?timeout=8000000,stream_selector=0,init_segment=240_init.m4s,segment_template=240_$Number%09d$.m4s,bandwidth=150000'
'in=udp://127.0.0.1:12345?timeout=8000000,stream_selector=0,input_format=webvtt,format=webvtt+mp4,init_segment=text_init.m4s,segment_template=text_$Number%09d$.m4s,language=eng,dash_roles=subtitle'
--mpd_output ./manifest.mpd --segment_duration 3.2
--suggested_presentation_delay 3.2 --min_buffer_time 3.2
--minimum_update_period 3.2 --time_shift_buffer_depth 60
--preserved_segments_outside_live_window 1 --default_language=eng
--dump_stream_info 2>&1`
Note the added `input_format=webvtt` to the shaka packager command's
second selector. That's new from this PR. If you don't use that, shaka's
format autodetection will not detect the webvtt format from the input,
as explained in
https://github.com/shaka-project/shaka-packager/issues/685#issuecomment-1029407191.
Try the command without it if you want to.
Fixes #685
Fixes #1017
---------
Co-authored-by: Daniel Cantarín <canta@canta.com.ar>
2024-02-24 00:02:19 +00:00
|
|
|
/// Optional value which specifies input container format.
|
|
|
|
/// Useful for live streaming situations, like auto-detecting webvtt without
|
|
|
|
/// its initial header.
|
|
|
|
std::string input_format;
|
|
|
|
|
2024-02-15 04:27:57 +00:00
|
|
|
/// Optional, indicates if this is a Forced Narrative subtitle stream.
|
|
|
|
bool forced_subtitle = false;
|
|
|
|
|
2024-02-14 18:36:08 +00:00
|
|
|
/// Optional for DASH output. It defines the Label element in Adaptation Set.
|
|
|
|
std::string dash_label;
|
2017-05-22 20:28:10 +00:00
|
|
|
};
|
|
|
|
|
2017-06-13 22:16:08 +00:00
|
|
|
class SHAKA_EXPORT Packager {
|
2017-05-22 20:28:10 +00:00
|
|
|
public:
|
2017-06-13 22:16:08 +00:00
|
|
|
Packager();
|
|
|
|
~Packager();
|
2017-05-22 20:28:10 +00:00
|
|
|
|
|
|
|
/// Initialize packaging pipeline.
|
|
|
|
/// @param packaging_params contains the packaging parameters.
|
|
|
|
/// @param stream_descriptors a list of stream descriptors.
|
|
|
|
/// @return OK on success, an appropriate error code on failure.
|
2023-12-01 17:32:19 +00:00
|
|
|
Status Initialize(const PackagingParams& packaging_params,
|
|
|
|
const std::vector<StreamDescriptor>& stream_descriptors);
|
2017-05-22 20:28:10 +00:00
|
|
|
|
|
|
|
/// Run the pipeline to completion (or failed / been cancelled). Note
|
|
|
|
/// that it blocks until completion.
|
|
|
|
/// @return OK on success, an appropriate error code on failure.
|
2017-06-29 22:23:53 +00:00
|
|
|
Status Run();
|
2017-05-22 20:28:10 +00:00
|
|
|
|
|
|
|
/// Cancel packaging. Note that it has to be called from another thread.
|
|
|
|
void Cancel();
|
|
|
|
|
2017-05-23 02:41:26 +00:00
|
|
|
/// @return The version of the library.
|
|
|
|
static std::string GetLibraryVersion();
|
|
|
|
|
2017-07-05 23:47:55 +00:00
|
|
|
/// Default stream label function implementation.
|
|
|
|
/// @param max_sd_pixels The threshold to determine whether a video track
|
|
|
|
/// should be considered as SD. If the max pixels per
|
|
|
|
/// frame is no higher than max_sd_pixels, i.e. [0,
|
|
|
|
/// max_sd_pixels], it is SD.
|
|
|
|
/// @param max_hd_pixels The threshold to determine whether a video track
|
|
|
|
/// should be considered as HD. If the max pixels per
|
|
|
|
/// frame is higher than max_sd_pixels, but no higher
|
|
|
|
/// than max_hd_pixels, i.e. (max_sd_pixels,
|
|
|
|
/// max_hd_pixels], it is HD.
|
|
|
|
/// @param max_uhd1_pixels The threshold to determine whether a video track
|
|
|
|
/// should be considered as UHD1. If the max pixels
|
|
|
|
/// per frame is higher than max_hd_pixels, but no
|
|
|
|
/// higher than max_uhd1_pixels, i.e. (max_hd_pixels,
|
|
|
|
/// max_uhd1_pixels], it is UHD1. Otherwise it is
|
|
|
|
/// UHD2.
|
|
|
|
/// @param stream_info Encrypted stream info.
|
|
|
|
/// @return the stream label associated with `stream_info`. Can be "AUDIO",
|
|
|
|
/// "SD", "HD", "UHD1" or "UHD2".
|
|
|
|
static std::string DefaultStreamLabelFunction(
|
|
|
|
int max_sd_pixels,
|
|
|
|
int max_hd_pixels,
|
|
|
|
int max_uhd1_pixels,
|
|
|
|
const EncryptionParams::EncryptedStreamAttributes& stream_attributes);
|
|
|
|
|
2017-05-22 20:28:10 +00:00
|
|
|
private:
|
2017-06-13 22:16:08 +00:00
|
|
|
Packager(const Packager&) = delete;
|
|
|
|
Packager& operator=(const Packager&) = delete;
|
2017-05-22 20:28:10 +00:00
|
|
|
|
|
|
|
struct PackagerInternal;
|
|
|
|
std::unique_ptr<PackagerInternal> internal_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace shaka
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#endif // PACKAGER_PUBLIC_PACKAGER_H_
|