| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/capture/webm_muxer.h" | 5 #include "media/capture/webm_muxer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/strings/stringprintf.h" |
| 8 #include "media/base/limits.h" | 9 #include "media/base/limits.h" |
| 9 #include "media/base/video_frame.h" | 10 #include "media/base/video_frame.h" |
| 10 | 11 |
| 11 namespace media { | 12 namespace media { |
| 12 | 13 |
| 13 static double GetFrameRate(const scoped_refptr<VideoFrame>& video_frame) { | 14 static double GetFrameRate(const scoped_refptr<VideoFrame>& video_frame) { |
| 14 const double kZeroFrameRate = 0.0; | 15 const double kZeroFrameRate = 0.0; |
| 15 const double kDefaultFrameRate = 30.0; | 16 const double kDefaultFrameRate = 30.0; |
| 16 | 17 |
| 17 double frame_rate = kDefaultFrameRate; | 18 double frame_rate = kDefaultFrameRate; |
| 18 if (!video_frame->metadata()->GetDouble( | 19 if (!video_frame->metadata()->GetDouble( |
| 19 VideoFrameMetadata::FRAME_RATE, &frame_rate) || | 20 VideoFrameMetadata::FRAME_RATE, &frame_rate) || |
| 20 frame_rate <= kZeroFrameRate || | 21 frame_rate <= kZeroFrameRate || |
| 21 frame_rate > media::limits::kMaxFramesPerSecond) { | 22 frame_rate > media::limits::kMaxFramesPerSecond) { |
| 22 frame_rate = kDefaultFrameRate; | 23 frame_rate = kDefaultFrameRate; |
| 23 } | 24 } |
| 24 return frame_rate; | 25 return frame_rate; |
| 25 } | 26 } |
| 26 | 27 |
| 27 WebmMuxer::WebmMuxer(const WriteDataCB& write_data_callback) | 28 WebmMuxer::WebmMuxer( |
| 28 : track_index_(0), | 29 const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
| 30 const WriteDataCB& write_data_callback) |
| 31 : main_task_runner_(main_task_runner), |
| 32 track_index_(0), |
| 29 write_data_callback_(write_data_callback), | 33 write_data_callback_(write_data_callback), |
| 30 position_(0) { | 34 position_(0), |
| 35 segment_(new mkvmuxer::Segment()) { |
| 36 DCHECK(main_task_runner_); |
| 31 DCHECK(!write_data_callback_.is_null()); | 37 DCHECK(!write_data_callback_.is_null()); |
| 32 // Creation is done on a different thread than main activities. | 38 // Creation is done on a different thread than main activities. |
| 33 thread_checker_.DetachFromThread(); | 39 thread_checker_.DetachFromThread(); |
| 34 } | 40 } |
| 35 | 41 |
| 36 WebmMuxer::~WebmMuxer() { | |
| 37 // No need to segment_.Finalize() since is not Seekable(), i.e. a live stream, | |
| 38 // but is good practice. | |
| 39 segment_.Finalize(); | |
| 40 } | |
| 41 | |
| 42 void WebmMuxer::OnEncodedVideo(const scoped_refptr<VideoFrame>& video_frame, | 42 void WebmMuxer::OnEncodedVideo(const scoped_refptr<VideoFrame>& video_frame, |
| 43 const base::StringPiece& encoded_data, | 43 const base::StringPiece& encoded_data, |
| 44 base::TimeTicks timestamp, | 44 base::TimeTicks timestamp, |
| 45 bool is_key_frame) { | 45 bool is_key_frame) { |
| 46 DVLOG(1) << __FUNCTION__ << " - " << encoded_data.size() << "B"; | 46 DVLOG(1) << __FUNCTION__ << " - " << encoded_data.size() << "B"; |
| 47 DCHECK(thread_checker_.CalledOnValidThread()); | 47 DCHECK(thread_checker_.CalledOnValidThread()); |
| 48 if (!track_index_) { | 48 if (!track_index_) { |
| 49 // |track_index_|, cannot be zero (!), initialize WebmMuxer in that case. | 49 // |track_index_|, cannot be zero (!), initialize WebmMuxer in that case. |
| 50 // http://www.matroska.org/technical/specs/index.html#Tracks | 50 // http://www.matroska.org/technical/specs/index.html#Tracks |
| 51 AddVideoTrack(video_frame->visible_rect().size(), | 51 AddVideoTrack(video_frame->visible_rect().size(), |
| 52 GetFrameRate(video_frame)); | 52 GetFrameRate(video_frame)); |
| 53 first_frame_timestamp_ = timestamp; | 53 first_frame_timestamp_ = timestamp; |
| 54 } | 54 } |
| 55 segment_.AddFrame(reinterpret_cast<const uint8_t*>(encoded_data.data()), | 55 segment_->AddFrame(reinterpret_cast<const uint8_t*>(encoded_data.data()), |
| 56 encoded_data.size(), | 56 encoded_data.size(), |
| 57 track_index_, | 57 track_index_, |
| 58 (timestamp - first_frame_timestamp_).InMicroseconds() * | 58 (timestamp - first_frame_timestamp_).InMicroseconds() * |
| 59 base::Time::kNanosecondsPerMicrosecond, | 59 base::Time::kNanosecondsPerMicrosecond, |
| 60 is_key_frame); | 60 is_key_frame); |
| 61 } |
| 62 |
| 63 //static |
| 64 void WebmMuxer::FinalizeSegment(scoped_ptr<mkvmuxer::Segment> segment) { |
| 65 // No need to segment_->Finalize() since is not Seekable(), i.e. a live |
| 66 // stream, but is a good practice. |
| 67 segment->Finalize(); |
| 68 } |
| 69 |
| 70 WebmMuxer::~WebmMuxer() { |
| 71 // Guarantee destruction of |segment_| in the appropriate thread. |
| 72 main_task_runner_->PostTask(FROM_HERE, |
| 73 base::Bind(&WebmMuxer::FinalizeSegment, base::Passed(&segment_))); |
| 61 } | 74 } |
| 62 | 75 |
| 63 void WebmMuxer::AddVideoTrack(const gfx::Size& frame_size, double frame_rate) { | 76 void WebmMuxer::AddVideoTrack(const gfx::Size& frame_size, double frame_rate) { |
| 64 DCHECK(thread_checker_.CalledOnValidThread()); | 77 DCHECK(thread_checker_.CalledOnValidThread()); |
| 65 DCHECK_EQ(track_index_, 0u) << "WebmMuxer can only be initialised once."; | 78 DCHECK_EQ(track_index_, 0u) << "WebmMuxer can only be initialised once."; |
| 66 | 79 |
| 67 segment_.Init(this); | 80 segment_->Init(this); |
| 68 segment_.set_mode(mkvmuxer::Segment::kLive); | 81 segment_->set_mode(mkvmuxer::Segment::kLive); |
| 69 segment_.OutputCues(false); | 82 segment_->OutputCues(false); |
| 70 | 83 |
| 71 mkvmuxer::SegmentInfo* const info = segment_.GetSegmentInfo(); | 84 mkvmuxer::SegmentInfo* const info = segment_->GetSegmentInfo(); |
| 72 info->set_writing_app("Chrome"); | 85 info->set_writing_app("Chrome"); |
| 73 info->set_muxing_app("Chrome"); | 86 info->set_muxing_app("Chrome"); |
| 74 | 87 |
| 75 track_index_ = | 88 track_index_ = |
| 76 segment_.AddVideoTrack(frame_size.width(), frame_size.height(), 0); | 89 segment_->AddVideoTrack(frame_size.width(), frame_size.height(), 0); |
| 77 DCHECK_GT(track_index_, 0u); | 90 DCHECK_GT(track_index_, 0u); |
| 78 | 91 |
| 79 mkvmuxer::VideoTrack* const video_track = | 92 mkvmuxer::VideoTrack* const video_track = |
| 80 reinterpret_cast<mkvmuxer::VideoTrack*>( | 93 reinterpret_cast<mkvmuxer::VideoTrack*>( |
| 81 segment_.GetTrackByNumber(track_index_)); | 94 segment_->GetTrackByNumber(track_index_)); |
| 82 DCHECK(video_track); | 95 DCHECK(video_track); |
| 83 video_track->set_codec_id(mkvmuxer::Tracks::kVp8CodecId); | 96 video_track->set_codec_id(mkvmuxer::Tracks::kVp8CodecId); |
| 84 DCHECK_EQ(video_track->crop_right(), 0ull); | 97 DCHECK_EQ(video_track->crop_right(), 0ull); |
| 85 DCHECK_EQ(video_track->crop_left(), 0ull); | 98 DCHECK_EQ(video_track->crop_left(), 0ull); |
| 86 DCHECK_EQ(video_track->crop_top(), 0ull); | 99 DCHECK_EQ(video_track->crop_top(), 0ull); |
| 87 DCHECK_EQ(video_track->crop_bottom(), 0ull); | 100 DCHECK_EQ(video_track->crop_bottom(), 0ull); |
| 88 | 101 |
| 89 video_track->set_frame_rate(frame_rate); | 102 video_track->set_frame_rate(frame_rate); |
| 90 video_track->set_default_duration(base::Time::kNanosecondsPerSecond / | 103 video_track->set_default_duration(base::Time::kNanosecondsPerSecond / |
| 91 frame_rate); | 104 frame_rate); |
| 92 // Segment's timestamps should be in milliseconds, DCHECK it. See | 105 // Segment's timestamps should be in milliseconds, DCHECK it. See |
| 93 // http://www.webmproject.org/docs/container/#muxer-guidelines | 106 // http://www.webmproject.org/docs/container/#muxer-guidelines |
| 94 DCHECK_EQ(segment_.GetSegmentInfo()->timecode_scale(), 1000000ull); | 107 DCHECK_EQ(segment_->GetSegmentInfo()->timecode_scale(), 1000000ull); |
| 95 } | 108 } |
| 96 | 109 |
| 97 mkvmuxer::int32 WebmMuxer::Write(const void* buf, mkvmuxer::uint32 len) { | 110 mkvmuxer::int32 WebmMuxer::Write(const void* buf, mkvmuxer::uint32 len) { |
| 98 DCHECK(thread_checker_.CalledOnValidThread()); | 111 DCHECK(thread_checker_.CalledOnValidThread()); |
| 99 DCHECK(buf); | 112 DCHECK(buf); |
| 100 write_data_callback_.Run(base::StringPiece(reinterpret_cast<const char*>(buf), | 113 write_data_callback_.Run(scoped_ptr<std::string>( |
| 101 len)); | 114 new std::string(reinterpret_cast<const char*>(buf), len))); |
| 102 position_ += len; | 115 position_ += len; |
| 103 return 0; | 116 return 0; |
| 104 } | 117 } |
| 105 | 118 |
| 106 mkvmuxer::int64 WebmMuxer::Position() const { | 119 mkvmuxer::int64 WebmMuxer::Position() const { |
| 107 return position_.ValueOrDie(); | 120 return position_.ValueOrDie(); |
| 108 } | 121 } |
| 109 | 122 |
| 110 mkvmuxer::int32 WebmMuxer::Position(mkvmuxer::int64 position) { | 123 mkvmuxer::int32 WebmMuxer::Position(mkvmuxer::int64 position) { |
| 111 // The stream is not Seekable() so indicate we cannot set the position. | 124 // The stream is not Seekable() so indicate we cannot set the position. |
| 112 return -1; | 125 return -1; |
| 113 } | 126 } |
| 114 | 127 |
| 115 bool WebmMuxer::Seekable() const { | 128 bool WebmMuxer::Seekable() const { |
| 116 return false; | 129 return false; |
| 117 } | 130 } |
| 118 | 131 |
| 119 void WebmMuxer::ElementStartNotify(mkvmuxer::uint64 element_id, | 132 void WebmMuxer::ElementStartNotify(mkvmuxer::uint64 element_id, |
| 120 mkvmuxer::int64 position) { | 133 mkvmuxer::int64 position) { |
| 121 // This method gets pinged before items are sent to |write_data_callback_|. | 134 // This method gets pinged before items are sent to |write_data_callback_|. |
| 122 DCHECK_GE(position, position_.ValueOrDefault(0)) | 135 DCHECK_GE(position, position_.ValueOrDefault(0)) |
| 123 << "Can't go back in a live WebM stream."; | 136 << "Can't go back in a live WebM stream."; |
| 124 } | 137 } |
| 125 | 138 |
| 126 } // namespace media | 139 } // namespace media |
| OLD | NEW |