henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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 | |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 | [diff] [blame] | 11 | #include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h" |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
| 14 | #include <string.h> |
| 15 | #ifdef WIN32 |
| 16 | #include <winsock2.h> |
| 17 | #else |
| 18 | #include <netinet/in.h> |
| 19 | #endif |
| 20 | |
henrik.lundin@webrtc.org | 4b133da | 2014-10-02 08:19:38 | [diff] [blame] | 21 | #include "webrtc/base/checks.h" |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 | [diff] [blame] | 22 | #include "webrtc/modules/audio_coding/neteq/tools/packet.h" |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 | [diff] [blame] | 23 | #include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h" |
henrik.lundin@webrtc.org | 4b133da | 2014-10-02 08:19:38 | [diff] [blame] | 24 | #include "webrtc/test/rtp_file_reader.h" |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | namespace test { |
| 28 | |
| 29 | RtpFileSource* RtpFileSource::Create(const std::string& file_name) { |
henrik.lundin@webrtc.org | 4b133da | 2014-10-02 08:19:38 | [diff] [blame] | 30 | RtpFileSource* source = new RtpFileSource(); |
| 31 | CHECK(source->OpenFile(file_name)); |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 | [diff] [blame] | 32 | return source; |
| 33 | } |
| 34 | |
| 35 | RtpFileSource::~RtpFileSource() { |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | bool RtpFileSource::RegisterRtpHeaderExtension(RTPExtensionType type, |
| 39 | uint8_t id) { |
| 40 | assert(parser_.get()); |
| 41 | return parser_->RegisterRtpHeaderExtension(type, id); |
| 42 | } |
| 43 | |
| 44 | Packet* RtpFileSource::NextPacket() { |
henrik.lundin@webrtc.org | 4b133da | 2014-10-02 08:19:38 | [diff] [blame] | 45 | while (true) { |
henrik.lundin@webrtc.org | 91d928e | 2014-11-26 15:50:30 | [diff] [blame^] | 46 | RtpPacket temp_packet; |
henrik.lundin@webrtc.org | 4b133da | 2014-10-02 08:19:38 | [diff] [blame] | 47 | if (!rtp_reader_->NextPacket(&temp_packet)) { |
henrik.lundin@webrtc.org | 12396ab | 2014-06-18 12:20:31 | [diff] [blame] | 48 | return NULL; |
| 49 | } |
henrik.lundin@webrtc.org | 4b133da | 2014-10-02 08:19:38 | [diff] [blame] | 50 | if (temp_packet.length == 0) { |
henrik.lundin@webrtc.org | 12396ab | 2014-06-18 12:20:31 | [diff] [blame] | 51 | // May be an RTCP packet. |
| 52 | // Read the next one. |
| 53 | continue; |
| 54 | } |
henrik.lundin@webrtc.org | 4b133da | 2014-10-02 08:19:38 | [diff] [blame] | 55 | scoped_ptr<uint8_t[]> packet_memory(new uint8_t[temp_packet.length]); |
| 56 | memcpy(packet_memory.get(), temp_packet.data, temp_packet.length); |
henrik.lundin@webrtc.org | 12396ab | 2014-06-18 12:20:31 | [diff] [blame] | 57 | scoped_ptr<Packet> packet(new Packet(packet_memory.release(), |
henrik.lundin@webrtc.org | 4b133da | 2014-10-02 08:19:38 | [diff] [blame] | 58 | temp_packet.length, |
| 59 | temp_packet.original_length, |
| 60 | temp_packet.time_ms, |
henrik.lundin@webrtc.org | 12396ab | 2014-06-18 12:20:31 | [diff] [blame] | 61 | *parser_.get())); |
| 62 | if (!packet->valid_header()) { |
| 63 | assert(false); |
| 64 | return NULL; |
| 65 | } |
henrik.lundin@webrtc.org | 4b133da | 2014-10-02 08:19:38 | [diff] [blame] | 66 | if (filter_.test(packet->header().payloadType) || |
| 67 | (use_ssrc_filter_ && packet->header().ssrc != ssrc_)) { |
henrik.lundin@webrtc.org | c8e9818 | 2014-06-26 19:07:04 | [diff] [blame] | 68 | // This payload type should be filtered out. Continue to the next packet. |
| 69 | continue; |
| 70 | } |
henrik.lundin@webrtc.org | 12396ab | 2014-06-18 12:20:31 | [diff] [blame] | 71 | return packet.release(); |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 | [diff] [blame] | 72 | } |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | RtpFileSource::RtpFileSource() |
| 76 | : PacketSource(), |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 | [diff] [blame] | 77 | parser_(RtpHeaderParser::Create()) {} |
| 78 | |
| 79 | bool RtpFileSource::OpenFile(const std::string& file_name) { |
henrik.lundin@webrtc.org | 4b133da | 2014-10-02 08:19:38 | [diff] [blame] | 80 | rtp_reader_.reset(RtpFileReader::Create(RtpFileReader::kRtpDump, file_name)); |
| 81 | if (rtp_reader_) |
| 82 | return true; |
| 83 | rtp_reader_.reset(RtpFileReader::Create(RtpFileReader::kPcap, file_name)); |
| 84 | if (!rtp_reader_) { |
| 85 | FATAL() << "Couldn't open input file as either a rtpdump or .pcap. Note " |
| 86 | "that .pcapng is not supported."; |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 | [diff] [blame] | 87 | } |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | } // namespace test |
| 92 | } // namespace webrtc |