Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(325)

Side by Side Diff: content/browser/renderer_host/media/video_capture_gpu_jpeg_decoder.cc

Issue 1132683004: MJPEG acceleration for video capture, browser part (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mjpeg-2-gpu
Patch Set: rebase Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/browser/renderer_host/media/video_capture_gpu_jpeg_decoder.h"
6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/trace_event/trace_event.h"
13 #include "content/browser/gpu/browser_gpu_channel_host_factory.h"
14 #include "content/common/gpu/client/gpu_jpeg_decode_accelerator_host.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "media/base/video_frame.h"
17
18 namespace content {
19
20 // static
21 bool VideoCaptureGpuJpegDecoder::Supported() {
22 // A lightweight check for caller to avoid IPC latency for known unsupported
23 // platform. Initialize() can do the real platform supporting check but it
24 // requires an IPC even for platforms that do not support HW decoder.
25 // TODO(kcwu): move this information to GpuInfo. https://crbug.com/503568
26 return false;
27 }
28
29 VideoCaptureGpuJpegDecoder::VideoCaptureGpuJpegDecoder(
30 const DecodeDoneCB& decode_done_cb,
31 const ErrorCB& error_cb)
32 : decode_done_cb_(decode_done_cb),
33 error_cb_(error_cb),
34 next_bitstream_buffer_id_(0),
35 in_buffer_id_(media::JpegDecodeAccelerator::kInvalidBitstreamBufferId) {
36 }
37
38 VideoCaptureGpuJpegDecoder::~VideoCaptureGpuJpegDecoder() {
39 DCHECK(CalledOnValidThread());
40
41 // |decoder_| guarantees no more JpegDecodeAccelerator::Client callbacks
42 // on IO thread after deletion.
43 decoder_.reset();
44
45 // |gpu_channel_host_| should outlive |decoder_|, so |gpu_channel_host_|
46 // must be released after |decoder_| has been destroyed.
47 gpu_channel_host_ = nullptr;
48 }
49
50 bool VideoCaptureGpuJpegDecoder::IsDecoding_Locked() {
51 lock_.AssertAcquired();
52 return !decode_done_closure_.is_null();
53 }
54
55 void VideoCaptureGpuJpegDecoder::Initialize() {
56 DCHECK(CalledOnValidThread());
57
58 const scoped_refptr<base::SingleThreadTaskRunner> current_task_runner(
59 base::ThreadTaskRunnerHandle::Get());
60 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
61 base::Bind(&EstablishGpuChannelOnUIThread,
62 current_task_runner, AsWeakPtr()));
63 }
64
65 bool VideoCaptureGpuJpegDecoder::ReadyToDecode() {
66 DCHECK(CalledOnValidThread());
67 return decoder_;
68 }
69
70 // static
71 void VideoCaptureGpuJpegDecoder::EstablishGpuChannelOnUIThread(
72 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
73 base::WeakPtr<VideoCaptureGpuJpegDecoder> weak_this) {
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
75 DCHECK(BrowserGpuChannelHostFactory::instance());
76
77 BrowserGpuChannelHostFactory::instance()->EstablishGpuChannel(
78 CAUSE_FOR_GPU_LAUNCH_JPEGDECODEACCELERATOR_INITIALIZE,
79 base::Bind(&VideoCaptureGpuJpegDecoder::GpuChannelEstablishedOnUIThread,
80 task_runner, weak_this));
81 }
82
83 // static
84 void VideoCaptureGpuJpegDecoder::GpuChannelEstablishedOnUIThread(
85 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
86 base::WeakPtr<VideoCaptureGpuJpegDecoder> weak_this) {
87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
88
89 scoped_refptr<GpuChannelHost> gpu_channel_host(
90 BrowserGpuChannelHostFactory::instance()->GetGpuChannel());
91 task_runner->PostTask(FROM_HERE,
92 base::Bind(&VideoCaptureGpuJpegDecoder::InitializeDone,
93 weak_this, base::Passed(&gpu_channel_host)));
94 }
95
96 void VideoCaptureGpuJpegDecoder::InitializeDone(
97 scoped_refptr<GpuChannelHost> gpu_channel_host) {
98 DCHECK(CalledOnValidThread());
99 if (!gpu_channel_host) {
100 LOG(ERROR) << "Failed to establish GPU channel for JPEG decoder";
101 return;
102 }
103 gpu_channel_host_ = gpu_channel_host.Pass();
104
105 decoder_ = gpu_channel_host_->CreateJpegDecoder(this);
106 }
107
108 void VideoCaptureGpuJpegDecoder::VideoFrameReady(int32_t bitstream_buffer_id) {
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
110 TRACE_EVENT0("jpeg", "VideoCaptureGpuJpegDecoder::VideoFrameReady");
111 base::AutoLock lock(lock_);
112
113 if (!IsDecoding_Locked()) {
114 LOG(ERROR) << "Got decode response while not decoding";
115 return;
116 }
117
118 if (bitstream_buffer_id != in_buffer_id_) {
119 LOG(ERROR) << "Unexpected bitstream_buffer_id " << bitstream_buffer_id
120 << ", expected " << in_buffer_id_;
121 return;
122 }
123 in_buffer_id_ = media::JpegDecodeAccelerator::kInvalidBitstreamBufferId;
124
125 decode_done_closure_.Run();
126 decode_done_closure_.Reset();
127
128 TRACE_EVENT_ASYNC_END0("jpeg", "VideoCaptureGpuJpegDecoder decoding",
129 bitstream_buffer_id);
130 }
131
132 void VideoCaptureGpuJpegDecoder::NotifyError(
133 int32_t bitstream_buffer_id,
134 media::JpegDecodeAccelerator::Error error) {
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
136 LOG(ERROR) << "Decode error, bitstream_buffer_id=" << bitstream_buffer_id
137 << ", error=" << error;
138
139 base::AutoLock lock(lock_);
140 decode_done_closure_.Reset();
141
142 error_cb_.Run(
143 base::StringPrintf("JPEG Decode error, bitstream_buffer_id=%d, error=%d",
144 bitstream_buffer_id, error));
145 }
146
147 void VideoCaptureGpuJpegDecoder::DecodeCapturedData(
148 const uint8_t* data,
149 size_t in_buffer_size,
150 const media::VideoCaptureFormat& frame_format,
151 const base::TimeTicks& timestamp,
152 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> out_buffer) {
153 DCHECK(CalledOnValidThread());
154 DCHECK(decoder_);
155
156 TRACE_EVENT_ASYNC_BEGIN0("jpeg", "VideoCaptureGpuJpegDecoder decoding",
157 next_bitstream_buffer_id_);
158 TRACE_EVENT0("jpeg", "VideoCaptureGpuJpegDecoder::DecodeCapturedData");
159
160 // TODO(kcwu): enqueue decode requests in case decoding is not fast enough
161 // (say, if decoding time is longer than 16ms for 60fps 4k image)
162 {
163 base::AutoLock lock(lock_);
164 if (IsDecoding_Locked()) {
165 DVLOG(1) << "Drop captured frame. Previous jpeg frame is still decoding";
166 return;
167 }
168 }
169
170 // Enlarge input buffer if necessary.
171 if (!in_shared_memory_.get() ||
172 in_buffer_size > in_shared_memory_->mapped_size()) {
173 // Reserve 2x space to avoid frequent reallocations for initial frames.
174 const size_t reserved_size = 2 * in_buffer_size;
175 in_shared_memory_.reset(new base::SharedMemory);
176 if (!in_shared_memory_->CreateAndMapAnonymous(reserved_size)) {
177 base::AutoLock lock(lock_);
178 error_cb_.Run(base::StringPrintf("CreateAndMapAnonymous failed, size=%zd",
179 reserved_size));
180 return;
181 }
182 }
183 memcpy(in_shared_memory_->memory(), data, in_buffer_size);
184
185 // No need to lock for |in_buffer_id_| since IsDecoding_Locked() is false.
186 in_buffer_id_ = next_bitstream_buffer_id_;
187 media::BitstreamBuffer in_buffer(in_buffer_id_, in_shared_memory_->handle(),
188 in_buffer_size);
189 // Mask against 30 bits, to avoid (undefined) wraparound on signed integer.
190 next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & 0x3FFFFFFF;
191
192 #if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS))
193 const gfx::Size dimensions = frame_format.frame_size;
194 base::SharedMemoryHandle out_handle = out_buffer->AsPlatformFile();
195 scoped_refptr<media::VideoFrame> out_frame =
196 media::VideoFrame::WrapExternalSharedMemory(
197 media::VideoFrame::I420, // format
198 dimensions, // coded_size
199 gfx::Rect(dimensions), // visible_rect
200 dimensions, // natural_size
201 static_cast<uint8_t*>(out_buffer->data()), // data
202 out_buffer->size(), // data_size
203 out_handle, // handle
204 0, // shared_memory_offset
205 base::TimeDelta()); // timestamp
206 if (!out_frame) {
207 base::AutoLock lock(lock_);
208 error_cb_.Run("DecodeCapturedData: WrapExternalSharedMemory");
209 return;
210 }
211 out_frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE,
212 frame_format.frame_rate);
213
214 {
215 base::AutoLock lock(lock_);
216 decode_done_closure_ = base::Bind(
217 decode_done_cb_, base::Passed(&out_buffer), out_frame, timestamp);
218 }
219 decoder_->Decode(in_buffer, out_frame);
220 #else
221 NOTREACHED();
222 #endif
223 }
224
225 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698