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/gpu_video_encode_accelerator.h" | 5 #include "content/common/gpu/media/gpu_video_encode_accelerator.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/shared_memory.h" | 9 #include "base/memory/shared_memory.h" |
10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
12 #include "content/common/gpu/gpu_channel.h" | 12 #include "content/common/gpu/gpu_channel.h" |
13 #include "content/common/gpu/gpu_messages.h" | 13 #include "content/common/gpu/gpu_messages.h" |
14 #include "ipc/ipc_message_macros.h" | 14 #include "ipc/ipc_message_macros.h" |
15 #include "media/base/limits.h" | 15 #include "media/base/limits.h" |
16 #include "media/base/video_frame.h" | 16 #include "media/base/video_frame.h" |
17 | 17 |
18 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) && defined(USE_X11) | 18 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) && defined(USE_X11) |
19 #include "content/common/gpu/media/exynos_video_encode_accelerator.h" | 19 #include "content/common/gpu/media/exynos_video_encode_accelerator.h" |
20 #elif defined(OS_ANDROID) && defined(ENABLE_WEBRTC) | 20 #elif defined(OS_ANDROID) && defined(ENABLE_WEBRTC) |
21 #include "content/common/gpu/media/android_video_encode_accelerator.h" | 21 #include "content/common/gpu/media/android_video_encode_accelerator.h" |
22 #endif | 22 #endif |
23 | 23 |
24 namespace content { | 24 namespace content { |
25 | 25 |
26 GpuVideoEncodeAccelerator::GpuVideoEncodeAccelerator(GpuChannel* gpu_channel, | 26 static bool MakeDecoderContextCurrent( |
27 int32 route_id) | 27 const base::WeakPtr<GpuCommandBufferStub> stub) { |
28 : weak_this_factory_(this), | 28 if (!stub) { |
29 channel_(gpu_channel), | 29 DLOG(ERROR) << "Stub is gone; won't MakeCurrent()."; |
30 route_id_(route_id), | 30 return false; |
| 31 } |
| 32 |
| 33 if (!stub->decoder()->MakeCurrent()) { |
| 34 DLOG(ERROR) << "Failed to MakeCurrent()"; |
| 35 return false; |
| 36 } |
| 37 |
| 38 return true; |
| 39 } |
| 40 |
| 41 GpuVideoEncodeAccelerator::GpuVideoEncodeAccelerator(int32 host_route_id, |
| 42 GpuCommandBufferStub* stub) |
| 43 : host_route_id_(host_route_id), |
| 44 stub_(stub), |
31 input_format_(media::VideoFrame::UNKNOWN), | 45 input_format_(media::VideoFrame::UNKNOWN), |
32 output_buffer_size_(0) {} | 46 output_buffer_size_(0), |
| 47 weak_this_factory_(this) { |
| 48 stub_->AddDestructionObserver(this); |
| 49 stub_->channel()->AddRoute(host_route_id_, this); |
| 50 make_context_current_ = |
| 51 base::Bind(&MakeDecoderContextCurrent, stub_->AsWeakPtr()); |
| 52 } |
33 | 53 |
34 GpuVideoEncodeAccelerator::~GpuVideoEncodeAccelerator() { | 54 GpuVideoEncodeAccelerator::~GpuVideoEncodeAccelerator() { |
35 if (encoder_) | 55 // This class can only be self-deleted from OnWillDestroyStub(), which means |
36 encoder_.release()->Destroy(); | 56 // the VEA has already been destroyed in there. |
| 57 DCHECK(!encoder_); |
| 58 } |
| 59 |
| 60 void GpuVideoEncodeAccelerator::Initialize( |
| 61 media::VideoFrame::Format input_format, |
| 62 const gfx::Size& input_visible_size, |
| 63 media::VideoCodecProfile output_profile, |
| 64 uint32 initial_bitrate, |
| 65 IPC::Message* init_done_msg) { |
| 66 DVLOG(2) << "GpuVideoEncodeAccelerator::Initialize(): " |
| 67 "input_format=" << input_format |
| 68 << ", input_visible_size=" << input_visible_size.ToString() |
| 69 << ", output_profile=" << output_profile |
| 70 << ", initial_bitrate=" << initial_bitrate; |
| 71 DCHECK(!encoder_); |
| 72 |
| 73 if (input_visible_size.width() > media::limits::kMaxDimension || |
| 74 input_visible_size.height() > media::limits::kMaxDimension || |
| 75 input_visible_size.GetArea() > media::limits::kMaxCanvas) { |
| 76 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Initialize(): " |
| 77 "input_visible_size " << input_visible_size.ToString() |
| 78 << " too large"; |
| 79 SendCreateEncoderReply(init_done_msg, MSG_ROUTING_NONE); |
| 80 return; |
| 81 } |
| 82 |
| 83 CreateEncoder(); |
| 84 if (!encoder_) { |
| 85 DLOG(ERROR) |
| 86 << "GpuVideoEncodeAccelerator::Initialize(): VEA creation failed"; |
| 87 SendCreateEncoderReply(init_done_msg, MSG_ROUTING_NONE); |
| 88 return; |
| 89 } |
| 90 if (!encoder_->Initialize(input_format, |
| 91 input_visible_size, |
| 92 output_profile, |
| 93 initial_bitrate, |
| 94 this)) { |
| 95 DLOG(ERROR) |
| 96 << "GpuVideoEncodeAccelerator::Initialize(): VEA initialization failed"; |
| 97 SendCreateEncoderReply(init_done_msg, MSG_ROUTING_NONE); |
| 98 return; |
| 99 } |
| 100 input_format_ = input_format; |
| 101 input_visible_size_ = input_visible_size; |
| 102 SendCreateEncoderReply(init_done_msg, host_route_id_); |
37 } | 103 } |
38 | 104 |
39 bool GpuVideoEncodeAccelerator::OnMessageReceived(const IPC::Message& message) { | 105 bool GpuVideoEncodeAccelerator::OnMessageReceived(const IPC::Message& message) { |
40 bool handled = true; | 106 bool handled = true; |
41 IPC_BEGIN_MESSAGE_MAP(GpuVideoEncodeAccelerator, message) | 107 IPC_BEGIN_MESSAGE_MAP(GpuVideoEncodeAccelerator, message) |
42 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_Initialize, OnInitialize) | |
43 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_Encode, OnEncode) | 108 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_Encode, OnEncode) |
44 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_UseOutputBitstreamBuffer, | 109 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_UseOutputBitstreamBuffer, |
45 OnUseOutputBitstreamBuffer) | 110 OnUseOutputBitstreamBuffer) |
46 IPC_MESSAGE_HANDLER( | 111 IPC_MESSAGE_HANDLER( |
47 AcceleratedVideoEncoderMsg_RequestEncodingParametersChange, | 112 AcceleratedVideoEncoderMsg_RequestEncodingParametersChange, |
48 OnRequestEncodingParametersChange) | 113 OnRequestEncodingParametersChange) |
| 114 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_Destroy, OnDestroy) |
49 IPC_MESSAGE_UNHANDLED(handled = false) | 115 IPC_MESSAGE_UNHANDLED(handled = false) |
50 IPC_END_MESSAGE_MAP() | 116 IPC_END_MESSAGE_MAP() |
51 return handled; | 117 return handled; |
52 } | 118 } |
53 | 119 |
54 void GpuVideoEncodeAccelerator::OnChannelError() { | |
55 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); | |
56 if (channel_) | |
57 channel_ = NULL; | |
58 } | |
59 | |
60 void GpuVideoEncodeAccelerator::NotifyInitializeDone() { | |
61 Send(new AcceleratedVideoEncoderHostMsg_NotifyInitializeDone(route_id_)); | |
62 } | |
63 | |
64 void GpuVideoEncodeAccelerator::RequireBitstreamBuffers( | 120 void GpuVideoEncodeAccelerator::RequireBitstreamBuffers( |
65 unsigned int input_count, | 121 unsigned int input_count, |
66 const gfx::Size& input_coded_size, | 122 const gfx::Size& input_coded_size, |
67 size_t output_buffer_size) { | 123 size_t output_buffer_size) { |
68 Send(new AcceleratedVideoEncoderHostMsg_RequireBitstreamBuffers( | 124 Send(new AcceleratedVideoEncoderHostMsg_RequireBitstreamBuffers( |
69 route_id_, input_count, input_coded_size, output_buffer_size)); | 125 host_route_id_, input_count, input_coded_size, output_buffer_size)); |
70 input_coded_size_ = input_coded_size; | 126 input_coded_size_ = input_coded_size; |
71 output_buffer_size_ = output_buffer_size; | 127 output_buffer_size_ = output_buffer_size; |
72 } | 128 } |
73 | 129 |
74 void GpuVideoEncodeAccelerator::BitstreamBufferReady(int32 bitstream_buffer_id, | 130 void GpuVideoEncodeAccelerator::BitstreamBufferReady(int32 bitstream_buffer_id, |
75 size_t payload_size, | 131 size_t payload_size, |
76 bool key_frame) { | 132 bool key_frame) { |
77 Send(new AcceleratedVideoEncoderHostMsg_BitstreamBufferReady( | 133 Send(new AcceleratedVideoEncoderHostMsg_BitstreamBufferReady( |
78 route_id_, bitstream_buffer_id, payload_size, key_frame)); | 134 host_route_id_, bitstream_buffer_id, payload_size, key_frame)); |
79 } | 135 } |
80 | 136 |
81 void GpuVideoEncodeAccelerator::NotifyError( | 137 void GpuVideoEncodeAccelerator::NotifyError( |
82 media::VideoEncodeAccelerator::Error error) { | 138 media::VideoEncodeAccelerator::Error error) { |
83 Send(new AcceleratedVideoEncoderHostMsg_NotifyError(route_id_, error)); | 139 Send(new AcceleratedVideoEncoderHostMsg_NotifyError(host_route_id_, error)); |
| 140 } |
| 141 |
| 142 void GpuVideoEncodeAccelerator::OnWillDestroyStub() { |
| 143 DCHECK(stub_); |
| 144 stub_->channel()->RemoveRoute(host_route_id_); |
| 145 stub_->RemoveDestructionObserver(this); |
| 146 |
| 147 if (encoder_) |
| 148 encoder_.release()->Destroy(); |
| 149 |
| 150 delete this; |
84 } | 151 } |
85 | 152 |
86 // static | 153 // static |
87 std::vector<media::VideoEncodeAccelerator::SupportedProfile> | 154 std::vector<media::VideoEncodeAccelerator::SupportedProfile> |
88 GpuVideoEncodeAccelerator::GetSupportedProfiles() { | 155 GpuVideoEncodeAccelerator::GetSupportedProfiles() { |
89 std::vector<media::VideoEncodeAccelerator::SupportedProfile> profiles; | 156 std::vector<media::VideoEncodeAccelerator::SupportedProfile> profiles; |
90 | 157 |
91 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) && defined(USE_X11) | 158 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) && defined(USE_X11) |
92 profiles = ExynosVideoEncodeAccelerator::GetSupportedProfiles(); | 159 profiles = ExynosVideoEncodeAccelerator::GetSupportedProfiles(); |
93 #elif defined(OS_ANDROID) && defined(ENABLE_WEBRTC) | 160 #elif defined(OS_ANDROID) && defined(ENABLE_WEBRTC) |
94 profiles = AndroidVideoEncodeAccelerator::GetSupportedProfiles(); | 161 profiles = AndroidVideoEncodeAccelerator::GetSupportedProfiles(); |
95 #endif | 162 #endif |
96 | 163 |
97 // TODO(sheu): return platform-specific profiles. | 164 // TODO(sheu): return platform-specific profiles. |
98 return profiles; | 165 return profiles; |
99 } | 166 } |
100 | 167 |
101 void GpuVideoEncodeAccelerator::CreateEncoder() { | 168 void GpuVideoEncodeAccelerator::CreateEncoder() { |
102 DCHECK(!encoder_); | 169 DCHECK(!encoder_); |
103 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) && defined(USE_X11) | 170 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) && defined(USE_X11) |
104 encoder_.reset(new ExynosVideoEncodeAccelerator()); | 171 encoder_.reset(new ExynosVideoEncodeAccelerator()); |
105 #elif defined(OS_ANDROID) && defined(ENABLE_WEBRTC) | 172 #elif defined(OS_ANDROID) && defined(ENABLE_WEBRTC) |
106 encoder_.reset(new AndroidVideoEncodeAccelerator()); | 173 encoder_.reset(new AndroidVideoEncodeAccelerator()); |
107 #endif | 174 #endif |
108 } | 175 } |
109 | 176 |
110 void GpuVideoEncodeAccelerator::OnInitialize( | |
111 media::VideoFrame::Format input_format, | |
112 const gfx::Size& input_visible_size, | |
113 media::VideoCodecProfile output_profile, | |
114 uint32 initial_bitrate) { | |
115 DVLOG(2) << "GpuVideoEncodeAccelerator::OnInitialize(): " | |
116 "input_format=" << input_format | |
117 << ", input_visible_size=" << input_visible_size.ToString() | |
118 << ", output_profile=" << output_profile | |
119 << ", initial_bitrate=" << initial_bitrate; | |
120 DCHECK(!encoder_); | |
121 | |
122 if (input_visible_size.width() > media::limits::kMaxDimension || | |
123 input_visible_size.height() > media::limits::kMaxDimension || | |
124 input_visible_size.GetArea() > media::limits::kMaxCanvas) { | |
125 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnInitialize(): " | |
126 "input_visible_size " << input_visible_size.ToString() | |
127 << " too large"; | |
128 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); | |
129 return; | |
130 } | |
131 | |
132 CreateEncoder(); | |
133 if (!encoder_) { | |
134 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnInitialize(): VEA creation " | |
135 "failed"; | |
136 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); | |
137 return; | |
138 } | |
139 encoder_->Initialize( | |
140 input_format, input_visible_size, output_profile, initial_bitrate, this); | |
141 input_format_ = input_format; | |
142 input_visible_size_ = input_visible_size; | |
143 } | |
144 | |
145 void GpuVideoEncodeAccelerator::OnEncode(int32 frame_id, | 177 void GpuVideoEncodeAccelerator::OnEncode(int32 frame_id, |
146 base::SharedMemoryHandle buffer_handle, | 178 base::SharedMemoryHandle buffer_handle, |
147 uint32 buffer_size, | 179 uint32 buffer_size, |
148 bool force_keyframe) { | 180 bool force_keyframe) { |
149 DVLOG(3) << "GpuVideoEncodeAccelerator::OnEncode(): frame_id=" << frame_id | 181 DVLOG(3) << "GpuVideoEncodeAccelerator::OnEncode(): frame_id=" << frame_id |
150 << ", buffer_size=" << buffer_size | 182 << ", buffer_size=" << buffer_size |
151 << ", force_keyframe=" << force_keyframe; | 183 << ", force_keyframe=" << force_keyframe; |
152 if (!encoder_) | 184 if (!encoder_) |
153 return; | 185 return; |
154 if (frame_id < 0) { | 186 if (frame_id < 0) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 if (buffer_size < output_buffer_size_) { | 247 if (buffer_size < output_buffer_size_) { |
216 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer(): " | 248 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer(): " |
217 "buffer too small for buffer_id=" << buffer_id; | 249 "buffer too small for buffer_id=" << buffer_id; |
218 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); | 250 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
219 return; | 251 return; |
220 } | 252 } |
221 encoder_->UseOutputBitstreamBuffer( | 253 encoder_->UseOutputBitstreamBuffer( |
222 media::BitstreamBuffer(buffer_id, buffer_handle, buffer_size)); | 254 media::BitstreamBuffer(buffer_id, buffer_handle, buffer_size)); |
223 } | 255 } |
224 | 256 |
| 257 void GpuVideoEncodeAccelerator::OnDestroy() { |
| 258 DVLOG(2) << "GpuVideoEncodeAccelerator::OnDestroy()"; |
| 259 OnWillDestroyStub(); |
| 260 } |
| 261 |
225 void GpuVideoEncodeAccelerator::OnRequestEncodingParametersChange( | 262 void GpuVideoEncodeAccelerator::OnRequestEncodingParametersChange( |
226 uint32 bitrate, | 263 uint32 bitrate, |
227 uint32 framerate) { | 264 uint32 framerate) { |
228 DVLOG(2) << "GpuVideoEncodeAccelerator::OnRequestEncodingParametersChange(): " | 265 DVLOG(2) << "GpuVideoEncodeAccelerator::OnRequestEncodingParametersChange(): " |
229 "bitrate=" << bitrate | 266 "bitrate=" << bitrate |
230 << ", framerate=" << framerate; | 267 << ", framerate=" << framerate; |
231 if (!encoder_) | 268 if (!encoder_) |
232 return; | 269 return; |
233 encoder_->RequestEncodingParametersChange(bitrate, framerate); | 270 encoder_->RequestEncodingParametersChange(bitrate, framerate); |
234 } | 271 } |
235 | 272 |
236 void GpuVideoEncodeAccelerator::EncodeFrameFinished( | 273 void GpuVideoEncodeAccelerator::EncodeFrameFinished( |
237 int32 frame_id, | 274 int32 frame_id, |
238 scoped_ptr<base::SharedMemory> shm) { | 275 scoped_ptr<base::SharedMemory> shm) { |
239 Send(new AcceleratedVideoEncoderHostMsg_NotifyInputDone(route_id_, frame_id)); | 276 Send(new AcceleratedVideoEncoderHostMsg_NotifyInputDone(host_route_id_, |
| 277 frame_id)); |
240 // Just let shm fall out of scope. | 278 // Just let shm fall out of scope. |
241 } | 279 } |
242 | 280 |
243 void GpuVideoEncodeAccelerator::Send(IPC::Message* message) { | 281 void GpuVideoEncodeAccelerator::Send(IPC::Message* message) { |
244 if (!channel_) { | 282 stub_->channel()->Send(message); |
245 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Send(): no channel"; | 283 } |
246 delete message; | 284 |
247 return; | 285 void GpuVideoEncodeAccelerator::SendCreateEncoderReply(IPC::Message* message, |
248 } else if (!channel_->Send(message)) { | 286 int32 route_id) { |
249 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Send(): sending failed: " | 287 GpuCommandBufferMsg_CreateVideoEncoder::WriteReplyParams(message, route_id); |
250 "message->type()=" << message->type(); | 288 Send(message); |
251 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); | |
252 return; | |
253 } | |
254 } | 289 } |
255 | 290 |
256 } // namespace content | 291 } // namespace content |
OLD | NEW |