Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / browser / renderer_host / media / video_capture_gpu_jpeg_decoder.h
blob00d7c63abbe76dda2950d8ace6a55828cb166070
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_
8 #include <string>
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"
20 namespace media {
21 class VideoFrame;
24 namespace content {
25 class GpuChannelHost;
27 // Adapter to GpuJpegDecodeAccelerator for VideoCaptureDevice::Client. It takes
28 // care of GpuJpegDecodeAccelerator creation, shared memory, and threading
29 // issues.
31 // All public methods except JpegDecodeAccelerator::Client ones should be called
32 // on the same thread. JpegDecodeAccelerator::Client methods should be called on
33 // the IO thread.
34 class CONTENT_EXPORT VideoCaptureGpuJpegDecoder
35 : public media::JpegDecodeAccelerator::Client,
36 public base::NonThreadSafe,
37 public base::SupportsWeakPtr<VideoCaptureGpuJpegDecoder> {
38 public:
39 // Enumeration of Initialize status.
40 enum 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.
61 void Initialize();
63 // Returns initialization status.
64 Status GetStatus();
66 // Decodes a JPEG picture.
67 void DecodeCapturedData(
68 const uint8_t* data,
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;
80 private:
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_|.
104 base::Lock lock_;
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
119 // backed by this.
120 scoped_ptr<base::SharedMemory> in_shared_memory_;
122 Status init_status_;
124 DISALLOW_COPY_AND_ASSIGN(VideoCaptureGpuJpegDecoder);
127 } // namespace content
129 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_GPU_JPEG_DECODER_H_