andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 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 | |
pbos@webrtc.org | 20a5c46 | 2013-05-27 08:02:22 +0000 | [diff] [blame] | 11 | #include "webrtc/test/testsupport/packet_reader.h" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 3f45c2e | 2013-08-05 16:22:53 +0000 | [diff] [blame^] | 13 | #include <assert.h> |
| 14 | #include <stdio.h> |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | namespace test { |
| 18 | |
| 19 | PacketReader::PacketReader() |
| 20 | : initialized_(false) {} |
| 21 | |
| 22 | PacketReader::~PacketReader() {} |
| 23 | |
pbos@webrtc.org | 91cab71 | 2013-04-09 11:10:21 +0000 | [diff] [blame] | 24 | void PacketReader::InitializeReading(uint8_t* data, |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 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 | |
pbos@webrtc.org | 91cab71 | 2013-04-09 11:10:21 +0000 | [diff] [blame] | 37 | int PacketReader::NextPacket(uint8_t** packet_pointer) { |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 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 |