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 bool addframe_result;
126 if (sample->side_data_size() > 0) {
127 uint64_t block_add_id;
130 CHECK_GT(sample->side_data_size(),
sizeof(block_add_id));
131 memcpy(&block_add_id, sample->side_data(),
sizeof(block_add_id));
132 addframe_result = cluster_->AddFrameWithAdditional(
133 sample->data(), sample->data_size(),
134 sample->side_data() +
sizeof(block_add_id),
135 sample->side_data_size() -
sizeof(block_add_id), block_add_id,
136 track_id_, time_ns, sample->is_key_frame());
139 cluster_->AddFrame(sample->data(), sample->data_size(), track_id_,
140 time_ns, sample->is_key_frame());
142 if (!addframe_result) {
143 LOG(ERROR) <<
"Error adding sample to segment.";
144 return Status(error::FILE_FAILURE,
"Error adding sample to segment.");
147 const double duration_sec =
148 static_cast<double>(sample->duration()) / info_->time_scale();
149 cluster_length_sec_ += duration_sec;
150 segment_length_sec_ += duration_sec;
151 total_duration_ += sample->duration();
157 return static_cast<float>(segment_info_.duration()) *
158 segment_info_.timecode_scale() / kSecondsToNs;
161 uint64_t Segmenter::FromBMFFTimescale(uint64_t time_timescale) {
163 const int64_t time_ns =
164 kSecondsToNs * time_timescale / info_->time_scale();
165 return time_ns / segment_info_.timecode_scale();
168 uint64_t Segmenter::FromWebMTimecode(uint64_t time_webm_timecode) {
170 const int64_t time_ns = time_webm_timecode * segment_info_.timecode_scale();
171 return time_ns * info_->time_scale() / kSecondsToNs;
175 Status error_status(error::FILE_FAILURE,
"Error writing segment header.");
177 if (!WriteEbmlHeader(writer))
180 if (WriteID(writer, mkvmuxer::kMkvSegment) != 0)
183 const uint64_t segment_size_size = 8;
184 segment_payload_pos_ = writer->
Position() + segment_size_size;
187 if (WriteUIntSize(writer, file_size - segment_payload_pos_,
188 segment_size_size) != 0)
190 if (!seek_head_.Write(writer))
193 if (SerializeInt(writer, mkvmuxer::kEbmlUnknownValue, segment_size_size) !=
197 if (!seek_head_.WriteVoid(writer))
201 if (!segment_info_.Write(writer))
204 if (!tracks_.Write(writer))
210 Status Segmenter::SetCluster(uint64_t start_webm_timecode,
213 const uint64_t scale = segment_info_.timecode_scale();
214 cluster_.reset(
new mkvmuxer::Cluster(start_webm_timecode, position, scale));
215 cluster_->Init(writer);
220 accumulated_progress_ += progress;
221 if (!progress_listener_ || progress_target_ == 0)
226 if (accumulated_progress_ >= progress_target_) {
229 progress_listener_->
OnProgress(static_cast<double>(accumulated_progress_) /
236 unsigned int seed = 0;
237 mkvmuxer::VideoTrack* track =
new mkvmuxer::VideoTrack(&seed);
239 return Status(error::INTERNAL_ERROR,
"Failed to create video track.");
241 if (info->codec() == kCodecVP8) {
242 track->set_codec_id(mkvmuxer::Tracks::kVp8CodecId);
243 }
else if (info->codec() == kCodecVP9) {
244 track->set_codec_id(mkvmuxer::Tracks::kVp9CodecId);
246 LOG(ERROR) <<
"Only VP8 and VP9 video codec is supported.";
247 return Status(error::UNIMPLEMENTED,
248 "Only VP8 and VP9 video codecs are supported.");
251 track->set_uid(info->track_id());
252 track->set_type(mkvmuxer::Tracks::kVideo);
253 track->set_width(info->width());
254 track->set_height(info->height());
255 track->set_display_height(info->height());
256 track->set_display_width(info->width() * info->
pixel_width() /
258 track->set_language(info->language().c_str());
260 tracks_.AddTrack(track, info->track_id());
261 track_id_ = track->number();
265 Status Segmenter::CreateAudioTrack(AudioStreamInfo* info) {
267 unsigned int seed = 0;
268 mkvmuxer::AudioTrack* track =
new mkvmuxer::AudioTrack(&seed);
270 return Status(error::INTERNAL_ERROR,
"Failed to create audio track.");
272 if (info->codec() == kCodecOpus) {
273 track->set_codec_id(mkvmuxer::Tracks::kOpusCodecId);
274 }
else if (info->codec() == kCodecVorbis) {
275 track->set_codec_id(mkvmuxer::Tracks::kVorbisCodecId);
277 LOG(ERROR) <<
"Only Vorbis and Opus audio codec is supported.";
278 return Status(error::UNIMPLEMENTED,
279 "Only Vorbis and Opus audio codecs are supported.");
282 track->set_uid(info->track_id());
283 track->set_language(info->language().c_str());
284 track->set_type(mkvmuxer::Tracks::kAudio);
285 track->set_sample_rate(info->sampling_frequency());
286 track->set_channels(info->num_channels());
288 tracks_.AddTrack(track, info->track_id());
289 track_id_ = track->number();