| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 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. |
| 4 |
| 5 #include "content/common/gpu/client/gpu_video_encode_accelerator_host.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "content/common/gpu/client/gpu_channel_host.h" |
| 9 #include "content/common/gpu/gpu_messages.h" |
| 10 #include "content/common/gpu/media/gpu_video_encode_accelerator.h" |
| 11 #include "media/base/video_frame.h" |
| 12 |
| 13 namespace content { |
| 14 |
| 15 GpuVideoEncodeAcceleratorHost::GpuVideoEncodeAcceleratorHost( |
| 16 media::VideoEncodeAccelerator::Client* client, |
| 17 const scoped_refptr<GpuChannelHost>& gpu_channel_host, |
| 18 int32 route_id) |
| 19 : client_(client), |
| 20 channel_(gpu_channel_host), |
| 21 route_id_(route_id), |
| 22 next_frame_id_(0) { |
| 23 channel_->AddRoute(route_id_, AsWeakPtr()); |
| 24 } |
| 25 |
| 26 GpuVideoEncodeAcceleratorHost::~GpuVideoEncodeAcceleratorHost() { |
| 27 if (channel_) |
| 28 channel_->RemoveRoute(route_id_); |
| 29 } |
| 30 |
| 31 // static |
| 32 std::vector<media::VideoEncodeAccelerator::SupportedProfile> |
| 33 GpuVideoEncodeAcceleratorHost::GetSupportedProfiles() { |
| 34 return GpuVideoEncodeAccelerator::GetSupportedProfiles(); |
| 35 } |
| 36 |
| 37 bool GpuVideoEncodeAcceleratorHost::OnMessageReceived( |
| 38 const IPC::Message& message) { |
| 39 bool handled = true; |
| 40 IPC_BEGIN_MESSAGE_MAP(GpuVideoEncodeAcceleratorHost, message) |
| 41 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_NotifyInitializeDone, |
| 42 OnNotifyInitializeDone) |
| 43 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_RequireBitstreamBuffers, |
| 44 OnRequireBitstreamBuffers) |
| 45 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_NotifyInputDone, |
| 46 OnNotifyInputDone) |
| 47 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_BitstreamBufferReady, |
| 48 OnBitstreamBufferReady) |
| 49 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_NotifyError, |
| 50 OnNotifyError) |
| 51 IPC_MESSAGE_UNHANDLED(handled = false) |
| 52 IPC_END_MESSAGE_MAP() |
| 53 DCHECK(handled); |
| 54 return handled; |
| 55 } |
| 56 |
| 57 void GpuVideoEncodeAcceleratorHost::OnChannelError() { |
| 58 DLOG(ERROR) << "OnChannelError()"; |
| 59 OnNotifyError(kPlatformFailureError); |
| 60 if (channel_) { |
| 61 channel_->RemoveRoute(route_id_); |
| 62 channel_ = NULL; |
| 63 } |
| 64 } |
| 65 |
| 66 void GpuVideoEncodeAcceleratorHost::Initialize( |
| 67 media::VideoFrame::Format input_format, |
| 68 const gfx::Size& input_visible_size, |
| 69 media::VideoCodecProfile output_profile, |
| 70 int32 initial_bitrate) { |
| 71 Send(new AcceleratedVideoEncoderMsg_Initialize(route_id_, |
| 72 input_format, |
| 73 input_visible_size, |
| 74 output_profile, |
| 75 initial_bitrate)); |
| 76 } |
| 77 |
| 78 void GpuVideoEncodeAcceleratorHost::Encode( |
| 79 const scoped_refptr<media::VideoFrame>& frame, |
| 80 bool force_keyframe) { |
| 81 if (!channel_) |
| 82 return; |
| 83 if (!base::SharedMemory::IsHandleValid(frame->shared_memory_handle())) { |
| 84 DLOG(ERROR) << "Encode(): cannot encode frame not backed by shared memory"; |
| 85 OnNotifyError(kPlatformFailureError); |
| 86 return; |
| 87 } |
| 88 base::SharedMemoryHandle handle = |
| 89 channel_->ShareToGpuProcess(frame->shared_memory_handle()); |
| 90 if (!base::SharedMemory::IsHandleValid(handle)) { |
| 91 DLOG(ERROR) << "Encode(): failed to duplicate buffer handle for GPU " |
| 92 "process"; |
| 93 OnNotifyError(kPlatformFailureError); |
| 94 return; |
| 95 } |
| 96 |
| 97 // We assume that planar frame data passed here is packed and contiguous. |
| 98 const size_t plane_count = media::VideoFrame::NumPlanes(frame->format()); |
| 99 size_t frame_size = 0; |
| 100 for (size_t i = 0; i < plane_count; ++i) { |
| 101 // Cast DCHECK parameters to void* to avoid printing uint8* as a string. |
| 102 DCHECK_EQ(reinterpret_cast<void*>(frame->data(i)), |
| 103 reinterpret_cast<void*>((frame->data(0) + frame_size))) |
| 104 << "plane=" << i; |
| 105 frame_size += frame->stride(i) * frame->rows(i); |
| 106 } |
| 107 |
| 108 Send(new AcceleratedVideoEncoderMsg_Encode( |
| 109 route_id_, next_frame_id_, handle, frame_size, force_keyframe)); |
| 110 frame_map_[next_frame_id_] = frame; |
| 111 |
| 112 // Mask against 30 bits, to avoid (undefined) wraparound on signed integer. |
| 113 next_frame_id_ = (next_frame_id_ + 1) & 0x3FFFFFFF; |
| 114 } |
| 115 |
| 116 void GpuVideoEncodeAcceleratorHost::UseOutputBitstreamBuffer( |
| 117 const media::BitstreamBuffer& buffer) { |
| 118 if (!channel_) |
| 119 return; |
| 120 base::SharedMemoryHandle handle = |
| 121 channel_->ShareToGpuProcess(buffer.handle()); |
| 122 if (!base::SharedMemory::IsHandleValid(handle)) { |
| 123 DLOG(ERROR) << "UseOutputBitstreamBuffer(): failed to duplicate buffer " |
| 124 "handle for GPU process: buffer.id()=" << buffer.id(); |
| 125 OnNotifyError(kPlatformFailureError); |
| 126 return; |
| 127 } |
| 128 Send(new AcceleratedVideoEncoderMsg_UseOutputBitstreamBuffer( |
| 129 route_id_, buffer.id(), handle, buffer.size())); |
| 130 } |
| 131 |
| 132 void GpuVideoEncodeAcceleratorHost::RequestEncodingParametersChange( |
| 133 int32 bitrate, |
| 134 uint32 framerate) { |
| 135 Send(new AcceleratedVideoEncoderMsg_RequestEncodingParametersChange( |
| 136 route_id_, bitrate, framerate)); |
| 137 } |
| 138 |
| 139 void GpuVideoEncodeAcceleratorHost::Destroy() { |
| 140 Send(new GpuChannelMsg_DestroyVideoEncoder(route_id_)); |
| 141 delete this; |
| 142 } |
| 143 |
| 144 void GpuVideoEncodeAcceleratorHost::OnNotifyInitializeDone() { |
| 145 DVLOG(2) << "OnNotifyInitializeDone()"; |
| 146 if (client_) |
| 147 client_->NotifyInitializeDone(); |
| 148 } |
| 149 |
| 150 void GpuVideoEncodeAcceleratorHost::OnRequireBitstreamBuffers( |
| 151 int input_count, |
| 152 const gfx::Size& input_coded_size, |
| 153 uint32 output_buffer_size) { |
| 154 DVLOG(2) << "OnRequireBitstreamBuffers(): input_count=" << input_count |
| 155 << ", input_coded_size=" << input_coded_size.ToString() |
| 156 << ", output_buffer_size=" << output_buffer_size; |
| 157 if (client_) { |
| 158 client_->RequireBitstreamBuffers( |
| 159 input_count, input_coded_size, output_buffer_size); |
| 160 } |
| 161 } |
| 162 |
| 163 void GpuVideoEncodeAcceleratorHost::OnNotifyInputDone(int32 frame_id) { |
| 164 DVLOG(3) << "OnNotifyInputDone(): frame_id=" << frame_id; |
| 165 if (!frame_map_.erase(frame_id)) { |
| 166 DLOG(ERROR) << "OnNotifyInputDone(): " |
| 167 "invalid frame_id=" << frame_id; |
| 168 OnNotifyError(kPlatformFailureError); |
| 169 return; |
| 170 } |
| 171 } |
| 172 |
| 173 void GpuVideoEncodeAcceleratorHost::OnBitstreamBufferReady( |
| 174 int32 bitstream_buffer_id, |
| 175 uint32 payload_size, |
| 176 bool key_frame) { |
| 177 DVLOG(3) << "OnBitstreamBufferReady(): " |
| 178 "bitstream_buffer_id=" << bitstream_buffer_id |
| 179 << ", payload_size=" << payload_size |
| 180 << ", key_frame=" << key_frame; |
| 181 if (client_) |
| 182 client_->BitstreamBufferReady(bitstream_buffer_id, payload_size, key_frame); |
| 183 } |
| 184 |
| 185 void GpuVideoEncodeAcceleratorHost::OnNotifyError(Error error) { |
| 186 DVLOG(2) << "OnNotifyError(): error=" << error; |
| 187 if (client_) { |
| 188 client_->NotifyError(error); |
| 189 client_ = NULL; |
| 190 } |
| 191 } |
| 192 |
| 193 void GpuVideoEncodeAcceleratorHost::Send(IPC::Message* message) { |
| 194 if (!channel_) { |
| 195 DLOG(ERROR) << "Send(): no channel"; |
| 196 delete message; |
| 197 OnNotifyError(kPlatformFailureError); |
| 198 } else if (!channel_->Send(message)) { |
| 199 DLOG(ERROR) << "Send(): sending failed: message->type()=" |
| 200 << message->type(); |
| 201 OnNotifyError(kPlatformFailureError); |
| 202 } |
| 203 } |
| 204 |
| 205 } // namespace content |
| OLD | NEW |