Ticket #465: utility to read PCAP file and sample application to read RTP files from PCAP file, decode the payload, and write the PCM output to WAV file

git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@1765 74dad513-b988-da41-8d7b-12977e46ad98
diff --git a/build.symbian/pjlib_util.mmp b/build.symbian/pjlib_util.mmp
index af5b86f..9f8ed50 100644
--- a/build.symbian/pjlib_util.mmp
+++ b/build.symbian/pjlib_util.mmp
@@ -37,6 +37,7 @@
 SOURCE	hmac_md5.c
 SOURCE	hmac_sha1.c
 SOURCE	md5.c
+SOURCE	pcap.c
 SOURCE	resolver.c
 SOURCE	scanner.c
 SOURCE	sha1.c
diff --git a/pjlib-util/build/Makefile b/pjlib-util/build/Makefile
index dd8a564..f3e1a5e 100644
--- a/pjlib-util/build/Makefile
+++ b/pjlib-util/build/Makefile
@@ -28,7 +28,7 @@
 export PJLIB_UTIL_SRCDIR = ../src/pjlib-util
 export PJLIB_UTIL_OBJS += $(OS_OBJS) $(M_OBJS) $(CC_OBJS) $(HOST_OBJS) \
 		base64.o crc32.o errno.o dns.o dns_dump.o getopt.o \
-		hmac_md5.o hmac_sha1.o md5.o resolver.o \
+		hmac_md5.o hmac_sha1.o md5.o pcap.o resolver.o \
 		scanner.o sha1.o srv_resolver.o string.o stun_simple.o \
 		stun_simple_client.o xml.o
 export PJLIB_UTIL_CFLAGS += $(_CFLAGS)
diff --git a/pjlib-util/build/pjlib_util.dsp b/pjlib-util/build/pjlib_util.dsp
index 825ede8..6578ab5 100644
--- a/pjlib-util/build/pjlib_util.dsp
+++ b/pjlib-util/build/pjlib_util.dsp
@@ -125,6 +125,10 @@
 # End Source File

 # Begin Source File

 

+SOURCE="..\src\pjlib-util\pcap.c"

+# End Source File

+# Begin Source File

+

 SOURCE="..\src\pjlib-util\resolver.c"

 # End Source File

 # Begin Source File

@@ -211,6 +215,10 @@
 # End Source File

 # Begin Source File

 

+SOURCE="..\include\pjlib-util\pcap.h"

+# End Source File

+# Begin Source File

+

 SOURCE="..\include\pjlib-util.h"

 # End Source File

 # Begin Source File

diff --git a/pjlib-util/build/pjlib_util.vcproj b/pjlib-util/build/pjlib_util.vcproj
index 2edaf65..41ed2c2 100644
--- a/pjlib-util/build/pjlib_util.vcproj
+++ b/pjlib-util/build/pjlib_util.vcproj
@@ -356,6 +356,10 @@
 				</FileConfiguration>

 			</File>

 			<File

+				RelativePath="..\src\pjlib-util\pcap.c"

+				>

+			</File>

+			<File

 				RelativePath="..\src\pjlib-util\resolver.c"

 				>

 				<FileConfiguration

@@ -625,6 +629,10 @@
 				>

 			</File>

 			<File

+				RelativePath="..\include\pjlib-util\pcap.h"

+				>

+			</File>

+			<File

 				RelativePath="..\include\pjlib-util.h"

 				>

 			</File>

diff --git a/pjlib-util/include/pjlib-util.h b/pjlib-util/include/pjlib-util.h
index fcafcf1..728d8fe 100644
--- a/pjlib-util/include/pjlib-util.h
+++ b/pjlib-util/include/pjlib-util.h
@@ -53,5 +53,7 @@
 /* Old STUN */
 #include <pjlib-util/stun_simple.h>
 
+/* PCAP */
+#include <pjlib-util/pcap.h>
 
 #endif	/* __PJLIB_UTIL_H__ */
diff --git a/pjlib-util/include/pjlib-util/pcap.h b/pjlib-util/include/pjlib-util/pcap.h
new file mode 100644
index 0000000..e27f570
--- /dev/null
+++ b/pjlib-util/include/pjlib-util/pcap.h
@@ -0,0 +1,178 @@
+/* $Id$ */
+/* 
+ * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
+ */
+#ifndef __PJLIB_UTIL_PCAP_H__
+#define __PJLIB_UTIL_PCAP_H__
+
+/**
+ * @file pcap.h
+ * @brief Simple PCAP file reader
+ */
+
+#include <pj/types.h>
+
+PJ_BEGIN_DECL
+
+/**
+ * @defgroup PJ_PCAP Simple PCAP file reader
+ * @ingroup PJLIB_UTIL
+ * @{
+ * This module describes simple utility to read PCAP file. It is not intended
+ * to support all PCAP features (that's what libpcap is for!), but it can
+ * be useful for example to playback or stream PCAP contents.
+ */
+
+/**
+ * Enumeration to describe supported data link types.
+ */
+typedef enum pj_pcap_link_type
+{
+    /** Ethernet data link */
+    PJ_PCAP_LINK_TYPE_ETH   = 1
+
+} pj_pcap_link_type;
+
+
+/**
+ * Enumeration to describe supported protocol types.
+ */
+typedef enum pj_pcap_proto_type
+{
+    /** UDP protocol */
+    PJ_PCAP_PROTO_TYPE_UDP  = 17
+
+} pj_pcap_proto_type;
+
+
+/**
+ * This structure describes the filter to be used when reading packets from
+ * a PCAP file. The filter is used to select what packets can be returned
+ * to a read operation.
+ */
+typedef struct pj_pcap_filter
+{
+    /**
+     * Select data link type, or zero to include any supported data links.
+     */
+    pj_pcap_link_type	link;
+
+    /**
+     * Select protocol, or zero to include all supported protocols.
+     */
+    pj_pcap_proto_type	proto;
+
+    /**
+     * Specify source IP address of the packets, or zero to include packets
+     * from any IP addresses. Note that IP address here must be in
+     * network byte order.
+     */
+    pj_uint32_t		ip_src;
+
+    /**
+     * Specify destination IP address of the packets, or zero to include packets
+     * destined to any IP addresses. Note that IP address here must be in
+     * network byte order.
+     */
+    pj_uint32_t		ip_dst;
+
+    /**
+     * Specify source port of the packets, or zero to include packets with
+     * any source port number. Note that the port number must be in network
+     * byte order.
+     */
+    pj_uint16_t		src_port;
+
+    /**
+     * Specify destination port of the packets, or zero to include packets with
+     * any destination port number. Note that the port number must be in network
+     * byte order.
+     */
+    pj_uint16_t		dst_port;
+
+} pj_pcap_filter;
+
+
+/** Opaque declaration for PCAP file */
+typedef struct pj_pcap_file pj_pcap_file;
+
+
+/**
+ * Initialize filter with default values. The default value is to allow
+ * any packets.
+ *
+ * @param filter    Filter to be initialized.
+ */
+PJ_DECL(void) pj_pcap_filter_default(pj_pcap_filter *filter);
+
+/**
+ * Open PCAP file.
+ *
+ * @param pool	    Pool to allocate memory.
+ * @param path	    File/path name.
+ * @param p_file    Pointer to receive PCAP file handle.
+ *
+ * @return	    PJ_SUCCESS if file can be opened successfully.
+ */
+PJ_DECL(pj_status_t) pj_pcap_open(pj_pool_t *pool,
+				  const char *path,
+				  pj_pcap_file **p_file);
+
+/**
+ * Close PCAP file.
+ *
+ * @param file	    PCAP file handle.
+ *
+ * @return	    PJ_SUCCESS on success, or the appropriate error code.
+ */
+PJ_DECL(pj_status_t) pj_pcap_close(pj_pcap_file *file);
+
+/**
+ * Configure filter for reading the file.
+ *
+ * @param file	    PCAP file handle.
+ * @param filter    The filter.
+ *
+ * @return	    PJ_SUCCESS on success, or the appropriate error code.
+ */
+PJ_DECL(pj_status_t) pj_pcap_set_filter(pj_pcap_file *file,
+				        const pj_pcap_filter *filter);
+
+/**
+ * Read UDP payload from the next packet in the PCAP file.
+ *
+ * @param file		    PCAP file handle.
+ * @param udp_payload	    Buffer to receive the UDP payload.
+ * @param udp_payload_size  On input, specify the size of the buffer.
+ *			    On output, it will be filled with the actual size
+ *			    of the payload as read from the packet.
+ *
+ * @return	    PJ_SUCCESS on success, or the appropriate error code.
+ */
+PJ_DECL(pj_status_t) pj_pcap_read_udp(pj_pcap_file *file,
+				      pj_uint8_t *udp_payload,
+				      pj_size_t *udp_payload_size);
+
+
+/**
+ * @}
+ */
+
+PJ_END_DECL
+
+#endif	/* __PJLIB_UTIL_PCAP_H__ */
+
diff --git a/pjlib-util/src/pjlib-util/pcap.c b/pjlib-util/src/pjlib-util/pcap.c
new file mode 100644
index 0000000..6be6af3
--- /dev/null
+++ b/pjlib-util/src/pjlib-util/pcap.c
@@ -0,0 +1,307 @@
+#include <pjlib-util/pcap.h>
+#include <pj/assert.h>
+#include <pj/errno.h>
+#include <pj/file_io.h>
+#include <pj/pool.h>
+#include <pj/sock.h>
+#include <pj/string.h>
+
+
+typedef struct pj_pcap_hdr 
+{
+    pj_uint32_t magic_number;   /* magic number */
+    pj_uint16_t version_major;  /* major version number */
+    pj_uint16_t version_minor;  /* minor version number */
+    pj_int32_t  thiszone;       /* GMT to local correction */
+    pj_uint32_t sigfigs;        /* accuracy of timestamps */
+    pj_uint32_t snaplen;        /* max length of captured packets, in octets */
+    pj_uint32_t network;        /* data link type */
+} pj_pcap_hdr;
+
+typedef struct pj_pcap_rec_hdr 
+{
+    pj_uint32_t ts_sec;         /* timestamp seconds */
+    pj_uint32_t ts_usec;        /* timestamp microseconds */
+    pj_uint32_t incl_len;       /* number of octets of packet saved in file */
+    pj_uint32_t orig_len;       /* actual length of packet */
+} pj_pcap_rec_hdr;
+
+typedef struct pj_pcap_eth_hdr 
+{
+    pj_uint8_t  dest[6];
+    pj_uint8_t  src[6];
+    pj_uint16_t len;
+} pj_pcap_eth_hdr;
+
+typedef struct pj_pcap_ip_hdr 
+{
+    pj_uint8_t	v_ihl;
+    pj_uint8_t	tos;
+    pj_uint16_t	len;
+    pj_uint16_t	id;
+    pj_uint16_t	flags_fragment;
+    pj_uint8_t	ttl;
+    pj_uint8_t	proto;
+    pj_uint16_t	csum;
+    pj_uint32_t	ip_src;
+    pj_uint32_t	ip_dst;
+} pj_pcap_ip_hdr;
+
+typedef struct pj_pcap_udp_hdr
+{
+    pj_uint16_t	src_port;
+    pj_uint16_t	dst_port;
+    pj_uint16_t	len;
+    pj_uint16_t	csum;
+} pj_pcap_udp_hdr;
+
+
+/* Implementation of pcap file */
+struct pj_pcap_file
+{
+    pj_oshandle_t   fd;
+    pj_bool_t	    swap;
+    pj_pcap_hdr	    hdr;
+    pj_pcap_filter  filter;
+};
+
+/* Init default filter */
+PJ_DEF(void) pj_pcap_filter_default(pj_pcap_filter *filter)
+{
+    pj_bzero(filter, sizeof(*filter));
+}
+
+/* Open pcap file */
+PJ_DEF(pj_status_t) pj_pcap_open(pj_pool_t *pool,
+				 const char *path,
+				 pj_pcap_file **p_file)
+{
+    pj_pcap_file *file;
+    pj_ssize_t sz;
+    pj_status_t status;
+
+    PJ_ASSERT_RETURN(pool && path && p_file, PJ_EINVAL);
+
+    file = PJ_POOL_ZALLOC_T(pool, pj_pcap_file);
+
+    status = pj_file_open(pool, path, PJ_O_RDONLY, &file->fd);
+    if (status != PJ_SUCCESS)
+	return status;
+
+    /* Read file pcap header */
+    sz = sizeof(file->hdr);
+    status = pj_file_read(file->fd, &file->hdr, &sz);
+    if (status != PJ_SUCCESS) {
+	pj_file_close(file->fd);
+	return status;
+    }
+
+    /* Check magic number */
+    if (file->hdr.magic_number == 0xa1b2c3d4) {
+	file->swap = PJ_FALSE;
+    } else if (file->hdr.magic_number == 0xd4c3b2a1) {
+	file->swap = PJ_TRUE;
+	file->hdr.network = pj_ntohl(file->hdr.network);
+    } else {
+	/* Not PCAP file */
+	pj_file_close(file->fd);
+	return PJ_EINVALIDOP;
+    }
+
+    *p_file = file;
+    return PJ_SUCCESS;
+}
+
+/* Close pcap file */
+PJ_DEF(pj_status_t) pj_pcap_close(pj_pcap_file *file)
+{
+    PJ_ASSERT_RETURN(file, PJ_EINVAL);
+    return pj_file_close(file->fd);
+}
+
+/* Setup filter */
+PJ_DEF(pj_status_t) pj_pcap_set_filter(pj_pcap_file *file,
+				       const pj_pcap_filter *fil)
+{
+    PJ_ASSERT_RETURN(file && fil, PJ_EINVAL);
+    pj_memcpy(&file->filter, fil, sizeof(pj_pcap_filter));
+    return PJ_SUCCESS;
+}
+
+/* Read file */
+static pj_status_t read_file(pj_pcap_file *file,
+			     void *buf,
+			     pj_ssize_t *sz)
+{
+    pj_status_t status;
+    status = pj_file_read(file->fd, buf, sz);
+    if (status != PJ_SUCCESS)
+	return status;
+    if (*sz == 0)
+	return PJ_EEOF;
+    return PJ_SUCCESS;
+}
+
+static pj_status_t skip(pj_oshandle_t fd, pj_off_t bytes)
+{
+    pj_status_t status;
+    status = pj_file_setpos(fd, bytes, PJ_SEEK_CUR);
+    if (status != PJ_SUCCESS)
+	return status; 
+    return PJ_SUCCESS;
+}
+
+
+#define SKIP_PKT()  \
+	if (rec_incl > sz_read) { \
+	    status = skip(file->fd, rec_incl-sz_read);\
+	    if (status != PJ_SUCCESS) \
+		return status; \
+	}
+
+/* Read UDP packet */
+PJ_DEF(pj_status_t) pj_pcap_read_udp(pj_pcap_file *file,
+				     pj_uint8_t *udp_payload,
+				     pj_size_t *udp_payload_size)
+{
+    PJ_ASSERT_RETURN(file && udp_payload && udp_payload_size, PJ_EINVAL);
+    PJ_ASSERT_RETURN(*udp_payload_size, PJ_EINVAL);
+
+    /* Check data link type in PCAP file header */
+    if ((file->filter.link && 
+	    file->hdr.network != (pj_uint32_t)file->filter.link) ||
+	file->hdr.network != PJ_PCAP_LINK_TYPE_ETH)
+    {
+	/* Link header other than Ethernet is not supported for now */
+	return PJ_ENOTSUP;
+    }
+
+    /* Loop until we have the packet */
+    for (;;) {
+	union {
+	    pj_pcap_rec_hdr rec;
+	    pj_pcap_eth_hdr eth;
+	    pj_pcap_ip_hdr ip;
+	    pj_pcap_udp_hdr udp;
+	} tmp;
+	unsigned rec_incl;
+	pj_ssize_t sz;
+	unsigned sz_read = 0;
+	pj_status_t status;
+
+	/* Read PCAP packet header */
+	sz = sizeof(tmp.rec);
+	status = read_file(file, &tmp.rec, &sz);
+	if (status != PJ_SUCCESS) 
+	    return status;
+
+	rec_incl = tmp.rec.incl_len;
+
+	/* Swap byte ordering */
+	if (file->swap) {
+	    tmp.rec.incl_len = pj_ntohl(tmp.rec.incl_len);
+	    tmp.rec.orig_len = pj_ntohl(tmp.rec.orig_len);
+	    tmp.rec.ts_sec = pj_ntohl(tmp.rec.ts_sec);
+	    tmp.rec.ts_usec = pj_ntohl(tmp.rec.ts_usec);
+	}
+
+	/* Read link layer header */
+	switch (file->hdr.network) {
+	case PJ_PCAP_LINK_TYPE_ETH:
+	    sz = sizeof(tmp.eth);
+	    status = read_file(file, &tmp.eth, &sz);
+	    break;
+	default:
+	    return PJ_ENOTSUP;
+	}
+
+	if (status != PJ_SUCCESS)
+	    return status;
+
+	sz_read += sz;
+	    
+	/* Read IP header */
+	sz = sizeof(tmp.ip);
+	status = read_file(file, &tmp.ip, &sz);
+	if (status != PJ_SUCCESS)
+	    return status;
+
+	sz_read += sz;
+
+	/* Skip if IP source mismatch */
+	if (file->filter.ip_src && tmp.ip.ip_src != file->filter.ip_src) {
+	    SKIP_PKT();
+	    continue;
+	}
+
+	/* Skip if IP destination mismatch */
+	if (file->filter.ip_dst && tmp.ip.ip_dst != file->filter.ip_dst) {
+	    SKIP_PKT();
+	    continue;
+	}
+
+	/* Skip if proto mismatch */
+	if (file->filter.proto && tmp.ip.proto != file->filter.proto) {
+	    SKIP_PKT();
+	    continue;
+	}
+
+	/* Read transport layer header */
+	switch (tmp.ip.proto) {
+	case PJ_PCAP_PROTO_TYPE_UDP:
+	    sz = sizeof(tmp.udp);
+	    status = read_file(file, &tmp.udp, &sz);
+	    if (status != PJ_SUCCESS)
+		return status;
+
+	    sz_read += sz;
+
+	    /* Skip if source port mismatch */
+	    if (file->filter.src_port && 
+	        tmp.udp.src_port != file->filter.src_port) 
+	    {
+		SKIP_PKT();
+		continue;
+	    }
+
+	    /* Skip if destination port mismatch */
+	    if (file->filter.dst_port && 
+		tmp.udp.dst_port != file->filter.dst_port) 
+	    {
+		SKIP_PKT();
+		continue;
+	    }
+
+	    /* Calculate payload size */
+	    sz = pj_ntohs(tmp.udp.len) - sizeof(tmp.udp);
+	    break;
+	default:
+	    SKIP_PKT();
+	    continue;
+	}
+
+	/* Check if payload fits the buffer */
+	if (sz > (pj_ssize_t)*udp_payload_size) {
+	    SKIP_PKT();
+	    return PJ_ETOOSMALL;
+	}
+
+	/* Read the payload */
+	status = read_file(file, udp_payload, &sz);
+	if (status != PJ_SUCCESS)
+	    return status;
+
+	sz_read += sz;
+
+	*udp_payload_size = sz;
+
+	/* Check that we've read all the packets */
+	PJ_ASSERT_RETURN(sz_read == rec_incl, PJ_EBUG);
+
+	return PJ_SUCCESS;
+    }
+
+    /* Does not reach here */
+}
+
+
diff --git a/pjproject.dsw b/pjproject.dsw
index 7054e5a..795a17d 100644
--- a/pjproject.dsw
+++ b/pjproject.dsw
@@ -3,7 +3,7 @@
 

 ###############################################################################

 

-Project: "libgsmcodec"=".\THIRD_PARTY\BUILD\GSM\libgsmcodec.dsp" - Package Owner=<4>

+Project: "libgsmcodec"=.\THIRD_PARTY\BUILD\GSM\libgsmcodec.dsp - Package Owner=<4>

 

 Package=<5>

 {{{

@@ -15,7 +15,7 @@
 

 ###############################################################################

 

-Project: "libilbccodec"=".\THIRD_PARTY\BUILD\ILBC\libilbccodec.dsp" - Package Owner=<4>

+Project: "libilbccodec"=.\THIRD_PARTY\BUILD\ILBC\libilbccodec.dsp - Package Owner=<4>

 

 Package=<5>

 {{{

@@ -27,7 +27,7 @@
 

 ###############################################################################

 

-Project: "libmilenage"=".\third_party\build\milenage\libmilenage.dsp" - Package Owner=<4>

+Project: "libmilenage"=.\third_party\build\milenage\libmilenage.dsp - Package Owner=<4>

 

 Package=<5>

 {{{

@@ -39,7 +39,7 @@
 

 ###############################################################################

 

-Project: "libportaudio"=".\THIRD_PARTY\BUILD\PORTAUDIO\libportaudio.dsp" - Package Owner=<4>

+Project: "libportaudio"=.\THIRD_PARTY\BUILD\PORTAUDIO\libportaudio.dsp - Package Owner=<4>

 

 Package=<5>

 {{{

@@ -51,7 +51,7 @@
 

 ###############################################################################

 

-Project: "libresample"=".\THIRD_PARTY\BUILD\RESAMPLE\libresample.dsp" - Package Owner=<4>

+Project: "libresample"=.\THIRD_PARTY\BUILD\RESAMPLE\libresample.dsp - Package Owner=<4>

 

 Package=<5>

 {{{

@@ -63,7 +63,7 @@
 

 ###############################################################################

 

-Project: "libresample_dll"=".\THIRD_PARTY\BUILD\RESAMPLE\libresample_dll.dsp" - Package Owner=<4>

+Project: "libresample_dll"=.\THIRD_PARTY\BUILD\RESAMPLE\libresample_dll.dsp - Package Owner=<4>

 

 Package=<5>

 {{{

@@ -75,7 +75,7 @@
 

 ###############################################################################

 

-Project: "libspeex"=".\third_party\build\speex\libspeex.dsp" - Package Owner=<4>

+Project: "libspeex"=.\third_party\build\speex\libspeex.dsp - Package Owner=<4>

 

 Package=<5>

 {{{

@@ -87,7 +87,7 @@
 

 ###############################################################################

 

-Project: "libsrtp"=".\third_party\build\srtp\libsrtp.dsp" - Package Owner=<4>

+Project: "libsrtp"=.\third_party\build\srtp\libsrtp.dsp - Package Owner=<4>

 

 Package=<5>

 {{{

@@ -99,7 +99,7 @@
 

 ###############################################################################

 

-Project: "pjlib"=".\pjlib\build\pjlib.dsp" - Package Owner=<4>

+Project: "pjlib"=.\pjlib\build\pjlib.dsp - Package Owner=<4>

 

 Package=<5>

 {{{

@@ -111,7 +111,7 @@
 

 ###############################################################################

 

-Project: "pjlib_test"=".\pjlib\build\pjlib_test.dsp" - Package Owner=<4>

+Project: "pjlib_test"=.\pjlib\build\pjlib_test.dsp - Package Owner=<4>

 

 Package=<5>

 {{{

@@ -156,7 +156,7 @@
 

 ###############################################################################

 

-Project: "pjmedia"=".\pjmedia\build\pjmedia.dsp" - Package Owner=<4>

+Project: "pjmedia"=.\pjmedia\build\pjmedia.dsp - Package Owner=<4>

 

 Package=<5>

 {{{

@@ -168,7 +168,7 @@
 

 ###############################################################################

 

-Project: "pjmedia_codec"=".\pjmedia\build\pjmedia_codec.dsp" - Package Owner=<4>

+Project: "pjmedia_codec"=.\pjmedia\build\pjmedia_codec.dsp - Package Owner=<4>

 

 Package=<5>

 {{{

@@ -180,7 +180,7 @@
 

 ###############################################################################

 

-Project: "pjnath"=".\pjnath\build\pjnath.dsp" - Package Owner=<4>

+Project: "pjnath"=.\pjnath\build\pjnath.dsp - Package Owner=<4>

 

 Package=<5>

 {{{

@@ -192,7 +192,7 @@
 

 ###############################################################################

 

-Project: "pjsip_core"=".\pjsip\build\pjsip_core.dsp" - Package Owner=<4>

+Project: "pjsip_core"=.\pjsip\build\pjsip_core.dsp - Package Owner=<4>

 

 Package=<5>

 {{{

@@ -204,7 +204,7 @@
 

 ###############################################################################

 

-Project: "pjsip_simple"=".\pjsip\build\pjsip_simple.dsp" - Package Owner=<4>

+Project: "pjsip_simple"=.\pjsip\build\pjsip_simple.dsp - Package Owner=<4>

 

 Package=<5>

 {{{

@@ -216,7 +216,7 @@
 

 ###############################################################################

 

-Project: "pjsip_ua"=".\pjsip\build\pjsip_ua.dsp" - Package Owner=<4>

+Project: "pjsip_ua"=.\pjsip\build\pjsip_ua.dsp - Package Owner=<4>

 

 Package=<5>

 {{{

@@ -288,7 +288,7 @@
 

 ###############################################################################

 

-Project: "pjsua_lib"=".\pjsip\build\pjsua_lib.dsp" - Package Owner=<4>

+Project: "pjsua_lib"=.\pjsip\build\pjsua_lib.dsp - Package Owner=<4>

 

 Package=<5>

 {{{

@@ -350,6 +350,9 @@
     Begin Project Dependency

     Project_Dep_Name pjnath

     End Project Dependency

+    Begin Project Dependency

+    Project_Dep_Name libsrtp

+    End Project Dependency

 }}}

 

 ###############################################################################

@@ -404,11 +407,14 @@
     Begin Project Dependency

     Project_Dep_Name pjnath

     End Project Dependency

+    Begin Project Dependency

+    Project_Dep_Name libsrtp

+    End Project Dependency

 }}}

 

 ###############################################################################

 

-Project: "test_pjsip"=".\pjsip\build\test_pjsip.dsp" - Package Owner=<4>

+Project: "test_pjsip"=.\pjsip\build\test_pjsip.dsp - Package Owner=<4>

 

 Package=<5>

 {{{

diff --git a/pjsip-apps/build/Samples-vc.mak b/pjsip-apps/build/Samples-vc.mak
index 0d862e3..f7df993 100644
--- a/pjsip-apps/build/Samples-vc.mak
+++ b/pjsip-apps/build/Samples-vc.mak
@@ -54,6 +54,7 @@
 	  $(BINDIR)\encdec.exe \
 	  $(BINDIR)\level.exe \
 	  $(BINDIR)\mix.exe \
+	  $(BINDIR)\pcaputil.exe\
 	  $(BINDIR)\pjsip-perf.exe \
 	  $(BINDIR)\playfile.exe \
 	  $(BINDIR)\playsine.exe\
diff --git a/pjsip-apps/build/Samples.mak b/pjsip-apps/build/Samples.mak
index 3a81f3c..f96f77f 100644
--- a/pjsip-apps/build/Samples.mak
+++ b/pjsip-apps/build/Samples.mak
@@ -18,6 +18,7 @@
 	   level \
 	   mix \
 	   pjsip-perf \
+	   pcaputil \
 	   playfile \
 	   playsine \
 	   recfile \
diff --git a/pjsip-apps/build/sample_debug.dsp b/pjsip-apps/build/sample_debug.dsp
index 03c49d1..c788f77 100644
--- a/pjsip-apps/build/sample_debug.dsp
+++ b/pjsip-apps/build/sample_debug.dsp
@@ -90,6 +90,13 @@
 # Begin Source File

 

 SOURCE=..\src\samples\debug.c

+

+!IF  "$(CFG)" == "sample_debug - Win32 Release"

+

+!ELSEIF  "$(CFG)" == "sample_debug - Win32 Debug"

+

+!ENDIF 

+

 # End Source File

 # End Group

 # Begin Group "Header Files"

diff --git a/pjsip-apps/build/samples.dsp b/pjsip-apps/build/samples.dsp
index 3bdca95..c5742ca 100644
--- a/pjsip-apps/build/samples.dsp
+++ b/pjsip-apps/build/samples.dsp
@@ -118,6 +118,10 @@
 # End Source File

 # Begin Source File

 

+SOURCE=..\src\samples\pcaputil.c

+# End Source File

+# Begin Source File

+

 SOURCE="..\src\samples\pjsip-perf.c"

 # End Source File

 # Begin Source File

@@ -184,10 +188,6 @@
 

 SOURCE=..\src\samples\tonegen.c

 # End Source File

-# Begin Source File

-

-SOURCE=..\src\samples\transportpausetest.c

-# End Source File

 # End Group

 # Begin Group "Header Files"

 

diff --git a/pjsip-apps/src/samples/pcaputil.c b/pjsip-apps/src/samples/pcaputil.c
new file mode 100644
index 0000000..83a7ead
--- /dev/null
+++ b/pjsip-apps/src/samples/pcaputil.c
@@ -0,0 +1,361 @@
+/* $Id$ */
+/* 
+ * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
+ */
+#include <pjlib.h>
+#include <pjlib-util.h>
+#include <pjmedia.h>
+#include <pjmedia-codec.h>
+
+static const char *USAGE =
+"pcaputil [options] INPUT OUTPUT\n"
+"\n"
+"  Convert captured RTP packets in PCAP file to WAV or stream it\n"
+"  to remote destination.\n"
+"\n"
+"INPUT is the PCAP file name/path\n"
+"\n"
+"Options to filter packets from PCAP file:\n"
+"  --src-ip=IP            Only include packets from this source address\n"
+"  --dst-ip=IP            Only include packets destined to this address\n"
+"  --src-port=port        Only include packets from this source port number\n"
+"  --dst-port=port        Only include packets destined to this port number\n"
+"\n"
+"Options for saving to WAV file:\n"
+""
+"  OUTPUT is WAV file:    Set output to WAV file. The program will decode the\n"
+"                         RTP contents to the specified WAV file using codec\n"
+"                         that is available in PJMEDIA, and optionally decrypt\n"
+"                         the content using the SRTP crypto and keys below.\n"
+"  --srtp-crypto=TAG, -c  Set crypto to be used to decrypt SRTP packets. Valid\n"
+"                         tags are: \n"
+"                           AES_CM_128_HMAC_SHA1_80 \n"
+"                           AES_CM_128_HMAC_SHA1_32\n"
+"  --srtp-key=KEY, -k     Set the base64 key to decrypt SRTP packets.\n"
+"\n"
+"  Example:\n"
+"    pcaputil file.pcap output.wav\n"
+"    pcaputil -c AES_CM_128_HMAC_SHA1_80 \\\n"
+"              -k VLDONbsbGl2Puqy+0PV7w/uGfpSPKFevDpxGsxN3 \\\n"
+"              file.pcap output.wav\n"
+;
+
+static pj_caching_pool cp;
+static pj_pool_t *pool;
+static pjmedia_endpt *mept;
+static pj_pcap_file *pcap;
+static pjmedia_port *wav;
+static pjmedia_transport *srtp;
+
+static void err_exit(const char *title, pj_status_t status)
+{
+    if (status != PJ_SUCCESS) {
+	char errmsg[PJ_ERR_MSG_SIZE];
+	pj_strerror(status, errmsg, sizeof(errmsg));
+	printf("Error: %s: %s\n", title, errmsg);
+    } else {
+	printf("Error: %s\n", title);
+    }
+
+    if (srtp) pjmedia_transport_close(srtp);
+    if (wav) pjmedia_port_destroy(wav);
+    if (pcap) pj_pcap_close(pcap);
+    if (mept) pjmedia_endpt_destroy(mept);
+    if (pool) pj_pool_release(pool);
+    pj_caching_pool_destroy(&cp);
+    pj_shutdown();
+
+    exit(1);
+}
+
+#define T(op)	    do { \
+			status = op; \
+			if (status != PJ_SUCCESS) \
+    			    err_exit(#op, status); \
+		    } while (0)
+
+
+static void read_rtp(pj_uint8_t *buf, pj_size_t bufsize,
+		     pjmedia_rtp_hdr **rtp,
+		     pj_uint8_t **payload,
+		     unsigned *payload_size)
+{
+    pj_size_t sz = bufsize;
+    pj_status_t status;
+
+    status = pj_pcap_read_udp(pcap, buf, &sz);
+    if (status != PJ_SUCCESS)
+	err_exit("Error reading PCAP file", status);
+
+    if (sz < sizeof(pjmedia_rtp_hdr) + 10) {
+	err_exit("Invalid RTP packet", PJ_SUCCESS);
+    }
+
+    /* Decrypt SRTP */
+    if (srtp) {
+	int len = sz;
+	T(pjmedia_transport_srtp_decrypt_pkt(srtp, PJ_TRUE, buf, &len));
+	sz = len;
+    }
+
+    *rtp = (pjmedia_rtp_hdr*)buf;
+    *payload = (pj_uint8_t*) (buf + sizeof(pjmedia_rtp_hdr));
+    *payload_size = sz - sizeof(pjmedia_rtp_hdr);
+}
+
+static void pcap2wav(const char *wav_filename, const pj_str_t *srtp_crypto,
+		     const pj_str_t *srtp_key)
+{
+    struct pkt
+    {
+	pj_uint8_t	 buffer[320];
+	pjmedia_rtp_hdr	*rtp;
+	pj_uint8_t	*payload;
+	unsigned	 payload_len;
+    } pkt0;
+    pjmedia_codec_mgr *cmgr;
+    pjmedia_codec_info *ci;
+    pjmedia_codec_param param;
+    pjmedia_codec *codec;
+    unsigned samples_per_frame;
+    pj_status_t status;
+
+    /* Initialize all codecs */
+#if PJMEDIA_HAS_SPEEX_CODEC
+    T( pjmedia_codec_speex_init(mept, 0, 10, 10) );
+#endif /* PJMEDIA_HAS_SPEEX_CODEC */
+
+#if PJMEDIA_HAS_ILBC_CODEC
+    T( pjmedia_codec_ilbc_init(mept, 30) );
+#endif /* PJMEDIA_HAS_ILBC_CODEC */
+
+#if PJMEDIA_HAS_GSM_CODEC
+    T( pjmedia_codec_gsm_init(mept) );
+#endif /* PJMEDIA_HAS_GSM_CODEC */
+
+#if PJMEDIA_HAS_G711_CODEC
+    T( pjmedia_codec_g711_init(mept) );
+#endif	/* PJMEDIA_HAS_G711_CODEC */
+
+#if PJMEDIA_HAS_L16_CODEC
+    T( pjmedia_codec_l16_init(mept, 0) );
+#endif	/* PJMEDIA_HAS_L16_CODEC */
+
+    /* Create SRTP transport is needed */
+    if (srtp_crypto->slen) {
+	pjmedia_srtp_crypto crypto;
+
+	pj_bzero(&crypto, sizeof(crypto));
+	crypto.key = *srtp_key;
+	crypto.name = *srtp_crypto;
+	T( pjmedia_transport_srtp_create(mept, NULL, NULL, &srtp) );
+	T( pjmedia_transport_srtp_start(srtp, &crypto, &crypto) );
+    }
+
+    /* Read first packet */
+    read_rtp(pkt0.buffer, sizeof(pkt0.buffer), &pkt0.rtp, 
+	     &pkt0.payload, &pkt0.payload_len);
+
+    cmgr = pjmedia_endpt_get_codec_mgr(mept);
+
+    /* Get codec info and param for the specified payload type */
+    T( pjmedia_codec_mgr_get_codec_info(cmgr, pkt0.rtp->pt, &ci) );
+    T( pjmedia_codec_mgr_get_default_param(cmgr, ci, &param) );
+
+    /* Alloc and init codec */
+    T( pjmedia_codec_mgr_alloc_codec(cmgr, ci, &codec) );
+    T( codec->op->init(codec, pool) );
+    T( codec->op->open(codec, &param) );
+
+    /* Open WAV file */
+    samples_per_frame = ci->clock_rate * param.info.frm_ptime / 1000;
+    T( pjmedia_wav_writer_port_create(pool, wav_filename,
+				      ci->clock_rate, ci->channel_cnt,
+				      samples_per_frame,
+				      param.info.pcm_bits_per_sample, 0, 0,
+				      &wav) );
+
+    /* Loop reading PCAP and writing WAV file */
+    for (;;) {
+	struct pkt pkt1;
+	pj_timestamp ts;
+	pjmedia_frame frames[16], pcm_frame;
+	short pcm[320];
+	unsigned i, frame_cnt;
+	long samples_cnt, ts_gap;
+
+	pj_assert(sizeof(pcm) >= samples_per_frame);
+
+	/* Parse first packet */
+	ts.u64 = 0;
+	frame_cnt = PJ_ARRAY_SIZE(frames);
+	T( codec->op->parse(codec, pkt0.payload, pkt0.payload_len, &ts, 
+			    &frame_cnt, frames) );
+
+	/* Decode and write to WAV file */
+	samples_cnt = 0;
+	for (i=0; i<frame_cnt; ++i) {
+	    pjmedia_frame pcm_frame;
+
+	    pcm_frame.buf = pcm;
+	    pcm_frame.size = samples_per_frame * 2;
+
+	    T( codec->op->decode(codec, &frames[i], pcm_frame.size, &pcm_frame) );
+	    T( pjmedia_port_put_frame(wav, &pcm_frame) );
+	    samples_cnt += samples_per_frame;
+	}
+
+	/* Read next packet */
+	read_rtp(pkt1.buffer, sizeof(pkt1.buffer), &pkt1.rtp,
+		 &pkt1.payload, &pkt1.payload_len);
+
+	/* Fill in the gap (if any) between pkt0 and pkt1 */
+	ts_gap = pj_ntohl(pkt1.rtp->ts) - pj_ntohl(pkt0.rtp->ts) -
+		 samples_cnt;
+	while (ts_gap >= (long)samples_per_frame) {
+
+	    pcm_frame.buf = pcm;
+	    pcm_frame.size = samples_per_frame * 2;
+
+	    if (codec->op->recover) {
+		T( codec->op->recover(codec, pcm_frame.size, &pcm_frame) );
+	    } else {
+		pj_bzero(pcm_frame.buf, pcm_frame.size);
+	    }
+
+	    T( pjmedia_port_put_frame(wav, &pcm_frame) );
+	    ts_gap -= samples_per_frame;
+	}
+	
+	/* Next */
+	pkt0 = pkt1;
+	pkt0.rtp = (pjmedia_rtp_hdr*)pkt0.buffer;
+	pkt0.payload = pkt0.buffer + (pkt1.payload - pkt1.buffer);
+    }
+}
+
+
+int main(int argc, char *argv[])
+{
+    pj_str_t input, output, wav, srtp_crypto, srtp_key;
+    pj_pcap_filter filter;
+    pj_status_t status;
+
+    enum { OPT_SRC_IP = 1, OPT_DST_IP, OPT_SRC_PORT, OPT_DST_PORT };
+    struct pj_getopt_option long_options[] = {
+	{ "srtp-crypto",    1, 0, 'c' },
+	{ "srtp-key",	    1, 0, 'k' },
+	{ "src-ip",	    1, 0, OPT_SRC_IP },
+	{ "dst-ip",	    1, 0, OPT_DST_IP },
+	{ "src-port",	    1, 0, OPT_SRC_PORT },
+	{ "dst-port",	    1, 0, OPT_DST_PORT },
+	{ NULL, 0, 0, 0}
+    };
+    int c;
+    int option_index;
+    char key_bin[32];
+
+    srtp_crypto.slen = srtp_key.slen = 0;
+
+    pj_pcap_filter_default(&filter);
+    filter.link = PJ_PCAP_LINK_TYPE_ETH;
+    filter.proto = PJ_PCAP_PROTO_TYPE_UDP;
+
+    /* Parse arguments */
+    pj_optind = 0;
+    while((c=pj_getopt_long(argc,argv, "c:k:", long_options, &option_index))!=-1) {
+	switch (c) {
+	case 'c':
+	    srtp_crypto = pj_str(pj_optarg);
+	    break;
+	case 'k':
+	    {
+		int key_len = sizeof(key_bin);
+		srtp_key = pj_str(pj_optarg);
+		if (pj_base64_decode(&srtp_key, (pj_uint8_t*)key_bin, &key_len)) {
+		    puts("Error: invalid key");
+		    return 1;
+		}
+		srtp_key.ptr = key_bin;
+		srtp_key.slen = key_len;
+	    }
+	    break;
+	case OPT_SRC_IP:
+	    {
+		pj_str_t t = pj_str(pj_optarg);
+		pj_in_addr a = pj_inet_addr(&t);
+		filter.ip_src = a.s_addr;
+	    }
+	    break;
+	case OPT_DST_IP:
+	    {
+		pj_str_t t = pj_str(pj_optarg);
+		pj_in_addr a = pj_inet_addr(&t);
+		filter.ip_dst = a.s_addr;
+	    }
+	    break;
+	case OPT_SRC_PORT:
+	    filter.src_port = pj_htons((pj_uint16_t)atoi(pj_optarg));
+	    break;
+	case OPT_DST_PORT:
+	    filter.dst_port = pj_htons((pj_uint16_t)atoi(pj_optarg));
+	    break;
+	default:
+	    puts("Error: invalid option");
+	    return 1;
+	}
+    }
+
+    if (pj_optind != argc - 2) {
+	puts(USAGE);
+	return 1;
+    }
+
+    if (!(srtp_crypto.slen) != !(srtp_key.slen)) {
+	puts("Error: both SRTP crypto and key must be specified");
+	puts(USAGE);
+	return 1;
+    }
+
+    input = pj_str(argv[pj_optind]);
+    output = pj_str(argv[pj_optind+1]);
+    wav = pj_str(".wav");
+    
+    T( pj_init() );
+
+    pj_caching_pool_init(&cp, NULL, 0);
+    pool = pj_pool_create(&cp.factory, "pcaputil", 1000, 1000, NULL);
+
+    T( pjlib_util_init() );
+    T( pjmedia_endpt_create(&cp.factory, NULL, 0, &mept) );
+
+    T( pj_pcap_open(pool, input.ptr, &pcap) );
+    T( pj_pcap_set_filter(pcap, &filter) );
+
+    if (pj_stristr(&output, &wav)) {
+	pcap2wav(output.ptr, &srtp_crypto, &srtp_key);
+    } else {
+	err_exit("invalid output file", PJ_EINVAL);
+    }
+
+    pjmedia_endpt_destroy(mept);
+    pj_pool_release(pool);
+    pj_caching_pool_destroy(&cp);
+    pj_shutdown();
+    return 0;
+}
+