Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(782)

Side by Side Diff: remoting/host/audio_pump.cc

Issue 914133003: Fix AudioPump to pause the stream when the network is congested. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@audio_pump
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/host/audio_pump.h ('k') | remoting/host/audio_pump_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/bind.h" 7 #include "base/bind.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/single_thread_task_runner.h" 10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h" 11 #include "base/thread_task_runner_handle.h"
12 #include "remoting/codec/audio_encoder.h" 12 #include "remoting/codec/audio_encoder.h"
13 #include "remoting/host/audio_capturer.h" 13 #include "remoting/host/audio_capturer.h"
14 #include "remoting/proto/audio.pb.h" 14 #include "remoting/proto/audio.pb.h"
15 #include "remoting/protocol/audio_stub.h" 15 #include "remoting/protocol/audio_stub.h"
16 16
17 namespace remoting { 17 namespace remoting {
18 18
19 // Limit the data stored in the pending send buffers to 250ms.
20 const int kMaxBufferedIntervalMs = 250;
21
19 class AudioPump::Core { 22 class AudioPump::Core {
20 public: 23 public:
21 Core(base::WeakPtr<AudioPump> pump, 24 Core(base::WeakPtr<AudioPump> pump,
22 scoped_ptr<AudioCapturer> audio_capturer, 25 scoped_ptr<AudioCapturer> audio_capturer,
23 scoped_ptr<AudioEncoder> audio_encoder); 26 scoped_ptr<AudioEncoder> audio_encoder);
24 ~Core(); 27 ~Core();
25 28
26 void Start(); 29 void Start();
27 void Pause(bool pause); 30 void Pause(bool pause);
28 31
32 void OnPacketSent(int size);
33
29 private: 34 private:
30 void EncodeAudioPacket(scoped_ptr<AudioPacket> packet); 35 void EncodeAudioPacket(scoped_ptr<AudioPacket> packet);
31 36
32 base::ThreadChecker thread_checker_; 37 base::ThreadChecker thread_checker_;
33 38
34 base::WeakPtr<AudioPump> pump_; 39 base::WeakPtr<AudioPump> pump_;
35 40
36 scoped_refptr<base::SingleThreadTaskRunner> pump_task_runner_; 41 scoped_refptr<base::SingleThreadTaskRunner> pump_task_runner_;
37 42
38 scoped_ptr<AudioCapturer> audio_capturer_; 43 scoped_ptr<AudioCapturer> audio_capturer_;
39
40 scoped_ptr<AudioEncoder> audio_encoder_; 44 scoped_ptr<AudioEncoder> audio_encoder_;
41 45
42 bool enabled_; 46 bool enabled_;
43 47
48 // Number of bytes in the queue that have been encoded but haven't been sent
49 // yet.
50 int bytes_pending_;
51
44 DISALLOW_COPY_AND_ASSIGN(Core); 52 DISALLOW_COPY_AND_ASSIGN(Core);
45 }; 53 };
46 54
47 AudioPump::Core::Core( 55 AudioPump::Core::Core(base::WeakPtr<AudioPump> pump,
48 base::WeakPtr<AudioPump> pump, 56 scoped_ptr<AudioCapturer> audio_capturer,
49 scoped_ptr<AudioCapturer> audio_capturer, 57 scoped_ptr<AudioEncoder> audio_encoder)
50 scoped_ptr<AudioEncoder> audio_encoder)
51 : pump_(pump), 58 : pump_(pump),
52 pump_task_runner_(base::ThreadTaskRunnerHandle::Get()), 59 pump_task_runner_(base::ThreadTaskRunnerHandle::Get()),
53 audio_capturer_(audio_capturer.Pass()), 60 audio_capturer_(audio_capturer.Pass()),
54 audio_encoder_(audio_encoder.Pass()), 61 audio_encoder_(audio_encoder.Pass()),
55 enabled_(true) { 62 enabled_(true),
63 bytes_pending_(0) {
56 thread_checker_.DetachFromThread(); 64 thread_checker_.DetachFromThread();
57 } 65 }
58 66
59 AudioPump::Core::~Core() { 67 AudioPump::Core::~Core() {
60 DCHECK(thread_checker_.CalledOnValidThread()); 68 DCHECK(thread_checker_.CalledOnValidThread());
61 } 69 }
62 70
63 void AudioPump::Core::Start() { 71 void AudioPump::Core::Start() {
64 DCHECK(thread_checker_.CalledOnValidThread()); 72 DCHECK(thread_checker_.CalledOnValidThread());
65 73
66 audio_capturer_->Start( 74 audio_capturer_->Start(
67 base::Bind(&Core::EncodeAudioPacket, base::Unretained(this))); 75 base::Bind(&Core::EncodeAudioPacket, base::Unretained(this)));
68 } 76 }
69 77
70 void AudioPump::Core::Pause(bool pause) { 78 void AudioPump::Core::Pause(bool pause) {
71 DCHECK(thread_checker_.CalledOnValidThread()); 79 DCHECK(thread_checker_.CalledOnValidThread());
72 80
73 enabled_ = !pause; 81 enabled_ = !pause;
74 } 82 }
75 83
84 void AudioPump::Core::OnPacketSent(int size) {
85 DCHECK(thread_checker_.CalledOnValidThread());
86
87 bytes_pending_-= size;
88 DCHECK_GE(bytes_pending_, 0);
89 }
90
76 void AudioPump::Core::EncodeAudioPacket(scoped_ptr<AudioPacket> packet) { 91 void AudioPump::Core::EncodeAudioPacket(scoped_ptr<AudioPacket> packet) {
77 DCHECK(thread_checker_.CalledOnValidThread()); 92 DCHECK(thread_checker_.CalledOnValidThread());
78 DCHECK(packet); 93 DCHECK(packet);
79 94
80 if (!enabled_) 95 int max_buffered_bytes =
96 audio_encoder_->GetBitrate() * kMaxBufferedIntervalMs / 1000 / 8;
97 if (!enabled_ || bytes_pending_ > max_buffered_bytes)
81 return; 98 return;
82 99
83 scoped_ptr<AudioPacket> encoded_packet = 100 scoped_ptr<AudioPacket> encoded_packet =
84 audio_encoder_->Encode(packet.Pass()); 101 audio_encoder_->Encode(packet.Pass());
85 102
86 // The audio encoder returns a null audio packet if there's no audio to send. 103 // The audio encoder returns a null audio packet if there's no audio to send.
87 if (!encoded_packet) 104 if (!encoded_packet)
88 return; 105 return;
89 106
90 pump_task_runner_->PostTask(FROM_HERE, 107 int packet_size = encoded_packet->ByteSize();
91 base::Bind(&AudioPump::SendAudioPacket, pump_, 108 bytes_pending_ += packet_size;
92 base::Passed(&encoded_packet))); 109
110 pump_task_runner_->PostTask(
111 FROM_HERE, base::Bind(&AudioPump::SendAudioPacket, pump_,
112 base::Passed(&encoded_packet), packet_size));
93 } 113 }
94 114
95 AudioPump::AudioPump( 115 AudioPump::AudioPump(
96 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner, 116 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner,
97 scoped_ptr<AudioCapturer> audio_capturer, 117 scoped_ptr<AudioCapturer> audio_capturer,
98 scoped_ptr<AudioEncoder> audio_encoder, 118 scoped_ptr<AudioEncoder> audio_encoder,
99 protocol::AudioStub* audio_stub) 119 protocol::AudioStub* audio_stub)
100 : audio_task_runner_(audio_task_runner), 120 : audio_task_runner_(audio_task_runner),
101 audio_stub_(audio_stub), 121 audio_stub_(audio_stub),
102 weak_factory_(this) { 122 weak_factory_(this) {
(...skipping 13 matching lines...) Expand all
116 } 136 }
117 137
118 void AudioPump::Pause(bool pause) { 138 void AudioPump::Pause(bool pause) {
119 DCHECK(thread_checker_.CalledOnValidThread()); 139 DCHECK(thread_checker_.CalledOnValidThread());
120 140
121 audio_task_runner_->PostTask( 141 audio_task_runner_->PostTask(
122 FROM_HERE, 142 FROM_HERE,
123 base::Bind(&Core::Pause, base::Unretained(core_.get()), pause)); 143 base::Bind(&Core::Pause, base::Unretained(core_.get()), pause));
124 } 144 }
125 145
126 void AudioPump::SendAudioPacket(scoped_ptr<AudioPacket> packet) { 146 void AudioPump::SendAudioPacket(scoped_ptr<AudioPacket> packet, int size) {
127 DCHECK(thread_checker_.CalledOnValidThread()); 147 DCHECK(thread_checker_.CalledOnValidThread());
128 DCHECK(packet); 148 DCHECK(packet);
129 149
130 audio_stub_->ProcessAudioPacket(packet.Pass(), base::Closure()); 150 audio_stub_->ProcessAudioPacket(
151 packet.Pass(),
152 base::Bind(&AudioPump::OnPacketSent, weak_factory_.GetWeakPtr(), size));
153 }
154
155 void AudioPump::OnPacketSent(int size) {
156 audio_task_runner_->PostTask(
157 FROM_HERE,
158 base::Bind(&Core::OnPacketSent, base::Unretained(core_.get()), size));
131 } 159 }
132 160
133 } // namespace remoting 161 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/audio_pump.h ('k') | remoting/host/audio_pump_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698