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 Bind(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 BindOnThread(int32 stream_id, cc::VideoFrameProvider::Client* client); |
42 void OnFrameAvailable(); | 44 void OnFrameAvailable(); |
43 | 45 |
| 46 base::Lock lock_; |
44 scoped_refptr<base::MessageLoopProxy> loop_; | 47 scoped_refptr<base::MessageLoopProxy> loop_; |
45 base::Lock client_lock_; | |
46 cc::VideoFrameProvider::Client* client_; | 48 cc::VideoFrameProvider::Client* client_; |
| 49 |
| 50 // Accessed on the |loop_| thread only. |
47 base::Closure callback_; | 51 base::Closure callback_; |
48 | |
49 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider> | 52 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider> |
50 context_provider_; | 53 context_provider_; |
51 scoped_refptr<gfx::SurfaceTexture> surface_texture_; | 54 scoped_refptr<gfx::SurfaceTexture> surface_texture_; |
52 | |
53 float current_matrix_[16]; | 55 float current_matrix_[16]; |
54 bool has_updated_; | 56 bool has_updated_; |
55 | 57 |
56 DISALLOW_IMPLICIT_CONSTRUCTORS(StreamTextureProxyImpl); | 58 DISALLOW_IMPLICIT_CONSTRUCTORS(StreamTextureProxyImpl); |
57 }; | 59 }; |
58 | 60 |
59 StreamTextureProxyImpl::StreamTextureProxyImpl( | 61 StreamTextureProxyImpl::StreamTextureProxyImpl( |
60 StreamTextureFactorySynchronousImpl::ContextProvider* provider) | 62 StreamTextureFactorySynchronousImpl::ContextProvider* provider) |
61 : context_provider_(provider), has_updated_(false) { | 63 : client_(NULL), context_provider_(provider), has_updated_(false) { |
62 std::fill(current_matrix_, current_matrix_ + 16, 0); | 64 std::fill(current_matrix_, current_matrix_ + 16, 0); |
63 } | 65 } |
64 | 66 |
65 StreamTextureProxyImpl::~StreamTextureProxyImpl() {} | 67 StreamTextureProxyImpl::~StreamTextureProxyImpl() {} |
66 | 68 |
67 void StreamTextureProxyImpl::Release() { | 69 void StreamTextureProxyImpl::Release() { |
68 SetClient(NULL); | 70 base::AutoLock lock(lock_); |
69 if (loop_.get() && !loop_->BelongsToCurrentThread()) | 71 client_ = NULL; |
70 loop_->DeleteSoon(FROM_HERE, this); | 72 if (!loop_.get() || loop_.get() != base::MessageLoopProxy::current() || |
71 else | 73 !loop_->DeleteSoon(FROM_HERE, this)) { |
72 delete this; | 74 delete this; |
| 75 } |
73 } | 76 } |
74 | 77 |
75 void StreamTextureProxyImpl::SetClient(cc::VideoFrameProvider::Client* client) { | 78 void StreamTextureProxyImpl::Bind(int32 stream_id, |
76 base::AutoLock lock(client_lock_); | 79 cc::VideoFrameProvider::Client* client, |
77 client_ = client; | 80 scoped_refptr<base::MessageLoopProxy> loop) { |
| 81 base::AutoLock lock(lock_); |
| 82 DCHECK(loop.get()); |
| 83 loop_ = loop; |
| 84 |
| 85 if (loop.get() != base::MessageLoopProxy::current()) { |
| 86 // Unretained is safe here only because the object is deleted on |loop_| |
| 87 // thread. |
| 88 loop->PostTask(FROM_HERE, |
| 89 base::Bind(&StreamTextureProxyImpl::BindOnThread, |
| 90 base::Unretained(this), |
| 91 stream_id, |
| 92 client)); |
| 93 } else { |
| 94 client_ = client; |
| 95 surface_texture_ = context_provider_->GetSurfaceTexture(stream_id); |
| 96 if (!surface_texture_) { |
| 97 LOG(ERROR) << "Failed to get SurfaceTexture for stream."; |
| 98 return; |
| 99 } |
| 100 |
| 101 callback_ = |
| 102 base::Bind(&StreamTextureProxyImpl::OnFrameAvailable, AsWeakPtr()); |
| 103 surface_texture_->SetFrameAvailableCallback(callback_); |
| 104 } |
78 } | 105 } |
79 | 106 |
80 void StreamTextureProxyImpl::BindToCurrentThread(int stream_id) { | 107 void StreamTextureProxyImpl::BindOnThread( |
81 loop_ = base::MessageLoopProxy::current(); | 108 int32 stream_id, |
| 109 cc::VideoFrameProvider::Client* client) { |
| 110 { |
| 111 base::AutoLock lock(lock_); |
| 112 client_ = client; |
| 113 } |
| 114 |
82 surface_texture_ = context_provider_->GetSurfaceTexture(stream_id); | 115 surface_texture_ = context_provider_->GetSurfaceTexture(stream_id); |
83 if (!surface_texture_) { | 116 if (!surface_texture_) { |
84 LOG(ERROR) << "Failed to get SurfaceTexture for stream."; | 117 LOG(ERROR) << "Failed to get SurfaceTexture for stream."; |
85 return; | 118 return; |
86 } | 119 } |
87 | 120 |
88 callback_ = | 121 callback_ = |
89 base::Bind(&StreamTextureProxyImpl::OnFrameAvailable, AsWeakPtr()); | 122 base::Bind(&StreamTextureProxyImpl::OnFrameAvailable, AsWeakPtr()); |
90 surface_texture_->SetFrameAvailableCallback(callback_); | 123 surface_texture_->SetFrameAvailableCallback(callback_); |
91 } | 124 } |
92 | 125 |
93 void StreamTextureProxyImpl::OnFrameAvailable() { | 126 void StreamTextureProxyImpl::OnFrameAvailable() { |
94 // GetTransformMatrix only returns something valid after both is true: | 127 // GetTransformMatrix only returns something valid after both is true: |
95 // - OnFrameAvailable was called | 128 // - OnFrameAvailable was called |
96 // - we called UpdateTexImage | 129 // - we called UpdateTexImage |
97 if (has_updated_) { | 130 if (has_updated_) { |
98 float matrix[16]; | 131 float matrix[16]; |
99 surface_texture_->GetTransformMatrix(matrix); | 132 surface_texture_->GetTransformMatrix(matrix); |
100 | 133 |
101 if (memcmp(current_matrix_, matrix, sizeof(matrix)) != 0) { | 134 if (memcmp(current_matrix_, matrix, sizeof(matrix)) != 0) { |
102 memcpy(current_matrix_, matrix, sizeof(matrix)); | 135 memcpy(current_matrix_, matrix, sizeof(matrix)); |
103 | 136 |
104 base::AutoLock lock(client_lock_); | 137 base::AutoLock lock(lock_); |
105 if (client_) | 138 if (client_) |
106 client_->DidUpdateMatrix(current_matrix_); | 139 client_->DidUpdateMatrix(current_matrix_); |
107 } | 140 } |
108 } | 141 } |
109 // OnFrameAvailable being called a second time implies that we called | 142 // OnFrameAvailable being called a second time implies that we called |
110 // updateTexImage since after we received the first frame. | 143 // updateTexImage since after we received the first frame. |
111 has_updated_ = true; | 144 has_updated_ = true; |
112 | 145 |
113 base::AutoLock lock(client_lock_); | 146 base::AutoLock lock(lock_); |
114 if (client_) | 147 if (client_) |
115 client_->DidReceiveFrame(); | 148 client_->DidReceiveFrame(); |
116 } | 149 } |
117 | 150 |
118 } // namespace | 151 } // namespace |
119 | 152 |
120 // static | 153 // static |
121 scoped_refptr<StreamTextureFactorySynchronousImpl> | 154 scoped_refptr<StreamTextureFactorySynchronousImpl> |
122 StreamTextureFactorySynchronousImpl::Create( | 155 StreamTextureFactorySynchronousImpl::Create( |
123 const CreateContextProviderCallback& try_create_callback, | 156 const CreateContextProviderCallback& try_create_callback, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 context_provider_->AddObserver(obs); |
| 221 } |
| 222 |
| 223 void StreamTextureFactorySynchronousImpl::RemoveObserver( |
| 224 StreamTextureFactoryContextObserver* obs) { |
| 225 context_provider_->RemoveObserver(obs); |
| 226 } |
| 227 |
185 } // namespace content | 228 } // namespace content |
OLD | NEW |