| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/browser/media/capture/web_contents_audio_input_stream.h" | 5 #include "content/browser/media/capture/web_contents_audio_input_stream.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/threading/thread_checker.h" | 13 #include "base/threading/thread_checker.h" |
| 14 #include "content/browser/media/capture/audio_mirroring_manager.h" | 14 #include "content/browser/media/capture/audio_mirroring_manager.h" |
| 15 #include "content/browser/media/capture/web_contents_capture_util.h" | 15 #include "content/browser/media/capture/web_contents_capture_util.h" |
| 16 #include "content/browser/media/capture/web_contents_tracker.h" | 16 #include "content/browser/media/capture/web_contents_tracker.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "media/audio/virtual_audio_input_stream.h" | 18 #include "media/audio/virtual_audio_input_stream.h" |
| 19 #include "media/audio/virtual_audio_output_stream.h" | 19 #include "media/audio/virtual_audio_output_stream.h" |
| 20 | 20 |
| 21 namespace content { | 21 namespace content { |
| 22 | 22 |
| 23 class WebContentsAudioInputStream::Impl | 23 class WebContentsAudioInputStream::Impl |
| 24 : public base::RefCountedThreadSafe<WebContentsAudioInputStream::Impl>, | 24 : public base::RefCountedThreadSafe<WebContentsAudioInputStream::Impl>, |
| 25 public AudioMirroringManager::MirroringDestination { | 25 public AudioMirroringManager::MirroringDestination { |
| 26 public: | 26 public: |
| 27 // Takes ownership of |mixer_stream|. The rest outlive this instance. | 27 // Takes ownership of |mixer_stream|. The rest outlive this instance. |
| 28 Impl(int render_process_id, int render_view_id, | 28 Impl(int render_process_id, int main_render_frame_id, |
| 29 AudioMirroringManager* mirroring_manager, | 29 AudioMirroringManager* mirroring_manager, |
| 30 const scoped_refptr<WebContentsTracker>& tracker, | 30 const scoped_refptr<WebContentsTracker>& tracker, |
| 31 media::VirtualAudioInputStream* mixer_stream); | 31 media::VirtualAudioInputStream* mixer_stream); |
| 32 | 32 |
| 33 // Open underlying VirtualAudioInputStream and start tracker. | 33 // Open underlying VirtualAudioInputStream and start tracker. |
| 34 bool Open(); | 34 bool Open(); |
| 35 | 35 |
| 36 // Start the underlying VirtualAudioInputStream and instruct | 36 // Start the underlying VirtualAudioInputStream and instruct |
| 37 // AudioMirroringManager to begin a mirroring session. | 37 // AudioMirroringManager to begin a mirroring session. |
| 38 void Start(AudioInputCallback* callback); | 38 void Start(AudioInputCallback* callback); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 const media::AudioParameters& params) OVERRIDE; | 77 const media::AudioParameters& params) OVERRIDE; |
| 78 | 78 |
| 79 // Callback which is run when |stream| is closed. Deletes |stream|. | 79 // Callback which is run when |stream| is closed. Deletes |stream|. |
| 80 void ReleaseInput(media::VirtualAudioOutputStream* stream); | 80 void ReleaseInput(media::VirtualAudioOutputStream* stream); |
| 81 | 81 |
| 82 // Called by WebContentsTracker when the target of the audio mirroring has | 82 // Called by WebContentsTracker when the target of the audio mirroring has |
| 83 // changed. | 83 // changed. |
| 84 void OnTargetChanged(int render_process_id, int render_view_id); | 84 void OnTargetChanged(int render_process_id, int render_view_id); |
| 85 | 85 |
| 86 // Injected dependencies. | 86 // Injected dependencies. |
| 87 const int initial_render_process_id_; |
| 88 const int initial_main_render_frame_id_; |
| 87 AudioMirroringManager* const mirroring_manager_; | 89 AudioMirroringManager* const mirroring_manager_; |
| 88 const scoped_refptr<WebContentsTracker> tracker_; | 90 const scoped_refptr<WebContentsTracker> tracker_; |
| 89 // The AudioInputStream implementation that handles the audio conversion and | 91 // The AudioInputStream implementation that handles the audio conversion and |
| 90 // mixing details. | 92 // mixing details. |
| 91 const scoped_ptr<media::VirtualAudioInputStream> mixer_stream_; | 93 const scoped_ptr<media::VirtualAudioInputStream> mixer_stream_; |
| 92 | 94 |
| 93 State state_; | 95 State state_; |
| 94 | 96 |
| 95 // Current audio mirroring target. | 97 // Current audio mirroring target. |
| 98 bool target_identified_; |
| 96 int target_render_process_id_; | 99 int target_render_process_id_; |
| 97 int target_render_view_id_; | 100 int target_render_view_id_; |
| 98 | 101 |
| 99 // Current callback used to consume the resulting mixed audio data. | 102 // Current callback used to consume the resulting mixed audio data. |
| 100 AudioInputCallback* callback_; | 103 AudioInputCallback* callback_; |
| 101 | 104 |
| 102 base::ThreadChecker thread_checker_; | 105 base::ThreadChecker thread_checker_; |
| 103 | 106 |
| 104 DISALLOW_COPY_AND_ASSIGN(Impl); | 107 DISALLOW_COPY_AND_ASSIGN(Impl); |
| 105 }; | 108 }; |
| 106 | 109 |
| 107 WebContentsAudioInputStream::Impl::Impl( | 110 WebContentsAudioInputStream::Impl::Impl( |
| 108 int render_process_id, int render_view_id, | 111 int render_process_id, int main_render_frame_id, |
| 109 AudioMirroringManager* mirroring_manager, | 112 AudioMirroringManager* mirroring_manager, |
| 110 const scoped_refptr<WebContentsTracker>& tracker, | 113 const scoped_refptr<WebContentsTracker>& tracker, |
| 111 media::VirtualAudioInputStream* mixer_stream) | 114 media::VirtualAudioInputStream* mixer_stream) |
| 112 : mirroring_manager_(mirroring_manager), | 115 : initial_render_process_id_(render_process_id), |
| 113 tracker_(tracker), mixer_stream_(mixer_stream), state_(CONSTRUCTED), | 116 initial_main_render_frame_id_(main_render_frame_id), |
| 114 target_render_process_id_(render_process_id), | 117 mirroring_manager_(mirroring_manager), |
| 115 target_render_view_id_(render_view_id), | 118 tracker_(tracker), |
| 119 mixer_stream_(mixer_stream), |
| 120 state_(CONSTRUCTED), |
| 121 target_identified_(false), |
| 122 target_render_process_id_(-1), |
| 123 target_render_view_id_(-1), |
| 116 callback_(NULL) { | 124 callback_(NULL) { |
| 117 DCHECK(mirroring_manager_); | 125 DCHECK(mirroring_manager_); |
| 118 DCHECK(tracker_.get()); | 126 DCHECK(tracker_.get()); |
| 119 DCHECK(mixer_stream_.get()); | 127 DCHECK(mixer_stream_.get()); |
| 120 | 128 |
| 121 // WAIS::Impl can be constructed on any thread, but will DCHECK that all | 129 // WAIS::Impl can be constructed on any thread, but will DCHECK that all |
| 122 // its methods from here on are called from the same thread. | 130 // its methods from here on are called from the same thread. |
| 123 thread_checker_.DetachFromThread(); | 131 thread_checker_.DetachFromThread(); |
| 124 } | 132 } |
| 125 | 133 |
| 126 WebContentsAudioInputStream::Impl::~Impl() { | 134 WebContentsAudioInputStream::Impl::~Impl() { |
| 127 DCHECK(state_ == CONSTRUCTED || state_ == CLOSED); | 135 DCHECK(state_ == CONSTRUCTED || state_ == CLOSED); |
| 128 } | 136 } |
| 129 | 137 |
| 130 bool WebContentsAudioInputStream::Impl::Open() { | 138 bool WebContentsAudioInputStream::Impl::Open() { |
| 131 DCHECK(thread_checker_.CalledOnValidThread()); | 139 DCHECK(thread_checker_.CalledOnValidThread()); |
| 132 | 140 |
| 133 DCHECK_EQ(CONSTRUCTED, state_) << "Illegal to Open more than once."; | 141 DCHECK_EQ(CONSTRUCTED, state_) << "Illegal to Open more than once."; |
| 134 | 142 |
| 135 if (!mixer_stream_->Open()) | 143 if (!mixer_stream_->Open()) |
| 136 return false; | 144 return false; |
| 137 | 145 |
| 138 state_ = OPENED; | 146 state_ = OPENED; |
| 139 | 147 |
| 140 tracker_->Start( | 148 tracker_->Start( |
| 141 target_render_process_id_, target_render_view_id_, | 149 initial_render_process_id_, initial_main_render_frame_id_, |
| 142 base::Bind(&Impl::OnTargetChanged, this)); | 150 base::Bind(&Impl::OnTargetChanged, this)); |
| 143 | 151 |
| 144 return true; | 152 return true; |
| 145 } | 153 } |
| 146 | 154 |
| 147 void WebContentsAudioInputStream::Impl::Start(AudioInputCallback* callback) { | 155 void WebContentsAudioInputStream::Impl::Start(AudioInputCallback* callback) { |
| 148 DCHECK(thread_checker_.CalledOnValidThread()); | 156 DCHECK(thread_checker_.CalledOnValidThread()); |
| 149 DCHECK(callback); | 157 DCHECK(callback); |
| 150 | 158 |
| 151 if (state_ != OPENED) | 159 if (state_ != OPENED) |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 tracker_->Stop(); | 197 tracker_->Stop(); |
| 190 mixer_stream_->Close(); | 198 mixer_stream_->Close(); |
| 191 } | 199 } |
| 192 | 200 |
| 193 DCHECK_EQ(CONSTRUCTED, state_); | 201 DCHECK_EQ(CONSTRUCTED, state_); |
| 194 state_ = CLOSED; | 202 state_ = CLOSED; |
| 195 } | 203 } |
| 196 | 204 |
| 197 bool WebContentsAudioInputStream::Impl::IsTargetLost() const { | 205 bool WebContentsAudioInputStream::Impl::IsTargetLost() const { |
| 198 DCHECK(thread_checker_.CalledOnValidThread()); | 206 DCHECK(thread_checker_.CalledOnValidThread()); |
| 199 | 207 if (!target_identified_) |
| 208 return false; |
| 200 return target_render_process_id_ <= 0 || target_render_view_id_ <= 0; | 209 return target_render_process_id_ <= 0 || target_render_view_id_ <= 0; |
| 201 } | 210 } |
| 202 | 211 |
| 203 void WebContentsAudioInputStream::Impl::ReportError() { | 212 void WebContentsAudioInputStream::Impl::ReportError() { |
| 204 DCHECK(thread_checker_.CalledOnValidThread()); | 213 DCHECK(thread_checker_.CalledOnValidThread()); |
| 205 | 214 |
| 206 // TODO(miu): Need clean-up of AudioInputCallback interface in a future | 215 // TODO(miu): Need clean-up of AudioInputCallback interface in a future |
| 207 // change, since its only implementation ignores the first argument entirely | 216 // change, since its only implementation ignores the first argument entirely |
| 208 callback_->OnError(NULL); | 217 callback_->OnError(NULL); |
| 209 } | 218 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 | 254 |
| 246 void WebContentsAudioInputStream::Impl::ReleaseInput( | 255 void WebContentsAudioInputStream::Impl::ReleaseInput( |
| 247 media::VirtualAudioOutputStream* stream) { | 256 media::VirtualAudioOutputStream* stream) { |
| 248 delete stream; | 257 delete stream; |
| 249 } | 258 } |
| 250 | 259 |
| 251 void WebContentsAudioInputStream::Impl::OnTargetChanged(int render_process_id, | 260 void WebContentsAudioInputStream::Impl::OnTargetChanged(int render_process_id, |
| 252 int render_view_id) { | 261 int render_view_id) { |
| 253 DCHECK(thread_checker_.CalledOnValidThread()); | 262 DCHECK(thread_checker_.CalledOnValidThread()); |
| 254 | 263 |
| 255 if (target_render_process_id_ == render_process_id && | 264 if (target_identified_ && |
| 265 target_render_process_id_ == render_process_id && |
| 256 target_render_view_id_ == render_view_id) { | 266 target_render_view_id_ == render_view_id) { |
| 257 return; | 267 return; |
| 258 } | 268 } |
| 259 | 269 |
| 260 DVLOG(1) << "Target RenderView has changed from " | 270 DVLOG(1) << "Target RenderView has changed from " |
| 261 << target_render_process_id_ << ':' << target_render_view_id_ | 271 << target_render_process_id_ << ':' << target_render_view_id_ |
| 262 << " to " << render_process_id << ':' << render_view_id; | 272 << " to " << render_process_id << ':' << render_view_id; |
| 263 | 273 |
| 264 if (state_ == MIRRORING) | 274 if (state_ == MIRRORING) |
| 265 StopMirroring(); | 275 StopMirroring(); |
| 266 | 276 |
| 277 target_identified_ = true; |
| 267 target_render_process_id_ = render_process_id; | 278 target_render_process_id_ = render_process_id; |
| 268 target_render_view_id_ = render_view_id; | 279 target_render_view_id_ = render_view_id; |
| 269 | 280 |
| 270 if (state_ == MIRRORING) { | 281 if (state_ == MIRRORING) { |
| 271 if (IsTargetLost()) { | 282 if (IsTargetLost()) { |
| 272 ReportError(); | 283 ReportError(); |
| 273 Stop(); | 284 Stop(); |
| 274 } else { | 285 } else { |
| 275 StartMirroring(); | 286 StartMirroring(); |
| 276 } | 287 } |
| 277 } | 288 } |
| 278 } | 289 } |
| 279 | 290 |
| 280 // static | 291 // static |
| 281 WebContentsAudioInputStream* WebContentsAudioInputStream::Create( | 292 WebContentsAudioInputStream* WebContentsAudioInputStream::Create( |
| 282 const std::string& device_id, | 293 const std::string& device_id, |
| 283 const media::AudioParameters& params, | 294 const media::AudioParameters& params, |
| 284 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner, | 295 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner, |
| 285 AudioMirroringManager* audio_mirroring_manager) { | 296 AudioMirroringManager* audio_mirroring_manager) { |
| 286 int render_process_id; | 297 int render_process_id; |
| 287 int render_view_id; | 298 int main_render_frame_id; |
| 288 if (!WebContentsCaptureUtil::ExtractTabCaptureTarget( | 299 if (!WebContentsCaptureUtil::ExtractTabCaptureTarget( |
| 289 device_id, &render_process_id, &render_view_id)) { | 300 device_id, &render_process_id, &main_render_frame_id)) { |
| 290 return NULL; | 301 return NULL; |
| 291 } | 302 } |
| 292 | 303 |
| 293 return new WebContentsAudioInputStream( | 304 return new WebContentsAudioInputStream( |
| 294 render_process_id, render_view_id, | 305 render_process_id, main_render_frame_id, |
| 295 audio_mirroring_manager, | 306 audio_mirroring_manager, |
| 296 new WebContentsTracker(), | 307 new WebContentsTracker(), |
| 297 new media::VirtualAudioInputStream( | 308 new media::VirtualAudioInputStream( |
| 298 params, worker_task_runner, | 309 params, worker_task_runner, |
| 299 media::VirtualAudioInputStream::AfterCloseCallback())); | 310 media::VirtualAudioInputStream::AfterCloseCallback())); |
| 300 } | 311 } |
| 301 | 312 |
| 302 WebContentsAudioInputStream::WebContentsAudioInputStream( | 313 WebContentsAudioInputStream::WebContentsAudioInputStream( |
| 303 int render_process_id, int render_view_id, | 314 int render_process_id, int main_render_frame_id, |
| 304 AudioMirroringManager* mirroring_manager, | 315 AudioMirroringManager* mirroring_manager, |
| 305 const scoped_refptr<WebContentsTracker>& tracker, | 316 const scoped_refptr<WebContentsTracker>& tracker, |
| 306 media::VirtualAudioInputStream* mixer_stream) | 317 media::VirtualAudioInputStream* mixer_stream) |
| 307 : impl_(new Impl(render_process_id, render_view_id, | 318 : impl_(new Impl(render_process_id, main_render_frame_id, |
| 308 mirroring_manager, tracker, mixer_stream)) {} | 319 mirroring_manager, tracker, mixer_stream)) {} |
| 309 | 320 |
| 310 WebContentsAudioInputStream::~WebContentsAudioInputStream() {} | 321 WebContentsAudioInputStream::~WebContentsAudioInputStream() {} |
| 311 | 322 |
| 312 bool WebContentsAudioInputStream::Open() { | 323 bool WebContentsAudioInputStream::Open() { |
| 313 return impl_->Open(); | 324 return impl_->Open(); |
| 314 } | 325 } |
| 315 | 326 |
| 316 void WebContentsAudioInputStream::Start(AudioInputCallback* callback) { | 327 void WebContentsAudioInputStream::Start(AudioInputCallback* callback) { |
| 317 impl_->Start(callback); | 328 impl_->Start(callback); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 340 | 351 |
| 341 void WebContentsAudioInputStream::SetAutomaticGainControl(bool enabled) { | 352 void WebContentsAudioInputStream::SetAutomaticGainControl(bool enabled) { |
| 342 impl_->mixer_stream()->SetAutomaticGainControl(enabled); | 353 impl_->mixer_stream()->SetAutomaticGainControl(enabled); |
| 343 } | 354 } |
| 344 | 355 |
| 345 bool WebContentsAudioInputStream::GetAutomaticGainControl() { | 356 bool WebContentsAudioInputStream::GetAutomaticGainControl() { |
| 346 return impl_->mixer_stream()->GetAutomaticGainControl(); | 357 return impl_->mixer_stream()->GetAutomaticGainControl(); |
| 347 } | 358 } |
| 348 | 359 |
| 349 } // namespace content | 360 } // namespace content |
| OLD | NEW |