Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1701)

Unified Diff: content/renderer/media/gpu/rtc_video_decoder.h

Issue 2418613002: Proxy RtcVideoDecoder calls to a media::VideoDecoder.
Patch Set: Comments addressed and unittests updated. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | content/renderer/media/gpu/rtc_video_decoder.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/renderer/media/gpu/rtc_video_decoder.h
diff --git a/content/renderer/media/gpu/rtc_video_decoder.h b/content/renderer/media/gpu/rtc_video_decoder.h
index e6eefade04b4073981784e09c6078d0d5ec2bae2..6c8de9ca9c49834c45f7a291b69f155e83e44e05 100644
--- a/content/renderer/media/gpu/rtc_video_decoder.h
+++ b/content/renderer/media/gpu/rtc_video_decoder.h
@@ -9,60 +9,62 @@
#include <stdint.h>
#include <deque>
-#include <list>
-#include <map>
#include <memory>
-#include <set>
+#include <unordered_map>
#include <utility>
#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/synchronization/lock.h"
-#include "base/threading/thread.h"
+#include "base/threading/thread_checker.h"
#include "content/common/content_export.h"
-#include "media/base/bitstream_buffer.h"
#include "media/base/video_decoder.h"
-#include "media/video/picture.h"
-#include "media/video/video_decode_accelerator.h"
+#include "media/base/video_decoder_config.h"
#include "third_party/webrtc/modules/video_coding/include/video_codec_interface.h"
-#include "ui/gfx/geometry/rect.h"
namespace base {
class WaitableEvent;
-};
+}
namespace media {
class DecoderBuffer;
-class GpuVideoAcceleratorFactories;
-}
-
-namespace gpu {
-struct SyncToken;
}
namespace content {
-// This class uses hardware accelerated video decoder to decode video for
-// WebRTC. Lives on the media thread, where VDA::Client methods run on.
-// webrtc::VideoDecoder methods run on WebRTC DecodingThread or
-// Chrome_libJingle_WorkerThread, which are trampolined to the media thread.
-// Decode() is non-blocking and queues the buffers. Decoded frames are
-// delivered to WebRTC on the media task runner.
+// This class proxies webrtc::VideoDecoder calls to an owned instance of
+// media::VideoDecoder, which is created during initialization. Usually, this
+// decoder will in turn proxy calls to a remote hardware-accelerated decoder
+// (ex. MojoVideoDecoder, GpuVideoDecoder), but a local media::VideoDecoder can
+// also be used.
+//
+// THREADING: webrtc::VideoDecoder methods run on the WebRTC DecodingThread or
+// the Chrome_libJingle_WorkerThread, and are proxied to the media::VideoDecoder
+// on |video_decoder_thread|, which is provided in Create().
class CONTENT_EXPORT RTCVideoDecoder
- : NON_EXPORTED_BASE(public webrtc::VideoDecoder),
- public media::VideoDecodeAccelerator::Client {
+ : NON_EXPORTED_BASE(public webrtc::VideoDecoder) {
public:
~RTCVideoDecoder() override;
- // Creates a RTCVideoDecoder on the message loop of |factories|. Returns NULL
- // if failed. The video decoder will run on the message loop of |factories|.
+ // A factory closure which can be run to create a media::VideoDecoder.
+ using CreateVideoDecoderCB =
+ base::Callback<std::unique_ptr<media::VideoDecoder>()>;
+
+ // Can be called on any thread. Creates and returns an RTCVideoDecoder for the
+ // given codec |type|. |create_video_decoder_cb| is Run() on
+ // |decoder_task_runner| asynchronously to create and initialize an instance
+ // of media::VideoDecoder.
static std::unique_ptr<RTCVideoDecoder> Create(
webrtc::VideoCodecType type,
- media::GpuVideoAcceleratorFactories* factories);
- // Destroys |decoder| on the loop of |factories|
- static void Destroy(webrtc::VideoDecoder* decoder,
- media::GpuVideoAcceleratorFactories* factories);
+ const CreateVideoDecoderCB& create_video_decoder_cb,
+ const scoped_refptr<base::SingleThreadTaskRunner>& decoder_task_runner);
+
+ // Destroys |decoder| on |decoder_task_runner|. This function takes ownership
+ // of |decoder|, which should not be used after this function is called.
+ static void Destroy(
+ webrtc::VideoDecoder* decoder,
+ const scoped_refptr<base::SingleThreadTaskRunner>& decoder_task_runner);
// webrtc::VideoDecoder implementation.
// Called on WebRTC DecodingThread.
@@ -77,149 +79,84 @@ class CONTENT_EXPORT RTCVideoDecoder
// Called on WebRTC DecodingThread.
int32_t RegisterDecodeCompleteCallback(
webrtc::DecodedImageCallback* callback) override;
+
// Called on Chrome_libJingle_WorkerThread. The child thread is blocked while
// this runs.
int32_t Release() override;
- // VideoDecodeAccelerator::Client implementation.
- void ProvidePictureBuffers(uint32_t count,
- media::VideoPixelFormat format,
- uint32_t textures_per_buffer,
- const gfx::Size& size,
- uint32_t texture_target) override;
- void DismissPictureBuffer(int32_t id) override;
- void PictureReady(const media::Picture& picture) override;
- void NotifyEndOfBitstreamBuffer(int32_t id) override;
- void NotifyFlushDone() override;
- void NotifyResetDone() override;
- void NotifyError(media::VideoDecodeAccelerator::Error error) override;
-
private:
- // Metadata of a bitstream buffer.
- struct BufferData {
- BufferData(int32_t bitstream_buffer_id,
- uint32_t timestamp,
- size_t size,
- const gfx::Rect& visible_rect);
- BufferData();
- ~BufferData();
- int32_t bitstream_buffer_id;
- uint32_t timestamp; // in 90KHz
- size_t size; // buffer size
- gfx::Rect visible_rect;
- };
-
FRIEND_TEST_ALL_PREFIXES(RTCVideoDecoderTest, IsBufferAfterReset);
FRIEND_TEST_ALL_PREFIXES(RTCVideoDecoderTest, IsFirstBufferAfterReset);
- FRIEND_TEST_ALL_PREFIXES(RTCVideoDecoderTest, GetVDAErrorCounterForTesting);
+ FRIEND_TEST_ALL_PREFIXES(RTCVideoDecoderTest,
+ GetDecoderErrorCounterForTesting);
- RTCVideoDecoder(webrtc::VideoCodecType type,
- media::GpuVideoAcceleratorFactories* factories);
-
- // Requests a buffer to be decoded by VDA.
+ // |create_video_decoder_cb| is Run() on |decoder_task_runner| asynchronously
+ // to create and initialize an instance of media::VideoDecoder.
+ RTCVideoDecoder(
+ webrtc::VideoCodecType type,
+ const CreateVideoDecoderCB& create_video_decoder_cb,
+ const scoped_refptr<base::SingleThreadTaskRunner>& decoder_task_runner);
+
+ // Create |decoder_| by running |create_decoder_cb_|, then Initialize()
+ // |decoder_| using |config|. |decoder_initialized| will be bound to the
+ // callback passed to the decoder. Must be called on |decoder_task_runner|.
+ void InitializeDecoder(base::WaitableEvent* decoder_initialized,
+ const media::VideoDecoderConfig& config);
+
+ // Called by |decoder_| once it has been initialized. After setting |state_|
+ // appropriately, signal |decoder_initialized|. Must be called on
+ // |decoder_task_runner_|.
+ void OnDecoderInitialized(base::WaitableEvent* decoder_initialized,
+ bool success);
+
+ // Called by |decoder_| when a decoded frame is ready for consumption. This
+ // call will be proxied to |decode_complete_callback_|.
+ void OnFrameReady(const scoped_refptr<media::VideoFrame>& frame);
+
+ // Requests a buffer to be decoded by |decoder_|. Must be called on
+ // |decoder_task_runner|.
void RequestBufferDecode();
+ // Returns true if |decoder_| can decode another buffer right now.
bool CanMoreDecodeWorkBeDone();
- // Returns true if bitstream buffer id |id_buffer| comes after |id_reset|.
- // This handles the wraparound.
- bool IsBufferAfterReset(int32_t id_buffer, int32_t id_reset);
-
- // Returns true if bitstream buffer |id_buffer| is the first buffer after
- // |id_reset|.
- bool IsFirstBufferAfterReset(int32_t id_buffer, int32_t id_reset);
-
- int GetVDAErrorCounterForTesting() { return vda_error_counter_; }
-
- // Saves a WebRTC buffer in |decode_buffers_| for decode.
- void SaveToDecodeBuffers_Locked(
- const webrtc::EncodedImage& input_image,
- std::unique_ptr<base::SharedMemory> shm_buffer,
- const BufferData& buffer_data);
-
- // Saves a WebRTC buffer in |pending_buffers_| waiting for SHM available.
- // Returns true on success.
- bool SaveToPendingBuffers_Locked(const webrtc::EncodedImage& input_image,
- const BufferData& buffer_data);
-
- // Gets SHM and moves pending buffers to decode buffers.
- void MovePendingBuffersToDecodeBuffers();
-
- scoped_refptr<media::VideoFrame> CreateVideoFrame(
- const media::Picture& picture,
- const media::PictureBuffer& pb,
- uint32_t timestamp,
- const gfx::Rect& visible_rect,
- media::VideoPixelFormat pixel_format);
-
- // Resets VDA.
- void ResetInternal();
-
- // Static method is to allow it to run even after RVD is deleted.
- static void ReleaseMailbox(base::WeakPtr<RTCVideoDecoder> decoder,
- media::GpuVideoAcceleratorFactories* factories,
- int64_t picture_buffer_id,
- uint32_t texture_id,
- const gpu::SyncToken& release_sync_token);
- // Tells VDA that a picture buffer can be recycled.
- void ReusePictureBuffer(int64_t picture_buffer_id);
-
- // Creates |vda_| on the media thread.
- void CreateVDA(media::VideoCodecProfile profile, base::WaitableEvent* waiter);
-
- void DestroyTextures();
- void DestroyVDA();
-
- // Gets a shared-memory segment of at least |min_size| bytes from
- // |available_shm_segments_|. Returns NULL if there is no buffer or the
- // buffer is not big enough.
- std::unique_ptr<base::SharedMemory> GetSHM_Locked(size_t min_size);
-
- // Returns a shared-memory segment to the available pool.
- void PutSHM_Locked(std::unique_ptr<base::SharedMemory> shm_buffer);
-
- // Allocates |count| shared memory buffers of |size| bytes.
- void CreateSHM(size_t count, size_t size);
-
- // Stores the buffer metadata to |input_buffer_data_|.
- void RecordBufferData(const BufferData& buffer_data);
- // Gets the buffer metadata from |input_buffer_data_|.
- void GetBufferData(int32_t bitstream_buffer_id,
- uint32_t* timestamp,
- gfx::Rect* visible_rect);
+ int GetDecoderErrorCounterForTesting();
- // Records the result of InitDecode to UMA and returns |status|.
- int32_t RecordInitDecodeUMA(int32_t status);
+ // Saves a DecoderBuffer buffer in |pending_buffers_| waiting to be sent to
+ // the decoder. Returns true on success.
+ bool SaveToPendingBuffers_Locked(
+ int32_t decoder_buffer_id,
+ scoped_refptr<media::DecoderBuffer> decoder_buffer);
- // Asserts the contract that this class is operated on the right thread.
- void DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent() const;
+ // |decoder_| will call this on |decoder_task_runner| when it has finshed
+ // a Reset().
+ void OnResetDone();
- // Queries factories_ whether |profile| is supported and return true is so,
- // false otherwise. If true, also set resolution limits for |profile|
- // in min/max_resolution_.
- bool IsProfileSupported(media::VideoCodecProfile profile);
+ // |decoder_| will call this when it has finished decoding the buffer
+ // identified by |decoder_buffer_id|. |decoder_| will also use this to bubble
+ // up decoding errors, by setting |status| to DECODE_ERROR. Will be called on
+ // |decoder_task_runner|.
+ void OnBufferDecoded(int32_t decoder_buffer_id, media::DecodeStatus status);
- // Clears the pending_buffers_ queue, freeing memory.
- void ClearPendingBuffers();
+ // Records the result of InitDecode to UMA and returns |status|.
+ int32_t RecordInitDecodeUMA(int32_t status);
+
+ // Clears the |pending_buffers_| queue. |lock_| must be acquired before
+ // calling this.
+ void ClearPendingBuffers_Locked();
enum State {
- UNINITIALIZED, // The decoder has not initialized.
- INITIALIZED, // The decoder has initialized.
- RESETTING, // The decoder is being reset.
- DECODE_ERROR, // Decoding error happened.
+ UNINITIALIZED = 0, // The decoder has not initialized.
+ INITIALIZED, // The decoder has initialized.
+ RESETTING, // The decoder is being reset.
+ DECODE_ERROR, // Decoding error happened.
};
- static const int32_t ID_LAST; // maximum bitstream buffer id
- static const int32_t ID_HALF; // half of the maximum bitstream buffer id
- static const int32_t ID_INVALID; // indicates Reset or Release never occurred
-
// The hardware video decoder.
- std::unique_ptr<media::VideoDecodeAccelerator> vda_;
-
- media::VideoCodecProfile vda_codec_profile_;
+ std::unique_ptr<::media::VideoDecoder> decoder_;
- // Number of times that |vda_| notified of an error.
- uint32_t vda_error_counter_;
+ // Number of times that |decoder_| notified of an error.
+ uint32_t decoder_error_counter_;
// The video codec type, as reported by WebRTC.
const webrtc::VideoCodecType video_codec_type_;
@@ -227,39 +164,13 @@ class CONTENT_EXPORT RTCVideoDecoder
// The size of the incoming video frames.
gfx::Size frame_size_;
- media::GpuVideoAcceleratorFactories* const factories_;
-
- // The texture target used for decoded pictures.
- uint32_t decoder_texture_target_;
-
- // The format of the decoded pictures.
- media::VideoPixelFormat pixel_format_;
-
- // Metadata of the buffers that have been sent for decode.
- std::list<BufferData> input_buffer_data_;
+ // The factory callback and task runner used to create the media::VideoDecoder
+ const CreateVideoDecoderCB create_video_decoder_cb_;
+ scoped_refptr<base::SingleThreadTaskRunner> decoder_task_runner_;
- // A map from bitstream buffer IDs to bitstream buffers that are being
- // processed by VDA.
- std::map<int32_t, std::unique_ptr<base::SharedMemory>>
- bitstream_buffers_in_decoder_;
-
- // A map from picture buffer IDs to texture-backed picture buffers.
- std::map<int32_t, media::PictureBuffer> assigned_picture_buffers_;
-
- // PictureBuffers given to us by VDA via PictureReady, which we sent forward
- // as VideoFrames to be rendered via read_cb_, and which will be returned
- // to us via ReusePictureBuffer.
- typedef std::map<int32_t /* picture_buffer_id */, uint32_t /* texture_id */>
- PictureBufferTextureMap;
- PictureBufferTextureMap picture_buffers_at_display_;
-
- // The id that will be given to the next picture buffer.
- int32_t next_picture_buffer_id_;
-
- // Protects |state_|, |decode_complete_callback_| , |num_shm_buffers_|,
- // |available_shm_segments_|, |pending_buffers_|, |decode_buffers_|,
- // |next_bitstream_buffer_id_|, |reset_bitstream_buffer_id_| and
- // |vda_error_counter_|.
+ // Protects |state_|, |decode_complete_callback_|, |pending_buffers_|,
+ // |next_decoder_buffer_id_|, |reset_bitstream_buffer_id_|, and
+ // |decoder_error_counter_|.
base::Lock lock_;
// The state of RTCVideoDecoder. Guarded by |lock_|.
@@ -268,33 +179,26 @@ class CONTENT_EXPORT RTCVideoDecoder
// Guarded by |lock_|.
webrtc::DecodedImageCallback* decode_complete_callback_;
- // Total number of allocated SHM buffers. Guarded by |lock_|.
- size_t num_shm_buffers_;
-
- // Shared-memory buffer pool. Since allocating SHM segments requires a
- // round-trip to the browser process, we keep allocation out of the
- // steady-state of the decoder. Guarded by |lock_|.
- std::vector<std::unique_ptr<base::SharedMemory>> available_shm_segments_;
-
- // A queue storing WebRTC encoding images (and their metadata) that are
- // waiting for the shared memory. Guarded by |lock_|.
- std::deque<std::pair<webrtc::EncodedImage, BufferData>> pending_buffers_;
+ // A queue storing decoder buffers that are waiting to be sent to |decoder_|.
+ // Guarded by |lock_|.
+ std::deque<std::pair<int32_t, scoped_refptr<media::DecoderBuffer>>>
+ pending_buffers_;
- // A queue storing buffers (and their metadata) that will be sent to VDA for
- // decode. Guarded by |lock_|.
- std::deque<std::pair<std::unique_ptr<base::SharedMemory>, BufferData>>
- decode_buffers_;
+ // A map of the buffers currently being decoded by |decoder_|.
+ std::unordered_map<int32_t /* decoder_buffer_id */,
+ scoped_refptr<media::DecoderBuffer>>
+ buffers_in_decoder_;
// The id that will be given to the next bitstream buffer. Guarded by |lock_|.
- int32_t next_bitstream_buffer_id_;
+ int32_t next_decoder_buffer_id_;
// A buffer that has an id less than this should be dropped because Reset or
// Release has been called. Guarded by |lock_|.
- int32_t reset_bitstream_buffer_id_;
+ int32_t reset_decoder_buffer_id_;
- // Minimum and maximum supported resolutions for the current profile/VDA.
- gfx::Size min_resolution_;
- gfx::Size max_resolution_;
+ // This will be used to verify that the proper set of calls are made on the
+ // webrtc DecodingThread.
+ base::ThreadChecker webrtc_decoding_thread_checker_;
// Must be destroyed, or invalidated, on the media thread.
// NOTE: Weak pointers must be invalidated before all other member variables.
« no previous file with comments | « no previous file | content/renderer/media/gpu/rtc_video_decoder.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698