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;
57 const std::string version_string =
58 "https://github.com/google/edash-packager version " +
62 segment_info_.set_timecode_scale(kTimecodeScale);
63 segment_info_.set_writing_app(version_string.c_str());
67 segment_info_.set_duration(1);
72 switch (info_->stream_type()) {
74 status = CreateVideoTrack(static_cast<VideoStreamInfo*>(info_));
77 status = CreateAudioTrack(static_cast<AudioStreamInfo*>(info_));
80 NOTIMPLEMENTED() <<
"Not implemented for stream type: "
81 << info_->stream_type();
82 status =
Status(error::UNIMPLEMENTED,
"Not implemented for stream type");
87 return DoInitialize(writer.Pass());
91 segment_info_.set_duration(FromBMFFTimescale(total_duration_));
96 if (sample_duration_ == 0) {
97 sample_duration_ = sample->duration();
107 status = NewSegment(sample->pts());
110 status = NewSegment(sample->pts());
111 segment_length_sec_ = 0;
112 cluster_length_sec_ = 0;
116 status = NewSubsegment(sample->pts());
117 cluster_length_sec_ = 0;
123 const int64_t time_ns =
124 sample->pts() * kSecondsToNs / info_->time_scale();
125 if (!cluster_->AddFrame(sample->data(), sample->data_size(), track_id_,
126 time_ns, sample->is_key_frame())) {
127 LOG(ERROR) <<
"Error adding sample to segment.";
128 return Status(error::FILE_FAILURE,
"Error adding sample to segment.");
130 const double duration_sec =
131 static_cast<double>(sample->duration()) / info_->time_scale();
132 cluster_length_sec_ += duration_sec;
133 segment_length_sec_ += duration_sec;
134 total_duration_ += sample->duration();
140 return static_cast<float>(segment_info_.duration()) *
141 segment_info_.timecode_scale() / kSecondsToNs;
144 uint64_t Segmenter::FromBMFFTimescale(uint64_t time_timescale) {
146 const int64_t time_ns =
147 kSecondsToNs * time_timescale / info_->time_scale();
148 return time_ns / segment_info_.timecode_scale();
151 uint64_t Segmenter::FromWebMTimecode(uint64_t time_webm_timecode) {
153 const int64_t time_ns = time_webm_timecode * segment_info_.timecode_scale();
154 return time_ns * info_->time_scale() / kSecondsToNs;
158 Status error_status(error::FILE_FAILURE,
"Error writing segment header.");
160 if (!WriteEbmlHeader(writer))
163 if (WriteID(writer, mkvmuxer::kMkvSegment) != 0)
166 const uint64_t segment_size_size = 8;
167 segment_payload_pos_ = writer->
Position() + segment_size_size;
170 if (WriteUIntSize(writer, file_size - segment_payload_pos_,
171 segment_size_size) != 0)
173 if (!seek_head_.Write(writer))
176 if (SerializeInt(writer, mkvmuxer::kEbmlUnknownValue, segment_size_size) !=
180 if (!seek_head_.WriteVoid(writer))
184 if (!segment_info_.Write(writer))
187 if (!tracks_.Write(writer))
193 Status Segmenter::SetCluster(uint64_t start_webm_timecode,
196 const uint64_t scale = segment_info_.timecode_scale();
197 cluster_.reset(
new mkvmuxer::Cluster(start_webm_timecode, position, scale));
198 cluster_->Init(writer);
203 accumulated_progress_ += progress;
204 if (!progress_listener_ || progress_target_ == 0)
209 if (accumulated_progress_ >= progress_target_) {
212 progress_listener_->
OnProgress(static_cast<double>(accumulated_progress_) /
219 unsigned int seed = 0;
220 mkvmuxer::VideoTrack* track =
new mkvmuxer::VideoTrack(&seed);
222 return Status(error::INTERNAL_ERROR,
"Failed to create video track.");
224 if (info->codec() == kCodecVP8) {
225 track->set_codec_id(mkvmuxer::Tracks::kVp8CodecId);
226 }
else if (info->codec() == kCodecVP9) {
227 track->set_codec_id(mkvmuxer::Tracks::kVp9CodecId);
229 LOG(ERROR) <<
"Only VP8 and VP9 video codec is supported.";
230 return Status(error::UNIMPLEMENTED,
231 "Only VP8 and VP9 video codecs are supported.");
234 track->set_uid(info->track_id());
235 track->set_type(mkvmuxer::Tracks::kVideo);
236 track->set_width(info->width());
237 track->set_height(info->height());
238 track->set_display_height(info->height());
239 track->set_display_width(info->width() * info->
pixel_width() /
241 track->set_language(info->language().c_str());
243 tracks_.AddTrack(track, info->track_id());
244 track_id_ = track->number();
248 Status Segmenter::CreateAudioTrack(AudioStreamInfo* info) {
250 unsigned int seed = 0;
251 mkvmuxer::AudioTrack* track =
new mkvmuxer::AudioTrack(&seed);
253 return Status(error::INTERNAL_ERROR,
"Failed to create audio track.");
255 if (info->codec() == kCodecOpus) {
256 track->set_codec_id(mkvmuxer::Tracks::kOpusCodecId);
257 }
else if (info->codec() == kCodecVorbis) {
258 track->set_codec_id(mkvmuxer::Tracks::kVorbisCodecId);
260 LOG(ERROR) <<
"Only Vorbis and Opus audio codec is supported.";
261 return Status(error::UNIMPLEMENTED,
262 "Only Vorbis and Opus audio codecs are supported.");
265 track->set_uid(info->track_id());
266 track->set_language(info->language().c_str());
267 track->set_type(mkvmuxer::Tracks::kAudio);
268 track->set_sample_rate(info->sampling_frequency());
269 track->set_channels(info->num_channels());
271 tracks_.AddTrack(track, info->track_id());
272 track_id_ = track->number();