| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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_RENDERER_MEDIA_ANDROID_STREAM_TEXTURE_WRAPPER_IMPL_H_ |
| 6 #define CONTENT_RENDERER_MEDIA_ANDROID_STREAM_TEXTURE_WRAPPER_IMPL_H_ |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "content/common/content_export.h" |
| 11 #include "content/renderer/media/android/stream_texture_factory.h" |
| 12 #include "gpu/command_buffer/common/mailbox.h" |
| 13 #include "media/base/video_frame.h" |
| 14 #include "media/blink/stream_texture_wrapper.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 // Concrete implementation of StreamTextureWrapper. Any method can be called on |
| 19 // any thread, but additional threading considerations are listed in the |
| 20 // comments of individual methods. |
| 21 // |
| 22 // The StreamTexture is an abstraction allowing Chrome to wrap a SurfaceTexture |
| 23 // living in the GPU process. It allows VideoFrames to be created from the |
| 24 // SurfaceTexture's texture, in the Renderer process. |
| 25 // |
| 26 // The general idea behind our use of StreamTexture is as follows: |
| 27 // - We create a client GL texture in the Renderer process. |
| 28 // - We request the creation of a StreamTexture via the StreamTextureFactory, |
| 29 // passing the client texture ID. The call is sent to the GPU process via the |
| 30 // CommandBuffer. The "platform" GL texture reference associated with the client |
| 31 // texture ID is looked up in the TextureManager. A StreamTexture is then |
| 32 // created, wrapping a SurfaceTexture created from the texture reference. The |
| 33 // SurfaceTexture's OnFrameAvailable() callback is tied to StreamTexture's |
| 34 // OnFrameAvailable(), which fires an IPC accross the GPU channel. |
| 35 // - We create a StreamTextureProxy in the Renderer process which listens for |
| 36 // the IPC fired by the StreamTexture's OnFrameAvailable() callback. |
| 37 // - We bind the StreamTextureProxy's lifetime to the |compositor_task_runner_|. |
| 38 // - We wrap the client texture into a VideoFrame. |
| 39 // - When the SurfaceTexture's OnFrameAvailable() callback is fired (and routed |
| 40 // to the StreamTextureProxy living on the compositor thread), we notify |
| 41 // |client_| that a new frame is available, via the DidReceiveFrame() callback. |
| 42 // |
| 43 // TODO(tguilbert): Register the underlying SurfaceTexture for retrieval in the |
| 44 // browser process. See crbug.com/627658. |
| 45 // |
| 46 // TODO(tguilbert): Change StreamTextureProxy's interface to accept a |
| 47 // base::Closure instead of requiring a VideoFrameProvider::Client, to simplify |
| 48 // the MediaPlayerRendererHost interface. See crbug.com/631178. |
| 49 class CONTENT_EXPORT StreamTextureWrapperImpl |
| 50 : public media::StreamTextureWrapper { |
| 51 public: |
| 52 static media::ScopedStreamTextureWrapper Create( |
| 53 scoped_refptr<StreamTextureFactory> factory, |
| 54 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner); |
| 55 |
| 56 // Creates the underlying StreamTexture, and binds |stream_texture_proxy_| to |
| 57 // |compositor_task_runner|. |
| 58 // |
| 59 // Additional threading considerations: |
| 60 // - Can be called from any thread. |
| 61 // - Initialization will be posted to |main_task_runner_|. |
| 62 // - |init_cb| will be run on the calling thread. |
| 63 // - New frames will be signaled on |compositor_task_runner| via |client|'s |
| 64 // DidReceiveFrame() method. |
| 65 void Initialize( |
| 66 cc::VideoFrameProvider::Client* client, |
| 67 const gfx::Size& natural_size, |
| 68 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner, |
| 69 const base::Closure& init_cb) override; |
| 70 |
| 71 // Should be called when the Video size changes. |
| 72 // Can be called from any thread, but runs on |main_task_runner_|. |
| 73 void UpdateTextureSize(const gfx::Size& natural_size) override; |
| 74 |
| 75 // Returns the latest frame. |
| 76 // N.B: We create a single VideoFrame at initialization time (and update it |
| 77 // in UpdateTextureSize()), and repeatedly return it here. The underlying |
| 78 // texture's changes are signalled via |client|'s DidReceiveFrame() callback. |
| 79 scoped_refptr<media::VideoFrame> GetCurrentFrame() override; |
| 80 |
| 81 private: |
| 82 StreamTextureWrapperImpl( |
| 83 scoped_refptr<StreamTextureFactory> factory, |
| 84 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner); |
| 85 ~StreamTextureWrapperImpl() override; |
| 86 |
| 87 // Destroys |this| safely on |main_task_runner_|. |
| 88 void Destroy() override; |
| 89 |
| 90 void InitializeOnMainThread(cc::VideoFrameProvider::Client* client, |
| 91 const base::Closure& init_cb); |
| 92 |
| 93 void ReallocateVideoFrame(const gfx::Size& natural_size); |
| 94 |
| 95 void SetCurrentFrameInternal( |
| 96 const scoped_refptr<media::VideoFrame>& video_frame); |
| 97 |
| 98 // Client GL texture ID allocated to the StreamTexture. |
| 99 unsigned texture_id_; |
| 100 |
| 101 // GL texture mailbox for |texture_id_|. |
| 102 gpu::Mailbox texture_mailbox_; |
| 103 |
| 104 // Stream texture ID. |
| 105 unsigned stream_id_; |
| 106 |
| 107 // Object for calling back the compositor thread to repaint the video when a |
| 108 // frame is available. It should be bound to |compositor_task_runner_|. |
| 109 ScopedStreamTextureProxy stream_texture_proxy_; |
| 110 |
| 111 // Size of the video frames. |
| 112 gfx::Size natural_size_; |
| 113 |
| 114 scoped_refptr<StreamTextureFactory> factory_; |
| 115 |
| 116 base::Lock current_frame_lock_; |
| 117 scoped_refptr<media::VideoFrame> current_frame_; |
| 118 |
| 119 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
| 120 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_; |
| 121 |
| 122 base::WeakPtrFactory<StreamTextureWrapperImpl> weak_factory_; |
| 123 |
| 124 DISALLOW_COPY_AND_ASSIGN(StreamTextureWrapperImpl); |
| 125 }; |
| 126 |
| 127 } // namespace media |
| 128 |
| 129 #endif // CONTENT_RENDERER_MEDIA_ANDROID_STREAM_TEXTURE_WRAPPER_IMPL_H_ |
| OLD | NEW |