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 Initialize status.
41 INIT_PENDING
, // Default value while waiting initialization finished.
42 INIT_FAILED
, // JPEG decode is not supported or initialization failed.
43 INIT_PASSED
, // Initialization succeed.
46 typedef base::Callback
<void(
47 scoped_ptr
<media::VideoCaptureDevice::Client::Buffer
>,
48 const scoped_refptr
<media::VideoFrame
>&,
49 const base::TimeTicks
&)> DecodeDoneCB
;
50 typedef base::Callback
<void(const std::string
&)> ErrorCB
;
52 // |decode_done_cb| is called on the IO thread when decode succeed.
53 // |error_cb| is called when error. This can be on any thread.
54 // Both |decode_done_cb| and |error_cb| are never called after
55 // VideoCaptureGpuJpegDecoder is destroyed.
56 VideoCaptureGpuJpegDecoder(const DecodeDoneCB
& decode_done_cb
,
57 const ErrorCB
& error_cb
);
58 ~VideoCaptureGpuJpegDecoder() override
;
60 // Creates and intializes decoder asynchronously.
63 // Returns initialization status.
66 // Decodes a JPEG picture.
67 void DecodeCapturedData(
69 size_t in_buffer_size
,
70 const media::VideoCaptureFormat
& frame_format
,
71 const base::TimeTicks
& timestamp
,
72 scoped_ptr
<media::VideoCaptureDevice::Client::Buffer
> out_buffer
);
74 // JpegDecodeAccelerator::Client implementation.
75 // These will be called on IO thread.
76 void VideoFrameReady(int32_t buffer_id
) override
;
77 void NotifyError(int32_t buffer_id
,
78 media::JpegDecodeAccelerator::Error error
) override
;
81 // Initialization helper, to establish GPU channel.
82 static void EstablishGpuChannelOnUIThread(
83 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
84 base::WeakPtr
<VideoCaptureGpuJpegDecoder
> weak_this
);
86 static void GpuChannelEstablishedOnUIThread(
87 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
88 base::WeakPtr
<VideoCaptureGpuJpegDecoder
> weak_this
);
90 void InitializeDone(scoped_refptr
<GpuChannelHost
> gpu_channel_host
);
92 // Returns true if the decoding of last frame is not finished yet.
93 bool IsDecoding_Locked();
95 scoped_refptr
<GpuChannelHost
> gpu_channel_host_
;
97 // The underlying JPEG decode accelerator.
98 scoped_ptr
<media::JpegDecodeAccelerator
> decoder_
;
100 // The callback to run when decode succeeds.
101 const DecodeDoneCB decode_done_cb_
;
103 // Guards |decode_done_closure_| and |error_cb_|.
106 // The callback to run when an error occurs.
107 const ErrorCB error_cb_
;
109 // The closure of |decode_done_cb_| with bound parameters.
110 base::Closure decode_done_closure_
;
112 // Next id for input BitstreamBuffer.
113 int32_t next_bitstream_buffer_id_
;
115 // The id for current input BitstreamBuffer being decoded.
116 int32_t in_buffer_id_
;
118 // Shared memory to store JPEG stream buffer. The input BitstreamBuffer is
120 scoped_ptr
<base::SharedMemory
> in_shared_memory_
;
124 DISALLOW_COPY_AND_ASSIGN(VideoCaptureGpuJpegDecoder
);
127 } // namespace content
129 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_GPU_JPEG_DECODER_H_