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 "media/base/limits.h" | 8 #include "media/base/limits.h" |
9 #include "media/base/video_frame.h" | 9 #include "media/base/video_frame.h" |
| 10 #include "ui/gfx/geometry/size.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(const WriteDataCB& write_data_callback) |
28 : track_index_(0), | 29 : track_index_(0), |
29 write_data_callback_(write_data_callback), | 30 write_data_callback_(write_data_callback), |
30 position_(0) { | 31 position_(0) { |
31 DCHECK(!write_data_callback_.is_null()); | 32 DCHECK(!write_data_callback_.is_null()); |
32 // Creation is done on a different thread than main activities. | 33 // Creation is done on a different thread than main activities. |
33 thread_checker_.DetachFromThread(); | 34 thread_checker_.DetachFromThread(); |
34 } | 35 } |
35 | 36 |
36 WebmMuxer::~WebmMuxer() { | 37 WebmMuxer::~WebmMuxer() { |
37 // No need to segment_.Finalize() since is not Seekable(), i.e. a live stream, | 38 // No need to segment_.Finalize() since is not Seekable(), i.e. a live |
38 // but is good practice. | 39 // stream, but is a good practice. |
| 40 DCHECK(thread_checker_.CalledOnValidThread()); |
39 segment_.Finalize(); | 41 segment_.Finalize(); |
40 } | 42 } |
41 | 43 |
42 void WebmMuxer::OnEncodedVideo(const scoped_refptr<VideoFrame>& video_frame, | 44 void WebmMuxer::OnEncodedVideo(const scoped_refptr<VideoFrame>& video_frame, |
43 const base::StringPiece& encoded_data, | 45 scoped_ptr<std::string> encoded_data, |
44 base::TimeTicks timestamp, | 46 base::TimeTicks timestamp, |
45 bool is_key_frame) { | 47 bool is_key_frame) { |
46 DVLOG(1) << __FUNCTION__ << " - " << encoded_data.size() << "B"; | 48 DVLOG(1) << __FUNCTION__ << " - " << encoded_data->size() << "B"; |
47 DCHECK(thread_checker_.CalledOnValidThread()); | 49 DCHECK(thread_checker_.CalledOnValidThread()); |
48 if (!track_index_) { | 50 if (!track_index_) { |
49 // |track_index_|, cannot be zero (!), initialize WebmMuxer in that case. | 51 // |track_index_|, cannot be zero (!), initialize WebmMuxer in that case. |
50 // http://www.matroska.org/technical/specs/index.html#Tracks | 52 // http://www.matroska.org/technical/specs/index.html#Tracks |
51 AddVideoTrack(video_frame->visible_rect().size(), | 53 AddVideoTrack(video_frame->visible_rect().size(), |
52 GetFrameRate(video_frame)); | 54 GetFrameRate(video_frame)); |
53 first_frame_timestamp_ = timestamp; | 55 first_frame_timestamp_ = timestamp; |
54 } | 56 } |
55 segment_.AddFrame(reinterpret_cast<const uint8_t*>(encoded_data.data()), | 57 segment_.AddFrame(reinterpret_cast<const uint8_t*>(encoded_data->data()), |
56 encoded_data.size(), | 58 encoded_data->size(), |
57 track_index_, | 59 track_index_, |
58 (timestamp - first_frame_timestamp_).InMicroseconds() * | 60 (timestamp - first_frame_timestamp_).InMicroseconds() * |
59 base::Time::kNanosecondsPerMicrosecond, | 61 base::Time::kNanosecondsPerMicrosecond, |
60 is_key_frame); | 62 is_key_frame); |
61 } | 63 } |
62 | 64 |
63 void WebmMuxer::AddVideoTrack(const gfx::Size& frame_size, double frame_rate) { | 65 void WebmMuxer::AddVideoTrack(const gfx::Size& frame_size, double frame_rate) { |
64 DCHECK(thread_checker_.CalledOnValidThread()); | 66 DCHECK(thread_checker_.CalledOnValidThread()); |
65 DCHECK_EQ(track_index_, 0u) << "WebmMuxer can only be initialised once."; | 67 DCHECK_EQ(track_index_, 0u) << "WebmMuxer can only be initialised once."; |
66 | 68 |
(...skipping 24 matching lines...) Expand all Loading... |
91 frame_rate); | 93 frame_rate); |
92 // Segment's timestamps should be in milliseconds, DCHECK it. See | 94 // Segment's timestamps should be in milliseconds, DCHECK it. See |
93 // http://www.webmproject.org/docs/container/#muxer-guidelines | 95 // http://www.webmproject.org/docs/container/#muxer-guidelines |
94 DCHECK_EQ(segment_.GetSegmentInfo()->timecode_scale(), 1000000ull); | 96 DCHECK_EQ(segment_.GetSegmentInfo()->timecode_scale(), 1000000ull); |
95 } | 97 } |
96 | 98 |
97 mkvmuxer::int32 WebmMuxer::Write(const void* buf, mkvmuxer::uint32 len) { | 99 mkvmuxer::int32 WebmMuxer::Write(const void* buf, mkvmuxer::uint32 len) { |
98 DCHECK(thread_checker_.CalledOnValidThread()); | 100 DCHECK(thread_checker_.CalledOnValidThread()); |
99 DCHECK(buf); | 101 DCHECK(buf); |
100 write_data_callback_.Run(base::StringPiece(reinterpret_cast<const char*>(buf), | 102 write_data_callback_.Run(base::StringPiece(reinterpret_cast<const char*>(buf), |
101 len)); | 103 len)); |
102 position_ += len; | 104 position_ += len; |
103 return 0; | 105 return 0; |
104 } | 106 } |
105 | 107 |
106 mkvmuxer::int64 WebmMuxer::Position() const { | 108 mkvmuxer::int64 WebmMuxer::Position() const { |
107 return position_.ValueOrDie(); | 109 return position_.ValueOrDie(); |
108 } | 110 } |
109 | 111 |
110 mkvmuxer::int32 WebmMuxer::Position(mkvmuxer::int64 position) { | 112 mkvmuxer::int32 WebmMuxer::Position(mkvmuxer::int64 position) { |
111 // The stream is not Seekable() so indicate we cannot set the position. | 113 // The stream is not Seekable() so indicate we cannot set the position. |
112 return -1; | 114 return -1; |
113 } | 115 } |
114 | 116 |
115 bool WebmMuxer::Seekable() const { | 117 bool WebmMuxer::Seekable() const { |
116 return false; | 118 return false; |
117 } | 119 } |
118 | 120 |
119 void WebmMuxer::ElementStartNotify(mkvmuxer::uint64 element_id, | 121 void WebmMuxer::ElementStartNotify(mkvmuxer::uint64 element_id, |
120 mkvmuxer::int64 position) { | 122 mkvmuxer::int64 position) { |
121 // This method gets pinged before items are sent to |write_data_callback_|. | 123 // This method gets pinged before items are sent to |write_data_callback_|. |
122 DCHECK_GE(position, position_.ValueOrDefault(0)) | 124 DCHECK_GE(position, position_.ValueOrDefault(0)) |
123 << "Can't go back in a live WebM stream."; | 125 << "Can't go back in a live WebM stream."; |
124 } | 126 } |
125 | 127 |
126 } // namespace media | 128 } // namespace media |
OLD | NEW |