| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/renderer_host/media/video_capture_gpu_jpeg_decoder.h" | 5 #include "content/browser/renderer_host/media/video_capture_gpu_jpeg_decoder.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "base/trace_event/trace_event.h" | 16 #include "base/trace_event/trace_event.h" |
| 17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 18 #include "content/browser/gpu/browser_gpu_channel_host_factory.h" | 18 #include "content/browser/gpu/browser_gpu_channel_host_factory.h" |
| 19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/common/content_switches.h" | 20 #include "content/public/common/content_switches.h" |
| 21 #include "media/base/video_frame.h" | 21 #include "media/base/video_frame.h" |
| 22 #include "media/gpu/ipc/client/gpu_jpeg_decode_accelerator_host.h" | 22 #include "media/gpu/ipc/client/gpu_jpeg_decode_accelerator_host.h" |
| 23 #include "mojo/public/cpp/system/platform_handle.h" | |
| 24 | 23 |
| 25 namespace content { | 24 namespace content { |
| 26 | 25 |
| 27 VideoCaptureGpuJpegDecoder::VideoCaptureGpuJpegDecoder( | 26 VideoCaptureGpuJpegDecoder::VideoCaptureGpuJpegDecoder( |
| 28 const DecodeDoneCB& decode_done_cb) | 27 const DecodeDoneCB& decode_done_cb) |
| 29 : decode_done_cb_(decode_done_cb), | 28 : decode_done_cb_(decode_done_cb), |
| 30 next_bitstream_buffer_id_(0), | 29 next_bitstream_buffer_id_(0), |
| 31 in_buffer_id_(media::JpegDecodeAccelerator::kInvalidBitstreamBufferId), | 30 in_buffer_id_(media::JpegDecodeAccelerator::kInvalidBitstreamBufferId), |
| 32 decoder_status_(INIT_PENDING) {} | 31 decoder_status_(INIT_PENDING) {} |
| 33 | 32 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 base::AutoLock lock(lock_); | 74 base::AutoLock lock(lock_); |
| 76 return decoder_status_; | 75 return decoder_status_; |
| 77 } | 76 } |
| 78 | 77 |
| 79 void VideoCaptureGpuJpegDecoder::DecodeCapturedData( | 78 void VideoCaptureGpuJpegDecoder::DecodeCapturedData( |
| 80 const uint8_t* data, | 79 const uint8_t* data, |
| 81 size_t in_buffer_size, | 80 size_t in_buffer_size, |
| 82 const media::VideoCaptureFormat& frame_format, | 81 const media::VideoCaptureFormat& frame_format, |
| 83 base::TimeTicks reference_time, | 82 base::TimeTicks reference_time, |
| 84 base::TimeDelta timestamp, | 83 base::TimeDelta timestamp, |
| 85 media::VideoCaptureDevice::Client::Buffer out_buffer) { | 84 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> out_buffer) { |
| 86 DCHECK(CalledOnValidThread()); | 85 DCHECK(CalledOnValidThread()); |
| 87 DCHECK(decoder_); | 86 DCHECK(decoder_); |
| 88 | 87 |
| 89 TRACE_EVENT_ASYNC_BEGIN0("jpeg", "VideoCaptureGpuJpegDecoder decoding", | 88 TRACE_EVENT_ASYNC_BEGIN0("jpeg", "VideoCaptureGpuJpegDecoder decoding", |
| 90 next_bitstream_buffer_id_); | 89 next_bitstream_buffer_id_); |
| 91 TRACE_EVENT0("jpeg", "VideoCaptureGpuJpegDecoder::DecodeCapturedData"); | 90 TRACE_EVENT0("jpeg", "VideoCaptureGpuJpegDecoder::DecodeCapturedData"); |
| 92 | 91 |
| 93 // TODO(kcwu): enqueue decode requests in case decoding is not fast enough | 92 // TODO(kcwu): enqueue decode requests in case decoding is not fast enough |
| 94 // (say, if decoding time is longer than 16ms for 60fps 4k image) | 93 // (say, if decoding time is longer than 16ms for 60fps 4k image) |
| 95 { | 94 { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 115 } | 114 } |
| 116 memcpy(in_shared_memory_->memory(), data, in_buffer_size); | 115 memcpy(in_shared_memory_->memory(), data, in_buffer_size); |
| 117 | 116 |
| 118 // No need to lock for |in_buffer_id_| since IsDecoding_Locked() is false. | 117 // No need to lock for |in_buffer_id_| since IsDecoding_Locked() is false. |
| 119 in_buffer_id_ = next_bitstream_buffer_id_; | 118 in_buffer_id_ = next_bitstream_buffer_id_; |
| 120 media::BitstreamBuffer in_buffer(in_buffer_id_, in_shared_memory_->handle(), | 119 media::BitstreamBuffer in_buffer(in_buffer_id_, in_shared_memory_->handle(), |
| 121 in_buffer_size); | 120 in_buffer_size); |
| 122 // Mask against 30 bits, to avoid (undefined) wraparound on signed integer. | 121 // Mask against 30 bits, to avoid (undefined) wraparound on signed integer. |
| 123 next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & 0x3FFFFFFF; | 122 next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & 0x3FFFFFFF; |
| 124 | 123 |
| 125 // The APIs of |decoder_| and |decode_done_cb_| require us to wrap the | 124 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 126 // |out_buffer| in a VideoFrame. | |
| 127 const gfx::Size dimensions = frame_format.frame_size; | 125 const gfx::Size dimensions = frame_format.frame_size; |
| 128 std::unique_ptr<media::VideoCaptureBufferHandle> out_buffer_access = | 126 base::SharedMemoryHandle out_handle = out_buffer->AsPlatformFile(); |
| 129 out_buffer.handle_provider()->GetHandleForInProcessAccess(); | |
| 130 base::SharedMemoryHandle memory_handle; | |
| 131 size_t memory_size = 0; | |
| 132 bool read_only_flag = false; | |
| 133 const MojoResult result = mojo::UnwrapSharedMemoryHandle( | |
| 134 out_buffer.handle_provider()->GetHandleForInterProcessTransit(), | |
| 135 &memory_handle, &memory_size, &read_only_flag); | |
| 136 DCHECK_EQ(MOJO_RESULT_OK, result); | |
| 137 DCHECK_GT(memory_size, 0u); | |
| 138 scoped_refptr<media::VideoFrame> out_frame = | 127 scoped_refptr<media::VideoFrame> out_frame = |
| 139 media::VideoFrame::WrapExternalSharedMemory( | 128 media::VideoFrame::WrapExternalSharedMemory( |
| 140 media::PIXEL_FORMAT_I420, // format | 129 media::PIXEL_FORMAT_I420, // format |
| 141 dimensions, // coded_size | 130 dimensions, // coded_size |
| 142 gfx::Rect(dimensions), // visible_rect | 131 gfx::Rect(dimensions), // visible_rect |
| 143 dimensions, // natural_size | 132 dimensions, // natural_size |
| 144 out_buffer_access->data(), // data | 133 static_cast<uint8_t*>(out_buffer->data()), // data |
| 145 out_buffer_access->mapped_size(), // data_size | 134 out_buffer->mapped_size(), // data_size |
| 146 std::move(memory_handle), // handle | 135 out_handle, // handle |
| 147 0, // shared_memory_offset | 136 0, // shared_memory_offset |
| 148 timestamp); // timestamp | 137 timestamp); // timestamp |
| 149 if (!out_frame) { | 138 if (!out_frame) { |
| 150 base::AutoLock lock(lock_); | 139 base::AutoLock lock(lock_); |
| 151 decoder_status_ = FAILED; | 140 decoder_status_ = FAILED; |
| 152 LOG(ERROR) << "DecodeCapturedData: WrapExternalSharedMemory failed"; | 141 LOG(ERROR) << "DecodeCapturedData: WrapExternalSharedMemory failed"; |
| 153 return; | 142 return; |
| 154 } | 143 } |
| 155 out_frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, | 144 out_frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, |
| 156 frame_format.frame_rate); | 145 frame_format.frame_rate); |
| 157 | 146 |
| 158 out_frame->metadata()->SetTimeTicks(media::VideoFrameMetadata::REFERENCE_TIME, | 147 out_frame->metadata()->SetTimeTicks(media::VideoFrameMetadata::REFERENCE_TIME, |
| 159 reference_time); | 148 reference_time); |
| 160 | 149 |
| 161 { | 150 { |
| 162 base::AutoLock lock(lock_); | 151 base::AutoLock lock(lock_); |
| 163 decode_done_closure_ = | 152 decode_done_closure_ = |
| 164 base::Bind(decode_done_cb_, base::Passed(&out_buffer), out_frame); | 153 base::Bind(decode_done_cb_, base::Passed(&out_buffer), out_frame); |
| 165 } | 154 } |
| 166 decoder_->Decode(in_buffer, std::move(out_frame)); | 155 decoder_->Decode(in_buffer, std::move(out_frame)); |
| 156 #else |
| 157 NOTREACHED(); |
| 158 #endif |
| 167 } | 159 } |
| 168 | 160 |
| 169 void VideoCaptureGpuJpegDecoder::VideoFrameReady(int32_t bitstream_buffer_id) { | 161 void VideoCaptureGpuJpegDecoder::VideoFrameReady(int32_t bitstream_buffer_id) { |
| 170 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 162 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 171 TRACE_EVENT0("jpeg", "VideoCaptureGpuJpegDecoder::VideoFrameReady"); | 163 TRACE_EVENT0("jpeg", "VideoCaptureGpuJpegDecoder::VideoFrameReady"); |
| 172 base::AutoLock lock(lock_); | 164 base::AutoLock lock(lock_); |
| 173 | 165 |
| 174 if (!IsDecoding_Locked()) { | 166 if (!IsDecoding_Locked()) { |
| 175 LOG(ERROR) << "Got decode response while not decoding"; | 167 LOG(ERROR) << "Got decode response while not decoding"; |
| 176 return; | 168 return; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 lock_.AssertAcquired(); | 250 lock_.AssertAcquired(); |
| 259 return !decode_done_closure_.is_null(); | 251 return !decode_done_closure_.is_null(); |
| 260 } | 252 } |
| 261 | 253 |
| 262 void VideoCaptureGpuJpegDecoder::RecordInitDecodeUMA_Locked() { | 254 void VideoCaptureGpuJpegDecoder::RecordInitDecodeUMA_Locked() { |
| 263 UMA_HISTOGRAM_BOOLEAN("Media.VideoCaptureGpuJpegDecoder.InitDecodeSuccess", | 255 UMA_HISTOGRAM_BOOLEAN("Media.VideoCaptureGpuJpegDecoder.InitDecodeSuccess", |
| 264 decoder_status_ == INIT_PASSED); | 256 decoder_status_ == INIT_PASSED); |
| 265 } | 257 } |
| 266 | 258 |
| 267 } // namespace content | 259 } // namespace content |
| OLD | NEW |