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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 | 58 |
60 private: | 59 private: |
61 uint64_t bytes_; | 60 uint64_t bytes_; |
62 uint64_t packets_; | 61 uint64_t packets_; |
63 std::deque<uint64_t> byte_data_; | 62 std::deque<uint64_t> byte_data_; |
64 std::deque<uint64_t> packet_data_; | 63 std::deque<uint64_t> packet_data_; |
65 std::deque<base::TimeTicks> time_data_; | 64 std::deque<base::TimeTicks> time_data_; |
66 }; | 65 }; |
67 | 66 |
68 namespace { | 67 namespace { |
| 68 |
69 struct GlobalCounter { | 69 struct GlobalCounter { |
70 base::TimeTicks last_printout; | 70 base::TimeTicks last_printout; |
71 ByteCounter in_pipe_input_counter; | 71 ByteCounter in_pipe_input_counter; |
72 ByteCounter in_pipe_output_counter; | 72 ByteCounter in_pipe_output_counter; |
73 ByteCounter out_pipe_input_counter; | 73 ByteCounter out_pipe_input_counter; |
74 ByteCounter out_pipe_output_counter; | 74 ByteCounter out_pipe_output_counter; |
75 }; | 75 }; |
| 76 |
| 77 GlobalCounter* GetGlobalCounter() { |
| 78 static GlobalCounter* counter = new GlobalCounter(); |
| 79 return counter; |
| 80 } |
| 81 |
76 } // namespace | 82 } // namespace |
77 | 83 |
78 base::LazyInstance<GlobalCounter>::Leaky g_counter = | |
79 LAZY_INSTANCE_INITIALIZER; | |
80 | |
81 class ByteCounterPipe : public media::cast::test::PacketPipe { | 84 class ByteCounterPipe : public media::cast::test::PacketPipe { |
82 public: | 85 public: |
83 ByteCounterPipe(ByteCounter* counter) : counter_(counter) {} | 86 ByteCounterPipe(ByteCounter* counter) : counter_(counter) {} |
84 void Send(std::unique_ptr<media::cast::Packet> packet) final { | 87 void Send(std::unique_ptr<media::cast::Packet> packet) final { |
85 counter_->Increment(packet->size()); | 88 counter_->Increment(packet->size()); |
86 pipe_->Send(std::move(packet)); | 89 pipe_->Send(std::move(packet)); |
87 } | 90 } |
88 private: | 91 private: |
89 ByteCounter* counter_; | 92 ByteCounter* counter_; |
90 }; | 93 }; |
91 | 94 |
92 void SetupByteCounters(std::unique_ptr<media::cast::test::PacketPipe>* pipe, | 95 void SetupByteCounters(std::unique_ptr<media::cast::test::PacketPipe>* pipe, |
93 ByteCounter* pipe_input_counter, | 96 ByteCounter* pipe_input_counter, |
94 ByteCounter* pipe_output_counter) { | 97 ByteCounter* pipe_output_counter) { |
95 media::cast::test::PacketPipe* new_pipe = | 98 media::cast::test::PacketPipe* new_pipe = |
96 new ByteCounterPipe(pipe_input_counter); | 99 new ByteCounterPipe(pipe_input_counter); |
97 new_pipe->AppendToPipe(std::move(*pipe)); | 100 new_pipe->AppendToPipe(std::move(*pipe)); |
98 new_pipe->AppendToPipe(std::unique_ptr<media::cast::test::PacketPipe>( | 101 new_pipe->AppendToPipe(std::unique_ptr<media::cast::test::PacketPipe>( |
99 new ByteCounterPipe(pipe_output_counter))); | 102 new ByteCounterPipe(pipe_output_counter))); |
100 pipe->reset(new_pipe); | 103 pipe->reset(new_pipe); |
101 } | 104 } |
102 | 105 |
103 void CheckByteCounters() { | 106 void CheckByteCounters() { |
| 107 GlobalCounter* counter = GetGlobalCounter(); |
| 108 |
104 base::TimeTicks now = base::TimeTicks::Now(); | 109 base::TimeTicks now = base::TimeTicks::Now(); |
105 g_counter.Get().in_pipe_input_counter.push(now); | 110 counter->in_pipe_input_counter.push(now); |
106 g_counter.Get().in_pipe_output_counter.push(now); | 111 counter->in_pipe_output_counter.push(now); |
107 g_counter.Get().out_pipe_input_counter.push(now); | 112 counter->out_pipe_input_counter.push(now); |
108 g_counter.Get().out_pipe_output_counter.push(now); | 113 counter->out_pipe_output_counter.push(now); |
109 if ((now - g_counter.Get().last_printout).InSeconds() >= 5) { | 114 if ((now - counter->last_printout).InSeconds() >= 5) { |
110 fprintf(stderr, "Sending : %5.2f / %5.2f mbps %6.2f / %6.2f packets / s\n", | 115 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(), | 116 counter->in_pipe_output_counter.megabits_per_second(), |
112 g_counter.Get().in_pipe_input_counter.megabits_per_second(), | 117 counter->in_pipe_input_counter.megabits_per_second(), |
113 g_counter.Get().in_pipe_output_counter.packets_per_second(), | 118 counter->in_pipe_output_counter.packets_per_second(), |
114 g_counter.Get().in_pipe_input_counter.packets_per_second()); | 119 counter->in_pipe_input_counter.packets_per_second()); |
115 fprintf(stderr, "Receiving: %5.2f / %5.2f mbps %6.2f / %6.2f packets / s\n", | 120 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(), | 121 counter->out_pipe_output_counter.megabits_per_second(), |
117 g_counter.Get().out_pipe_input_counter.megabits_per_second(), | 122 counter->out_pipe_input_counter.megabits_per_second(), |
118 g_counter.Get().out_pipe_output_counter.packets_per_second(), | 123 counter->out_pipe_output_counter.packets_per_second(), |
119 g_counter.Get().out_pipe_input_counter.packets_per_second()); | 124 counter->out_pipe_input_counter.packets_per_second()); |
120 | 125 |
121 g_counter.Get().last_printout = now; | 126 counter->last_printout = now; |
122 } | 127 } |
123 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 128 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
124 FROM_HERE, | 129 FROM_HERE, |
125 base::Bind(&CheckByteCounters), | 130 base::Bind(&CheckByteCounters), |
126 base::TimeDelta::FromMilliseconds(100)); | 131 base::TimeDelta::FromMilliseconds(100)); |
127 } | 132 } |
128 | 133 |
129 int main(int argc, char** argv) { | 134 int main(int argc, char** argv) { |
130 base::AtExitManager at_exit; | 135 base::AtExitManager at_exit; |
131 base::CommandLine::Init(argc, argv); | 136 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(); | 186 in_pipe = media::cast::test::EvilNetwork(); |
182 out_pipe = media::cast::test::EvilNetwork(); | 187 out_pipe = media::cast::test::EvilNetwork(); |
183 } else if (network_type == "poisson-wifi") { | 188 } else if (network_type == "poisson-wifi") { |
184 in_pipe = ipp->NewBuffer(128 * 1024); | 189 in_pipe = ipp->NewBuffer(128 * 1024); |
185 out_pipe = ipp->NewBuffer(128 * 1024); | 190 out_pipe = ipp->NewBuffer(128 * 1024); |
186 } else { | 191 } else { |
187 fprintf(stderr, "Unknown network type.\n"); | 192 fprintf(stderr, "Unknown network type.\n"); |
188 exit(1); | 193 exit(1); |
189 } | 194 } |
190 | 195 |
191 SetupByteCounters(&in_pipe, &(g_counter.Get().in_pipe_input_counter), | 196 GlobalCounter* counter = GetGlobalCounter(); |
192 &(g_counter.Get().in_pipe_output_counter)); | 197 |
193 SetupByteCounters( | 198 SetupByteCounters(&in_pipe, &(counter->in_pipe_input_counter), |
194 &out_pipe, &(g_counter.Get().out_pipe_input_counter), | 199 &(counter->in_pipe_output_counter)); |
195 &(g_counter.Get().out_pipe_output_counter)); | 200 SetupByteCounters(&out_pipe, &(counter->out_pipe_input_counter), |
| 201 &(counter->out_pipe_output_counter)); |
196 | 202 |
197 printf("Press Ctrl-C when done.\n"); | 203 printf("Press Ctrl-C when done.\n"); |
198 std::unique_ptr<media::cast::test::UDPProxy> proxy( | 204 std::unique_ptr<media::cast::test::UDPProxy> proxy( |
199 media::cast::test::UDPProxy::Create(local_endpoint, remote_endpoint, | 205 media::cast::test::UDPProxy::Create(local_endpoint, remote_endpoint, |
200 std::move(in_pipe), | 206 std::move(in_pipe), |
201 std::move(out_pipe), NULL)); | 207 std::move(out_pipe), NULL)); |
202 base::MessageLoop message_loop; | 208 base::MessageLoop message_loop; |
203 g_counter.Get().last_printout = base::TimeTicks::Now(); | 209 counter->last_printout = base::TimeTicks::Now(); |
204 CheckByteCounters(); | 210 CheckByteCounters(); |
205 base::RunLoop().Run(); | 211 base::RunLoop().Run(); |
206 return 1; | 212 return 1; |
207 } | 213 } |
OLD | NEW |