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"
16 #define NOTIFY_ERROR(error) \
17 PostNotifyError(error); \
20 GpuVideoEncodeAcceleratorHost::GpuVideoEncodeAcceleratorHost(
21 GpuChannelHost
* channel
,
22 CommandBufferProxyImpl
* impl
)
24 encoder_route_id_(MSG_ROUTING_NONE
),
28 weak_this_factory_(this) {
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_
);
39 impl_
->RemoveDeletionObserver(this);
42 bool GpuVideoEncodeAcceleratorHost::OnMessageReceived(
43 const IPC::Message
& message
) {
45 IPC_BEGIN_MESSAGE_MAP(GpuVideoEncodeAcceleratorHost
, message
)
46 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_RequireBitstreamBuffers
,
47 OnRequireBitstreamBuffers
)
48 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_NotifyInputDone
,
50 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_BitstreamBufferReady
,
51 OnBitstreamBufferReady
)
52 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_NotifyError
,
54 IPC_MESSAGE_UNHANDLED(handled
= false)
57 // See OnNotifyError for why |this| mustn't be used after OnNotifyError might
58 // have been called above.
62 void GpuVideoEncodeAcceleratorHost::OnChannelError() {
63 DCHECK(CalledOnValidThread());
65 if (encoder_route_id_
!= MSG_ROUTING_NONE
)
66 channel_
->RemoveRoute(encoder_route_id_
);
69 NOTIFY_ERROR(kPlatformFailureError
) << "OnChannelError()";
72 media::VideoEncodeAccelerator::SupportedProfiles
73 GpuVideoEncodeAcceleratorHost::GetSupportedProfiles() {
74 DCHECK(CalledOnValidThread());
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
,
87 DCHECK(CalledOnValidThread());
90 DLOG(ERROR
) << "impl_ destroyed";
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(),
106 DLOG(ERROR
) << "Send(GpuCommandBufferMsg_CreateVideoEncoder()) failed";
107 channel_
->RemoveRoute(route_id
);
110 encoder_route_id_
= route_id
;
114 void GpuVideoEncodeAcceleratorHost::Encode(
115 const scoped_refptr
<media::VideoFrame
>& frame
,
116 bool force_keyframe
) {
117 DCHECK(CalledOnValidThread());
121 if (!base::SharedMemory::IsHandleValid(frame
->shared_memory_handle())) {
122 NOTIFY_ERROR(kPlatformFailureError
)
123 << "Encode(): cannot encode frame not backed by shared memory";
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";
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
)))
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());
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();
168 Send(new AcceleratedVideoEncoderMsg_UseOutputBitstreamBuffer(
169 encoder_route_id_
, buffer
.id(), handle
, buffer
.size()));
172 void GpuVideoEncodeAcceleratorHost::RequestEncodingParametersChange(
175 DCHECK(CalledOnValidThread());
179 Send(new AcceleratedVideoEncoderMsg_RequestEncodingParametersChange(
180 encoder_route_id_
, bitrate
, framerate
));
183 void GpuVideoEncodeAcceleratorHost::Destroy() {
184 DCHECK(CalledOnValidThread());
186 Send(new AcceleratedVideoEncoderMsg_Destroy(encoder_route_id_
));
191 void GpuVideoEncodeAcceleratorHost::OnWillDeleteImpl() {
192 DCHECK(CalledOnValidThread());
195 // The CommandBufferProxyImpl is going away; error out this VEA.
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(
205 base::Bind(&GpuVideoEncodeAcceleratorHost::OnNotifyError
,
206 weak_this_factory_
.GetWeakPtr(),
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
219 void GpuVideoEncodeAcceleratorHost::OnRequireBitstreamBuffers(
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
;
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
247 OnNotifyError(kPlatformFailureError
);
250 frame
= NULL
; // Not necessary but nice to be explicit; see fun-fact above.
253 void GpuVideoEncodeAcceleratorHost::OnBitstreamBufferReady(
254 int32 bitstream_buffer_id
,
257 DCHECK(CalledOnValidThread());
258 DVLOG(3) << "OnBitstreamBufferReady(): "
259 "bitstream_buffer_id=" << bitstream_buffer_id
260 << ", payload_size=" << payload_size
261 << ", key_frame=" << key_frame
;
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
;
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