Update PacketSource and RtpFileSource

The NextPacket method should now return NULL when the end of the
source was reached. In the RtpFileSource, this means that when
the end of file is reached, NULL is returned. Also, when an RTCP
packet is encountered, the next packet will be read from file
immediately.

R=kwiberg@webrtc.org

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

git-svn-id: http://webrtc.googlecode.com/svn/trunk@6479 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/modules/audio_coding/neteq/tools/packet_source.h b/webrtc/modules/audio_coding/neteq/tools/packet_source.h
index c539b8e..669bc14 100644
--- a/webrtc/modules/audio_coding/neteq/tools/packet_source.h
+++ b/webrtc/modules/audio_coding/neteq/tools/packet_source.h
@@ -24,7 +24,8 @@
   PacketSource() {}
   virtual ~PacketSource() {}
 
-  // Returns a pointer to the next packet.
+  // Returns a pointer to the next packet. Returns NULL if the source is
+  // depleted, or if an error occurred.
   virtual Packet* NextPacket() = 0;
 
  private:
diff --git a/webrtc/modules/audio_coding/neteq/tools/rtp_file_source.cc b/webrtc/modules/audio_coding/neteq/tools/rtp_file_source.cc
index 8278635..6490d46 100644
--- a/webrtc/modules/audio_coding/neteq/tools/rtp_file_source.cc
+++ b/webrtc/modules/audio_coding/neteq/tools/rtp_file_source.cc
@@ -47,47 +47,54 @@
 }
 
 Packet* RtpFileSource::NextPacket() {
-  uint16_t length;
-  if (fread(&length, sizeof(uint16_t), 1, in_file_) == 0) {
-    assert(false);
-    return NULL;
-  }
-  length = ntohs(length);
+  while (!EndOfFile()) {
+    uint16_t length;
+    if (fread(&length, sizeof(length), 1, in_file_) == 0) {
+      assert(false);
+      return NULL;
+    }
+    length = ntohs(length);
 
-  uint16_t plen;
-  if (fread(&plen, sizeof(uint16_t), 1, in_file_) == 0) {
-    assert(false);
-    return NULL;
-  }
-  plen = ntohs(plen);
+    uint16_t plen;
+    if (fread(&plen, sizeof(plen), 1, in_file_) == 0) {
+      assert(false);
+      return NULL;
+    }
+    plen = ntohs(plen);
 
-  uint32_t offset;
-  if (fread(&offset, sizeof(uint32_t), 1, in_file_) == 0) {
-    assert(false);
-    return NULL;
-  }
+    uint32_t offset;
+    if (fread(&offset, sizeof(offset), 1, in_file_) == 0) {
+      assert(false);
+      return NULL;
+    }
+    offset = ntohl(offset);
 
-  // Use length here because a plen of 0 specifies RTCP.
-  size_t packet_size_bytes = length - kPacketHeaderSize;
-  if (packet_size_bytes <= 0) {
-    // May be an RTCP packet.
-    return NULL;
+    // Use length here because a plen of 0 specifies RTCP.
+    assert(length >= kPacketHeaderSize);
+    size_t packet_size_bytes = length - kPacketHeaderSize;
+    if (packet_size_bytes == 0) {
+      // May be an RTCP packet.
+      // Read the next one.
+      continue;
+    }
+    scoped_ptr<uint8_t> packet_memory(new uint8_t[packet_size_bytes]);
+    if (fread(packet_memory.get(), 1, packet_size_bytes, in_file_) !=
+        packet_size_bytes) {
+      assert(false);
+      return NULL;
+    }
+    scoped_ptr<Packet> packet(new Packet(packet_memory.release(),
+                                         packet_size_bytes,
+                                         plen,
+                                         offset,
+                                         *parser_.get()));
+    if (!packet->valid_header()) {
+      assert(false);
+      return NULL;
+    }
+    return packet.release();
   }
-  uint8_t* packet_memory = new uint8_t[packet_size_bytes];
-  if (fread(packet_memory, 1, packet_size_bytes, in_file_) !=
-      packet_size_bytes) {
-    assert(false);
-    delete[] packet_memory;
-    return NULL;
-  }
-  Packet* packet = new Packet(
-      packet_memory, packet_size_bytes, plen, ntohl(offset), *parser_.get());
-  if (!packet->valid_header()) {
-    assert(false);
-    delete packet;
-    return NULL;
-  }
-  return packet;
+  return NULL;
 }
 
 bool RtpFileSource::EndOfFile() const {
diff --git a/webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h b/webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h
index 527018e..6b92a88 100644
--- a/webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h
+++ b/webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h
@@ -37,7 +37,8 @@
   // Registers an RTP header extension and binds it to |id|.
   virtual bool RegisterRtpHeaderExtension(RTPExtensionType type, uint8_t id);
 
-  // Returns a pointer to the next packet.
+  // Returns a pointer to the next packet. Returns NULL if end of file was
+  // reached, or if a the data was corrupt.
   virtual Packet* NextPacket();
 
   // Returns true if the end of file has been reached.