Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 "chrome/gpu/gpu_arc_video_encode_accelerator.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/sys_info.h" | |
| 11 #include "media/base/video_types.h" | |
| 12 #include "media/gpu/gpu_video_encode_accelerator_factory.h" | |
| 13 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 14 #include "mojo/public/cpp/bindings/type_converter.h" | |
| 15 #include "mojo/public/cpp/system/platform_handle.h" | |
| 16 | |
| 17 namespace chromeos { | |
| 18 namespace arc { | |
| 19 | |
| 20 GpuArcVideoEncodeAccelerator::GpuArcVideoEncodeAccelerator( | |
| 21 const gpu::GpuPreferences& gpu_preferences) | |
| 22 : gpu_preferences_(gpu_preferences) {} | |
| 23 | |
| 24 GpuArcVideoEncodeAccelerator::~GpuArcVideoEncodeAccelerator() = default; | |
| 25 | |
| 26 // VideoEncodeAccelerator::Client implementation. | |
| 27 void GpuArcVideoEncodeAccelerator::RequireBitstreamBuffers( | |
| 28 unsigned int input_count, | |
| 29 const gfx::Size& coded_size, | |
| 30 size_t output_buffer_size) { | |
| 31 DVLOG(2) << "RequireBitstreamBuffers(input_count=" << input_count | |
|
Pawel Osciak
2017/06/14 03:57:38
Not sure about your preference, but perhaps a macr
Owen Lin
2017/06/14 09:13:59
That's nice.
| |
| 32 << ", coded_size=" << coded_size.ToString() | |
| 33 << ", output_buffer_size=" << output_buffer_size << ")"; | |
| 34 DCHECK(client_); | |
| 35 coded_size_ = coded_size; | |
| 36 client_->RequireBitstreamBuffers(input_count, coded_size, output_buffer_size); | |
| 37 } | |
| 38 | |
| 39 void GpuArcVideoEncodeAccelerator::BitstreamBufferReady( | |
| 40 int32_t bitstream_buffer_id, | |
| 41 size_t payload_size, | |
| 42 bool key_frame, | |
| 43 base::TimeDelta timestamp) { | |
| 44 DVLOG(2) << "BitstreamBufferReady(id=" << bitstream_buffer_id << ")"; | |
| 45 DCHECK(client_); | |
| 46 client_->BitstreamBufferReady(bitstream_buffer_id, payload_size, key_frame, | |
| 47 timestamp.InMicroseconds()); | |
| 48 } | |
| 49 | |
| 50 void GpuArcVideoEncodeAccelerator::NotifyError(Error error) { | |
| 51 DVLOG(2) << "NotifyError(" << error << ")"; | |
| 52 DCHECK(client_); | |
| 53 client_->NotifyError(error); | |
| 54 } | |
| 55 | |
| 56 // ::arc::mojom::VideoEncodeAccelerator implementation. | |
| 57 void GpuArcVideoEncodeAccelerator::GetSupportedProfiles( | |
| 58 const GetSupportedProfilesCallback& callback) { | |
| 59 callback.Run(media::GpuVideoEncodeAcceleratorFactory::GetSupportedProfiles( | |
| 60 gpu_preferences_)); | |
| 61 } | |
| 62 | |
| 63 void GpuArcVideoEncodeAccelerator::Initialize( | |
| 64 VideoPixelFormat input_format, | |
| 65 const gfx::Size& visible_size, | |
| 66 VideoEncodeAccelerator::StorageType input_storage, | |
| 67 VideoCodecProfile output_profile, | |
| 68 uint32_t initial_bitrate, | |
| 69 VideoEncodeClientPtr client, | |
| 70 const InitializeCallback& callback) { | |
| 71 DVLOG(2) << "Initialize()"; | |
| 72 | |
| 73 if (!client) { | |
| 74 DLOG(ERROR) << "Invalid client"; | |
|
Pawel Osciak
2017/06/14 03:57:38
Perhaps LOG(ERROR)
Owen Lin
2017/06/14 09:13:59
Done.
| |
| 75 callback.Run(false); | |
| 76 return; | |
| 77 } | |
| 78 | |
| 79 input_pixel_format_ = static_cast<media::VideoPixelFormat>(input_format); | |
| 80 visible_size_ = visible_size; | |
| 81 accelerator_ = media::GpuVideoEncodeAcceleratorFactory::CreateVEA( | |
| 82 input_pixel_format_, visible_size_, | |
| 83 static_cast<media::VideoCodecProfile>(output_profile), initial_bitrate, | |
| 84 this, gpu_preferences_); | |
| 85 if (accelerator_ == nullptr) { | |
| 86 DLOG(ERROR) << "failed to create Accelerator."; | |
|
Pawel Osciak
2017/06/14 03:57:38
Failed to create a VideoEncodeAccelerator.
Owen Lin
2017/06/14 09:13:59
Done.
| |
| 87 callback.Run(false); | |
| 88 return; | |
| 89 } | |
| 90 client_ = std::move(client); | |
| 91 callback.Run(true); | |
| 92 } | |
| 93 | |
| 94 static void DropSharedMemory(std::unique_ptr<base::SharedMemory> shm) { | |
| 95 // Just let |shm| fall out of scope. | |
| 96 } | |
| 97 | |
| 98 void GpuArcVideoEncodeAccelerator::Encode( | |
| 99 mojo::ScopedHandle handle, | |
| 100 std::vector<::arc::VideoFramePlane> planes, | |
|
Pawel Osciak
2017/06/14 03:57:38
Could this be a const&?
Owen Lin
2017/06/14 09:13:59
The interface is defined by mojom. It is not const
| |
| 101 int64_t timestamp, | |
| 102 bool force_keyframe) { | |
| 103 DVLOG(2) << "Encode(timestamp=" << timestamp << ")"; | |
| 104 if (!accelerator_) { | |
| 105 DLOG(ERROR) << "Accelerator not initialized"; | |
| 106 return; | |
| 107 } | |
| 108 | |
| 109 if (planes.empty()) { // EOS | |
| 110 accelerator_->Encode(media::VideoFrame::CreateEOSFrame(), force_keyframe); | |
| 111 return; | |
| 112 } | |
| 113 | |
| 114 base::ScopedFD fd = UnwrapFdFromMojoHandle(std::move(handle)); | |
| 115 if (!fd.is_valid()) | |
| 116 return; | |
| 117 | |
| 118 size_t allocation_size = | |
| 119 media::VideoFrame::AllocationSize(input_pixel_format_, coded_size_); | |
| 120 | |
| 121 // TODO(rockot): Pass GUIDs through Mojo. https://crbug.com/713763. | |
| 122 // TODO(rockot): This fd comes from a mojo::ScopedHandle in | |
| 123 // GpuArcVideoService::BindSharedMemory. That should be passed through, | |
| 124 // rather than pulling out the fd. https://crbug.com/713763. | |
| 125 // TODO(rockot): Pass through a real size rather than |0|. | |
| 126 base::UnguessableToken guid = base::UnguessableToken::Create(); | |
| 127 base::SharedMemoryHandle shm_handle(base::FileDescriptor(fd.release(), true), | |
| 128 0u, guid); | |
| 129 auto shm = base::MakeUnique<base::SharedMemory>(shm_handle, true); | |
| 130 | |
| 131 base::CheckedNumeric<off_t> map_offset = planes[0].offset; | |
| 132 base::CheckedNumeric<size_t> map_size = allocation_size; | |
| 133 const uint32_t aligned_offset = | |
| 134 planes[0].offset % base::SysInfo::VMAllocationGranularity(); | |
| 135 map_offset -= aligned_offset; | |
| 136 map_size += aligned_offset; | |
| 137 | |
| 138 if (!map_offset.IsValid() || !map_size.IsValid()) { | |
| 139 DLOG(ERROR) << "Invalid map_offset or map_size"; | |
| 140 client_->NotifyError(Error::kInvalidArgumentError); | |
| 141 return; | |
| 142 } | |
| 143 if (!shm->MapAt(map_offset.ValueOrDie(), map_size.ValueOrDie())) { | |
| 144 DLOG(ERROR) << "Could not map memory"; | |
| 145 client_->NotifyError(Error::kPlatformFailureError); | |
| 146 return; | |
| 147 } | |
| 148 | |
| 149 uint8_t* shm_memory = reinterpret_cast<uint8_t*>(shm->memory()); | |
| 150 auto frame = media::VideoFrame::WrapExternalSharedMemory( | |
| 151 input_pixel_format_, coded_size_, gfx::Rect(visible_size_), visible_size_, | |
| 152 shm_memory + aligned_offset, allocation_size, shm_handle, | |
| 153 planes[0].offset, base::TimeDelta::FromMicroseconds(timestamp)); | |
| 154 | |
| 155 // Wrap |shm| in a callback and add it as a destruction observer, so it | |
| 156 // stays alive and mapped until |frame| goes out of scope. | |
| 157 frame->AddDestructionObserver( | |
| 158 base::Bind(&DropSharedMemory, base::Passed(&shm))); | |
| 159 accelerator_->Encode(frame, force_keyframe); | |
| 160 } | |
| 161 | |
| 162 void GpuArcVideoEncodeAccelerator::UseOutputBitstreamBuffer( | |
| 163 int32_t bitstream_buffer_id, | |
| 164 mojo::ScopedHandle shmem_fd, | |
| 165 uint32_t offset, | |
| 166 uint32_t size) { | |
| 167 DVLOG(2) << "UseOutputBitstreamBuffer(id=" << bitstream_buffer_id << ")"; | |
| 168 if (!accelerator_) { | |
| 169 DLOG(ERROR) << "Accelerator not initialized"; | |
| 170 return; | |
| 171 } | |
| 172 | |
| 173 base::ScopedFD fd = UnwrapFdFromMojoHandle(std::move(shmem_fd)); | |
| 174 if (!fd.is_valid()) | |
| 175 return; | |
| 176 | |
| 177 // TODO(rockot): Pass GUIDs through Mojo. https://crbug.com/713763. | |
| 178 // TODO(rockot): This fd comes from a mojo::ScopedHandle in | |
| 179 // GpuArcVideoService::BindSharedMemory. That should be passed through, | |
| 180 // rather than pulling out the fd. https://crbug.com/713763. | |
| 181 // TODO(rockot): Pass through a real size rather than |0|. | |
| 182 base::UnguessableToken guid = base::UnguessableToken::Create(); | |
| 183 base::SharedMemoryHandle shm_handle(base::FileDescriptor(fd.release(), true), | |
| 184 0u, guid); | |
| 185 accelerator_->UseOutputBitstreamBuffer( | |
| 186 media::BitstreamBuffer(bitstream_buffer_id, shm_handle, size, offset)); | |
| 187 } | |
| 188 | |
| 189 void GpuArcVideoEncodeAccelerator::RequestEncodingParametersChange( | |
| 190 uint32_t bitrate, | |
| 191 uint32_t framerate) { | |
| 192 DVLOG(2) << "RequestEncodingParametersChange(" << bitrate << ", " << framerate | |
| 193 << ")"; | |
| 194 if (!accelerator_) { | |
| 195 DLOG(ERROR) << "Accelerator not initialized"; | |
| 196 return; | |
| 197 } | |
| 198 accelerator_->RequestEncodingParametersChange(bitrate, framerate); | |
| 199 } | |
| 200 | |
| 201 base::ScopedFD GpuArcVideoEncodeAccelerator::UnwrapFdFromMojoHandle( | |
| 202 mojo::ScopedHandle handle) { | |
| 203 DCHECK(client_); | |
| 204 if (!handle.is_valid()) { | |
| 205 DLOG(ERROR) << "handle is invalid"; | |
| 206 client_->NotifyError(Error::kInvalidArgumentError); | |
| 207 return base::ScopedFD(); | |
| 208 } | |
| 209 | |
| 210 base::PlatformFile platform_file; | |
| 211 MojoResult mojo_result = | |
| 212 mojo::UnwrapPlatformFile(std::move(handle), &platform_file); | |
| 213 if (mojo_result != MOJO_RESULT_OK) { | |
| 214 DLOG(ERROR) << "UnwrapPlatformFile failed: " << mojo_result; | |
| 215 client_->NotifyError(Error::kPlatformFailureError); | |
| 216 return base::ScopedFD(); | |
| 217 } | |
| 218 | |
| 219 return base::ScopedFD(platform_file); | |
| 220 } | |
| 221 | |
| 222 } // namespace arc | |
| 223 } // namespace chromeos | |
| OLD | NEW |