Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / common / gpu / client / gpu_video_encode_accelerator_host.cc
blob214b32d16aff3c3bd2770af34c988dea0c7f1a22
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
3 // found in the LICENSE file.
5 #include "content/common/gpu/client/gpu_video_encode_accelerator_host.h"
7 #include "base/logging.h"
8 #include "base/message_loop/message_loop_proxy.h"
9 #include "content/common/gpu/client/gpu_channel_host.h"
10 #include "content/common/gpu/gpu_messages.h"
11 #include "content/common/gpu/media/gpu_video_accelerator_util.h"
12 #include "media/base/video_frame.h"
14 namespace content {
16 #define NOTIFY_ERROR(error) \
17 PostNotifyError(error); \
18 DLOG(ERROR)
20 GpuVideoEncodeAcceleratorHost::GpuVideoEncodeAcceleratorHost(
21 GpuChannelHost* channel,
22 CommandBufferProxyImpl* impl)
23 : channel_(channel),
24 encoder_route_id_(MSG_ROUTING_NONE),
25 client_(NULL),
26 impl_(impl),
27 next_frame_id_(0),
28 weak_this_factory_(this) {
29 DCHECK(channel_);
30 DCHECK(impl_);
31 impl_->AddDeletionObserver(this);
34 GpuVideoEncodeAcceleratorHost::~GpuVideoEncodeAcceleratorHost() {
35 DCHECK(CalledOnValidThread());
36 if (channel_ && encoder_route_id_ != MSG_ROUTING_NONE)
37 channel_->RemoveRoute(encoder_route_id_);
38 if (impl_)
39 impl_->RemoveDeletionObserver(this);
42 bool GpuVideoEncodeAcceleratorHost::OnMessageReceived(
43 const IPC::Message& message) {
44 bool handled = true;
45 IPC_BEGIN_MESSAGE_MAP(GpuVideoEncodeAcceleratorHost, message)
46 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_RequireBitstreamBuffers,
47 OnRequireBitstreamBuffers)
48 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_NotifyInputDone,
49 OnNotifyInputDone)
50 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_BitstreamBufferReady,
51 OnBitstreamBufferReady)
52 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_NotifyError,
53 OnNotifyError)
54 IPC_MESSAGE_UNHANDLED(handled = false)
55 IPC_END_MESSAGE_MAP()
56 DCHECK(handled);
57 // See OnNotifyError for why |this| mustn't be used after OnNotifyError might
58 // have been called above.
59 return handled;
62 void GpuVideoEncodeAcceleratorHost::OnChannelError() {
63 DCHECK(CalledOnValidThread());
64 if (channel_) {
65 if (encoder_route_id_ != MSG_ROUTING_NONE)
66 channel_->RemoveRoute(encoder_route_id_);
67 channel_ = NULL;
69 NOTIFY_ERROR(kPlatformFailureError) << "OnChannelError()";
72 media::VideoEncodeAccelerator::SupportedProfiles
73 GpuVideoEncodeAcceleratorHost::GetSupportedProfiles() {
74 DCHECK(CalledOnValidThread());
75 if (!channel_)
76 return media::VideoEncodeAccelerator::SupportedProfiles();
77 return GpuVideoAcceleratorUtil::ConvertGpuToMediaEncodeProfiles(
78 channel_->gpu_info().video_encode_accelerator_supported_profiles);
81 bool GpuVideoEncodeAcceleratorHost::Initialize(
82 media::VideoFrame::Format input_format,
83 const gfx::Size& input_visible_size,
84 media::VideoCodecProfile output_profile,
85 uint32 initial_bitrate,
86 Client* client) {
87 DCHECK(CalledOnValidThread());
88 client_ = client;
89 if (!impl_) {
90 DLOG(ERROR) << "impl_ destroyed";
91 return false;
94 int32 route_id = channel_->GenerateRouteID();
95 channel_->AddRoute(route_id, weak_this_factory_.GetWeakPtr());
97 bool succeeded = false;
98 Send(new GpuCommandBufferMsg_CreateVideoEncoder(impl_->GetRouteID(),
99 input_format,
100 input_visible_size,
101 output_profile,
102 initial_bitrate,
103 route_id,
104 &succeeded));
105 if (!succeeded) {
106 DLOG(ERROR) << "Send(GpuCommandBufferMsg_CreateVideoEncoder()) failed";
107 channel_->RemoveRoute(route_id);
108 return false;
110 encoder_route_id_ = route_id;
111 return true;
114 void GpuVideoEncodeAcceleratorHost::Encode(
115 const scoped_refptr<media::VideoFrame>& frame,
116 bool force_keyframe) {
117 DCHECK(CalledOnValidThread());
118 if (!channel_)
119 return;
121 if (!base::SharedMemory::IsHandleValid(frame->shared_memory_handle())) {
122 NOTIFY_ERROR(kPlatformFailureError)
123 << "Encode(): cannot encode frame not backed by shared memory";
124 return;
126 base::SharedMemoryHandle handle =
127 channel_->ShareToGpuProcess(frame->shared_memory_handle());
128 if (!base::SharedMemory::IsHandleValid(handle)) {
129 NOTIFY_ERROR(kPlatformFailureError)
130 << "Encode(): failed to duplicate buffer handle for GPU process";
131 return;
134 // We assume that planar frame data passed here is packed and contiguous.
135 const size_t plane_count = media::VideoFrame::NumPlanes(frame->format());
136 size_t frame_size = 0;
137 for (size_t i = 0; i < plane_count; ++i) {
138 // Cast DCHECK parameters to void* to avoid printing uint8* as a string.
139 DCHECK_EQ(reinterpret_cast<void*>(frame->data(i)),
140 reinterpret_cast<void*>((frame->data(0) + frame_size)))
141 << "plane=" << i;
142 frame_size += frame->stride(i) * frame->rows(i);
145 Send(new AcceleratedVideoEncoderMsg_Encode(
146 encoder_route_id_, next_frame_id_, handle, frame->shared_memory_offset(),
147 frame_size, force_keyframe));
148 frame_map_[next_frame_id_] = frame;
150 // Mask against 30 bits, to avoid (undefined) wraparound on signed integer.
151 next_frame_id_ = (next_frame_id_ + 1) & 0x3FFFFFFF;
154 void GpuVideoEncodeAcceleratorHost::UseOutputBitstreamBuffer(
155 const media::BitstreamBuffer& buffer) {
156 DCHECK(CalledOnValidThread());
157 if (!channel_)
158 return;
160 base::SharedMemoryHandle handle =
161 channel_->ShareToGpuProcess(buffer.handle());
162 if (!base::SharedMemory::IsHandleValid(handle)) {
163 NOTIFY_ERROR(kPlatformFailureError)
164 << "UseOutputBitstreamBuffer(): failed to duplicate buffer handle "
165 "for GPU process: buffer.id()=" << buffer.id();
166 return;
168 Send(new AcceleratedVideoEncoderMsg_UseOutputBitstreamBuffer(
169 encoder_route_id_, buffer.id(), handle, buffer.size()));
172 void GpuVideoEncodeAcceleratorHost::RequestEncodingParametersChange(
173 uint32 bitrate,
174 uint32 framerate) {
175 DCHECK(CalledOnValidThread());
176 if (!channel_)
177 return;
179 Send(new AcceleratedVideoEncoderMsg_RequestEncodingParametersChange(
180 encoder_route_id_, bitrate, framerate));
183 void GpuVideoEncodeAcceleratorHost::Destroy() {
184 DCHECK(CalledOnValidThread());
185 if (channel_)
186 Send(new AcceleratedVideoEncoderMsg_Destroy(encoder_route_id_));
187 client_ = NULL;
188 delete this;
191 void GpuVideoEncodeAcceleratorHost::OnWillDeleteImpl() {
192 DCHECK(CalledOnValidThread());
193 impl_ = NULL;
195 // The CommandBufferProxyImpl is going away; error out this VEA.
196 OnChannelError();
199 void GpuVideoEncodeAcceleratorHost::PostNotifyError(Error error) {
200 DCHECK(CalledOnValidThread());
201 DVLOG(2) << "PostNotifyError(): error=" << error;
202 // Post the error notification back to this thread, to avoid re-entrancy.
203 base::MessageLoopProxy::current()->PostTask(
204 FROM_HERE,
205 base::Bind(&GpuVideoEncodeAcceleratorHost::OnNotifyError,
206 weak_this_factory_.GetWeakPtr(),
207 error));
210 void GpuVideoEncodeAcceleratorHost::Send(IPC::Message* message) {
211 DCHECK(CalledOnValidThread());
212 uint32 message_type = message->type();
213 if (!channel_->Send(message)) {
214 NOTIFY_ERROR(kPlatformFailureError) << "Send(" << message_type
215 << ") failed";
219 void GpuVideoEncodeAcceleratorHost::OnRequireBitstreamBuffers(
220 uint32 input_count,
221 const gfx::Size& input_coded_size,
222 uint32 output_buffer_size) {
223 DCHECK(CalledOnValidThread());
224 DVLOG(2) << "OnRequireBitstreamBuffers(): input_count=" << input_count
225 << ", input_coded_size=" << input_coded_size.ToString()
226 << ", output_buffer_size=" << output_buffer_size;
227 if (client_) {
228 client_->RequireBitstreamBuffers(
229 input_count, input_coded_size, output_buffer_size);
233 void GpuVideoEncodeAcceleratorHost::OnNotifyInputDone(int32 frame_id) {
234 DCHECK(CalledOnValidThread());
235 DVLOG(3) << "OnNotifyInputDone(): frame_id=" << frame_id;
236 // Fun-fact: std::hash_map is not spec'd to be re-entrant; since freeing a
237 // frame can trigger a further encode to be kicked off and thus an .insert()
238 // back into the map, we separate the frame's dtor running from the .erase()
239 // running by holding on to the frame temporarily. This isn't "just
240 // theoretical" - Android's std::hash_map crashes if we don't do this.
241 scoped_refptr<media::VideoFrame> frame = frame_map_[frame_id];
242 if (!frame_map_.erase(frame_id)) {
243 DLOG(ERROR) << "OnNotifyInputDone(): "
244 "invalid frame_id=" << frame_id;
245 // See OnNotifyError for why this needs to be the last thing in this
246 // function.
247 OnNotifyError(kPlatformFailureError);
248 return;
250 frame = NULL; // Not necessary but nice to be explicit; see fun-fact above.
253 void GpuVideoEncodeAcceleratorHost::OnBitstreamBufferReady(
254 int32 bitstream_buffer_id,
255 uint32 payload_size,
256 bool key_frame) {
257 DCHECK(CalledOnValidThread());
258 DVLOG(3) << "OnBitstreamBufferReady(): "
259 "bitstream_buffer_id=" << bitstream_buffer_id
260 << ", payload_size=" << payload_size
261 << ", key_frame=" << key_frame;
262 if (client_)
263 client_->BitstreamBufferReady(bitstream_buffer_id, payload_size, key_frame);
266 void GpuVideoEncodeAcceleratorHost::OnNotifyError(Error error) {
267 DCHECK(CalledOnValidThread());
268 DVLOG(2) << "OnNotifyError(): error=" << error;
269 if (!client_)
270 return;
271 weak_this_factory_.InvalidateWeakPtrs();
273 // Client::NotifyError() may Destroy() |this|, so calling it needs to be the
274 // last thing done on this stack!
275 media::VideoEncodeAccelerator::Client* client = NULL;
276 std::swap(client_, client);
277 client->NotifyError(error);
280 } // namespace content