Read recv timestamps from socket (posix only).

This helps a lot on Android devices where the user threads can be scheduled with low priority when the app is in the background, causing spurious significantly delayed before a packet can be read from the socket. With this patch the timestamp is taken by the kernel when the packet actually arrives.

R=juberti@chromium.org
TBR=juberti@webrtc.org

BUG=webrtc:5773

Review URL: https://codereview.webrtc.org/1944683002 .

Cr-Commit-Position: refs/heads/master@{#12850}
diff --git a/webrtc/base/socket_unittest.cc b/webrtc/base/socket_unittest.cc
index 718903d..ea37f23 100644
--- a/webrtc/base/socket_unittest.cc
+++ b/webrtc/base/socket_unittest.cc
@@ -184,6 +184,10 @@
   GetSetOptionsInternal(kIPv6Loopback);
 }
 
+void SocketTest::TestSocketRecvTimestamp() {
+  SocketRecvTimestamp(kIPv4Loopback);
+}
+
 // For unbound sockets, GetLocalAddress / GetRemoteAddress return AF_UNSPEC
 // values on Windows, but an empty address of the same family on Linux/MacOS X.
 bool IsUnspecOrEmptyIP(const IPAddress& address) {
@@ -541,7 +545,7 @@
 
   // Ensure the data can be read.
   char buffer[10];
-  EXPECT_EQ(1, client->Recv(buffer, sizeof(buffer)));
+  EXPECT_EQ(1, client->Recv(buffer, sizeof(buffer), nullptr));
   EXPECT_EQ('a', buffer[0]);
 
   // Now we should close, but the remote address will remain.
@@ -673,7 +677,7 @@
 
   // But should signal when process_io is true.
   EXPECT_TRUE_WAIT((sink.Check(accepted.get(), testing::SSE_READ)), kTimeout);
-  EXPECT_LT(0, accepted->Recv(buf, 1024));
+  EXPECT_LT(0, accepted->Recv(buf, 1024, nullptr));
 }
 
 void SocketTest::TcpInternal(const IPAddress& loopback, size_t data_size,
@@ -763,7 +767,7 @@
 
       // Receive as much as we can get in a single recv call.
       char recved_data[data_size];
-      int recved_size = receiver->Recv(recved_data, data_size);
+      int recved_size = receiver->Recv(recved_data, data_size, nullptr);
 
       if (!recv_called) {
         // The first Recv() after getting readability should succeed and receive
@@ -850,7 +854,7 @@
 
   // Pull data.
   for (int i = 0; i < sends; ++i) {
-    client->Recv(buf, arraysize(buf));
+    client->Recv(buf, arraysize(buf), nullptr);
   }
 
   // Expect at least one additional writable callback.
@@ -1023,4 +1027,25 @@
   }
 }
 
+void SocketTest::SocketRecvTimestamp(const IPAddress& loopback) {
+  std::unique_ptr<Socket> socket(
+      ss_->CreateSocket(loopback.family(), SOCK_DGRAM));
+  EXPECT_EQ(0, socket->Bind(SocketAddress(loopback, 0)));
+  SocketAddress address = socket->GetLocalAddress();
+
+  socket->SendTo("foo", 3, address);
+  int64_t timestamp;
+  char buffer[3];
+  socket->RecvFrom(buffer, 3, nullptr, &timestamp);
+  EXPECT_GT(timestamp, -1);
+  int64_t prev_timestamp = timestamp;
+
+  const int64_t kTimeBetweenPacketsMs = 10;
+  Thread::SleepMs(kTimeBetweenPacketsMs);
+
+  socket->SendTo("bar", 3, address);
+  socket->RecvFrom(buffer, 3, nullptr, &timestamp);
+  EXPECT_NEAR(timestamp, prev_timestamp + kTimeBetweenPacketsMs * 1000, 2000);
+}
+
 }  // namespace rtc