blob: 794c9836f609d8625a0f8697558345742c6da20f [file] [log] [blame]
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:231/*
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.org9c55f0f2014-06-09 08:10:2811#include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h"
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:2312
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.org4b133da2014-10-02 08:19:3821#include "webrtc/base/checks.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:2822#include "webrtc/modules/audio_coding/neteq/tools/packet.h"
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:2323#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
henrik.lundin@webrtc.org4b133da2014-10-02 08:19:3824#include "webrtc/test/rtp_file_reader.h"
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:2325
26namespace webrtc {
27namespace test {
28
29RtpFileSource* RtpFileSource::Create(const std::string& file_name) {
henrik.lundin@webrtc.org4b133da2014-10-02 08:19:3830 RtpFileSource* source = new RtpFileSource();
31 CHECK(source->OpenFile(file_name));
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:2332 return source;
33}
34
35RtpFileSource::~RtpFileSource() {
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:2336}
37
38bool RtpFileSource::RegisterRtpHeaderExtension(RTPExtensionType type,
39 uint8_t id) {
40 assert(parser_.get());
41 return parser_->RegisterRtpHeaderExtension(type, id);
42}
43
44Packet* RtpFileSource::NextPacket() {
henrik.lundin@webrtc.org4b133da2014-10-02 08:19:3845 while (true) {
henrik.lundin@webrtc.org91d928e2014-11-26 15:50:3046 RtpPacket temp_packet;
henrik.lundin@webrtc.org4b133da2014-10-02 08:19:3847 if (!rtp_reader_->NextPacket(&temp_packet)) {
henrik.lundin@webrtc.org12396ab2014-06-18 12:20:3148 return NULL;
49 }
henrik.lundin@webrtc.org4b133da2014-10-02 08:19:3850 if (temp_packet.length == 0) {
henrik.lundin@webrtc.org12396ab2014-06-18 12:20:3151 // May be an RTCP packet.
52 // Read the next one.
53 continue;
54 }
henrik.lundin@webrtc.org4b133da2014-10-02 08:19:3855 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.org12396ab2014-06-18 12:20:3157 scoped_ptr<Packet> packet(new Packet(packet_memory.release(),
henrik.lundin@webrtc.org4b133da2014-10-02 08:19:3858 temp_packet.length,
59 temp_packet.original_length,
60 temp_packet.time_ms,
henrik.lundin@webrtc.org12396ab2014-06-18 12:20:3161 *parser_.get()));
62 if (!packet->valid_header()) {
63 assert(false);
64 return NULL;
65 }
henrik.lundin@webrtc.org4b133da2014-10-02 08:19:3866 if (filter_.test(packet->header().payloadType) ||
67 (use_ssrc_filter_ && packet->header().ssrc != ssrc_)) {
henrik.lundin@webrtc.orgc8e98182014-06-26 19:07:0468 // This payload type should be filtered out. Continue to the next packet.
69 continue;
70 }
henrik.lundin@webrtc.org12396ab2014-06-18 12:20:3171 return packet.release();
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:2372 }
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:2373}
74
75RtpFileSource::RtpFileSource()
76 : PacketSource(),
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:2377 parser_(RtpHeaderParser::Create()) {}
78
79bool RtpFileSource::OpenFile(const std::string& file_name) {
henrik.lundin@webrtc.org4b133da2014-10-02 08:19:3880 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.org810acbc2014-04-14 18:42:2387 }
88 return true;
89}
90
91} // namespace test
92} // namespace webrtc