7 #include "packager/media/formats/webm/segmenter.h"
9 #include "packager/base/time/time.h"
10 #include "packager/media/base/audio_stream_info.h"
11 #include "packager/media/base/media_sample.h"
12 #include "packager/media/base/media_stream.h"
13 #include "packager/media/base/muxer_options.h"
14 #include "packager/media/base/stream_info.h"
15 #include "packager/media/base/video_stream_info.h"
16 #include "packager/media/event/muxer_listener.h"
17 #include "packager/media/event/progress_listener.h"
18 #include "packager/third_party/libwebm/src/mkvmuxerutil.hpp"
19 #include "packager/third_party/libwebm/src/webmids.hpp"
21 namespace edash_packager {
25 int64_t kTimecodeScale = 1000000;
26 int64_t kSecondsToNs = 1000000000L;
29 Segmenter::Segmenter(
const MuxerOptions& options)
32 muxer_listener_(NULL),
33 progress_listener_(NULL),
35 accumulated_progress_(0),
38 segment_payload_pos_(0),
39 cluster_length_sec_(0),
40 segment_length_sec_(0),
43 Segmenter::~Segmenter() {}
50 muxer_listener_ = muxer_listener;
54 progress_target_ = info_->duration();
55 progress_listener_ = progress_listener;
58 segment_info_.set_timecode_scale(kTimecodeScale);
59 if (options().single_segment) {
62 segment_info_.set_duration(1);
67 switch (info_->stream_type()) {
69 status = CreateVideoTrack(static_cast<VideoStreamInfo*>(info_));
72 status = CreateAudioTrack(static_cast<AudioStreamInfo*>(info_));
75 NOTIMPLEMENTED() <<
"Not implemented for stream type: "
76 << info_->stream_type();
77 status =
Status(error::UNIMPLEMENTED,
"Not implemented for stream type");
82 return DoInitialize(writer.Pass());
86 segment_info_.set_duration(FromBMFFTimescale(total_duration_));
91 if (sample_duration_ == 0) {
92 sample_duration_ = sample->duration();
102 status = NewSegment(sample->pts());
105 status = NewSegment(sample->pts());
106 segment_length_sec_ = 0;
107 cluster_length_sec_ = 0;
111 status = NewSubsegment(sample->pts());
112 cluster_length_sec_ = 0;
118 const int64_t time_ns =
119 sample->pts() * kSecondsToNs / info_->time_scale();
120 if (!cluster_->AddFrame(sample->data(), sample->data_size(), track_id_,
121 time_ns, sample->is_key_frame())) {
122 LOG(ERROR) <<
"Error adding sample to segment.";
123 return Status(error::FILE_FAILURE,
"Error adding sample to segment.");
125 const double duration_sec =
126 static_cast<double>(sample->duration()) / info_->time_scale();
127 cluster_length_sec_ += duration_sec;
128 segment_length_sec_ += duration_sec;
129 total_duration_ += sample->duration();
135 return static_cast<float>(segment_info_.duration()) *
136 segment_info_.timecode_scale() / kSecondsToNs;
139 uint64_t Segmenter::FromBMFFTimescale(uint64_t time_timescale) {
141 const int64_t time_ns =
142 kSecondsToNs * time_timescale / info_->time_scale();
143 return time_ns / segment_info_.timecode_scale();
146 uint64_t Segmenter::FromWebMTimecode(uint64_t time_webm_timecode) {
148 const int64_t time_ns = time_webm_timecode * segment_info_.timecode_scale();
149 return time_ns * info_->time_scale() / kSecondsToNs;
153 Status error_status(error::FILE_FAILURE,
"Error writing segment header.");
155 if (!WriteEbmlHeader(writer))
158 if (WriteID(writer, mkvmuxer::kMkvSegment) != 0)
161 const uint64_t segment_size_size = 8;
162 segment_payload_pos_ = writer->
Position() + segment_size_size;
165 if (WriteUIntSize(writer, file_size - segment_payload_pos_,
166 segment_size_size) != 0)
168 if (!seek_head_.Write(writer))
171 if (SerializeInt(writer, mkvmuxer::kEbmlUnknownValue, segment_size_size) !=
175 if (!seek_head_.WriteVoid(writer))
179 if (!segment_info_.Write(writer))
182 if (!tracks_.Write(writer))
188 Status Segmenter::SetCluster(uint64_t start_webm_timecode,
191 const uint64_t scale = segment_info_.timecode_scale();
192 cluster_.reset(
new mkvmuxer::Cluster(start_webm_timecode, position, scale));
193 cluster_->Init(writer);
198 accumulated_progress_ += progress;
199 if (!progress_listener_ || progress_target_ == 0)
204 if (accumulated_progress_ >= progress_target_) {
207 progress_listener_->
OnProgress(static_cast<double>(accumulated_progress_) /
214 unsigned int seed = 0;
215 mkvmuxer::VideoTrack* track =
new mkvmuxer::VideoTrack(&seed);
217 return Status(error::INTERNAL_ERROR,
"Failed to create video track.");
219 if (info->codec() == kCodecVP8) {
220 track->set_codec_id(mkvmuxer::Tracks::kVp8CodecId);
221 }
else if (info->codec() == kCodecVP9) {
222 track->set_codec_id(mkvmuxer::Tracks::kVp9CodecId);
224 LOG(ERROR) <<
"Only VP8 and VP9 video codec is supported.";
225 return Status(error::UNIMPLEMENTED,
226 "Only VP8 and VP9 video codecs are supported.");
229 track->set_uid(info->track_id());
230 track->set_type(mkvmuxer::Tracks::kVideo);
231 track->set_width(info->width());
232 track->set_height(info->height());
233 track->set_display_height(info->height());
234 track->set_display_width(info->width() * info->
pixel_width() /
236 track->set_language(info->language().c_str());
238 tracks_.AddTrack(track, info->track_id());
239 track_id_ = track->number();
243 Status Segmenter::CreateAudioTrack(AudioStreamInfo* info) {
245 unsigned int seed = 0;
246 mkvmuxer::AudioTrack* track =
new mkvmuxer::AudioTrack(&seed);
248 return Status(error::INTERNAL_ERROR,
"Failed to create audio track.");
250 if (info->codec() == kCodecOpus) {
251 track->set_codec_id(mkvmuxer::Tracks::kOpusCodecId);
252 }
else if (info->codec() == kCodecVorbis) {
253 track->set_codec_id(mkvmuxer::Tracks::kVorbisCodecId);
255 LOG(ERROR) <<
"Only Vorbis and Opus audio codec is supported.";
256 return Status(error::UNIMPLEMENTED,
257 "Only Vorbis and Opus audio codecs are supported.");
260 track->set_uid(info->track_id());
261 track->set_language(info->language().c_str());
262 track->set_type(mkvmuxer::Tracks::kAudio);
263 track->set_sample_rate(info->sampling_frequency());
264 track->set_channels(info->num_channels());
266 tracks_.AddTrack(track, info->track_id());
267 track_id_ = track->number();