Chromium Code Reviews| 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, |
|
ncarter (slow)
2014/07/10 01:17:51
Interesting, this points to a possible hurdle (whi
miu
2014/07/10 22:16:12
Yes. I've prototyped a solution that seems to wor
ncarter (slow)
2014/07/11 22:32:24
Pretty cool. Glad to hear you're on top of it.
| |
| 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. |
| 96 int target_render_process_id_; | 98 int target_render_process_id_; |
| 97 int target_render_view_id_; | 99 int target_render_view_id_; |
| 98 | 100 |
| 99 // Current callback used to consume the resulting mixed audio data. | 101 // Current callback used to consume the resulting mixed audio data. |
| 100 AudioInputCallback* callback_; | 102 AudioInputCallback* callback_; |
| 101 | 103 |
| 102 base::ThreadChecker thread_checker_; | 104 base::ThreadChecker thread_checker_; |
| 103 | 105 |
| 104 DISALLOW_COPY_AND_ASSIGN(Impl); | 106 DISALLOW_COPY_AND_ASSIGN(Impl); |
| 105 }; | 107 }; |
| 106 | 108 |
| 107 WebContentsAudioInputStream::Impl::Impl( | 109 WebContentsAudioInputStream::Impl::Impl( |
| 108 int render_process_id, int render_view_id, | 110 int render_process_id, int main_render_frame_id, |
| 109 AudioMirroringManager* mirroring_manager, | 111 AudioMirroringManager* mirroring_manager, |
| 110 const scoped_refptr<WebContentsTracker>& tracker, | 112 const scoped_refptr<WebContentsTracker>& tracker, |
| 111 media::VirtualAudioInputStream* mixer_stream) | 113 media::VirtualAudioInputStream* mixer_stream) |
| 112 : mirroring_manager_(mirroring_manager), | 114 : initial_render_process_id_(render_process_id), |
| 113 tracker_(tracker), mixer_stream_(mixer_stream), state_(CONSTRUCTED), | 115 initial_main_render_frame_id_(main_render_frame_id), |
| 114 target_render_process_id_(render_process_id), | 116 mirroring_manager_(mirroring_manager), |
| 115 target_render_view_id_(render_view_id), | 117 tracker_(tracker), |
| 118 mixer_stream_(mixer_stream), | |
| 119 state_(CONSTRUCTED), | |
| 120 // TODO(miu): This hack for initializing target_render_XXX_id_ is needed | |
| 121 // so IsTargetLost() won't prematurely return false when called in Start() | |
| 122 // below. This will be removed in a soon-upcoming change when all this | |
| 123 // code is migrated away from using RenderViewHosts (for cross-site | |
| 124 // isolation). | |
|
ncarter (slow)
2014/07/10 01:17:51
So -- it seems like this workaround is needed beca
miu
2014/07/10 22:16:12
Done.
| |
| 125 target_render_process_id_(kint32max), | |
| 126 target_render_view_id_(kint32max), | |
| 116 callback_(NULL) { | 127 callback_(NULL) { |
| 117 DCHECK(mirroring_manager_); | 128 DCHECK(mirroring_manager_); |
| 118 DCHECK(tracker_.get()); | 129 DCHECK(tracker_.get()); |
| 119 DCHECK(mixer_stream_.get()); | 130 DCHECK(mixer_stream_.get()); |
| 120 | 131 |
| 121 // WAIS::Impl can be constructed on any thread, but will DCHECK that all | 132 // 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. | 133 // its methods from here on are called from the same thread. |
| 123 thread_checker_.DetachFromThread(); | 134 thread_checker_.DetachFromThread(); |
| 124 } | 135 } |
| 125 | 136 |
| 126 WebContentsAudioInputStream::Impl::~Impl() { | 137 WebContentsAudioInputStream::Impl::~Impl() { |
| 127 DCHECK(state_ == CONSTRUCTED || state_ == CLOSED); | 138 DCHECK(state_ == CONSTRUCTED || state_ == CLOSED); |
| 128 } | 139 } |
| 129 | 140 |
| 130 bool WebContentsAudioInputStream::Impl::Open() { | 141 bool WebContentsAudioInputStream::Impl::Open() { |
| 131 DCHECK(thread_checker_.CalledOnValidThread()); | 142 DCHECK(thread_checker_.CalledOnValidThread()); |
| 132 | 143 |
| 133 DCHECK_EQ(CONSTRUCTED, state_) << "Illegal to Open more than once."; | 144 DCHECK_EQ(CONSTRUCTED, state_) << "Illegal to Open more than once."; |
| 134 | 145 |
| 135 if (!mixer_stream_->Open()) | 146 if (!mixer_stream_->Open()) |
| 136 return false; | 147 return false; |
| 137 | 148 |
| 138 state_ = OPENED; | 149 state_ = OPENED; |
| 139 | 150 |
| 140 tracker_->Start( | 151 tracker_->Start( |
| 141 target_render_process_id_, target_render_view_id_, | 152 initial_render_process_id_, initial_main_render_frame_id_, |
| 142 base::Bind(&Impl::OnTargetChanged, this)); | 153 base::Bind(&Impl::OnTargetChanged, this)); |
| 143 | 154 |
| 144 return true; | 155 return true; |
| 145 } | 156 } |
| 146 | 157 |
| 147 void WebContentsAudioInputStream::Impl::Start(AudioInputCallback* callback) { | 158 void WebContentsAudioInputStream::Impl::Start(AudioInputCallback* callback) { |
| 148 DCHECK(thread_checker_.CalledOnValidThread()); | 159 DCHECK(thread_checker_.CalledOnValidThread()); |
| 149 DCHECK(callback); | 160 DCHECK(callback); |
| 150 | 161 |
| 151 if (state_ != OPENED) | 162 if (state_ != OPENED) |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 |