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/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/public/platform/WebGraphicsContext3D.h" | |
16 #include "ui/gfx/size.h" | |
17 | |
18 namespace { | |
19 | |
20 // Implementation of the StreamTextureProxy class. This class listens to all | |
21 // the stream texture updates and forward them to the | |
22 // cc::VideoFrameProvider::Client. | |
23 class StreamTextureProxyImpl : public webkit_media::StreamTextureProxy, | |
24 public content::StreamTextureHost::Listener { | |
25 public: | |
26 explicit StreamTextureProxyImpl(content::StreamTextureHost* host); | |
27 virtual ~StreamTextureProxyImpl() {} | |
28 | |
29 // webkit_media::StreamTextureProxy implementation: | |
30 virtual void BindToCurrentThread( | |
31 int stream_id, int width, int height) OVERRIDE; | |
32 virtual bool IsBoundToThread() OVERRIDE { return !!loop_.get(); } | |
33 virtual void SetClient(cc::VideoFrameProvider::Client* client) OVERRIDE; | |
34 virtual void Release() OVERRIDE; | |
35 | |
36 // StreamTextureHost::Listener implementation: | |
37 virtual void OnFrameAvailable() OVERRIDE; | |
38 virtual void OnMatrixChanged(const float matrix[16]) OVERRIDE; | |
39 | |
40 private: | |
41 scoped_ptr<content::StreamTextureHost> host_; | |
42 scoped_refptr<base::MessageLoopProxy> loop_; | |
43 | |
44 base::Lock client_lock_; | |
45 cc::VideoFrameProvider::Client* client_; | |
46 | |
47 DISALLOW_COPY_AND_ASSIGN(StreamTextureProxyImpl); | |
48 }; | |
49 | |
50 StreamTextureProxyImpl::StreamTextureProxyImpl( | |
51 content::StreamTextureHost* host) | |
52 : host_(host), | |
53 client_(NULL) { | |
54 DCHECK(host); | |
55 host->SetListener(this); | |
56 } | |
57 | |
58 void StreamTextureProxyImpl::Release() { | |
59 SetClient(NULL); | |
60 if (loop_.get() && loop_.get() != base::MessageLoopProxy::current()) | |
61 loop_->DeleteSoon(FROM_HERE, this); | |
62 else | |
63 delete this; | |
64 } | |
65 | |
66 void StreamTextureProxyImpl::SetClient(cc::VideoFrameProvider::Client* client) { | |
67 base::AutoLock lock(client_lock_); | |
68 client_ = client; | |
69 } | |
70 | |
71 void StreamTextureProxyImpl::BindToCurrentThread( | |
72 int stream_id, int width, int height) { | |
73 loop_ = base::MessageLoopProxy::current(); | |
74 host_->Initialize(stream_id, gfx::Size(width, height)); | |
75 } | |
76 | |
77 void StreamTextureProxyImpl::OnFrameAvailable() { | |
78 base::AutoLock lock(client_lock_); | |
79 if (client_) | |
80 client_->DidReceiveFrame(); | |
81 } | |
82 | |
83 void StreamTextureProxyImpl::OnMatrixChanged(const float matrix[16]) { | |
84 base::AutoLock lock(client_lock_); | |
85 if (client_) | |
86 client_->DidUpdateMatrix(matrix); | |
87 } | |
88 | |
89 } // anonymous namespace | |
90 | |
91 namespace content { | |
92 | |
93 StreamTextureFactoryImpl::StreamTextureFactoryImpl( | |
94 WebKit::WebGraphicsContext3D* context, | |
95 GpuChannelHost* channel, | |
96 int view_id) | |
97 : context_(context), | |
98 channel_(channel), | |
99 view_id_(view_id) { | |
100 DCHECK(context_); | |
101 DCHECK(channel); | |
102 } | |
103 | |
104 StreamTextureFactoryImpl::~StreamTextureFactoryImpl() { | |
105 } | |
106 | |
107 webkit_media::StreamTextureProxy* StreamTextureFactoryImpl::CreateProxy() { | |
108 DCHECK(channel_.get()); | |
109 StreamTextureHost* host = new StreamTextureHost(channel_.get()); | |
110 return new StreamTextureProxyImpl(host); | |
111 } | |
112 | |
113 void StreamTextureFactoryImpl::EstablishPeer(int stream_id, int player_id) { | |
114 DCHECK(channel_.get()); | |
115 channel_->Send(new GpuChannelMsg_EstablishStreamTexture( | |
116 stream_id, view_id_, player_id)); | |
117 } | |
118 | |
119 unsigned StreamTextureFactoryImpl::CreateStreamTexture(unsigned* texture_id) { | |
120 unsigned stream_id = 0; | |
121 if (context_->makeContextCurrent()) { | |
122 *texture_id = context_->createTexture(); | |
123 stream_id = context_->createStreamTextureCHROMIUM(*texture_id); | |
124 context_->flush(); | |
125 } | |
126 return stream_id; | |
127 } | |
128 | |
129 void StreamTextureFactoryImpl::DestroyStreamTexture(unsigned texture_id) { | |
130 if (context_->makeContextCurrent()) { | |
131 context_->destroyStreamTextureCHROMIUM(texture_id); | |
132 context_->deleteTexture(texture_id); | |
133 context_->flush(); | |
134 } | |
135 } | |
136 | |
137 } // namespace content | |
OLD | NEW |