OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/callback.h" |
| 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/task_runner.h" |
| 9 |
| 10 #include "services/media/audio/audio_output_manager.h" |
| 11 #include "services/media/audio/audio_server_impl.h" |
| 12 #include "services/media/audio/audio_track_impl.h" |
| 13 |
| 14 namespace mojo { |
| 15 namespace media { |
| 16 namespace audio { |
| 17 |
| 18 AudioServerImpl::AudioServerImpl() |
| 19 : output_manager_(this), |
| 20 cleanup_queue_(new CleanupQueue) { |
| 21 cleanup_closure_ = base::Bind( |
| 22 &AudioServerImpl::DoPacketCleanup, |
| 23 base::Unretained(this)); |
| 24 } |
| 25 |
| 26 AudioServerImpl::~AudioServerImpl() { |
| 27 Shutdown(); |
| 28 DCHECK(cleanup_queue_); |
| 29 DCHECK_EQ(cleanup_queue_->size(), 0u); |
| 30 } |
| 31 |
| 32 void AudioServerImpl::Initialize() { |
| 33 // Stash a pointer to our task runner. |
| 34 DCHECK(base::MessageLoop::current()); |
| 35 task_runner_ = base::MessageLoop::current()->task_runner(); |
| 36 DCHECK(task_runner_); |
| 37 |
| 38 // Set up our output manager. |
| 39 MediaResult res = output_manager_.Init(); |
| 40 // TODO(johngro): Do better at error handling than this weak check. |
| 41 DCHECK(res == MediaResult::OK); |
| 42 } |
| 43 |
| 44 void AudioServerImpl::Shutdown() { |
| 45 shutting_down_ = true; |
| 46 output_manager_.Shutdown(); |
| 47 DoPacketCleanup(); |
| 48 } |
| 49 |
| 50 void AudioServerImpl::CreateTrack(InterfaceRequest<AudioTrack> track) { |
| 51 tracks_.insert(AudioTrackImpl::Create(track.Pass(), this)); |
| 52 } |
| 53 |
| 54 void AudioServerImpl::DoPacketCleanup() { |
| 55 // In order to minimize the time we spend in the lock, we allocate a new |
| 56 // queue, then lock, swap and clear the sched flag, and finally clean out the |
| 57 // queue (which has the side effect of triggering all of the send packet |
| 58 // callbacks). |
| 59 // |
| 60 // Note: this is only safe because we know that we are executing on a single |
| 61 // threaded task runner. Without this guarantee, it might be possible call |
| 62 // the send packet callbacks for a media pipe in a different order than the |
| 63 // packets were sent in the first place. If the task_runner for the audio |
| 64 // server ever loses this serialization guarantee (because it becomes |
| 65 // multi-threaded, for example) we will need to introduce another lock |
| 66 // (different from the cleanup lock) in order to keep the cleanup tasks |
| 67 // properly ordered while guaranteeing minimal contention of the cleanup lock |
| 68 // (which is being acquired by the high priority mixing threads). |
| 69 std::unique_ptr<CleanupQueue> tmp_queue(new CleanupQueue()); |
| 70 |
| 71 { |
| 72 base::AutoLock lock(cleanup_queue_lock_); |
| 73 cleanup_queue_.swap(tmp_queue); |
| 74 cleanup_scheduled_ = false; |
| 75 } |
| 76 |
| 77 // The clear method of standard containers do not guarantee any ordering of |
| 78 // destruction of the objects they hold. In order to guarantee proper |
| 79 // sequencing of the callbacks, go over the container front-to-back, nulling |
| 80 // out the std::unique_ptrs they hold as we go (which will trigger the |
| 81 // callbacks). Afterwards, just let tmp_queue go out of scope and clear() |
| 82 // itself automatically. |
| 83 for (auto iter = tmp_queue->begin(); iter != tmp_queue->end(); ++iter) { |
| 84 (*iter) = nullptr; |
| 85 } |
| 86 } |
| 87 |
| 88 void AudioServerImpl::SchedulePacketCleanup( |
| 89 MediaPipeBase::MediaPacketStatePtr state) { |
| 90 base::AutoLock lock(cleanup_queue_lock_); |
| 91 |
| 92 cleanup_queue_->emplace_back(std::move(state)); |
| 93 |
| 94 if (!cleanup_scheduled_ && !shutting_down_) { |
| 95 DCHECK(task_runner_); |
| 96 cleanup_scheduled_ = task_runner_->PostTask(FROM_HERE, cleanup_closure_); |
| 97 DCHECK(cleanup_scheduled_); |
| 98 } |
| 99 } |
| 100 |
| 101 } // namespace audio |
| 102 } // namespace media |
| 103 } // namespace mojo |
OLD | NEW |