mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/video_coding/test/stream_generator.h" |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 12 | |
| 13 | #include <string.h> |
| 14 | |
| 15 | #include <list> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "modules/video_coding/packet.h" |
| 18 | #include "system_wrappers/include/clock.h" |
| 19 | #include "test/gtest.h" |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 20 | |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 21 | namespace webrtc { |
| 22 | |
Wan-Teh Chang | 349c2bb | 2015-06-05 14:45:05 -0700 | [diff] [blame] | 23 | StreamGenerator::StreamGenerator(uint16_t start_seq_num, int64_t current_time) |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 24 | : packets_(), sequence_number_(start_seq_num), start_time_(current_time) {} |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 25 | |
Wan-Teh Chang | 349c2bb | 2015-06-05 14:45:05 -0700 | [diff] [blame] | 26 | void StreamGenerator::Init(uint16_t start_seq_num, int64_t current_time) { |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 27 | packets_.clear(); |
| 28 | sequence_number_ = start_seq_num; |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 29 | start_time_ = current_time; |
Wan-Teh Chang | 55b6acb | 2015-06-05 15:02:36 -0700 | [diff] [blame] | 30 | memset(packet_buffer_, 0, sizeof(packet_buffer_)); |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void StreamGenerator::GenerateFrame(FrameType type, |
| 34 | int num_media_packets, |
| 35 | int num_empty_packets, |
Qiang Chen | d4cec15 | 2015-06-19 09:17:00 -0700 | [diff] [blame] | 36 | int64_t time_ms) { |
| 37 | uint32_t timestamp = 90 * (time_ms - start_time_); |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 38 | for (int i = 0; i < num_media_packets; ++i) { |
andresp@webrtc.org | 1f09dbe | 2013-09-13 19:17:54 +0000 | [diff] [blame] | 39 | const int packet_size = |
| 40 | (kFrameSize + num_media_packets / 2) / num_media_packets; |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 41 | bool marker_bit = (i == num_media_packets - 1); |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 42 | packets_.push_back(GeneratePacket(sequence_number_, timestamp, packet_size, |
| 43 | (i == 0), marker_bit, type)); |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 44 | ++sequence_number_; |
| 45 | } |
| 46 | for (int i = 0; i < num_empty_packets; ++i) { |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 47 | packets_.push_back(GeneratePacket(sequence_number_, timestamp, 0, false, |
| 48 | false, kEmptyFrame)); |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 49 | ++sequence_number_; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | VCMPacket StreamGenerator::GeneratePacket(uint16_t sequence_number, |
| 54 | uint32_t timestamp, |
| 55 | unsigned int size, |
| 56 | bool first_packet, |
| 57 | bool marker_bit, |
| 58 | FrameType type) { |
| 59 | EXPECT_LT(size, kMaxPacketSize); |
| 60 | VCMPacket packet; |
| 61 | packet.seqNum = sequence_number; |
| 62 | packet.timestamp = timestamp; |
| 63 | packet.frameType = type; |
johan | 0d1b2b6 | 2017-01-10 04:21:35 -0800 | [diff] [blame] | 64 | packet.is_first_packet_in_frame = first_packet; |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 65 | packet.markerBit = marker_bit; |
| 66 | packet.sizeBytes = size; |
Wan-Teh Chang | 55b6acb | 2015-06-05 15:02:36 -0700 | [diff] [blame] | 67 | packet.dataPtr = packet_buffer_; |
johan | 0d1b2b6 | 2017-01-10 04:21:35 -0800 | [diff] [blame] | 68 | if (packet.is_first_packet_in_frame) |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 69 | packet.completeNALU = kNaluStart; |
| 70 | else if (packet.markerBit) |
| 71 | packet.completeNALU = kNaluEnd; |
| 72 | else |
| 73 | packet.completeNALU = kNaluIncomplete; |
| 74 | return packet; |
| 75 | } |
| 76 | |
| 77 | bool StreamGenerator::PopPacket(VCMPacket* packet, int index) { |
| 78 | std::list<VCMPacket>::iterator it = GetPacketIterator(index); |
| 79 | if (it == packets_.end()) |
| 80 | return false; |
| 81 | if (packet) |
| 82 | *packet = (*it); |
| 83 | packets_.erase(it); |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | bool StreamGenerator::GetPacket(VCMPacket* packet, int index) { |
| 88 | std::list<VCMPacket>::iterator it = GetPacketIterator(index); |
| 89 | if (it == packets_.end()) |
| 90 | return false; |
| 91 | if (packet) |
| 92 | *packet = (*it); |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | bool StreamGenerator::NextPacket(VCMPacket* packet) { |
| 97 | if (packets_.empty()) |
| 98 | return false; |
| 99 | if (packet != NULL) |
| 100 | *packet = packets_.front(); |
| 101 | packets_.pop_front(); |
| 102 | return true; |
| 103 | } |
| 104 | |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 105 | void StreamGenerator::DropLastPacket() { |
| 106 | packets_.pop_back(); |
| 107 | } |
stefan@webrtc.org | c8b29a2 | 2013-06-17 07:13:16 +0000 | [diff] [blame] | 108 | |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 109 | uint16_t StreamGenerator::NextSequenceNumber() const { |
| 110 | if (packets_.empty()) |
| 111 | return sequence_number_; |
| 112 | return packets_.front().seqNum; |
| 113 | } |
| 114 | |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 115 | int StreamGenerator::PacketsRemaining() const { |
| 116 | return packets_.size(); |
| 117 | } |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 118 | |
| 119 | std::list<VCMPacket>::iterator StreamGenerator::GetPacketIterator(int index) { |
| 120 | std::list<VCMPacket>::iterator it = packets_.begin(); |
| 121 | for (int i = 0; i < index; ++i) { |
| 122 | ++it; |
andresp@webrtc.org | 1f09dbe | 2013-09-13 19:17:54 +0000 | [diff] [blame] | 123 | if (it == packets_.end()) |
| 124 | break; |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 125 | } |
| 126 | return it; |
| 127 | } |
| 128 | |
| 129 | } // namespace webrtc |