OLD | NEW |
(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 #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_ |
| 7 |
| 8 #include <string> |
| 9 |
| 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" |
| 19 |
| 20 namespace media { |
| 21 class VideoFrame; |
| 22 } |
| 23 |
| 24 namespace content { |
| 25 class GpuChannelHost; |
| 26 |
| 27 // Adapter to GpuJpegDecodeAccelerator for VideoCaptureDevice::Client. It takes |
| 28 // care of GpuJpegDecodeAccelerator creation, shared memory, and threading |
| 29 // issues. |
| 30 // |
| 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 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; |
| 44 |
| 45 // Returns true if JPEG hardware decoding is supported on this device. |
| 46 static bool Supported(); |
| 47 |
| 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; |
| 55 |
| 56 // Creates and intializes decoder asynchronously. |
| 57 void Initialize(); |
| 58 |
| 59 // Returns true if Initialize is done and it's okay to call |
| 60 // DecodeCapturedData. |
| 61 bool ReadyToDecode(); |
| 62 |
| 63 // Decodes a JPEG picture. |
| 64 void DecodeCapturedData( |
| 65 const uint8_t* data, |
| 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); |
| 70 |
| 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; |
| 76 |
| 77 private: |
| 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); |
| 82 |
| 83 static void GpuChannelEstablishedOnUIThread( |
| 84 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 85 base::WeakPtr<VideoCaptureGpuJpegDecoder> weak_this); |
| 86 |
| 87 void InitializeDone(scoped_refptr<GpuChannelHost> gpu_channel_host); |
| 88 |
| 89 // Returns true if the decoding of last frame is not finished yet. |
| 90 bool IsDecoding_Locked(); |
| 91 |
| 92 scoped_refptr<GpuChannelHost> gpu_channel_host_; |
| 93 |
| 94 // The underlying JPEG decode accelerator. |
| 95 scoped_ptr<media::JpegDecodeAccelerator> decoder_; |
| 96 |
| 97 // The callback to run when decode succeeds. |
| 98 const DecodeDoneCB decode_done_cb_; |
| 99 |
| 100 // Guards |decode_done_closure_| and |error_cb_|. |
| 101 base::Lock lock_; |
| 102 |
| 103 // The callback to run when an error occurs. |
| 104 const ErrorCB error_cb_; |
| 105 |
| 106 // The closure of |decode_done_cb_| with bound parameters. |
| 107 base::Closure decode_done_closure_; |
| 108 |
| 109 // Next id for input BitstreamBuffer. |
| 110 int32_t next_bitstream_buffer_id_; |
| 111 |
| 112 // The id for current input BitstreamBuffer being decoded. |
| 113 int32_t in_buffer_id_; |
| 114 |
| 115 // Shared memory to store JPEG stream buffer. The input BitstreamBuffer is |
| 116 // backed by this. |
| 117 scoped_ptr<base::SharedMemory> in_shared_memory_; |
| 118 |
| 119 DISALLOW_COPY_AND_ASSIGN(VideoCaptureGpuJpegDecoder); |
| 120 }; |
| 121 |
| 122 } // namespace content |
| 123 |
| 124 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_GPU_JPEG_DECODER_H_ |
OLD | NEW |