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 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; |
boliu
2014/09/15 17:00:29
Hmm, this crashes in lock because lock can't be de
| |
75 } | |
73 } | 76 } |
74 | 77 |
75 void StreamTextureProxyImpl::SetClient(cc::VideoFrameProvider::Client* client) { | 78 void StreamTextureProxyImpl::BindToLoop( |
76 base::AutoLock lock(client_lock_); | 79 int32 stream_id, |
77 client_ = client; | 80 cc::VideoFrameProvider::Client* client, |
81 scoped_refptr<base::MessageLoopProxy> loop) { | |
82 base::AutoLock lock(lock_); | |
83 DCHECK(loop.get()); | |
84 loop_ = loop; | |
85 | |
86 if (loop.get() != base::MessageLoopProxy::current()) { | |
87 // Unretained is safe here only because the object is deleted on |loop_| | |
88 // thread. | |
89 loop->PostTask(FROM_HERE, | |
90 base::Bind(&StreamTextureProxyImpl::BindOnThread, | |
91 base::Unretained(this), | |
92 stream_id, | |
93 client)); | |
94 } else { | |
95 client_ = client; | |
96 surface_texture_ = context_provider_->GetSurfaceTexture(stream_id); | |
97 if (!surface_texture_) { | |
98 LOG(ERROR) << "Failed to get SurfaceTexture for stream."; | |
99 return; | |
100 } | |
101 | |
102 callback_ = | |
103 base::Bind(&StreamTextureProxyImpl::OnFrameAvailable, AsWeakPtr()); | |
104 surface_texture_->SetFrameAvailableCallback(callback_); | |
105 } | |
78 } | 106 } |
79 | 107 |
80 void StreamTextureProxyImpl::BindToCurrentThread(int stream_id) { | 108 void StreamTextureProxyImpl::BindOnThread( |
81 loop_ = base::MessageLoopProxy::current(); | 109 int32 stream_id, |
110 cc::VideoFrameProvider::Client* client) { | |
111 { | |
112 base::AutoLock lock(lock_); | |
113 client_ = client; | |
114 } | |
115 | |
82 surface_texture_ = context_provider_->GetSurfaceTexture(stream_id); | 116 surface_texture_ = context_provider_->GetSurfaceTexture(stream_id); |
83 if (!surface_texture_) { | 117 if (!surface_texture_) { |
84 LOG(ERROR) << "Failed to get SurfaceTexture for stream."; | 118 LOG(ERROR) << "Failed to get SurfaceTexture for stream."; |
85 return; | 119 return; |
86 } | 120 } |
87 | 121 |
88 callback_ = | 122 callback_ = |
89 base::Bind(&StreamTextureProxyImpl::OnFrameAvailable, AsWeakPtr()); | 123 base::Bind(&StreamTextureProxyImpl::OnFrameAvailable, AsWeakPtr()); |
90 surface_texture_->SetFrameAvailableCallback(callback_); | 124 surface_texture_->SetFrameAvailableCallback(callback_); |
91 } | 125 } |
92 | 126 |
93 void StreamTextureProxyImpl::OnFrameAvailable() { | 127 void StreamTextureProxyImpl::OnFrameAvailable() { |
94 // GetTransformMatrix only returns something valid after both is true: | 128 // GetTransformMatrix only returns something valid after both is true: |
95 // - OnFrameAvailable was called | 129 // - OnFrameAvailable was called |
96 // - we called UpdateTexImage | 130 // - we called UpdateTexImage |
97 if (has_updated_) { | 131 if (has_updated_) { |
98 float matrix[16]; | 132 float matrix[16]; |
99 surface_texture_->GetTransformMatrix(matrix); | 133 surface_texture_->GetTransformMatrix(matrix); |
100 | 134 |
101 if (memcmp(current_matrix_, matrix, sizeof(matrix)) != 0) { | 135 if (memcmp(current_matrix_, matrix, sizeof(matrix)) != 0) { |
102 memcpy(current_matrix_, matrix, sizeof(matrix)); | 136 memcpy(current_matrix_, matrix, sizeof(matrix)); |
103 | 137 |
104 base::AutoLock lock(client_lock_); | 138 base::AutoLock lock(lock_); |
105 if (client_) | 139 if (client_) |
106 client_->DidUpdateMatrix(current_matrix_); | 140 client_->DidUpdateMatrix(current_matrix_); |
107 } | 141 } |
108 } | 142 } |
109 // OnFrameAvailable being called a second time implies that we called | 143 // OnFrameAvailable being called a second time implies that we called |
110 // updateTexImage since after we received the first frame. | 144 // updateTexImage since after we received the first frame. |
111 has_updated_ = true; | 145 has_updated_ = true; |
112 | 146 |
113 base::AutoLock lock(client_lock_); | 147 base::AutoLock lock(lock_); |
114 if (client_) | 148 if (client_) |
115 client_->DidReceiveFrame(); | 149 client_->DidReceiveFrame(); |
116 } | 150 } |
117 | 151 |
118 } // namespace | 152 } // namespace |
119 | 153 |
120 // static | 154 // static |
121 scoped_refptr<StreamTextureFactorySynchronousImpl> | 155 scoped_refptr<StreamTextureFactorySynchronousImpl> |
122 StreamTextureFactorySynchronousImpl::Create( | 156 StreamTextureFactorySynchronousImpl::Create( |
123 const CreateContextProviderCallback& try_create_callback, | 157 const CreateContextProviderCallback& try_create_callback, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
175 | 209 |
176 void StreamTextureFactorySynchronousImpl::SetStreamTextureSize( | 210 void StreamTextureFactorySynchronousImpl::SetStreamTextureSize( |
177 int32 stream_id, | 211 int32 stream_id, |
178 const gfx::Size& size) {} | 212 const gfx::Size& size) {} |
179 | 213 |
180 gpu::gles2::GLES2Interface* StreamTextureFactorySynchronousImpl::ContextGL() { | 214 gpu::gles2::GLES2Interface* StreamTextureFactorySynchronousImpl::ContextGL() { |
181 DCHECK(context_provider_); | 215 DCHECK(context_provider_); |
182 return context_provider_->ContextGL(); | 216 return context_provider_->ContextGL(); |
183 } | 217 } |
184 | 218 |
219 void StreamTextureFactorySynchronousImpl::AddObserver( | |
220 StreamTextureFactoryContextObserver* obs) { | |
221 context_provider_->AddObserver(obs); | |
222 } | |
223 | |
224 void StreamTextureFactorySynchronousImpl::RemoveObserver( | |
225 StreamTextureFactoryContextObserver* obs) { | |
226 context_provider_->RemoveObserver(obs); | |
227 } | |
228 | |
185 } // namespace content | 229 } // namespace content |
OLD | NEW |