NetEq: Changed Packet::payload to be an rtc::Buffer

That is, rather than keeping a separate pointer and size.
This helps automate memory management in NetEq and will be useful in the
work to minimize the AudioDecoder interface as part of the injectable
audio codec work.

I'm planning a follow-up that will change the current management of Packet* to wrapping them in unique_ptr instead.

Review-Url: https://codereview.webrtc.org/2289093003
Cr-Commit-Position: refs/heads/master@{#14002}
diff --git a/webrtc/modules/audio_coding/neteq/comfort_noise.cc b/webrtc/modules/audio_coding/neteq/comfort_noise.cc
index 90b02da..a57ced1 100644
--- a/webrtc/modules/audio_coding/neteq/comfort_noise.cc
+++ b/webrtc/modules/audio_coding/neteq/comfort_noise.cc
@@ -29,15 +29,12 @@
   // Get comfort noise decoder.
   if (decoder_database_->SetActiveCngDecoder(packet->header.payloadType)
       != kOK) {
-    delete [] packet->payload;
     delete packet;
     return kUnknownPayloadType;
   }
   ComfortNoiseDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
   RTC_DCHECK(cng_decoder);
-  cng_decoder->UpdateSid(rtc::ArrayView<const uint8_t>(
-      packet->payload, packet->payload_length));
-  delete [] packet->payload;
+  cng_decoder->UpdateSid(packet->payload);
   delete packet;
   return kOK;
 }
diff --git a/webrtc/modules/audio_coding/neteq/neteq_impl.cc b/webrtc/modules/audio_coding/neteq/neteq_impl.cc
index 8c36dd9..b911d5a 100644
--- a/webrtc/modules/audio_coding/neteq/neteq_impl.cc
+++ b/webrtc/modules/audio_coding/neteq/neteq_impl.cc
@@ -559,17 +559,11 @@
     packet->header.timestamp = rtp_header.header.timestamp;
     packet->header.ssrc = rtp_header.header.ssrc;
     packet->header.numCSRCs = 0;
-    packet->payload_length = payload.size();
+    packet->payload.SetData(payload.data(), payload.size());
     packet->primary = true;
     // Waiting time will be set upon inserting the packet in the buffer.
     RTC_DCHECK(!packet->waiting_time);
-    packet->payload = new uint8_t[packet->payload_length];
     packet->sync_packet = is_sync_packet;
-    if (!packet->payload) {
-      LOG_F(LS_ERROR) << "Payload pointer is NULL.";
-    }
-    assert(!payload.empty());  // Already checked above.
-    memcpy(packet->payload, payload.data(), packet->payload_length);
     // Insert packet in a packet list.
     packet_list.push_back(packet);
     // Save main payloads header for later.
@@ -639,15 +633,13 @@
   while (it != packet_list.end()) {
     Packet* current_packet = (*it);
     assert(current_packet);
-    assert(current_packet->payload);
+    assert(!current_packet->payload.empty());
     if (decoder_database_->IsDtmf(current_packet->header.payloadType)) {
       assert(!current_packet->sync_packet);  // We had a sanity check for this.
       DtmfEvent event;
-      int ret = DtmfBuffer::ParseEvent(
-          current_packet->header.timestamp,
-          current_packet->payload,
-          current_packet->payload_length,
-          &event);
+      int ret = DtmfBuffer::ParseEvent(current_packet->header.timestamp,
+                                       current_packet->payload.data(),
+                                       current_packet->payload.size(), &event);
       if (ret != DtmfBuffer::kOK) {
         PacketBuffer::DeleteAllPackets(&packet_list);
         return kDtmfParsingError;
@@ -656,8 +648,6 @@
         PacketBuffer::DeleteAllPackets(&packet_list);
         return kDtmfInsertError;
       }
-      // TODO(hlundin): Let the destructor of Packet handle the payload.
-      delete [] current_packet->payload;
       delete current_packet;
       it = packet_list.erase(it);
     } else {
@@ -702,8 +692,8 @@
         decoder_database_->GetDecoder(main_header.payloadType);
     assert(decoder);  // Should always get a valid object, since we have
     // already checked that the payload types are known.
-    decoder->IncomingPacket(packet_list.front()->payload,
-                            packet_list.front()->payload_length,
+    decoder->IncomingPacket(packet_list.front()->payload.data(),
+                            packet_list.front()->payload.size(),
                             packet_list.front()->header.sequenceNumber,
                             packet_list.front()->header.timestamp,
                             receive_timestamp);
@@ -1468,7 +1458,7 @@
            operation == kFastAccelerate || operation == kMerge ||
            operation == kPreemptiveExpand);
     packet_list->pop_front();
-    size_t payload_length = packet->payload_length;
+    const size_t payload_length = packet->payload.size();
     int decode_length;
     if (packet->sync_packet) {
       // Decode to silence with the same frame size as the last decode.
@@ -1479,18 +1469,16 @@
     } else if (!packet->primary) {
       // This is a redundant payload; call the special decoder method.
       decode_length = decoder->DecodeRedundant(
-          packet->payload, packet->payload_length, fs_hz_,
+          packet->payload.data(), packet->payload.size(), fs_hz_,
           (decoded_buffer_length_ - *decoded_length) * sizeof(int16_t),
           &decoded_buffer_[*decoded_length], speech_type);
     } else {
-      decode_length =
-          decoder->Decode(
-              packet->payload, packet->payload_length, fs_hz_,
-              (decoded_buffer_length_ - *decoded_length) * sizeof(int16_t),
-              &decoded_buffer_[*decoded_length], speech_type);
+      decode_length = decoder->Decode(
+          packet->payload.data(), packet->payload.size(), fs_hz_,
+          (decoded_buffer_length_ - *decoded_length) * sizeof(int16_t),
+          &decoded_buffer_[*decoded_length], speech_type);
     }
 
-    delete[] packet->payload;
     delete packet;
     packet = NULL;
     if (decode_length > 0) {
@@ -1963,7 +1951,7 @@
     }
     stats_.PacketsDiscarded(discard_count);
     stats_.StoreWaitingTime(packet->waiting_time->ElapsedMs());
-    assert(packet->payload_length > 0);
+    assert(!packet->payload.empty());
     packet_list->push_back(packet);  // Store packet in list.
 
     if (first_packet) {
@@ -1988,11 +1976,11 @@
         packet_duration = rtc::checked_cast<int>(decoder_frame_length_);
       } else {
         if (packet->primary) {
-          packet_duration = decoder->PacketDuration(packet->payload,
-                                                    packet->payload_length);
+          packet_duration = decoder->PacketDuration(packet->payload.data(),
+                                                    packet->payload.size());
         } else {
-          packet_duration = decoder->
-              PacketDurationRedundant(packet->payload, packet->payload_length);
+          packet_duration = decoder->PacketDurationRedundant(
+              packet->payload.data(), packet->payload.size());
           stats_.SecondaryDecodedSamples(packet_duration);
         }
       }
diff --git a/webrtc/modules/audio_coding/neteq/packet.h b/webrtc/modules/audio_coding/neteq/packet.h
index d6f64c7..273c2d2 100644
--- a/webrtc/modules/audio_coding/neteq/packet.h
+++ b/webrtc/modules/audio_coding/neteq/packet.h
@@ -14,6 +14,7 @@
 #include <list>
 #include <memory>
 
+#include "webrtc/base/buffer.h"
 #include "webrtc/modules/audio_coding/neteq/tick_timer.h"
 #include "webrtc/modules/include/module_common_types.h"
 #include "webrtc/typedefs.h"
@@ -24,8 +25,7 @@
 struct Packet {
   RTPHeader header;
   // Datagram excluding RTP header and header extension.
-  uint8_t* payload = nullptr;
-  size_t payload_length = 0;
+  rtc::Buffer payload;
   bool primary = true;  // Primary, i.e., not redundant payload.
   bool sync_packet = false;
   std::unique_ptr<TickTimer::Stopwatch> waiting_time;
diff --git a/webrtc/modules/audio_coding/neteq/packet_buffer.cc b/webrtc/modules/audio_coding/neteq/packet_buffer.cc
index 61cc957..0e512b6 100644
--- a/webrtc/modules/audio_coding/neteq/packet_buffer.cc
+++ b/webrtc/modules/audio_coding/neteq/packet_buffer.cc
@@ -57,7 +57,7 @@
 }
 
 int PacketBuffer::InsertPacket(Packet* packet) {
-  if (!packet || !packet->payload) {
+  if (!packet || packet->payload.empty()) {
     if (packet) {
       delete packet;
     }
@@ -88,7 +88,6 @@
   // packet to list.
   if (rit != buffer_.rend() &&
       packet->header.timestamp == (*rit)->header.timestamp) {
-    delete [] packet->payload;
     delete packet;
     return return_val;
   }
@@ -99,7 +98,6 @@
   PacketList::iterator it = rit.base();
   if (it != buffer_.end() &&
       packet->header.timestamp == (*it)->header.timestamp) {
-    delete [] (*it)->payload;
     delete *it;
     it = buffer_.erase(it);
   }
@@ -196,7 +194,7 @@
 
   Packet* packet = buffer_.front();
   // Assert that the packet sanity checks in InsertPacket method works.
-  assert(packet && packet->payload);
+  assert(packet && !packet->payload.empty());
   buffer_.pop_front();
 
   // Discard other packets with the same timestamp. These are duplicates or
@@ -225,7 +223,7 @@
   }
   // Assert that the packet sanity checks in InsertPacket method works.
   assert(buffer_.front());
-  assert(buffer_.front()->payload);
+  assert(!buffer_.front()->payload.empty());
   DeleteFirstPacket(&buffer_);
   return kOK;
 }
@@ -264,8 +262,8 @@
       if (!packet->primary) {
         continue;
       }
-      int duration =
-          decoder->PacketDuration(packet->payload, packet->payload_length);
+      int duration = decoder->PacketDuration(packet->payload.data(),
+                                             packet->payload.size());
       if (duration >= 0) {
         last_duration = duration;  // Save the most up-to-date (valid) duration.
       }
@@ -280,7 +278,6 @@
     return false;
   }
   Packet* first_packet = packet_list->front();
-  delete [] first_packet->payload;
   delete first_packet;
   packet_list->pop_front();
   return true;
diff --git a/webrtc/modules/audio_coding/neteq/packet_buffer_unittest.cc b/webrtc/modules/audio_coding/neteq/packet_buffer_unittest.cc
index f1d2ad0..d17d49f 100644
--- a/webrtc/modules/audio_coding/neteq/packet_buffer_unittest.cc
+++ b/webrtc/modules/audio_coding/neteq/packet_buffer_unittest.cc
@@ -59,9 +59,8 @@
   packet->header.ssrc = 0x12345678;
   packet->header.numCSRCs = 0;
   packet->header.paddingLength = 0;
-  packet->payload_length = payload_size_bytes;
   packet->primary = true;
-  packet->payload = new uint8_t[payload_size_bytes];
+  packet->payload.SetSize(payload_size_bytes);
   ++seq_no_;
   ts_ += frame_size_;
   return packet;
@@ -284,7 +283,6 @@
     Packet* packet = buffer.GetNextPacket(&drop_count);
     EXPECT_EQ(0u, drop_count);
     EXPECT_EQ(packet, expect_order[i]);  // Compare pointer addresses.
-    delete[] packet->payload;
     delete packet;
   }
   EXPECT_TRUE(buffer.Empty());
@@ -359,7 +357,6 @@
     ASSERT_FALSE(packet == NULL);
     EXPECT_EQ(current_ts, packet->header.timestamp);
     current_ts += ts_increment;
-    delete [] packet->payload;
     delete packet;
   }
   EXPECT_TRUE(buffer.Empty());
@@ -379,8 +376,7 @@
   Packet* packet = NULL;
   EXPECT_EQ(PacketBuffer::kInvalidPacket, buffer->InsertPacket(packet));
   packet = gen.NextPacket(payload_len);
-  delete [] packet->payload;
-  packet->payload = NULL;
+  packet->payload.Clear();
   EXPECT_EQ(PacketBuffer::kInvalidPacket, buffer->InsertPacket(packet));
   // Packet is deleted by the PacketBuffer.
 
@@ -409,8 +405,7 @@
   PacketList list;
   list.push_back(gen.NextPacket(payload_len));  // Valid packet.
   packet = gen.NextPacket(payload_len);
-  delete [] packet->payload;
-  packet->payload = NULL;  // Invalid.
+  packet->payload.Clear();  // Invalid.
   list.push_back(packet);
   list.push_back(gen.NextPacket(payload_len));  // Valid packet.
   MockDecoderDatabase decoder_database;
@@ -486,9 +481,7 @@
   EXPECT_FALSE(*a <= *b);
   EXPECT_TRUE(*a >= *b);
 
-  delete [] a->payload;
   delete a;
-  delete [] b->payload;
   delete b;
 }
 
diff --git a/webrtc/modules/audio_coding/neteq/payload_splitter.cc b/webrtc/modules/audio_coding/neteq/payload_splitter.cc
index 530e9d0..87d2ce5 100644
--- a/webrtc/modules/audio_coding/neteq/payload_splitter.cc
+++ b/webrtc/modules/audio_coding/neteq/payload_splitter.cc
@@ -11,6 +11,7 @@
 #include "webrtc/modules/audio_coding/neteq/payload_splitter.h"
 
 #include <assert.h>
+#include <iostream>
 
 #include "webrtc/base/checks.h"
 #include "webrtc/base/logging.h"
@@ -29,10 +30,9 @@
   int ret = kOK;
   PacketList::iterator it = packet_list->begin();
   while (it != packet_list->end()) {
-    PacketList new_packets;  // An empty list to store the split packets in.
-    Packet* red_packet = (*it);
-    assert(red_packet->payload);
-    uint8_t* payload_ptr = red_packet->payload;
+    const Packet* red_packet = (*it);
+    assert(!red_packet->payload.empty());
+    const uint8_t* payload_ptr = red_packet->payload.data();
 
     // Read RED headers (according to RFC 2198):
     //
@@ -47,72 +47,74 @@
     //   |0|   Block PT  |
     //   +-+-+-+-+-+-+-+-+
 
+    struct RedHeader {
+      uint8_t payload_type;
+      uint32_t timestamp;
+      size_t payload_length;
+      bool primary;
+    };
+
+    std::vector<RedHeader> new_headers;
     bool last_block = false;
     size_t sum_length = 0;
     while (!last_block) {
-      Packet* new_packet = new Packet;
-      new_packet->header = red_packet->header;
+      RedHeader new_header;
       // Check the F bit. If F == 0, this was the last block.
       last_block = ((*payload_ptr & 0x80) == 0);
       // Bits 1 through 7 are payload type.
-      new_packet->header.payloadType = payload_ptr[0] & 0x7F;
+      new_header.payload_type = payload_ptr[0] & 0x7F;
       if (last_block) {
         // No more header data to read.
         ++sum_length;  // Account for RED header size of 1 byte.
-        new_packet->payload_length = red_packet->payload_length - sum_length;
-        new_packet->primary = true;  // Last block is always primary.
+        new_header.timestamp = red_packet->header.timestamp;
+        new_header.payload_length = red_packet->payload.size() - sum_length;
+        new_header.primary = true;  // Last block is always primary.
         payload_ptr += 1;  // Advance to first payload byte.
       } else {
         // Bits 8 through 21 are timestamp offset.
         int timestamp_offset = (payload_ptr[1] << 6) +
             ((payload_ptr[2] & 0xFC) >> 2);
-        new_packet->header.timestamp = red_packet->header.timestamp -
-            timestamp_offset;
+        new_header.timestamp = red_packet->header.timestamp - timestamp_offset;
         // Bits 22 through 31 are payload length.
-        new_packet->payload_length = ((payload_ptr[2] & 0x03) << 8) +
-            payload_ptr[3];
-        new_packet->primary = false;
+        new_header.payload_length =
+            ((payload_ptr[2] & 0x03) << 8) + payload_ptr[3];
+        new_header.primary = false;
         payload_ptr += 4;  // Advance to next RED header.
       }
-      sum_length += new_packet->payload_length;
+      sum_length += new_header.payload_length;
       sum_length += 4;  // Account for RED header size of 4 bytes.
       // Store in new list of packets.
-      new_packets.push_back(new_packet);
+      new_headers.push_back(new_header);
     }
 
     // Populate the new packets with payload data.
     // |payload_ptr| now points at the first payload byte.
-    PacketList::iterator new_it;
-    for (new_it = new_packets.begin(); new_it != new_packets.end(); ++new_it) {
-      size_t payload_length = (*new_it)->payload_length;
+    PacketList new_packets;  // An empty list to store the split packets in.
+    for (const auto& new_header : new_headers) {
+      size_t payload_length = new_header.payload_length;
       if (payload_ptr + payload_length >
-          red_packet->payload + red_packet->payload_length) {
+          red_packet->payload.data() + red_packet->payload.size()) {
         // The block lengths in the RED headers do not match the overall packet
         // length. Something is corrupt. Discard this and the remaining
         // payloads from this packet.
         LOG(LS_WARNING) << "SplitRed length mismatch";
-        while (new_it != new_packets.end()) {
-          // Payload should not have been allocated yet.
-          assert(!(*new_it)->payload);
-          delete (*new_it);
-          new_it = new_packets.erase(new_it);
-        }
         ret = kRedLengthMismatch;
         break;
       }
-      (*new_it)->payload = new uint8_t[payload_length];
-      memcpy((*new_it)->payload, payload_ptr, payload_length);
+      Packet* new_packet = new Packet;
+      new_packet->header = red_packet->header;
+      new_packet->header.timestamp = new_header.timestamp;
+      new_packet->header.payloadType = new_header.payload_type;
+      new_packet->primary = new_header.primary;
+      new_packet->payload.SetData(payload_ptr, payload_length);
+      new_packets.push_front(new_packet);
       payload_ptr += payload_length;
     }
-    // Reverse the order of the new packets, so that the primary payload is
-    // always first.
-    new_packets.reverse();
     // Insert new packets into original list, before the element pointed to by
     // iterator |it|.
     packet_list->splice(it, new_packets, new_packets.begin(),
                         new_packets.end());
     // Delete old packet payload.
-    delete [] (*it)->payload;
     delete (*it);
     // Remove |it| from the packet list. This operation effectively moves the
     // iterator |it| to the next packet in the list. Thus, we do not have to
@@ -148,7 +150,8 @@
     // are handled separately.
     assert(decoder != NULL || decoder_database->IsComfortNoise(payload_type));
     if (!decoder ||
-        !decoder->PacketHasFec(packet->payload, packet->payload_length)) {
+        !decoder->PacketHasFec(packet->payload.data(),
+                               packet->payload.size())) {
       ++it;
       continue;
     }
@@ -162,12 +165,10 @@
 
         Packet* new_packet = new Packet;
         new_packet->header = packet->header;
-        int duration = decoder->
-            PacketDurationRedundant(packet->payload, packet->payload_length);
+        int duration = decoder->PacketDurationRedundant(packet->payload.data(),
+                                                        packet->payload.size());
         new_packet->header.timestamp -= duration;
-        new_packet->payload = new uint8_t[packet->payload_length];
-        memcpy(new_packet->payload, packet->payload, packet->payload_length);
-        new_packet->payload_length = packet->payload_length;
+        new_packet->payload.SetData(packet->payload);
         new_packet->primary = false;
         new_packet->sync_packet = packet->sync_packet;
         // Waiting time should not be set here.
@@ -203,7 +204,6 @@
         if (this_payload_type != main_payload_type) {
           // We do not allow redundant payloads of a different type.
           // Discard this payload.
-          delete [] (*it)->payload;
           delete (*it);
           // Remove |it| from the packet list. This operation effectively
           // moves the iterator |it| to the next packet in the list. Thus, we
@@ -304,15 +304,15 @@
       case NetEqDecoder::kDecoderILBC: {
         size_t bytes_per_frame;
         int timestamps_per_frame;
-        if (packet->payload_length >= 950) {
+        if (packet->payload.size() >= 950) {
           LOG(LS_WARNING) << "SplitAudio too large iLBC payload";
           return kTooLargePayload;
         }
-        if (packet->payload_length % 38 == 0) {
+        if (packet->payload.size() % 38 == 0) {
           // 20 ms frames.
           bytes_per_frame = 38;
           timestamps_per_frame = 160;
-        } else if (packet->payload_length % 50 == 0) {
+        } else if (packet->payload.size() % 50 == 0) {
           // 30 ms frames.
           bytes_per_frame = 50;
           timestamps_per_frame = 240;
@@ -348,7 +348,6 @@
     packet_list->splice(it, new_packets, new_packets.begin(),
                         new_packets.end());
     // Delete old packet payload.
-    delete [] (*it)->payload;
     delete (*it);
     // Remove |it| from the packet list. This operation effectively moves the
     // iterator |it| to the next packet in the list. Thus, we do not have to
@@ -365,7 +364,7 @@
   assert(packet);
   assert(new_packets);
 
-  size_t split_size_bytes = packet->payload_length;
+  size_t split_size_bytes = packet->payload.size();
 
   // Find a "chunk size" >= 20 ms and < 40 ms.
   size_t min_chunk_size = bytes_per_ms * 20;
@@ -379,17 +378,15 @@
       split_size_bytes * timestamps_per_ms / bytes_per_ms);
   uint32_t timestamp = packet->header.timestamp;
 
-  uint8_t* payload_ptr = packet->payload;
-  size_t len = packet->payload_length;
+  const uint8_t* payload_ptr = packet->payload.data();
+  size_t len = packet->payload.size();
   while (len >= (2 * split_size_bytes)) {
     Packet* new_packet = new Packet;
-    new_packet->payload_length = split_size_bytes;
     new_packet->header = packet->header;
     new_packet->header.timestamp = timestamp;
     timestamp += timestamps_per_chunk;
     new_packet->primary = packet->primary;
-    new_packet->payload = new uint8_t[split_size_bytes];
-    memcpy(new_packet->payload, payload_ptr, split_size_bytes);
+    new_packet->payload.SetData(payload_ptr, split_size_bytes);
     payload_ptr += split_size_bytes;
     new_packets->push_back(new_packet);
     len -= split_size_bytes;
@@ -397,12 +394,10 @@
 
   if (len > 0) {
     Packet* new_packet = new Packet;
-    new_packet->payload_length = len;
     new_packet->header = packet->header;
     new_packet->header.timestamp = timestamp;
     new_packet->primary = packet->primary;
-    new_packet->payload = new uint8_t[len];
-    memcpy(new_packet->payload, payload_ptr, len);
+    new_packet->payload.SetData(payload_ptr, len);
     new_packets->push_back(new_packet);
   }
 }
@@ -411,29 +406,27 @@
                                    size_t bytes_per_frame,
                                    uint32_t timestamps_per_frame,
                                    PacketList* new_packets) {
-  if (packet->payload_length % bytes_per_frame != 0) {
+  if (packet->payload.size() % bytes_per_frame != 0) {
     LOG(LS_WARNING) << "SplitByFrames length mismatch";
     return kFrameSplitError;
   }
 
-  if (packet->payload_length == bytes_per_frame) {
+  if (packet->payload.size() == bytes_per_frame) {
     // Special case. Do not split the payload.
     return kNoSplit;
   }
 
   uint32_t timestamp = packet->header.timestamp;
-  uint8_t* payload_ptr = packet->payload;
-  size_t len = packet->payload_length;
+  const uint8_t* payload_ptr = packet->payload.data();
+  size_t len = packet->payload.size();
   while (len > 0) {
     assert(len >= bytes_per_frame);
     Packet* new_packet = new Packet;
-    new_packet->payload_length = bytes_per_frame;
     new_packet->header = packet->header;
     new_packet->header.timestamp = timestamp;
     timestamp += timestamps_per_frame;
     new_packet->primary = packet->primary;
-    new_packet->payload = new uint8_t[bytes_per_frame];
-    memcpy(new_packet->payload, payload_ptr, bytes_per_frame);
+    new_packet->payload.SetData(payload_ptr, bytes_per_frame);
     payload_ptr += bytes_per_frame;
     new_packets->push_back(new_packet);
     len -= bytes_per_frame;
diff --git a/webrtc/modules/audio_coding/neteq/payload_splitter_unittest.cc b/webrtc/modules/audio_coding/neteq/payload_splitter_unittest.cc
index f0b16d0..bd9d4d4 100644
--- a/webrtc/modules/audio_coding/neteq/payload_splitter_unittest.cc
+++ b/webrtc/modules/audio_coding/neteq/payload_splitter_unittest.cc
@@ -82,10 +82,10 @@
   packet->header.payloadType = kRedPayloadType;
   packet->header.timestamp = kBaseTimestamp;
   packet->header.sequenceNumber = kSequenceNumber;
-  packet->payload_length = (kPayloadLength + 1) +
-      (num_payloads - 1) * (kPayloadLength + kRedHeaderLength);
-  uint8_t* payload = new uint8_t[packet->payload_length];
-  uint8_t* payload_ptr = payload;
+  packet->payload.SetSize((kPayloadLength + 1) +
+                          (num_payloads - 1) *
+                              (kPayloadLength + kRedHeaderLength));
+  uint8_t* payload_ptr = packet->payload.data();
   for (size_t i = 0; i < num_payloads; ++i) {
     // Write the RED headers.
     if (i == num_payloads - 1) {
@@ -117,7 +117,6 @@
     }
     payload_ptr += kPayloadLength;
   }
-  packet->payload = payload;
   return packet;
 }
 
@@ -128,14 +127,12 @@
   packet->header.payloadType = payload_type;
   packet->header.timestamp = kBaseTimestamp;
   packet->header.sequenceNumber = kSequenceNumber;
-  packet->payload_length = payload_length;
-  uint8_t* payload = new uint8_t[packet->payload_length];
-  packet->payload = payload;
+  packet->payload.SetSize(payload_length);
   if (opus_fec) {
-    CreateOpusFecPayload(packet->payload, packet->payload_length,
+    CreateOpusFecPayload(packet->payload.data(), packet->payload.size(),
                          payload_value);
   } else {
-    memset(payload, payload_value, payload_length);
+    memset(packet->payload.data(), payload_value, packet->payload.size());
   }
   return packet;
 }
@@ -148,13 +145,13 @@
                   uint32_t timestamp,
                   uint8_t payload_value,
                   bool primary = true) {
-  EXPECT_EQ(payload_length, packet->payload_length);
+  EXPECT_EQ(payload_length, packet->payload.size());
   EXPECT_EQ(payload_type, packet->header.payloadType);
   EXPECT_EQ(sequence_number, packet->header.sequenceNumber);
   EXPECT_EQ(timestamp, packet->header.timestamp);
   EXPECT_EQ(primary, packet->primary);
-  ASSERT_FALSE(packet->payload == NULL);
-  for (size_t i = 0; i < packet->payload_length; ++i) {
+  ASSERT_FALSE(packet->payload.empty());
+  for (size_t i = 0; i < packet->payload.size(); ++i) {
     EXPECT_EQ(payload_value, packet->payload[i]);
   }
 }
@@ -180,14 +177,12 @@
   packet = packet_list.front();
   VerifyPacket(packet, kPayloadLength, payload_types[1], kSequenceNumber,
                kBaseTimestamp, 1, true);
-  delete [] packet->payload;
   delete packet;
   packet_list.pop_front();
   // Check second packet.
   packet = packet_list.front();
   VerifyPacket(packet, kPayloadLength, payload_types[0], kSequenceNumber,
                kBaseTimestamp - kTimestampOffset, 0, false);
-  delete [] packet->payload;
   delete packet;
 }
 
@@ -213,14 +208,12 @@
   packet = packet_list.front();
   VerifyPacket(packet, kPayloadLength, payload_types[0], kSequenceNumber,
                kBaseTimestamp, 0, true);
-  delete [] packet->payload;
   delete packet;
   packet_list.pop_front();
   // Check second packet.
   packet = packet_list.front();
   VerifyPacket(packet, kPayloadLength, payload_types[0], kSequenceNumber + 1,
                kBaseTimestamp + kTimestampOffset, 0, true);
-  delete [] packet->payload;
   delete packet;
 }
 
@@ -253,42 +246,36 @@
   packet = packet_list.front();
   VerifyPacket(packet, kPayloadLength, payload_types[2], kSequenceNumber,
                kBaseTimestamp, 2, true);
-  delete [] packet->payload;
   delete packet;
   packet_list.pop_front();
   // Check second packet, A2.
   packet = packet_list.front();
   VerifyPacket(packet, kPayloadLength, payload_types[1], kSequenceNumber,
                kBaseTimestamp - kTimestampOffset, 1, false);
-  delete [] packet->payload;
   delete packet;
   packet_list.pop_front();
   // Check third packet, A3.
   packet = packet_list.front();
   VerifyPacket(packet, kPayloadLength, payload_types[0], kSequenceNumber,
                kBaseTimestamp - 2 * kTimestampOffset, 0, false);
-  delete [] packet->payload;
   delete packet;
   packet_list.pop_front();
   // Check fourth packet, B1.
   packet = packet_list.front();
   VerifyPacket(packet, kPayloadLength, payload_types[2], kSequenceNumber + 1,
                kBaseTimestamp + kTimestampOffset, 2, true);
-  delete [] packet->payload;
   delete packet;
   packet_list.pop_front();
   // Check fifth packet, B2.
   packet = packet_list.front();
   VerifyPacket(packet, kPayloadLength, payload_types[1], kSequenceNumber + 1,
                kBaseTimestamp, 1, false);
-  delete [] packet->payload;
   delete packet;
   packet_list.pop_front();
   // Check sixth packet, B3.
   packet = packet_list.front();
   VerifyPacket(packet, kPayloadLength, payload_types[0], kSequenceNumber + 1,
                kBaseTimestamp - kTimestampOffset, 0, false);
-  delete [] packet->payload;
   delete packet;
 }
 
@@ -327,7 +314,6 @@
   for (int i = 0; i <= 2; ++i) {
     Packet* packet = packet_list.front();
     VerifyPacket(packet, 10, i, kSequenceNumber, kBaseTimestamp, 0, true);
-    delete [] packet->payload;
     delete packet;
     packet_list.pop_front();
   }
@@ -343,7 +329,7 @@
   // Manually tamper with the payload length of the packet.
   // This is one byte too short for the second payload (out of three).
   // We expect only the first payload to be returned.
-  packet->payload_length -= kPayloadLength + 1;
+  packet->payload.SetSize(packet->payload.size() - (kPayloadLength + 1));
   PacketList packet_list;
   packet_list.push_back(packet);
   PayloadSplitter splitter;
@@ -354,7 +340,6 @@
   packet = packet_list.front();
   VerifyPacket(packet, kPayloadLength, payload_types[0], kSequenceNumber,
                kBaseTimestamp - 2 * kTimestampOffset, 0, false);
-  delete [] packet->payload;
   delete packet;
   packet_list.pop_front();
 }
@@ -410,7 +395,6 @@
     VerifyPacket((*it), kPayloadLength, payload_type, kSequenceNumber,
                  kBaseTimestamp, 10 * payload_type);
     ++payload_type;
-    delete [] (*it)->payload;
     delete (*it);
     it = packet_list.erase(it);
   }
@@ -441,7 +425,6 @@
   // Delete the packets and payloads to avoid having the test leak memory.
   PacketList::iterator it = packet_list.begin();
   while (it != packet_list.end()) {
-    delete [] (*it)->payload;
     delete (*it);
     it = packet_list.erase(it);
   }
@@ -561,7 +544,6 @@
         expected_timestamp_offset_ms[i] * samples_per_ms_;
     VerifyPacket((*it), length_bytes, kPayloadType, kSequenceNumber,
                  expected_timestamp, expected_payload_value[i]);
-    delete [] (*it)->payload;
     delete (*it);
     it = packet_list.erase(it);
     ++i;
@@ -610,7 +592,7 @@
   size_t payload_length_bytes = frame_length_bytes_ * num_frames_;
   Packet* packet = CreatePacket(kPayloadType, payload_length_bytes, 0);
   // Fill payload with increasing integers {0, 1, 2, ...}.
-  for (size_t i = 0; i < packet->payload_length; ++i) {
+  for (size_t i = 0; i < packet->payload.size(); ++i) {
     packet->payload[i] = static_cast<uint8_t>(i);
   }
   packet_list.push_back(packet);
@@ -635,16 +617,15 @@
     Packet* packet = (*it);
     EXPECT_EQ(kBaseTimestamp + frame_length_samples * frame_num,
               packet->header.timestamp);
-    EXPECT_EQ(frame_length_bytes_, packet->payload_length);
+    EXPECT_EQ(frame_length_bytes_, packet->payload.size());
     EXPECT_EQ(kPayloadType, packet->header.payloadType);
     EXPECT_EQ(kSequenceNumber, packet->header.sequenceNumber);
     EXPECT_EQ(true, packet->primary);
-    ASSERT_FALSE(packet->payload == NULL);
-    for (size_t i = 0; i < packet->payload_length; ++i) {
+    ASSERT_FALSE(packet->payload.empty());
+    for (size_t i = 0; i < packet->payload.size(); ++i) {
       EXPECT_EQ(payload_value, packet->payload[i]);
       ++payload_value;
     }
-    delete [] (*it)->payload;
     delete (*it);
     it = packet_list.erase(it);
     ++frame_num;
@@ -695,7 +676,6 @@
   // Delete the packets and payloads to avoid having the test leak memory.
   PacketList::iterator it = packet_list.begin();
   while (it != packet_list.end()) {
-    delete [] (*it)->payload;
     delete (*it);
     it = packet_list.erase(it);
   }
@@ -726,7 +706,6 @@
   // Delete the packets and payloads to avoid having the test leak memory.
   PacketList::iterator it = packet_list.begin();
   while (it != packet_list.end()) {
-    delete [] (*it)->payload;
     delete (*it);
     it = packet_list.erase(it);
   }
@@ -760,9 +739,8 @@
   packet = packet_list.front();
   EXPECT_EQ(0, packet->header.payloadType);
   EXPECT_EQ(kBaseTimestamp - 20 * 48, packet->header.timestamp);
-  EXPECT_EQ(10U, packet->payload_length);
+  EXPECT_EQ(10U, packet->payload.size());
   EXPECT_FALSE(packet->primary);
-  delete [] packet->payload;
   delete packet;
   packet_list.pop_front();
 
@@ -770,23 +748,20 @@
   packet = packet_list.front();
   EXPECT_EQ(0, packet->header.payloadType);
   EXPECT_EQ(kBaseTimestamp, packet->header.timestamp);
-  EXPECT_EQ(10U, packet->payload_length);
+  EXPECT_EQ(10U, packet->payload.size());
   EXPECT_TRUE(packet->primary);
-  delete [] packet->payload;
   delete packet;
   packet_list.pop_front();
 
   // Check third packet.
   packet = packet_list.front();
   VerifyPacket(packet, 10, 0, kSequenceNumber, kBaseTimestamp, 0, true);
-  delete [] packet->payload;
   delete packet;
   packet_list.pop_front();
 
   // Check fourth packet.
   packet = packet_list.front();
   VerifyPacket(packet, 10, 1, kSequenceNumber, kBaseTimestamp, 0, true);
-  delete [] packet->payload;
   delete packet;
 }
 
@@ -812,10 +787,9 @@
   packet = packet_list.front();
   EXPECT_EQ(0, packet->header.payloadType);
   EXPECT_EQ(kBaseTimestamp - kTimestampOffset, packet->header.timestamp);
-  EXPECT_EQ(kPayloadLength, packet->payload_length);
+  EXPECT_EQ(kPayloadLength, packet->payload.size());
   EXPECT_FALSE(packet->primary);
   EXPECT_EQ(packet->payload[3], 1);
-  delete [] packet->payload;
   delete packet;
   packet_list.pop_front();
 
@@ -823,10 +797,9 @@
   packet = packet_list.front();
   EXPECT_EQ(0, packet->header.payloadType);
   EXPECT_EQ(kBaseTimestamp, packet->header.timestamp);
-  EXPECT_EQ(kPayloadLength, packet->payload_length);
+  EXPECT_EQ(kPayloadLength, packet->payload.size());
   EXPECT_TRUE(packet->primary);
   EXPECT_EQ(packet->payload[3], 1);
-  delete [] packet->payload;
   delete packet;
   packet_list.pop_front();
 
@@ -834,10 +807,9 @@
   packet = packet_list.front();
   EXPECT_EQ(0, packet->header.payloadType);
   EXPECT_EQ(kBaseTimestamp - 2 * kTimestampOffset, packet->header.timestamp);
-  EXPECT_EQ(kPayloadLength, packet->payload_length);
+  EXPECT_EQ(kPayloadLength, packet->payload.size());
   EXPECT_FALSE(packet->primary);
   EXPECT_EQ(packet->payload[3], 0);
-  delete [] packet->payload;
   delete packet;
   packet_list.pop_front();
 
@@ -845,10 +817,9 @@
   packet = packet_list.front();
   EXPECT_EQ(0, packet->header.payloadType);
   EXPECT_EQ(kBaseTimestamp - kTimestampOffset, packet->header.timestamp);
-  EXPECT_EQ(kPayloadLength, packet->payload_length);
+  EXPECT_EQ(kPayloadLength, packet->payload.size());
   EXPECT_TRUE(packet->primary);
   EXPECT_EQ(packet->payload[3], 0);
-  delete [] packet->payload;
   delete packet;
   packet_list.pop_front();
 }