OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/common/gpu/media/android_video_encode_accelerator.h" | 5 #include "content/common/gpu/media/android_video_encode_accelerator.h" |
6 | 6 |
7 #include <algorithm> | |
8 #include <vector> | |
9 | |
7 #include "base/bind.h" | 10 #include "base/bind.h" |
8 #include "base/command_line.h" | 11 #include "base/command_line.h" |
9 #include "base/logging.h" | 12 #include "base/logging.h" |
10 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
11 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
12 #include "content/common/gpu/gpu_channel.h" | 15 #include "content/common/gpu/gpu_channel.h" |
13 #include "content/public/common/content_switches.h" | 16 #include "content/public/common/content_switches.h" |
14 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" | 17 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" |
15 #include "media/base/android/media_codec_bridge.h" | 18 #include "media/base/android/media_codec_bridge.h" |
16 #include "media/base/bitstream_buffer.h" | 19 #include "media/base/bitstream_buffer.h" |
17 #include "media/base/limits.h" | 20 #include "media/base/limits.h" |
18 #include "media/video/picture.h" | 21 #include "media/video/picture.h" |
19 #include "third_party/libyuv/include/libyuv/convert_from.h" | 22 #include "third_party/libyuv/include/libyuv/convert_from.h" |
20 #include "ui/gl/android/scoped_java_surface.h" | 23 #include "ui/gl/android/scoped_java_surface.h" |
21 #include "ui/gl/gl_bindings.h" | 24 #include "ui/gl/gl_bindings.h" |
22 | 25 |
23 using media::MediaCodecBridge; | 26 using media::MediaCodecBridge; |
24 using media::VideoCodecBridge; | 27 using media::VideoCodecBridge; |
25 using media::VideoFrame; | 28 using media::VideoFrame; |
26 | 29 |
27 namespace content { | 30 namespace content { |
28 | 31 |
29 enum { | 32 enum ColorFormat { |
mcasas
2014/10/10 08:16:16
Please consider calling this PixelFormat.
changbin
2014/10/10 08:55:51
Done.
| |
30 // Subset of MediaCodecInfo.CodecCapabilities. | 33 // Subset of MediaCodecInfo.CodecCapabilities. |
34 COLOR_FORMAT_YUV420_PLANAR = 19, | |
31 COLOR_FORMAT_YUV420_SEMIPLANAR = 21, | 35 COLOR_FORMAT_YUV420_SEMIPLANAR = 21, |
32 }; | 36 }; |
33 | 37 |
34 // Helper macros for dealing with failure. If |result| evaluates false, emit | 38 // Helper macros for dealing with failure. If |result| evaluates false, emit |
35 // |log| to DLOG(ERROR), register |error| with the client, and return. | 39 // |log| to DLOG(ERROR), register |error| with the client, and return. |
36 #define RETURN_ON_FAILURE(result, log, error) \ | 40 #define RETURN_ON_FAILURE(result, log, error) \ |
37 do { \ | 41 do { \ |
38 if (!(result)) { \ | 42 if (!(result)) { \ |
39 DLOG(ERROR) << log; \ | 43 DLOG(ERROR) << log; \ |
40 if (client_ptr_factory_->GetWeakPtr()) { \ | 44 if (client_ptr_factory_->GetWeakPtr()) { \ |
(...skipping 19 matching lines...) Expand all Loading... | |
60 // pictures have been fed to saturate any internal buffering). This is | 64 // pictures have been fed to saturate any internal buffering). This is |
61 // speculative and it's unclear that this would be a win (nor that there's a | 65 // speculative and it's unclear that this would be a win (nor that there's a |
62 // reasonably device-agnostic way to fill in the "believes" above). | 66 // reasonably device-agnostic way to fill in the "believes" above). |
63 return base::TimeDelta::FromMilliseconds(10); | 67 return base::TimeDelta::FromMilliseconds(10); |
64 } | 68 } |
65 | 69 |
66 static inline const base::TimeDelta NoWaitTimeOut() { | 70 static inline const base::TimeDelta NoWaitTimeOut() { |
67 return base::TimeDelta::FromMicroseconds(0); | 71 return base::TimeDelta::FromMicroseconds(0); |
68 } | 72 } |
69 | 73 |
74 ColorFormat GetSupportedColorFormatForMime(const std::string& mime) { | |
mcasas
2014/10/10 08:16:16
static
changbin
2014/10/10 08:55:51
Done.
| |
75 std::vector<int> formats = MediaCodecBridge::GetEncoderColorFormats(mime); | |
76 for (std::vector<int>::iterator it = formats.begin(); it != formats.end(); | |
mcasas
2014/10/10 08:16:16
Please use range-based for loop and const auto [1]
changbin
2014/10/10 08:55:51
Thanks for suggestion. Looks the loop can be remov
| |
77 ++it) | |
78 if (std::find(formats.begin(), formats.end(), COLOR_FORMAT_YUV420_PLANAR) != | |
79 formats.end()) | |
80 return COLOR_FORMAT_YUV420_PLANAR; | |
81 | |
mcasas
2014/10/10 08:16:16
nit: remove empty line.
changbin
2014/10/10 08:55:51
Done.
| |
82 return COLOR_FORMAT_YUV420_SEMIPLANAR; | |
83 } | |
84 | |
70 AndroidVideoEncodeAccelerator::AndroidVideoEncodeAccelerator() | 85 AndroidVideoEncodeAccelerator::AndroidVideoEncodeAccelerator() |
71 : num_buffers_at_codec_(0), | 86 : num_buffers_at_codec_(0), |
72 num_output_buffers_(-1), | 87 num_output_buffers_(-1), |
73 output_buffers_capacity_(0), | 88 output_buffers_capacity_(0), |
74 last_set_bitrate_(0) {} | 89 last_set_bitrate_(0) {} |
75 | 90 |
76 AndroidVideoEncodeAccelerator::~AndroidVideoEncodeAccelerator() { | 91 AndroidVideoEncodeAccelerator::~AndroidVideoEncodeAccelerator() { |
77 DCHECK(thread_checker_.CalledOnValidThread()); | 92 DCHECK(thread_checker_.CalledOnValidThread()); |
78 } | 93 } |
79 | 94 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
135 | 150 |
136 last_set_bitrate_ = initial_bitrate; | 151 last_set_bitrate_ = initial_bitrate; |
137 | 152 |
138 // Only consider using MediaCodec if it's likely backed by hardware. | 153 // Only consider using MediaCodec if it's likely backed by hardware. |
139 if (media::VideoCodecBridge::IsKnownUnaccelerated( | 154 if (media::VideoCodecBridge::IsKnownUnaccelerated( |
140 media::kCodecVP8, media::MEDIA_CODEC_ENCODER)) { | 155 media::kCodecVP8, media::MEDIA_CODEC_ENCODER)) { |
141 DLOG(ERROR) << "No HW support"; | 156 DLOG(ERROR) << "No HW support"; |
142 return false; | 157 return false; |
143 } | 158 } |
144 | 159 |
145 // TODO(fischman): when there is more HW out there with different color-space | 160 std::string mime = "video/x-vnd.on2.vp8"; |
146 // support, this should turn into a negotiation with the codec for supported | 161 media_codec_.reset(media::VideoCodecBridge::CreateEncoder( |
147 // formats. For now we use the only format supported by the only available | 162 media::kCodecVP8, |
148 // HW. | 163 input_visible_size, |
149 media_codec_.reset( | 164 initial_bitrate, |
150 media::VideoCodecBridge::CreateEncoder(media::kCodecVP8, | 165 INITIAL_FRAMERATE, |
151 input_visible_size, | 166 IFRAME_INTERVAL, |
152 initial_bitrate, | 167 GetSupportedColorFormatForMime(mime))); |
153 INITIAL_FRAMERATE, | |
154 IFRAME_INTERVAL, | |
155 COLOR_FORMAT_YUV420_SEMIPLANAR)); | |
156 | 168 |
157 if (!media_codec_) { | 169 if (!media_codec_) { |
158 DLOG(ERROR) << "Failed to create/start the codec: " | 170 DLOG(ERROR) << "Failed to create/start the codec: " |
159 << input_visible_size.ToString(); | 171 << input_visible_size.ToString(); |
160 return false; | 172 return false; |
161 } | 173 } |
162 | 174 |
163 num_output_buffers_ = media_codec_->GetOutputBuffersCount(); | 175 num_output_buffers_ = media_codec_->GetOutputBuffersCount(); |
164 output_buffers_capacity_ = media_codec_->GetOutputBuffersCapacity(); | 176 output_buffers_capacity_ = media_codec_->GetOutputBuffersCapacity(); |
165 base::MessageLoop::current()->PostTask( | 177 base::MessageLoop::current()->PostTask( |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
403 base::MessageLoop::current()->PostTask( | 415 base::MessageLoop::current()->PostTask( |
404 FROM_HERE, | 416 FROM_HERE, |
405 base::Bind(&VideoEncodeAccelerator::Client::BitstreamBufferReady, | 417 base::Bind(&VideoEncodeAccelerator::Client::BitstreamBufferReady, |
406 client_ptr_factory_->GetWeakPtr(), | 418 client_ptr_factory_->GetWeakPtr(), |
407 bitstream_buffer.id(), | 419 bitstream_buffer.id(), |
408 size, | 420 size, |
409 key_frame)); | 421 key_frame)); |
410 } | 422 } |
411 | 423 |
412 } // namespace content | 424 } // namespace content |
OLD | NEW |