Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/media/android/stream_texture_factory_synchronous_impl .h" | 5 #include "content/renderer/media/android/stream_texture_factory_synchronous_impl .h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 | 27 |
| 28 class StreamTextureProxyImpl | 28 class StreamTextureProxyImpl |
| 29 : public StreamTextureProxy, | 29 : public StreamTextureProxy, |
| 30 public base::SupportsWeakPtr<StreamTextureProxyImpl> { | 30 public base::SupportsWeakPtr<StreamTextureProxyImpl> { |
| 31 public: | 31 public: |
| 32 explicit StreamTextureProxyImpl( | 32 explicit StreamTextureProxyImpl( |
| 33 StreamTextureFactorySynchronousImpl::ContextProvider* provider); | 33 StreamTextureFactorySynchronousImpl::ContextProvider* provider); |
| 34 virtual ~StreamTextureProxyImpl(); | 34 virtual ~StreamTextureProxyImpl(); |
| 35 | 35 |
| 36 // StreamTextureProxy implementation: | 36 // StreamTextureProxy implementation: |
| 37 virtual void BindToCurrentThread(int32 stream_id) OVERRIDE; | 37 virtual void BindToLoop(int32 stream_id, |
| 38 virtual void SetClient(cc::VideoFrameProvider::Client* client) OVERRIDE; | 38 cc::VideoFrameProvider::Client* client, |
| 39 scoped_refptr<base::MessageLoopProxy> loop) OVERRIDE; | |
| 39 virtual void Release() OVERRIDE; | 40 virtual void Release() OVERRIDE; |
| 40 | 41 |
| 41 private: | 42 private: |
| 43 void SetClient(cc::VideoFrameProvider::Client* client); | |
| 44 void BindOnThread(int32 stream_id, | |
| 45 scoped_refptr<base::MessageLoopProxy> loop); | |
| 42 void OnFrameAvailable(); | 46 void OnFrameAvailable(); |
| 43 | 47 |
| 44 scoped_refptr<base::MessageLoopProxy> loop_; | |
| 45 base::Lock client_lock_; | 48 base::Lock client_lock_; |
| 46 cc::VideoFrameProvider::Client* client_; | 49 cc::VideoFrameProvider::Client* client_; |
| 50 | |
| 51 // Accessed on the |loop_| thread only. | |
| 52 scoped_refptr<base::MessageLoopProxy> loop_; | |
| 47 base::Closure callback_; | 53 base::Closure callback_; |
| 48 | |
| 49 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider> | 54 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider> |
| 50 context_provider_; | 55 context_provider_; |
| 51 scoped_refptr<gfx::SurfaceTexture> surface_texture_; | 56 scoped_refptr<gfx::SurfaceTexture> surface_texture_; |
| 52 | |
| 53 float current_matrix_[16]; | 57 float current_matrix_[16]; |
| 54 bool has_updated_; | 58 bool has_updated_; |
| 55 | 59 |
| 56 DISALLOW_IMPLICIT_CONSTRUCTORS(StreamTextureProxyImpl); | 60 DISALLOW_IMPLICIT_CONSTRUCTORS(StreamTextureProxyImpl); |
| 57 }; | 61 }; |
| 58 | 62 |
| 59 StreamTextureProxyImpl::StreamTextureProxyImpl( | 63 StreamTextureProxyImpl::StreamTextureProxyImpl( |
| 60 StreamTextureFactorySynchronousImpl::ContextProvider* provider) | 64 StreamTextureFactorySynchronousImpl::ContextProvider* provider) |
| 61 : context_provider_(provider), has_updated_(false) { | 65 : client_(NULL), context_provider_(provider), has_updated_(false) { |
| 62 std::fill(current_matrix_, current_matrix_ + 16, 0); | 66 std::fill(current_matrix_, current_matrix_ + 16, 0); |
| 63 } | 67 } |
| 64 | 68 |
| 65 StreamTextureProxyImpl::~StreamTextureProxyImpl() {} | 69 StreamTextureProxyImpl::~StreamTextureProxyImpl() {} |
| 66 | 70 |
| 67 void StreamTextureProxyImpl::Release() { | 71 void StreamTextureProxyImpl::Release() { |
| 72 // Assumes this is the last reference to this object. So no need to acquire | |
| 73 // lock. | |
| 68 SetClient(NULL); | 74 SetClient(NULL); |
| 69 if (loop_.get() && !loop_->BelongsToCurrentThread()) | 75 if (!loop_.get() || loop_->BelongsToCurrentThread() || |
| 70 loop_->DeleteSoon(FROM_HERE, this); | 76 !loop_->DeleteSoon(FROM_HERE, this)) { |
| 71 else | |
| 72 delete this; | 77 delete this; |
| 78 } | |
| 73 } | 79 } |
| 74 | 80 |
| 75 void StreamTextureProxyImpl::SetClient(cc::VideoFrameProvider::Client* client) { | 81 void StreamTextureProxyImpl::SetClient(cc::VideoFrameProvider::Client* client) { |
| 76 base::AutoLock lock(client_lock_); | 82 base::AutoLock lock(client_lock_); |
| 77 client_ = client; | 83 client_ = client; |
| 78 } | 84 } |
| 79 | 85 |
| 80 void StreamTextureProxyImpl::BindToCurrentThread(int stream_id) { | 86 void StreamTextureProxyImpl::BindToLoop( |
| 81 loop_ = base::MessageLoopProxy::current(); | 87 int32 stream_id, |
| 88 cc::VideoFrameProvider::Client* client, | |
| 89 scoped_refptr<base::MessageLoopProxy> loop) { | |
| 90 DCHECK(loop); | |
| 91 SetClient(client); | |
| 92 if (loop->BelongsToCurrentThread()) { | |
| 93 BindOnThread(stream_id, loop); | |
| 94 return; | |
| 95 } | |
| 96 // Unretained is safe here only because the object is deleted on |loop_| | |
| 97 // thread. | |
| 98 loop->PostTask(FROM_HERE, | |
| 99 base::Bind(&StreamTextureProxyImpl::BindOnThread, | |
| 100 base::Unretained(this), | |
| 101 stream_id, | |
| 102 loop)); | |
| 103 } | |
| 104 | |
| 105 void StreamTextureProxyImpl::BindOnThread( | |
| 106 int32 stream_id, | |
| 107 scoped_refptr<base::MessageLoopProxy> loop) { | |
| 108 DCHECK(!loop_ || (loop == loop_)); | |
| 109 loop_ = loop; | |
| 110 | |
| 82 surface_texture_ = context_provider_->GetSurfaceTexture(stream_id); | 111 surface_texture_ = context_provider_->GetSurfaceTexture(stream_id); |
| 83 if (!surface_texture_) { | 112 if (!surface_texture_) { |
| 84 LOG(ERROR) << "Failed to get SurfaceTexture for stream."; | 113 LOG(ERROR) << "Failed to get SurfaceTexture for stream."; |
| 85 return; | 114 return; |
| 86 } | 115 } |
| 87 | 116 |
| 88 callback_ = | 117 callback_ = |
| 89 base::Bind(&StreamTextureProxyImpl::OnFrameAvailable, AsWeakPtr()); | 118 base::Bind(&StreamTextureProxyImpl::OnFrameAvailable, AsWeakPtr()); |
| 90 surface_texture_->SetFrameAvailableCallback(callback_); | 119 surface_texture_->SetFrameAvailableCallback(callback_); |
| 91 } | 120 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 123 const CreateContextProviderCallback& try_create_callback, | 152 const CreateContextProviderCallback& try_create_callback, |
| 124 int frame_id) { | 153 int frame_id) { |
| 125 return new StreamTextureFactorySynchronousImpl(try_create_callback, frame_id); | 154 return new StreamTextureFactorySynchronousImpl(try_create_callback, frame_id); |
| 126 } | 155 } |
| 127 | 156 |
| 128 StreamTextureFactorySynchronousImpl::StreamTextureFactorySynchronousImpl( | 157 StreamTextureFactorySynchronousImpl::StreamTextureFactorySynchronousImpl( |
| 129 const CreateContextProviderCallback& try_create_callback, | 158 const CreateContextProviderCallback& try_create_callback, |
| 130 int frame_id) | 159 int frame_id) |
| 131 : create_context_provider_callback_(try_create_callback), | 160 : create_context_provider_callback_(try_create_callback), |
| 132 context_provider_(create_context_provider_callback_.Run()), | 161 context_provider_(create_context_provider_callback_.Run()), |
| 133 frame_id_(frame_id) {} | 162 frame_id_(frame_id), |
| 163 observer_(NULL) {} | |
| 134 | 164 |
| 135 StreamTextureFactorySynchronousImpl::~StreamTextureFactorySynchronousImpl() {} | 165 StreamTextureFactorySynchronousImpl::~StreamTextureFactorySynchronousImpl() {} |
| 136 | 166 |
| 137 StreamTextureProxy* StreamTextureFactorySynchronousImpl::CreateProxy() { | 167 StreamTextureProxy* StreamTextureFactorySynchronousImpl::CreateProxy() { |
| 138 if (!context_provider_) | 168 if (!context_provider_) |
| 139 context_provider_ = create_context_provider_callback_.Run(); | 169 context_provider_ = create_context_provider_callback_.Run(); |
| 140 | 170 |
| 141 if (!context_provider_) | 171 if (!context_provider_) |
| 142 return NULL; | 172 return NULL; |
| 173 | |
| 174 if (observer_) | |
| 175 context_provider_->AddObserver(observer_); | |
|
no sievers
2014/09/17 01:29:52
Beware: ObserverList has a NOTREACHED() << "Observ
boliu
2014/09/17 02:09:03
Should be fine, context_provider_ is created only
| |
| 143 return new StreamTextureProxyImpl(context_provider_); | 176 return new StreamTextureProxyImpl(context_provider_); |
| 144 } | 177 } |
| 145 | 178 |
| 146 void StreamTextureFactorySynchronousImpl::EstablishPeer(int32 stream_id, | 179 void StreamTextureFactorySynchronousImpl::EstablishPeer(int32 stream_id, |
| 147 int player_id) { | 180 int player_id) { |
| 148 DCHECK(context_provider_); | 181 DCHECK(context_provider_); |
| 149 scoped_refptr<gfx::SurfaceTexture> surface_texture = | 182 scoped_refptr<gfx::SurfaceTexture> surface_texture = |
| 150 context_provider_->GetSurfaceTexture(stream_id); | 183 context_provider_->GetSurfaceTexture(stream_id); |
| 151 if (surface_texture) { | 184 if (surface_texture) { |
| 152 SurfaceTexturePeer::GetInstance()->EstablishSurfaceTexturePeer( | 185 SurfaceTexturePeer::GetInstance()->EstablishSurfaceTexturePeer( |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 175 | 208 |
| 176 void StreamTextureFactorySynchronousImpl::SetStreamTextureSize( | 209 void StreamTextureFactorySynchronousImpl::SetStreamTextureSize( |
| 177 int32 stream_id, | 210 int32 stream_id, |
| 178 const gfx::Size& size) {} | 211 const gfx::Size& size) {} |
| 179 | 212 |
| 180 gpu::gles2::GLES2Interface* StreamTextureFactorySynchronousImpl::ContextGL() { | 213 gpu::gles2::GLES2Interface* StreamTextureFactorySynchronousImpl::ContextGL() { |
| 181 DCHECK(context_provider_); | 214 DCHECK(context_provider_); |
| 182 return context_provider_->ContextGL(); | 215 return context_provider_->ContextGL(); |
| 183 } | 216 } |
| 184 | 217 |
| 218 void StreamTextureFactorySynchronousImpl::AddObserver( | |
| 219 StreamTextureFactoryContextObserver* obs) { | |
| 220 DCHECK(!observer_); | |
| 221 observer_ = obs; | |
| 222 if (context_provider_) | |
| 223 context_provider_->AddObserver(obs); | |
| 224 } | |
| 225 | |
| 226 void StreamTextureFactorySynchronousImpl::RemoveObserver( | |
| 227 StreamTextureFactoryContextObserver* obs) { | |
| 228 DCHECK_EQ(observer_, obs); | |
| 229 observer_ = NULL; | |
| 230 if (context_provider_) | |
| 231 context_provider_->AddObserver(obs); | |
| 232 } | |
| 233 | |
| 185 } // namespace content | 234 } // namespace content |
| OLD | NEW |