Use size_t more consistently for packet/payload lengths.

See design doc at https://docs.google.com/a/chromium.org/document/d/1I6nmE9D_BmCY-IoV6MDPY2V6WYpEI-dg2apWXTfZyUI/edit?usp=sharing for more information.

This CL was reviewed and approved in pieces in the following CLs:
https://webrtc-codereview.appspot.com/24209004/
https://webrtc-codereview.appspot.com/24229004/
https://webrtc-codereview.appspot.com/24259004/
https://webrtc-codereview.appspot.com/25109004/
https://webrtc-codereview.appspot.com/26099004/
https://webrtc-codereview.appspot.com/27069004/
https://webrtc-codereview.appspot.com/27969004/
https://webrtc-codereview.appspot.com/27989004/
https://webrtc-codereview.appspot.com/29009004/
https://webrtc-codereview.appspot.com/30929004/
https://webrtc-codereview.appspot.com/30939004/
https://webrtc-codereview.appspot.com/31999004/
Committing as TBR to the original reviewers.

BUG=chromium:81439
TEST=none
TBR=pthatcher,henrik.lundin,tina.legrand,stefan,tkchin,glaznev,kjellander,perkj,mflodman,henrika,asapersson,niklas.enbom

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

git-svn-id: http://webrtc.googlecode.com/svn/trunk@7726 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/modules/utility/interface/rtp_dump.h b/webrtc/modules/utility/interface/rtp_dump.h
index 6c2dc7c..df45ae2 100644
--- a/webrtc/modules/utility/interface/rtp_dump.h
+++ b/webrtc/modules/utility/interface/rtp_dump.h
@@ -43,7 +43,7 @@
     // Note: packet should contain the RTP/RTCP part of the packet. I.e. the
     // first bytes of packet should be the RTP/RTCP header.
     virtual int32_t DumpPacket(const uint8_t* packet,
-                               uint16_t packetLength) = 0;
+                               size_t packetLength) = 0;
 
 protected:
     virtual ~RtpDump();
diff --git a/webrtc/modules/utility/source/coder.cc b/webrtc/modules/utility/source/coder.cc
index 11a462a..5a39748 100644
--- a/webrtc/modules/utility/source/coder.cc
+++ b/webrtc/modules/utility/source/coder.cc
@@ -54,7 +54,7 @@
 int32_t AudioCoder::Decode(AudioFrame& decodedAudio,
                            uint32_t sampFreqHz,
                            const int8_t*  incomingPayload,
-                           int32_t  payloadLength)
+                           size_t  payloadLength)
 {
     if (payloadLength > 0)
     {
@@ -79,7 +79,7 @@
 
 int32_t AudioCoder::Encode(const AudioFrame& audio,
                            int8_t* encodedData,
-                           uint32_t& encodedLengthInBytes)
+                           size_t& encodedLengthInBytes)
 {
     // Fake a timestamp in case audio doesn't contain a correct timestamp.
     // Make a local copy of the audio frame since audio is const
@@ -109,7 +109,7 @@
     uint8_t   /* payloadType */,
     uint32_t  /* timeStamp */,
     const uint8_t*  payloadData,
-    uint16_t  payloadSize,
+    size_t  payloadSize,
     const RTPFragmentationHeader* /* fragmentation*/)
 {
     memcpy(_encodedData,payloadData,sizeof(uint8_t) * payloadSize);
diff --git a/webrtc/modules/utility/source/coder.h b/webrtc/modules/utility/source/coder.h
index e04372d..0363690 100644
--- a/webrtc/modules/utility/source/coder.h
+++ b/webrtc/modules/utility/source/coder.h
@@ -34,12 +34,12 @@
         ACMAMRPackingFormat amrFormat = AMRBandwidthEfficient);
 
     int32_t Decode(AudioFrame& decodedAudio, uint32_t sampFreqHz,
-                   const int8_t* incomingPayload, int32_t payloadLength);
+                   const int8_t* incomingPayload, size_t payloadLength);
 
     int32_t PlayoutData(AudioFrame& decodedAudio, uint16_t& sampFreqHz);
 
     int32_t Encode(const AudioFrame& audio, int8_t* encodedData,
-                   uint32_t& encodedLengthInBytes);
+                   size_t& encodedLengthInBytes);
 
 protected:
     virtual int32_t SendData(
@@ -47,7 +47,7 @@
         uint8_t payloadType,
         uint32_t timeStamp,
         const uint8_t* payloadData,
-        uint16_t payloadSize,
+        size_t payloadSize,
         const RTPFragmentationHeader* fragmentation) OVERRIDE;
 
 private:
@@ -57,7 +57,7 @@
 
     uint32_t _encodeTimestamp;
     int8_t*  _encodedData;
-    uint32_t _encodedLengthInBytes;
+    size_t _encodedLengthInBytes;
 
     uint32_t _decodeTimestamp;
 };
diff --git a/webrtc/modules/utility/source/file_player_impl.cc b/webrtc/modules/utility/source/file_player_impl.cc
index 8049245..29f7042 100644
--- a/webrtc/modules/utility/source/file_player_impl.cc
+++ b/webrtc/modules/utility/source/file_player_impl.cc
@@ -124,7 +124,7 @@
         unresampledAudioFrame.sample_rate_hz_ = _codec.plfreq;
 
         // L16 is un-encoded data. Just pull 10 ms.
-        uint32_t lengthInBytes =
+        size_t lengthInBytes =
             sizeof(unresampledAudioFrame.data_);
         if (_fileModule.PlayoutAudioData(
                 (int8_t*)unresampledAudioFrame.data_,
@@ -147,11 +147,11 @@
         // expects a full frame. If the frame size is larger than 10 ms,
         // PlayoutAudioData(..) data should be called proportionally less often.
         int16_t encodedBuffer[MAX_AUDIO_BUFFER_IN_SAMPLES];
-        uint32_t encodedLengthInBytes = 0;
+        size_t encodedLengthInBytes = 0;
         if(++_numberOf10MsInDecoder >= _numberOf10MsPerFrame)
         {
             _numberOf10MsInDecoder = 0;
-            uint32_t bytesFromFile = sizeof(encodedBuffer);
+            size_t bytesFromFile = sizeof(encodedBuffer);
             if (_fileModule.PlayoutAudioData((int8_t*)encodedBuffer,
                                              bytesFromFile) == -1)
             {
@@ -581,7 +581,7 @@
         if(_fileFormat == kFileFormatAviFile)
         {
             // Get next video frame
-            uint32_t encodedBufferLengthInBytes = _encodedData.bufferSize;
+            size_t encodedBufferLengthInBytes = _encodedData.bufferSize;
             if(_fileModule.PlayoutAVIVideoData(
                    reinterpret_cast< int8_t*>(_encodedData.payloadData),
                    encodedBufferLengthInBytes) != 0)
@@ -656,7 +656,7 @@
 
     // Size of unencoded data (I420) should be the largest possible frame size
     // in a file.
-    const uint32_t KReadBufferSize = 3 * video_codec_info_.width *
+    const size_t KReadBufferSize = 3 * video_codec_info_.width *
         video_codec_info_.height / 2;
     _encodedData.VerifyAndAllocate(KReadBufferSize);
     _encodedData.encodedHeight = video_codec_info_.height;
diff --git a/webrtc/modules/utility/source/file_recorder_impl.cc b/webrtc/modules/utility/source/file_recorder_impl.cc
index 264b867..14c5288 100644
--- a/webrtc/modules/utility/source/file_recorder_impl.cc
+++ b/webrtc/modules/utility/source/file_recorder_impl.cc
@@ -227,7 +227,7 @@
     // NOTE: stereo recording is only supported for WAV files.
     // TODO (hellner): WAV expect PCM in little endian byte order. Not
     // "encoding" with PCM coder should be a problem for big endian systems.
-    uint32_t encodedLenInBytes = 0;
+    size_t encodedLenInBytes = 0;
     if (_fileFormat == kFileFormatPreencodedFile ||
         STR_CASE_CMP(codec_info_.plname, "L16") != 0)
     {
@@ -272,9 +272,8 @@
         uint16_t msOfData =
             ptrAudioFrame->samples_per_channel_ /
             uint16_t(ptrAudioFrame->sample_rate_hz_ / 1000);
-        if (WriteEncodedAudioData(_audioBuffer,
-                                  (uint16_t)encodedLenInBytes,
-                                  msOfData, playoutTS) == -1)
+        if (WriteEncodedAudioData(_audioBuffer, encodedLenInBytes, msOfData,
+                                  playoutTS) == -1)
         {
             return -1;
         }
@@ -309,7 +308,7 @@
 
 int32_t FileRecorderImpl::WriteEncodedAudioData(
     const int8_t* audioBuffer,
-    uint16_t bufferLength,
+    size_t bufferLength,
     uint16_t /*millisecondsOfData*/,
     const TickTime* /*playoutTS*/)
 {
@@ -398,7 +397,7 @@
     return FileRecorderImpl::StopRecording();
 }
 
-int32_t AviRecorder::CalcI420FrameSize( ) const
+size_t AviRecorder::CalcI420FrameSize( ) const
 {
     return 3 * _videoCodecInst.width * _videoCodecInst.height / 2;
 }
@@ -641,8 +640,8 @@
 
     if( STR_CASE_CMP(_videoCodecInst.plName, "I420") == 0)
     {
-       int length  = CalcBufferSize(kI420, videoFrame.width(),
-                                    videoFrame.height());
+       size_t length =
+           CalcBufferSize(kI420, videoFrame.width(), videoFrame.height());
         _videoEncodedData.VerifyAndAllocate(length);
 
         // I420 is raw data. No encoding needed (each sample is represented by
@@ -681,7 +680,7 @@
 // happens in AviRecorder::Process().
 int32_t AviRecorder::WriteEncodedAudioData(
     const int8_t* audioBuffer,
-    uint16_t bufferLength,
+    size_t bufferLength,
     uint16_t millisecondsOfData,
     const TickTime* playoutTS)
 {
diff --git a/webrtc/modules/utility/source/file_recorder_impl.h b/webrtc/modules/utility/source/file_recorder_impl.h
index 53fd26b..9e17fd6 100644
--- a/webrtc/modules/utility/source/file_recorder_impl.h
+++ b/webrtc/modules/utility/source/file_recorder_impl.h
@@ -86,7 +86,7 @@
 protected:
     virtual int32_t WriteEncodedAudioData(
         const int8_t* audioBuffer,
-        uint16_t bufferLength,
+        size_t bufferLength,
         uint16_t millisecondsOfData,
         const TickTime* playoutTS);
 
@@ -111,7 +111,7 @@
 {
     public:
        AudioFrameFileInfo(const int8_t* audioData,
-                     const uint16_t audioSize,
+                     const size_t audioSize,
                      const uint16_t audioMS,
                      const TickTime& playoutTS)
            : _audioData(), _audioSize(audioSize), _audioMS(audioMS),
@@ -127,7 +127,7 @@
        };
     // TODO (hellner): either turn into a struct or provide get/set functions.
     int8_t   _audioData[MAX_AUDIO_BUFFER_IN_BYTES];
-    uint16_t _audioSize;
+    size_t   _audioSize;
     uint16_t _audioMS;
     TickTime _playoutTS;
 };
@@ -151,7 +151,7 @@
 protected:
     virtual int32_t WriteEncodedAudioData(
         const int8_t*  audioBuffer,
-        uint16_t bufferLength,
+        size_t bufferLength,
         uint16_t millisecondsOfData,
         const TickTime* playoutTS);
 private:
@@ -165,7 +165,7 @@
     int32_t EncodeAndWriteVideoToFile(I420VideoFrame& videoFrame);
     int32_t ProcessAudio();
 
-    int32_t CalcI420FrameSize() const;
+    size_t CalcI420FrameSize() const;
     int32_t SetUpVideoEncoder();
 
     VideoCodec _videoCodecInst;
@@ -178,7 +178,7 @@
 
     FrameScaler* _frameScaler;
     VideoCoder* _videoEncoder;
-    int32_t _videoMaxPayloadSize;
+    size_t _videoMaxPayloadSize;
     EncodedVideoData _videoEncodedData;
 
     ThreadWrapper* _thread;
diff --git a/webrtc/modules/utility/source/rtp_dump_impl.cc b/webrtc/modules/utility/source/rtp_dump_impl.cc
index 547df33..cab4065 100644
--- a/webrtc/modules/utility/source/rtp_dump_impl.cc
+++ b/webrtc/modules/utility/source/rtp_dump_impl.cc
@@ -12,6 +12,7 @@
 
 #include <assert.h>
 #include <stdio.h>
+#include <limits>
 
 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
 #include "webrtc/system_wrappers/interface/logging.h"
@@ -145,7 +146,7 @@
     return _file.Open();
 }
 
-int32_t RtpDumpImpl::DumpPacket(const uint8_t* packet, uint16_t packetLength)
+int32_t RtpDumpImpl::DumpPacket(const uint8_t* packet, size_t packetLength)
 {
     CriticalSectionScoped lock(_critSect);
     if (!IsActive())
@@ -158,7 +159,9 @@
         return -1;
     }
 
-    if (packetLength < 1)
+    rtpDumpPktHdr_t hdr;
+    size_t total_size = packetLength + sizeof hdr;
+    if (packetLength < 1 || total_size > std::numeric_limits<uint16_t>::max())
     {
         return -1;
     }
@@ -167,11 +170,8 @@
     // considered RTP (without further verification).
     bool isRTCP = RTCP(packet);
 
-    rtpDumpPktHdr_t hdr;
-    uint32_t offset;
-
     // Offset is relative to when recording was started.
-    offset = GetTimeInMS();
+    uint32_t offset = GetTimeInMS();
     if (offset < _startTime)
     {
         // Compensate for wraparound.
@@ -181,7 +181,7 @@
     }
     hdr.offset = RtpDumpHtonl(offset);
 
-    hdr.length = RtpDumpHtons((uint16_t)(packetLength + sizeof(hdr)));
+    hdr.length = RtpDumpHtons((uint16_t)(total_size));
     if (isRTCP)
     {
         hdr.plen = 0;
diff --git a/webrtc/modules/utility/source/rtp_dump_impl.h b/webrtc/modules/utility/source/rtp_dump_impl.h
index 0b72132..5be9cbe 100644
--- a/webrtc/modules/utility/source/rtp_dump_impl.h
+++ b/webrtc/modules/utility/source/rtp_dump_impl.h
@@ -26,7 +26,7 @@
     virtual int32_t Stop() OVERRIDE;
     virtual bool IsActive() const OVERRIDE;
     virtual int32_t DumpPacket(const uint8_t* packet,
-                               uint16_t packetLength) OVERRIDE;
+                               size_t packetLength) OVERRIDE;
 private:
     // Return the system time in ms.
     inline uint32_t GetTimeInMS() const;
diff --git a/webrtc/modules/utility/source/video_coder.cc b/webrtc/modules/utility/source/video_coder.cc
index 5096ace..e0d969d 100644
--- a/webrtc/modules/utility/source/video_coder.cc
+++ b/webrtc/modules/utility/source/video_coder.cc
@@ -113,7 +113,7 @@
     const uint32_t timeStamp,
     int64_t capture_time_ms,
     const uint8_t* payloadData,
-    uint32_t payloadSize,
+    size_t payloadSize,
     const RTPFragmentationHeader& fragmentationHeader,
     const RTPVideoHeader* /*rtpVideoHdr*/)
 {
diff --git a/webrtc/modules/utility/source/video_coder.h b/webrtc/modules/utility/source/video_coder.h
index 03aa511..a1d1a17 100644
--- a/webrtc/modules/utility/source/video_coder.h
+++ b/webrtc/modules/utility/source/video_coder.h
@@ -53,7 +53,7 @@
         uint32_t /*timeStamp*/,
         int64_t capture_time_ms,
         const uint8_t* payloadData,
-        uint32_t payloadSize,
+        size_t payloadSize,
         const RTPFragmentationHeader& /* fragmentationHeader*/,
         const RTPVideoHeader* rtpTypeHdr) OVERRIDE;