| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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 #include "content/renderer/media/stream_texture_factory_impl_android.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" |
| 9 #include "base/message_loop_proxy.h" |
| 10 #include "base/synchronization/lock.h" |
| 11 #include "content/common/android/surface_texture_peer.h" |
| 12 #include "content/common/gpu/client/gpu_channel_host.h" |
| 13 #include "content/common/gpu/gpu_messages.h" |
| 14 #include "content/renderer/render_thread_impl.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebStreamTextureClien
t.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebGraphicsC
ontext3D.h" |
| 17 #include "ui/gfx/size.h" |
| 18 |
| 19 namespace { |
| 20 |
| 21 static void DeleteStreamTextureHost(content::StreamTextureHost* host) { |
| 22 delete host; |
| 23 } |
| 24 |
| 25 // Implementation of the StreamTextureProxy class. This class listens to all |
| 26 // the stream texture updates and forward them to the WebStreamTextureClient. |
| 27 class StreamTextureProxyImpl : public webkit_media::StreamTextureProxy, |
| 28 public content::StreamTextureHost::Listener { |
| 29 public: |
| 30 explicit StreamTextureProxyImpl(content::StreamTextureHost* host); |
| 31 virtual ~StreamTextureProxyImpl(); |
| 32 |
| 33 // webkit_media::StreamTextureProxy implementation: |
| 34 virtual bool Initialize(int stream_id, int width, int height) OVERRIDE; |
| 35 virtual bool IsInitialized() OVERRIDE { return initialized_; } |
| 36 virtual void SetClient(WebKit::WebStreamTextureClient* client) OVERRIDE; |
| 37 |
| 38 // StreamTextureHost::Listener implementation: |
| 39 virtual void OnFrameAvailable() OVERRIDE; |
| 40 virtual void OnMatrixChanged(const float matrix[16]) OVERRIDE; |
| 41 |
| 42 private: |
| 43 scoped_ptr<content::StreamTextureHost> host_; |
| 44 scoped_refptr<base::MessageLoopProxy> loop_; |
| 45 |
| 46 base::Lock client_lock_; |
| 47 WebKit::WebStreamTextureClient* client_; |
| 48 bool initialized_; |
| 49 |
| 50 DISALLOW_COPY_AND_ASSIGN(StreamTextureProxyImpl); |
| 51 }; |
| 52 |
| 53 StreamTextureProxyImpl::StreamTextureProxyImpl( |
| 54 content::StreamTextureHost* host) |
| 55 : host_(host), |
| 56 client_(NULL), |
| 57 initialized_(false) { |
| 58 DCHECK(host); |
| 59 host->SetListener(this); |
| 60 } |
| 61 |
| 62 StreamTextureProxyImpl::~StreamTextureProxyImpl() { |
| 63 SetClient(NULL); |
| 64 // The StreamTextureHost instance needs to be deleted on the thread |
| 65 // it receives messages on (where it uses a WeakPtr). |
| 66 if (loop_.get()) { |
| 67 loop_->PostTask(FROM_HERE, base::Bind(&DeleteStreamTextureHost, |
| 68 host_.release())); |
| 69 } |
| 70 } |
| 71 |
| 72 void StreamTextureProxyImpl::SetClient(WebKit::WebStreamTextureClient* client) { |
| 73 base::AutoLock lock(client_lock_); |
| 74 client_ = client; |
| 75 } |
| 76 |
| 77 bool StreamTextureProxyImpl::Initialize(int stream_id, int width, int height) { |
| 78 loop_ = base::MessageLoopProxy::current(); |
| 79 initialized_ = true; |
| 80 return host_->Initialize(stream_id, gfx::Size(width, height)); |
| 81 } |
| 82 |
| 83 void StreamTextureProxyImpl::OnFrameAvailable() { |
| 84 base::AutoLock lock(client_lock_); |
| 85 if (client_) |
| 86 client_->didReceiveFrame(); |
| 87 } |
| 88 |
| 89 void StreamTextureProxyImpl::OnMatrixChanged(const float matrix[16]) { |
| 90 base::AutoLock lock(client_lock_); |
| 91 if (client_) |
| 92 client_->didUpdateMatrix(matrix); |
| 93 } |
| 94 |
| 95 } // anonymous namespace |
| 96 |
| 97 namespace content { |
| 98 |
| 99 StreamTextureFactoryImpl::StreamTextureFactoryImpl( |
| 100 WebKit::WebGraphicsContext3D* context, |
| 101 GpuChannelHost* channel, |
| 102 int view_id) |
| 103 : context_(context), |
| 104 channel_(channel), |
| 105 view_id_(view_id) { |
| 106 DCHECK(context_); |
| 107 DCHECK(channel); |
| 108 } |
| 109 |
| 110 StreamTextureFactoryImpl::~StreamTextureFactoryImpl() { |
| 111 } |
| 112 |
| 113 webkit_media::StreamTextureProxy* StreamTextureFactoryImpl::CreateProxy() { |
| 114 DCHECK(channel_.get()); |
| 115 StreamTextureHost* host = new StreamTextureHost(channel_.get()); |
| 116 return new StreamTextureProxyImpl(host); |
| 117 } |
| 118 |
| 119 void StreamTextureFactoryImpl::EstablishPeer(int stream_id, int player_id) { |
| 120 DCHECK(channel_.get()); |
| 121 channel_->Send(new GpuChannelMsg_EstablishStreamTexture( |
| 122 stream_id, SurfaceTexturePeer::SET_VIDEO_SURFACE_TEXTURE, |
| 123 view_id_, player_id)); |
| 124 } |
| 125 |
| 126 unsigned StreamTextureFactoryImpl::CreateStreamTexture(unsigned* texture_id) { |
| 127 unsigned stream_id = 0; |
| 128 if (context_->makeContextCurrent()) { |
| 129 *texture_id = context_->createTexture(); |
| 130 // TODO(qinmin): upstream the implementation of |
| 131 // WebGraphicsContext::createStreamTextureCHROMIUM(). |
| 132 // stream_id = context_->createStreamTextureCHROMIUM(*texture_id); |
| 133 context_->flush(); |
| 134 } |
| 135 return stream_id; |
| 136 } |
| 137 |
| 138 void StreamTextureFactoryImpl::DestroyStreamTexture(unsigned texture_id) { |
| 139 if (context_->makeContextCurrent()) { |
| 140 // TODO(qinmin): upstream the implementation of |
| 141 // WebGraphicsContext::destroyStreamTextureCHROMIUM(). |
| 142 // context_->destroyStreamTextureCHROMIUM(texture_id); |
| 143 context_->deleteTexture(texture_id); |
| 144 context_->flush(); |
| 145 } |
| 146 } |
| 147 |
| 148 } // namespace content |
| OLD | NEW |