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

Side by Side Diff: webrtc/base/testclient.cc

Issue 1944683002: Read recv timestamps from socket (posix only). (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Disable ipv6 test on Linux due to bot issues. Created 4 years, 7 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
OLDNEW
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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 } 83 }
84 84
85 return packet; 85 return packet;
86 } 86 }
87 87
88 bool TestClient::CheckNextPacket(const char* buf, size_t size, 88 bool TestClient::CheckNextPacket(const char* buf, size_t size,
89 SocketAddress* addr) { 89 SocketAddress* addr) {
90 bool res = false; 90 bool res = false;
91 Packet* packet = NextPacket(kTimeoutMs); 91 Packet* packet = NextPacket(kTimeoutMs);
92 if (packet) { 92 if (packet) {
93 res = (packet->size == size && memcmp(packet->buf, buf, size) == 0); 93 res = (packet->size == size && memcmp(packet->buf, buf, size) == 0 &&
94 CheckTimestamp(packet->packet_time.timestamp));
94 if (addr) 95 if (addr)
95 *addr = packet->addr; 96 *addr = packet->addr;
96 delete packet; 97 delete packet;
97 } 98 }
98 return res; 99 return res;
99 } 100 }
100 101
102 bool TestClient::CheckTimestamp(int64_t packet_timestamp) {
103 bool res = true;
104 if (packet_timestamp == -1) {
105 res = false;
106 }
107 int64_t time_us = rtc::TimeMicros();
108 if (prev_packet_timestamp_ != -1) {
109 if (packet_timestamp < prev_packet_timestamp_) {
110 res = false;
111 }
112 const int64_t kErrorMarginUs = 20000;
113 if (packet_timestamp - prev_packet_timestamp_ <
114 time_us - prev_time_us_ - kErrorMarginUs ||
115 packet_timestamp - prev_packet_timestamp_ >
116 time_us - prev_time_us_ + kErrorMarginUs) {
117 res = false;
118 }
119 }
120 prev_packet_timestamp_ = packet_timestamp;
121 prev_time_us_ = time_us;
122 return res;
123 }
124
101 bool TestClient::CheckNoPacket() { 125 bool TestClient::CheckNoPacket() {
102 bool res; 126 bool res;
103 Packet* packet = NextPacket(kNoPacketTimeoutMs); 127 Packet* packet = NextPacket(kNoPacketTimeoutMs);
104 res = (packet == NULL); 128 res = (packet == NULL);
105 delete packet; 129 delete packet;
106 return res; 130 return res;
107 } 131 }
108 132
109 int TestClient::GetError() { 133 int TestClient::GetError() {
110 return socket_->GetError(); 134 return socket_->GetError();
111 } 135 }
112 136
113 int TestClient::SetOption(Socket::Option opt, int value) { 137 int TestClient::SetOption(Socket::Option opt, int value) {
114 return socket_->SetOption(opt, value); 138 return socket_->SetOption(opt, value);
115 } 139 }
116 140
117 bool TestClient::ready_to_send() const { 141 bool TestClient::ready_to_send() const {
118 return ready_to_send_; 142 return ready_to_send_;
119 } 143 }
120 144
121 void TestClient::OnPacket(AsyncPacketSocket* socket, const char* buf, 145 void TestClient::OnPacket(AsyncPacketSocket* socket, const char* buf,
122 size_t size, const SocketAddress& remote_addr, 146 size_t size, const SocketAddress& remote_addr,
123 const PacketTime& packet_time) { 147 const PacketTime& packet_time) {
124 CritScope cs(&crit_); 148 CritScope cs(&crit_);
125 packets_->push_back(new Packet(remote_addr, buf, size)); 149 packets_->push_back(new Packet(remote_addr, buf, size, packet_time));
126 } 150 }
127 151
128 void TestClient::OnReadyToSend(AsyncPacketSocket* socket) { 152 void TestClient::OnReadyToSend(AsyncPacketSocket* socket) {
129 ready_to_send_ = true; 153 ready_to_send_ = true;
130 } 154 }
131 155
132 TestClient::Packet::Packet(const SocketAddress& a, const char* b, size_t s) 156 TestClient::Packet::Packet(const SocketAddress& a,
133 : addr(a), buf(0), size(s) { 157 const char* b,
158 size_t s,
159 const PacketTime& packet_time)
160 : addr(a), buf(0), size(s), packet_time(packet_time) {
134 buf = new char[size]; 161 buf = new char[size];
135 memcpy(buf, b, size); 162 memcpy(buf, b, size);
136 } 163 }
137 164
138 TestClient::Packet::Packet(const Packet& p) 165 TestClient::Packet::Packet(const Packet& p)
139 : addr(p.addr), buf(0), size(p.size) { 166 : addr(p.addr), buf(0), size(p.size), packet_time(p.packet_time) {
140 buf = new char[size]; 167 buf = new char[size];
141 memcpy(buf, p.buf, size); 168 memcpy(buf, p.buf, size);
142 } 169 }
143 170
144 TestClient::Packet::~Packet() { 171 TestClient::Packet::~Packet() {
145 delete[] buf; 172 delete[] buf;
146 } 173 }
147 174
148 } // namespace rtc 175 } // namespace rtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698