1 /*
2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "webrtc/test/testsupport/packet_reader.h"
12 
13 #include <assert.h>
14 #include <stdio.h>
15 #include <algorithm>
16 
17 namespace webrtc {
18 namespace test {
19 
PacketReader()20 PacketReader::PacketReader()
21     : initialized_(false) {}
22 
~PacketReader()23 PacketReader::~PacketReader() {}
24 
InitializeReading(uint8_t * data,size_t data_length_in_bytes,size_t packet_size_in_bytes)25 void PacketReader::InitializeReading(uint8_t* data,
26                                      size_t data_length_in_bytes,
27                                      size_t packet_size_in_bytes) {
28   assert(data);
29   assert(packet_size_in_bytes > 0);
30   data_ = data;
31   data_length_ = data_length_in_bytes;
32   packet_size_ = packet_size_in_bytes;
33   currentIndex_ = 0;
34   initialized_ = true;
35 }
36 
NextPacket(uint8_t ** packet_pointer)37 int PacketReader::NextPacket(uint8_t** packet_pointer) {
38   if (!initialized_) {
39     fprintf(stderr, "Attempting to use uninitialized PacketReader!\n");
40     return -1;
41   }
42   *packet_pointer = data_ + currentIndex_;
43   size_t old_index = currentIndex_;
44   currentIndex_ = std::min(currentIndex_ + packet_size_, data_length_);
45   return static_cast<int>(currentIndex_ - old_index);
46 }
47 
48 }  // namespace test
49 }  // namespace webrtc
50