2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-02-14 23:21:05 +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
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#ifndef PACKAGER_MEDIA_FORMATS_MP4_SEGMENTER_H_
|
|
|
|
#define PACKAGER_MEDIA_FORMATS_MP4_SEGMENTER_H_
|
2013-11-12 20:37:58 +00:00
|
|
|
|
|
|
|
#include <map>
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
2023-10-11 08:49:50 +00:00
|
|
|
#include <optional>
|
2013-11-12 20:37:58 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2023-10-14 16:36:01 +00:00
|
|
|
#include <packager/macros/classes.h>
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/base/fourccs.h>
|
|
|
|
#include <packager/media/base/range.h>
|
|
|
|
#include <packager/media/formats/mp4/box_definitions.h>
|
2023-10-14 16:36:01 +00:00
|
|
|
#include <packager/status.h>
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-11-12 20:37:58 +00:00
|
|
|
namespace media {
|
|
|
|
|
2017-03-11 02:49:55 +00:00
|
|
|
struct EncryptionConfig;
|
2013-11-12 20:37:58 +00:00
|
|
|
struct MuxerOptions;
|
2017-03-11 02:49:55 +00:00
|
|
|
struct SegmentInfo;
|
2013-11-12 20:37:58 +00:00
|
|
|
|
|
|
|
class BufferWriter;
|
|
|
|
class MediaSample;
|
2014-05-22 18:51:55 +00:00
|
|
|
class MuxerListener;
|
2015-05-11 21:07:10 +00:00
|
|
|
class ProgressListener;
|
2017-02-21 18:36:50 +00:00
|
|
|
class StreamInfo;
|
2014-05-22 18:51:55 +00:00
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
namespace mp4 {
|
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
class Fragmenter;
|
2018-02-01 20:25:07 +00:00
|
|
|
struct KeyFrameInfo;
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
/// This class defines the Segmenter which is responsible for organizing
|
|
|
|
/// fragments into segments/subsegments and package them into a MP4 file.
|
|
|
|
/// Inherited by MultiSegmentSegmenter and SingleSegmentSegmenter.
|
|
|
|
/// SingleSegmentSegmenter defines the Segmenter for DASH Video-On-Demand with
|
|
|
|
/// a single segment for each media presentation while MultiSegmentSegmenter
|
|
|
|
/// handles all other cases including DASH live profile.
|
|
|
|
class Segmenter {
|
2013-11-12 20:37:58 +00:00
|
|
|
public:
|
2014-04-08 20:21:07 +00:00
|
|
|
Segmenter(const MuxerOptions& options,
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<FileType> ftyp,
|
|
|
|
std::unique_ptr<Movie> moov);
|
2014-04-08 20:21:07 +00:00
|
|
|
virtual ~Segmenter();
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Initialize the segmenter.
|
|
|
|
/// Calling other public methods of this class without this method returning
|
2014-04-18 22:00:30 +00:00
|
|
|
/// Status::OK results in an undefined behavior.
|
2017-12-18 21:28:19 +00:00
|
|
|
/// @param streams contains the vector of StreamInfos for initialization.
|
2015-05-11 21:07:10 +00:00
|
|
|
/// @param muxer_listener receives muxer events. Can be NULL.
|
|
|
|
/// @param progress_listener receives progress updates. Can be NULL.
|
2014-04-18 22:00:30 +00:00
|
|
|
/// @return OK on success, an error status otherwise.
|
2017-09-12 17:24:24 +00:00
|
|
|
Status Initialize(
|
|
|
|
const std::vector<std::shared_ptr<const StreamInfo>>& streams,
|
|
|
|
MuxerListener* muxer_listener,
|
|
|
|
ProgressListener* progress_listener);
|
2014-04-18 22:00:30 +00:00
|
|
|
|
|
|
|
/// Finalize the segmenter.
|
|
|
|
/// @return OK on success, an error status otherwise.
|
|
|
|
Status Finalize();
|
|
|
|
|
|
|
|
/// Add sample to the indicated stream.
|
2017-02-24 01:17:47 +00:00
|
|
|
/// @param stream_id is the zero-based stream index.
|
2014-04-18 22:00:30 +00:00
|
|
|
/// @param sample points to the sample to be added.
|
|
|
|
/// @return OK on success, an error status otherwise.
|
2017-09-12 17:24:24 +00:00
|
|
|
Status AddSample(size_t stream_id, const MediaSample& sample);
|
2017-02-24 01:17:47 +00:00
|
|
|
|
|
|
|
/// Finalize the segment / subsegment.
|
|
|
|
/// @param stream_id is the zero-based stream index.
|
|
|
|
/// @param is_subsegment indicates if it is a subsegment (fragment).
|
|
|
|
/// @return OK on success, an error status otherwise.
|
2017-09-12 17:24:24 +00:00
|
|
|
Status FinalizeSegment(size_t stream_id, const SegmentInfo& segment_info);
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2017-05-01 23:17:09 +00:00
|
|
|
// TODO(rkuroiwa): Change these Get*Range() methods to return
|
2023-08-31 23:59:46 +00:00
|
|
|
// std::optional<Range> as well.
|
2014-01-23 22:34:39 +00:00
|
|
|
/// @return true if there is an initialization range, while setting @a offset
|
|
|
|
/// and @a size; or false if initialization range does not apply.
|
2013-12-12 23:49:31 +00:00
|
|
|
virtual bool GetInitRange(size_t* offset, size_t* size) = 0;
|
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// @return true if there is an index byte range, while setting @a offset
|
|
|
|
/// and @a size; or false if index byte range does not apply.
|
2013-12-12 23:49:31 +00:00
|
|
|
virtual bool GetIndexRange(size_t* offset, size_t* size) = 0;
|
|
|
|
|
2017-05-01 23:17:09 +00:00
|
|
|
// Returns an empty vector if there are no specific ranges for the segments,
|
|
|
|
// e.g. the media is in multiple files.
|
|
|
|
// Otherwise, a vector of ranges for the media segments are returned.
|
|
|
|
virtual std::vector<Range> GetSegmentRanges() = 0;
|
|
|
|
|
2021-08-04 18:56:44 +00:00
|
|
|
int32_t GetReferenceTimeScale() const;
|
2013-12-12 23:49:31 +00:00
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// @return The total length, in seconds, of segmented media files.
|
2013-12-12 23:49:31 +00:00
|
|
|
double GetDuration() const;
|
|
|
|
|
2015-06-15 21:12:42 +00:00
|
|
|
/// @return The sample duration in the timescale of the media.
|
|
|
|
/// Returns 0 if no samples are added yet.
|
2021-08-04 18:56:44 +00:00
|
|
|
int32_t sample_duration() const { return sample_duration_; }
|
2015-06-15 21:12:42 +00:00
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
protected:
|
2015-05-11 21:07:10 +00:00
|
|
|
/// Update segmentation progress using ProgressListener.
|
|
|
|
void UpdateProgress(uint64_t progress);
|
|
|
|
/// Set progress to 100%.
|
|
|
|
void SetComplete();
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
const MuxerOptions& options() const { return options_; }
|
|
|
|
FileType* ftyp() { return ftyp_.get(); }
|
|
|
|
Movie* moov() { return moov_.get(); }
|
|
|
|
BufferWriter* fragment_buffer() { return fragment_buffer_.get(); }
|
|
|
|
SegmentIndex* sidx() { return sidx_.get(); }
|
2015-05-07 21:06:16 +00:00
|
|
|
MuxerListener* muxer_listener() { return muxer_listener_; }
|
2015-05-11 21:07:10 +00:00
|
|
|
uint64_t progress_target() { return progress_target_; }
|
2018-02-01 20:25:07 +00:00
|
|
|
const std::vector<KeyFrameInfo>& key_frame_infos() const {
|
|
|
|
return key_frame_infos_;
|
|
|
|
}
|
2015-05-11 21:07:10 +00:00
|
|
|
|
|
|
|
void set_progress_target(uint64_t progress_target) {
|
|
|
|
progress_target_ = progress_target;
|
|
|
|
}
|
2013-11-12 20:37:58 +00:00
|
|
|
|
|
|
|
private:
|
2014-04-18 22:00:30 +00:00
|
|
|
virtual Status DoInitialize() = 0;
|
|
|
|
virtual Status DoFinalize() = 0;
|
|
|
|
virtual Status DoFinalizeSegment() = 0;
|
|
|
|
|
2021-08-25 15:38:05 +00:00
|
|
|
virtual Status DoFinalizeChunk() { return Status::OK; }
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t GetReferenceStreamId();
|
2014-04-18 22:00:30 +00:00
|
|
|
|
2017-03-11 02:49:55 +00:00
|
|
|
void FinalizeFragmentForKeyRotation(
|
|
|
|
size_t stream_id,
|
|
|
|
bool fragment_encrypted,
|
|
|
|
const EncryptionConfig& encryption_config);
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
const MuxerOptions& options_;
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<FileType> ftyp_;
|
|
|
|
std::unique_ptr<Movie> moov_;
|
|
|
|
std::unique_ptr<MovieFragment> moof_;
|
|
|
|
std::unique_ptr<BufferWriter> fragment_buffer_;
|
|
|
|
std::unique_ptr<SegmentIndex> sidx_;
|
2016-08-30 23:01:19 +00:00
|
|
|
std::vector<std::unique_ptr<Fragmenter>> fragmenters_;
|
2017-03-15 19:42:00 +00:00
|
|
|
MuxerListener* muxer_listener_ = nullptr;
|
|
|
|
ProgressListener* progress_listener_ = nullptr;
|
|
|
|
uint64_t progress_target_ = 0u;
|
|
|
|
uint64_t accumulated_progress_ = 0u;
|
2021-08-04 18:56:44 +00:00
|
|
|
int32_t sample_duration_ = 0;
|
2017-03-15 19:42:00 +00:00
|
|
|
std::vector<uint64_t> stream_durations_;
|
2018-02-01 20:25:07 +00:00
|
|
|
std::vector<KeyFrameInfo> key_frame_infos_;
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(Segmenter);
|
2013-11-12 20:37:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mp4
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#endif // PACKAGER_MEDIA_FORMATS_MP4_SEGMENTER_H_
|