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/video/capture/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 typedef base::Callback
<void(
40 scoped_ptr
<media::VideoCaptureDevice::Client::Buffer
>,
41 const scoped_refptr
<media::VideoFrame
>&,
42 const base::TimeTicks
&)> DecodeDoneCB
;
43 typedef base::Callback
<void(const std::string
&)> ErrorCB
;
45 // Returns true if JPEG hardware decoding is supported on this device.
46 static bool Supported();
48 // |decode_done_cb| is called on the IO thread when decode succeed.
49 // |error_cb| is called when error. This can be on any thread.
50 // Both |decode_done_cb| and |error_cb| are never called after
51 // VideoCaptureGpuJpegDecoder is destroyed.
52 VideoCaptureGpuJpegDecoder(const DecodeDoneCB
& decode_done_cb
,
53 const ErrorCB
& error_cb
);
54 ~VideoCaptureGpuJpegDecoder() override
;
56 // Creates and intializes decoder asynchronously.
59 // Returns true if Initialize is done and it's okay to call
60 // DecodeCapturedData.
63 // Decodes a JPEG picture.
64 void DecodeCapturedData(
66 size_t in_buffer_size
,
67 const media::VideoCaptureFormat
& frame_format
,
68 const base::TimeTicks
& timestamp
,
69 scoped_ptr
<media::VideoCaptureDevice::Client::Buffer
> out_buffer
);
71 // JpegDecodeAccelerator::Client implementation.
72 // These will be called on IO thread.
73 void VideoFrameReady(int32_t buffer_id
) override
;
74 void NotifyError(int32_t buffer_id
,
75 media::JpegDecodeAccelerator::Error error
) override
;
78 // Initialization helper, to establish GPU channel.
79 static void EstablishGpuChannelOnUIThread(
80 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
81 base::WeakPtr
<VideoCaptureGpuJpegDecoder
> weak_this
);
83 static void GpuChannelEstablishedOnUIThread(
84 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
85 base::WeakPtr
<VideoCaptureGpuJpegDecoder
> weak_this
);
87 void InitializeDone(scoped_refptr
<GpuChannelHost
> gpu_channel_host
);
89 // Returns true if the decoding of last frame is not finished yet.
90 bool IsDecoding_Locked();
92 scoped_refptr
<GpuChannelHost
> gpu_channel_host_
;
94 // The underlying JPEG decode accelerator.
95 scoped_ptr
<media::JpegDecodeAccelerator
> decoder_
;
97 // The callback to run when decode succeeds.
98 const DecodeDoneCB decode_done_cb_
;
100 // Guards |decode_done_closure_| and |error_cb_|.
103 // The callback to run when an error occurs.
104 const ErrorCB error_cb_
;
106 // The closure of |decode_done_cb_| with bound parameters.
107 base::Closure decode_done_closure_
;
109 // Next id for input BitstreamBuffer.
110 int32_t next_bitstream_buffer_id_
;
112 // The id for current input BitstreamBuffer being decoded.
113 int32_t in_buffer_id_
;
115 // Shared memory to store JPEG stream buffer. The input BitstreamBuffer is
117 scoped_ptr
<base::SharedMemory
> in_shared_memory_
;
119 DISALLOW_COPY_AND_ASSIGN(VideoCaptureGpuJpegDecoder
);
122 } // namespace content
124 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_GPU_JPEG_DECODER_H_