OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/base/testclient.h" | 11 #include "webrtc/base/testclient.h" |
12 #include "webrtc/base/thread.h" | 12 #include "webrtc/base/thread.h" |
13 #include "webrtc/base/timeutils.h" | 13 #include "webrtc/base/timeutils.h" |
14 | 14 |
15 namespace rtc { | 15 namespace rtc { |
16 | 16 |
17 // DESIGN: Each packet received is put it into a list of packets. | 17 // DESIGN: Each packet received is put it into a list of packets. |
18 // Callers can retrieve received packets from any thread by calling | 18 // Callers can retrieve received packets from any thread by calling |
19 // NextPacket. | 19 // NextPacket. |
20 | 20 |
21 TestClient::TestClient(AsyncPacketSocket* socket) | 21 TestClient::TestClient(AsyncPacketSocket* socket) |
22 : socket_(socket), ready_to_send_(false) { | 22 : socket_(socket), ready_to_send_(false), prev_packet_timestamp_(-1) { |
23 packets_ = new std::vector<Packet*>(); | 23 packets_ = new std::vector<Packet*>(); |
24 socket_->SignalReadPacket.connect(this, &TestClient::OnPacket); | 24 socket_->SignalReadPacket.connect(this, &TestClient::OnPacket); |
25 socket_->SignalReadyToSend.connect(this, &TestClient::OnReadyToSend); | 25 socket_->SignalReadyToSend.connect(this, &TestClient::OnReadyToSend); |
26 } | 26 } |
27 | 27 |
28 TestClient::~TestClient() { | 28 TestClient::~TestClient() { |
29 delete socket_; | 29 delete socket_; |
30 for (unsigned i = 0; i < packets_->size(); i++) | 30 for (unsigned i = 0; i < packets_->size(); i++) |
31 delete (*packets_)[i]; | 31 delete (*packets_)[i]; |
32 delete packets_; | 32 delete packets_; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 } | 84 } |
85 | 85 |
86 return packet; | 86 return packet; |
87 } | 87 } |
88 | 88 |
89 bool TestClient::CheckNextPacket(const char* buf, size_t size, | 89 bool TestClient::CheckNextPacket(const char* buf, size_t size, |
90 SocketAddress* addr) { | 90 SocketAddress* addr) { |
91 bool res = false; | 91 bool res = false; |
92 Packet* packet = NextPacket(kTimeoutMs); | 92 Packet* packet = NextPacket(kTimeoutMs); |
93 if (packet) { | 93 if (packet) { |
94 res = (packet->size == size && memcmp(packet->buf, buf, size) == 0); | 94 res = (packet->size == size && memcmp(packet->buf, buf, size) == 0 && |
| 95 CheckTimestamp(packet->packet_time.timestamp)); |
95 if (addr) | 96 if (addr) |
96 *addr = packet->addr; | 97 *addr = packet->addr; |
97 delete packet; | 98 delete packet; |
98 } | 99 } |
99 return res; | 100 return res; |
100 } | 101 } |
101 | 102 |
| 103 bool TestClient::CheckTimestamp(int64_t packet_timestamp) { |
| 104 bool res = true; |
| 105 if (packet_timestamp == -1) { |
| 106 res = false; |
| 107 } |
| 108 int64_t time_us = rtc::TimeMicros(); |
| 109 if (prev_packet_timestamp_ != -1) { |
| 110 if (packet_timestamp < prev_packet_timestamp_) { |
| 111 res = false; |
| 112 } |
| 113 const int64_t kErrorMarginUs = 20000; |
| 114 if (packet_timestamp - prev_packet_timestamp_ < |
| 115 time_us - prev_time_us_ - kErrorMarginUs || |
| 116 packet_timestamp - prev_packet_timestamp_ > |
| 117 time_us - prev_time_us_ + kErrorMarginUs) { |
| 118 res = false; |
| 119 } |
| 120 } |
| 121 prev_packet_timestamp_ = packet_timestamp; |
| 122 prev_time_us_ = time_us; |
| 123 return res; |
| 124 } |
| 125 |
102 bool TestClient::CheckNoPacket() { | 126 bool TestClient::CheckNoPacket() { |
103 bool res; | 127 bool res; |
104 Packet* packet = NextPacket(kNoPacketTimeoutMs); | 128 Packet* packet = NextPacket(kNoPacketTimeoutMs); |
105 res = (packet == NULL); | 129 res = (packet == NULL); |
106 delete packet; | 130 delete packet; |
107 return res; | 131 return res; |
108 } | 132 } |
109 | 133 |
110 int TestClient::GetError() { | 134 int TestClient::GetError() { |
111 return socket_->GetError(); | 135 return socket_->GetError(); |
112 } | 136 } |
113 | 137 |
114 int TestClient::SetOption(Socket::Option opt, int value) { | 138 int TestClient::SetOption(Socket::Option opt, int value) { |
115 return socket_->SetOption(opt, value); | 139 return socket_->SetOption(opt, value); |
116 } | 140 } |
117 | 141 |
118 bool TestClient::ready_to_send() const { | 142 bool TestClient::ready_to_send() const { |
119 return ready_to_send_; | 143 return ready_to_send_; |
120 } | 144 } |
121 | 145 |
122 void TestClient::OnPacket(AsyncPacketSocket* socket, const char* buf, | 146 void TestClient::OnPacket(AsyncPacketSocket* socket, const char* buf, |
123 size_t size, const SocketAddress& remote_addr, | 147 size_t size, const SocketAddress& remote_addr, |
124 const PacketTime& packet_time) { | 148 const PacketTime& packet_time) { |
125 CritScope cs(&crit_); | 149 CritScope cs(&crit_); |
126 packets_->push_back(new Packet(remote_addr, buf, size)); | 150 packets_->push_back(new Packet(remote_addr, buf, size, packet_time)); |
127 } | 151 } |
128 | 152 |
129 void TestClient::OnReadyToSend(AsyncPacketSocket* socket) { | 153 void TestClient::OnReadyToSend(AsyncPacketSocket* socket) { |
130 ready_to_send_ = true; | 154 ready_to_send_ = true; |
131 } | 155 } |
132 | 156 |
133 TestClient::Packet::Packet(const SocketAddress& a, const char* b, size_t s) | 157 TestClient::Packet::Packet(const SocketAddress& a, |
134 : addr(a), buf(0), size(s) { | 158 const char* b, |
| 159 size_t s, |
| 160 const PacketTime& packet_time) |
| 161 : addr(a), buf(0), size(s), packet_time(packet_time) { |
135 buf = new char[size]; | 162 buf = new char[size]; |
136 memcpy(buf, b, size); | 163 memcpy(buf, b, size); |
137 } | 164 } |
138 | 165 |
139 TestClient::Packet::Packet(const Packet& p) | 166 TestClient::Packet::Packet(const Packet& p) |
140 : addr(p.addr), buf(0), size(p.size) { | 167 : addr(p.addr), buf(0), size(p.size), packet_time(p.packet_time) { |
141 buf = new char[size]; | 168 buf = new char[size]; |
142 memcpy(buf, p.buf, size); | 169 memcpy(buf, p.buf, size); |
143 } | 170 } |
144 | 171 |
145 TestClient::Packet::~Packet() { | 172 TestClient::Packet::~Packet() { |
146 delete[] buf; | 173 delete[] buf; |
147 } | 174 } |
148 | 175 |
149 } // namespace rtc | 176 } // namespace rtc |
OLD | NEW |