2015-10-28 17:23:08 +00:00
|
|
|
// Copyright 2015 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
|
|
|
#ifndef MEDIA_FORMATS_WEBM_SEGMENTER_H_
|
|
|
|
#define MEDIA_FORMATS_WEBM_SEGMENTER_H_
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
2015-10-28 17:23:08 +00:00
|
|
|
#include "packager/media/base/status.h"
|
2015-11-18 21:11:31 +00:00
|
|
|
#include "packager/media/formats/webm/encryptor.h"
|
2015-10-28 17:23:08 +00:00
|
|
|
#include "packager/media/formats/webm/mkv_writer.h"
|
|
|
|
#include "packager/media/formats/webm/seek_head.h"
|
|
|
|
#include "packager/third_party/libwebm/src/mkvmuxer.hpp"
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2015-10-28 17:23:08 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
struct MuxerOptions;
|
|
|
|
|
|
|
|
class AudioStreamInfo;
|
|
|
|
class KeySource;
|
|
|
|
class MediaSample;
|
2015-12-22 00:33:55 +00:00
|
|
|
class StreamInfo;
|
2015-10-28 17:23:08 +00:00
|
|
|
class MuxerListener;
|
|
|
|
class ProgressListener;
|
|
|
|
class StreamInfo;
|
|
|
|
class VideoStreamInfo;
|
|
|
|
|
|
|
|
namespace webm {
|
|
|
|
|
|
|
|
class Segmenter {
|
|
|
|
public:
|
|
|
|
explicit Segmenter(const MuxerOptions& options);
|
|
|
|
virtual ~Segmenter();
|
|
|
|
|
|
|
|
/// Initialize the segmenter.
|
|
|
|
/// Calling other public methods of this class without this method returning
|
|
|
|
/// Status::OK results in an undefined behavior.
|
|
|
|
/// @param writer contains the output file (or init file in multi-segment).
|
2015-12-22 00:33:55 +00:00
|
|
|
/// @param info The stream info for the stream being segmented.
|
2015-10-28 17:23:08 +00:00
|
|
|
/// @param muxer_listener receives muxer events. Can be NULL.
|
|
|
|
/// @param encryption_key_source points to the key source which contains
|
|
|
|
/// the encryption keys. It can be NULL to indicate that no encryption
|
|
|
|
/// is required.
|
2015-11-18 21:11:31 +00:00
|
|
|
/// @param max_sd_pixels specifies the threshold to determine whether a video
|
2016-11-11 23:17:17 +00:00
|
|
|
/// track should be considered as SD. If the max pixels per frame is
|
|
|
|
/// no higher than max_sd_pixels, it is SD.
|
|
|
|
/// @param max_hd_pixels specifies 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,
|
|
|
|
/// it is HD.
|
|
|
|
/// @param max_uhd1_pixels specifies 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,
|
|
|
|
/// it is UHD1. Otherwise it is UHD2.
|
2015-11-18 21:11:31 +00:00
|
|
|
/// @param clear_time specifies clear lead duration in seconds.
|
2015-10-28 17:23:08 +00:00
|
|
|
/// @return OK on success, an error status otherwise.
|
2016-08-17 17:41:40 +00:00
|
|
|
Status Initialize(std::unique_ptr<MkvWriter> writer,
|
2015-12-22 00:33:55 +00:00
|
|
|
StreamInfo* info,
|
2015-10-28 17:23:08 +00:00
|
|
|
ProgressListener* progress_listener,
|
|
|
|
MuxerListener* muxer_listener,
|
2015-11-18 21:11:31 +00:00
|
|
|
KeySource* encryption_key_source,
|
|
|
|
uint32_t max_sd_pixels,
|
2016-11-11 23:17:17 +00:00
|
|
|
uint32_t max_hd_pixels,
|
|
|
|
uint32_t max_uhd1_pixels,
|
2015-11-18 21:11:31 +00:00
|
|
|
double clear_lead_in_seconds);
|
2015-10-28 17:23:08 +00:00
|
|
|
|
|
|
|
/// Finalize the segmenter.
|
|
|
|
/// @return OK on success, an error status otherwise.
|
|
|
|
Status Finalize();
|
|
|
|
|
|
|
|
/// Add sample to the indicated stream.
|
|
|
|
/// @param sample points to the sample to be added.
|
|
|
|
/// @return OK on success, an error status otherwise.
|
2017-01-24 00:55:02 +00:00
|
|
|
Status AddSample(std::shared_ptr<MediaSample> sample);
|
2015-10-28 17:23:08 +00:00
|
|
|
|
|
|
|
/// @return true if there is an initialization range, while setting @a start
|
|
|
|
/// and @a end; or false if initialization range does not apply.
|
2016-04-15 23:00:27 +00:00
|
|
|
virtual bool GetInitRangeStartAndEnd(uint64_t* start, uint64_t* end) = 0;
|
2015-10-28 17:23:08 +00:00
|
|
|
|
|
|
|
/// @return true if there is an index byte range, while setting @a start
|
|
|
|
/// and @a end; or false if index byte range does not apply.
|
2016-04-15 23:00:27 +00:00
|
|
|
virtual bool GetIndexRangeStartAndEnd(uint64_t* start, uint64_t* end) = 0;
|
2015-10-28 17:23:08 +00:00
|
|
|
|
|
|
|
/// @return The total length, in seconds, of segmented media files.
|
|
|
|
float GetDuration() const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/// Converts the given time in ISO BMFF timescale to the current WebM
|
|
|
|
/// timecode.
|
|
|
|
uint64_t FromBMFFTimescale(uint64_t time_timescale);
|
|
|
|
/// Converts the given time in WebM timecode to ISO BMFF timescale.
|
|
|
|
uint64_t FromWebMTimecode(uint64_t time_webm_timecode);
|
|
|
|
/// Writes the Segment header to @a writer.
|
|
|
|
Status WriteSegmentHeader(uint64_t file_size, MkvWriter* writer);
|
|
|
|
/// Creates a Cluster object with the given parameters.
|
|
|
|
Status SetCluster(uint64_t start_webm_timecode,
|
|
|
|
uint64_t position,
|
|
|
|
MkvWriter* writer);
|
|
|
|
|
|
|
|
/// Update segmentation progress using ProgressListener.
|
|
|
|
void UpdateProgress(uint64_t progress);
|
|
|
|
void set_progress_target(uint64_t target) { progress_target_ = target; }
|
|
|
|
|
|
|
|
const MuxerOptions& options() const { return options_; }
|
|
|
|
mkvmuxer::Cluster* cluster() { return cluster_.get(); }
|
|
|
|
mkvmuxer::Cues* cues() { return &cues_; }
|
|
|
|
MuxerListener* muxer_listener() { return muxer_listener_; }
|
2015-12-22 00:33:55 +00:00
|
|
|
StreamInfo* info() { return info_; }
|
2015-10-28 17:23:08 +00:00
|
|
|
SeekHead* seek_head() { return &seek_head_; }
|
|
|
|
|
|
|
|
int track_id() const { return track_id_; }
|
|
|
|
uint64_t segment_payload_pos() const { return segment_payload_pos_; }
|
2016-09-30 00:26:33 +00:00
|
|
|
uint64_t cluster_length_in_time_scale() const {
|
|
|
|
return cluster_length_in_time_scale_;
|
|
|
|
}
|
2015-10-28 17:23:08 +00:00
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
virtual Status DoInitialize(std::unique_ptr<MkvWriter> writer) = 0;
|
2015-10-28 17:23:08 +00:00
|
|
|
virtual Status DoFinalize() = 0;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Status CreateVideoTrack(VideoStreamInfo* info);
|
|
|
|
Status CreateAudioTrack(AudioStreamInfo* info);
|
2016-11-11 23:17:17 +00:00
|
|
|
Status InitializeEncryptor(KeySource* key_source, uint32_t max_sd_pixels,
|
|
|
|
uint32_t max_hd_pixels, uint32_t max_uhd1_pixels);
|
2015-10-28 17:23:08 +00:00
|
|
|
|
2016-01-21 23:58:13 +00:00
|
|
|
// Writes the previous frame to the file.
|
|
|
|
Status WriteFrame(bool write_duration);
|
|
|
|
|
2015-10-28 17:23:08 +00:00
|
|
|
// This is called when there needs to be a new subsegment. This does nothing
|
|
|
|
// in single-segment mode. In multi-segment mode this creates a new Cluster
|
|
|
|
// element.
|
|
|
|
virtual Status NewSubsegment(uint64_t start_timescale) = 0;
|
|
|
|
// This is called when there needs to be a new segment. In single-segment
|
|
|
|
// mode, this creates a new Cluster element. In multi-segment mode this
|
|
|
|
// creates a new output file.
|
|
|
|
virtual Status NewSegment(uint64_t start_timescale) = 0;
|
2016-01-21 23:58:13 +00:00
|
|
|
|
|
|
|
// Store the previous sample so we know which one is the last frame.
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<MediaSample> prev_sample_;
|
2016-01-21 23:58:13 +00:00
|
|
|
// The reference frame timestamp; used to populate the ReferenceBlock element
|
|
|
|
// when writing non-keyframe BlockGroups.
|
|
|
|
uint64_t reference_frame_timestamp_;
|
2015-10-28 17:23:08 +00:00
|
|
|
|
|
|
|
const MuxerOptions& options_;
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<Encryptor> encryptor_;
|
2015-11-18 21:11:31 +00:00
|
|
|
double clear_lead_;
|
2016-08-02 23:43:23 +00:00
|
|
|
bool enable_encryption_; // Encryption is enabled only after clear_lead_.
|
2015-10-28 17:23:08 +00:00
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<mkvmuxer::Cluster> cluster_;
|
2015-10-28 17:23:08 +00:00
|
|
|
mkvmuxer::Cues cues_;
|
|
|
|
SeekHead seek_head_;
|
|
|
|
mkvmuxer::SegmentInfo segment_info_;
|
|
|
|
mkvmuxer::Tracks tracks_;
|
|
|
|
|
2015-12-22 00:33:55 +00:00
|
|
|
StreamInfo* info_;
|
2015-10-28 17:23:08 +00:00
|
|
|
MuxerListener* muxer_listener_;
|
|
|
|
ProgressListener* progress_listener_;
|
|
|
|
uint64_t progress_target_;
|
|
|
|
uint64_t accumulated_progress_;
|
2016-01-21 23:58:13 +00:00
|
|
|
uint64_t first_timestamp_;
|
|
|
|
int64_t sample_duration_;
|
2015-10-28 17:23:08 +00:00
|
|
|
// The position (in bytes) of the start of the Segment payload in the init
|
|
|
|
// file. This is also the size of the header before the SeekHead.
|
|
|
|
uint64_t segment_payload_pos_;
|
|
|
|
|
2016-09-30 00:26:33 +00:00
|
|
|
// Durations in timescale.
|
|
|
|
uint64_t cluster_length_in_time_scale_;
|
|
|
|
uint64_t segment_length_in_time_scale_;
|
2015-10-28 17:23:08 +00:00
|
|
|
|
|
|
|
int track_id_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(Segmenter);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace webm
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2015-10-28 17:23:08 +00:00
|
|
|
|
|
|
|
#endif // MEDIA_FORMATS_WEBM_SEGMENTER_H_
|