OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <stdint.h> | 5 #include <stdint.h> |
6 #include <cstdio> | 6 #include <cstdio> |
7 #include <cstdlib> | 7 #include <cstdlib> |
8 #include <deque> | 8 #include <deque> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/at_exit.h" | 12 #include "base/at_exit.h" |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
15 #include "base/lazy_instance.h" | |
16 #include "base/logging.h" | 15 #include "base/logging.h" |
17 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
18 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
19 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
20 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
21 #include "media/cast/test/utility/udp_proxy.h" | 20 #include "media/cast/test/utility/udp_proxy.h" |
22 #include "net/base/ip_address.h" | 21 #include "net/base/ip_address.h" |
23 | 22 |
24 class ByteCounter { | 23 class ByteCounter { |
25 public: | 24 public: |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
68 namespace { | 67 namespace { |
69 struct GlobalCounter { | 68 struct GlobalCounter { |
70 base::TimeTicks last_printout; | 69 base::TimeTicks last_printout; |
71 ByteCounter in_pipe_input_counter; | 70 ByteCounter in_pipe_input_counter; |
72 ByteCounter in_pipe_output_counter; | 71 ByteCounter in_pipe_output_counter; |
73 ByteCounter out_pipe_input_counter; | 72 ByteCounter out_pipe_input_counter; |
74 ByteCounter out_pipe_output_counter; | 73 ByteCounter out_pipe_output_counter; |
75 }; | 74 }; |
76 } // namespace | 75 } // namespace |
77 | 76 |
78 base::LazyInstance<GlobalCounter>::Leaky g_counter = | 77 GlobalCounter* GetGlobalCounter() { |
Mark Mentovai
2017/01/31 21:33:56
This should live in the unnamed namespace immediat
DaleCurtis
2017/01/31 22:04:33
Done, though this whole class is a bit wonky since
| |
79 LAZY_INSTANCE_INITIALIZER; | 78 static GlobalCounter* counter = new GlobalCounter(); |
79 return counter; | |
80 } | |
80 | 81 |
81 class ByteCounterPipe : public media::cast::test::PacketPipe { | 82 class ByteCounterPipe : public media::cast::test::PacketPipe { |
82 public: | 83 public: |
83 ByteCounterPipe(ByteCounter* counter) : counter_(counter) {} | 84 ByteCounterPipe(ByteCounter* counter) : counter_(counter) {} |
84 void Send(std::unique_ptr<media::cast::Packet> packet) final { | 85 void Send(std::unique_ptr<media::cast::Packet> packet) final { |
85 counter_->Increment(packet->size()); | 86 counter_->Increment(packet->size()); |
86 pipe_->Send(std::move(packet)); | 87 pipe_->Send(std::move(packet)); |
87 } | 88 } |
88 private: | 89 private: |
89 ByteCounter* counter_; | 90 ByteCounter* counter_; |
90 }; | 91 }; |
91 | 92 |
92 void SetupByteCounters(std::unique_ptr<media::cast::test::PacketPipe>* pipe, | 93 void SetupByteCounters(std::unique_ptr<media::cast::test::PacketPipe>* pipe, |
93 ByteCounter* pipe_input_counter, | 94 ByteCounter* pipe_input_counter, |
94 ByteCounter* pipe_output_counter) { | 95 ByteCounter* pipe_output_counter) { |
95 media::cast::test::PacketPipe* new_pipe = | 96 media::cast::test::PacketPipe* new_pipe = |
96 new ByteCounterPipe(pipe_input_counter); | 97 new ByteCounterPipe(pipe_input_counter); |
97 new_pipe->AppendToPipe(std::move(*pipe)); | 98 new_pipe->AppendToPipe(std::move(*pipe)); |
98 new_pipe->AppendToPipe(std::unique_ptr<media::cast::test::PacketPipe>( | 99 new_pipe->AppendToPipe(std::unique_ptr<media::cast::test::PacketPipe>( |
99 new ByteCounterPipe(pipe_output_counter))); | 100 new ByteCounterPipe(pipe_output_counter))); |
100 pipe->reset(new_pipe); | 101 pipe->reset(new_pipe); |
101 } | 102 } |
102 | 103 |
103 void CheckByteCounters() { | 104 void CheckByteCounters() { |
105 GlobalCounter* counter = GetGlobalCounter(); | |
106 | |
104 base::TimeTicks now = base::TimeTicks::Now(); | 107 base::TimeTicks now = base::TimeTicks::Now(); |
105 g_counter.Get().in_pipe_input_counter.push(now); | 108 counter->in_pipe_input_counter.push(now); |
106 g_counter.Get().in_pipe_output_counter.push(now); | 109 counter->in_pipe_output_counter.push(now); |
107 g_counter.Get().out_pipe_input_counter.push(now); | 110 counter->out_pipe_input_counter.push(now); |
108 g_counter.Get().out_pipe_output_counter.push(now); | 111 counter->out_pipe_output_counter.push(now); |
109 if ((now - g_counter.Get().last_printout).InSeconds() >= 5) { | 112 if ((now - counter->last_printout).InSeconds() >= 5) { |
110 fprintf(stderr, "Sending : %5.2f / %5.2f mbps %6.2f / %6.2f packets / s\n", | 113 fprintf(stderr, "Sending : %5.2f / %5.2f mbps %6.2f / %6.2f packets / s\n", |
111 g_counter.Get().in_pipe_output_counter.megabits_per_second(), | 114 counter->in_pipe_output_counter.megabits_per_second(), |
112 g_counter.Get().in_pipe_input_counter.megabits_per_second(), | 115 counter->in_pipe_input_counter.megabits_per_second(), |
113 g_counter.Get().in_pipe_output_counter.packets_per_second(), | 116 counter->in_pipe_output_counter.packets_per_second(), |
114 g_counter.Get().in_pipe_input_counter.packets_per_second()); | 117 counter->in_pipe_input_counter.packets_per_second()); |
115 fprintf(stderr, "Receiving: %5.2f / %5.2f mbps %6.2f / %6.2f packets / s\n", | 118 fprintf(stderr, "Receiving: %5.2f / %5.2f mbps %6.2f / %6.2f packets / s\n", |
116 g_counter.Get().out_pipe_output_counter.megabits_per_second(), | 119 counter->out_pipe_output_counter.megabits_per_second(), |
117 g_counter.Get().out_pipe_input_counter.megabits_per_second(), | 120 counter->out_pipe_input_counter.megabits_per_second(), |
118 g_counter.Get().out_pipe_output_counter.packets_per_second(), | 121 counter->out_pipe_output_counter.packets_per_second(), |
119 g_counter.Get().out_pipe_input_counter.packets_per_second()); | 122 counter->out_pipe_input_counter.packets_per_second()); |
120 | 123 |
121 g_counter.Get().last_printout = now; | 124 counter->last_printout = now; |
122 } | 125 } |
123 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 126 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
124 FROM_HERE, | 127 FROM_HERE, |
125 base::Bind(&CheckByteCounters), | 128 base::Bind(&CheckByteCounters), |
126 base::TimeDelta::FromMilliseconds(100)); | 129 base::TimeDelta::FromMilliseconds(100)); |
127 } | 130 } |
128 | 131 |
129 int main(int argc, char** argv) { | 132 int main(int argc, char** argv) { |
130 base::AtExitManager at_exit; | 133 base::AtExitManager at_exit; |
131 base::CommandLine::Init(argc, argv); | 134 base::CommandLine::Init(argc, argv); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 in_pipe = media::cast::test::EvilNetwork(); | 184 in_pipe = media::cast::test::EvilNetwork(); |
182 out_pipe = media::cast::test::EvilNetwork(); | 185 out_pipe = media::cast::test::EvilNetwork(); |
183 } else if (network_type == "poisson-wifi") { | 186 } else if (network_type == "poisson-wifi") { |
184 in_pipe = ipp->NewBuffer(128 * 1024); | 187 in_pipe = ipp->NewBuffer(128 * 1024); |
185 out_pipe = ipp->NewBuffer(128 * 1024); | 188 out_pipe = ipp->NewBuffer(128 * 1024); |
186 } else { | 189 } else { |
187 fprintf(stderr, "Unknown network type.\n"); | 190 fprintf(stderr, "Unknown network type.\n"); |
188 exit(1); | 191 exit(1); |
189 } | 192 } |
190 | 193 |
191 SetupByteCounters(&in_pipe, &(g_counter.Get().in_pipe_input_counter), | 194 GlobalCounter* counter = GetGlobalCounter(); |
192 &(g_counter.Get().in_pipe_output_counter)); | 195 |
193 SetupByteCounters( | 196 SetupByteCounters(&in_pipe, &(counter->in_pipe_input_counter), |
194 &out_pipe, &(g_counter.Get().out_pipe_input_counter), | 197 &(counter->in_pipe_output_counter)); |
195 &(g_counter.Get().out_pipe_output_counter)); | 198 SetupByteCounters(&out_pipe, &(counter->out_pipe_input_counter), |
199 &(counter->out_pipe_output_counter)); | |
196 | 200 |
197 printf("Press Ctrl-C when done.\n"); | 201 printf("Press Ctrl-C when done.\n"); |
198 std::unique_ptr<media::cast::test::UDPProxy> proxy( | 202 std::unique_ptr<media::cast::test::UDPProxy> proxy( |
199 media::cast::test::UDPProxy::Create(local_endpoint, remote_endpoint, | 203 media::cast::test::UDPProxy::Create(local_endpoint, remote_endpoint, |
200 std::move(in_pipe), | 204 std::move(in_pipe), |
201 std::move(out_pipe), NULL)); | 205 std::move(out_pipe), NULL)); |
202 base::MessageLoop message_loop; | 206 base::MessageLoop message_loop; |
203 g_counter.Get().last_printout = base::TimeTicks::Now(); | 207 counter->last_printout = base::TimeTicks::Now(); |
204 CheckByteCounters(); | 208 CheckByteCounters(); |
205 base::RunLoop().Run(); | 209 base::RunLoop().Run(); |
206 return 1; | 210 return 1; |
207 } | 211 } |
OLD | NEW |