| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "remoting/host/audio_pump.h" | 5 #include "remoting/host/audio_pump.h" |
| 6 | 6 |
| 7 #include <utility> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/location.h" | 10 #include "base/location.h" |
| 9 #include "base/logging.h" | 11 #include "base/logging.h" |
| 10 #include "base/macros.h" | 12 #include "base/macros.h" |
| 11 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 12 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
| 13 #include "remoting/codec/audio_encoder.h" | 15 #include "remoting/codec/audio_encoder.h" |
| 14 #include "remoting/host/audio_capturer.h" | 16 #include "remoting/host/audio_capturer.h" |
| 15 #include "remoting/proto/audio.pb.h" | 17 #include "remoting/proto/audio.pb.h" |
| 16 #include "remoting/protocol/audio_stub.h" | 18 #include "remoting/protocol/audio_stub.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 int bytes_pending_; | 53 int bytes_pending_; |
| 52 | 54 |
| 53 DISALLOW_COPY_AND_ASSIGN(Core); | 55 DISALLOW_COPY_AND_ASSIGN(Core); |
| 54 }; | 56 }; |
| 55 | 57 |
| 56 AudioPump::Core::Core(base::WeakPtr<AudioPump> pump, | 58 AudioPump::Core::Core(base::WeakPtr<AudioPump> pump, |
| 57 scoped_ptr<AudioCapturer> audio_capturer, | 59 scoped_ptr<AudioCapturer> audio_capturer, |
| 58 scoped_ptr<AudioEncoder> audio_encoder) | 60 scoped_ptr<AudioEncoder> audio_encoder) |
| 59 : pump_(pump), | 61 : pump_(pump), |
| 60 pump_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 62 pump_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 61 audio_capturer_(audio_capturer.Pass()), | 63 audio_capturer_(std::move(audio_capturer)), |
| 62 audio_encoder_(audio_encoder.Pass()), | 64 audio_encoder_(std::move(audio_encoder)), |
| 63 enabled_(true), | 65 enabled_(true), |
| 64 bytes_pending_(0) { | 66 bytes_pending_(0) { |
| 65 thread_checker_.DetachFromThread(); | 67 thread_checker_.DetachFromThread(); |
| 66 } | 68 } |
| 67 | 69 |
| 68 AudioPump::Core::~Core() { | 70 AudioPump::Core::~Core() { |
| 69 DCHECK(thread_checker_.CalledOnValidThread()); | 71 DCHECK(thread_checker_.CalledOnValidThread()); |
| 70 } | 72 } |
| 71 | 73 |
| 72 void AudioPump::Core::Start() { | 74 void AudioPump::Core::Start() { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 92 void AudioPump::Core::EncodeAudioPacket(scoped_ptr<AudioPacket> packet) { | 94 void AudioPump::Core::EncodeAudioPacket(scoped_ptr<AudioPacket> packet) { |
| 93 DCHECK(thread_checker_.CalledOnValidThread()); | 95 DCHECK(thread_checker_.CalledOnValidThread()); |
| 94 DCHECK(packet); | 96 DCHECK(packet); |
| 95 | 97 |
| 96 int max_buffered_bytes = | 98 int max_buffered_bytes = |
| 97 audio_encoder_->GetBitrate() * kMaxBufferedIntervalMs / 1000 / 8; | 99 audio_encoder_->GetBitrate() * kMaxBufferedIntervalMs / 1000 / 8; |
| 98 if (!enabled_ || bytes_pending_ > max_buffered_bytes) | 100 if (!enabled_ || bytes_pending_ > max_buffered_bytes) |
| 99 return; | 101 return; |
| 100 | 102 |
| 101 scoped_ptr<AudioPacket> encoded_packet = | 103 scoped_ptr<AudioPacket> encoded_packet = |
| 102 audio_encoder_->Encode(packet.Pass()); | 104 audio_encoder_->Encode(std::move(packet)); |
| 103 | 105 |
| 104 // The audio encoder returns a null audio packet if there's no audio to send. | 106 // The audio encoder returns a null audio packet if there's no audio to send. |
| 105 if (!encoded_packet) | 107 if (!encoded_packet) |
| 106 return; | 108 return; |
| 107 | 109 |
| 108 int packet_size = encoded_packet->ByteSize(); | 110 int packet_size = encoded_packet->ByteSize(); |
| 109 bytes_pending_ += packet_size; | 111 bytes_pending_ += packet_size; |
| 110 | 112 |
| 111 pump_task_runner_->PostTask( | 113 pump_task_runner_->PostTask( |
| 112 FROM_HERE, base::Bind(&AudioPump::SendAudioPacket, pump_, | 114 FROM_HERE, base::Bind(&AudioPump::SendAudioPacket, pump_, |
| 113 base::Passed(&encoded_packet), packet_size)); | 115 base::Passed(&encoded_packet), packet_size)); |
| 114 } | 116 } |
| 115 | 117 |
| 116 AudioPump::AudioPump( | 118 AudioPump::AudioPump( |
| 117 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner, | 119 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner, |
| 118 scoped_ptr<AudioCapturer> audio_capturer, | 120 scoped_ptr<AudioCapturer> audio_capturer, |
| 119 scoped_ptr<AudioEncoder> audio_encoder, | 121 scoped_ptr<AudioEncoder> audio_encoder, |
| 120 protocol::AudioStub* audio_stub) | 122 protocol::AudioStub* audio_stub) |
| 121 : audio_task_runner_(audio_task_runner), | 123 : audio_task_runner_(audio_task_runner), |
| 122 audio_stub_(audio_stub), | 124 audio_stub_(audio_stub), |
| 123 weak_factory_(this) { | 125 weak_factory_(this) { |
| 124 DCHECK(audio_stub_); | 126 DCHECK(audio_stub_); |
| 125 | 127 |
| 126 core_.reset(new Core(weak_factory_.GetWeakPtr(), audio_capturer.Pass(), | 128 core_.reset(new Core(weak_factory_.GetWeakPtr(), std::move(audio_capturer), |
| 127 audio_encoder.Pass())); | 129 std::move(audio_encoder))); |
| 128 | 130 |
| 129 audio_task_runner_->PostTask( | 131 audio_task_runner_->PostTask( |
| 130 FROM_HERE, base::Bind(&Core::Start, base::Unretained(core_.get()))); | 132 FROM_HERE, base::Bind(&Core::Start, base::Unretained(core_.get()))); |
| 131 } | 133 } |
| 132 | 134 |
| 133 AudioPump::~AudioPump() { | 135 AudioPump::~AudioPump() { |
| 134 DCHECK(thread_checker_.CalledOnValidThread()); | 136 DCHECK(thread_checker_.CalledOnValidThread()); |
| 135 | 137 |
| 136 audio_task_runner_->DeleteSoon(FROM_HERE, core_.release()); | 138 audio_task_runner_->DeleteSoon(FROM_HERE, core_.release()); |
| 137 } | 139 } |
| 138 | 140 |
| 139 void AudioPump::Pause(bool pause) { | 141 void AudioPump::Pause(bool pause) { |
| 140 DCHECK(thread_checker_.CalledOnValidThread()); | 142 DCHECK(thread_checker_.CalledOnValidThread()); |
| 141 | 143 |
| 142 audio_task_runner_->PostTask( | 144 audio_task_runner_->PostTask( |
| 143 FROM_HERE, | 145 FROM_HERE, |
| 144 base::Bind(&Core::Pause, base::Unretained(core_.get()), pause)); | 146 base::Bind(&Core::Pause, base::Unretained(core_.get()), pause)); |
| 145 } | 147 } |
| 146 | 148 |
| 147 void AudioPump::SendAudioPacket(scoped_ptr<AudioPacket> packet, int size) { | 149 void AudioPump::SendAudioPacket(scoped_ptr<AudioPacket> packet, int size) { |
| 148 DCHECK(thread_checker_.CalledOnValidThread()); | 150 DCHECK(thread_checker_.CalledOnValidThread()); |
| 149 DCHECK(packet); | 151 DCHECK(packet); |
| 150 | 152 |
| 151 audio_stub_->ProcessAudioPacket( | 153 audio_stub_->ProcessAudioPacket( |
| 152 packet.Pass(), | 154 std::move(packet), |
| 153 base::Bind(&AudioPump::OnPacketSent, weak_factory_.GetWeakPtr(), size)); | 155 base::Bind(&AudioPump::OnPacketSent, weak_factory_.GetWeakPtr(), size)); |
| 154 } | 156 } |
| 155 | 157 |
| 156 void AudioPump::OnPacketSent(int size) { | 158 void AudioPump::OnPacketSent(int size) { |
| 157 audio_task_runner_->PostTask( | 159 audio_task_runner_->PostTask( |
| 158 FROM_HERE, | 160 FROM_HERE, |
| 159 base::Bind(&Core::OnPacketSent, base::Unretained(core_.get()), size)); | 161 base::Bind(&Core::OnPacketSent, base::Unretained(core_.get()), size)); |
| 160 } | 162 } |
| 161 | 163 |
| 162 } // namespace remoting | 164 } // namespace remoting |
| OLD | NEW |