Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: content/renderer/media/android/stream_texture_factory_synchronous_impl.cc

Issue 575293003: Fix threading issue in StreamTextureProxyImpl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 22 matching lines...) Expand all
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 BindToLoop(int32 stream_id, 37 virtual void BindToLoop(int32 stream_id,
38 cc::VideoFrameProvider::Client* client, 38 cc::VideoFrameProvider::Client* client,
39 scoped_refptr<base::MessageLoopProxy> loop) OVERRIDE; 39 scoped_refptr<base::MessageLoopProxy> loop) OVERRIDE;
40 virtual void Release() OVERRIDE; 40 virtual void Release() OVERRIDE;
41 41
42 private: 42 private:
43 void SetClient(cc::VideoFrameProvider::Client* client); 43 void BindOnThread(int32 stream_id);
44 void BindOnThread(int32 stream_id,
45 scoped_refptr<base::MessageLoopProxy> loop);
46 void OnFrameAvailable(); 44 void OnFrameAvailable();
47 45
48 base::Lock client_lock_; 46 base::Lock lock_;
49 cc::VideoFrameProvider::Client* client_; 47 cc::VideoFrameProvider::Client* client_;
48 scoped_refptr<base::MessageLoopProxy> loop_;
50 49
51 // Accessed on the |loop_| thread only. 50 // Accessed on the |loop_| thread only.
52 scoped_refptr<base::MessageLoopProxy> loop_;
53 base::Closure callback_; 51 base::Closure callback_;
54 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider> 52 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider>
55 context_provider_; 53 context_provider_;
56 scoped_refptr<gfx::SurfaceTexture> surface_texture_; 54 scoped_refptr<gfx::SurfaceTexture> surface_texture_;
57 float current_matrix_[16]; 55 float current_matrix_[16];
58 bool has_updated_; 56 bool has_updated_;
59 57
60 DISALLOW_IMPLICIT_CONSTRUCTORS(StreamTextureProxyImpl); 58 DISALLOW_IMPLICIT_CONSTRUCTORS(StreamTextureProxyImpl);
61 }; 59 };
62 60
63 StreamTextureProxyImpl::StreamTextureProxyImpl( 61 StreamTextureProxyImpl::StreamTextureProxyImpl(
64 StreamTextureFactorySynchronousImpl::ContextProvider* provider) 62 StreamTextureFactorySynchronousImpl::ContextProvider* provider)
65 : client_(NULL), context_provider_(provider), has_updated_(false) { 63 : client_(NULL), context_provider_(provider), has_updated_(false) {
66 std::fill(current_matrix_, current_matrix_ + 16, 0); 64 std::fill(current_matrix_, current_matrix_ + 16, 0);
67 } 65 }
68 66
69 StreamTextureProxyImpl::~StreamTextureProxyImpl() {} 67 StreamTextureProxyImpl::~StreamTextureProxyImpl() {}
70 68
71 void StreamTextureProxyImpl::Release() { 69 void StreamTextureProxyImpl::Release() {
72 // Assumes this is the last reference to this object. So no need to acquire 70 scoped_refptr<base::MessageLoopProxy> loop;
qinmin 2014/09/18 02:01:30 ditto
73 // lock. 71 {
74 SetClient(NULL); 72 base::AutoLock lock(lock_);
75 if (!loop_.get() || loop_->BelongsToCurrentThread() || 73 loop = loop_;
76 !loop_->DeleteSoon(FROM_HERE, this)) { 74 client_ = NULL;
75 }
76 if (!loop.get() || loop->BelongsToCurrentThread() ||
77 !loop->DeleteSoon(FROM_HERE, this)) {
77 delete this; 78 delete this;
78 } 79 }
79 } 80 }
80 81
81 void StreamTextureProxyImpl::SetClient(cc::VideoFrameProvider::Client* client) {
82 base::AutoLock lock(client_lock_);
83 client_ = client;
84 }
85
86 void StreamTextureProxyImpl::BindToLoop( 82 void StreamTextureProxyImpl::BindToLoop(
87 int32 stream_id, 83 int32 stream_id,
88 cc::VideoFrameProvider::Client* client, 84 cc::VideoFrameProvider::Client* client,
89 scoped_refptr<base::MessageLoopProxy> loop) { 85 scoped_refptr<base::MessageLoopProxy> loop) {
90 DCHECK(loop); 86 DCHECK(loop);
91 SetClient(client); 87
88 {
89 base::AutoLock lock(lock_);
90 DCHECK(!loop_ || (loop == loop_));
91 loop_ = loop;
92 client_ = client;
93 }
94
92 if (loop->BelongsToCurrentThread()) { 95 if (loop->BelongsToCurrentThread()) {
93 BindOnThread(stream_id, loop); 96 BindOnThread(stream_id);
94 return; 97 return;
95 } 98 }
96 // Unretained is safe here only because the object is deleted on |loop_| 99 // Unretained is safe here only because the object is deleted on |loop_|
97 // thread. 100 // thread.
98 loop->PostTask(FROM_HERE, 101 loop->PostTask(FROM_HERE,
99 base::Bind(&StreamTextureProxyImpl::BindOnThread, 102 base::Bind(&StreamTextureProxyImpl::BindOnThread,
100 base::Unretained(this), 103 base::Unretained(this),
101 stream_id, 104 stream_id));
102 loop));
103 } 105 }
104 106
105 void StreamTextureProxyImpl::BindOnThread( 107 void StreamTextureProxyImpl::BindOnThread(int32 stream_id) {
106 int32 stream_id,
107 scoped_refptr<base::MessageLoopProxy> loop) {
108 DCHECK(!loop_ || (loop == loop_));
109 loop_ = loop;
110
111 surface_texture_ = context_provider_->GetSurfaceTexture(stream_id); 108 surface_texture_ = context_provider_->GetSurfaceTexture(stream_id);
112 if (!surface_texture_) { 109 if (!surface_texture_) {
113 LOG(ERROR) << "Failed to get SurfaceTexture for stream."; 110 LOG(ERROR) << "Failed to get SurfaceTexture for stream.";
114 return; 111 return;
115 } 112 }
116 113
117 callback_ = 114 callback_ =
118 base::Bind(&StreamTextureProxyImpl::OnFrameAvailable, AsWeakPtr()); 115 base::Bind(&StreamTextureProxyImpl::OnFrameAvailable, AsWeakPtr());
119 surface_texture_->SetFrameAvailableCallback(callback_); 116 surface_texture_->SetFrameAvailableCallback(callback_);
120 } 117 }
121 118
122 void StreamTextureProxyImpl::OnFrameAvailable() { 119 void StreamTextureProxyImpl::OnFrameAvailable() {
123 // GetTransformMatrix only returns something valid after both is true: 120 // GetTransformMatrix only returns something valid after both is true:
124 // - OnFrameAvailable was called 121 // - OnFrameAvailable was called
125 // - we called UpdateTexImage 122 // - we called UpdateTexImage
126 if (has_updated_) { 123 if (has_updated_) {
127 float matrix[16]; 124 float matrix[16];
128 surface_texture_->GetTransformMatrix(matrix); 125 surface_texture_->GetTransformMatrix(matrix);
129 126
130 if (memcmp(current_matrix_, matrix, sizeof(matrix)) != 0) { 127 if (memcmp(current_matrix_, matrix, sizeof(matrix)) != 0) {
131 memcpy(current_matrix_, matrix, sizeof(matrix)); 128 memcpy(current_matrix_, matrix, sizeof(matrix));
132 129
133 base::AutoLock lock(client_lock_); 130 base::AutoLock lock(lock_);
134 if (client_) 131 if (client_)
135 client_->DidUpdateMatrix(current_matrix_); 132 client_->DidUpdateMatrix(current_matrix_);
136 } 133 }
137 } 134 }
138 // OnFrameAvailable being called a second time implies that we called 135 // OnFrameAvailable being called a second time implies that we called
139 // updateTexImage since after we received the first frame. 136 // updateTexImage since after we received the first frame.
140 has_updated_ = true; 137 has_updated_ = true;
141 138
142 base::AutoLock lock(client_lock_); 139 base::AutoLock lock(lock_);
143 if (client_) 140 if (client_)
144 client_->DidReceiveFrame(); 141 client_->DidReceiveFrame();
145 } 142 }
146 143
147 } // namespace 144 } // namespace
148 145
149 // static 146 // static
150 scoped_refptr<StreamTextureFactorySynchronousImpl> 147 scoped_refptr<StreamTextureFactorySynchronousImpl>
151 StreamTextureFactorySynchronousImpl::Create( 148 StreamTextureFactorySynchronousImpl::Create(
152 const CreateContextProviderCallback& try_create_callback, 149 const CreateContextProviderCallback& try_create_callback,
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 222
226 void StreamTextureFactorySynchronousImpl::RemoveObserver( 223 void StreamTextureFactorySynchronousImpl::RemoveObserver(
227 StreamTextureFactoryContextObserver* obs) { 224 StreamTextureFactoryContextObserver* obs) {
228 DCHECK_EQ(observer_, obs); 225 DCHECK_EQ(observer_, obs);
229 observer_ = NULL; 226 observer_ = NULL;
230 if (context_provider_) 227 if (context_provider_)
231 context_provider_->RemoveObserver(obs); 228 context_provider_->RemoveObserver(obs);
232 } 229 }
233 230
234 } // namespace content 231 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698