Count total bytes sent in RTPSender::Bytes().

Previously only media bytes were included, this adds header bytes and
padding bytes to the calculation.

BUG=
R=stefan@webrtc.org, tommi@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/19939004

git-svn-id: http://webrtc.googlecode.com/svn/trunk/webrtc@6654 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/common_types.h b/common_types.h
index 193e281..796ddca 100644
--- a/common_types.h
+++ b/common_types.h
@@ -225,6 +225,7 @@
      retransmitted_packets(0),
      fec_packets(0) {}
 
+  // TODO(pbos): Rename bytes -> media_bytes.
   uint32_t bytes;  // Payload bytes, excluding RTP headers and padding.
   uint32_t header_bytes;  // Number of bytes used by RTP headers.
   uint32_t padding_bytes;  // Number of padding bytes.
diff --git a/modules/rtp_rtcp/source/rtp_sender.cc b/modules/rtp_rtcp/source/rtp_sender.cc
index c98d498..e638074 100644
--- a/modules/rtp_rtcp/source/rtp_sender.cc
+++ b/modules/rtp_rtcp/source/rtp_sender.cc
@@ -509,7 +509,7 @@
     last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
   }
   int bytes_sent = SendPadData(payload_type, timestamp, capture_time_ms,
-                               bytes, kDontRetransmit, false, false);
+                               bytes, false, false);
   // We did not manage to send all bytes. Comparing with 31 due to modulus 32.
   return bytes - bytes_sent < 31;
 }
@@ -533,10 +533,12 @@
   return padding_bytes_in_packet;
 }
 
-int RTPSender::SendPadData(int payload_type, uint32_t timestamp,
-                           int64_t capture_time_ms, int32_t bytes,
-                           StorageType store, bool force_full_size_packets,
-                           bool only_pad_after_markerbit) {
+int RTPSender::SendPadData(int payload_type,
+                           uint32_t timestamp,
+                           int64_t capture_time_ms,
+                           int32_t bytes,
+                           bool force_full_size_packets,
+                           bool over_rtx) {
   // Drop this packet if we're not sending media packets.
   if (!SendingMedia()) {
     return bytes;
@@ -565,7 +567,7 @@
       CriticalSectionScoped cs(send_critsect_);
       // Only send padding packets following the last packet of a frame,
       // indicated by the marker bit.
-      if (only_pad_after_markerbit && !last_packet_marker_bit_)
+      if (!over_rtx && !last_packet_marker_bit_)
         return bytes_sent;
       if (rtx_ == kRtxOff) {
         ssrc = ssrc_;
@@ -579,19 +581,35 @@
     }
 
     uint8_t padding_packet[IP_PACKET_SIZE];
-    int header_length = CreateRTPHeader(padding_packet, payload_type, ssrc,
-                                        false, timestamp, sequence_number, NULL,
+    int header_length = CreateRTPHeader(padding_packet,
+                                        payload_type,
+                                        ssrc,
+                                        false,
+                                        timestamp,
+                                        sequence_number,
+                                        NULL,
                                         0);
-    padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length,
-                                                 bytes);
-    if (0 > SendToNetwork(padding_packet, padding_bytes_in_packet,
-                          header_length, capture_time_ms, store,
-                          PacedSender::kLowPriority)) {
-      // Error sending the packet.
-      break;
+    padding_bytes_in_packet =
+        BuildPaddingPacket(padding_packet, header_length, bytes);
+    int length = padding_bytes_in_packet + header_length;
+    int64_t now_ms = clock_->TimeInMilliseconds();
+
+    RtpUtility::RtpHeaderParser rtp_parser(padding_packet, length);
+    RTPHeader rtp_header;
+    rtp_parser.Parse(rtp_header);
+
+    if (capture_time_ms > 0) {
+      UpdateTransmissionTimeOffset(
+          padding_packet, length, rtp_header, now_ms - capture_time_ms);
     }
+
+    UpdateAbsoluteSendTime(padding_packet, length, rtp_header, now_ms);
+    if (!SendPacketToNetwork(padding_packet, length))
+      break;
     bytes_sent += padding_bytes_in_packet;
+    UpdateRtpStats(padding_packet, length, rtp_header, over_rtx, false);
   }
+
   return bytes_sent;
 }
 
@@ -919,9 +937,8 @@
                                    timestamp,
                                    capture_time_ms,
                                    bytes,
-                                   kDontStore,
                                    true,
-                                   rtx == kRtxOff);
+                                   rtx != kRtxOff);
     bytes_sent += padding_sent;
   }
   return bytes_sent;
@@ -1036,7 +1053,9 @@
 // Number of sent RTP bytes.
 uint32_t RTPSender::Bytes() const {
   CriticalSectionScoped lock(statistics_crit_.get());
-  return rtp_stats_.bytes + rtx_rtp_stats_.bytes;
+  return rtp_stats_.bytes + rtp_stats_.header_bytes + rtp_stats_.padding_bytes +
+         rtx_rtp_stats_.bytes + rtx_rtp_stats_.header_bytes +
+         rtx_rtp_stats_.padding_bytes;
 }
 
 int RTPSender::CreateRTPHeader(
diff --git a/modules/rtp_rtcp/source/rtp_sender.h b/modules/rtp_rtcp/source/rtp_sender.h
index 265e69c..f65c8c2 100644
--- a/modules/rtp_rtcp/source/rtp_sender.h
+++ b/modules/rtp_rtcp/source/rtp_sender.h
@@ -266,9 +266,12 @@
   int32_t SetFecParameters(const FecProtectionParams *delta_params,
                            const FecProtectionParams *key_params);
 
-  int SendPadData(int payload_type, uint32_t timestamp, int64_t capture_time_ms,
-                  int32_t bytes, StorageType store,
-                  bool force_full_size_packets, bool only_pad_after_markerbit);
+  int SendPadData(int payload_type,
+                  uint32_t timestamp,
+                  int64_t capture_time_ms,
+                  int32_t bytes,
+                  bool force_full_size_packets,
+                  bool only_pad_after_markerbit);
 
   // Called on update of RTP statistics.
   void RegisterRtpStatisticsCallback(StreamDataCountersCallback* callback);
diff --git a/modules/rtp_rtcp/source/rtp_sender_unittest.cc b/modules/rtp_rtcp/source/rtp_sender_unittest.cc
index 42ee023..57f1460 100644
--- a/modules/rtp_rtcp/source/rtp_sender_unittest.cc
+++ b/modules/rtp_rtcp/source/rtp_sender_unittest.cc
@@ -62,13 +62,12 @@
 class LoopbackTransportTest : public webrtc::Transport {
  public:
   LoopbackTransportTest()
-    : packets_sent_(0),
-      last_sent_packet_len_(0) {
-  }
+      : packets_sent_(0), last_sent_packet_len_(0), total_bytes_sent_(0) {}
   virtual int SendPacket(int channel, const void *data, int len) {
     packets_sent_++;
     memcpy(last_sent_packet_, data, len);
     last_sent_packet_len_ = len;
+    total_bytes_sent_ += static_cast<size_t>(len);
     return len;
   }
   virtual int SendRTCPPacket(int channel, const void *data, int len) {
@@ -76,6 +75,7 @@
   }
   int packets_sent_;
   int last_sent_packet_len_;
+  size_t total_bytes_sent_;
   uint8_t last_sent_packet_[kMaxPacketLength];
 };
 
@@ -1071,4 +1071,35 @@
                       sizeof(extension)));
 }
 
+TEST_F(RtpSenderTest, BytesReportedCorrectly) {
+  const char* kPayloadName = "GENERIC";
+  const uint8_t kPayloadType = 127;
+  rtp_sender_->SetSSRC(1234);
+  rtp_sender_->SetRtxSsrc(4321);
+  rtp_sender_->SetRtxPayloadType(kPayloadType - 1);
+  rtp_sender_->SetRTXStatus(kRtxRetransmitted | kRtxRedundantPayloads);
+
+  ASSERT_EQ(
+      0,
+      rtp_sender_->RegisterPayload(kPayloadName, kPayloadType, 90000, 0, 1500));
+  uint8_t payload[] = {47, 11, 32, 93, 89};
+
+  ASSERT_EQ(0,
+            rtp_sender_->SendOutgoingData(kVideoFrameKey,
+                                          kPayloadType,
+                                          1234,
+                                          4321,
+                                          payload,
+                                          sizeof(payload),
+                                          0));
+
+  EXPECT_GT(transport_.total_bytes_sent_, 0u);
+  EXPECT_EQ(transport_.total_bytes_sent_, rtp_sender_->Bytes());
+  size_t last_bytes_sent = transport_.total_bytes_sent_;
+
+  rtp_sender_->TimeToSendPadding(42);
+
+  EXPECT_GT(transport_.total_bytes_sent_, last_bytes_sent);
+  EXPECT_EQ(transport_.total_bytes_sent_, rtp_sender_->Bytes());
+}
 }  // namespace webrtc