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/media/gpu_video_encode_accelerator.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/memory/shared_memory.h" |
| 10 #include "base/message_loop/message_loop_proxy.h" |
| 11 #include "content/common/gpu/gpu_channel.h" |
| 12 #include "content/common/gpu/gpu_messages.h" |
| 13 #include "ipc/ipc_message_macros.h" |
| 14 #include "media/base/video_frame.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 GpuVideoEncodeAccelerator::GpuVideoEncodeAccelerator(GpuChannel* gpu_channel, |
| 19 int32 route_id) |
| 20 : weak_this_factory_(this), |
| 21 channel_(gpu_channel), |
| 22 route_id_(route_id), |
| 23 input_format_(media::VideoFrame::INVALID), |
| 24 output_buffer_size_(0) {} |
| 25 |
| 26 GpuVideoEncodeAccelerator::~GpuVideoEncodeAccelerator() { |
| 27 if (encoder_) |
| 28 encoder_.release()->Destroy(); |
| 29 } |
| 30 |
| 31 bool GpuVideoEncodeAccelerator::OnMessageReceived(const IPC::Message& message) { |
| 32 bool handled = true; |
| 33 IPC_BEGIN_MESSAGE_MAP(GpuVideoEncodeAccelerator, message) |
| 34 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_Initialize, OnInitialize) |
| 35 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_Encode, OnEncode) |
| 36 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_UseOutputBitstreamBuffer, |
| 37 OnUseOutputBitstreamBuffer) |
| 38 IPC_MESSAGE_HANDLER( |
| 39 AcceleratedVideoEncoderMsg_RequestEncodingParametersChange, |
| 40 OnRequestEncodingParametersChange) |
| 41 IPC_MESSAGE_UNHANDLED(handled = false) |
| 42 IPC_END_MESSAGE_MAP() |
| 43 return handled; |
| 44 } |
| 45 |
| 46 void GpuVideoEncodeAccelerator::OnChannelError() { |
| 47 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 48 if (channel_) |
| 49 channel_ = NULL; |
| 50 } |
| 51 |
| 52 void GpuVideoEncodeAccelerator::NotifyInitializeDone() { |
| 53 Send(new AcceleratedVideoEncoderHostMsg_NotifyInitializeDone(route_id_)); |
| 54 } |
| 55 |
| 56 void GpuVideoEncodeAccelerator::RequireBitstreamBuffers( |
| 57 unsigned int input_count, |
| 58 const gfx::Size& input_coded_size, |
| 59 size_t output_buffer_size) { |
| 60 Send(new AcceleratedVideoEncoderHostMsg_RequireBitstreamBuffers( |
| 61 route_id_, input_count, input_coded_size, output_buffer_size)); |
| 62 input_coded_size_ = input_coded_size; |
| 63 output_buffer_size_ = output_buffer_size; |
| 64 } |
| 65 |
| 66 void GpuVideoEncodeAccelerator::BitstreamBufferReady(int32 bitstream_buffer_id, |
| 67 size_t payload_size, |
| 68 bool key_frame) { |
| 69 Send(new AcceleratedVideoEncoderHostMsg_BitstreamBufferReady( |
| 70 route_id_, bitstream_buffer_id, payload_size, key_frame)); |
| 71 } |
| 72 |
| 73 void GpuVideoEncodeAccelerator::NotifyError( |
| 74 media::VideoEncodeAccelerator::Error error) { |
| 75 Send(new AcceleratedVideoEncoderHostMsg_NotifyError(route_id_, error)); |
| 76 } |
| 77 |
| 78 // static |
| 79 std::vector<media::VideoEncodeAccelerator::SupportedProfile> |
| 80 GpuVideoEncodeAccelerator::GetSupportedProfiles() { |
| 81 std::vector<media::VideoEncodeAccelerator::SupportedProfile> profiles; |
| 82 |
| 83 // TODO(sheu): return platform-specific profiles. |
| 84 return profiles; |
| 85 } |
| 86 |
| 87 void GpuVideoEncodeAccelerator::CreateEncoder() { |
| 88 // TODO(sheu): actual create the encoder. |
| 89 } |
| 90 |
| 91 void GpuVideoEncodeAccelerator::OnInitialize( |
| 92 media::VideoFrame::Format input_format, |
| 93 const gfx::Size& input_visible_size, |
| 94 media::VideoCodecProfile output_profile, |
| 95 uint32 initial_bitrate) { |
| 96 DVLOG(2) << "GpuVideoEncodeAccelerator::OnInitialize(): " |
| 97 "input_format=" << input_format |
| 98 << ", input_visible_size=" << input_visible_size.ToString() |
| 99 << ", output_profile=" << output_profile |
| 100 << ", initial_bitrate=" << initial_bitrate; |
| 101 DCHECK(!encoder_); |
| 102 |
| 103 if (input_visible_size.width() > kint32max / input_visible_size.height()) { |
| 104 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnInitialize(): " |
| 105 "input_visible_size too large"; |
| 106 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 107 return; |
| 108 } |
| 109 |
| 110 CreateEncoder(); |
| 111 if (!encoder_) { |
| 112 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnInitialize(): VEA creation " |
| 113 "failed"; |
| 114 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 115 return; |
| 116 } |
| 117 encoder_->Initialize( |
| 118 input_format, input_visible_size, output_profile, initial_bitrate); |
| 119 input_format_ = input_format; |
| 120 input_visible_size_ = input_visible_size; |
| 121 } |
| 122 |
| 123 void GpuVideoEncodeAccelerator::OnEncode(int32 frame_id, |
| 124 base::SharedMemoryHandle buffer_handle, |
| 125 uint32 buffer_size, |
| 126 bool force_keyframe) { |
| 127 DVLOG(3) << "GpuVideoEncodeAccelerator::OnEncode(): frame_id=" << frame_id |
| 128 << ", buffer_size=" << buffer_size |
| 129 << ", force_keyframe=" << force_keyframe; |
| 130 if (!encoder_) |
| 131 return; |
| 132 if (frame_id < 0) { |
| 133 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnEncode(): invalid frame_id=" |
| 134 << frame_id; |
| 135 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 136 return; |
| 137 } |
| 138 |
| 139 scoped_ptr<base::SharedMemory> shm( |
| 140 new base::SharedMemory(buffer_handle, true)); |
| 141 if (!shm->Map(buffer_size)) { |
| 142 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnEncode(): " |
| 143 "could not map frame_id=" << frame_id; |
| 144 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 145 return; |
| 146 } |
| 147 |
| 148 scoped_refptr<media::VideoFrame> frame = |
| 149 media::VideoFrame::WrapExternalSharedMemory( |
| 150 media::VideoFrame::I420, |
| 151 input_coded_size_, |
| 152 gfx::Rect(input_visible_size_), |
| 153 input_visible_size_, |
| 154 reinterpret_cast<uint8*>(shm->memory()), |
| 155 buffer_handle, |
| 156 base::TimeDelta(), |
| 157 // It's turtles all the way down... |
| 158 base::Bind(base::IgnoreResult(&base::MessageLoopProxy::PostTask), |
| 159 base::MessageLoopProxy::current(), |
| 160 FROM_HERE, |
| 161 base::Bind(&GpuVideoEncodeAccelerator::EncodeFrameFinished, |
| 162 weak_this_factory_.GetWeakPtr(), |
| 163 frame_id, |
| 164 base::Passed(&shm)))); |
| 165 |
| 166 if (!frame) { |
| 167 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnEncode(): " |
| 168 "could not create VideoFrame for frame_id=" << frame_id; |
| 169 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 170 return; |
| 171 } |
| 172 |
| 173 encoder_->Encode(frame, force_keyframe); |
| 174 } |
| 175 |
| 176 void GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer( |
| 177 int32 buffer_id, |
| 178 base::SharedMemoryHandle buffer_handle, |
| 179 uint32 buffer_size) { |
| 180 DVLOG(3) << "GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer(): " |
| 181 "buffer_id=" << buffer_id |
| 182 << ", buffer_size=" << buffer_size; |
| 183 if (!encoder_) |
| 184 return; |
| 185 if (buffer_id < 0) { |
| 186 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer(): " |
| 187 "invalid buffer_id=" << buffer_id; |
| 188 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 189 return; |
| 190 } |
| 191 if (buffer_size < output_buffer_size_) { |
| 192 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer(): " |
| 193 "buffer too small for buffer_id=" << buffer_id; |
| 194 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 195 return; |
| 196 } |
| 197 encoder_->UseOutputBitstreamBuffer( |
| 198 media::BitstreamBuffer(buffer_id, buffer_handle, buffer_size)); |
| 199 } |
| 200 |
| 201 void GpuVideoEncodeAccelerator::OnRequestEncodingParametersChange( |
| 202 uint32 bitrate, |
| 203 uint32 framerate) { |
| 204 DVLOG(2) << "GpuVideoEncodeAccelerator::OnRequestEncodingParametersChange(): " |
| 205 "bitrate=" << bitrate |
| 206 << ", framerate=" << framerate; |
| 207 if (!encoder_) |
| 208 return; |
| 209 encoder_->RequestEncodingParametersChange(bitrate, framerate); |
| 210 } |
| 211 |
| 212 void GpuVideoEncodeAccelerator::EncodeFrameFinished( |
| 213 int32 frame_id, |
| 214 scoped_ptr<base::SharedMemory> shm) { |
| 215 Send(new AcceleratedVideoEncoderHostMsg_NotifyInputDone(route_id_, frame_id)); |
| 216 // Just let shm fall out of scope. |
| 217 } |
| 218 |
| 219 void GpuVideoEncodeAccelerator::Send(IPC::Message* message) { |
| 220 if (!channel_) { |
| 221 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Send(): no channel"; |
| 222 delete message; |
| 223 return; |
| 224 } else if (!channel_->Send(message)) { |
| 225 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Send(): sending failed: " |
| 226 "message->type()=" << message->type(); |
| 227 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 228 return; |
| 229 } |
| 230 } |
| 231 |
| 232 } // namespace content |
OLD | NEW |