| 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" |
| 23 | 24 |
| 24 namespace content { | 25 namespace content { |
| 25 | 26 |
| 26 VideoCaptureGpuJpegDecoder::VideoCaptureGpuJpegDecoder( | 27 VideoCaptureGpuJpegDecoder::VideoCaptureGpuJpegDecoder( |
| 27 const DecodeDoneCB& decode_done_cb) | 28 const DecodeDoneCB& decode_done_cb) |
| 28 : decode_done_cb_(decode_done_cb), | 29 : decode_done_cb_(decode_done_cb), |
| 29 next_bitstream_buffer_id_(0), | 30 next_bitstream_buffer_id_(0), |
| 30 in_buffer_id_(media::JpegDecodeAccelerator::kInvalidBitstreamBufferId), | 31 in_buffer_id_(media::JpegDecodeAccelerator::kInvalidBitstreamBufferId), |
| 31 decoder_status_(INIT_PENDING) {} | 32 decoder_status_(INIT_PENDING) {} |
| 32 | 33 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 base::AutoLock lock(lock_); | 75 base::AutoLock lock(lock_); |
| 75 return decoder_status_; | 76 return decoder_status_; |
| 76 } | 77 } |
| 77 | 78 |
| 78 void VideoCaptureGpuJpegDecoder::DecodeCapturedData( | 79 void VideoCaptureGpuJpegDecoder::DecodeCapturedData( |
| 79 const uint8_t* data, | 80 const uint8_t* data, |
| 80 size_t in_buffer_size, | 81 size_t in_buffer_size, |
| 81 const media::VideoCaptureFormat& frame_format, | 82 const media::VideoCaptureFormat& frame_format, |
| 82 base::TimeTicks reference_time, | 83 base::TimeTicks reference_time, |
| 83 base::TimeDelta timestamp, | 84 base::TimeDelta timestamp, |
| 84 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> out_buffer) { | 85 media::VideoCaptureDevice::Client::Buffer out_buffer) { |
| 85 DCHECK(CalledOnValidThread()); | 86 DCHECK(CalledOnValidThread()); |
| 86 DCHECK(decoder_); | 87 DCHECK(decoder_); |
| 87 | 88 |
| 88 TRACE_EVENT_ASYNC_BEGIN0("jpeg", "VideoCaptureGpuJpegDecoder decoding", | 89 TRACE_EVENT_ASYNC_BEGIN0("jpeg", "VideoCaptureGpuJpegDecoder decoding", |
| 89 next_bitstream_buffer_id_); | 90 next_bitstream_buffer_id_); |
| 90 TRACE_EVENT0("jpeg", "VideoCaptureGpuJpegDecoder::DecodeCapturedData"); | 91 TRACE_EVENT0("jpeg", "VideoCaptureGpuJpegDecoder::DecodeCapturedData"); |
| 91 | 92 |
| 92 // TODO(kcwu): enqueue decode requests in case decoding is not fast enough | 93 // TODO(kcwu): enqueue decode requests in case decoding is not fast enough |
| 93 // (say, if decoding time is longer than 16ms for 60fps 4k image) | 94 // (say, if decoding time is longer than 16ms for 60fps 4k image) |
| 94 { | 95 { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 114 } | 115 } |
| 115 memcpy(in_shared_memory_->memory(), data, in_buffer_size); | 116 memcpy(in_shared_memory_->memory(), data, in_buffer_size); |
| 116 | 117 |
| 117 // No need to lock for |in_buffer_id_| since IsDecoding_Locked() is false. | 118 // No need to lock for |in_buffer_id_| since IsDecoding_Locked() is false. |
| 118 in_buffer_id_ = next_bitstream_buffer_id_; | 119 in_buffer_id_ = next_bitstream_buffer_id_; |
| 119 media::BitstreamBuffer in_buffer(in_buffer_id_, in_shared_memory_->handle(), | 120 media::BitstreamBuffer in_buffer(in_buffer_id_, in_shared_memory_->handle(), |
| 120 in_buffer_size); | 121 in_buffer_size); |
| 121 // Mask against 30 bits, to avoid (undefined) wraparound on signed integer. | 122 // Mask against 30 bits, to avoid (undefined) wraparound on signed integer. |
| 122 next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & 0x3FFFFFFF; | 123 next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & 0x3FFFFFFF; |
| 123 | 124 |
| 124 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 125 // The APIs of |decoder_| and |decode_done_cb_| require us to wrap the |
| 126 // |out_buffer| in a VideoFrame. |
| 125 const gfx::Size dimensions = frame_format.frame_size; | 127 const gfx::Size dimensions = frame_format.frame_size; |
| 126 base::SharedMemoryHandle out_handle = out_buffer->AsPlatformFile(); | 128 std::unique_ptr<media::VideoCaptureBufferHandle> out_buffer_access = |
| 129 out_buffer.handle_provider()->GetHandleForInProcessAccess(); |
| 130 base::SharedMemoryHandle out_handle = |
| 131 out_buffer.handle_provider()->GetNonOwnedSharedMemoryHandleForLegacyIPC(); |
| 127 scoped_refptr<media::VideoFrame> out_frame = | 132 scoped_refptr<media::VideoFrame> out_frame = |
| 128 media::VideoFrame::WrapExternalSharedMemory( | 133 media::VideoFrame::WrapExternalSharedMemory( |
| 129 media::PIXEL_FORMAT_I420, // format | 134 media::PIXEL_FORMAT_I420, // format |
| 130 dimensions, // coded_size | 135 dimensions, // coded_size |
| 131 gfx::Rect(dimensions), // visible_rect | 136 gfx::Rect(dimensions), // visible_rect |
| 132 dimensions, // natural_size | 137 dimensions, // natural_size |
| 133 static_cast<uint8_t*>(out_buffer->data()), // data | 138 out_buffer_access->data(), // data |
| 134 out_buffer->mapped_size(), // data_size | 139 out_buffer_access->mapped_size(), // data_size |
| 135 out_handle, // handle | 140 out_handle, // handle |
| 136 0, // shared_memory_offset | 141 0, // shared_memory_offset |
| 137 timestamp); // timestamp | 142 timestamp); // timestamp |
| 138 if (!out_frame) { | 143 if (!out_frame) { |
| 139 base::AutoLock lock(lock_); | 144 base::AutoLock lock(lock_); |
| 140 decoder_status_ = FAILED; | 145 decoder_status_ = FAILED; |
| 141 LOG(ERROR) << "DecodeCapturedData: WrapExternalSharedMemory failed"; | 146 LOG(ERROR) << "DecodeCapturedData: WrapExternalSharedMemory failed"; |
| 142 return; | 147 return; |
| 143 } | 148 } |
| 144 out_frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, | 149 out_frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, |
| 145 frame_format.frame_rate); | 150 frame_format.frame_rate); |
| 146 | 151 |
| 147 out_frame->metadata()->SetTimeTicks(media::VideoFrameMetadata::REFERENCE_TIME, | 152 out_frame->metadata()->SetTimeTicks(media::VideoFrameMetadata::REFERENCE_TIME, |
| 148 reference_time); | 153 reference_time); |
| 149 | 154 |
| 150 { | 155 { |
| 151 base::AutoLock lock(lock_); | 156 base::AutoLock lock(lock_); |
| 152 decode_done_closure_ = | 157 decode_done_closure_ = |
| 153 base::Bind(decode_done_cb_, base::Passed(&out_buffer), out_frame); | 158 base::Bind(decode_done_cb_, base::Passed(&out_buffer), out_frame); |
| 154 } | 159 } |
| 155 decoder_->Decode(in_buffer, std::move(out_frame)); | 160 decoder_->Decode(in_buffer, std::move(out_frame)); |
| 156 #else | |
| 157 NOTREACHED(); | |
| 158 #endif | |
| 159 } | 161 } |
| 160 | 162 |
| 161 void VideoCaptureGpuJpegDecoder::VideoFrameReady(int32_t bitstream_buffer_id) { | 163 void VideoCaptureGpuJpegDecoder::VideoFrameReady(int32_t bitstream_buffer_id) { |
| 162 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 164 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 163 TRACE_EVENT0("jpeg", "VideoCaptureGpuJpegDecoder::VideoFrameReady"); | 165 TRACE_EVENT0("jpeg", "VideoCaptureGpuJpegDecoder::VideoFrameReady"); |
| 164 base::AutoLock lock(lock_); | 166 base::AutoLock lock(lock_); |
| 165 | 167 |
| 166 if (!IsDecoding_Locked()) { | 168 if (!IsDecoding_Locked()) { |
| 167 LOG(ERROR) << "Got decode response while not decoding"; | 169 LOG(ERROR) << "Got decode response while not decoding"; |
| 168 return; | 170 return; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 lock_.AssertAcquired(); | 252 lock_.AssertAcquired(); |
| 251 return !decode_done_closure_.is_null(); | 253 return !decode_done_closure_.is_null(); |
| 252 } | 254 } |
| 253 | 255 |
| 254 void VideoCaptureGpuJpegDecoder::RecordInitDecodeUMA_Locked() { | 256 void VideoCaptureGpuJpegDecoder::RecordInitDecodeUMA_Locked() { |
| 255 UMA_HISTOGRAM_BOOLEAN("Media.VideoCaptureGpuJpegDecoder.InitDecodeSuccess", | 257 UMA_HISTOGRAM_BOOLEAN("Media.VideoCaptureGpuJpegDecoder.InitDecodeSuccess", |
| 256 decoder_status_ == INIT_PASSED); | 258 decoder_status_ == INIT_PASSED); |
| 257 } | 259 } |
| 258 | 260 |
| 259 } // namespace content | 261 } // namespace content |
| OLD | NEW |