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 "testsupport/packet_reader.h"
12
13#include <cassert>
14#include <cstdio>
15
16namespace webrtc {
17namespace test {
18
19PacketReader::PacketReader()
20    : initialized_(false) {}
21
22PacketReader::~PacketReader() {}
23
24void PacketReader::InitializeReading(WebRtc_UWord8* data,
25                                     int data_length_in_bytes,
26                                     int packet_size_in_bytes) {
27  assert(data);
28  assert(data_length_in_bytes >= 0);
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
37int PacketReader::NextPacket(WebRtc_UWord8** packet_pointer) {
38  if (!initialized_) {
39    fprintf(stderr, "Attempting to use uninitialized PacketReader!\n");
40    return -1;
41  }
42  *packet_pointer = data_ + currentIndex_;
43  // Check if we're about to read the last packet:
44  if (data_length_ - currentIndex_ <= packet_size_) {
45    int size = data_length_ - currentIndex_;
46    currentIndex_ = data_length_;
47    assert(size >= 0);
48    return size;
49  }
50  currentIndex_ += packet_size_;
51  assert(packet_size_ >= 0);
52  return packet_size_;
53}
54
55}  // namespace test
56}  // namespace webrtc
57