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/renderer/media/android/stream_texture_factory.h" |
| 11 #include "gpu/command_buffer/common/mailbox.h" |
| 12 #include "media/base/android/stream_texture_wrapper.h" |
| 13 #include "media/base/video_frame.h" |
| 14 |
| 15 namespace content { |
| 16 |
| 17 // Concrete implementation of SurfaceTextureFrameProvider, backed by a |
| 18 // StreamTexture living in the GPU process. |
| 19 // |
| 20 // A SurfaceTexture is an Android primitive, joining an Android Surface (the |
| 21 // producer side, used by MediaPlayer for example) with a GL Texture (the |
| 22 // consumer side, used by our compositor). Its documentation can be found here: |
| 23 // https://developer.android.com/reference/android/graphics/SurfaceTexture.html |
| 24 // |
| 25 // The StreamTexture is an abstraction allowing Chrome to wrap a SurfaceTexture |
| 26 // living in the GPU process. It allows VideoFrames to be created from the |
| 27 // SurfaceTexture's texture, in the Renderer process. |
| 28 // |
| 29 // The general idea behind our use of StreamTexture is as follows: |
| 30 // - We create a client GL texture in the Renderer process. |
| 31 // - We request the creation of a StreamTexture via the StreamTextureFactory, |
| 32 // passing the client texture ID. The call is sent to the GPU process via the |
| 33 // CommandBuffer. The "platform" GL texture reference associated with the client |
| 34 // texture ID is looked up in the TextureManager. A StreamTexture is then |
| 35 // created, wrapping a SurfaceTexture created from the texture reference. The |
| 36 // SurfaceTexture's OnFrameAvailable() callback is tied to StreamTexture's |
| 37 // OnFrameAvailable(), which fires an IPC accross the GPU channel. |
| 38 // - We create a StreamTextureProxy in the Renderer process which listens for |
| 39 // the IPC fired by the StreamTexture's OnFrameAvailable() callback. |
| 40 // - We bind the StreamTextureProxy's lifetime to the |compositor_task_runner_|. |
| 41 // - We wrap the client texture into a VideoFrame. |
| 42 // - When the SurfaceTexture's OnFrameAvailable() callback is fired (and routed |
| 43 // to the StreamTextureProxy living on the compositor thread), we notify |
| 44 // |client_| that a new frame is available, via the DidReceiveFrame() callback. |
| 45 // |
| 46 // N.B: Technically, we only create one VideoFrame, but pretend it is a new one |
| 47 // and ask the compositor to re-paint it whenever the underlying texture's data |
| 48 // has changed. |
| 49 // |
| 50 // TODO(tguilbert): Register the underlying SurfaceTexture for retrieval in the |
| 51 // browser process. See crbug.com/627658. |
| 52 // |
| 53 class StreamTextureWrapperImpl : public media::StreamTextureWrapper { |
| 54 public: |
| 55 StreamTextureWrapperImpl( |
| 56 scoped_refptr<StreamTextureFactory> factory, |
| 57 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner); |
| 58 virtual ~StreamTextureWrapperImpl(); |
| 59 |
| 60 // StreamTextureWrapper implementation. |
| 61 void Initialize( |
| 62 cc::VideoFrameProvider::Client* client, |
| 63 const gfx::Size& natural_size, |
| 64 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, |
| 65 const base::Closure& init_cb) override; |
| 66 void UpdateTextureSize(const gfx::Size& natural_size) override; |
| 67 scoped_refptr<media::VideoFrame> GetCurrentFrame() override; |
| 68 |
| 69 private: |
| 70 void InitializeInternal(const base::Closure& init_cb); |
| 71 |
| 72 void ReallocateVideoFrame(const gfx::Size& natural_size); |
| 73 |
| 74 void SetCurrentFrameInternal(scoped_refptr<media::VideoFrame>& video_frame); |
| 75 |
| 76 // GL texture ID allocated to the video. |
| 77 unsigned int texture_id_; |
| 78 |
| 79 // GL texture mailbox for |texture_id_| to provide in the VideoFrame, and sync |
| 80 // point for when the mailbox was produced. |
| 81 gpu::Mailbox texture_mailbox_; |
| 82 |
| 83 // Stream texture ID allocated to the video. |
| 84 unsigned int stream_id_; |
| 85 |
| 86 // Object for calling back the compositor thread to repaint the video when a |
| 87 // frame available. It should be bound to the compositor thread. |
| 88 ScopedStreamTextureProxy stream_texture_proxy_; |
| 89 |
| 90 cc::VideoFrameProvider::Client* client_; |
| 91 |
| 92 // Size of the allocated underlying texture. |
| 93 gfx::Size natural_size_; |
| 94 |
| 95 scoped_refptr<StreamTextureFactory> factory_; |
| 96 |
| 97 base::Lock current_frame_lock_; |
| 98 scoped_refptr<media::VideoFrame> current_frame_; |
| 99 |
| 100 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
| 101 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; |
| 102 }; |
| 103 |
| 104 } // namespace media |
| 105 |
| 106 #endif // _CONTENT_RENDERER_MEDIA_ANDROID_STREAM_TEXTURE_WRAPPER_IMPL_H_ |
OLD | NEW |