Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/capture/webm_muxer.h" | |
| 6 | |
| 7 #include <limits> | |
| 8 | |
| 9 namespace media { | |
| 10 | |
| 11 // Template to +1 a non-negative Natural number to make a strictly positive one. | |
| 12 template <typename NonNegativeNaturalType> | |
| 13 class PositiveInteger{ | |
| 14 public: | |
| 15 explicit PositiveInteger(NonNegativeNaturalType value) | |
| 16 : value_(value + static_cast<NonNegativeNaturalType>(1)) { | |
| 17 static_assert(std::numeric_limits<NonNegativeNaturalType>::is_integer && | |
| 18 !std::numeric_limits<NonNegativeNaturalType>::is_signed, | |
| 19 "NonNegativeNaturalType must be an unsigned integer type"); | |
| 20 } | |
| 21 NonNegativeNaturalType Value() const { return value_; } | |
| 22 | |
| 23 private: | |
| 24 const NonNegativeNaturalType value_; | |
| 25 | |
| 26 DISALLOW_IMPLICIT_CONSTRUCTORS(PositiveInteger); | |
| 27 }; | |
| 28 | |
| 29 WebmMuxer::WebmMuxer(const WriteDataCB& write_data_callback) | |
| 30 : write_data_callback_(write_data_callback), | |
| 31 position_(0), | |
| 32 segment_(new mkvmuxer::Segment()) { | |
| 33 DVLOG(2) << __FUNCTION__; | |
|
DaleCurtis
2015/07/20 18:45:17
Remove DVLOG() of this type? Even at (2) this isn
mcasas
2015/07/20 20:15:20
Done.
| |
| 34 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 35 segment_->Init(this); | |
| 36 segment_->set_mode(mkvmuxer::Segment::kLive); | |
| 37 segment_->OutputCues(false); | |
| 38 | |
| 39 mkvmuxer::SegmentInfo* const info = segment_->GetSegmentInfo(); | |
| 40 info->set_writing_app("Chrome"); | |
| 41 info->set_muxing_app("Chrome"); | |
| 42 } | |
| 43 | |
| 44 WebmMuxer::~WebmMuxer() { | |
| 45 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 46 DVLOG(2) << __FUNCTION__; | |
| 47 segment_->Finalize(); | |
| 48 } | |
| 49 | |
| 50 void WebmMuxer::AddVideoTrack(uint32_t track_number, | |
|
DaleCurtis
2015/07/20 18:45:17
Why do you need to expose the track number? Just k
mcasas
2015/07/20 20:15:20
Even better: if the caller doesn't decide on the t
| |
| 51 const gfx::Size& frame_size, | |
| 52 double frame_rate) { | |
| 53 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 54 | |
| 55 // |track_number|, a caller-side identifier, cannot be zero (!), see | |
| 56 // http://www.matroska.org/technical/specs/index.html#Tracks | |
| 57 const PositiveInteger<uint32_t> nonzero_track_number(track_number); | |
| 58 DCHECK(!segment_->GetTrackByNumber(nonzero_track_number.Value())); | |
| 59 | |
| 60 segment_->AddVideoTrack(frame_size.width(), | |
| 61 frame_size.height(), | |
| 62 nonzero_track_number.Value()); | |
| 63 mkvmuxer::VideoTrack* const video_track = | |
| 64 reinterpret_cast<mkvmuxer::VideoTrack*>( | |
| 65 segment_->GetTrackByNumber(nonzero_track_number.Value())); | |
| 66 DCHECK(video_track); | |
| 67 video_track->set_codec_id(mkvmuxer::Tracks::kVp8CodecId); | |
| 68 DCHECK_EQ(video_track->crop_right(), 0ull); | |
| 69 DCHECK_EQ(video_track->crop_left(), 0ull); | |
| 70 DCHECK_EQ(video_track->crop_top(), 0ull); | |
| 71 DCHECK_EQ(video_track->crop_bottom(), 0ull); | |
| 72 | |
| 73 video_track->set_frame_rate(frame_rate); | |
| 74 video_track->set_default_duration(base::Time::kMicrosecondsPerSecond / | |
| 75 frame_rate); | |
| 76 // Change segment's timestamps to microseconds instead of milliseconds: | |
| 77 // http://matroska.org/technical/specs/index.html#TimecodeScale | |
| 78 segment_->GetSegmentInfo()->set_timecode_scale( | |
| 79 base::Time::kNanosecondsPerMicrosecond); | |
|
Tom Finegan
2015/07/20 18:58:23
http://www.webmproject.org/docs/container/
Since
mcasas
2015/07/20 20:15:20
Done.
| |
| 80 } | |
| 81 | |
| 82 void WebmMuxer::OnEncodedVideo(uint32_t track_number, | |
| 83 const std::string& encoded_data, | |
| 84 const base::TimeDelta& timestamp, | |
| 85 bool keyframe) { | |
| 86 DVLOG(2) << __FUNCTION__; | |
| 87 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 88 | |
| 89 // |track_number|, a caller-side identifier, cannot be zero (!), see | |
| 90 // http://www.matroska.org/technical/specs/index.html#Tracks | |
| 91 const PositiveInteger<uint32_t> nonzero_track_number(track_number); | |
| 92 DCHECK(segment_->GetTrackByNumber(nonzero_track_number.Value())); | |
| 93 | |
| 94 segment_->AddFrame(reinterpret_cast<const uint8_t*>(encoded_data.data()), | |
| 95 encoded_data.size(), | |
| 96 nonzero_track_number.Value(), | |
| 97 timestamp.InMicroseconds(), | |
|
Tom Finegan
2015/07/20 18:58:23
You'll need to scale this for the new timecode sca
mcasas
2015/07/20 20:15:20
Done.
| |
| 98 keyframe); | |
| 99 } | |
| 100 | |
| 101 mkvmuxer::int32 WebmMuxer::Write(const void* buf, mkvmuxer::uint32 len) { | |
| 102 DVLOG(3) << __FUNCTION__ << " length=" << len; | |
| 103 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 104 write_data_callback_.Run(static_cast<const char*>(buf), len); | |
| 105 position_ += len; | |
| 106 return 0; | |
| 107 } | |
| 108 | |
| 109 mkvmuxer::int64 WebmMuxer::Position() const { | |
| 110 return position_.ValueOrDie(); | |
| 111 } | |
| 112 | |
| 113 mkvmuxer::int32 WebmMuxer::Position(mkvmuxer::int64 position) { | |
| 114 // The stream is not Seekable() so indicate we cannot set the position. | |
| 115 return -1; | |
| 116 } | |
| 117 | |
| 118 bool WebmMuxer::Seekable() const { | |
| 119 return false; | |
| 120 } | |
| 121 | |
| 122 void WebmMuxer::ElementStartNotify(mkvmuxer::uint64 element_id, | |
| 123 mkvmuxer::int64 position) { | |
| 124 // This method gets pinged before items are sent to |write_data_callback_|. | |
| 125 DCHECK_GE(position, position_.ValueOrDefault(0)) | |
| 126 << "Can't go back in a Live Mkv stream."; | |
| 127 } | |
| 128 | |
| 129 } // namespace media | |
| OLD | NEW |