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

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: 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
« no previous file with comments | « media/filters/webm_muxer.h ('k') | media/filters/webm_muxer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
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 track_index_starting_from_one = track_index + 1;
49
50 // Add the video track the first time we see it.
51 if (std::find(track_indexes_.begin(), track_indexes_.end(),
52 track_index_starting_from_one) == track_indexes_.end()) {
emircan 2015/07/13 18:14:40 Do you need to keep the order of incoming track_in
mcasas 2015/07/15 23:29:12 Done.
53 AddVideoTrack(frame_size, frame_rate, track_index_starting_from_one);
54 track_indexes_.push_back(track_index_starting_from_one);
55 }
56
57 // Depending on segment->GetSegmentInfo()->timecode_scale(), timestamps are in
58 // milliseconds or microseconds. By default in milliseconds.
59 segment_->AddFrame(reinterpret_cast<const uint8_t*>(encoded_data.data()),
60 encoded_data.size(),
61 track_index_starting_from_one,
62 (timestamp - time_start_).InMilliseconds(),
63 keyframe);
64 }
65
66 bool WebmMuxer::AddVideoTrack(const gfx::Size& frame_size,
67 double frame_rate,
68 int track_index_starting_from_one) {
69 DVLOG(3) << "AddVideoTrack: " << frame_size.ToString() << "@"
70 << frame_rate << "fps, index: " << track_index_starting_from_one;
71 DCHECK(thread_checker_.CalledOnValidThread());
72
73 segment_->AddVideoTrack(frame_size.width(), frame_size.height(),
74 track_index_starting_from_one);
75 mkvmuxer::VideoTrack* const video_track =
76 reinterpret_cast<mkvmuxer::VideoTrack*>(
77 segment_->GetTrackByNumber(track_index_starting_from_one));
78 DCHECK(video_track);
79 video_track->set_codec_id(mkvmuxer::Tracks::kVp8CodecId);
80 DCHECK_EQ(video_track->crop_right(), 0ull);
81 DCHECK_EQ(video_track->crop_left(), 0ull);
82 DCHECK_EQ(video_track->crop_top(), 0ull);
83 DCHECK_EQ(video_track->crop_bottom(), 0ull);
84
85 video_track->set_frame_rate(frame_rate);
86 // Depending on segment->GetSegmentInfo()->timecode_scale(), timestamps are
87 // interpreted as milliseconds, microseconds, etc.
88 // By default it's is milliseconds, but DCHECK that.
89 DCHECK_EQ(segment_->GetSegmentInfo()->timecode_scale(), 1000000ull);
90 video_track->set_default_duration(base::Time::kMillisecondsPerSecond /
91 frame_rate);
92
93 return true;
94 }
95
96 mkvmuxer::int32 WebmMuxer::Write(const void* buf, mkvmuxer::uint32 len) {
97 DVLOG(3) << __FUNCTION__ << " length=" << len;
98 DCHECK(thread_checker_.CalledOnValidThread());
99 // TODO(mcasas): Last parameter is |last_in_slice|, how to calculate it?
100 write_data_callback_.Run(static_cast<const char*>(buf), len, false);
101 position_ += len;
emircan 2015/07/13 18:14:40 base::CheckedNumeric would be useful here in case
mcasas 2015/07/15 23:29:12 Done.
102 return 0;
103 }
104
105 mkvmuxer::int64 WebmMuxer::Position() const {
106 return position_;
107 }
108
109 mkvmuxer::int32 WebmMuxer::Position(mkvmuxer::int64 position) {
110 // The stream is not Seekable() so indicate we cannot set the position.
111 return -1;
112 }
113
114 bool WebmMuxer::Seekable() const {
115 return false;
116 }
117
118 void WebmMuxer::ElementStartNotify(mkvmuxer::uint64 element_id,
119 mkvmuxer::int64 position) {
120 // This method gets pinged before items are sent to |write_data_callback_|.
121 DCHECK_GE(position, position_) << "Can't go back in a Live Mkv stream.";
122 }
123
124 } // namespace media
OLDNEW
« no previous file with comments | « media/filters/webm_muxer.h ('k') | media/filters/webm_muxer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698