2015-10-08 21:48:07 +00:00
|
|
|
// Copyright 2014 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#ifndef MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_
|
|
|
|
#define MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_
|
|
|
|
|
|
|
|
#include <deque>
|
|
|
|
#include <map>
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
2015-10-08 21:48:07 +00:00
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
|
2016-01-21 18:30:29 +00:00
|
|
|
#include "packager/base/compiler_specific.h"
|
2016-01-13 01:32:48 +00:00
|
|
|
#include "packager/media/base/decryptor_source.h"
|
2015-10-14 23:10:12 +00:00
|
|
|
#include "packager/media/base/media_parser.h"
|
|
|
|
#include "packager/media/base/media_sample.h"
|
2015-10-14 22:46:23 +00:00
|
|
|
#include "packager/media/formats/webm/webm_parser.h"
|
|
|
|
#include "packager/media/formats/webm/webm_tracks_parser.h"
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2015-10-08 21:48:07 +00:00
|
|
|
namespace media {
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
class WebMClusterParser : public WebMParserClient {
|
2015-10-08 21:48:07 +00:00
|
|
|
public:
|
2015-10-15 22:39:16 +00:00
|
|
|
/// Numbers chosen to estimate the duration of a buffer if none is set and
|
|
|
|
/// there is not enough information to get a better estimate.
|
2015-10-08 21:48:07 +00:00
|
|
|
enum {
|
2015-10-15 22:39:16 +00:00
|
|
|
/// Common 1k samples @44.1kHz
|
2015-10-08 21:48:07 +00:00
|
|
|
kDefaultAudioBufferDurationInMs = 23,
|
|
|
|
|
2015-10-15 22:39:16 +00:00
|
|
|
/// Chosen to represent 16fps duration, which will prevent MSE stalls in
|
|
|
|
/// videos with frame-rates as low as 8fps.
|
2015-10-08 21:48:07 +00:00
|
|
|
kDefaultVideoBufferDurationInMs = 63
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Helper class that manages per-track state.
|
|
|
|
class Track {
|
|
|
|
public:
|
|
|
|
Track(int track_num,
|
|
|
|
bool is_video,
|
2015-10-14 23:10:12 +00:00
|
|
|
int64_t default_duration,
|
|
|
|
const MediaParser::NewSampleCB& new_sample_cb);
|
2015-10-08 21:48:07 +00:00
|
|
|
~Track();
|
|
|
|
|
|
|
|
int track_num() const { return track_num_; }
|
|
|
|
|
|
|
|
// If |last_added_buffer_missing_duration_| is set, updates its duration
|
2015-10-15 22:39:16 +00:00
|
|
|
// relative to |buffer|'s timestamp, and emits it and unsets
|
|
|
|
// |last_added_buffer_missing_duration_|. Otherwise, if |buffer| is missing
|
|
|
|
// duration, saves |buffer| into |last_added_buffer_missing_duration_|.
|
|
|
|
bool EmitBuffer(const scoped_refptr<MediaSample>& buffer);
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2016-01-20 02:10:08 +00:00
|
|
|
// If |last_added_buffer_missing_duration_| is set, estimate the duration
|
|
|
|
// for this buffer using helper function GetDurationEstimate() then emits it
|
|
|
|
// and unsets |last_added_buffer_missing_duration_| (This method helps
|
|
|
|
// stream parser emit all buffers in a media segment).
|
2016-01-21 18:30:29 +00:00
|
|
|
bool ApplyDurationEstimateIfNeeded();
|
2015-10-08 21:48:07 +00:00
|
|
|
|
|
|
|
// Clears all buffer state, including any possibly held-aside buffer that
|
2015-10-15 22:39:16 +00:00
|
|
|
// was missing duration.
|
2015-10-08 21:48:07 +00:00
|
|
|
void Reset();
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Helper that sanity-checks |buffer| duration, updates
|
2015-10-15 22:39:16 +00:00
|
|
|
// |estimated_next_frame_duration_|, and emits |buffer|.
|
|
|
|
// Returns false if |buffer| failed sanity check and therefore was not
|
|
|
|
// emitted. Returns true otherwise.
|
|
|
|
bool EmitBufferHelp(const scoped_refptr<MediaSample>& buffer);
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2016-01-20 02:10:08 +00:00
|
|
|
// Helper function that calculates the buffer duration to use in
|
2015-10-08 21:48:07 +00:00
|
|
|
// ApplyDurationEstimateIfNeeded().
|
2015-10-14 23:10:12 +00:00
|
|
|
int64_t GetDurationEstimate();
|
2015-10-08 21:48:07 +00:00
|
|
|
|
|
|
|
int track_num_;
|
|
|
|
bool is_video_;
|
|
|
|
|
2016-01-20 02:10:08 +00:00
|
|
|
// Holding the sample that is missing duration. The duration will be
|
|
|
|
// computed from the difference in timestamp when next sample arrives; or
|
|
|
|
// estimated if it is the last sample in this track.
|
2015-10-14 23:10:12 +00:00
|
|
|
scoped_refptr<MediaSample> last_added_buffer_missing_duration_;
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2015-10-15 22:39:16 +00:00
|
|
|
// If kNoTimestamp, then |estimated_next_frame_duration_| will be used.
|
2015-10-14 23:10:12 +00:00
|
|
|
int64_t default_duration_;
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2016-01-20 02:10:08 +00:00
|
|
|
// If kNoTimestamp, then a hardcoded default value will be used. This
|
|
|
|
// estimate is the maximum duration seen so far for this track, and is used
|
|
|
|
// only if |default_duration_| is kNoTimestamp.
|
2015-10-14 23:10:12 +00:00
|
|
|
int64_t estimated_next_frame_duration_;
|
|
|
|
|
|
|
|
MediaParser::NewSampleCB new_sample_cb_;
|
2015-10-08 21:48:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::map<int, Track> TextTrackMap;
|
|
|
|
|
|
|
|
public:
|
2016-01-13 01:32:48 +00:00
|
|
|
/// Create a WebMClusterParser from given parameters.
|
|
|
|
/// @param timecode_scale indicates timecode scale for the clusters.
|
|
|
|
/// @param audio_stream_info references audio stream information. It will
|
|
|
|
/// be NULL if there are no audio tracks available.
|
|
|
|
/// @param video_stream_info references video stream information. It will
|
|
|
|
/// be NULL if there are no video tracks available.
|
|
|
|
/// @param audio_default_duration indicates default duration for audio
|
|
|
|
/// samples.
|
|
|
|
/// @param video_default_duration indicates default duration for video
|
|
|
|
/// samples.
|
|
|
|
/// @param text_tracks contains text track information.
|
|
|
|
/// @param ignored_tracks contains a list of ignored track ids.
|
|
|
|
/// @param audio_encryption_key_id indicates the encryption key id for audio
|
|
|
|
/// samples if there is an audio stream and the audio stream is
|
|
|
|
/// encrypted. It is empty otherwise.
|
|
|
|
/// @param video_encryption_key_id indicates the encryption key id for video
|
|
|
|
/// samples if there is a video stream and the video stream is
|
|
|
|
/// encrypted. It is empty otherwise.
|
|
|
|
/// @param new_sample_cb is the callback to emit new samples.
|
|
|
|
/// @param init_cb is the callback to initialize streams.
|
|
|
|
/// @param decryption_key_source points to a decryption key source to fetch
|
|
|
|
/// decryption keys. Should not be NULL if the tracks are encrypted.
|
2015-10-14 22:46:23 +00:00
|
|
|
WebMClusterParser(int64_t timecode_scale,
|
2015-11-18 19:51:15 +00:00
|
|
|
scoped_refptr<AudioStreamInfo> audio_stream_info,
|
|
|
|
scoped_refptr<VideoStreamInfo> video_stream_info,
|
2015-10-14 23:10:12 +00:00
|
|
|
int64_t audio_default_duration,
|
|
|
|
int64_t video_default_duration,
|
2015-10-08 21:48:07 +00:00
|
|
|
const WebMTracksParser::TextTracks& text_tracks,
|
2015-10-14 22:46:23 +00:00
|
|
|
const std::set<int64_t>& ignored_tracks,
|
2015-10-08 21:48:07 +00:00
|
|
|
const std::string& audio_encryption_key_id,
|
|
|
|
const std::string& video_encryption_key_id,
|
2015-11-18 19:51:15 +00:00
|
|
|
const MediaParser::NewSampleCB& new_sample_cb,
|
2016-01-13 01:32:48 +00:00
|
|
|
const MediaParser::InitCB& init_cb,
|
|
|
|
KeySource* decryption_key_source);
|
2015-10-08 21:48:07 +00:00
|
|
|
~WebMClusterParser() override;
|
|
|
|
|
2015-10-15 22:39:16 +00:00
|
|
|
/// Resets the parser state so it can accept a new cluster.
|
2015-10-08 21:48:07 +00:00
|
|
|
void Reset();
|
|
|
|
|
2015-12-14 20:33:18 +00:00
|
|
|
/// Flush data currently in the parser and reset the parser so it can accept a
|
|
|
|
/// new cluster.
|
2016-01-21 18:30:29 +00:00
|
|
|
/// @return true on success, false otherwise.
|
|
|
|
bool Flush() WARN_UNUSED_RESULT;
|
2015-12-14 20:33:18 +00:00
|
|
|
|
2015-10-15 22:39:16 +00:00
|
|
|
/// Parses a WebM cluster element in |buf|.
|
|
|
|
/// @return -1 if the parse fails.
|
|
|
|
/// @return 0 if more data is needed.
|
|
|
|
/// @return The number of bytes parsed on success.
|
2015-10-08 21:48:07 +00:00
|
|
|
int Parse(const uint8_t* buf, int size);
|
|
|
|
|
2015-10-14 23:10:12 +00:00
|
|
|
int64_t cluster_start_time() const { return cluster_start_time_; }
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2015-10-15 22:39:16 +00:00
|
|
|
/// @return true if the last Parse() call stopped at the end of a cluster.
|
2015-10-08 21:48:07 +00:00
|
|
|
bool cluster_ended() const { return cluster_ended_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// WebMParserClient methods.
|
|
|
|
WebMParserClient* OnListStart(int id) override;
|
|
|
|
bool OnListEnd(int id) override;
|
2015-10-14 22:46:23 +00:00
|
|
|
bool OnUInt(int id, int64_t val) override;
|
2015-10-08 21:48:07 +00:00
|
|
|
bool OnBinary(int id, const uint8_t* data, int size) override;
|
|
|
|
|
|
|
|
bool ParseBlock(bool is_simple_block,
|
|
|
|
const uint8_t* buf,
|
|
|
|
int size,
|
|
|
|
const uint8_t* additional,
|
|
|
|
int additional_size,
|
|
|
|
int duration,
|
2016-07-29 23:06:20 +00:00
|
|
|
int64_t discard_padding,
|
|
|
|
bool reference_block_set);
|
2015-10-08 21:48:07 +00:00
|
|
|
bool OnBlock(bool is_simple_block,
|
|
|
|
int track_num,
|
|
|
|
int timecode,
|
|
|
|
int duration,
|
|
|
|
const uint8_t* data,
|
|
|
|
int size,
|
|
|
|
const uint8_t* additional,
|
|
|
|
int additional_size,
|
2016-07-29 23:06:20 +00:00
|
|
|
int64_t discard_padding,
|
|
|
|
bool is_key_frame);
|
2015-10-08 21:48:07 +00:00
|
|
|
|
|
|
|
// Resets the Track objects associated with each text track.
|
|
|
|
void ResetTextTracks();
|
|
|
|
|
|
|
|
// Search for the indicated track_num among the text tracks. Returns NULL
|
|
|
|
// if that track num is not a text track.
|
|
|
|
Track* FindTextTrack(int track_num);
|
|
|
|
|
2016-01-20 02:10:08 +00:00
|
|
|
// Multiplier used to convert timecodes into microseconds.
|
|
|
|
double timecode_multiplier_;
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2015-11-18 19:51:15 +00:00
|
|
|
scoped_refptr<AudioStreamInfo> audio_stream_info_;
|
|
|
|
scoped_refptr<VideoStreamInfo> video_stream_info_;
|
2015-10-14 22:46:23 +00:00
|
|
|
std::set<int64_t> ignored_tracks_;
|
2016-01-13 01:32:48 +00:00
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<DecryptorSource> decryptor_source_;
|
2015-10-08 21:48:07 +00:00
|
|
|
std::string audio_encryption_key_id_;
|
|
|
|
std::string video_encryption_key_id_;
|
|
|
|
|
|
|
|
WebMListParser parser_;
|
|
|
|
|
2015-11-18 19:51:15 +00:00
|
|
|
// Indicates whether init_cb has been executed. |init_cb| is executed when we
|
|
|
|
// have codec configuration of video stream, which is extracted from the first
|
|
|
|
// video sample.
|
|
|
|
bool initialized_;
|
|
|
|
MediaParser::InitCB init_cb_;
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
int64_t last_block_timecode_ = -1;
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<uint8_t[]> block_data_;
|
2015-10-08 21:48:07 +00:00
|
|
|
int block_data_size_ = -1;
|
2015-10-14 22:46:23 +00:00
|
|
|
int64_t block_duration_ = -1;
|
|
|
|
int64_t block_add_id_ = -1;
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<uint8_t[]> block_additional_data_;
|
2015-10-08 21:48:07 +00:00
|
|
|
// Must be 0 if |block_additional_data_| is null. Must be > 0 if
|
|
|
|
// |block_additional_data_| is NOT null.
|
|
|
|
int block_additional_data_size_ = 0;
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
int64_t discard_padding_ = -1;
|
2015-10-08 21:48:07 +00:00
|
|
|
bool discard_padding_set_ = false;
|
|
|
|
|
2016-07-29 23:06:20 +00:00
|
|
|
bool reference_block_set_ = false;
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
int64_t cluster_timecode_ = -1;
|
2015-10-14 23:10:12 +00:00
|
|
|
int64_t cluster_start_time_;
|
2015-10-08 21:48:07 +00:00
|
|
|
bool cluster_ended_ = false;
|
|
|
|
|
|
|
|
Track audio_;
|
|
|
|
Track video_;
|
|
|
|
TextTrackMap text_track_map_;
|
|
|
|
|
2016-01-20 02:10:08 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(WebMClusterParser);
|
2015-10-08 21:48:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2015-10-08 21:48:07 +00:00
|
|
|
|
|
|
|
#endif // MEDIA_FORMATS_WEBM_WEBM_CLUSTER_PARSER_H_
|