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.
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_GPU_JPEG_DECODER_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_GPU_JPEG_DECODER_H_
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/threading/thread.h"
16 #include "content/common/content_export.h"
17 #include "media/capture/video/video_capture_device.h"
18 #include "media/video/jpeg_decode_accelerator.h"
27 // Adapter to GpuJpegDecodeAccelerator for VideoCaptureDevice::Client. It takes
28 // care of GpuJpegDecodeAccelerator creation, shared memory, and threading
31 // All public methods except JpegDecodeAccelerator::Client ones should be called
32 // on the same thread. JpegDecodeAccelerator::Client methods should be called on
34 class CONTENT_EXPORT VideoCaptureGpuJpegDecoder
35 : public media::JpegDecodeAccelerator::Client
,
36 public base::NonThreadSafe
,
37 public base::SupportsWeakPtr
<VideoCaptureGpuJpegDecoder
> {
39 // Enumeration of decoder status. The enumeration is published for clients to
40 // decide the behavior according to STATUS.
42 INIT_PENDING
, // Default value while waiting initialization finished.
43 INIT_PASSED
, // Initialization succeed.
44 FAILED
, // JPEG decode is not supported, initialization failed, or
48 typedef base::Callback
<void(
49 scoped_ptr
<media::VideoCaptureDevice::Client::Buffer
>,
50 const scoped_refptr
<media::VideoFrame
>&,
51 const base::TimeTicks
&)> DecodeDoneCB
;
53 // |decode_done_cb| is called on the IO thread when decode succeed. This can
54 // be on any thread. |decode_done_cb| is never called after
55 // VideoCaptureGpuJpegDecoder is destroyed.
56 VideoCaptureGpuJpegDecoder(const DecodeDoneCB
& decode_done_cb
);
57 ~VideoCaptureGpuJpegDecoder() override
;
59 // Creates and intializes decoder asynchronously.
62 // Returns initialization status.
63 STATUS
GetStatus() const;
65 // Decodes a JPEG picture.
66 void DecodeCapturedData(
68 size_t in_buffer_size
,
69 const media::VideoCaptureFormat
& frame_format
,
70 const base::TimeTicks
& timestamp
,
71 scoped_ptr
<media::VideoCaptureDevice::Client::Buffer
> out_buffer
);
73 // JpegDecodeAccelerator::Client implementation.
74 // These will be called on IO thread.
75 void VideoFrameReady(int32_t buffer_id
) override
;
76 void NotifyError(int32_t buffer_id
,
77 media::JpegDecodeAccelerator::Error error
) override
;
80 // Initialization helper, to establish GPU channel.
81 static void EstablishGpuChannelOnUIThread(
82 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
83 base::WeakPtr
<VideoCaptureGpuJpegDecoder
> weak_this
);
85 static void GpuChannelEstablishedOnUIThread(
86 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
87 base::WeakPtr
<VideoCaptureGpuJpegDecoder
> weak_this
);
89 void FinishInitialization(scoped_refptr
<GpuChannelHost
> gpu_channel_host
);
91 // Returns true if the decoding of last frame is not finished yet.
92 bool IsDecoding_Locked() const;
94 // Records |decoder_status_| to histogram.
95 void RecordInitDecodeUMA_Locked();
97 scoped_refptr
<GpuChannelHost
> gpu_channel_host_
;
99 // The underlying JPEG decode accelerator.
100 scoped_ptr
<media::JpegDecodeAccelerator
> decoder_
;
102 // The callback to run when decode succeeds.
103 const DecodeDoneCB decode_done_cb_
;
105 // Guards |decode_done_closure_| and |decoder_status_|.
106 mutable base::Lock lock_
;
108 // The closure of |decode_done_cb_| with bound parameters.
109 base::Closure decode_done_closure_
;
111 // Next id for input BitstreamBuffer.
112 int32_t next_bitstream_buffer_id_
;
114 // The id for current input BitstreamBuffer being decoded.
115 int32_t in_buffer_id_
;
117 // Shared memory to store JPEG stream buffer. The input BitstreamBuffer is
119 scoped_ptr
<base::SharedMemory
> in_shared_memory_
;
121 STATUS decoder_status_
;
123 DISALLOW_COPY_AND_ASSIGN(VideoCaptureGpuJpegDecoder
);
126 } // namespace content
128 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_GPU_JPEG_DECODER_H_