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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "media/filters/webm_muxer.h"
6
7 #include "base/time/time.h"
8
9 namespace media {
10
11 WebmMuxer::WebmMuxer(const WriteDataCB& write_data_callback)
12 : write_data_callback_(write_data_callback),
13 time_start_(base::TimeTicks::Now()),
14 position_(0),
15 segment_(new mkvmuxer::Segment()) {
16 DVLOG(3) << __FUNCTION__;
17 DCHECK(thread_checker_.CalledOnValidThread());
18 segment_->Init(this);
19 segment_->set_mode(mkvmuxer::Segment::kLive);
20 segment_->OutputCues(false);
21
22 mkvmuxer::SegmentInfo* const info = segment_->GetSegmentInfo();
23 info->set_writing_app("Chrome");
24 info->set_muxing_app("Chrome");
25 // SegmentInfo::set_date_utc() is nanoseconds from 0:00 on January 1st, 2001.
26 base::Time utc_epoch;
27 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.
28 DCHECK(!utc_epoch.is_null());
29 info->set_date_utc((base::Time::Now() - utc_epoch).InMicroseconds() *
30 base::Time::kNanosecondsPerMicrosecond);
31 }
32
33 WebmMuxer::~WebmMuxer() {
34 DCHECK(thread_checker_.CalledOnValidThread());
35 DVLOG(3) << __FUNCTION__;
36 segment_->Finalize();
37 }
38
39 void WebmMuxer::OnEncodedVideo(int track_index,
40 const std::string& encoded_data,
41 const base::TimeTicks& timestamp,
42 bool keyframe,
43 const gfx::Size& frame_size,
44 double frame_rate) {
45 DVLOG(3) << __FUNCTION__;
46 DCHECK(thread_checker_.CalledOnValidThread());
47
48 const int index_starting_from_one = track_index + 1;
49 // Add the video track the first time we see it.
50 if (track_indexes_.find(index_starting_from_one) == track_indexes_.end()) {
51 AddVideoTrack(frame_size, frame_rate, index_starting_from_one);
52 track_indexes_.insert(index_starting_from_one);
53 }
54
55 // Depending on segment->GetSegmentInfo()->timecode_scale(), timestamps are in
56 // milliseconds or microseconds. By default in milliseconds.
57 segment_->AddFrame(reinterpret_cast<const uint8_t*>(encoded_data.data()),
58 encoded_data.size(),
59 index_starting_from_one,
60 (timestamp - time_start_).InMilliseconds(),
miu 2015/07/16 01:46:25 InMicroseconds() please. See comment below.
mcasas 2015/07/17 17:45:07 Done.
61 keyframe);
62 }
63
64 bool WebmMuxer::AddVideoTrack(const gfx::Size& frame_size,
65 double frame_rate,
66 int track_index_starting_from_one) {
67 DVLOG(3) << "AddVideoTrack: " << frame_size.ToString() << "@"
68 << frame_rate << "fps, index: " << track_index_starting_from_one;
69 DCHECK(thread_checker_.CalledOnValidThread());
70
71 segment_->AddVideoTrack(frame_size.width(), frame_size.height(),
72 track_index_starting_from_one);
73 mkvmuxer::VideoTrack* const video_track =
74 reinterpret_cast<mkvmuxer::VideoTrack*>(
75 segment_->GetTrackByNumber(track_index_starting_from_one));
76 DCHECK(video_track);
77 video_track->set_codec_id(mkvmuxer::Tracks::kVp8CodecId);
78 DCHECK_EQ(video_track->crop_right(), 0ull);
79 DCHECK_EQ(video_track->crop_left(), 0ull);
80 DCHECK_EQ(video_track->crop_top(), 0ull);
81 DCHECK_EQ(video_track->crop_bottom(), 0ull);
82
83 video_track->set_frame_rate(frame_rate);
84 // Depending on segment->GetSegmentInfo()->timecode_scale(), timestamps are
85 // interpreted as milliseconds, microseconds, etc.
86 // 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.
87 DCHECK_EQ(segment_->GetSegmentInfo()->timecode_scale(), 1000000ull);
88 video_track->set_default_duration(base::Time::kMillisecondsPerSecond /
89 frame_rate);
90
91 return true;
92 }
93
94 mkvmuxer::int32 WebmMuxer::Write(const void* buf, mkvmuxer::uint32 len) {
95 DVLOG(3) << __FUNCTION__ << " length=" << len;
96 DCHECK(thread_checker_.CalledOnValidThread());
97 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.
98 position_ += len;
99 return 0;
100 }
101
102 mkvmuxer::int64 WebmMuxer::Position() const {
103 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.
104 }
105
106 mkvmuxer::int32 WebmMuxer::Position(mkvmuxer::int64 position) {
107 // The stream is not Seekable() so indicate we cannot set the position.
108 return -1;
109 }
110
111 bool WebmMuxer::Seekable() const {
112 return false;
113 }
114
115 void WebmMuxer::ElementStartNotify(mkvmuxer::uint64 element_id,
116 mkvmuxer::int64 position) {
117 // This method gets pinged before items are sent to |write_data_callback_|.
118 DCHECK_GE(static_cast<uint64_t>(position), position_.ValueOrDefault(0))
119 << "Can't go back in a Live Mkv stream.";
120 }
121
122 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698