| 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 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 int32 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 CreateEncoder(); |
| 104 if (!encoder_) { |
| 105 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnInitialize(): VEA creation " |
| 106 "failed"; |
| 107 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 108 return; |
| 109 } |
| 110 encoder_->Initialize( |
| 111 input_format, input_visible_size, output_profile, initial_bitrate); |
| 112 input_format_ = input_format; |
| 113 input_visible_size_ = input_visible_size; |
| 114 } |
| 115 |
| 116 void GpuVideoEncodeAccelerator::OnEncode(int32 frame_id, |
| 117 base::SharedMemoryHandle buffer_handle, |
| 118 uint32 buffer_size, |
| 119 bool force_keyframe) { |
| 120 DVLOG(3) << "GpuVideoEncodeAccelerator::OnEncode(): frame_id=" << frame_id |
| 121 << ", buffer_size=" << buffer_size |
| 122 << ", force_keyframe=" << force_keyframe; |
| 123 if (!encoder_) |
| 124 return; |
| 125 if (frame_id < 0) { |
| 126 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnEncode(): invalid frame_id=" |
| 127 << frame_id; |
| 128 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 129 return; |
| 130 } |
| 131 |
| 132 scoped_ptr<base::SharedMemory> shm( |
| 133 new base::SharedMemory(buffer_handle, true)); |
| 134 if (!shm->Map(buffer_size)) { |
| 135 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnEncode(): " |
| 136 "could not map frame_id=" << frame_id; |
| 137 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 138 return; |
| 139 } |
| 140 |
| 141 scoped_refptr<media::VideoFrame> frame; |
| 142 switch (input_format_) { |
| 143 case media::VideoFrame::I420: { |
| 144 if (buffer_size < |
| 145 static_cast<size_t>(input_coded_size_.GetArea() * 3 / 2)) { |
| 146 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnEncode(): " |
| 147 "buffer too small for frame_id=" << frame_id; |
| 148 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 149 return; |
| 150 } |
| 151 uint8* data = reinterpret_cast<uint8*>(shm->memory()); |
| 152 frame = media::VideoFrame::WrapExternalYuvData( |
| 153 media::VideoFrame::I420, |
| 154 input_coded_size_, |
| 155 gfx::Rect(input_visible_size_), |
| 156 input_visible_size_, |
| 157 input_coded_size_.width(), |
| 158 input_coded_size_.width() / 2, |
| 159 input_coded_size_.width() / 2, |
| 160 data, |
| 161 data + input_coded_size_.GetArea(), |
| 162 data + (input_coded_size_.GetArea() * 5 / 4), |
| 163 base::TimeDelta(), |
| 164 buffer_handle, |
| 165 // It's turtles all the way down... |
| 166 base::Bind(base::IgnoreResult(&base::MessageLoopProxy::PostTask), |
| 167 base::MessageLoopProxy::current(), |
| 168 FROM_HERE, |
| 169 base::Bind(&GpuVideoEncodeAccelerator::EncodeFrameFinished, |
| 170 weak_this_factory_.GetWeakPtr(), |
| 171 frame_id, |
| 172 base::Passed(&shm)))); |
| 173 break; |
| 174 } |
| 175 default: |
| 176 NOTREACHED(); |
| 177 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 178 return; |
| 179 } |
| 180 |
| 181 encoder_->Encode(frame, force_keyframe); |
| 182 } |
| 183 |
| 184 void GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer( |
| 185 int32 buffer_id, |
| 186 base::SharedMemoryHandle buffer_handle, |
| 187 uint32 buffer_size) { |
| 188 DVLOG(3) << "GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer(): " |
| 189 "buffer_id=" << buffer_id |
| 190 << ", buffer_size=" << buffer_size; |
| 191 if (!encoder_) |
| 192 return; |
| 193 if (buffer_id < 0) { |
| 194 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer(): " |
| 195 "invalid buffer_id=" << buffer_id; |
| 196 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 197 return; |
| 198 } |
| 199 if (buffer_size < output_buffer_size_) { |
| 200 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer(): " |
| 201 "buffer too small for buffer_id=" << buffer_id; |
| 202 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 203 return; |
| 204 } |
| 205 encoder_->UseOutputBitstreamBuffer( |
| 206 media::BitstreamBuffer(buffer_id, buffer_handle, buffer_size)); |
| 207 } |
| 208 |
| 209 void GpuVideoEncodeAccelerator::OnRequestEncodingParametersChange( |
| 210 int32 bitrate, |
| 211 uint32 framerate) { |
| 212 DVLOG(2) << "GpuVideoEncodeAccelerator::OnRequestEncodingParametersChange(): " |
| 213 "bitrate=" << bitrate |
| 214 << ", framerate=" << framerate; |
| 215 if (!encoder_) |
| 216 return; |
| 217 encoder_->RequestEncodingParametersChange(bitrate, framerate); |
| 218 } |
| 219 |
| 220 void GpuVideoEncodeAccelerator::EncodeFrameFinished( |
| 221 int32 frame_id, |
| 222 scoped_ptr<base::SharedMemory> shm) { |
| 223 Send(new AcceleratedVideoEncoderHostMsg_NotifyInputDone(route_id_, frame_id)); |
| 224 // Just let shm fall out of scope. |
| 225 } |
| 226 |
| 227 void GpuVideoEncodeAccelerator::Send(IPC::Message* message) { |
| 228 if (!channel_) { |
| 229 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Send(): no channel"; |
| 230 delete message; |
| 231 return; |
| 232 } else if (!channel_->Send(message)) { |
| 233 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Send(): sending failed: " |
| 234 "message->type()=" << message->type(); |
| 235 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 236 return; |
| 237 } |
| 238 } |
| 239 |
| 240 } // namespace content |
| OLD | NEW |