sergeyu | 77b42df8 | 2016-10-04 02:08:38 | [diff] [blame^] | 1 | // Copyright 2016 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 "remoting/protocol/audio_decode_scheduler.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
| 9 | #include "base/bind.h" |
| 10 | #include "base/location.h" |
| 11 | #include "base/single_thread_task_runner.h" |
| 12 | #include "base/task_runner_util.h" |
| 13 | #include "remoting/codec/audio_decoder.h" |
| 14 | #include "remoting/proto/audio.pb.h" |
| 15 | #include "remoting/protocol/audio_stub.h" |
| 16 | |
| 17 | namespace remoting { |
| 18 | namespace protocol { |
| 19 | |
| 20 | AudioDecodeScheduler::AudioDecodeScheduler( |
| 21 | scoped_refptr<base::SingleThreadTaskRunner> audio_decode_task_runner, |
| 22 | base::WeakPtr<protocol::AudioStub> audio_consumer) |
| 23 | : audio_decode_task_runner_(audio_decode_task_runner), |
| 24 | audio_consumer_(audio_consumer), |
| 25 | weak_factory_(this) {} |
| 26 | |
| 27 | AudioDecodeScheduler::~AudioDecodeScheduler() { |
| 28 | audio_decode_task_runner_->DeleteSoon(FROM_HERE, decoder_.release()); |
| 29 | } |
| 30 | |
| 31 | void AudioDecodeScheduler::Initialize(const protocol::SessionConfig& config) { |
| 32 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 33 | DCHECK(!decoder_); |
| 34 | decoder_.reset(AudioDecoder::CreateAudioDecoder(config).release()); |
| 35 | } |
| 36 | |
| 37 | void AudioDecodeScheduler::ProcessAudioPacket( |
| 38 | std::unique_ptr<AudioPacket> packet, |
| 39 | const base::Closure& done) { |
| 40 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 41 | |
| 42 | base::PostTaskAndReplyWithResult( |
| 43 | audio_decode_task_runner_.get(), FROM_HERE, |
| 44 | base::Bind(&AudioDecoder::Decode, base::Unretained(decoder_.get()), |
| 45 | base::Passed(&packet)), |
| 46 | base::Bind(&AudioDecodeScheduler::ProcessDecodedPacket, |
| 47 | weak_factory_.GetWeakPtr(), done)); |
| 48 | } |
| 49 | |
| 50 | void AudioDecodeScheduler::ProcessDecodedPacket( |
| 51 | const base::Closure& done, |
| 52 | std::unique_ptr<AudioPacket> packet) { |
| 53 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 54 | |
| 55 | if (!packet || !audio_consumer_) { |
| 56 | done.Run(); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | audio_consumer_->ProcessAudioPacket(std::move(packet), done); |
| 61 | } |
| 62 | |
| 63 | } // namespace protocol |
| 64 | } // namespace remoting |