Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1330)

Unified Diff: media/filters/webm_muxer.cc

Issue 1225123006: media/capture: Adding WebmMuxer class and unittests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@m_crbug262211__MSRecorder__2__libwebm_reland_in_third_party
Patch Set: emircan@ nits, rebase (only media.gyp affected), removed bool from WriteCallbackCB signature Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/filters/webm_muxer.cc
diff --git a/media/filters/webm_muxer.cc b/media/filters/webm_muxer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..3e00cdde45776584f80a1f9612083458d90472d9
--- /dev/null
+++ b/media/filters/webm_muxer.cc
@@ -0,0 +1,122 @@
+// Copyright 2015 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.
+
+#include "media/filters/webm_muxer.h"
+
+#include "base/time/time.h"
+
+namespace media {
+
+WebmMuxer::WebmMuxer(const WriteDataCB& write_data_callback)
+ : write_data_callback_(write_data_callback),
+ time_start_(base::TimeTicks::Now()),
+ position_(0),
+ segment_(new mkvmuxer::Segment()) {
+ DVLOG(3) << __FUNCTION__;
+ DCHECK(thread_checker_.CalledOnValidThread());
+ segment_->Init(this);
+ segment_->set_mode(mkvmuxer::Segment::kLive);
+ segment_->OutputCues(false);
+
+ mkvmuxer::SegmentInfo* const info = segment_->GetSegmentInfo();
+ info->set_writing_app("Chrome");
+ info->set_muxing_app("Chrome");
+ // SegmentInfo::set_date_utc() is nanoseconds from 0:00 on January 1st, 2001.
+ base::Time utc_epoch;
+ base::Time::FromUTCString("1 Jan 2001 00:00:00.000", &utc_epoch);
miu 2015/07/16 01:46:25 BTW--The media/PRESUBMIT.py script may cause commi
mcasas 2015/07/17 17:45:07 Done.
+ DCHECK(!utc_epoch.is_null());
+ info->set_date_utc((base::Time::Now() - utc_epoch).InMicroseconds() *
+ base::Time::kNanosecondsPerMicrosecond);
+}
+
+WebmMuxer::~WebmMuxer() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DVLOG(3) << __FUNCTION__;
+ segment_->Finalize();
+}
+
+void WebmMuxer::OnEncodedVideo(int track_index,
+ const std::string& encoded_data,
+ const base::TimeTicks& timestamp,
+ bool keyframe,
+ const gfx::Size& frame_size,
+ double frame_rate) {
+ DVLOG(3) << __FUNCTION__;
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ const int index_starting_from_one = track_index + 1;
+ // Add the video track the first time we see it.
+ if (track_indexes_.find(index_starting_from_one) == track_indexes_.end()) {
+ AddVideoTrack(frame_size, frame_rate, index_starting_from_one);
+ track_indexes_.insert(index_starting_from_one);
+ }
+
+ // Depending on segment->GetSegmentInfo()->timecode_scale(), timestamps are in
+ // milliseconds or microseconds. By default in milliseconds.
+ segment_->AddFrame(reinterpret_cast<const uint8_t*>(encoded_data.data()),
+ encoded_data.size(),
+ index_starting_from_one,
+ (timestamp - time_start_).InMilliseconds(),
miu 2015/07/16 01:46:25 InMicroseconds() please. See comment below.
mcasas 2015/07/17 17:45:07 Done.
+ keyframe);
+}
+
+bool WebmMuxer::AddVideoTrack(const gfx::Size& frame_size,
+ double frame_rate,
+ int track_index_starting_from_one) {
+ DVLOG(3) << "AddVideoTrack: " << frame_size.ToString() << "@"
+ << frame_rate << "fps, index: " << track_index_starting_from_one;
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ segment_->AddVideoTrack(frame_size.width(), frame_size.height(),
+ track_index_starting_from_one);
+ mkvmuxer::VideoTrack* const video_track =
+ reinterpret_cast<mkvmuxer::VideoTrack*>(
+ segment_->GetTrackByNumber(track_index_starting_from_one));
+ DCHECK(video_track);
+ video_track->set_codec_id(mkvmuxer::Tracks::kVp8CodecId);
+ DCHECK_EQ(video_track->crop_right(), 0ull);
+ DCHECK_EQ(video_track->crop_left(), 0ull);
+ DCHECK_EQ(video_track->crop_top(), 0ull);
+ DCHECK_EQ(video_track->crop_bottom(), 0ull);
+
+ video_track->set_frame_rate(frame_rate);
+ // Depending on segment->GetSegmentInfo()->timecode_scale(), timestamps are
+ // interpreted as milliseconds, microseconds, etc.
+ // By default it's is milliseconds, but DCHECK that.
miu 2015/07/16 01:46:25 BTW--One of my pet peeves is truncating media time
mcasas 2015/07/17 17:45:07 Done.
+ DCHECK_EQ(segment_->GetSegmentInfo()->timecode_scale(), 1000000ull);
+ video_track->set_default_duration(base::Time::kMillisecondsPerSecond /
+ frame_rate);
+
+ return true;
+}
+
+mkvmuxer::int32 WebmMuxer::Write(const void* buf, mkvmuxer::uint32 len) {
+ DVLOG(3) << __FUNCTION__ << " length=" << len;
+ DCHECK(thread_checker_.CalledOnValidThread());
+ write_data_callback_.Run(static_cast<const char*>(buf), len);
miu 2015/07/16 01:46:25 Potential bug: If |len| is greater than 2^31-1, th
mcasas 2015/07/17 17:45:07 Made them size_t in WriteDataCB etc.
+ position_ += len;
+ return 0;
+}
+
+mkvmuxer::int64 WebmMuxer::Position() const {
+ return position_.ValueOrDefault(0);
miu 2015/07/16 01:46:25 Shouldn't this be ValueOrDie()? Also, there's an
mcasas 2015/07/17 17:45:07 Done.
+}
+
+mkvmuxer::int32 WebmMuxer::Position(mkvmuxer::int64 position) {
+ // The stream is not Seekable() so indicate we cannot set the position.
+ return -1;
+}
+
+bool WebmMuxer::Seekable() const {
+ return false;
+}
+
+void WebmMuxer::ElementStartNotify(mkvmuxer::uint64 element_id,
+ mkvmuxer::int64 position) {
+ // This method gets pinged before items are sent to |write_data_callback_|.
+ DCHECK_GE(static_cast<uint64_t>(position), position_.ValueOrDefault(0))
+ << "Can't go back in a Live Mkv stream.";
+}
+
+} // namespace media

Powered by Google App Engine
This is Rietveld 408576698