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

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: review, comment only changes 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
« no previous file with comments | « content/renderer/media/android/stream_texture_factory_impl.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // Protects access to |client_| and |loop_|.
47 base::Lock lock_;
49 cc::VideoFrameProvider::Client* client_; 48 cc::VideoFrameProvider::Client* client_;
49 scoped_refptr<base::MessageLoopProxy> loop_;
50 50
51 // Accessed on the |loop_| thread only. 51 // Accessed on the |loop_| thread only.
52 scoped_refptr<base::MessageLoopProxy> loop_;
53 base::Closure callback_; 52 base::Closure callback_;
54 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider> 53 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider>
55 context_provider_; 54 context_provider_;
56 scoped_refptr<gfx::SurfaceTexture> surface_texture_; 55 scoped_refptr<gfx::SurfaceTexture> surface_texture_;
57 float current_matrix_[16]; 56 float current_matrix_[16];
58 bool has_updated_; 57 bool has_updated_;
59 58
60 DISALLOW_IMPLICIT_CONSTRUCTORS(StreamTextureProxyImpl); 59 DISALLOW_IMPLICIT_CONSTRUCTORS(StreamTextureProxyImpl);
61 }; 60 };
62 61
63 StreamTextureProxyImpl::StreamTextureProxyImpl( 62 StreamTextureProxyImpl::StreamTextureProxyImpl(
64 StreamTextureFactorySynchronousImpl::ContextProvider* provider) 63 StreamTextureFactorySynchronousImpl::ContextProvider* provider)
65 : client_(NULL), context_provider_(provider), has_updated_(false) { 64 : client_(NULL), context_provider_(provider), has_updated_(false) {
66 std::fill(current_matrix_, current_matrix_ + 16, 0); 65 std::fill(current_matrix_, current_matrix_ + 16, 0);
67 } 66 }
68 67
69 StreamTextureProxyImpl::~StreamTextureProxyImpl() {} 68 StreamTextureProxyImpl::~StreamTextureProxyImpl() {}
70 69
71 void StreamTextureProxyImpl::Release() { 70 void StreamTextureProxyImpl::Release() {
72 // Assumes this is the last reference to this object. So no need to acquire 71 {
73 // lock. 72 // Cannot call into |client_| anymore (from any thread) after returning
74 SetClient(NULL); 73 // from here.
74 base::AutoLock lock(lock_);
75 client_ = NULL;
76 }
77 // Release is analogous to the destructor, so there should be no more external
78 // calls to this object in Release. Therefore there is no need to acquire the
79 // lock to access |loop_|.
75 if (!loop_.get() || loop_->BelongsToCurrentThread() || 80 if (!loop_.get() || loop_->BelongsToCurrentThread() ||
76 !loop_->DeleteSoon(FROM_HERE, this)) { 81 !loop_->DeleteSoon(FROM_HERE, this)) {
77 delete this; 82 delete this;
78 } 83 }
79 } 84 }
80 85
81 void StreamTextureProxyImpl::SetClient(cc::VideoFrameProvider::Client* client) {
82 base::AutoLock lock(client_lock_);
83 client_ = client;
84 }
85
86 void StreamTextureProxyImpl::BindToLoop( 86 void StreamTextureProxyImpl::BindToLoop(
87 int32 stream_id, 87 int32 stream_id,
88 cc::VideoFrameProvider::Client* client, 88 cc::VideoFrameProvider::Client* client,
89 scoped_refptr<base::MessageLoopProxy> loop) { 89 scoped_refptr<base::MessageLoopProxy> loop) {
90 DCHECK(loop); 90 DCHECK(loop);
91 SetClient(client); 91
92 {
93 base::AutoLock lock(lock_);
94 DCHECK(!loop_ || (loop == loop_));
95 loop_ = loop;
96 client_ = client;
97 }
98
92 if (loop->BelongsToCurrentThread()) { 99 if (loop->BelongsToCurrentThread()) {
93 BindOnThread(stream_id, loop); 100 BindOnThread(stream_id);
94 return; 101 return;
95 } 102 }
96 // Unretained is safe here only because the object is deleted on |loop_| 103 // Unretained is safe here only because the object is deleted on |loop_|
97 // thread. 104 // thread.
98 loop->PostTask(FROM_HERE, 105 loop->PostTask(FROM_HERE,
99 base::Bind(&StreamTextureProxyImpl::BindOnThread, 106 base::Bind(&StreamTextureProxyImpl::BindOnThread,
100 base::Unretained(this), 107 base::Unretained(this),
101 stream_id, 108 stream_id));
102 loop));
103 } 109 }
104 110
105 void StreamTextureProxyImpl::BindOnThread( 111 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); 112 surface_texture_ = context_provider_->GetSurfaceTexture(stream_id);
112 if (!surface_texture_) { 113 if (!surface_texture_) {
113 LOG(ERROR) << "Failed to get SurfaceTexture for stream."; 114 LOG(ERROR) << "Failed to get SurfaceTexture for stream.";
114 return; 115 return;
115 } 116 }
116 117
117 callback_ = 118 callback_ =
118 base::Bind(&StreamTextureProxyImpl::OnFrameAvailable, AsWeakPtr()); 119 base::Bind(&StreamTextureProxyImpl::OnFrameAvailable, AsWeakPtr());
119 surface_texture_->SetFrameAvailableCallback(callback_); 120 surface_texture_->SetFrameAvailableCallback(callback_);
120 } 121 }
121 122
122 void StreamTextureProxyImpl::OnFrameAvailable() { 123 void StreamTextureProxyImpl::OnFrameAvailable() {
123 // GetTransformMatrix only returns something valid after both is true: 124 // GetTransformMatrix only returns something valid after both is true:
124 // - OnFrameAvailable was called 125 // - OnFrameAvailable was called
125 // - we called UpdateTexImage 126 // - we called UpdateTexImage
126 if (has_updated_) { 127 if (has_updated_) {
127 float matrix[16]; 128 float matrix[16];
128 surface_texture_->GetTransformMatrix(matrix); 129 surface_texture_->GetTransformMatrix(matrix);
129 130
130 if (memcmp(current_matrix_, matrix, sizeof(matrix)) != 0) { 131 if (memcmp(current_matrix_, matrix, sizeof(matrix)) != 0) {
131 memcpy(current_matrix_, matrix, sizeof(matrix)); 132 memcpy(current_matrix_, matrix, sizeof(matrix));
132 133
133 base::AutoLock lock(client_lock_); 134 base::AutoLock lock(lock_);
134 if (client_) 135 if (client_)
135 client_->DidUpdateMatrix(current_matrix_); 136 client_->DidUpdateMatrix(current_matrix_);
136 } 137 }
137 } 138 }
138 // OnFrameAvailable being called a second time implies that we called 139 // OnFrameAvailable being called a second time implies that we called
139 // updateTexImage since after we received the first frame. 140 // updateTexImage since after we received the first frame.
140 has_updated_ = true; 141 has_updated_ = true;
141 142
142 base::AutoLock lock(client_lock_); 143 base::AutoLock lock(lock_);
143 if (client_) 144 if (client_)
144 client_->DidReceiveFrame(); 145 client_->DidReceiveFrame();
145 } 146 }
146 147
147 } // namespace 148 } // namespace
148 149
149 // static 150 // static
150 scoped_refptr<StreamTextureFactorySynchronousImpl> 151 scoped_refptr<StreamTextureFactorySynchronousImpl>
151 StreamTextureFactorySynchronousImpl::Create( 152 StreamTextureFactorySynchronousImpl::Create(
152 const CreateContextProviderCallback& try_create_callback, 153 const CreateContextProviderCallback& try_create_callback,
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 226
226 void StreamTextureFactorySynchronousImpl::RemoveObserver( 227 void StreamTextureFactorySynchronousImpl::RemoveObserver(
227 StreamTextureFactoryContextObserver* obs) { 228 StreamTextureFactoryContextObserver* obs) {
228 DCHECK_EQ(observer_, obs); 229 DCHECK_EQ(observer_, obs);
229 observer_ = NULL; 230 observer_ = NULL;
230 if (context_provider_) 231 if (context_provider_)
231 context_provider_->RemoveObserver(obs); 232 context_provider_->RemoveObserver(obs);
232 } 233 }
233 234
234 } // namespace content 235 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/media/android/stream_texture_factory_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698