summaryrefslogtreecommitdiff
path: root/pjmedia
diff options
context:
space:
mode:
Diffstat (limited to 'pjmedia')
-rw-r--r--pjmedia/src/pjmedia.h76
-rw-r--r--pjmedia/src/pjmedia/rtcp.c432
-rw-r--r--pjmedia/src/pjmedia/rtcp.h384
-rw-r--r--pjmedia/src/pjmedia/rtp.c522
-rw-r--r--pjmedia/src/pjmedia/rtp.h512
-rw-r--r--pjmedia/src/pjmedia/sdp.c1898
-rw-r--r--pjmedia/src/pjmedia/sdp.h664
-rw-r--r--pjmedia/src/pjmedia/session.c1666
-rw-r--r--pjmedia/src/pjmedia/session.h304
-rw-r--r--pjmedia/src/pjmedia/sound.h402
-rw-r--r--pjmedia/src/pjmedia/stream.c1288
-rw-r--r--pjmedia/src/pjmedia/stream.h198
12 files changed, 4173 insertions, 4173 deletions
diff --git a/pjmedia/src/pjmedia.h b/pjmedia/src/pjmedia.h
index 534e0f24..c4f2ab64 100644
--- a/pjmedia/src/pjmedia.h
+++ b/pjmedia/src/pjmedia.h
@@ -1,38 +1,38 @@
-/* $Id$
- *
- */
-/*
- * PJMEDIA - Multimedia over IP Stack
- * (C)2003-2005 Benny Prijono <bennylp@bulukucing.org>
- *
- * Author:
- * Benny Prijono <bennylp@bulukucing.org>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library 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
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-#ifndef __PJMEDIA_H__
-#define __PJMEDIA_H__
-
-#include <pjmedia/codec.h>
-#include <pjmedia/jbuf.h>
-#include <pjmedia/mediamgr.h>
-#include <pjmedia/rtcp.h>
-#include <pjmedia/rtp.h>
-#include <pjmedia/session.h>
-#include <pjmedia/sound.h>
-#include <pjmedia/sdp.h>
-
-#endif /* __PJMEDIA_H__ */
-
+/* $Id$
+ *
+ */
+/*
+ * PJMEDIA - Multimedia over IP Stack
+ * (C)2003-2005 Benny Prijono <bennylp@bulukucing.org>
+ *
+ * Author:
+ * Benny Prijono <bennylp@bulukucing.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+#ifndef __PJMEDIA_H__
+#define __PJMEDIA_H__
+
+#include <pjmedia/codec.h>
+#include <pjmedia/jbuf.h>
+#include <pjmedia/mediamgr.h>
+#include <pjmedia/rtcp.h>
+#include <pjmedia/rtp.h>
+#include <pjmedia/session.h>
+#include <pjmedia/sound.h>
+#include <pjmedia/sdp.h>
+
+#endif /* __PJMEDIA_H__ */
+
diff --git a/pjmedia/src/pjmedia/rtcp.c b/pjmedia/src/pjmedia/rtcp.c
index d606579d..70484388 100644
--- a/pjmedia/src/pjmedia/rtcp.c
+++ b/pjmedia/src/pjmedia/rtcp.c
@@ -1,216 +1,216 @@
-/* $Id$ */
-/*
- * Copyright (C) 2003-2006 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 <pjmedia/rtcp.h>
-#include <pj/os.h> /* pj_gettimeofday */
-#include <pj/sock.h> /* pj_htonx, pj_ntohx */
-#include <string.h> /* memset */
-
-#define RTCP_SR 200
-#define RTCP_RR 201
-
-
-
-/*
- * Get NTP time.
- */
-static void rtcp_get_ntp_time(struct pj_rtcp_ntp_rec *ntp)
-{
- pj_time_val tv;
-
- pj_gettimeofday(&tv);
-
- ntp->hi = tv.sec;
- tv.msec = tv.msec % 1000;
- ntp->lo = tv.msec * 0xFFFF / 1000;
- ntp->lo <<= 16;
-}
-
-
-PJ_DEF(void) pj_rtcp_init(pj_rtcp_session *s, pj_uint32_t ssrc)
-{
- pj_rtcp_pkt *rtcp_pkt = &s->rtcp_pkt;
-
- memset(rtcp_pkt, 0, sizeof(pj_rtcp_pkt));
-
- /* Init time */
- s->rtcp_lsr.hi = s->rtcp_lsr.lo = 0;
- s->rtcp_lsr_time = 0;
-
- /* Init common RTCP header */
- rtcp_pkt->common.version = 2;
- rtcp_pkt->common.count = 1;
- rtcp_pkt->common.pt = RTCP_SR;
- rtcp_pkt->common.length = pj_htons(12);
-
- /* Init SR */
- rtcp_pkt->sr.ssrc = pj_htonl(ssrc);
-
- /* RR will be initialized on receipt of the first RTP packet. */
-}
-
-PJ_DEF(void) pj_rtcp_fini(pj_rtcp_session *session)
-{
- /* Nothing to do. */
- PJ_UNUSED_ARG(session)
-}
-
-static void rtcp_init_seq(pj_rtcp_session *s, pj_uint16_t seq)
-{
- s->received = 0;
- s->expected_prior = 0;
- s->received_prior = 0;
- s->transit = 0;
- s->jitter = 0;
-
- pj_rtp_seq_restart(&s->seq_ctrl, seq);
-}
-
-PJ_DEF(void) pj_rtcp_rx_rtp(pj_rtcp_session *s, pj_uint16_t seq, pj_uint32_t rtp_ts)
-{
- pj_uint32_t arrival;
- pj_int32_t transit;
- unsigned long timer_tick;
- pj_time_val tv;
- int status;
-
- /* Update sequence numbers (received, lost, etc). */
- status = pj_rtp_seq_update(&s->seq_ctrl, seq);
- if (status == PJ_RTP_ERR_SESSION_RESTARTED) {
- rtcp_init_seq(s, seq);
- status = 0;
- }
-
- if (status != 0)
- return;
-
- ++s->received;
-
- pj_gettimeofday(&tv);
- timer_tick = tv.sec * 1000 + tv.msec;
-
- /*
- * Calculate jitter (s->jitter is in timer tick unit)
- */
- PJ_TODO(SUPPORT_JITTER_CALCULATION_FOR_NON_8KHZ_SAMPLE_RATE)
-
- arrival = timer_tick << 3; // 8 samples per ms.
- transit = arrival - rtp_ts;
-
- if (s->transit == 0) {
- s->transit = transit;
- } else {
- pj_int32_t d, jitter = s->jitter;
-
- d = transit - s->transit;
- s->transit = transit;
- if (d < 0)
- d = -d;
-
- jitter += d - ((jitter + 8) >> 4);
- s->jitter = jitter;
- }
-}
-
-PJ_DEF(void) pj_rtcp_tx_rtp(pj_rtcp_session *s, pj_uint16_t bytes_payload_size)
-{
- pj_rtcp_pkt *rtcp_pkt = &s->rtcp_pkt;
- rtcp_pkt->sr.sender_pcount = pj_htonl( pj_ntohl(rtcp_pkt->sr.sender_pcount) + 1);
- rtcp_pkt->sr.sender_bcount = pj_htonl( pj_ntohl(rtcp_pkt->sr.sender_bcount) + bytes_payload_size );
-}
-
-static void rtcp_build_rtcp(pj_rtcp_session *s, pj_uint32_t receiver_ssrc)
-{
- pj_uint32_t expected;
- pj_uint32_t u32;
- pj_uint32_t expected_interval, received_interval, lost_interval;
- pj_rtcp_pkt *rtcp_pkt = &s->rtcp_pkt;
-
- /* SSRC and last_seq */
- rtcp_pkt->rr.ssrc = pj_htonl(receiver_ssrc);
- rtcp_pkt->rr.last_seq = (s->seq_ctrl.cycles & 0xFFFF0000L);
- rtcp_pkt->rr.last_seq += s->seq_ctrl.max_seq;
- rtcp_pkt->rr.last_seq = pj_htonl(rtcp_pkt->rr.last_seq);
-
- /* Jitter */
- rtcp_pkt->rr.jitter = pj_htonl(s->jitter >> 4);
-
- /* Total lost. */
- expected = pj_ntohl(rtcp_pkt->rr.last_seq) - s->seq_ctrl.base_seq + 1;
- u32 = expected - s->received;
- rtcp_pkt->rr.total_lost_2 = (u32 >> 16) & 0x00FF;
- rtcp_pkt->rr.total_lost_1 = (u32 >> 8) & 0x00FF;
- rtcp_pkt->rr.total_lost_0 = u32 & 0x00FF;
-
- /* Fraction lost calculation */
- expected_interval = expected - s->expected_prior;
- s->expected_prior = expected;
-
- received_interval = s->received - s->received_prior;
- s->received_prior = s->received;
-
- lost_interval = expected_interval - received_interval;
-
- if (expected_interval==0 || lost_interval == 0) {
- rtcp_pkt->rr.fract_lost = 0;
- } else {
- rtcp_pkt->rr.fract_lost = (lost_interval << 8) / expected_interval;
- }
-}
-
-PJ_DEF(void) pj_rtcp_build_rtcp(pj_rtcp_session *session, pj_rtcp_pkt **ret_p_pkt, int *len)
-{
- pj_rtcp_pkt *rtcp_pkt = &session->rtcp_pkt;
- pj_rtcp_ntp_rec ntp;
- pj_time_val now;
-
- rtcp_build_rtcp(session, session->peer_ssrc);
-
- /* Get current NTP time. */
- rtcp_get_ntp_time(&ntp);
-
- /* Fill in NTP timestamp in SR. */
- rtcp_pkt->sr.ntp_sec = pj_htonl(ntp.hi);
- rtcp_pkt->sr.ntp_frac = pj_htonl(ntp.lo);
-
- if (session->rtcp_lsr_time == 0 || session->rtcp_lsr.lo == 0) {
- rtcp_pkt->rr.lsr = 0;
- rtcp_pkt->rr.dlsr = 0;
- } else {
- unsigned msec_elapsed;
-
- /* Fill in LSR.
- LSR is the middle 32bit of the last SR NTP time received.
- */
- rtcp_pkt->rr.lsr = ((session->rtcp_lsr.hi & 0x0000FFFF) << 16) |
- ((session->rtcp_lsr.lo >> 16) & 0xFFFF);
- rtcp_pkt->rr.lsr = pj_htonl(rtcp_pkt->rr.lsr);
-
- /* Fill in DLSR.
- DLSR is Delay since Last SR, in 1/65536 seconds.
- */
- pj_gettimeofday(&now);
- msec_elapsed = (now.msec - session->rtcp_lsr_time);
- rtcp_pkt->rr.dlsr = pj_htonl((msec_elapsed * 65536) / 1000);
- }
-
- /* Return pointer. */
- *ret_p_pkt = rtcp_pkt;
- *len = sizeof(pj_rtcp_pkt);
-}
-
+/* $Id$ */
+/*
+ * Copyright (C) 2003-2006 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 <pjmedia/rtcp.h>
+#include <pj/os.h> /* pj_gettimeofday */
+#include <pj/sock.h> /* pj_htonx, pj_ntohx */
+#include <string.h> /* memset */
+
+#define RTCP_SR 200
+#define RTCP_RR 201
+
+
+
+/*
+ * Get NTP time.
+ */
+static void rtcp_get_ntp_time(struct pj_rtcp_ntp_rec *ntp)
+{
+ pj_time_val tv;
+
+ pj_gettimeofday(&tv);
+
+ ntp->hi = tv.sec;
+ tv.msec = tv.msec % 1000;
+ ntp->lo = tv.msec * 0xFFFF / 1000;
+ ntp->lo <<= 16;
+}
+
+
+PJ_DEF(void) pj_rtcp_init(pj_rtcp_session *s, pj_uint32_t ssrc)
+{
+ pj_rtcp_pkt *rtcp_pkt = &s->rtcp_pkt;
+
+ memset(rtcp_pkt, 0, sizeof(pj_rtcp_pkt));
+
+ /* Init time */
+ s->rtcp_lsr.hi = s->rtcp_lsr.lo = 0;
+ s->rtcp_lsr_time = 0;
+
+ /* Init common RTCP header */
+ rtcp_pkt->common.version = 2;
+ rtcp_pkt->common.count = 1;
+ rtcp_pkt->common.pt = RTCP_SR;
+ rtcp_pkt->common.length = pj_htons(12);
+
+ /* Init SR */
+ rtcp_pkt->sr.ssrc = pj_htonl(ssrc);
+
+ /* RR will be initialized on receipt of the first RTP packet. */
+}
+
+PJ_DEF(void) pj_rtcp_fini(pj_rtcp_session *session)
+{
+ /* Nothing to do. */
+ PJ_UNUSED_ARG(session)
+}
+
+static void rtcp_init_seq(pj_rtcp_session *s, pj_uint16_t seq)
+{
+ s->received = 0;
+ s->expected_prior = 0;
+ s->received_prior = 0;
+ s->transit = 0;
+ s->jitter = 0;
+
+ pj_rtp_seq_restart(&s->seq_ctrl, seq);
+}
+
+PJ_DEF(void) pj_rtcp_rx_rtp(pj_rtcp_session *s, pj_uint16_t seq, pj_uint32_t rtp_ts)
+{
+ pj_uint32_t arrival;
+ pj_int32_t transit;
+ unsigned long timer_tick;
+ pj_time_val tv;
+ int status;
+
+ /* Update sequence numbers (received, lost, etc). */
+ status = pj_rtp_seq_update(&s->seq_ctrl, seq);
+ if (status == PJ_RTP_ERR_SESSION_RESTARTED) {
+ rtcp_init_seq(s, seq);
+ status = 0;
+ }
+
+ if (status != 0)
+ return;
+
+ ++s->received;
+
+ pj_gettimeofday(&tv);
+ timer_tick = tv.sec * 1000 + tv.msec;
+
+ /*
+ * Calculate jitter (s->jitter is in timer tick unit)
+ */
+ PJ_TODO(SUPPORT_JITTER_CALCULATION_FOR_NON_8KHZ_SAMPLE_RATE)
+
+ arrival = timer_tick << 3; // 8 samples per ms.
+ transit = arrival - rtp_ts;
+
+ if (s->transit == 0) {
+ s->transit = transit;
+ } else {
+ pj_int32_t d, jitter = s->jitter;
+
+ d = transit - s->transit;
+ s->transit = transit;
+ if (d < 0)
+ d = -d;
+
+ jitter += d - ((jitter + 8) >> 4);
+ s->jitter = jitter;
+ }
+}
+
+PJ_DEF(void) pj_rtcp_tx_rtp(pj_rtcp_session *s, pj_uint16_t bytes_payload_size)
+{
+ pj_rtcp_pkt *rtcp_pkt = &s->rtcp_pkt;
+ rtcp_pkt->sr.sender_pcount = pj_htonl( pj_ntohl(rtcp_pkt->sr.sender_pcount) + 1);
+ rtcp_pkt->sr.sender_bcount = pj_htonl( pj_ntohl(rtcp_pkt->sr.sender_bcount) + bytes_payload_size );
+}
+
+static void rtcp_build_rtcp(pj_rtcp_session *s, pj_uint32_t receiver_ssrc)
+{
+ pj_uint32_t expected;
+ pj_uint32_t u32;
+ pj_uint32_t expected_interval, received_interval, lost_interval;
+ pj_rtcp_pkt *rtcp_pkt = &s->rtcp_pkt;
+
+ /* SSRC and last_seq */
+ rtcp_pkt->rr.ssrc = pj_htonl(receiver_ssrc);
+ rtcp_pkt->rr.last_seq = (s->seq_ctrl.cycles & 0xFFFF0000L);
+ rtcp_pkt->rr.last_seq += s->seq_ctrl.max_seq;
+ rtcp_pkt->rr.last_seq = pj_htonl(rtcp_pkt->rr.last_seq);
+
+ /* Jitter */
+ rtcp_pkt->rr.jitter = pj_htonl(s->jitter >> 4);
+
+ /* Total lost. */
+ expected = pj_ntohl(rtcp_pkt->rr.last_seq) - s->seq_ctrl.base_seq + 1;
+ u32 = expected - s->received;
+ rtcp_pkt->rr.total_lost_2 = (u32 >> 16) & 0x00FF;
+ rtcp_pkt->rr.total_lost_1 = (u32 >> 8) & 0x00FF;
+ rtcp_pkt->rr.total_lost_0 = u32 & 0x00FF;
+
+ /* Fraction lost calculation */
+ expected_interval = expected - s->expected_prior;
+ s->expected_prior = expected;
+
+ received_interval = s->received - s->received_prior;
+ s->received_prior = s->received;
+
+ lost_interval = expected_interval - received_interval;
+
+ if (expected_interval==0 || lost_interval == 0) {
+ rtcp_pkt->rr.fract_lost = 0;
+ } else {
+ rtcp_pkt->rr.fract_lost = (lost_interval << 8) / expected_interval;
+ }
+}
+
+PJ_DEF(void) pj_rtcp_build_rtcp(pj_rtcp_session *session, pj_rtcp_pkt **ret_p_pkt, int *len)
+{
+ pj_rtcp_pkt *rtcp_pkt = &session->rtcp_pkt;
+ pj_rtcp_ntp_rec ntp;
+ pj_time_val now;
+
+ rtcp_build_rtcp(session, session->peer_ssrc);
+
+ /* Get current NTP time. */
+ rtcp_get_ntp_time(&ntp);
+
+ /* Fill in NTP timestamp in SR. */
+ rtcp_pkt->sr.ntp_sec = pj_htonl(ntp.hi);
+ rtcp_pkt->sr.ntp_frac = pj_htonl(ntp.lo);
+
+ if (session->rtcp_lsr_time == 0 || session->rtcp_lsr.lo == 0) {
+ rtcp_pkt->rr.lsr = 0;
+ rtcp_pkt->rr.dlsr = 0;
+ } else {
+ unsigned msec_elapsed;
+
+ /* Fill in LSR.
+ LSR is the middle 32bit of the last SR NTP time received.
+ */
+ rtcp_pkt->rr.lsr = ((session->rtcp_lsr.hi & 0x0000FFFF) << 16) |
+ ((session->rtcp_lsr.lo >> 16) & 0xFFFF);
+ rtcp_pkt->rr.lsr = pj_htonl(rtcp_pkt->rr.lsr);
+
+ /* Fill in DLSR.
+ DLSR is Delay since Last SR, in 1/65536 seconds.
+ */
+ pj_gettimeofday(&now);
+ msec_elapsed = (now.msec - session->rtcp_lsr_time);
+ rtcp_pkt->rr.dlsr = pj_htonl((msec_elapsed * 65536) / 1000);
+ }
+
+ /* Return pointer. */
+ *ret_p_pkt = rtcp_pkt;
+ *len = sizeof(pj_rtcp_pkt);
+}
+
diff --git a/pjmedia/src/pjmedia/rtcp.h b/pjmedia/src/pjmedia/rtcp.h
index b680c1bf..8a8f8909 100644
--- a/pjmedia/src/pjmedia/rtcp.h
+++ b/pjmedia/src/pjmedia/rtcp.h
@@ -1,192 +1,192 @@
-/* $Id$ */
-/*
- * Copyright (C) 2003-2006 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 __PJMEDIA_RTCP_H__
-#define __PJMEDIA_RTCP_H__
-
-/**
- * @file rtcp.h
- * @brief RTCP implementation.
- */
-
-#include <pj/types.h>
-#include <pjmedia/rtp.h>
-
-PJ_BEGIN_DECL
-
-
-/**
- * @defgroup PJMED_RTCP RTCP
- * @ingroup PJMEDIA
- * @{
- */
-
-/**
- * RTCP sender report.
- */
-struct pj_rtcp_sr
-{
- pj_uint32_t ssrc;
- pj_uint32_t ntp_sec;
- pj_uint32_t ntp_frac;
- pj_uint32_t rtp_ts;
- pj_uint32_t sender_pcount;
- pj_uint32_t sender_bcount;
-};
-
-typedef struct pj_rtcp_sr pj_rtcp_sr;
-
-/**
- * RTCP receiver report.
- */
-struct pj_rtcp_rr
-{
- pj_uint32_t ssrc;
-#if defined(PJ_IS_BIG_ENDIAN) && PJ_IS_BIG_ENDIAN!=0
- pj_uint32_t fract_lost:8;
- pj_uint32_t total_lost_2:8;
- pj_uint32_t total_lost_1:8;
- pj_uint32_t total_lost_0:8;
-#else
- pj_uint32_t fract_lost:8;
- pj_uint32_t total_lost_0:8;
- pj_uint32_t total_lost_1:8;
- pj_uint32_t total_lost_2:8;
-#endif
- pj_uint32_t last_seq;
- pj_uint32_t jitter;
- pj_uint32_t lsr;
- pj_uint32_t dlsr;
-};
-
-typedef struct pj_rtcp_rr pj_rtcp_rr;
-
-/**
- * RTCP common header.
- */
-struct pj_rtcp_common
-{
-#if defined(PJ_IS_BIG_ENDIAN) && PJ_IS_BIG_ENDIAN!=0
- unsigned version:2; /* packet type */
- unsigned p:1; /* padding flag */
- unsigned count:5; /* varies by payload type */
- unsigned pt:8; /* payload type */
-#else
- unsigned count:5; /* varies by payload type */
- unsigned p:1; /* padding flag */
- unsigned version:2; /* packet type */
- unsigned pt:8; /* payload type */
-#endif
- pj_uint16_t length; /* packet length */
-};
-
-typedef struct pj_rtcp_common pj_rtcp_common;
-
-/**
- * RTCP packet.
- */
-struct pj_rtcp_pkt
-{
- pj_rtcp_common common;
- pj_rtcp_sr sr;
- pj_rtcp_rr rr; /* variable-length list */
-};
-
-typedef struct pj_rtcp_pkt pj_rtcp_pkt;
-
-/**
- * NTP time representation.
- */
-struct pj_rtcp_ntp_rec
-{
- pj_uint32_t hi;
- pj_uint32_t lo;
-};
-
-typedef struct pj_rtcp_ntp_rec pj_rtcp_ntp_rec;
-
-/**
- * RTCP session.
- */
-struct pj_rtcp_session
-{
- pj_rtcp_pkt rtcp_pkt;
-
- pj_rtp_seq_session seq_ctrl;
-
- pj_uint32_t received; /* packets received */
- pj_uint32_t expected_prior; /* packet expected at last interval */
- pj_uint32_t received_prior; /* packet received at last interval */
- pj_int32_t transit; /* relative trans time for prev pkt */
- pj_uint32_t jitter; /* estimated jitter */
-
- pj_rtcp_ntp_rec rtcp_lsr; /* NTP timestamp in last sender report received */
- unsigned rtcp_lsr_time; /* Time when last RTCP SR is received. */
- unsigned peer_ssrc; /* Peer SSRC */
-
-};
-
-typedef struct pj_rtcp_session pj_rtcp_session;
-
-/**
- * Init RTCP session.
- * @param session The session
- * @param ssrc The SSRC used in to identify the session.
- */
-PJ_DECL(void) pj_rtcp_init( pj_rtcp_session *session, pj_uint32_t ssrc );
-
-/**
- * Deinit RTCP session.
- * @param session The session.
- */
-PJ_DECL(void) pj_rtcp_fini( pj_rtcp_session *session);
-
-/**
- * Call this function everytime an RTP packet is received to let the RTCP
- * session do its internal calculations.
- * @param session The session.
- * @param seq The RTP packet sequence number, in host byte order.
- * @param ts The RTP packet timestamp, in host byte order.
- */
-PJ_DECL(void) pj_rtcp_rx_rtp( pj_rtcp_session *session, pj_uint16_t seq, pj_uint32_t ts );
-
-/**
- * Call this function everytime an RTP packet is sent to let the RTCP session
- * do its internal calculations.
- * @param session The session.
- * @param bytes_payload_size The payload size of the RTP packet (ie packet minus
- * RTP header).
- */
-PJ_DECL(void) pj_rtcp_tx_rtp( pj_rtcp_session *session, pj_uint16_t bytes_payload_size );
-
-/**
- * Build a RTCP SR/RR packet to be transmitted to remote RTP peer.
- * @param session The session.
- * @param rtcp_pkt [output] Upon return, it will contain pointer to the RTCP packet.
- * @param len [output] Upon return, it will indicate the size of the RTCP packet.
- */
-PJ_DECL(void) pj_rtcp_build_rtcp( pj_rtcp_session *session, pj_rtcp_pkt **rtcp_pkt, int *len );
-
-/**
- * @}
- */
-
-PJ_END_DECL
-
-
-#endif /* __PJMEDIA_RTCP_H__ */
+/* $Id$ */
+/*
+ * Copyright (C) 2003-2006 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 __PJMEDIA_RTCP_H__
+#define __PJMEDIA_RTCP_H__
+
+/**
+ * @file rtcp.h
+ * @brief RTCP implementation.
+ */
+
+#include <pj/types.h>
+#include <pjmedia/rtp.h>
+
+PJ_BEGIN_DECL
+
+
+/**
+ * @defgroup PJMED_RTCP RTCP
+ * @ingroup PJMEDIA
+ * @{
+ */
+
+/**
+ * RTCP sender report.
+ */
+struct pj_rtcp_sr
+{
+ pj_uint32_t ssrc;
+ pj_uint32_t ntp_sec;
+ pj_uint32_t ntp_frac;
+ pj_uint32_t rtp_ts;
+ pj_uint32_t sender_pcount;
+ pj_uint32_t sender_bcount;
+};
+
+typedef struct pj_rtcp_sr pj_rtcp_sr;
+
+/**
+ * RTCP receiver report.
+ */
+struct pj_rtcp_rr
+{
+ pj_uint32_t ssrc;
+#if defined(PJ_IS_BIG_ENDIAN) && PJ_IS_BIG_ENDIAN!=0
+ pj_uint32_t fract_lost:8;
+ pj_uint32_t total_lost_2:8;
+ pj_uint32_t total_lost_1:8;
+ pj_uint32_t total_lost_0:8;
+#else
+ pj_uint32_t fract_lost:8;
+ pj_uint32_t total_lost_0:8;
+ pj_uint32_t total_lost_1:8;
+ pj_uint32_t total_lost_2:8;
+#endif
+ pj_uint32_t last_seq;
+ pj_uint32_t jitter;
+ pj_uint32_t lsr;
+ pj_uint32_t dlsr;
+};
+
+typedef struct pj_rtcp_rr pj_rtcp_rr;
+
+/**
+ * RTCP common header.
+ */
+struct pj_rtcp_common
+{
+#if defined(PJ_IS_BIG_ENDIAN) && PJ_IS_BIG_ENDIAN!=0
+ unsigned version:2; /* packet type */
+ unsigned p:1; /* padding flag */
+ unsigned count:5; /* varies by payload type */
+ unsigned pt:8; /* payload type */
+#else
+ unsigned count:5; /* varies by payload type */
+ unsigned p:1; /* padding flag */
+ unsigned version:2; /* packet type */
+ unsigned pt:8; /* payload type */
+#endif
+ pj_uint16_t length; /* packet length */
+};
+
+typedef struct pj_rtcp_common pj_rtcp_common;
+
+/**
+ * RTCP packet.
+ */
+struct pj_rtcp_pkt
+{
+ pj_rtcp_common common;
+ pj_rtcp_sr sr;
+ pj_rtcp_rr rr; /* variable-length list */
+};
+
+typedef struct pj_rtcp_pkt pj_rtcp_pkt;
+
+/**
+ * NTP time representation.
+ */
+struct pj_rtcp_ntp_rec
+{
+ pj_uint32_t hi;
+ pj_uint32_t lo;
+};
+
+typedef struct pj_rtcp_ntp_rec pj_rtcp_ntp_rec;
+
+/**
+ * RTCP session.
+ */
+struct pj_rtcp_session
+{
+ pj_rtcp_pkt rtcp_pkt;
+
+ pj_rtp_seq_session seq_ctrl;
+
+ pj_uint32_t received; /* packets received */
+ pj_uint32_t expected_prior; /* packet expected at last interval */
+ pj_uint32_t received_prior; /* packet received at last interval */
+ pj_int32_t transit; /* relative trans time for prev pkt */
+ pj_uint32_t jitter; /* estimated jitter */
+
+ pj_rtcp_ntp_rec rtcp_lsr; /* NTP timestamp in last sender report received */
+ unsigned rtcp_lsr_time; /* Time when last RTCP SR is received. */
+ unsigned peer_ssrc; /* Peer SSRC */
+
+};
+
+typedef struct pj_rtcp_session pj_rtcp_session;
+
+/**
+ * Init RTCP session.
+ * @param session The session
+ * @param ssrc The SSRC used in to identify the session.
+ */
+PJ_DECL(void) pj_rtcp_init( pj_rtcp_session *session, pj_uint32_t ssrc );
+
+/**
+ * Deinit RTCP session.
+ * @param session The session.
+ */
+PJ_DECL(void) pj_rtcp_fini( pj_rtcp_session *session);
+
+/**
+ * Call this function everytime an RTP packet is received to let the RTCP
+ * session do its internal calculations.
+ * @param session The session.
+ * @param seq The RTP packet sequence number, in host byte order.
+ * @param ts The RTP packet timestamp, in host byte order.
+ */
+PJ_DECL(void) pj_rtcp_rx_rtp( pj_rtcp_session *session, pj_uint16_t seq, pj_uint32_t ts );
+
+/**
+ * Call this function everytime an RTP packet is sent to let the RTCP session
+ * do its internal calculations.
+ * @param session The session.
+ * @param bytes_payload_size The payload size of the RTP packet (ie packet minus
+ * RTP header).
+ */
+PJ_DECL(void) pj_rtcp_tx_rtp( pj_rtcp_session *session, pj_uint16_t bytes_payload_size );
+
+/**
+ * Build a RTCP SR/RR packet to be transmitted to remote RTP peer.
+ * @param session The session.
+ * @param rtcp_pkt [output] Upon return, it will contain pointer to the RTCP packet.
+ * @param len [output] Upon return, it will indicate the size of the RTCP packet.
+ */
+PJ_DECL(void) pj_rtcp_build_rtcp( pj_rtcp_session *session, pj_rtcp_pkt **rtcp_pkt, int *len );
+
+/**
+ * @}
+ */
+
+PJ_END_DECL
+
+
+#endif /* __PJMEDIA_RTCP_H__ */
diff --git a/pjmedia/src/pjmedia/rtp.c b/pjmedia/src/pjmedia/rtp.c
index ac62a1a4..5aadd7c1 100644
--- a/pjmedia/src/pjmedia/rtp.c
+++ b/pjmedia/src/pjmedia/rtp.c
@@ -1,261 +1,261 @@
-/* $Id$ */
-/*
- * Copyright (C) 2003-2006 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 <pjmedia/rtp.h>
-#include <pj/log.h>
-#include <pj/os.h> /* pj_gettimeofday() */
-#include <pj/sock.h> /* pj_htonx, pj_htonx */
-#include <string.h> /* memset() */
-
-#define THIS_FILE "rtp.c"
-
-#define RTP_VERSION 2
-
-#define RTP_SEQ_MOD (1 << 16)
-#define MAX_DROPOUT ((pj_int16_t)3000)
-#define MAX_MISORDER ((pj_int16_t)100)
-#define MIN_SEQUENTIAL ((pj_int16_t)2)
-
-
-PJ_DEF(pj_status_t) pj_rtp_session_init( pj_rtp_session *ses,
- int default_pt, pj_uint32_t sender_ssrc )
-{
- PJ_LOG(4, (THIS_FILE, "pj_rtp_session_init: ses=%p, default_pt=%d, ssrc=0x%x",
- ses, default_pt, sender_ssrc));
-
- /* Check RTP header packing. */
- if (sizeof(struct pj_rtp_hdr) != 12) {
- pj_assert(!"Wrong RTP header packing!");
- return PJ_RTP_ERR_RTP_PACKING;
- }
-
- /* If sender_ssrc is not specified, create from time value. */
- if (sender_ssrc == 0 || sender_ssrc == (pj_uint32_t)-1) {
- pj_time_val tv;
-
- pj_gettimeofday(&tv);
- sender_ssrc = (pj_uint32_t) pj_htonl(tv.sec);
- } else {
- sender_ssrc = pj_htonl(sender_ssrc);
- }
-
- /* Initialize session. */
- ses->out_extseq = 0;
- ses->peer_ssrc = 0;
-
- /* Sequence number will be initialized when the first RTP packet is receieved. */
-
- /* Build default header for outgoing RTP packet. */
- memset(ses, 0, sizeof(*ses));
- ses->out_hdr.v = RTP_VERSION;
- ses->out_hdr.p = 0;
- ses->out_hdr.x = 0;
- ses->out_hdr.cc = 0;
- ses->out_hdr.m = 0;
- ses->out_hdr.pt = (pj_uint8_t) default_pt;
- ses->out_hdr.seq = (pj_uint16_t) pj_htons( (pj_uint16_t)ses->out_extseq );
- ses->out_hdr.ts = 0;
- ses->out_hdr.ssrc = sender_ssrc;
-
- /* Keep some arguments as session defaults. */
- ses->out_pt = (pj_uint16_t) default_pt;
-
- return PJ_SUCCESS;
-}
-
-
-PJ_DEF(pj_status_t) pj_rtp_encode_rtp( pj_rtp_session *ses, int pt, int m,
- int payload_len, int ts_len,
- const void **rtphdr, int *hdrlen )
-{
- PJ_UNUSED_ARG(payload_len)
-
- PJ_LOG(6, (THIS_FILE,
- "pj_rtp_encode_rtp: ses=%p, pt=%d, m=%d, pt_len=%d, ts_len=%d",
- ses, pt, m, payload_len, ts_len));
-
- /* Update session. */
- ses->out_extseq++;
- ses->out_hdr.ts = pj_htonl(pj_ntohl(ses->out_hdr.ts)+ts_len);
-
- /* Create outgoing header. */
- ses->out_hdr.pt = (pj_uint8_t) ((pt == -1) ? ses->out_pt : pt);
- ses->out_hdr.m = (pj_uint16_t) m;
- ses->out_hdr.seq = pj_htons( (pj_uint16_t) ses->out_extseq);
-
- /* Return values */
- *rtphdr = &ses->out_hdr;
- *hdrlen = sizeof(pj_rtp_hdr);
-
- return PJ_SUCCESS;
-}
-
-
-PJ_DEF(pj_status_t) pj_rtp_decode_rtp( pj_rtp_session *ses,
- const void *pkt, int pkt_len,
- const pj_rtp_hdr **hdr,
- const void **payload,
- unsigned *payloadlen)
-{
- int offset;
-
- PJ_UNUSED_ARG(ses)
-
- PJ_LOG(6, (THIS_FILE,
- "pj_rtp_decode_rtp: ses=%p, pkt=%p, pkt_len=%d",
- ses, pkt, pkt_len));
-
- /* Assume RTP header at the start of packet. We'll verify this later. */
- *hdr = (pj_rtp_hdr*)pkt;
-
- /* Check RTP header sanity. */
- if ((*hdr)->v != RTP_VERSION) {
- PJ_LOG(4, (THIS_FILE, " invalid RTP version!"));
- return PJ_RTP_ERR_INVALID_VERSION;
- }
-
- /* Payload is located right after header plus CSRC */
- offset = sizeof(pj_rtp_hdr) + ((*hdr)->cc * sizeof(pj_uint32_t));
-
- /* Adjust offset if RTP extension is used. */
- if ((*hdr)->x) {
- pj_rtp_ext_hdr *ext = (pj_rtp_ext_hdr*) (((pj_uint8_t*)pkt) + offset);
- offset += (pj_ntohs(ext->length) * sizeof(pj_uint32_t));
- }
-
- /* Check that offset is less than packet size */
- if (offset >= pkt_len)
- return PJ_RTP_ERR_INVALID_PACKET;
-
- /* Find and set payload. */
- *payload = ((pj_uint8_t*)pkt) + offset;
- *payloadlen = pkt_len - offset;
-
- return PJ_SUCCESS;
-}
-
-
-PJ_DEF(pj_status_t) pj_rtp_session_update( pj_rtp_session *ses, const pj_rtp_hdr *hdr)
-{
- int status;
-
- /* Check SSRC. */
- if (ses->peer_ssrc == 0) ses->peer_ssrc = pj_ntohl(hdr->ssrc);
- /*
- if (pj_ntohl(ses->peer_ssrc) != hdr->ssrc) {
- PJ_LOG(4, (THIS_FILE, "pj_rtp_session_update: ses=%p, invalid ssrc 0x%p (!=0x%p)",
- ses, pj_ntohl(hdr->ssrc), ses->peer_ssrc));
- return PJ_RTP_ERR_INVALID_SSRC;
- }
- */
-
- /* Check payload type. */
- if (hdr->pt != ses->out_pt) {
- PJ_LOG(4, (THIS_FILE, "pj_rtp_session_update: ses=%p, invalid payload type %d (!=%d)",
- ses, hdr->pt, ses->out_pt));
- return PJ_RTP_ERR_INVALID_PT;
- }
-
- /* Initialize sequence number on first packet received. */
- if (ses->received == 0)
- pj_rtp_seq_init( &ses->seq_ctrl, pj_ntohs(hdr->seq) );
-
- /* Check sequence number to see if remote session has been restarted. */
- status = pj_rtp_seq_update( &ses->seq_ctrl, pj_ntohs(hdr->seq));
- if (status == PJ_RTP_ERR_SESSION_RESTARTED) {
- pj_rtp_seq_restart( &ses->seq_ctrl, pj_ntohs(hdr->seq));
- ++ses->received;
- } else if (status == 0 || status == PJ_RTP_ERR_SESSION_PROBATION) {
- ++ses->received;
- }
-
-
- return status;
-}
-
-
-void pj_rtp_seq_restart(pj_rtp_seq_session *sctrl, pj_uint16_t seq)
-{
- sctrl->base_seq = seq;
- sctrl->max_seq = seq;
- sctrl->bad_seq = RTP_SEQ_MOD + 1;
- sctrl->cycles = 0;
-}
-
-
-void pj_rtp_seq_init(pj_rtp_seq_session *sctrl, pj_uint16_t seq)
-{
- pj_rtp_seq_restart(sctrl, seq);
-
- sctrl->max_seq = (pj_uint16_t) (seq - 1);
- sctrl->probation = MIN_SEQUENTIAL;
-}
-
-
-int pj_rtp_seq_update(pj_rtp_seq_session *sctrl, pj_uint16_t seq)
-{
- pj_uint16_t udelta = (pj_uint16_t) (seq - sctrl->max_seq);
-
- /*
- * Source is not valid until MIN_SEQUENTIAL packets with
- * sequential sequence numbers have been received.
- */
- if (sctrl->probation) {
- /* packet is in sequence */
- if (seq == sctrl->max_seq+ 1) {
- sctrl->probation--;
- sctrl->max_seq = seq;
- if (sctrl->probation == 0) {
- return PJ_RTP_ERR_SESSION_RESTARTED;
- }
- } else {
- sctrl->probation = MIN_SEQUENTIAL - 1;
- sctrl->max_seq = seq;
- }
- return PJ_RTP_ERR_SESSION_PROBATION;
-
- } else if (udelta < MAX_DROPOUT) {
- /* in order, with permissible gap */
- if (seq < sctrl->max_seq) {
- /* Sequence number wrapped - count another 64K cycle. */
- sctrl->cycles += RTP_SEQ_MOD;
- }
- sctrl->max_seq = seq;
-
- } else if (udelta <= (RTP_SEQ_MOD - MAX_MISORDER)) {
- /* the sequence number made a very large jump */
- if (seq == sctrl->bad_seq) {
- /*
- * Two sequential packets -- assume that the other side
- * restarted without telling us so just re-sync
- * (i.e., pretend this was the first packet).
- */
- return PJ_RTP_ERR_SESSION_RESTARTED;
- }
- else {
- sctrl->bad_seq = (seq + 1) & (RTP_SEQ_MOD-1);
- return PJ_RTP_ERR_BAD_SEQUENCE;
- }
- } else {
- /* duplicate or reordered packet */
- }
-
- return 0;
-}
-
-
+/* $Id$ */
+/*
+ * Copyright (C) 2003-2006 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 <pjmedia/rtp.h>
+#include <pj/log.h>
+#include <pj/os.h> /* pj_gettimeofday() */
+#include <pj/sock.h> /* pj_htonx, pj_htonx */
+#include <string.h> /* memset() */
+
+#define THIS_FILE "rtp.c"
+
+#define RTP_VERSION 2
+
+#define RTP_SEQ_MOD (1 << 16)
+#define MAX_DROPOUT ((pj_int16_t)3000)
+#define MAX_MISORDER ((pj_int16_t)100)
+#define MIN_SEQUENTIAL ((pj_int16_t)2)
+
+
+PJ_DEF(pj_status_t) pj_rtp_session_init( pj_rtp_session *ses,
+ int default_pt, pj_uint32_t sender_ssrc )
+{
+ PJ_LOG(4, (THIS_FILE, "pj_rtp_session_init: ses=%p, default_pt=%d, ssrc=0x%x",
+ ses, default_pt, sender_ssrc));
+
+ /* Check RTP header packing. */
+ if (sizeof(struct pj_rtp_hdr) != 12) {
+ pj_assert(!"Wrong RTP header packing!");
+ return PJ_RTP_ERR_RTP_PACKING;
+ }
+
+ /* If sender_ssrc is not specified, create from time value. */
+ if (sender_ssrc == 0 || sender_ssrc == (pj_uint32_t)-1) {
+ pj_time_val tv;
+
+ pj_gettimeofday(&tv);
+ sender_ssrc = (pj_uint32_t) pj_htonl(tv.sec);
+ } else {
+ sender_ssrc = pj_htonl(sender_ssrc);
+ }
+
+ /* Initialize session. */
+ ses->out_extseq = 0;
+ ses->peer_ssrc = 0;
+
+ /* Sequence number will be initialized when the first RTP packet is receieved. */
+
+ /* Build default header for outgoing RTP packet. */
+ memset(ses, 0, sizeof(*ses));
+ ses->out_hdr.v = RTP_VERSION;
+ ses->out_hdr.p = 0;
+ ses->out_hdr.x = 0;
+ ses->out_hdr.cc = 0;
+ ses->out_hdr.m = 0;
+ ses->out_hdr.pt = (pj_uint8_t) default_pt;
+ ses->out_hdr.seq = (pj_uint16_t) pj_htons( (pj_uint16_t)ses->out_extseq );
+ ses->out_hdr.ts = 0;
+ ses->out_hdr.ssrc = sender_ssrc;
+
+ /* Keep some arguments as session defaults. */
+ ses->out_pt = (pj_uint16_t) default_pt;
+
+ return PJ_SUCCESS;
+}
+
+
+PJ_DEF(pj_status_t) pj_rtp_encode_rtp( pj_rtp_session *ses, int pt, int m,
+ int payload_len, int ts_len,
+ const void **rtphdr, int *hdrlen )
+{
+ PJ_UNUSED_ARG(payload_len)
+
+ PJ_LOG(6, (THIS_FILE,
+ "pj_rtp_encode_rtp: ses=%p, pt=%d, m=%d, pt_len=%d, ts_len=%d",
+ ses, pt, m, payload_len, ts_len));
+
+ /* Update session. */
+ ses->out_extseq++;
+ ses->out_hdr.ts = pj_htonl(pj_ntohl(ses->out_hdr.ts)+ts_len);
+
+ /* Create outgoing header. */
+ ses->out_hdr.pt = (pj_uint8_t) ((pt == -1) ? ses->out_pt : pt);
+ ses->out_hdr.m = (pj_uint16_t) m;
+ ses->out_hdr.seq = pj_htons( (pj_uint16_t) ses->out_extseq);
+
+ /* Return values */
+ *rtphdr = &ses->out_hdr;
+ *hdrlen = sizeof(pj_rtp_hdr);
+
+ return PJ_SUCCESS;
+}
+
+
+PJ_DEF(pj_status_t) pj_rtp_decode_rtp( pj_rtp_session *ses,
+ const void *pkt, int pkt_len,
+ const pj_rtp_hdr **hdr,
+ const void **payload,
+ unsigned *payloadlen)
+{
+ int offset;
+
+ PJ_UNUSED_ARG(ses)
+
+ PJ_LOG(6, (THIS_FILE,
+ "pj_rtp_decode_rtp: ses=%p, pkt=%p, pkt_len=%d",
+ ses, pkt, pkt_len));
+
+ /* Assume RTP header at the start of packet. We'll verify this later. */
+ *hdr = (pj_rtp_hdr*)pkt;
+
+ /* Check RTP header sanity. */
+ if ((*hdr)->v != RTP_VERSION) {
+ PJ_LOG(4, (THIS_FILE, " invalid RTP version!"));
+ return PJ_RTP_ERR_INVALID_VERSION;
+ }
+
+ /* Payload is located right after header plus CSRC */
+ offset = sizeof(pj_rtp_hdr) + ((*hdr)->cc * sizeof(pj_uint32_t));
+
+ /* Adjust offset if RTP extension is used. */
+ if ((*hdr)->x) {
+ pj_rtp_ext_hdr *ext = (pj_rtp_ext_hdr*) (((pj_uint8_t*)pkt) + offset);
+ offset += (pj_ntohs(ext->length) * sizeof(pj_uint32_t));
+ }
+
+ /* Check that offset is less than packet size */
+ if (offset >= pkt_len)
+ return PJ_RTP_ERR_INVALID_PACKET;
+
+ /* Find and set payload. */
+ *payload = ((pj_uint8_t*)pkt) + offset;
+ *payloadlen = pkt_len - offset;
+
+ return PJ_SUCCESS;
+}
+
+
+PJ_DEF(pj_status_t) pj_rtp_session_update( pj_rtp_session *ses, const pj_rtp_hdr *hdr)
+{
+ int status;
+
+ /* Check SSRC. */
+ if (ses->peer_ssrc == 0) ses->peer_ssrc = pj_ntohl(hdr->ssrc);
+ /*
+ if (pj_ntohl(ses->peer_ssrc) != hdr->ssrc) {
+ PJ_LOG(4, (THIS_FILE, "pj_rtp_session_update: ses=%p, invalid ssrc 0x%p (!=0x%p)",
+ ses, pj_ntohl(hdr->ssrc), ses->peer_ssrc));
+ return PJ_RTP_ERR_INVALID_SSRC;
+ }
+ */
+
+ /* Check payload type. */
+ if (hdr->pt != ses->out_pt) {
+ PJ_LOG(4, (THIS_FILE, "pj_rtp_session_update: ses=%p, invalid payload type %d (!=%d)",
+ ses, hdr->pt, ses->out_pt));
+ return PJ_RTP_ERR_INVALID_PT;
+ }
+
+ /* Initialize sequence number on first packet received. */
+ if (ses->received == 0)
+ pj_rtp_seq_init( &ses->seq_ctrl, pj_ntohs(hdr->seq) );
+
+ /* Check sequence number to see if remote session has been restarted. */
+ status = pj_rtp_seq_update( &ses->seq_ctrl, pj_ntohs(hdr->seq));
+ if (status == PJ_RTP_ERR_SESSION_RESTARTED) {
+ pj_rtp_seq_restart( &ses->seq_ctrl, pj_ntohs(hdr->seq));
+ ++ses->received;
+ } else if (status == 0 || status == PJ_RTP_ERR_SESSION_PROBATION) {
+ ++ses->received;
+ }
+
+
+ return status;
+}
+
+
+void pj_rtp_seq_restart(pj_rtp_seq_session *sctrl, pj_uint16_t seq)
+{
+ sctrl->base_seq = seq;
+ sctrl->max_seq = seq;
+ sctrl->bad_seq = RTP_SEQ_MOD + 1;
+ sctrl->cycles = 0;
+}
+
+
+void pj_rtp_seq_init(pj_rtp_seq_session *sctrl, pj_uint16_t seq)
+{
+ pj_rtp_seq_restart(sctrl, seq);
+
+ sctrl->max_seq = (pj_uint16_t) (seq - 1);
+ sctrl->probation = MIN_SEQUENTIAL;
+}
+
+
+int pj_rtp_seq_update(pj_rtp_seq_session *sctrl, pj_uint16_t seq)
+{
+ pj_uint16_t udelta = (pj_uint16_t) (seq - sctrl->max_seq);
+
+ /*
+ * Source is not valid until MIN_SEQUENTIAL packets with
+ * sequential sequence numbers have been received.
+ */
+ if (sctrl->probation) {
+ /* packet is in sequence */
+ if (seq == sctrl->max_seq+ 1) {
+ sctrl->probation--;
+ sctrl->max_seq = seq;
+ if (sctrl->probation == 0) {
+ return PJ_RTP_ERR_SESSION_RESTARTED;
+ }
+ } else {
+ sctrl->probation = MIN_SEQUENTIAL - 1;
+ sctrl->max_seq = seq;
+ }
+ return PJ_RTP_ERR_SESSION_PROBATION;
+
+ } else if (udelta < MAX_DROPOUT) {
+ /* in order, with permissible gap */
+ if (seq < sctrl->max_seq) {
+ /* Sequence number wrapped - count another 64K cycle. */
+ sctrl->cycles += RTP_SEQ_MOD;
+ }
+ sctrl->max_seq = seq;
+
+ } else if (udelta <= (RTP_SEQ_MOD - MAX_MISORDER)) {
+ /* the sequence number made a very large jump */
+ if (seq == sctrl->bad_seq) {
+ /*
+ * Two sequential packets -- assume that the other side
+ * restarted without telling us so just re-sync
+ * (i.e., pretend this was the first packet).
+ */
+ return PJ_RTP_ERR_SESSION_RESTARTED;
+ }
+ else {
+ sctrl->bad_seq = (seq + 1) & (RTP_SEQ_MOD-1);
+ return PJ_RTP_ERR_BAD_SEQUENCE;
+ }
+ } else {
+ /* duplicate or reordered packet */
+ }
+
+ return 0;
+}
+
+
diff --git a/pjmedia/src/pjmedia/rtp.h b/pjmedia/src/pjmedia/rtp.h
index 2d2832ad..54c1e32e 100644
--- a/pjmedia/src/pjmedia/rtp.h
+++ b/pjmedia/src/pjmedia/rtp.h
@@ -1,256 +1,256 @@
-/* $Id$ */
-/*
- * Copyright (C) 2003-2006 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 __PJMEDIA_RTP_H__
-#define __PJMEDIA_RTP_H__
-
-#include <pj/types.h>
-
-/**
- * @file rtp.h
- * @brief RTP implementation.
- */
-
-PJ_BEGIN_DECL
-
-
-/**
- * @defgroup PJMED_RTP RTP
- * @ingroup PJMEDIA
- * @{
- *
- * The RTP module is designed to be dependent only to PJLIB, it does not depend
- * on any other parts of PJMEDIA library. The RTP module does not even depend
- * on any transports (sockets), to promote even more use.
- *
- * An RTCP implementation is also separated from this module.
- *
- * The functions that are provided by this module:
- * - creating RTP header for each outgoing packet.
- * - decoding RTP packet into RTP header and payload.
- * - provide simple RTP session management (sequence number, etc.)
- *
- * The RTP module does not use any dynamic memory at all.
- *
- * \section P1 How to Use the RTP Module
- *
- * First application must call #pj_rtp_session_init to initialize the RTP
- * session.
- *
- * When application wants to send RTP packet, it needs to call
- * #pj_rtp_encode_rtp to build the RTP header. Note that this WILL NOT build
- * the complete RTP packet, but instead only the header. Application can
- * then either concatenate the header with the payload, or send the two
- * fragments (the header and the payload) using scatter-gather transport API
- * (e.g. \a sendv()).
- *
- * When application receives an RTP packet, first it should call
- * #pj_rtp_decode_rtp to decode RTP header and payload, then it should call
- * #pj_rtp_session_update to check whether we can process the RTP payload,
- * and to let the RTP session updates its internal status. The decode function
- * is guaranteed to point the payload to the correct position regardless of
- * any options present in the RTP packet.
- *
- */
-
-
-#ifdef _MSC_VER
-# pragma warning ( disable : 4214 )
-#endif
-
-
-/**
- * Error codes.
- */
-enum pj_rtp_error_t
-{
- PJ_RTP_ERR_RTP_PACKING, /**< Invalid RTP packet. */
- PJ_RTP_ERR_INVALID_VERSION, /**< Invalid RTP version. */
- PJ_RTP_ERR_INVALID_SSRC, /**< Invalid SSRC. */
- PJ_RTP_ERR_INVALID_PT, /**< Invalid payload type. */
- PJ_RTP_ERR_INVALID_PACKET, /**< Invalid packet. */
- PJ_RTP_ERR_SESSION_RESTARTED, /**< Session has just been restarted. */
- PJ_RTP_ERR_SESSION_PROBATION, /**< Session in probation. */
- PJ_RTP_ERR_BAD_SEQUENCE, /**< Bad RTP sequence number. */
-};
-
-#pragma pack(1)
-/**
- * RTP packet header.
- */
-struct pj_rtp_hdr
-{
-#if defined(PJ_IS_BIG_ENDIAN) && (PJ_IS_BIG_ENDIAN!=0)
- pj_uint16_t v:2; /**< packet type/version */
- pj_uint16_t p:1; /**< padding flag */
- pj_uint16_t x:1; /**< extension flag */
- pj_uint16_t cc:4; /**< CSRC count */
- pj_uint16_t m:1; /**< marker bit */
- pj_uint16_t pt:7; /**< payload type */
-#else
- pj_uint16_t cc:4; /**< CSRC count */
- pj_uint16_t x:1; /**< header extension flag */
- pj_uint16_t p:1; /**< padding flag */
- pj_uint16_t v:2; /**< packet type/version */
- pj_uint16_t pt:7; /**< payload type */
- pj_uint16_t m:1; /**< marker bit */
-#endif
- pj_uint16_t seq; /**< sequence number */
- pj_uint32_t ts; /**< timestamp */
- pj_uint32_t ssrc; /**< synchronization source */
-};
-#pragma pack()
-
-typedef struct pj_rtp_hdr pj_rtp_hdr;
-
-/**
- * RTP extendsion header.
- */
-struct pj_rtp_ext_hdr
-{
- pj_uint16_t profile_data;
- pj_uint16_t length;
-};
-
-typedef struct pj_rtp_ext_hdr pj_rtp_ext_hdr;
-
-/**
- * A generic sequence number management, used by both RTP and RTCP.
- */
-struct pj_rtp_seq_session
-{
- pj_uint16_t max_seq; /**< highest sequence number heard */
- pj_uint32_t cycles; /**< shifted count of seq. number cycles */
- pj_uint32_t base_seq; /**< base seq number */
- pj_uint32_t bad_seq; /**< last 'bad' seq number + 1 */
- pj_uint32_t probation; /**< sequ. packets till source is valid */
-};
-
-typedef struct pj_rtp_seq_session pj_rtp_seq_session;
-
-/**
- * RTP session descriptor.
- */
-struct pj_rtp_session
-{
- pj_rtp_hdr out_hdr; /**< Saved header for outgoing packets. */
- pj_rtp_seq_session seq_ctrl; /**< Sequence number management. */
- pj_uint16_t out_pt; /**< Default outgoing payload type. */
- pj_uint32_t out_extseq; /**< Outgoing extended sequence number. */
- pj_uint32_t peer_ssrc; /**< Peer SSRC. */
- pj_uint32_t received; /**< Number of received packets. */
-};
-
-typedef struct pj_rtp_session pj_rtp_session;
-
-/**
- * \brief Initialize RTP session.
- * This function will initialize the RTP session according to given parameters.
- *
- * @param ses The session.
- * @param default_pt Default payload type.
- * @param sender_ssrc SSRC used for outgoing packets.
- *
- * @return zero if successfull.
- */
-PJ_DECL(pj_status_t) pj_rtp_session_init( pj_rtp_session *ses,
- int default_pt, pj_uint32_t sender_ssrc );
-
-/**
- * \brief Encode outgoing RTP packet header.
- * Create the RTP header based on arguments and current state of the RTP
- * session.
- *
- * @param ses The session.
- * @param pt Payload type.
- * @param m Marker flag.
- * @param payload_len Payload length in bytes.
- * @param ts_len Timestamp length.
- * @param rtphdr Upon return will point to RTP packet header.
- * @param hdrlen Upon return will indicate the size of RTP packet header
- *
- * @return zero if successfull.
- */
-PJ_DECL(pj_status_t) pj_rtp_encode_rtp( pj_rtp_session *ses, int pt, int m,
- int payload_len, int ts_len,
- const void **rtphdr, int *hdrlen );
-
-/**
- * \brief Decode an incoming RTP packet.
- * This function will decode incoming packet into RTP header and payload.
- * The decode function is guaranteed to point the payload to the correct
- * position regardless of any options present in the RTP packet.
- *
- * @param ses The session.
- * @param pkt The received RTP packet.
- * @param pkt_len The length of the packet.
- * @param hdr Upon return will point to the location of the RTP header
- * inside the packet.
- * @param payload Upon return will point to the location of the
- * payload inside the packet.
- * @param payloadlen Upon return will indicate the size of the payload.
- *
- * @return zero if successfull.
- */
-PJ_DECL(pj_status_t) pj_rtp_decode_rtp( pj_rtp_session *ses,
- const void *pkt, int pkt_len,
- const pj_rtp_hdr **hdr,
- const void **payload,
- unsigned *payloadlen);
-
-/**
- * \brief Update RTP session with an incoming RTP packet.
- * Call this function everytime
- * an RTP packet is received to check whether the packet can be received and to
- * let the RTP session performs its internal calculations.
- *
- * @param ses The session.
- * @param hdr The RTP header of the incoming packet.
- *
- * @return zero if the packet is valid and can be processed, otherwise will
- * return one of the error in #pj_rtp_error_t.
- */
-PJ_DECL(pj_status_t) pj_rtp_session_update( pj_rtp_session *ses,
- const pj_rtp_hdr *hdr);
-
-/**
-* \brief Internal.
- * Internal function for sequence control, shared by RTCP implementation.
- */
-void pj_rtp_seq_init(pj_rtp_seq_session *seq_ctrl, pj_uint16_t seq);
-
-/**
-* \brief Internal.
- * Internal function for sequence control, shared by RTCP implementation.
- */
-void pj_rtp_seq_restart(pj_rtp_seq_session *seq_ctrl, pj_uint16_t seq);
-
-/**
-* \brief Internal.
- * Internal function for sequence control, shared by RTCP implementation.
- */
-int pj_rtp_seq_update(pj_rtp_seq_session *seq_ctrl, pj_uint16_t seq);
-
-/**
- * @}
- */
-
-PJ_END_DECL
-
-
-#endif /* __PJMEDIA_RTP_H__ */
+/* $Id$ */
+/*
+ * Copyright (C) 2003-2006 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 __PJMEDIA_RTP_H__
+#define __PJMEDIA_RTP_H__
+
+#include <pj/types.h>
+
+/**
+ * @file rtp.h
+ * @brief RTP implementation.
+ */
+
+PJ_BEGIN_DECL
+
+
+/**
+ * @defgroup PJMED_RTP RTP
+ * @ingroup PJMEDIA
+ * @{
+ *
+ * The RTP module is designed to be dependent only to PJLIB, it does not depend
+ * on any other parts of PJMEDIA library. The RTP module does not even depend
+ * on any transports (sockets), to promote even more use.
+ *
+ * An RTCP implementation is also separated from this module.
+ *
+ * The functions that are provided by this module:
+ * - creating RTP header for each outgoing packet.
+ * - decoding RTP packet into RTP header and payload.
+ * - provide simple RTP session management (sequence number, etc.)
+ *
+ * The RTP module does not use any dynamic memory at all.
+ *
+ * \section P1 How to Use the RTP Module
+ *
+ * First application must call #pj_rtp_session_init to initialize the RTP
+ * session.
+ *
+ * When application wants to send RTP packet, it needs to call
+ * #pj_rtp_encode_rtp to build the RTP header. Note that this WILL NOT build
+ * the complete RTP packet, but instead only the header. Application can
+ * then either concatenate the header with the payload, or send the two
+ * fragments (the header and the payload) using scatter-gather transport API
+ * (e.g. \a sendv()).
+ *
+ * When application receives an RTP packet, first it should call
+ * #pj_rtp_decode_rtp to decode RTP header and payload, then it should call
+ * #pj_rtp_session_update to check whether we can process the RTP payload,
+ * and to let the RTP session updates its internal status. The decode function
+ * is guaranteed to point the payload to the correct position regardless of
+ * any options present in the RTP packet.
+ *
+ */
+
+
+#ifdef _MSC_VER
+# pragma warning ( disable : 4214 )
+#endif
+
+
+/**
+ * Error codes.
+ */
+enum pj_rtp_error_t
+{
+ PJ_RTP_ERR_RTP_PACKING, /**< Invalid RTP packet. */
+ PJ_RTP_ERR_INVALID_VERSION, /**< Invalid RTP version. */
+ PJ_RTP_ERR_INVALID_SSRC, /**< Invalid SSRC. */
+ PJ_RTP_ERR_INVALID_PT, /**< Invalid payload type. */
+ PJ_RTP_ERR_INVALID_PACKET, /**< Invalid packet. */
+ PJ_RTP_ERR_SESSION_RESTARTED, /**< Session has just been restarted. */
+ PJ_RTP_ERR_SESSION_PROBATION, /**< Session in probation. */
+ PJ_RTP_ERR_BAD_SEQUENCE, /**< Bad RTP sequence number. */
+};
+
+#pragma pack(1)
+/**
+ * RTP packet header.
+ */
+struct pj_rtp_hdr
+{
+#if defined(PJ_IS_BIG_ENDIAN) && (PJ_IS_BIG_ENDIAN!=0)
+ pj_uint16_t v:2; /**< packet type/version */
+ pj_uint16_t p:1; /**< padding flag */
+ pj_uint16_t x:1; /**< extension flag */
+ pj_uint16_t cc:4; /**< CSRC count */
+ pj_uint16_t m:1; /**< marker bit */
+ pj_uint16_t pt:7; /**< payload type */
+#else
+ pj_uint16_t cc:4; /**< CSRC count */
+ pj_uint16_t x:1; /**< header extension flag */
+ pj_uint16_t p:1; /**< padding flag */
+ pj_uint16_t v:2; /**< packet type/version */
+ pj_uint16_t pt:7; /**< payload type */
+ pj_uint16_t m:1; /**< marker bit */
+#endif
+ pj_uint16_t seq; /**< sequence number */
+ pj_uint32_t ts; /**< timestamp */
+ pj_uint32_t ssrc; /**< synchronization source */
+};
+#pragma pack()
+
+typedef struct pj_rtp_hdr pj_rtp_hdr;
+
+/**
+ * RTP extendsion header.
+ */
+struct pj_rtp_ext_hdr
+{
+ pj_uint16_t profile_data;
+ pj_uint16_t length;
+};
+
+typedef struct pj_rtp_ext_hdr pj_rtp_ext_hdr;
+
+/**
+ * A generic sequence number management, used by both RTP and RTCP.
+ */
+struct pj_rtp_seq_session
+{
+ pj_uint16_t max_seq; /**< highest sequence number heard */
+ pj_uint32_t cycles; /**< shifted count of seq. number cycles */
+ pj_uint32_t base_seq; /**< base seq number */
+ pj_uint32_t bad_seq; /**< last 'bad' seq number + 1 */
+ pj_uint32_t probation; /**< sequ. packets till source is valid */
+};
+
+typedef struct pj_rtp_seq_session pj_rtp_seq_session;
+
+/**
+ * RTP session descriptor.
+ */
+struct pj_rtp_session
+{
+ pj_rtp_hdr out_hdr; /**< Saved header for outgoing packets. */
+ pj_rtp_seq_session seq_ctrl; /**< Sequence number management. */
+ pj_uint16_t out_pt; /**< Default outgoing payload type. */
+ pj_uint32_t out_extseq; /**< Outgoing extended sequence number. */
+ pj_uint32_t peer_ssrc; /**< Peer SSRC. */
+ pj_uint32_t received; /**< Number of received packets. */
+};
+
+typedef struct pj_rtp_session pj_rtp_session;
+
+/**
+ * \brief Initialize RTP session.
+ * This function will initialize the RTP session according to given parameters.
+ *
+ * @param ses The session.
+ * @param default_pt Default payload type.
+ * @param sender_ssrc SSRC used for outgoing packets.
+ *
+ * @return zero if successfull.
+ */
+PJ_DECL(pj_status_t) pj_rtp_session_init( pj_rtp_session *ses,
+ int default_pt, pj_uint32_t sender_ssrc );
+
+/**
+ * \brief Encode outgoing RTP packet header.
+ * Create the RTP header based on arguments and current state of the RTP
+ * session.
+ *
+ * @param ses The session.
+ * @param pt Payload type.
+ * @param m Marker flag.
+ * @param payload_len Payload length in bytes.
+ * @param ts_len Timestamp length.
+ * @param rtphdr Upon return will point to RTP packet header.
+ * @param hdrlen Upon return will indicate the size of RTP packet header
+ *
+ * @return zero if successfull.
+ */
+PJ_DECL(pj_status_t) pj_rtp_encode_rtp( pj_rtp_session *ses, int pt, int m,
+ int payload_len, int ts_len,
+ const void **rtphdr, int *hdrlen );
+
+/**
+ * \brief Decode an incoming RTP packet.
+ * This function will decode incoming packet into RTP header and payload.
+ * The decode function is guaranteed to point the payload to the correct
+ * position regardless of any options present in the RTP packet.
+ *
+ * @param ses The session.
+ * @param pkt The received RTP packet.
+ * @param pkt_len The length of the packet.
+ * @param hdr Upon return will point to the location of the RTP header
+ * inside the packet.
+ * @param payload Upon return will point to the location of the
+ * payload inside the packet.
+ * @param payloadlen Upon return will indicate the size of the payload.
+ *
+ * @return zero if successfull.
+ */
+PJ_DECL(pj_status_t) pj_rtp_decode_rtp( pj_rtp_session *ses,
+ const void *pkt, int pkt_len,
+ const pj_rtp_hdr **hdr,
+ const void **payload,
+ unsigned *payloadlen);
+
+/**
+ * \brief Update RTP session with an incoming RTP packet.
+ * Call this function everytime
+ * an RTP packet is received to check whether the packet can be received and to
+ * let the RTP session performs its internal calculations.
+ *
+ * @param ses The session.
+ * @param hdr The RTP header of the incoming packet.
+ *
+ * @return zero if the packet is valid and can be processed, otherwise will
+ * return one of the error in #pj_rtp_error_t.
+ */
+PJ_DECL(pj_status_t) pj_rtp_session_update( pj_rtp_session *ses,
+ const pj_rtp_hdr *hdr);
+
+/**
+* \brief Internal.
+ * Internal function for sequence control, shared by RTCP implementation.
+ */
+void pj_rtp_seq_init(pj_rtp_seq_session *seq_ctrl, pj_uint16_t seq);
+
+/**
+* \brief Internal.
+ * Internal function for sequence control, shared by RTCP implementation.
+ */
+void pj_rtp_seq_restart(pj_rtp_seq_session *seq_ctrl, pj_uint16_t seq);
+
+/**
+* \brief Internal.
+ * Internal function for sequence control, shared by RTCP implementation.
+ */
+int pj_rtp_seq_update(pj_rtp_seq_session *seq_ctrl, pj_uint16_t seq);
+
+/**
+ * @}
+ */
+
+PJ_END_DECL
+
+
+#endif /* __PJMEDIA_RTP_H__ */
diff --git a/pjmedia/src/pjmedia/sdp.c b/pjmedia/src/pjmedia/sdp.c
index 1fe2b7b2..42a1fd4c 100644
--- a/pjmedia/src/pjmedia/sdp.c
+++ b/pjmedia/src/pjmedia/sdp.c
@@ -1,949 +1,949 @@
-/* $Id$ */
-/*
- * Copyright (C) 2003-2006 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 <pjmedia/sdp.h>
-#include <pj/scanner.h>
-#include <pj/except.h>
-#include <pj/log.h>
-#include <pj/os.h>
-#include <pj/string.h>
-#include <pj/pool.h>
-
-enum {
- SKIP_WS = 0,
- SYNTAX_ERROR = 1,
-};
-#define TOKEN "-.!%*_=`'~"
-#define NTP_OFFSET ((pj_uint32_t)2208988800)
-#define LOG_THIS "sdp"
-
-/*
- * Prototypes for line parser.
- */
-static void parse_version(pj_scanner *scanner);
-static void parse_origin(pj_scanner *scanner, pjsdp_session_desc *ses);
-static void parse_time(pj_scanner *scanner, pjsdp_session_desc *ses);
-static void parse_generic_line(pj_scanner *scanner, pj_str_t *str);
-static void parse_connection_info(pj_scanner *scanner, pjsdp_conn_info *conn);
-static pjsdp_attr *parse_attr(pj_pool_t *pool, pj_scanner *scanner);
-static void parse_media(pj_scanner *scanner, pjsdp_media_desc *med);
-
-/*
- * Prototypes for attribute parsers.
- */
-static pjsdp_rtpmap_attr * parse_rtpmap_attr( pj_pool_t *pool, pj_scanner *scanner );
-static pjsdp_attr_string * parse_generic_string_attr( pj_pool_t *pool, pj_scanner *scanner );
-static pjsdp_attr_num * parse_generic_num_attr( pj_pool_t *pool, pj_scanner *scanner );
-static pjsdp_attr * parse_name_only_attr( pj_pool_t *pool, pj_scanner *scanner );
-static pjsdp_fmtp_attr * parse_fmtp_attr( pj_pool_t *pool, pj_scanner *scanner );
-
-
-/*
- * Prototypes for functions to print attribute.
- * All of them returns integer for the length printed, or -1 on error.
- */
-static int print_rtpmap_attr(const pjsdp_rtpmap_attr *attr,
- char *buf, int length);
-static int print_generic_string_attr(const pjsdp_attr_string *attr,
- char *buf, int length);
-static int print_generic_num_attr(const pjsdp_attr_num *attr,
- char *buf, int length);
-static int print_name_only_attr(const pjsdp_attr *attr,
- char *buf, int length);
-static int print_fmtp_attr(const pjsdp_fmtp_attr *attr,
- char *buf, int length);
-
-/*
- * Prototypes for cloning attributes.
- */
-static pjsdp_attr* clone_rtpmap_attr (pj_pool_t *pool, const pjsdp_attr *rhs);
-static pjsdp_attr* clone_generic_string_attr (pj_pool_t *pool, const pjsdp_attr *rhs);
-static pjsdp_attr* clone_generic_num_attr (pj_pool_t *pool, const pjsdp_attr *rhs);
-static pjsdp_attr* clone_name_only_attr (pj_pool_t *pool, const pjsdp_attr *rhs);
-static pjsdp_attr* clone_fmtp_attr (pj_pool_t *pool, const pjsdp_attr *rhs);
-
-
-/*
- * Prototypes
- */
-static void init_sdp_parser(void);
-
-
-typedef void * (*FPARSE)(pj_pool_t *pool, pj_scanner *scanner);
-typedef int (*FPRINT)(const void *attr, char *buf, int length);
-typedef pjsdp_attr* (*FCLONE)(pj_pool_t *pool, const pjsdp_attr *rhs);
-
-/*
- * Array of functions to print attribute.
- */
-static struct attr_map_rec
-{
- pj_str_t name;
- FPARSE parse_attr;
- FPRINT print_attr;
- FCLONE clone;
-} attr_map[] =
-{
- {{"rtpmap", 6}, (FPARSE)&parse_rtpmap_attr, (FPRINT)&print_rtpmap_attr, (FCLONE)&clone_rtpmap_attr},
- {{"cat", 3}, (FPARSE)&parse_generic_string_attr, (FPRINT)&print_generic_string_attr, (FCLONE)&clone_generic_string_attr},
- {{"keywds", 6}, (FPARSE)&parse_generic_string_attr, (FPRINT)&print_generic_string_attr, (FCLONE)&clone_generic_string_attr},
- {{"tool", 4}, (FPARSE)&parse_generic_string_attr, (FPRINT)&print_generic_string_attr, (FCLONE)&clone_generic_string_attr},
- {{"ptime", 5}, (FPARSE)&parse_generic_num_attr, (FPRINT)&print_generic_num_attr, (FCLONE)&clone_generic_num_attr},
- {{"recvonly", 8}, (FPARSE)&parse_name_only_attr, (FPRINT)&print_name_only_attr, (FCLONE)&clone_name_only_attr},
- {{"sendonly", 8}, (FPARSE)&parse_name_only_attr, (FPRINT)&print_name_only_attr, (FCLONE)&clone_name_only_attr},
- {{"sendrecv", 8}, (FPARSE)&parse_name_only_attr, (FPRINT)&print_name_only_attr, (FCLONE)&clone_name_only_attr},
- {{"orient", 6}, (FPARSE)&parse_generic_string_attr, (FPRINT)&print_generic_string_attr, (FCLONE)&clone_generic_string_attr},
- {{"type", 4}, (FPARSE)&parse_generic_string_attr, (FPRINT)&print_generic_string_attr, (FCLONE)&clone_generic_string_attr},
- {{"charset", 7}, (FPARSE)&parse_generic_string_attr, (FPRINT)&print_generic_string_attr, (FCLONE)&clone_generic_string_attr},
- {{"sdplang", 7}, (FPARSE)&parse_generic_string_attr, (FPRINT)&print_generic_string_attr, (FCLONE)&clone_generic_string_attr},
- {{"lang", 4}, (FPARSE)&parse_generic_string_attr, (FPRINT)&print_generic_string_attr, (FCLONE)&clone_generic_string_attr},
- {{"framerate", 9}, (FPARSE)&parse_generic_string_attr, (FPRINT)&print_generic_string_attr, (FCLONE)&clone_generic_string_attr},
- {{"quality", 7}, (FPARSE)&parse_generic_num_attr, (FPRINT)&print_generic_num_attr, (FCLONE)&clone_generic_num_attr},
- {{"fmtp", 4}, (FPARSE)&parse_fmtp_attr, (FPRINT)&print_fmtp_attr, (FCLONE)&clone_fmtp_attr},
- {{"inactive", 8}, (FPARSE)&parse_name_only_attr, (FPRINT)&print_name_only_attr, (FCLONE)&clone_name_only_attr},
- {{"", 0}, NULL, (FPRINT)&print_generic_string_attr, (FCLONE)&clone_generic_string_attr}
-};
-
-/*
- * Scanner character specification.
- */
-static int is_initialized;
-static pj_char_spec cs_token;
-
-static void init_sdp_parser(void)
-{
- if (is_initialized == 0) {
- is_initialized = 1;
- if (is_initialized != 1) {
- return;
- }
- }
- pj_cs_add_alpha(cs_token);
- pj_cs_add_num(cs_token);
- pj_cs_add_str( cs_token, TOKEN);
-}
-
-static int print_rtpmap_attr(const pjsdp_rtpmap_attr *rtpmap,
- char *buf, int len)
-{
- char *p = buf;
-
- if (len < 16+rtpmap->encoding_name.slen+rtpmap->parameter.slen) {
- return -1;
- }
-
- /* colon and payload type. */
- *p++ = ':';
- len = pj_utoa(rtpmap->payload_type, p);
- p += len;
-
- /* space, encoding name */
- *p++ = ' ';
- pj_memcpy(p, rtpmap->encoding_name.ptr, rtpmap->encoding_name.slen);
- p += rtpmap->encoding_name.slen;
-
- /* slash, clock-rate. */
- *p++ = '/';
- len = pj_utoa(rtpmap->clock_rate, p);
- p += len;
-
- /* optionally add encoding parameter. */
- if (rtpmap->parameter.slen) {
- *p++ = '/';
- pj_memcpy(p, rtpmap->parameter.ptr, rtpmap->parameter.slen);
- p += rtpmap->parameter.slen;
- }
-
- return p-buf;
-}
-
-static int print_generic_string_attr(const pjsdp_attr_string *attr,
- char *buf, int len)
-{
- char *p = buf;
-
- if (len < attr->value.slen + 4) {
- return -1;
- }
-
- /* colon and attribute value. */
- *p++ = ':';
- pj_memcpy(p, attr->value.ptr, attr->value.slen);
- p += attr->value.slen;
-
- return p-buf;
-}
-
-static int print_generic_num_attr(const pjsdp_attr_num *attr, char *buf, int len)
-{
- char *p = buf;
-
- if (len < 10) {
- return -1;
- }
- *p++ = ':';
- return pj_utoa(attr->value, p);
-}
-
-static int print_name_only_attr(const pjsdp_attr *attr, char *buf, int len)
-{
- PJ_UNUSED_ARG(attr)
- PJ_UNUSED_ARG(buf)
- PJ_UNUSED_ARG(len)
- return 0;
-}
-
-static int print_fmtp_attr(const pjsdp_fmtp_attr *fmtp, char *buf, int len)
-{
- char *p = buf;
-
- if (len < 4+fmtp->format.slen+fmtp->param.slen) {
- return -1;
- }
-
- /* colon and format. */
- *p++ = ':';
- pj_memcpy(p, fmtp->format.ptr, fmtp->format.slen);
- p += fmtp->format.slen;
-
- /* space and parameter. */
- *p++ = ' ';
- pj_memcpy(p, fmtp->param.ptr, fmtp->param.slen);
- p += fmtp->param.slen;
-
- return p-buf;
-}
-
-
-static int print_attr(const pjsdp_attr *attr, char *buf, int len)
-{
- char *p = buf;
- struct attr_map_rec *desc = &attr_map[attr->type];
-
- if (len < 16) {
- return -1;
- }
-
- *p++ = 'a';
- *p++ = '=';
- pj_memcpy(p, desc->name.ptr, desc->name.slen);
- p += desc->name.slen;
-
- len = (*desc->print_attr)(attr, p, (buf+len)-p);
- if (len < 0) {
- return -1;
- }
- p += len;
- *p++ = '\r';
- *p++ = '\n';
- return p-buf;
-}
-
-static pjsdp_attr* clone_rtpmap_attr (pj_pool_t *pool, const pjsdp_attr *p)
-{
- const pjsdp_rtpmap_attr *rhs = (const pjsdp_rtpmap_attr*)p;
- pjsdp_rtpmap_attr *attr = pj_pool_alloc (pool, sizeof(pjsdp_rtpmap_attr));
- if (!attr)
- return NULL;
-
- attr->type = rhs->type;
- attr->payload_type = rhs->payload_type;
- if (!pj_strdup (pool, &attr->encoding_name, &rhs->encoding_name)) return NULL;
- attr->clock_rate = rhs->clock_rate;
- if (!pj_strdup (pool, &attr->parameter, &rhs->parameter)) return NULL;
-
- return (pjsdp_attr*)attr;
-}
-
-static pjsdp_attr* clone_generic_string_attr (pj_pool_t *pool, const pjsdp_attr *p)
-{
- const pjsdp_attr_string* rhs = (const pjsdp_attr_string*) p;
- pjsdp_attr_string *attr = pj_pool_alloc (pool, sizeof(pjsdp_attr_string));
- if (!attr)
- return NULL;
-
- attr->type = rhs->type;
- if (!pj_strdup (pool, &attr->value, &rhs->value)) return NULL;
-
- return (pjsdp_attr*)attr;
-}
-
-static pjsdp_attr* clone_generic_num_attr (pj_pool_t *pool, const pjsdp_attr *p)
-{
- const pjsdp_attr_num* rhs = (const pjsdp_attr_num*) p;
- pjsdp_attr_num *attr = pj_pool_alloc (pool, sizeof(pjsdp_attr_num));
- if (!attr)
- return NULL;
-
- attr->type = rhs->type;
- attr->value = rhs->value;
-
- return (pjsdp_attr*)attr;
-}
-
-static pjsdp_attr* clone_name_only_attr (pj_pool_t *pool, const pjsdp_attr *rhs)
-{
- pjsdp_attr *attr = pj_pool_alloc (pool, sizeof(pjsdp_attr));
- if (!attr)
- return NULL;
-
- attr->type = rhs->type;
- return attr;
-}
-
-static pjsdp_attr* clone_fmtp_attr (pj_pool_t *pool, const pjsdp_attr *p)
-{
- const pjsdp_fmtp_attr* rhs = (const pjsdp_fmtp_attr*) p;
- pjsdp_fmtp_attr *attr = pj_pool_alloc (pool, sizeof(pjsdp_fmtp_attr));
- if (!attr)
- return NULL;
-
- attr->type = rhs->type;
- if (!pj_strdup (pool, &attr->format, &rhs->format)) return NULL;
- if (!pj_strdup (pool, &attr->param, &rhs->param)) return NULL;
-
- return (pjsdp_attr*)attr;
-}
-
-PJ_DEF(pjsdp_attr*) pjsdp_attr_clone (pj_pool_t *pool, const pjsdp_attr *rhs)
-{
- struct attr_map_rec *desc;
-
- if (rhs->type >= PJSDP_END_OF_ATTR) {
- pj_assert(0);
- return NULL;
- }
-
- desc = &attr_map[rhs->type];
- return (*desc->clone) (pool, rhs);
-}
-
-PJ_DEF(const pjsdp_attr*) pjsdp_attr_find (int count, const pjsdp_attr *attr_array[], int type)
-{
- int i;
-
- for (i=0; i<count; ++i) {
- if (attr_array[i]->type == type)
- return attr_array[i];
- }
- return NULL;
-}
-
-static int print_connection_info( pjsdp_conn_info *c, char *buf, int len)
-{
- char *p = buf;
-
- if (len < 8+c->net_type.slen+c->addr_type.slen+c->addr.slen) {
- return -1;
- }
- *p++ = 'c';
- *p++ = '=';
- pj_memcpy(p, c->net_type.ptr, c->net_type.slen);
- p += c->net_type.slen;
- *p++ = ' ';
- pj_memcpy(p, c->addr_type.ptr, c->addr_type.slen);
- p += c->addr_type.slen;
- *p++ = ' ';
- pj_memcpy(p, c->addr.ptr, c->addr.slen);
- p += c->addr.slen;
- *p++ = '\r';
- *p++ = '\n';
-
- return p-buf;
-}
-
-PJ_DEF(pjsdp_conn_info*) pjsdp_conn_info_clone (pj_pool_t *pool, const pjsdp_conn_info *rhs)
-{
- pjsdp_conn_info *c = pj_pool_alloc (pool, sizeof(pjsdp_conn_info));
- if (!c) return NULL;
-
- if (!pj_strdup (pool, &c->net_type, &rhs->net_type)) return NULL;
- if (!pj_strdup (pool, &c->addr_type, &rhs->addr_type)) return NULL;
- if (!pj_strdup (pool, &c->addr, &rhs->addr)) return NULL;
-
- return c;
-}
-
-static int print_media_desc( pjsdp_media_desc *m, char *buf, int len)
-{
- char *p = buf;
- char *end = buf+len;
- unsigned i;
- int printed;
-
- /* check length for the "m=" line. */
- if (len < m->desc.media.slen+m->desc.transport.slen+12+24) {
- return -1;
- }
- *p++ = 'm'; /* m= */
- *p++ = '=';
- pj_memcpy(p, m->desc.media.ptr, m->desc.media.slen);
- p += m->desc.media.slen;
- *p++ = ' ';
- printed = pj_utoa(m->desc.port, p);
- p += printed;
- if (m->desc.port_count > 1) {
- *p++ = '/';
- printed = pj_utoa(m->desc.port_count, p);
- p += printed;
- }
- *p++ = ' ';
- pj_memcpy(p, m->desc.transport.ptr, m->desc.transport.slen);
- p += m->desc.transport.slen;
- for (i=0; i<m->desc.fmt_count; ++i) {
- *p++ = ' ';
- pj_memcpy(p, m->desc.fmt[i].ptr, m->desc.fmt[i].slen);
- p += m->desc.fmt[i].slen;
- }
- *p++ = '\r';
- *p++ = '\n';
-
- /* print connection info, if present. */
- if (m->conn) {
- printed = print_connection_info(m->conn, p, end-p);
- if (printed < 0) {
- return -1;
- }
- p += printed;
- }
-
- /* print attributes. */
- for (i=0; i<m->attr_count; ++i) {
- printed = print_attr(m->attr[i], p, end-p);
- if (printed < 0) {
- return -1;
- }
- p += printed;
- }
-
- return p-buf;
-}
-
-PJ_DEF(pjsdp_media_desc*) pjsdp_media_desc_clone (pj_pool_t *pool,
- const pjsdp_media_desc *rhs)
-{
- unsigned int i;
- pjsdp_media_desc *m = pj_pool_alloc (pool, sizeof(pjsdp_media_desc));
- if (!m)
- return NULL;
-
- pj_strdup (pool, &m->desc.media, &rhs->desc.media);
- m->desc.port = rhs->desc.port;
- m->desc.port_count = rhs->desc.port_count;
- pj_strdup (pool, &m->desc.transport, &rhs->desc.transport);
- m->desc.fmt_count = rhs->desc.fmt_count;
- for (i=0; i<rhs->desc.fmt_count; ++i)
- m->desc.fmt[i] = rhs->desc.fmt[i];
-
- if (rhs->conn) {
- m->conn = pjsdp_conn_info_clone (pool, rhs->conn);
- if (!m->conn)
- return NULL;
- } else {
- m->conn = NULL;
- }
-
- m->attr_count = rhs->attr_count;
- for (i=0; i < rhs->attr_count; ++i) {
- m->attr[i] = pjsdp_attr_clone (pool, rhs->attr[i]);
- if (!m->attr[i])
- return NULL;
- }
-
- return m;
-}
-
-/** Check if the media description has the specified attribute. */
-PJ_DEF(pj_bool_t) pjsdp_media_desc_has_attr (const pjsdp_media_desc *m,
- pjsdp_attr_type_e attr_type)
-{
- unsigned i;
- for (i=0; i<m->attr_count; ++i) {
- pjsdp_attr *attr = m->attr[i];
- if (attr->type == attr_type)
- return 1;
- }
- return 0;
-}
-
-/** Find rtpmap attribute for the specified payload type. */
-PJ_DEF(const pjsdp_rtpmap_attr*)
-pjsdp_media_desc_find_rtpmap (const pjsdp_media_desc *m, unsigned pt)
-{
- unsigned i;
- for (i=0; i<m->attr_count; ++i) {
- pjsdp_attr *attr = m->attr[i];
- if (attr->type == PJSDP_ATTR_RTPMAP) {
- const pjsdp_rtpmap_attr* rtpmap = (const pjsdp_rtpmap_attr*)attr;
- if (rtpmap->payload_type == pt)
- return rtpmap;
- }
- }
- return NULL;
-}
-
-
-static int print_session(const pjsdp_session_desc *ses, char *buf, pj_ssize_t len)
-{
- char *p = buf;
- char *end = buf+len;
- unsigned i;
- int printed;
-
- /* Check length for v= and o= lines. */
- if (len < 5+
- 2+ses->origin.user.slen+18+
- ses->origin.net_type.slen+ses->origin.addr.slen + 2)
- {
- return -1;
- }
-
- /* SDP version (v= line) */
- pj_memcpy(p, "v=0\r\n", 5);
- p += 5;
-
- /* Owner (o=) line. */
- *p++ = 'o';
- *p++ = '=';
- pj_memcpy(p, ses->origin.user.ptr, ses->origin.user.slen);
- p += ses->origin.user.slen;
- *p++ = ' ';
- printed = pj_utoa(ses->origin.id, p);
- p += printed;
- *p++ = ' ';
- printed = pj_utoa(ses->origin.version, p);
- p += printed;
- *p++ = ' ';
- pj_memcpy(p, ses->origin.net_type.ptr, ses->origin.net_type.slen);
- p += ses->origin.net_type.slen;
- *p++ = ' ';
- pj_memcpy(p, ses->origin.addr_type.ptr, ses->origin.addr_type.slen);
- p += ses->origin.addr_type.slen;
- *p++ = ' ';
- pj_memcpy(p, ses->origin.addr.ptr, ses->origin.addr.slen);
- p += ses->origin.addr.slen;
- *p++ = '\r';
- *p++ = '\n';
-
- /* Session name (s=) line. */
- if ((end-p) < 8+ses->name.slen) {
- return -1;
- }
- *p++ = 's';
- *p++ = '=';
- pj_memcpy(p, ses->name.ptr, ses->name.slen);
- p += ses->name.slen;
- *p++ = '\r';
- *p++ = '\n';
-
- /* Time */
- if ((end-p) < 24) {
- return -1;
- }
- *p++ = 't';
- *p++ = '=';
- printed = pj_utoa(ses->time.start, p);
- p += printed;
- *p++ = ' ';
- printed = pj_utoa(ses->time.stop, p);
- p += printed;
- *p++ = '\r';
- *p++ = '\n';
-
- /* Connection line (c=) if exist. */
- if (ses->conn) {
- printed = print_connection_info(ses->conn, p, end-p);
- if (printed < 1) {
- return -1;
- }
- p += printed;
- }
-
- /* Print all attribute (a=) lines. */
- for (i=0; i<ses->attr_count; ++i) {
- printed = print_attr(ses->attr[i], p, end-p);
- if (printed < 0) {
- return -1;
- }
- p += printed;
- }
-
- /* Print media (m=) lines. */
- for (i=0; i<ses->media_count; ++i) {
- printed = print_media_desc(ses->media[i], p, end-p);
- if (printed < 0) {
- return -1;
- }
- p += printed;
- }
-
- return p-buf;
-}
-
-/******************************************************************************
- * PARSERS
- */
-
-static void parse_version(pj_scanner *scanner)
-{
- pj_scan_advance_n(scanner, 3, SKIP_WS);
- pj_scan_get_newline(scanner);
-}
-
-static void parse_origin(pj_scanner *scanner, pjsdp_session_desc *ses)
-{
- pj_str_t str;
-
- /* o= */
- pj_scan_advance_n(scanner, 2, SKIP_WS);
-
- /* username. */
- pj_scan_get_until_ch(scanner, ' ', &ses->origin.user);
- pj_scan_get_char(scanner);
-
- /* id */
- pj_scan_get_until_ch(scanner, ' ', &str);
- ses->origin.id = pj_strtoul(&str);
- pj_scan_get_char(scanner);
-
- /* version */
- pj_scan_get_until_ch(scanner, ' ', &str);
- ses->origin.version = pj_strtoul(&str);
- pj_scan_get_char(scanner);
-
- /* network-type */
- pj_scan_get_until_ch(scanner, ' ', &ses->origin.net_type);
- pj_scan_get_char(scanner);
-
- /* addr-type */
- pj_scan_get_until_ch(scanner, ' ', &ses->origin.addr_type);
- pj_scan_get_char(scanner);
-
- /* address */
- pj_scan_get_until_ch(scanner, '\r', &ses->origin.addr);
-
- /* newline */
- pj_scan_get_newline(scanner);
-}
-
-static void parse_time(pj_scanner *scanner, pjsdp_session_desc *ses)
-{
- pj_str_t str;
-
- /* t= */
- pj_scan_advance_n(scanner, 2, SKIP_WS);
-
- /* start time */
- pj_scan_get_until_ch(scanner, ' ', &str);
- ses->time.start = pj_strtoul(&str);
-
- pj_scan_get_char(scanner);
-
- /* stop time */
- pj_scan_get_until_ch(scanner, '\r', &str);
- ses->time.stop = pj_strtoul(&str);
-
- /* newline */
- pj_scan_get_newline(scanner);
-}
-
-static void parse_generic_line(pj_scanner *scanner, pj_str_t *str)
-{
- /* x= */
- pj_scan_advance_n(scanner, 2, SKIP_WS);
-
- /* get anything until newline. */
- pj_scan_get_until_ch(scanner, '\r', str);
-
- /* newline. */
- pj_scan_get_newline(scanner);
-}
-
-static void parse_connection_info(pj_scanner *scanner, pjsdp_conn_info *conn)
-{
- /* c= */
- pj_scan_advance_n(scanner, 2, SKIP_WS);
-
- /* network-type */
- pj_scan_get_until_ch(scanner, ' ', &conn->net_type);
- pj_scan_get_char(scanner);
-
- /* addr-type */
- pj_scan_get_until_ch(scanner, ' ', &conn->addr_type);
- pj_scan_get_char(scanner);
-
- /* address. */
- pj_scan_get_until_ch(scanner, '\r', &conn->addr);
-
- /* newline */
- pj_scan_get_newline(scanner);
-}
-
-static void parse_media(pj_scanner *scanner, pjsdp_media_desc *med)
-{
- pj_str_t str;
-
- /* m= */
- pj_scan_advance_n(scanner, 2, SKIP_WS);
-
- /* type */
- pj_scan_get_until_ch(scanner, ' ', &med->desc.media);
- pj_scan_get_char(scanner);
-
- /* port */
- pj_scan_get(scanner, cs_token, &str);
- med->desc.port = (unsigned short)pj_strtoul(&str);
- if (*scanner->current == '/') {
- /* port count */
- pj_scan_get_char(scanner);
- pj_scan_get(scanner, cs_token, &str);
- med->desc.port_count = pj_strtoul(&str);
-
- } else {
- med->desc.port_count = 0;
- }
-
- if (pj_scan_get_char(scanner) != ' ') {
- PJ_THROW(SYNTAX_ERROR);
- }
-
- /* transport */
- pj_scan_get_until_ch(scanner, ' ', &med->desc.transport);
-
- /* format list */
- med->desc.fmt_count = 0;
- while (*scanner->current == ' ') {
- pj_scan_get_char(scanner);
- pj_scan_get(scanner, cs_token, &med->desc.fmt[med->desc.fmt_count++]);
- }
-
- /* newline */
- pj_scan_get_newline(scanner);
-}
-
-static pjsdp_rtpmap_attr * parse_rtpmap_attr( pj_pool_t *pool, pj_scanner *scanner )
-{
- pjsdp_rtpmap_attr *rtpmap;
- pj_str_t str;
-
- rtpmap = pj_pool_calloc(pool, 1, sizeof(*rtpmap));
- if (pj_scan_get_char(scanner) != ':') {
- PJ_THROW(SYNTAX_ERROR);
- }
- pj_scan_get_until_ch(scanner, ' ', &str);
- rtpmap->payload_type = pj_strtoul(&str);
- pj_scan_get_char(scanner);
-
- pj_scan_get_until_ch(scanner, '/', &rtpmap->encoding_name);
- pj_scan_get_char(scanner);
- pj_scan_get(scanner, cs_token, &str);
- rtpmap->clock_rate = pj_strtoul(&str);
-
- if (*scanner->current == '/') {
- pj_scan_get_char(scanner);
- pj_scan_get_until_ch(scanner, '\r', &rtpmap->parameter);
- }
-
- return rtpmap;
-}
-
-static pjsdp_attr_string * parse_generic_string_attr( pj_pool_t *pool, pj_scanner *scanner )
-{
- pjsdp_attr_string *attr;
- attr = pj_pool_calloc(pool, 1, sizeof(*attr));
-
- if (pj_scan_get_char(scanner) != ':') {
- PJ_THROW(SYNTAX_ERROR);
- }
- pj_scan_get_until_ch(scanner, '\r', &attr->value);
- return attr;
-}
-
-static pjsdp_attr_num * parse_generic_num_attr( pj_pool_t *pool, pj_scanner *scanner )
-{
- pjsdp_attr_num *attr;
- pj_str_t str;
-
- attr = pj_pool_calloc(pool, 1, sizeof(*attr));
-
- if (pj_scan_get_char(scanner) != ':') {
- PJ_THROW(SYNTAX_ERROR);
- }
- pj_scan_get_until_ch(scanner, '\r', &str);
- attr->value = pj_strtoul(&str);
- return attr;
-}
-
-static pjsdp_attr * parse_name_only_attr( pj_pool_t *pool, pj_scanner *scanner )
-{
- pjsdp_attr *attr;
-
- PJ_UNUSED_ARG(scanner)
- attr = pj_pool_calloc(pool, 1, sizeof(*attr));
- return attr;
-}
-
-static pjsdp_fmtp_attr * parse_fmtp_attr( pj_pool_t *pool, pj_scanner *scanner )
-{
- pjsdp_fmtp_attr *fmtp;
-
- fmtp = pj_pool_calloc(pool, 1, sizeof(*fmtp));
-
- if (pj_scan_get_char(scanner) != ':') {
- PJ_THROW(SYNTAX_ERROR);
- }
- pj_scan_get_until_ch(scanner, ' ', &fmtp->format);
- pj_scan_get_char(scanner);
- pj_scan_get_until_ch(scanner, '\r', &fmtp->param);
- return fmtp;
-}
-
-static pjsdp_attr *parse_attr( pj_pool_t *pool, pj_scanner *scanner)
-{
- void * (*parse_func)(pj_pool_t *pool, pj_scanner *scanner) = NULL;
- pj_str_t attrname;
- unsigned i;
- pjsdp_attr *attr;
-
- /* skip a= */
- pj_scan_advance_n(scanner, 2, SKIP_WS);
-
- /* get attr name. */
- pj_scan_get(scanner, cs_token, &attrname);
-
- /* find entry to handle attrname */
- for (i=0; i<PJ_ARRAY_SIZE(attr_map); ++i) {
- struct attr_map_rec *p = &attr_map[i];
- if (pj_strcmp(&attrname, &p->name) == 0) {
- parse_func = p->parse_attr;
- break;
- }
- }
-
- /* fallback to generic string parser. */
- if (parse_func == NULL) {
- parse_func = &parse_generic_string_attr;
- }
-
- attr = (*parse_func)(pool, scanner);
- attr->type = i;
-
- /* newline */
- pj_scan_get_newline(scanner);
-
- return attr;
-}
-
-static void on_scanner_error(pj_scanner *scanner)
-{
- PJ_UNUSED_ARG(scanner)
-
- PJ_THROW(SYNTAX_ERROR);
-}
-
-/*
- * Parse SDP message.
- */
-PJ_DEF(pjsdp_session_desc*) pjsdp_parse( char *buf, pj_size_t len,
- pj_pool_t *pool)
-{
- pj_scanner scanner;
- pjsdp_session_desc *session;
- pjsdp_media_desc *media = NULL;
- void *attr;
- pjsdp_conn_info *conn;
- pj_str_t dummy;
- int cur_name = 254;
- PJ_USE_EXCEPTION;
-
- init_sdp_parser();
-
- pj_scan_init(&scanner, buf, len, 0, &on_scanner_error);
- session = pj_pool_calloc(pool, 1, sizeof(*session));
-
- PJ_TRY {
- while (!pj_scan_is_eof(&scanner)) {
- cur_name = *scanner.current;
- switch (cur_name) {
- case 'a':
- attr = parse_attr(pool, &scanner);
- if (attr) {
- if (media) {
- media->attr[media->attr_count++] = attr;
- } else {
- session->attr[session->attr_count++] = attr;
- }
- }
- break;
- case 'o':
- parse_origin(&scanner, session);
- break;
- case 's':
- parse_generic_line(&scanner, &session->name);
- break;
- case 'c':
- conn = pj_pool_calloc(pool, 1, sizeof(*conn));
- parse_connection_info(&scanner, conn);
- if (media) {
- media->conn = conn;
- } else {
- session->conn = conn;
- }
- break;
- case 't':
- parse_time(&scanner, session);
- break;
- case 'm':
- media = pj_pool_calloc(pool, 1, sizeof(*media));
- parse_media(&scanner, media);
- session->media[ session->media_count++ ] = media;
- break;
- case 'v':
- parse_version(&scanner);
- break;
- default:
- parse_generic_line(&scanner, &dummy);
- break;
- }
- }
- }
- PJ_CATCH(SYNTAX_ERROR) {
- PJ_LOG(2, (LOG_THIS, "Syntax error in SDP parser '%c' line %d col %d",
- cur_name, scanner.line, scanner.col));
- if (!pj_scan_is_eof(&scanner)) {
- if (*scanner.current != '\r') {
- pj_scan_get_until_ch(&scanner, '\r', &dummy);
- }
- pj_scan_get_newline(&scanner);
- }
- }
- PJ_END;
-
- pj_scan_fini(&scanner);
- return session;
-}
-
-/*
- * Print SDP description.
- */
-PJ_DEF(int) pjsdp_print( const pjsdp_session_desc *desc, char *buf, pj_size_t size)
-{
- return print_session(desc, buf, size);
-}
-
-
+/* $Id$ */
+/*
+ * Copyright (C) 2003-2006 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 <pjmedia/sdp.h>
+#include <pj/scanner.h>
+#include <pj/except.h>
+#include <pj/log.h>
+#include <pj/os.h>
+#include <pj/string.h>
+#include <pj/pool.h>
+
+enum {
+ SKIP_WS = 0,
+ SYNTAX_ERROR = 1,
+};
+#define TOKEN "-.!%*_=`'~"
+#define NTP_OFFSET ((pj_uint32_t)2208988800)
+#define LOG_THIS "sdp"
+
+/*
+ * Prototypes for line parser.
+ */
+static void parse_version(pj_scanner *scanner);
+static void parse_origin(pj_scanner *scanner, pjsdp_session_desc *ses);
+static void parse_time(pj_scanner *scanner, pjsdp_session_desc *ses);
+static void parse_generic_line(pj_scanner *scanner, pj_str_t *str);
+static void parse_connection_info(pj_scanner *scanner, pjsdp_conn_info *conn);
+static pjsdp_attr *parse_attr(pj_pool_t *pool, pj_scanner *scanner);
+static void parse_media(pj_scanner *scanner, pjsdp_media_desc *med);
+
+/*
+ * Prototypes for attribute parsers.
+ */
+static pjsdp_rtpmap_attr * parse_rtpmap_attr( pj_pool_t *pool, pj_scanner *scanner );
+static pjsdp_attr_string * parse_generic_string_attr( pj_pool_t *pool, pj_scanner *scanner );
+static pjsdp_attr_num * parse_generic_num_attr( pj_pool_t *pool, pj_scanner *scanner );
+static pjsdp_attr * parse_name_only_attr( pj_pool_t *pool, pj_scanner *scanner );
+static pjsdp_fmtp_attr * parse_fmtp_attr( pj_pool_t *pool, pj_scanner *scanner );
+
+
+/*
+ * Prototypes for functions to print attribute.
+ * All of them returns integer for the length printed, or -1 on error.
+ */
+static int print_rtpmap_attr(const pjsdp_rtpmap_attr *attr,
+ char *buf, int length);
+static int print_generic_string_attr(const pjsdp_attr_string *attr,
+ char *buf, int length);
+static int print_generic_num_attr(const pjsdp_attr_num *attr,
+ char *buf, int length);
+static int print_name_only_attr(const pjsdp_attr *attr,
+ char *buf, int length);
+static int print_fmtp_attr(const pjsdp_fmtp_attr *attr,
+ char *buf, int length);
+
+/*
+ * Prototypes for cloning attributes.
+ */
+static pjsdp_attr* clone_rtpmap_attr (pj_pool_t *pool, const pjsdp_attr *rhs);
+static pjsdp_attr* clone_generic_string_attr (pj_pool_t *pool, const pjsdp_attr *rhs);
+static pjsdp_attr* clone_generic_num_attr (pj_pool_t *pool, const pjsdp_attr *rhs);
+static pjsdp_attr* clone_name_only_attr (pj_pool_t *pool, const pjsdp_attr *rhs);
+static pjsdp_attr* clone_fmtp_attr (pj_pool_t *pool, const pjsdp_attr *rhs);
+
+
+/*
+ * Prototypes
+ */
+static void init_sdp_parser(void);
+
+
+typedef void * (*FPARSE)(pj_pool_t *pool, pj_scanner *scanner);
+typedef int (*FPRINT)(const void *attr, char *buf, int length);
+typedef pjsdp_attr* (*FCLONE)(pj_pool_t *pool, const pjsdp_attr *rhs);
+
+/*
+ * Array of functions to print attribute.
+ */
+static struct attr_map_rec
+{
+ pj_str_t name;
+ FPARSE parse_attr;
+ FPRINT print_attr;
+ FCLONE clone;
+} attr_map[] =
+{
+ {{"rtpmap", 6}, (FPARSE)&parse_rtpmap_attr, (FPRINT)&print_rtpmap_attr, (FCLONE)&clone_rtpmap_attr},
+ {{"cat", 3}, (FPARSE)&parse_generic_string_attr, (FPRINT)&print_generic_string_attr, (FCLONE)&clone_generic_string_attr},
+ {{"keywds", 6}, (FPARSE)&parse_generic_string_attr, (FPRINT)&print_generic_string_attr, (FCLONE)&clone_generic_string_attr},
+ {{"tool", 4}, (FPARSE)&parse_generic_string_attr, (FPRINT)&print_generic_string_attr, (FCLONE)&clone_generic_string_attr},
+ {{"ptime", 5}, (FPARSE)&parse_generic_num_attr, (FPRINT)&print_generic_num_attr, (FCLONE)&clone_generic_num_attr},
+ {{"recvonly", 8}, (FPARSE)&parse_name_only_attr, (FPRINT)&print_name_only_attr, (FCLONE)&clone_name_only_attr},
+ {{"sendonly", 8}, (FPARSE)&parse_name_only_attr, (FPRINT)&print_name_only_attr, (FCLONE)&clone_name_only_attr},
+ {{"sendrecv", 8}, (FPARSE)&parse_name_only_attr, (FPRINT)&print_name_only_attr, (FCLONE)&clone_name_only_attr},
+ {{"orient", 6}, (FPARSE)&parse_generic_string_attr, (FPRINT)&print_generic_string_attr, (FCLONE)&clone_generic_string_attr},
+ {{"type", 4}, (FPARSE)&parse_generic_string_attr, (FPRINT)&print_generic_string_attr, (FCLONE)&clone_generic_string_attr},
+ {{"charset", 7}, (FPARSE)&parse_generic_string_attr, (FPRINT)&print_generic_string_attr, (FCLONE)&clone_generic_string_attr},
+ {{"sdplang", 7}, (FPARSE)&parse_generic_string_attr, (FPRINT)&print_generic_string_attr, (FCLONE)&clone_generic_string_attr},
+ {{"lang", 4}, (FPARSE)&parse_generic_string_attr, (FPRINT)&print_generic_string_attr, (FCLONE)&clone_generic_string_attr},
+ {{"framerate", 9}, (FPARSE)&parse_generic_string_attr, (FPRINT)&print_generic_string_attr, (FCLONE)&clone_generic_string_attr},
+ {{"quality", 7}, (FPARSE)&parse_generic_num_attr, (FPRINT)&print_generic_num_attr, (FCLONE)&clone_generic_num_attr},
+ {{"fmtp", 4}, (FPARSE)&parse_fmtp_attr, (FPRINT)&print_fmtp_attr, (FCLONE)&clone_fmtp_attr},
+ {{"inactive", 8}, (FPARSE)&parse_name_only_attr, (FPRINT)&print_name_only_attr, (FCLONE)&clone_name_only_attr},
+ {{"", 0}, NULL, (FPRINT)&print_generic_string_attr, (FCLONE)&clone_generic_string_attr}
+};
+
+/*
+ * Scanner character specification.
+ */
+static int is_initialized;
+static pj_char_spec cs_token;
+
+static void init_sdp_parser(void)
+{
+ if (is_initialized == 0) {
+ is_initialized = 1;
+ if (is_initialized != 1) {
+ return;
+ }
+ }
+ pj_cs_add_alpha(cs_token);
+ pj_cs_add_num(cs_token);
+ pj_cs_add_str( cs_token, TOKEN);
+}
+
+static int print_rtpmap_attr(const pjsdp_rtpmap_attr *rtpmap,
+ char *buf, int len)
+{
+ char *p = buf;
+
+ if (len < 16+rtpmap->encoding_name.slen+rtpmap->parameter.slen) {
+ return -1;
+ }
+
+ /* colon and payload type. */
+ *p++ = ':';
+ len = pj_utoa(rtpmap->payload_type, p);
+ p += len;
+
+ /* space, encoding name */
+ *p++ = ' ';
+ pj_memcpy(p, rtpmap->encoding_name.ptr, rtpmap->encoding_name.slen);
+ p += rtpmap->encoding_name.slen;
+
+ /* slash, clock-rate. */
+ *p++ = '/';
+ len = pj_utoa(rtpmap->clock_rate, p);
+ p += len;
+
+ /* optionally add encoding parameter. */
+ if (rtpmap->parameter.slen) {
+ *p++ = '/';
+ pj_memcpy(p, rtpmap->parameter.ptr, rtpmap->parameter.slen);
+ p += rtpmap->parameter.slen;
+ }
+
+ return p-buf;
+}
+
+static int print_generic_string_attr(const pjsdp_attr_string *attr,
+ char *buf, int len)
+{
+ char *p = buf;
+
+ if (len < attr->value.slen + 4) {
+ return -1;
+ }
+
+ /* colon and attribute value. */
+ *p++ = ':';
+ pj_memcpy(p, attr->value.ptr, attr->value.slen);
+ p += attr->value.slen;
+
+ return p-buf;
+}
+
+static int print_generic_num_attr(const pjsdp_attr_num *attr, char *buf, int len)
+{
+ char *p = buf;
+
+ if (len < 10) {
+ return -1;
+ }
+ *p++ = ':';
+ return pj_utoa(attr->value, p);
+}
+
+static int print_name_only_attr(const pjsdp_attr *attr, char *buf, int len)
+{
+ PJ_UNUSED_ARG(attr)
+ PJ_UNUSED_ARG(buf)
+ PJ_UNUSED_ARG(len)
+ return 0;
+}
+
+static int print_fmtp_attr(const pjsdp_fmtp_attr *fmtp, char *buf, int len)
+{
+ char *p = buf;
+
+ if (len < 4+fmtp->format.slen+fmtp->param.slen) {
+ return -1;
+ }
+
+ /* colon and format. */
+ *p++ = ':';
+ pj_memcpy(p, fmtp->format.ptr, fmtp->format.slen);
+ p += fmtp->format.slen;
+
+ /* space and parameter. */
+ *p++ = ' ';
+ pj_memcpy(p, fmtp->param.ptr, fmtp->param.slen);
+ p += fmtp->param.slen;
+
+ return p-buf;
+}
+
+
+static int print_attr(const pjsdp_attr *attr, char *buf, int len)
+{
+ char *p = buf;
+ struct attr_map_rec *desc = &attr_map[attr->type];
+
+ if (len < 16) {
+ return -1;
+ }
+
+ *p++ = 'a';
+ *p++ = '=';
+ pj_memcpy(p, desc->name.ptr, desc->name.slen);
+ p += desc->name.slen;
+
+ len = (*desc->print_attr)(attr, p, (buf+len)-p);
+ if (len < 0) {
+ return -1;
+ }
+ p += len;
+ *p++ = '\r';
+ *p++ = '\n';
+ return p-buf;
+}
+
+static pjsdp_attr* clone_rtpmap_attr (pj_pool_t *pool, const pjsdp_attr *p)
+{
+ const pjsdp_rtpmap_attr *rhs = (const pjsdp_rtpmap_attr*)p;
+ pjsdp_rtpmap_attr *attr = pj_pool_alloc (pool, sizeof(pjsdp_rtpmap_attr));
+ if (!attr)
+ return NULL;
+
+ attr->type = rhs->type;
+ attr->payload_type = rhs->payload_type;
+ if (!pj_strdup (pool, &attr->encoding_name, &rhs->encoding_name)) return NULL;
+ attr->clock_rate = rhs->clock_rate;
+ if (!pj_strdup (pool, &attr->parameter, &rhs->parameter)) return NULL;
+
+ return (pjsdp_attr*)attr;
+}
+
+static pjsdp_attr* clone_generic_string_attr (pj_pool_t *pool, const pjsdp_attr *p)
+{
+ const pjsdp_attr_string* rhs = (const pjsdp_attr_string*) p;
+ pjsdp_attr_string *attr = pj_pool_alloc (pool, sizeof(pjsdp_attr_string));
+ if (!attr)
+ return NULL;
+
+ attr->type = rhs->type;
+ if (!pj_strdup (pool, &attr->value, &rhs->value)) return NULL;
+
+ return (pjsdp_attr*)attr;
+}
+
+static pjsdp_attr* clone_generic_num_attr (pj_pool_t *pool, const pjsdp_attr *p)
+{
+ const pjsdp_attr_num* rhs = (const pjsdp_attr_num*) p;
+ pjsdp_attr_num *attr = pj_pool_alloc (pool, sizeof(pjsdp_attr_num));
+ if (!attr)
+ return NULL;
+
+ attr->type = rhs->type;
+ attr->value = rhs->value;
+
+ return (pjsdp_attr*)attr;
+}
+
+static pjsdp_attr* clone_name_only_attr (pj_pool_t *pool, const pjsdp_attr *rhs)
+{
+ pjsdp_attr *attr = pj_pool_alloc (pool, sizeof(pjsdp_attr));
+ if (!attr)
+ return NULL;
+
+ attr->type = rhs->type;
+ return attr;
+}
+
+static pjsdp_attr* clone_fmtp_attr (pj_pool_t *pool, const pjsdp_attr *p)
+{
+ const pjsdp_fmtp_attr* rhs = (const pjsdp_fmtp_attr*) p;
+ pjsdp_fmtp_attr *attr = pj_pool_alloc (pool, sizeof(pjsdp_fmtp_attr));
+ if (!attr)
+ return NULL;
+
+ attr->type = rhs->type;
+ if (!pj_strdup (pool, &attr->format, &rhs->format)) return NULL;
+ if (!pj_strdup (pool, &attr->param, &rhs->param)) return NULL;
+
+ return (pjsdp_attr*)attr;
+}
+
+PJ_DEF(pjsdp_attr*) pjsdp_attr_clone (pj_pool_t *pool, const pjsdp_attr *rhs)
+{
+ struct attr_map_rec *desc;
+
+ if (rhs->type >= PJSDP_END_OF_ATTR) {
+ pj_assert(0);
+ return NULL;
+ }
+
+ desc = &attr_map[rhs->type];
+ return (*desc->clone) (pool, rhs);
+}
+
+PJ_DEF(const pjsdp_attr*) pjsdp_attr_find (int count, const pjsdp_attr *attr_array[], int type)
+{
+ int i;
+
+ for (i=0; i<count; ++i) {
+ if (attr_array[i]->type == type)
+ return attr_array[i];
+ }
+ return NULL;
+}
+
+static int print_connection_info( pjsdp_conn_info *c, char *buf, int len)
+{
+ char *p = buf;
+
+ if (len < 8+c->net_type.slen+c->addr_type.slen+c->addr.slen) {
+ return -1;
+ }
+ *p++ = 'c';
+ *p++ = '=';
+ pj_memcpy(p, c->net_type.ptr, c->net_type.slen);
+ p += c->net_type.slen;
+ *p++ = ' ';
+ pj_memcpy(p, c->addr_type.ptr, c->addr_type.slen);
+ p += c->addr_type.slen;
+ *p++ = ' ';
+ pj_memcpy(p, c->addr.ptr, c->addr.slen);
+ p += c->addr.slen;
+ *p++ = '\r';
+ *p++ = '\n';
+
+ return p-buf;
+}
+
+PJ_DEF(pjsdp_conn_info*) pjsdp_conn_info_clone (pj_pool_t *pool, const pjsdp_conn_info *rhs)
+{
+ pjsdp_conn_info *c = pj_pool_alloc (pool, sizeof(pjsdp_conn_info));
+ if (!c) return NULL;
+
+ if (!pj_strdup (pool, &c->net_type, &rhs->net_type)) return NULL;
+ if (!pj_strdup (pool, &c->addr_type, &rhs->addr_type)) return NULL;
+ if (!pj_strdup (pool, &c->addr, &rhs->addr)) return NULL;
+
+ return c;
+}
+
+static int print_media_desc( pjsdp_media_desc *m, char *buf, int len)
+{
+ char *p = buf;
+ char *end = buf+len;
+ unsigned i;
+ int printed;
+
+ /* check length for the "m=" line. */
+ if (len < m->desc.media.slen+m->desc.transport.slen+12+24) {
+ return -1;
+ }
+ *p++ = 'm'; /* m= */
+ *p++ = '=';
+ pj_memcpy(p, m->desc.media.ptr, m->desc.media.slen);
+ p += m->desc.media.slen;
+ *p++ = ' ';
+ printed = pj_utoa(m->desc.port, p);
+ p += printed;
+ if (m->desc.port_count > 1) {
+ *p++ = '/';
+ printed = pj_utoa(m->desc.port_count, p);
+ p += printed;
+ }
+ *p++ = ' ';
+ pj_memcpy(p, m->desc.transport.ptr, m->desc.transport.slen);
+ p += m->desc.transport.slen;
+ for (i=0; i<m->desc.fmt_count; ++i) {
+ *p++ = ' ';
+ pj_memcpy(p, m->desc.fmt[i].ptr, m->desc.fmt[i].slen);
+ p += m->desc.fmt[i].slen;
+ }
+ *p++ = '\r';
+ *p++ = '\n';
+
+ /* print connection info, if present. */
+ if (m->conn) {
+ printed = print_connection_info(m->conn, p, end-p);
+ if (printed < 0) {
+ return -1;
+ }
+ p += printed;
+ }
+
+ /* print attributes. */
+ for (i=0; i<m->attr_count; ++i) {
+ printed = print_attr(m->attr[i], p, end-p);
+ if (printed < 0) {
+ return -1;
+ }
+ p += printed;
+ }
+
+ return p-buf;
+}
+
+PJ_DEF(pjsdp_media_desc*) pjsdp_media_desc_clone (pj_pool_t *pool,
+ const pjsdp_media_desc *rhs)
+{
+ unsigned int i;
+ pjsdp_media_desc *m = pj_pool_alloc (pool, sizeof(pjsdp_media_desc));
+ if (!m)
+ return NULL;
+
+ pj_strdup (pool, &m->desc.media, &rhs->desc.media);
+ m->desc.port = rhs->desc.port;
+ m->desc.port_count = rhs->desc.port_count;
+ pj_strdup (pool, &m->desc.transport, &rhs->desc.transport);
+ m->desc.fmt_count = rhs->desc.fmt_count;
+ for (i=0; i<rhs->desc.fmt_count; ++i)
+ m->desc.fmt[i] = rhs->desc.fmt[i];
+
+ if (rhs->conn) {
+ m->conn = pjsdp_conn_info_clone (pool, rhs->conn);
+ if (!m->conn)
+ return NULL;
+ } else {
+ m->conn = NULL;
+ }
+
+ m->attr_count = rhs->attr_count;
+ for (i=0; i < rhs->attr_count; ++i) {
+ m->attr[i] = pjsdp_attr_clone (pool, rhs->attr[i]);
+ if (!m->attr[i])
+ return NULL;
+ }
+
+ return m;
+}
+
+/** Check if the media description has the specified attribute. */
+PJ_DEF(pj_bool_t) pjsdp_media_desc_has_attr (const pjsdp_media_desc *m,
+ pjsdp_attr_type_e attr_type)
+{
+ unsigned i;
+ for (i=0; i<m->attr_count; ++i) {
+ pjsdp_attr *attr = m->attr[i];
+ if (attr->type == attr_type)
+ return 1;
+ }
+ return 0;
+}
+
+/** Find rtpmap attribute for the specified payload type. */
+PJ_DEF(const pjsdp_rtpmap_attr*)
+pjsdp_media_desc_find_rtpmap (const pjsdp_media_desc *m, unsigned pt)
+{
+ unsigned i;
+ for (i=0; i<m->attr_count; ++i) {
+ pjsdp_attr *attr = m->attr[i];
+ if (attr->type == PJSDP_ATTR_RTPMAP) {
+ const pjsdp_rtpmap_attr* rtpmap = (const pjsdp_rtpmap_attr*)attr;
+ if (rtpmap->payload_type == pt)
+ return rtpmap;
+ }
+ }
+ return NULL;
+}
+
+
+static int print_session(const pjsdp_session_desc *ses, char *buf, pj_ssize_t len)
+{
+ char *p = buf;
+ char *end = buf+len;
+ unsigned i;
+ int printed;
+
+ /* Check length for v= and o= lines. */
+ if (len < 5+
+ 2+ses->origin.user.slen+18+
+ ses->origin.net_type.slen+ses->origin.addr.slen + 2)
+ {
+ return -1;
+ }
+
+ /* SDP version (v= line) */
+ pj_memcpy(p, "v=0\r\n", 5);
+ p += 5;
+
+ /* Owner (o=) line. */
+ *p++ = 'o';
+ *p++ = '=';
+ pj_memcpy(p, ses->origin.user.ptr, ses->origin.user.slen);
+ p += ses->origin.user.slen;
+ *p++ = ' ';
+ printed = pj_utoa(ses->origin.id, p);
+ p += printed;
+ *p++ = ' ';
+ printed = pj_utoa(ses->origin.version, p);
+ p += printed;
+ *p++ = ' ';
+ pj_memcpy(p, ses->origin.net_type.ptr, ses->origin.net_type.slen);
+ p += ses->origin.net_type.slen;
+ *p++ = ' ';
+ pj_memcpy(p, ses->origin.addr_type.ptr, ses->origin.addr_type.slen);
+ p += ses->origin.addr_type.slen;
+ *p++ = ' ';
+ pj_memcpy(p, ses->origin.addr.ptr, ses->origin.addr.slen);
+ p += ses->origin.addr.slen;
+ *p++ = '\r';
+ *p++ = '\n';
+
+ /* Session name (s=) line. */
+ if ((end-p) < 8+ses->name.slen) {
+ return -1;
+ }
+ *p++ = 's';
+ *p++ = '=';
+ pj_memcpy(p, ses->name.ptr, ses->name.slen);
+ p += ses->name.slen;
+ *p++ = '\r';
+ *p++ = '\n';
+
+ /* Time */
+ if ((end-p) < 24) {
+ return -1;
+ }
+ *p++ = 't';
+ *p++ = '=';
+ printed = pj_utoa(ses->time.start, p);
+ p += printed;
+ *p++ = ' ';
+ printed = pj_utoa(ses->time.stop, p);
+ p += printed;
+ *p++ = '\r';
+ *p++ = '\n';
+
+ /* Connection line (c=) if exist. */
+ if (ses->conn) {
+ printed = print_connection_info(ses->conn, p, end-p);
+ if (printed < 1) {
+ return -1;
+ }
+ p += printed;
+ }
+
+ /* Print all attribute (a=) lines. */
+ for (i=0; i<ses->attr_count; ++i) {
+ printed = print_attr(ses->attr[i], p, end-p);
+ if (printed < 0) {
+ return -1;
+ }
+ p += printed;
+ }
+
+ /* Print media (m=) lines. */
+ for (i=0; i<ses->media_count; ++i) {
+ printed = print_media_desc(ses->media[i], p, end-p);
+ if (printed < 0) {
+ return -1;
+ }
+ p += printed;
+ }
+
+ return p-buf;
+}
+
+/******************************************************************************
+ * PARSERS
+ */
+
+static void parse_version(pj_scanner *scanner)
+{
+ pj_scan_advance_n(scanner, 3, SKIP_WS);
+ pj_scan_get_newline(scanner);
+}
+
+static void parse_origin(pj_scanner *scanner, pjsdp_session_desc *ses)
+{
+ pj_str_t str;
+
+ /* o= */
+ pj_scan_advance_n(scanner, 2, SKIP_WS);
+
+ /* username. */
+ pj_scan_get_until_ch(scanner, ' ', &ses->origin.user);
+ pj_scan_get_char(scanner);
+
+ /* id */
+ pj_scan_get_until_ch(scanner, ' ', &str);
+ ses->origin.id = pj_strtoul(&str);
+ pj_scan_get_char(scanner);
+
+ /* version */
+ pj_scan_get_until_ch(scanner, ' ', &str);
+ ses->origin.version = pj_strtoul(&str);
+ pj_scan_get_char(scanner);
+
+ /* network-type */
+ pj_scan_get_until_ch(scanner, ' ', &ses->origin.net_type);
+ pj_scan_get_char(scanner);
+
+ /* addr-type */
+ pj_scan_get_until_ch(scanner, ' ', &ses->origin.addr_type);
+ pj_scan_get_char(scanner);
+
+ /* address */
+ pj_scan_get_until_ch(scanner, '\r', &ses->origin.addr);
+
+ /* newline */
+ pj_scan_get_newline(scanner);
+}
+
+static void parse_time(pj_scanner *scanner, pjsdp_session_desc *ses)
+{
+ pj_str_t str;
+
+ /* t= */
+ pj_scan_advance_n(scanner, 2, SKIP_WS);
+
+ /* start time */
+ pj_scan_get_until_ch(scanner, ' ', &str);
+ ses->time.start = pj_strtoul(&str);
+
+ pj_scan_get_char(scanner);
+
+ /* stop time */
+ pj_scan_get_until_ch(scanner, '\r', &str);
+ ses->time.stop = pj_strtoul(&str);
+
+ /* newline */
+ pj_scan_get_newline(scanner);
+}
+
+static void parse_generic_line(pj_scanner *scanner, pj_str_t *str)
+{
+ /* x= */
+ pj_scan_advance_n(scanner, 2, SKIP_WS);
+
+ /* get anything until newline. */
+ pj_scan_get_until_ch(scanner, '\r', str);
+
+ /* newline. */
+ pj_scan_get_newline(scanner);
+}
+
+static void parse_connection_info(pj_scanner *scanner, pjsdp_conn_info *conn)
+{
+ /* c= */
+ pj_scan_advance_n(scanner, 2, SKIP_WS);
+
+ /* network-type */
+ pj_scan_get_until_ch(scanner, ' ', &conn->net_type);
+ pj_scan_get_char(scanner);
+
+ /* addr-type */
+ pj_scan_get_until_ch(scanner, ' ', &conn->addr_type);
+ pj_scan_get_char(scanner);
+
+ /* address. */
+ pj_scan_get_until_ch(scanner, '\r', &conn->addr);
+
+ /* newline */
+ pj_scan_get_newline(scanner);
+}
+
+static void parse_media(pj_scanner *scanner, pjsdp_media_desc *med)
+{
+ pj_str_t str;
+
+ /* m= */
+ pj_scan_advance_n(scanner, 2, SKIP_WS);
+
+ /* type */
+ pj_scan_get_until_ch(scanner, ' ', &med->desc.media);
+ pj_scan_get_char(scanner);
+
+ /* port */
+ pj_scan_get(scanner, cs_token, &str);
+ med->desc.port = (unsigned short)pj_strtoul(&str);
+ if (*scanner->current == '/') {
+ /* port count */
+ pj_scan_get_char(scanner);
+ pj_scan_get(scanner, cs_token, &str);
+ med->desc.port_count = pj_strtoul(&str);
+
+ } else {
+ med->desc.port_count = 0;
+ }
+
+ if (pj_scan_get_char(scanner) != ' ') {
+ PJ_THROW(SYNTAX_ERROR);
+ }
+
+ /* transport */
+ pj_scan_get_until_ch(scanner, ' ', &med->desc.transport);
+
+ /* format list */
+ med->desc.fmt_count = 0;
+ while (*scanner->current == ' ') {
+ pj_scan_get_char(scanner);
+ pj_scan_get(scanner, cs_token, &med->desc.fmt[med->desc.fmt_count++]);
+ }
+
+ /* newline */
+ pj_scan_get_newline(scanner);
+}
+
+static pjsdp_rtpmap_attr * parse_rtpmap_attr( pj_pool_t *pool, pj_scanner *scanner )
+{
+ pjsdp_rtpmap_attr *rtpmap;
+ pj_str_t str;
+
+ rtpmap = pj_pool_calloc(pool, 1, sizeof(*rtpmap));
+ if (pj_scan_get_char(scanner) != ':') {
+ PJ_THROW(SYNTAX_ERROR);
+ }
+ pj_scan_get_until_ch(scanner, ' ', &str);
+ rtpmap->payload_type = pj_strtoul(&str);
+ pj_scan_get_char(scanner);
+
+ pj_scan_get_until_ch(scanner, '/', &rtpmap->encoding_name);
+ pj_scan_get_char(scanner);
+ pj_scan_get(scanner, cs_token, &str);
+ rtpmap->clock_rate = pj_strtoul(&str);
+
+ if (*scanner->current == '/') {
+ pj_scan_get_char(scanner);
+ pj_scan_get_until_ch(scanner, '\r', &rtpmap->parameter);
+ }
+
+ return rtpmap;
+}
+
+static pjsdp_attr_string * parse_generic_string_attr( pj_pool_t *pool, pj_scanner *scanner )
+{
+ pjsdp_attr_string *attr;
+ attr = pj_pool_calloc(pool, 1, sizeof(*attr));
+
+ if (pj_scan_get_char(scanner) != ':') {
+ PJ_THROW(SYNTAX_ERROR);
+ }
+ pj_scan_get_until_ch(scanner, '\r', &attr->value);
+ return attr;
+}
+
+static pjsdp_attr_num * parse_generic_num_attr( pj_pool_t *pool, pj_scanner *scanner )
+{
+ pjsdp_attr_num *attr;
+ pj_str_t str;
+
+ attr = pj_pool_calloc(pool, 1, sizeof(*attr));
+
+ if (pj_scan_get_char(scanner) != ':') {
+ PJ_THROW(SYNTAX_ERROR);
+ }
+ pj_scan_get_until_ch(scanner, '\r', &str);
+ attr->value = pj_strtoul(&str);
+ return attr;
+}
+
+static pjsdp_attr * parse_name_only_attr( pj_pool_t *pool, pj_scanner *scanner )
+{
+ pjsdp_attr *attr;
+
+ PJ_UNUSED_ARG(scanner)
+ attr = pj_pool_calloc(pool, 1, sizeof(*attr));
+ return attr;
+}
+
+static pjsdp_fmtp_attr * parse_fmtp_attr( pj_pool_t *pool, pj_scanner *scanner )
+{
+ pjsdp_fmtp_attr *fmtp;
+
+ fmtp = pj_pool_calloc(pool, 1, sizeof(*fmtp));
+
+ if (pj_scan_get_char(scanner) != ':') {
+ PJ_THROW(SYNTAX_ERROR);
+ }
+ pj_scan_get_until_ch(scanner, ' ', &fmtp->format);
+ pj_scan_get_char(scanner);
+ pj_scan_get_until_ch(scanner, '\r', &fmtp->param);
+ return fmtp;
+}
+
+static pjsdp_attr *parse_attr( pj_pool_t *pool, pj_scanner *scanner)
+{
+ void * (*parse_func)(pj_pool_t *pool, pj_scanner *scanner) = NULL;
+ pj_str_t attrname;
+ unsigned i;
+ pjsdp_attr *attr;
+
+ /* skip a= */
+ pj_scan_advance_n(scanner, 2, SKIP_WS);
+
+ /* get attr name. */
+ pj_scan_get(scanner, cs_token, &attrname);
+
+ /* find entry to handle attrname */
+ for (i=0; i<PJ_ARRAY_SIZE(attr_map); ++i) {
+ struct attr_map_rec *p = &attr_map[i];
+ if (pj_strcmp(&attrname, &p->name) == 0) {
+ parse_func = p->parse_attr;
+ break;
+ }
+ }
+
+ /* fallback to generic string parser. */
+ if (parse_func == NULL) {
+ parse_func = &parse_generic_string_attr;
+ }
+
+ attr = (*parse_func)(pool, scanner);
+ attr->type = i;
+
+ /* newline */
+ pj_scan_get_newline(scanner);
+
+ return attr;
+}
+
+static void on_scanner_error(pj_scanner *scanner)
+{
+ PJ_UNUSED_ARG(scanner)
+
+ PJ_THROW(SYNTAX_ERROR);
+}
+
+/*
+ * Parse SDP message.
+ */
+PJ_DEF(pjsdp_session_desc*) pjsdp_parse( char *buf, pj_size_t len,
+ pj_pool_t *pool)
+{
+ pj_scanner scanner;
+ pjsdp_session_desc *session;
+ pjsdp_media_desc *media = NULL;
+ void *attr;
+ pjsdp_conn_info *conn;
+ pj_str_t dummy;
+ int cur_name = 254;
+ PJ_USE_EXCEPTION;
+
+ init_sdp_parser();
+
+ pj_scan_init(&scanner, buf, len, 0, &on_scanner_error);
+ session = pj_pool_calloc(pool, 1, sizeof(*session));
+
+ PJ_TRY {
+ while (!pj_scan_is_eof(&scanner)) {
+ cur_name = *scanner.current;
+ switch (cur_name) {
+ case 'a':
+ attr = parse_attr(pool, &scanner);
+ if (attr) {
+ if (media) {
+ media->attr[media->attr_count++] = attr;
+ } else {
+ session->attr[session->attr_count++] = attr;
+ }
+ }
+ break;
+ case 'o':
+ parse_origin(&scanner, session);
+ break;
+ case 's':
+ parse_generic_line(&scanner, &session->name);
+ break;
+ case 'c':
+ conn = pj_pool_calloc(pool, 1, sizeof(*conn));
+ parse_connection_info(&scanner, conn);
+ if (media) {
+ media->conn = conn;
+ } else {
+ session->conn = conn;
+ }
+ break;
+ case 't':
+ parse_time(&scanner, session);
+ break;
+ case 'm':
+ media = pj_pool_calloc(pool, 1, sizeof(*media));
+ parse_media(&scanner, media);
+ session->media[ session->media_count++ ] = media;
+ break;
+ case 'v':
+ parse_version(&scanner);
+ break;
+ default:
+ parse_generic_line(&scanner, &dummy);
+ break;
+ }
+ }
+ }
+ PJ_CATCH(SYNTAX_ERROR) {
+ PJ_LOG(2, (LOG_THIS, "Syntax error in SDP parser '%c' line %d col %d",
+ cur_name, scanner.line, scanner.col));
+ if (!pj_scan_is_eof(&scanner)) {
+ if (*scanner.current != '\r') {
+ pj_scan_get_until_ch(&scanner, '\r', &dummy);
+ }
+ pj_scan_get_newline(&scanner);
+ }
+ }
+ PJ_END;
+
+ pj_scan_fini(&scanner);
+ return session;
+}
+
+/*
+ * Print SDP description.
+ */
+PJ_DEF(int) pjsdp_print( const pjsdp_session_desc *desc, char *buf, pj_size_t size)
+{
+ return print_session(desc, buf, size);
+}
+
+
diff --git a/pjmedia/src/pjmedia/sdp.h b/pjmedia/src/pjmedia/sdp.h
index 0adfe7f4..1b45744c 100644
--- a/pjmedia/src/pjmedia/sdp.h
+++ b/pjmedia/src/pjmedia/sdp.h
@@ -1,332 +1,332 @@
-/* $Id$ */
-/*
- * Copyright (C) 2003-2006 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 __PJSDP_SDP_H__
-#define __PJSDP_SDP_H__
-
-/**
- * @defgroup PJSDP SDP Library
- */
-/**
- * @file sdp.h
- * @brief SDP header file.
- */
-/**
- * @defgroup PJ_SDP_H SDP stack.
- * @ingroup PJSDP
- * @{
- *
- * This SDP module consists of SDP parser, SDP structure, and function to
- * print back the structure as SDP message.
- */
-
-#include <pj/types.h>
-
-PJ_BEGIN_DECL
-
-#define PJSDP_MAX_FMT 32
-#define PJSDP_MAX_ATTR 32
-#define PJSDP_MAX_MEDIA 16
-
-/**
- * This enumeration describes the attribute type.
- */
-typedef enum pjsdp_attr_type_e
-{
- PJSDP_ATTR_RTPMAP,
- PJSDP_ATTR_CAT,
- PJSDP_ATTR_KEYWORDS,
- PJSDP_ATTR_TOOL,
- PJSDP_ATTR_PTIME,
- PJSDP_ATTR_RECV_ONLY,
- PJSDP_ATTR_SEND_ONLY,
- PJSDP_ATTR_SEND_RECV,
- PJSDP_ATTR_ORIENT,
- PJSDP_ATTR_TYPE,
- PJSDP_ATTR_CHARSET,
- PJSDP_ATTR_SDP_LANG,
- PJSDP_ATTR_LANG,
- PJSDP_ATTR_FRAME_RATE,
- PJSDP_ATTR_QUALITY,
- PJSDP_ATTR_FMTP,
- PJSDP_ATTR_INACTIVE,
- PJSDP_ATTR_GENERIC,
- PJSDP_END_OF_ATTR,
-} pjsdp_attr_type_e;
-
-
-/**
- * This structure keeps the common attributes that all 'descendants'
- * will have.
- */
-typedef struct pjsdp_attr
-{
- pjsdp_attr_type_e type; /**< Attribute type. */
-} pjsdp_attr;
-
-
-/**
- * This is the structure to represent generic attribute which has a
- * string value.
- */
-typedef struct pjsdp_attr_string
-{
- pjsdp_attr_type_e type;
- pj_str_t value;
-} pjsdp_attr_string;
-
-
-/**
- * This is the structure to represent generic SDP attribute which has
- * a numeric value.
- */
-typedef struct pjsdp_attr_num
-{
- pjsdp_attr_type_e type;
- pj_uint32_t value;
-} pjsdp_attr_num;
-
-
-/**
- * SDP \a rtpmap attribute.
- */
-typedef struct pjsdp_rtpmap_attr
-{
- pjsdp_attr_type_e type;
- unsigned payload_type;
- pj_str_t encoding_name;
- unsigned clock_rate;
- pj_str_t parameter;
-} pjsdp_rtpmap_attr;
-
-
-/**
- * SDP \a fmtp attribute.
- */
-typedef struct pjsdp_fmtp_attr
-{
- pjsdp_attr_type_e type;
- pj_str_t format;
- pj_str_t param;
-} pjsdp_fmtp_attr;
-
-
-/**
- * SDP generic attribute.
- */
-typedef struct pjsdp_generic_attr
-{
- pjsdp_attr_type_e type;
- pj_str_t name;
- pj_str_t value;
-} pjsdp_generic_attr;
-
-
-/** SDP \a cat attribute. */
-typedef struct pjsdp_attr_string pjsdp_cat_attr;
-
-/** SDP \a keywds attribute. */
-typedef struct pjsdp_attr_string pjsdp_keywds_attr;
-
-/** SDP \a tool attribute. */
-typedef struct pjsdp_attr_string pjsdp_tool_attr;
-
-/** SDP \a ptime attribute. */
-typedef struct pjsdp_attr_num pjsdp_ptime_attr;
-
-/** SDP \a recvonly attribute. */
-typedef struct pjsdp_attr pjsdp_recv_only_attr;
-
-/** SDP \a sendonly attribute. */
-typedef struct pjsdp_attr pjsdp_send_only_attr;
-
-/** SDP \a sendrecv attribute. */
-typedef struct pjsdp_attr pjsdp_send_recv_attr;
-
-/** SDP \a orient attribute. */
-typedef struct pjsdp_attr_string pjsdp_orient_attr;
-
-/** SDP \a type attribute. */
-typedef struct pjsdp_attr_string pjsdp_type_attr;
-
-/** SDP \a charset attribute. */
-typedef struct pjsdp_attr_string pjsdp_charset_attr;
-
-/** SDP \a sdplang attribute. */
-typedef struct pjsdp_attr_string pjsdp_sdp_lang_attr;
-
-/** SDP \a lang attribute. */
-typedef struct pjsdp_attr_string pjsdp_lang_attr;
-
-/** SDP \a framerate attribute. */
-typedef struct pjsdp_attr_string pjsdp_frame_rate_attr;
-
-/** SDP \a quality attribute. */
-typedef struct pjsdp_attr_num pjsdp_quality_attr;
-
-/** SDP \a inactive attribute. */
-typedef struct pjsdp_attr pjsdp_inactive_attr;
-
-/** Clone attribute */
-PJ_DECL(pjsdp_attr*) pjsdp_attr_clone (pj_pool_t *pool, const pjsdp_attr *rhs);
-
-/** Find attribute */
-PJ_DECL(const pjsdp_attr*) pjsdp_attr_find (int count, const pjsdp_attr *attr_array[], int type);
-
-/**
- * SDP connection info.
- */
-typedef struct pjsdp_conn_info
-{
- pj_str_t net_type;
- pj_str_t addr_type;
- pj_str_t addr;
-} pjsdp_conn_info;
-
-/**
- *Clone connection info.
- *
- * @param pool Pool to allocate memory for the new connection info.
- * @param rhs The connection into to clone.
- *
- * @return the new connection info.
- */
-PJ_DECL(pjsdp_conn_info*) pjsdp_conn_info_clone (pj_pool_t *pool,
- const pjsdp_conn_info *rhs);
-
-/**
- * SDP media description.
- */
-typedef struct pjsdp_media_desc
-{
- struct
- {
- pj_str_t media;
- pj_uint16_t port;
- unsigned port_count;
- pj_str_t transport;
- unsigned fmt_count;
- pj_str_t fmt[PJSDP_MAX_FMT];
- } desc;
-
- pjsdp_conn_info *conn;
- unsigned attr_count;
- pjsdp_attr *attr[PJSDP_MAX_ATTR];
-
-} pjsdp_media_desc;
-
-/**
- * Clone SDP media description.
- *
- * @param pool Pool to allocate memory for the new media description.
- * @param rhs The media descriptin to clone.
- *
- * @return a new media description.
- */
-PJ_DECL(pjsdp_media_desc*) pjsdp_media_desc_clone (pj_pool_t *pool,
- const pjsdp_media_desc *rhs);
-
-/**
- * Check if the media description has the specified attribute.
- *
- * @param m The SDP media description.
- * @param attr_type The attribute type.
- *
- * @return nonzero if true.
- */
-PJ_DECL(pj_bool_t) pjsdp_media_desc_has_attr (const pjsdp_media_desc *m,
- pjsdp_attr_type_e attr_type);
-
-/**
- * Find rtpmap attribute for the specified payload type.
- *
- * @param m The SDP media description.
- * @param pt RTP payload type.
- *
- * @return the SDP rtpmap attribute for the payload type, or NULL if not found.
- */
-PJ_DECL(const pjsdp_rtpmap_attr*)
-pjsdp_media_desc_find_rtpmap (const pjsdp_media_desc *m, unsigned pt);
-
-
-/**
- * This structure describes SDP session description.
- */
-typedef struct pjsdp_session_desc
-{
- struct
- {
- pj_str_t user;
- pj_uint32_t id;
- pj_uint32_t version;
- pj_str_t net_type;
- pj_str_t addr_type;
- pj_str_t addr;
- } origin;
-
- pj_str_t name;
- pjsdp_conn_info *conn;
-
- struct
- {
- pj_uint32_t start;
- pj_uint32_t stop;
- } time;
-
- unsigned attr_count;
- pjsdp_attr *attr[PJSDP_MAX_ATTR];
-
- unsigned media_count;
- pjsdp_media_desc *media[PJSDP_MAX_MEDIA];
-
-} pjsdp_session_desc;
-
-
-/**
- * Parse SDP message.
- *
- * @param buf The message buffer.
- * @param len The length of the message.
- * @param pool The pool to allocate SDP session description.
- *
- * @return SDP session description.
- */
-PJ_DECL(pjsdp_session_desc*) pjsdp_parse( char *buf, pj_size_t len,
- pj_pool_t *pool);
-
-/**
- * Print SDP description to a buffer.
- *
- * @param buf The buffer.
- * @param size The buffer length.
- * @param desc The SDP session description.
- *
- * @return the length printed, or -1.
- */
-PJ_DECL(int) pjsdp_print( const pjsdp_session_desc *desc,
- char *buf, pj_size_t size);
-
-
-/**
- * @}
- */
-
-PJ_END_DECL
-
-#endif /* __PJSDP_SDP_H__ */
-
+/* $Id$ */
+/*
+ * Copyright (C) 2003-2006 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 __PJSDP_SDP_H__
+#define __PJSDP_SDP_H__
+
+/**
+ * @defgroup PJSDP SDP Library
+ */
+/**
+ * @file sdp.h
+ * @brief SDP header file.
+ */
+/**
+ * @defgroup PJ_SDP_H SDP stack.
+ * @ingroup PJSDP
+ * @{
+ *
+ * This SDP module consists of SDP parser, SDP structure, and function to
+ * print back the structure as SDP message.
+ */
+
+#include <pj/types.h>
+
+PJ_BEGIN_DECL
+
+#define PJSDP_MAX_FMT 32
+#define PJSDP_MAX_ATTR 32
+#define PJSDP_MAX_MEDIA 16
+
+/**
+ * This enumeration describes the attribute type.
+ */
+typedef enum pjsdp_attr_type_e
+{
+ PJSDP_ATTR_RTPMAP,
+ PJSDP_ATTR_CAT,
+ PJSDP_ATTR_KEYWORDS,
+ PJSDP_ATTR_TOOL,
+ PJSDP_ATTR_PTIME,
+ PJSDP_ATTR_RECV_ONLY,
+ PJSDP_ATTR_SEND_ONLY,
+ PJSDP_ATTR_SEND_RECV,
+ PJSDP_ATTR_ORIENT,
+ PJSDP_ATTR_TYPE,
+ PJSDP_ATTR_CHARSET,
+ PJSDP_ATTR_SDP_LANG,
+ PJSDP_ATTR_LANG,
+ PJSDP_ATTR_FRAME_RATE,
+ PJSDP_ATTR_QUALITY,
+ PJSDP_ATTR_FMTP,
+ PJSDP_ATTR_INACTIVE,
+ PJSDP_ATTR_GENERIC,
+ PJSDP_END_OF_ATTR,
+} pjsdp_attr_type_e;
+
+
+/**
+ * This structure keeps the common attributes that all 'descendants'
+ * will have.
+ */
+typedef struct pjsdp_attr
+{
+ pjsdp_attr_type_e type; /**< Attribute type. */
+} pjsdp_attr;
+
+
+/**
+ * This is the structure to represent generic attribute which has a
+ * string value.
+ */
+typedef struct pjsdp_attr_string
+{
+ pjsdp_attr_type_e type;
+ pj_str_t value;
+} pjsdp_attr_string;
+
+
+/**
+ * This is the structure to represent generic SDP attribute which has
+ * a numeric value.
+ */
+typedef struct pjsdp_attr_num
+{
+ pjsdp_attr_type_e type;
+ pj_uint32_t value;
+} pjsdp_attr_num;
+
+
+/**
+ * SDP \a rtpmap attribute.
+ */
+typedef struct pjsdp_rtpmap_attr
+{
+ pjsdp_attr_type_e type;
+ unsigned payload_type;
+ pj_str_t encoding_name;
+ unsigned clock_rate;
+ pj_str_t parameter;
+} pjsdp_rtpmap_attr;
+
+
+/**
+ * SDP \a fmtp attribute.
+ */
+typedef struct pjsdp_fmtp_attr
+{
+ pjsdp_attr_type_e type;
+ pj_str_t format;
+ pj_str_t param;
+} pjsdp_fmtp_attr;
+
+
+/**
+ * SDP generic attribute.
+ */
+typedef struct pjsdp_generic_attr
+{
+ pjsdp_attr_type_e type;
+ pj_str_t name;
+ pj_str_t value;
+} pjsdp_generic_attr;
+
+
+/** SDP \a cat attribute. */
+typedef struct pjsdp_attr_string pjsdp_cat_attr;
+
+/** SDP \a keywds attribute. */
+typedef struct pjsdp_attr_string pjsdp_keywds_attr;
+
+/** SDP \a tool attribute. */
+typedef struct pjsdp_attr_string pjsdp_tool_attr;
+
+/** SDP \a ptime attribute. */
+typedef struct pjsdp_attr_num pjsdp_ptime_attr;
+
+/** SDP \a recvonly attribute. */
+typedef struct pjsdp_attr pjsdp_recv_only_attr;
+
+/** SDP \a sendonly attribute. */
+typedef struct pjsdp_attr pjsdp_send_only_attr;
+
+/** SDP \a sendrecv attribute. */
+typedef struct pjsdp_attr pjsdp_send_recv_attr;
+
+/** SDP \a orient attribute. */
+typedef struct pjsdp_attr_string pjsdp_orient_attr;
+
+/** SDP \a type attribute. */
+typedef struct pjsdp_attr_string pjsdp_type_attr;
+
+/** SDP \a charset attribute. */
+typedef struct pjsdp_attr_string pjsdp_charset_attr;
+
+/** SDP \a sdplang attribute. */
+typedef struct pjsdp_attr_string pjsdp_sdp_lang_attr;
+
+/** SDP \a lang attribute. */
+typedef struct pjsdp_attr_string pjsdp_lang_attr;
+
+/** SDP \a framerate attribute. */
+typedef struct pjsdp_attr_string pjsdp_frame_rate_attr;
+
+/** SDP \a quality attribute. */
+typedef struct pjsdp_attr_num pjsdp_quality_attr;
+
+/** SDP \a inactive attribute. */
+typedef struct pjsdp_attr pjsdp_inactive_attr;
+
+/** Clone attribute */
+PJ_DECL(pjsdp_attr*) pjsdp_attr_clone (pj_pool_t *pool, const pjsdp_attr *rhs);
+
+/** Find attribute */
+PJ_DECL(const pjsdp_attr*) pjsdp_attr_find (int count, const pjsdp_attr *attr_array[], int type);
+
+/**
+ * SDP connection info.
+ */
+typedef struct pjsdp_conn_info
+{
+ pj_str_t net_type;
+ pj_str_t addr_type;
+ pj_str_t addr;
+} pjsdp_conn_info;
+
+/**
+ *Clone connection info.
+ *
+ * @param pool Pool to allocate memory for the new connection info.
+ * @param rhs The connection into to clone.
+ *
+ * @return the new connection info.
+ */
+PJ_DECL(pjsdp_conn_info*) pjsdp_conn_info_clone (pj_pool_t *pool,
+ const pjsdp_conn_info *rhs);
+
+/**
+ * SDP media description.
+ */
+typedef struct pjsdp_media_desc
+{
+ struct
+ {
+ pj_str_t media;
+ pj_uint16_t port;
+ unsigned port_count;
+ pj_str_t transport;
+ unsigned fmt_count;
+ pj_str_t fmt[PJSDP_MAX_FMT];
+ } desc;
+
+ pjsdp_conn_info *conn;
+ unsigned attr_count;
+ pjsdp_attr *attr[PJSDP_MAX_ATTR];
+
+} pjsdp_media_desc;
+
+/**
+ * Clone SDP media description.
+ *
+ * @param pool Pool to allocate memory for the new media description.
+ * @param rhs The media descriptin to clone.
+ *
+ * @return a new media description.
+ */
+PJ_DECL(pjsdp_media_desc*) pjsdp_media_desc_clone (pj_pool_t *pool,
+ const pjsdp_media_desc *rhs);
+
+/**
+ * Check if the media description has the specified attribute.
+ *
+ * @param m The SDP media description.
+ * @param attr_type The attribute type.
+ *
+ * @return nonzero if true.
+ */
+PJ_DECL(pj_bool_t) pjsdp_media_desc_has_attr (const pjsdp_media_desc *m,
+ pjsdp_attr_type_e attr_type);
+
+/**
+ * Find rtpmap attribute for the specified payload type.
+ *
+ * @param m The SDP media description.
+ * @param pt RTP payload type.
+ *
+ * @return the SDP rtpmap attribute for the payload type, or NULL if not found.
+ */
+PJ_DECL(const pjsdp_rtpmap_attr*)
+pjsdp_media_desc_find_rtpmap (const pjsdp_media_desc *m, unsigned pt);
+
+
+/**
+ * This structure describes SDP session description.
+ */
+typedef struct pjsdp_session_desc
+{
+ struct
+ {
+ pj_str_t user;
+ pj_uint32_t id;
+ pj_uint32_t version;
+ pj_str_t net_type;
+ pj_str_t addr_type;
+ pj_str_t addr;
+ } origin;
+
+ pj_str_t name;
+ pjsdp_conn_info *conn;
+
+ struct
+ {
+ pj_uint32_t start;
+ pj_uint32_t stop;
+ } time;
+
+ unsigned attr_count;
+ pjsdp_attr *attr[PJSDP_MAX_ATTR];
+
+ unsigned media_count;
+ pjsdp_media_desc *media[PJSDP_MAX_MEDIA];
+
+} pjsdp_session_desc;
+
+
+/**
+ * Parse SDP message.
+ *
+ * @param buf The message buffer.
+ * @param len The length of the message.
+ * @param pool The pool to allocate SDP session description.
+ *
+ * @return SDP session description.
+ */
+PJ_DECL(pjsdp_session_desc*) pjsdp_parse( char *buf, pj_size_t len,
+ pj_pool_t *pool);
+
+/**
+ * Print SDP description to a buffer.
+ *
+ * @param buf The buffer.
+ * @param size The buffer length.
+ * @param desc The SDP session description.
+ *
+ * @return the length printed, or -1.
+ */
+PJ_DECL(int) pjsdp_print( const pjsdp_session_desc *desc,
+ char *buf, pj_size_t size);
+
+
+/**
+ * @}
+ */
+
+PJ_END_DECL
+
+#endif /* __PJSDP_SDP_H__ */
+
diff --git a/pjmedia/src/pjmedia/session.c b/pjmedia/src/pjmedia/session.c
index 773bb7be..98b359f2 100644
--- a/pjmedia/src/pjmedia/session.c
+++ b/pjmedia/src/pjmedia/session.c
@@ -1,833 +1,833 @@
-/* $Id$ */
-/*
- * Copyright (C) 2003-2006 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 <pjmedia/session.h>
-#include <pj/log.h>
-#include <pj/os.h>
-#include <pj/pool.h>
-#include <pj/string.h>
-
-typedef struct pj_media_stream_desc
-{
- pj_media_stream_info info;
- pj_media_stream_t *enc_stream, *dec_stream;
-} pj_media_stream_desc;
-
-struct pj_media_session_t
-{
- pj_pool_t *pool;
- pj_med_mgr_t *mediamgr;
- unsigned stream_cnt;
- pj_media_stream_desc *stream_desc[PJSDP_MAX_MEDIA];
-};
-
-#define THIS_FILE "session.c"
-
-#define PJ_MEDIA_SESSION_SIZE (48*1024)
-#define PJ_MEDIA_SESSION_INC 1024
-
-static const pj_str_t ID_AUDIO = { "audio", 5};
-static const pj_str_t ID_VIDEO = { "video", 5};
-static const pj_str_t ID_IN = { "IN", 2 };
-static const pj_str_t ID_IP4 = { "IP4", 3};
-static const pj_str_t ID_RTP_AVP = { "RTP/AVP", 7 };
-static const pj_str_t ID_SDP_NAME = { "pjmedia", 7 };
-
-static void session_init (pj_media_session_t *ses)
-{
- pj_memset (ses, 0, sizeof(pj_media_session_t));
-}
-
-
-/**
- * Create new session offering.
- */
-PJ_DEF(pj_media_session_t*)
-pj_media_session_create (pj_med_mgr_t *mgr, const pj_media_sock_info *sock_info)
-{
- pj_pool_factory *pf;
- pj_pool_t *pool;
- pj_media_session_t *session;
- pj_media_stream_desc *sd;
- unsigned i, codec_cnt;
- pj_codec_mgr *cm;
- const pj_codec_id *codecs[PJSDP_MAX_FMT];
-
- pf = pj_med_mgr_get_pool_factory(mgr);
-
- pool = pj_pool_create( pf, "session", PJ_MEDIA_SESSION_SIZE, PJ_MEDIA_SESSION_INC, NULL);
- if (!pool)
- return NULL;
-
- session = pj_pool_alloc(pool, sizeof(pj_media_session_t));
- if (!session)
- return NULL;
-
- session_init (session);
-
- session->pool = pool;
- session->mediamgr = mgr;
-
- /* Create first stream */
- sd = pj_pool_calloc (pool, 1, sizeof(pj_media_stream_desc));
- if (!sd)
- return NULL;
-
- sd->info.type = ID_AUDIO;
- sd->info.dir = PJ_MEDIA_DIR_ENCODING_DECODING;
- sd->info.transport = ID_RTP_AVP;
- pj_memcpy(&sd->info.sock_info, sock_info, sizeof(*sock_info));
-
- /* Enum audio codecs. */
- sd->info.fmt_cnt = 0;
- cm = pj_med_mgr_get_codec_mgr (mgr);
- codec_cnt = pj_codec_mgr_enum_codecs(cm, PJSDP_MAX_FMT, codecs);
- if (codec_cnt > PJSDP_MAX_FMT) codec_cnt = PJSDP_MAX_FMT;
- for (i=0; i<codec_cnt; ++i) {
- if (codecs[i]->type != PJ_MEDIA_TYPE_AUDIO)
- continue;
-
- sd->info.fmt[sd->info.fmt_cnt].pt = codecs[i]->pt;
- sd->info.fmt[sd->info.fmt_cnt].sample_rate = codecs[i]->sample_rate;
- pj_strdup (pool, &sd->info.fmt[sd->info.fmt_cnt].encoding_name, &codecs[i]->encoding_name);
- ++sd->info.fmt_cnt;
- }
-
- session->stream_desc[session->stream_cnt++] = sd;
-
- return session;
-}
-
-static int sdp_check (const pjsdp_session_desc *sdp)
-{
- int has_conn = 0;
- unsigned i;
-
- if (sdp->conn)
- has_conn = 1;
-
- if (sdp->media_count == 0) {
- PJ_LOG(4,(THIS_FILE, "SDP check failed: no media stream definition"));
- return -1;
- }
-
- for (i=0; i<sdp->media_count; ++i) {
- pjsdp_media_desc *m = sdp->media[i];
-
- if (!m) {
- pj_assert(0);
- return -1;
- }
-
- if (m->desc.fmt_count == 0) {
- PJ_LOG(4,(THIS_FILE, "SDP check failed: no format listed in media stream"));
- return -1;
- }
-
- if (!has_conn && m->conn == NULL) {
- PJ_LOG(4,(THIS_FILE, "SDP check failed: no connection information for media"));
- return -1;
- }
- }
-
- return 0;
-}
-
-/*
- * Create local stream definition that matches SDP received from peer.
- */
-static pj_media_stream_desc*
-create_stream_from_sdp (pj_pool_t *pool, pj_med_mgr_t *mgr, const pjsdp_conn_info *conn,
- const pjsdp_media_desc *m, const pj_media_sock_info *sock_info)
-{
- pj_media_stream_desc *sd;
-
- sd = pj_pool_calloc (pool, 1, sizeof(pj_media_stream_desc));
- if (!sd) {
- PJ_LOG(2,(THIS_FILE, "No memory to allocate stream descriptor"));
- return NULL;
- }
-
- if (pj_stricmp(&conn->net_type, &ID_IN)==0 &&
- pj_stricmp(&conn->addr_type, &ID_IP4)==0 &&
- pj_stricmp(&m->desc.media, &ID_AUDIO)==0 &&
- pj_stricmp(&m->desc.transport, &ID_RTP_AVP) == 0)
- {
- /*
- * Got audio stream.
- */
- unsigned i, codec_cnt;
- pj_codec_mgr *cm;
- const pj_codec_id *codecs[PJSDP_MAX_FMT];
-
- sd->info.type = ID_AUDIO;
- sd->info.transport = ID_RTP_AVP;
- pj_memcpy(&sd->info.sock_info, sock_info, sizeof(*sock_info));
- sd->info.rem_port = m->desc.port;
- pj_strdup (pool, &sd->info.rem_addr, &conn->addr);
-
- /* Enum audio codecs. */
- sd->info.fmt_cnt = 0;
- cm = pj_med_mgr_get_codec_mgr (mgr);
- codec_cnt = pj_codec_mgr_enum_codecs (cm, PJSDP_MAX_FMT, codecs);
- if (codec_cnt > PJSDP_MAX_FMT) codec_cnt = PJSDP_MAX_FMT;
-
- /* Find just one codec which we can support. */
- for (i=0; i<m->desc.fmt_count && sd->info.fmt_cnt == 0; ++i) {
- unsigned j, fmt_i;
-
- /* For static payload, just match payload type. */
- /* Else match clock rate and encoding name. */
- fmt_i = pj_strtoul(&m->desc.fmt[i]);
- if (fmt_i < PJ_RTP_PT_DYNAMIC) {
- for (j=0; j<codec_cnt; ++j) {
- if (codecs[j]->pt == fmt_i) {
- sd->info.fmt_cnt = 1;
- sd->info.fmt[0].type = PJ_MEDIA_TYPE_AUDIO;
- sd->info.fmt[0].pt = codecs[j]->pt;
- sd->info.fmt[0].sample_rate = codecs[j]->sample_rate;
- pj_strdup (pool, &sd->info.fmt[0].encoding_name, &codecs[j]->encoding_name);
- break;
- }
- }
- } else {
-
- /* Find the rtpmap for the payload type. */
- const pjsdp_rtpmap_attr *rtpmap = pjsdp_media_desc_find_rtpmap (m, fmt_i);
-
- /* Don't accept the media if no rtpmap for dynamic PT. */
- if (rtpmap == NULL) {
- PJ_LOG(4,(THIS_FILE, "SDP: No rtpmap found for payload id %d", m->desc.fmt[i]));
- continue;
- }
-
- /* Check whether we can take this codec. */
- for (j=0; j<codec_cnt; ++j) {
- if (rtpmap->clock_rate == codecs[j]->sample_rate &&
- pj_stricmp(&rtpmap->encoding_name, &codecs[j]->encoding_name) == 0)
- {
- sd->info.fmt_cnt = 1;
- sd->info.fmt[0].type = PJ_MEDIA_TYPE_AUDIO;
- sd->info.fmt[0].pt = codecs[j]->pt;
- sd->info.fmt[0].sample_rate = codecs[j]->sample_rate;
- sd->info.fmt[0].encoding_name = codecs[j]->encoding_name;
- break;
- }
- }
- }
- }
-
- /* Match codec and direction. */
- if (sd->info.fmt_cnt == 0 || m->desc.port == 0 ||
- pjsdp_media_desc_has_attr(m, PJSDP_ATTR_INACTIVE))
- {
- sd->info.dir = PJ_MEDIA_DIR_NONE;
- }
- else if (pjsdp_media_desc_has_attr(m, PJSDP_ATTR_RECV_ONLY)) {
- sd->info.dir = PJ_MEDIA_DIR_ENCODING;
- }
- else if (pjsdp_media_desc_has_attr(m, PJSDP_ATTR_SEND_ONLY)) {
- sd->info.dir = PJ_MEDIA_DIR_DECODING;
- }
- else {
- sd->info.dir = PJ_MEDIA_DIR_ENCODING_DECODING;
- }
-
- } else {
- /* Unsupported media stream. */
- unsigned fmt_num;
- const pjsdp_rtpmap_attr *rtpmap = NULL;
-
- pj_strdup(pool, &sd->info.type, &m->desc.media);
- pj_strdup(pool, &sd->info.transport, &m->desc.transport);
- pj_memset(&sd->info.sock_info, 0, sizeof(*sock_info));
- pj_strdup (pool, &sd->info.rem_addr, &conn->addr);
- sd->info.rem_port = m->desc.port;
-
- /* Just put one format and rtpmap, so that we don't have to make
- * special exception when we convert this stream to SDP.
- */
-
- /* Find the rtpmap for the payload type. */
- fmt_num = pj_strtoul(&m->desc.fmt[0]);
- rtpmap = pjsdp_media_desc_find_rtpmap (m, fmt_num);
-
- sd->info.fmt_cnt = 1;
- if (pj_stricmp(&m->desc.media, &ID_VIDEO)==0) {
- sd->info.fmt[0].type = PJ_MEDIA_TYPE_VIDEO;
- sd->info.fmt[0].pt = fmt_num;
- if (rtpmap) {
- pj_strdup (pool, &sd->info.fmt[0].encoding_name,
- &rtpmap->encoding_name);
- sd->info.fmt[0].sample_rate = rtpmap->clock_rate;
- }
- } else {
- sd->info.fmt[0].type = PJ_MEDIA_TYPE_UNKNOWN;
- pj_strdup(pool, &sd->info.fmt[0].encoding_name, &m->desc.fmt[0]);
- }
-
- sd->info.dir = PJ_MEDIA_DIR_NONE;
- }
-
- return sd;
-}
-
-/**
- * Create new session based on peer's offering.
- */
-PJ_DEF(pj_media_session_t*)
-pj_media_session_create_from_sdp (pj_med_mgr_t *mgr, const pjsdp_session_desc *sdp,
- const pj_media_sock_info *sock_info)
-{
- pj_pool_factory *pf;
- pj_pool_t *pool;
- pj_media_session_t *session;
- unsigned i;
-
- if (sdp_check(sdp) != 0)
- return NULL;
-
- pf = pj_med_mgr_get_pool_factory(mgr);
- pool = pj_pool_create( pf, "session", PJ_MEDIA_SESSION_SIZE, PJ_MEDIA_SESSION_INC, NULL);
- if (!pool)
- return NULL;
-
- session = pj_pool_alloc(pool, sizeof(pj_media_session_t));
- if (!session) {
- PJ_LOG(3,(THIS_FILE, "No memory to create media session descriptor"));
- pj_pool_release (pool);
- return NULL;
- }
-
- session_init (session);
-
- session->pool = pool;
- session->mediamgr = mgr;
-
- /* Enumerate each media stream and create our peer. */
- for (i=0; i<sdp->media_count; ++i) {
- const pjsdp_conn_info *conn;
- const pjsdp_media_desc *m;
- pj_media_stream_desc *sd;
-
- m = sdp->media[i];
- conn = m->conn ? m->conn : sdp->conn;
-
- /*
- * Bug:
- * the sock_info below is used by more than one 'm' lines
- */
- PJ_TODO(SUPPORT_MORE_THAN_ONE_SDP_M_LINES)
-
- sd = create_stream_from_sdp (pool, mgr, conn, m, sock_info);
- pj_assert (sd);
-
- session->stream_desc[session->stream_cnt++] = sd;
- }
-
- return session;
-}
-
-/**
- * Duplicate session. The new session is inactive.
- */
-PJ_DEF(pj_media_session_t*)
-pj_media_session_clone (const pj_media_session_t *rhs)
-{
- pj_pool_factory *pf;
- pj_pool_t *pool;
- pj_media_session_t *session;
- unsigned i;
-
- pf = pj_med_mgr_get_pool_factory(rhs->mediamgr);
- pool = pj_pool_create( pf, "session", PJ_MEDIA_SESSION_SIZE, PJ_MEDIA_SESSION_INC, NULL);
- if (!pool) {
- return NULL;
- }
-
- session = pj_pool_alloc (pool, sizeof(*session));
- if (!session) {
- PJ_LOG(3,(THIS_FILE, "No memory to create media session descriptor"));
- pj_pool_release (pool);
- return NULL;
- }
-
- session->pool = pool;
- session->mediamgr = rhs->mediamgr;
- session->stream_cnt = rhs->stream_cnt;
-
- for (i=0; i<rhs->stream_cnt; ++i) {
- pj_media_stream_desc *sd1 = pj_pool_alloc (session->pool, sizeof(pj_media_stream_desc));
- const pj_media_stream_desc *sd2 = rhs->stream_desc[i];
-
- if (!sd1) {
- PJ_LOG(3,(THIS_FILE, "No memory to create media stream descriptor"));
- pj_pool_release (pool);
- return NULL;
- }
-
- session->stream_desc[i] = sd1;
- sd1->enc_stream = sd1->dec_stream = NULL;
- pj_strdup (pool, &sd1->info.type, &sd2->info.type);
- sd1->info.dir = sd2->info.dir;
- pj_strdup (pool, &sd1->info.transport, &sd2->info.transport);
- pj_memcpy(&sd1->info.sock_info, &sd2->info.sock_info, sizeof(pj_media_sock_info));
- pj_strdup (pool, &sd1->info.rem_addr, &sd2->info.rem_addr);
- sd1->info.rem_port = sd2->info.rem_port;
- sd1->info.fmt_cnt = sd2->info.fmt_cnt;
- pj_memcpy (sd1->info.fmt, sd2->info.fmt, sizeof(sd2->info.fmt));
- }
-
- return session;
-}
-
-/**
- * Create SDP description from the session.
- */
-PJ_DEF(pjsdp_session_desc*)
-pj_media_session_create_sdp (const pj_media_session_t *session, pj_pool_t *pool,
- pj_bool_t only_first_fmt)
-{
- pjsdp_session_desc *sdp;
- pj_time_val tv;
- unsigned i;
- pj_media_sock_info *c_addr = NULL;
-
- if (session->stream_cnt == 0) {
- pj_assert(0);
- return NULL;
- }
-
- sdp = pj_pool_calloc (pool, 1, sizeof(pjsdp_session_desc));
- if (!sdp) {
- PJ_LOG(3,(THIS_FILE, "No memory to allocate SDP session descriptor"));
- return NULL;
- }
-
- pj_gettimeofday(&tv);
-
- sdp->origin.user = pj_str("-");
- sdp->origin.version = sdp->origin.id = tv.sec + 2208988800UL;
- sdp->origin.net_type = ID_IN;
- sdp->origin.addr_type = ID_IP4;
- sdp->origin.addr = *pj_gethostname();
-
- sdp->name = ID_SDP_NAME;
-
- /* If all media addresses are the same, then put the connection
- * info in the session level, otherwise put it in media stream
- * level.
- */
- for (i=0; i<session->stream_cnt; ++i) {
- if (c_addr == NULL) {
- c_addr = &session->stream_desc[i]->info.sock_info;
- } else if (c_addr->rtp_addr_name.sin_addr.s_addr != session->stream_desc[i]->info.sock_info.rtp_addr_name.sin_addr.s_addr)
- {
- c_addr = NULL;
- break;
- }
- }
-
- if (c_addr) {
- /* All addresses are the same, put connection info in session level. */
- sdp->conn = pj_pool_alloc (pool, sizeof(pjsdp_conn_info));
- if (!sdp->conn) {
- PJ_LOG(2,(THIS_FILE, "No memory to allocate SDP connection info"));
- return NULL;
- }
-
- sdp->conn->net_type = ID_IN;
- sdp->conn->addr_type = ID_IP4;
- pj_strdup2 (pool, &sdp->conn->addr, pj_inet_ntoa(c_addr->rtp_addr_name.sin_addr));
- }
-
- sdp->time.start = sdp->time.stop = 0;
- sdp->attr_count = 0;
-
- /* Create each media. */
- sdp->media_count = 0;
- for (i=0; i<session->stream_cnt; ++i) {
- const pj_media_stream_desc *sd = session->stream_desc[i];
- pjsdp_media_desc *m;
- unsigned j;
- unsigned fmt_cnt;
- pjsdp_attr *attr;
-
- m = pj_pool_calloc (pool, 1, sizeof(pjsdp_media_desc));
- if (!m) {
- PJ_LOG(3,(THIS_FILE, "No memory to allocate SDP media stream descriptor"));
- return NULL;
- }
-
- sdp->media[sdp->media_count++] = m;
-
- pj_strdup (pool, &m->desc.media, &sd->info.type);
- m->desc.port = pj_ntohs(sd->info.sock_info.rtp_addr_name.sin_port);
- m->desc.port_count = 1;
- pj_strdup (pool, &m->desc.transport, &sd->info.transport);
-
- /* Add format and rtpmap for each codec. */
- m->desc.fmt_count = 0;
- m->attr_count = 0;
- fmt_cnt = sd->info.fmt_cnt;
- if (fmt_cnt > 0 && only_first_fmt)
- fmt_cnt = 1;
- for (j=0; j<fmt_cnt; ++j) {
- pjsdp_rtpmap_attr *rtpmap;
- pj_str_t *fmt = &m->desc.fmt[m->desc.fmt_count++];
-
- if (sd->info.fmt[j].type==PJ_MEDIA_TYPE_UNKNOWN) {
- pj_strdup(pool, fmt, &sd->info.fmt[j].encoding_name);
- } else {
- fmt->ptr = pj_pool_alloc(pool, 8);
- fmt->slen = pj_utoa(sd->info.fmt[j].pt, fmt->ptr);
-
- rtpmap = pj_pool_calloc(pool, 1, sizeof(pjsdp_rtpmap_attr));
- if (rtpmap) {
- m->attr[m->attr_count++] = (pjsdp_attr*)rtpmap;
- rtpmap->type = PJSDP_ATTR_RTPMAP;
- rtpmap->payload_type = sd->info.fmt[j].pt;
- rtpmap->clock_rate = sd->info.fmt[j].sample_rate;
- pj_strdup (pool, &rtpmap->encoding_name, &sd->info.fmt[j].encoding_name);
- } else {
- PJ_LOG(3,(THIS_FILE, "No memory to allocate SDP rtpmap descriptor"));
- }
- }
- }
-
- /* If we don't have connection info in session level, create one. */
- if (sdp->conn == NULL) {
- m->conn = pj_pool_alloc (pool, sizeof(pjsdp_conn_info));
- if (m->conn) {
- m->conn->net_type = ID_IN;
- m->conn->addr_type = ID_IP4;
- pj_strdup2 (pool, &m->conn->addr, pj_inet_ntoa(sd->info.sock_info.rtp_addr_name.sin_addr));
- } else {
- PJ_LOG(3,(THIS_FILE, "No memory to allocate SDP media connection info"));
- return NULL;
- }
- }
-
- /* Add additional attribute to the media stream. */
- attr = pj_pool_alloc(pool, sizeof(pjsdp_attr));
- if (!attr) {
- PJ_LOG(3,(THIS_FILE, "No memory to allocate SDP attribute"));
- return NULL;
- }
- m->attr[m->attr_count++] = attr;
-
- switch (sd->info.dir) {
- case PJ_MEDIA_DIR_NONE:
- attr->type = PJSDP_ATTR_INACTIVE;
- break;
- case PJ_MEDIA_DIR_ENCODING:
- attr->type = PJSDP_ATTR_SEND_ONLY;
- break;
- case PJ_MEDIA_DIR_DECODING:
- attr->type = PJSDP_ATTR_RECV_ONLY;
- break;
- case PJ_MEDIA_DIR_ENCODING_DECODING:
- attr->type = PJSDP_ATTR_SEND_RECV;
- break;
- }
- }
-
- return sdp;
-}
-
-/**
- * Update session with SDP answer from peer.
- */
-PJ_DEF(pj_status_t)
-pj_media_session_update (pj_media_session_t *session,
- const pjsdp_session_desc *sdp)
-{
- unsigned i;
- unsigned count;
-
- /* Check SDP */
- if (sdp_check (sdp) != 0) {
- return -1;
- }
-
- /* If the media stream count doesn't match, only update one. */
- if (session->stream_cnt != sdp->media_count) {
- PJ_LOG(3,(THIS_FILE, "pj_media_session_update : "
- "SDP media count mismatch! (rmt=%d, lcl=%d)",
- sdp->media_count, session->stream_cnt));
- count = (session->stream_cnt < sdp->media_count) ?
- session->stream_cnt : sdp->media_count;
- } else {
- count = session->stream_cnt;
- }
-
- for (i=0; i<count; ++i) {
- pj_media_stream_desc *sd = session->stream_desc[i];
- const pjsdp_media_desc *m = sdp->media[i];
- const pjsdp_conn_info *conn;
- unsigned j;
-
- /* Check that the session is not active. */
- pj_assert (sd->enc_stream == NULL && sd->dec_stream == NULL);
-
- conn = m->conn ? m->conn : sdp->conn;
- pj_assert(conn);
-
- /* Update remote address. */
- sd->info.rem_port = m->desc.port;
- pj_strdup (session->pool, &sd->info.rem_addr, &conn->addr);
-
- /* Select one active codec according to what peer wants. */
- for (j=0; j<sd->info.fmt_cnt; ++j) {
- unsigned fmt_0 = pj_strtoul(&m->desc.fmt[0]);
- if (sd->info.fmt[j].pt == fmt_0) {
- pj_codec_id temp;
-
- /* Put active format to the front. */
- if (j == 0)
- break;
-
- pj_memcpy(&temp, &sd->info.fmt[0], sizeof(temp));
- pj_memcpy(&sd->info.fmt[0], &sd->info.fmt[j], sizeof(temp));
- pj_memcpy(&sd->info.fmt[j], &temp, sizeof(temp));
- break;
- }
- }
-
- if (j == sd->info.fmt_cnt) {
- /* Peer has answered SDP with new codec, which doesn't exist
- * in the offer!
- * Mute this media.
- */
- PJ_LOG(3,(THIS_FILE, "Peer has answered SDP with new codec!"));
- sd->info.dir = PJ_MEDIA_DIR_NONE;
- continue;
- }
-
- /* Check direction. */
- if (m->desc.port == 0 || pjsdp_media_desc_has_attr(m, PJSDP_ATTR_INACTIVE)) {
- sd->info.dir = PJ_MEDIA_DIR_NONE;
- }
- else if (pjsdp_media_desc_has_attr(m, PJSDP_ATTR_RECV_ONLY)) {
- sd->info.dir = PJ_MEDIA_DIR_ENCODING;
- }
- else if (pjsdp_media_desc_has_attr(m, PJSDP_ATTR_SEND_ONLY)) {
- sd->info.dir = PJ_MEDIA_DIR_DECODING;
- }
- else {
- sd->info.dir = PJ_MEDIA_DIR_ENCODING_DECODING;
- }
- }
-
- return 0;
-}
-
-/**
- * Enumerate media streams in the session.
- */
-PJ_DEF(unsigned)
-pj_media_session_enum_streams (const pj_media_session_t *session,
- unsigned count, const pj_media_stream_info *info[])
-{
- unsigned i;
-
- if (count > session->stream_cnt)
- count = session->stream_cnt;
-
- for (i=0; i<count; ++i) {
- info[i] = &session->stream_desc[i]->info;
- }
-
- return session->stream_cnt;
-}
-
-/**
- * Get statistics
- */
-PJ_DEF(pj_status_t)
-pj_media_session_get_stat (const pj_media_session_t *session, unsigned index,
- pj_media_stream_stat *tx_stat,
- pj_media_stream_stat *rx_stat)
-{
- pj_media_stream_desc *sd;
- int stat_cnt = 0;
-
- if (index >= session->stream_cnt) {
- pj_assert(0);
- return -1;
- }
-
- sd = session->stream_desc[index];
-
- if (sd->enc_stream && tx_stat) {
- pj_media_stream_get_stat (sd->enc_stream, tx_stat);
- ++stat_cnt;
- } else if (tx_stat) {
- pj_memset (tx_stat, 0, sizeof(*tx_stat));
- }
-
- if (sd->dec_stream && rx_stat) {
- pj_media_stream_get_stat (sd->dec_stream, rx_stat);
- ++stat_cnt;
- } else if (rx_stat) {
- pj_memset (rx_stat, 0, sizeof(*rx_stat));
- }
-
- return stat_cnt ? 0 : -1;
-}
-
-/**
- * Modify stream, only when stream is inactive.
- */
-PJ_DEF(pj_status_t)
-pj_media_session_modify_stream (pj_media_session_t *session, unsigned index,
- unsigned modify_flag, const pj_media_stream_info *info)
-{
- pj_media_stream_desc *sd;
-
- if (index >= session->stream_cnt) {
- pj_assert(0);
- return -1;
- }
-
- sd = session->stream_desc[index];
-
- if (sd->enc_stream || sd->dec_stream) {
- pj_assert(0);
- return -1;
- }
-
- if (modify_flag & PJ_MEDIA_STREAM_MODIFY_DIR) {
- sd->info.dir = info->dir;
- }
-
- return 0;
-}
-
-/**
- * Activate media session.
- */
-PJ_DEF(pj_status_t)
-pj_media_session_activate (pj_media_session_t *session)
-{
- unsigned i;
- pj_status_t status = 0;
-
- for (i=0; i<session->stream_cnt; ++i) {
- pj_status_t rc;
- rc = pj_media_session_activate_stream (session, i);
- if (status == 0)
- status = rc;
- }
- return status;
-}
-
-/**
- * Activate individual stream in media session.
- */
-PJ_DEF(pj_status_t)
-pj_media_session_activate_stream (pj_media_session_t *session, unsigned index)
-{
- pj_media_stream_desc *sd;
- pj_media_stream_create_param scp;
- pj_status_t status;
- pj_time_val tv;
-
- if (index < 0 || index >= session->stream_cnt) {
- pj_assert(0);
- return -1;
- }
-
- sd = session->stream_desc[index];
-
- if (sd->enc_stream || sd->dec_stream) {
- /* Stream already active. */
- pj_assert(0);
- return 0;
- }
-
- pj_gettimeofday(&tv);
-
- /* Initialize parameter to create stream. */
- pj_memset (&scp, 0, sizeof(scp));
- scp.codec_id = &sd->info.fmt[0];
- scp.mediamgr = session->mediamgr;
- scp.dir = sd->info.dir;
- scp.rtp_sock = sd->info.sock_info.rtp_sock;
- scp.rtcp_sock = sd->info.sock_info.rtcp_sock;
- scp.remote_addr = pj_pool_calloc (session->pool, 1, sizeof(pj_sockaddr_in));
- pj_sockaddr_init (scp.remote_addr, &sd->info.rem_addr, sd->info.rem_port);
- scp.ssrc = tv.sec;
- scp.jb_min = 1;
- scp.jb_max = 15;
- scp.jb_maxcnt = 16;
-
- /* Build the stream! */
- status = pj_media_stream_create (session->pool, &sd->enc_stream, &sd->dec_stream, &scp);
-
- if (status==0 && sd->enc_stream) {
- status = pj_media_stream_start (sd->enc_stream);
- if (status != 0)
- goto on_error;
- }
- if (status==0 && sd->dec_stream) {
- status = pj_media_stream_start (sd->dec_stream);
- if (status != 0)
- goto on_error;
- }
- return status;
-
-on_error:
- if (sd->enc_stream) {
- pj_media_stream_destroy (sd->enc_stream);
- sd->enc_stream = NULL;
- }
- if (sd->dec_stream) {
- pj_media_stream_destroy (sd->dec_stream);
- sd->dec_stream = NULL;
- }
- return status;
-}
-
-/**
- * Destroy media session.
- */
-PJ_DEF(pj_status_t)
-pj_media_session_destroy (pj_media_session_t *session)
-{
- unsigned i;
-
- if (!session)
- return -1;
-
- for (i=0; i<session->stream_cnt; ++i) {
- pj_media_stream_desc *sd = session->stream_desc[i];
-
- if (sd->enc_stream) {
- pj_media_stream_destroy (sd->enc_stream);
- sd->enc_stream = NULL;
- }
- if (sd->dec_stream) {
- pj_media_stream_destroy (sd->dec_stream);
- sd->dec_stream = NULL;
- }
- }
- pj_pool_release (session->pool);
- return 0;
-}
-
+/* $Id$ */
+/*
+ * Copyright (C) 2003-2006 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 <pjmedia/session.h>
+#include <pj/log.h>
+#include <pj/os.h>
+#include <pj/pool.h>
+#include <pj/string.h>
+
+typedef struct pj_media_stream_desc
+{
+ pj_media_stream_info info;
+ pj_media_stream_t *enc_stream, *dec_stream;
+} pj_media_stream_desc;
+
+struct pj_media_session_t
+{
+ pj_pool_t *pool;
+ pj_med_mgr_t *mediamgr;
+ unsigned stream_cnt;
+ pj_media_stream_desc *stream_desc[PJSDP_MAX_MEDIA];
+};
+
+#define THIS_FILE "session.c"
+
+#define PJ_MEDIA_SESSION_SIZE (48*1024)
+#define PJ_MEDIA_SESSION_INC 1024
+
+static const pj_str_t ID_AUDIO = { "audio", 5};
+static const pj_str_t ID_VIDEO = { "video", 5};
+static const pj_str_t ID_IN = { "IN", 2 };
+static const pj_str_t ID_IP4 = { "IP4", 3};
+static const pj_str_t ID_RTP_AVP = { "RTP/AVP", 7 };
+static const pj_str_t ID_SDP_NAME = { "pjmedia", 7 };
+
+static void session_init (pj_media_session_t *ses)
+{
+ pj_memset (ses, 0, sizeof(pj_media_session_t));
+}
+
+
+/**
+ * Create new session offering.
+ */
+PJ_DEF(pj_media_session_t*)
+pj_media_session_create (pj_med_mgr_t *mgr, const pj_media_sock_info *sock_info)
+{
+ pj_pool_factory *pf;
+ pj_pool_t *pool;
+ pj_media_session_t *session;
+ pj_media_stream_desc *sd;
+ unsigned i, codec_cnt;
+ pj_codec_mgr *cm;
+ const pj_codec_id *codecs[PJSDP_MAX_FMT];
+
+ pf = pj_med_mgr_get_pool_factory(mgr);
+
+ pool = pj_pool_create( pf, "session", PJ_MEDIA_SESSION_SIZE, PJ_MEDIA_SESSION_INC, NULL);
+ if (!pool)
+ return NULL;
+
+ session = pj_pool_alloc(pool, sizeof(pj_media_session_t));
+ if (!session)
+ return NULL;
+
+ session_init (session);
+
+ session->pool = pool;
+ session->mediamgr = mgr;
+
+ /* Create first stream */
+ sd = pj_pool_calloc (pool, 1, sizeof(pj_media_stream_desc));
+ if (!sd)
+ return NULL;
+
+ sd->info.type = ID_AUDIO;
+ sd->info.dir = PJ_MEDIA_DIR_ENCODING_DECODING;
+ sd->info.transport = ID_RTP_AVP;
+ pj_memcpy(&sd->info.sock_info, sock_info, sizeof(*sock_info));
+
+ /* Enum audio codecs. */
+ sd->info.fmt_cnt = 0;
+ cm = pj_med_mgr_get_codec_mgr (mgr);
+ codec_cnt = pj_codec_mgr_enum_codecs(cm, PJSDP_MAX_FMT, codecs);
+ if (codec_cnt > PJSDP_MAX_FMT) codec_cnt = PJSDP_MAX_FMT;
+ for (i=0; i<codec_cnt; ++i) {
+ if (codecs[i]->type != PJ_MEDIA_TYPE_AUDIO)
+ continue;
+
+ sd->info.fmt[sd->info.fmt_cnt].pt = codecs[i]->pt;
+ sd->info.fmt[sd->info.fmt_cnt].sample_rate = codecs[i]->sample_rate;
+ pj_strdup (pool, &sd->info.fmt[sd->info.fmt_cnt].encoding_name, &codecs[i]->encoding_name);
+ ++sd->info.fmt_cnt;
+ }
+
+ session->stream_desc[session->stream_cnt++] = sd;
+
+ return session;
+}
+
+static int sdp_check (const pjsdp_session_desc *sdp)
+{
+ int has_conn = 0;
+ unsigned i;
+
+ if (sdp->conn)
+ has_conn = 1;
+
+ if (sdp->media_count == 0) {
+ PJ_LOG(4,(THIS_FILE, "SDP check failed: no media stream definition"));
+ return -1;
+ }
+
+ for (i=0; i<sdp->media_count; ++i) {
+ pjsdp_media_desc *m = sdp->media[i];
+
+ if (!m) {
+ pj_assert(0);
+ return -1;
+ }
+
+ if (m->desc.fmt_count == 0) {
+ PJ_LOG(4,(THIS_FILE, "SDP check failed: no format listed in media stream"));
+ return -1;
+ }
+
+ if (!has_conn && m->conn == NULL) {
+ PJ_LOG(4,(THIS_FILE, "SDP check failed: no connection information for media"));
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * Create local stream definition that matches SDP received from peer.
+ */
+static pj_media_stream_desc*
+create_stream_from_sdp (pj_pool_t *pool, pj_med_mgr_t *mgr, const pjsdp_conn_info *conn,
+ const pjsdp_media_desc *m, const pj_media_sock_info *sock_info)
+{
+ pj_media_stream_desc *sd;
+
+ sd = pj_pool_calloc (pool, 1, sizeof(pj_media_stream_desc));
+ if (!sd) {
+ PJ_LOG(2,(THIS_FILE, "No memory to allocate stream descriptor"));
+ return NULL;
+ }
+
+ if (pj_stricmp(&conn->net_type, &ID_IN)==0 &&
+ pj_stricmp(&conn->addr_type, &ID_IP4)==0 &&
+ pj_stricmp(&m->desc.media, &ID_AUDIO)==0 &&
+ pj_stricmp(&m->desc.transport, &ID_RTP_AVP) == 0)
+ {
+ /*
+ * Got audio stream.
+ */
+ unsigned i, codec_cnt;
+ pj_codec_mgr *cm;
+ const pj_codec_id *codecs[PJSDP_MAX_FMT];
+
+ sd->info.type = ID_AUDIO;
+ sd->info.transport = ID_RTP_AVP;
+ pj_memcpy(&sd->info.sock_info, sock_info, sizeof(*sock_info));
+ sd->info.rem_port = m->desc.port;
+ pj_strdup (pool, &sd->info.rem_addr, &conn->addr);
+
+ /* Enum audio codecs. */
+ sd->info.fmt_cnt = 0;
+ cm = pj_med_mgr_get_codec_mgr (mgr);
+ codec_cnt = pj_codec_mgr_enum_codecs (cm, PJSDP_MAX_FMT, codecs);
+ if (codec_cnt > PJSDP_MAX_FMT) codec_cnt = PJSDP_MAX_FMT;
+
+ /* Find just one codec which we can support. */
+ for (i=0; i<m->desc.fmt_count && sd->info.fmt_cnt == 0; ++i) {
+ unsigned j, fmt_i;
+
+ /* For static payload, just match payload type. */
+ /* Else match clock rate and encoding name. */
+ fmt_i = pj_strtoul(&m->desc.fmt[i]);
+ if (fmt_i < PJ_RTP_PT_DYNAMIC) {
+ for (j=0; j<codec_cnt; ++j) {
+ if (codecs[j]->pt == fmt_i) {
+ sd->info.fmt_cnt = 1;
+ sd->info.fmt[0].type = PJ_MEDIA_TYPE_AUDIO;
+ sd->info.fmt[0].pt = codecs[j]->pt;
+ sd->info.fmt[0].sample_rate = codecs[j]->sample_rate;
+ pj_strdup (pool, &sd->info.fmt[0].encoding_name, &codecs[j]->encoding_name);
+ break;
+ }
+ }
+ } else {
+
+ /* Find the rtpmap for the payload type. */
+ const pjsdp_rtpmap_attr *rtpmap = pjsdp_media_desc_find_rtpmap (m, fmt_i);
+
+ /* Don't accept the media if no rtpmap for dynamic PT. */
+ if (rtpmap == NULL) {
+ PJ_LOG(4,(THIS_FILE, "SDP: No rtpmap found for payload id %d", m->desc.fmt[i]));
+ continue;
+ }
+
+ /* Check whether we can take this codec. */
+ for (j=0; j<codec_cnt; ++j) {
+ if (rtpmap->clock_rate == codecs[j]->sample_rate &&
+ pj_stricmp(&rtpmap->encoding_name, &codecs[j]->encoding_name) == 0)
+ {
+ sd->info.fmt_cnt = 1;
+ sd->info.fmt[0].type = PJ_MEDIA_TYPE_AUDIO;
+ sd->info.fmt[0].pt = codecs[j]->pt;
+ sd->info.fmt[0].sample_rate = codecs[j]->sample_rate;
+ sd->info.fmt[0].encoding_name = codecs[j]->encoding_name;
+ break;
+ }
+ }
+ }
+ }
+
+ /* Match codec and direction. */
+ if (sd->info.fmt_cnt == 0 || m->desc.port == 0 ||
+ pjsdp_media_desc_has_attr(m, PJSDP_ATTR_INACTIVE))
+ {
+ sd->info.dir = PJ_MEDIA_DIR_NONE;
+ }
+ else if (pjsdp_media_desc_has_attr(m, PJSDP_ATTR_RECV_ONLY)) {
+ sd->info.dir = PJ_MEDIA_DIR_ENCODING;
+ }
+ else if (pjsdp_media_desc_has_attr(m, PJSDP_ATTR_SEND_ONLY)) {
+ sd->info.dir = PJ_MEDIA_DIR_DECODING;
+ }
+ else {
+ sd->info.dir = PJ_MEDIA_DIR_ENCODING_DECODING;
+ }
+
+ } else {
+ /* Unsupported media stream. */
+ unsigned fmt_num;
+ const pjsdp_rtpmap_attr *rtpmap = NULL;
+
+ pj_strdup(pool, &sd->info.type, &m->desc.media);
+ pj_strdup(pool, &sd->info.transport, &m->desc.transport);
+ pj_memset(&sd->info.sock_info, 0, sizeof(*sock_info));
+ pj_strdup (pool, &sd->info.rem_addr, &conn->addr);
+ sd->info.rem_port = m->desc.port;
+
+ /* Just put one format and rtpmap, so that we don't have to make
+ * special exception when we convert this stream to SDP.
+ */
+
+ /* Find the rtpmap for the payload type. */
+ fmt_num = pj_strtoul(&m->desc.fmt[0]);
+ rtpmap = pjsdp_media_desc_find_rtpmap (m, fmt_num);
+
+ sd->info.fmt_cnt = 1;
+ if (pj_stricmp(&m->desc.media, &ID_VIDEO)==0) {
+ sd->info.fmt[0].type = PJ_MEDIA_TYPE_VIDEO;
+ sd->info.fmt[0].pt = fmt_num;
+ if (rtpmap) {
+ pj_strdup (pool, &sd->info.fmt[0].encoding_name,
+ &rtpmap->encoding_name);
+ sd->info.fmt[0].sample_rate = rtpmap->clock_rate;
+ }
+ } else {
+ sd->info.fmt[0].type = PJ_MEDIA_TYPE_UNKNOWN;
+ pj_strdup(pool, &sd->info.fmt[0].encoding_name, &m->desc.fmt[0]);
+ }
+
+ sd->info.dir = PJ_MEDIA_DIR_NONE;
+ }
+
+ return sd;
+}
+
+/**
+ * Create new session based on peer's offering.
+ */
+PJ_DEF(pj_media_session_t*)
+pj_media_session_create_from_sdp (pj_med_mgr_t *mgr, const pjsdp_session_desc *sdp,
+ const pj_media_sock_info *sock_info)
+{
+ pj_pool_factory *pf;
+ pj_pool_t *pool;
+ pj_media_session_t *session;
+ unsigned i;
+
+ if (sdp_check(sdp) != 0)
+ return NULL;
+
+ pf = pj_med_mgr_get_pool_factory(mgr);
+ pool = pj_pool_create( pf, "session", PJ_MEDIA_SESSION_SIZE, PJ_MEDIA_SESSION_INC, NULL);
+ if (!pool)
+ return NULL;
+
+ session = pj_pool_alloc(pool, sizeof(pj_media_session_t));
+ if (!session) {
+ PJ_LOG(3,(THIS_FILE, "No memory to create media session descriptor"));
+ pj_pool_release (pool);
+ return NULL;
+ }
+
+ session_init (session);
+
+ session->pool = pool;
+ session->mediamgr = mgr;
+
+ /* Enumerate each media stream and create our peer. */
+ for (i=0; i<sdp->media_count; ++i) {
+ const pjsdp_conn_info *conn;
+ const pjsdp_media_desc *m;
+ pj_media_stream_desc *sd;
+
+ m = sdp->media[i];
+ conn = m->conn ? m->conn : sdp->conn;
+
+ /*
+ * Bug:
+ * the sock_info below is used by more than one 'm' lines
+ */
+ PJ_TODO(SUPPORT_MORE_THAN_ONE_SDP_M_LINES)
+
+ sd = create_stream_from_sdp (pool, mgr, conn, m, sock_info);
+ pj_assert (sd);
+
+ session->stream_desc[session->stream_cnt++] = sd;
+ }
+
+ return session;
+}
+
+/**
+ * Duplicate session. The new session is inactive.
+ */
+PJ_DEF(pj_media_session_t*)
+pj_media_session_clone (const pj_media_session_t *rhs)
+{
+ pj_pool_factory *pf;
+ pj_pool_t *pool;
+ pj_media_session_t *session;
+ unsigned i;
+
+ pf = pj_med_mgr_get_pool_factory(rhs->mediamgr);
+ pool = pj_pool_create( pf, "session", PJ_MEDIA_SESSION_SIZE, PJ_MEDIA_SESSION_INC, NULL);
+ if (!pool) {
+ return NULL;
+ }
+
+ session = pj_pool_alloc (pool, sizeof(*session));
+ if (!session) {
+ PJ_LOG(3,(THIS_FILE, "No memory to create media session descriptor"));
+ pj_pool_release (pool);
+ return NULL;
+ }
+
+ session->pool = pool;
+ session->mediamgr = rhs->mediamgr;
+ session->stream_cnt = rhs->stream_cnt;
+
+ for (i=0; i<rhs->stream_cnt; ++i) {
+ pj_media_stream_desc *sd1 = pj_pool_alloc (session->pool, sizeof(pj_media_stream_desc));
+ const pj_media_stream_desc *sd2 = rhs->stream_desc[i];
+
+ if (!sd1) {
+ PJ_LOG(3,(THIS_FILE, "No memory to create media stream descriptor"));
+ pj_pool_release (pool);
+ return NULL;
+ }
+
+ session->stream_desc[i] = sd1;
+ sd1->enc_stream = sd1->dec_stream = NULL;
+ pj_strdup (pool, &sd1->info.type, &sd2->info.type);
+ sd1->info.dir = sd2->info.dir;
+ pj_strdup (pool, &sd1->info.transport, &sd2->info.transport);
+ pj_memcpy(&sd1->info.sock_info, &sd2->info.sock_info, sizeof(pj_media_sock_info));
+ pj_strdup (pool, &sd1->info.rem_addr, &sd2->info.rem_addr);
+ sd1->info.rem_port = sd2->info.rem_port;
+ sd1->info.fmt_cnt = sd2->info.fmt_cnt;
+ pj_memcpy (sd1->info.fmt, sd2->info.fmt, sizeof(sd2->info.fmt));
+ }
+
+ return session;
+}
+
+/**
+ * Create SDP description from the session.
+ */
+PJ_DEF(pjsdp_session_desc*)
+pj_media_session_create_sdp (const pj_media_session_t *session, pj_pool_t *pool,
+ pj_bool_t only_first_fmt)
+{
+ pjsdp_session_desc *sdp;
+ pj_time_val tv;
+ unsigned i;
+ pj_media_sock_info *c_addr = NULL;
+
+ if (session->stream_cnt == 0) {
+ pj_assert(0);
+ return NULL;
+ }
+
+ sdp = pj_pool_calloc (pool, 1, sizeof(pjsdp_session_desc));
+ if (!sdp) {
+ PJ_LOG(3,(THIS_FILE, "No memory to allocate SDP session descriptor"));
+ return NULL;
+ }
+
+ pj_gettimeofday(&tv);
+
+ sdp->origin.user = pj_str("-");
+ sdp->origin.version = sdp->origin.id = tv.sec + 2208988800UL;
+ sdp->origin.net_type = ID_IN;
+ sdp->origin.addr_type = ID_IP4;
+ sdp->origin.addr = *pj_gethostname();
+
+ sdp->name = ID_SDP_NAME;
+
+ /* If all media addresses are the same, then put the connection
+ * info in the session level, otherwise put it in media stream
+ * level.
+ */
+ for (i=0; i<session->stream_cnt; ++i) {
+ if (c_addr == NULL) {
+ c_addr = &session->stream_desc[i]->info.sock_info;
+ } else if (c_addr->rtp_addr_name.sin_addr.s_addr != session->stream_desc[i]->info.sock_info.rtp_addr_name.sin_addr.s_addr)
+ {
+ c_addr = NULL;
+ break;
+ }
+ }
+
+ if (c_addr) {
+ /* All addresses are the same, put connection info in session level. */
+ sdp->conn = pj_pool_alloc (pool, sizeof(pjsdp_conn_info));
+ if (!sdp->conn) {
+ PJ_LOG(2,(THIS_FILE, "No memory to allocate SDP connection info"));
+ return NULL;
+ }
+
+ sdp->conn->net_type = ID_IN;
+ sdp->conn->addr_type = ID_IP4;
+ pj_strdup2 (pool, &sdp->conn->addr, pj_inet_ntoa(c_addr->rtp_addr_name.sin_addr));
+ }
+
+ sdp->time.start = sdp->time.stop = 0;
+ sdp->attr_count = 0;
+
+ /* Create each media. */
+ sdp->media_count = 0;
+ for (i=0; i<session->stream_cnt; ++i) {
+ const pj_media_stream_desc *sd = session->stream_desc[i];
+ pjsdp_media_desc *m;
+ unsigned j;
+ unsigned fmt_cnt;
+ pjsdp_attr *attr;
+
+ m = pj_pool_calloc (pool, 1, sizeof(pjsdp_media_desc));
+ if (!m) {
+ PJ_LOG(3,(THIS_FILE, "No memory to allocate SDP media stream descriptor"));
+ return NULL;
+ }
+
+ sdp->media[sdp->media_count++] = m;
+
+ pj_strdup (pool, &m->desc.media, &sd->info.type);
+ m->desc.port = pj_ntohs(sd->info.sock_info.rtp_addr_name.sin_port);
+ m->desc.port_count = 1;
+ pj_strdup (pool, &m->desc.transport, &sd->info.transport);
+
+ /* Add format and rtpmap for each codec. */
+ m->desc.fmt_count = 0;
+ m->attr_count = 0;
+ fmt_cnt = sd->info.fmt_cnt;
+ if (fmt_cnt > 0 && only_first_fmt)
+ fmt_cnt = 1;
+ for (j=0; j<fmt_cnt; ++j) {
+ pjsdp_rtpmap_attr *rtpmap;
+ pj_str_t *fmt = &m->desc.fmt[m->desc.fmt_count++];
+
+ if (sd->info.fmt[j].type==PJ_MEDIA_TYPE_UNKNOWN) {
+ pj_strdup(pool, fmt, &sd->info.fmt[j].encoding_name);
+ } else {
+ fmt->ptr = pj_pool_alloc(pool, 8);
+ fmt->slen = pj_utoa(sd->info.fmt[j].pt, fmt->ptr);
+
+ rtpmap = pj_pool_calloc(pool, 1, sizeof(pjsdp_rtpmap_attr));
+ if (rtpmap) {
+ m->attr[m->attr_count++] = (pjsdp_attr*)rtpmap;
+ rtpmap->type = PJSDP_ATTR_RTPMAP;
+ rtpmap->payload_type = sd->info.fmt[j].pt;
+ rtpmap->clock_rate = sd->info.fmt[j].sample_rate;
+ pj_strdup (pool, &rtpmap->encoding_name, &sd->info.fmt[j].encoding_name);
+ } else {
+ PJ_LOG(3,(THIS_FILE, "No memory to allocate SDP rtpmap descriptor"));
+ }
+ }
+ }
+
+ /* If we don't have connection info in session level, create one. */
+ if (sdp->conn == NULL) {
+ m->conn = pj_pool_alloc (pool, sizeof(pjsdp_conn_info));
+ if (m->conn) {
+ m->conn->net_type = ID_IN;
+ m->conn->addr_type = ID_IP4;
+ pj_strdup2 (pool, &m->conn->addr, pj_inet_ntoa(sd->info.sock_info.rtp_addr_name.sin_addr));
+ } else {
+ PJ_LOG(3,(THIS_FILE, "No memory to allocate SDP media connection info"));
+ return NULL;
+ }
+ }
+
+ /* Add additional attribute to the media stream. */
+ attr = pj_pool_alloc(pool, sizeof(pjsdp_attr));
+ if (!attr) {
+ PJ_LOG(3,(THIS_FILE, "No memory to allocate SDP attribute"));
+ return NULL;
+ }
+ m->attr[m->attr_count++] = attr;
+
+ switch (sd->info.dir) {
+ case PJ_MEDIA_DIR_NONE:
+ attr->type = PJSDP_ATTR_INACTIVE;
+ break;
+ case PJ_MEDIA_DIR_ENCODING:
+ attr->type = PJSDP_ATTR_SEND_ONLY;
+ break;
+ case PJ_MEDIA_DIR_DECODING:
+ attr->type = PJSDP_ATTR_RECV_ONLY;
+ break;
+ case PJ_MEDIA_DIR_ENCODING_DECODING:
+ attr->type = PJSDP_ATTR_SEND_RECV;
+ break;
+ }
+ }
+
+ return sdp;
+}
+
+/**
+ * Update session with SDP answer from peer.
+ */
+PJ_DEF(pj_status_t)
+pj_media_session_update (pj_media_session_t *session,
+ const pjsdp_session_desc *sdp)
+{
+ unsigned i;
+ unsigned count;
+
+ /* Check SDP */
+ if (sdp_check (sdp) != 0) {
+ return -1;
+ }
+
+ /* If the media stream count doesn't match, only update one. */
+ if (session->stream_cnt != sdp->media_count) {
+ PJ_LOG(3,(THIS_FILE, "pj_media_session_update : "
+ "SDP media count mismatch! (rmt=%d, lcl=%d)",
+ sdp->media_count, session->stream_cnt));
+ count = (session->stream_cnt < sdp->media_count) ?
+ session->stream_cnt : sdp->media_count;
+ } else {
+ count = session->stream_cnt;
+ }
+
+ for (i=0; i<count; ++i) {
+ pj_media_stream_desc *sd = session->stream_desc[i];
+ const pjsdp_media_desc *m = sdp->media[i];
+ const pjsdp_conn_info *conn;
+ unsigned j;
+
+ /* Check that the session is not active. */
+ pj_assert (sd->enc_stream == NULL && sd->dec_stream == NULL);
+
+ conn = m->conn ? m->conn : sdp->conn;
+ pj_assert(conn);
+
+ /* Update remote address. */
+ sd->info.rem_port = m->desc.port;
+ pj_strdup (session->pool, &sd->info.rem_addr, &conn->addr);
+
+ /* Select one active codec according to what peer wants. */
+ for (j=0; j<sd->info.fmt_cnt; ++j) {
+ unsigned fmt_0 = pj_strtoul(&m->desc.fmt[0]);
+ if (sd->info.fmt[j].pt == fmt_0) {
+ pj_codec_id temp;
+
+ /* Put active format to the front. */
+ if (j == 0)
+ break;
+
+ pj_memcpy(&temp, &sd->info.fmt[0], sizeof(temp));
+ pj_memcpy(&sd->info.fmt[0], &sd->info.fmt[j], sizeof(temp));
+ pj_memcpy(&sd->info.fmt[j], &temp, sizeof(temp));
+ break;
+ }
+ }
+
+ if (j == sd->info.fmt_cnt) {
+ /* Peer has answered SDP with new codec, which doesn't exist
+ * in the offer!
+ * Mute this media.
+ */
+ PJ_LOG(3,(THIS_FILE, "Peer has answered SDP with new codec!"));
+ sd->info.dir = PJ_MEDIA_DIR_NONE;
+ continue;
+ }
+
+ /* Check direction. */
+ if (m->desc.port == 0 || pjsdp_media_desc_has_attr(m, PJSDP_ATTR_INACTIVE)) {
+ sd->info.dir = PJ_MEDIA_DIR_NONE;
+ }
+ else if (pjsdp_media_desc_has_attr(m, PJSDP_ATTR_RECV_ONLY)) {
+ sd->info.dir = PJ_MEDIA_DIR_ENCODING;
+ }
+ else if (pjsdp_media_desc_has_attr(m, PJSDP_ATTR_SEND_ONLY)) {
+ sd->info.dir = PJ_MEDIA_DIR_DECODING;
+ }
+ else {
+ sd->info.dir = PJ_MEDIA_DIR_ENCODING_DECODING;
+ }
+ }
+
+ return 0;
+}
+
+/**
+ * Enumerate media streams in the session.
+ */
+PJ_DEF(unsigned)
+pj_media_session_enum_streams (const pj_media_session_t *session,
+ unsigned count, const pj_media_stream_info *info[])
+{
+ unsigned i;
+
+ if (count > session->stream_cnt)
+ count = session->stream_cnt;
+
+ for (i=0; i<count; ++i) {
+ info[i] = &session->stream_desc[i]->info;
+ }
+
+ return session->stream_cnt;
+}
+
+/**
+ * Get statistics
+ */
+PJ_DEF(pj_status_t)
+pj_media_session_get_stat (const pj_media_session_t *session, unsigned index,
+ pj_media_stream_stat *tx_stat,
+ pj_media_stream_stat *rx_stat)
+{
+ pj_media_stream_desc *sd;
+ int stat_cnt = 0;
+
+ if (index >= session->stream_cnt) {
+ pj_assert(0);
+ return -1;
+ }
+
+ sd = session->stream_desc[index];
+
+ if (sd->enc_stream && tx_stat) {
+ pj_media_stream_get_stat (sd->enc_stream, tx_stat);
+ ++stat_cnt;
+ } else if (tx_stat) {
+ pj_memset (tx_stat, 0, sizeof(*tx_stat));
+ }
+
+ if (sd->dec_stream && rx_stat) {
+ pj_media_stream_get_stat (sd->dec_stream, rx_stat);
+ ++stat_cnt;
+ } else if (rx_stat) {
+ pj_memset (rx_stat, 0, sizeof(*rx_stat));
+ }
+
+ return stat_cnt ? 0 : -1;
+}
+
+/**
+ * Modify stream, only when stream is inactive.
+ */
+PJ_DEF(pj_status_t)
+pj_media_session_modify_stream (pj_media_session_t *session, unsigned index,
+ unsigned modify_flag, const pj_media_stream_info *info)
+{
+ pj_media_stream_desc *sd;
+
+ if (index >= session->stream_cnt) {
+ pj_assert(0);
+ return -1;
+ }
+
+ sd = session->stream_desc[index];
+
+ if (sd->enc_stream || sd->dec_stream) {
+ pj_assert(0);
+ return -1;
+ }
+
+ if (modify_flag & PJ_MEDIA_STREAM_MODIFY_DIR) {
+ sd->info.dir = info->dir;
+ }
+
+ return 0;
+}
+
+/**
+ * Activate media session.
+ */
+PJ_DEF(pj_status_t)
+pj_media_session_activate (pj_media_session_t *session)
+{
+ unsigned i;
+ pj_status_t status = 0;
+
+ for (i=0; i<session->stream_cnt; ++i) {
+ pj_status_t rc;
+ rc = pj_media_session_activate_stream (session, i);
+ if (status == 0)
+ status = rc;
+ }
+ return status;
+}
+
+/**
+ * Activate individual stream in media session.
+ */
+PJ_DEF(pj_status_t)
+pj_media_session_activate_stream (pj_media_session_t *session, unsigned index)
+{
+ pj_media_stream_desc *sd;
+ pj_media_stream_create_param scp;
+ pj_status_t status;
+ pj_time_val tv;
+
+ if (index < 0 || index >= session->stream_cnt) {
+ pj_assert(0);
+ return -1;
+ }
+
+ sd = session->stream_desc[index];
+
+ if (sd->enc_stream || sd->dec_stream) {
+ /* Stream already active. */
+ pj_assert(0);
+ return 0;
+ }
+
+ pj_gettimeofday(&tv);
+
+ /* Initialize parameter to create stream. */
+ pj_memset (&scp, 0, sizeof(scp));
+ scp.codec_id = &sd->info.fmt[0];
+ scp.mediamgr = session->mediamgr;
+ scp.dir = sd->info.dir;
+ scp.rtp_sock = sd->info.sock_info.rtp_sock;
+ scp.rtcp_sock = sd->info.sock_info.rtcp_sock;
+ scp.remote_addr = pj_pool_calloc (session->pool, 1, sizeof(pj_sockaddr_in));
+ pj_sockaddr_init (scp.remote_addr, &sd->info.rem_addr, sd->info.rem_port);
+ scp.ssrc = tv.sec;
+ scp.jb_min = 1;
+ scp.jb_max = 15;
+ scp.jb_maxcnt = 16;
+
+ /* Build the stream! */
+ status = pj_media_stream_create (session->pool, &sd->enc_stream, &sd->dec_stream, &scp);
+
+ if (status==0 && sd->enc_stream) {
+ status = pj_media_stream_start (sd->enc_stream);
+ if (status != 0)
+ goto on_error;
+ }
+ if (status==0 && sd->dec_stream) {
+ status = pj_media_stream_start (sd->dec_stream);
+ if (status != 0)
+ goto on_error;
+ }
+ return status;
+
+on_error:
+ if (sd->enc_stream) {
+ pj_media_stream_destroy (sd->enc_stream);
+ sd->enc_stream = NULL;
+ }
+ if (sd->dec_stream) {
+ pj_media_stream_destroy (sd->dec_stream);
+ sd->dec_stream = NULL;
+ }
+ return status;
+}
+
+/**
+ * Destroy media session.
+ */
+PJ_DEF(pj_status_t)
+pj_media_session_destroy (pj_media_session_t *session)
+{
+ unsigned i;
+
+ if (!session)
+ return -1;
+
+ for (i=0; i<session->stream_cnt; ++i) {
+ pj_media_stream_desc *sd = session->stream_desc[i];
+
+ if (sd->enc_stream) {
+ pj_media_stream_destroy (sd->enc_stream);
+ sd->enc_stream = NULL;
+ }
+ if (sd->dec_stream) {
+ pj_media_stream_destroy (sd->dec_stream);
+ sd->dec_stream = NULL;
+ }
+ }
+ pj_pool_release (session->pool);
+ return 0;
+}
+
diff --git a/pjmedia/src/pjmedia/session.h b/pjmedia/src/pjmedia/session.h
index 1159fe71..d9efeb81 100644
--- a/pjmedia/src/pjmedia/session.h
+++ b/pjmedia/src/pjmedia/session.h
@@ -1,152 +1,152 @@
-/* $Id$ */
-/*
- * Copyright (C) 2003-2006 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 __PJMEDIA_SESSION_H__
-#define __PJMEDIA_SESSION_H__
-
-
-/**
- * @file session.h
- * @brief Media Session.
- */
-
-#include <pj/types.h>
-#include <pjmedia/mediamgr.h>
-#include <pjmedia/stream.h>
-#include <pjmedia/sdp.h>
-
-PJ_BEGIN_DECL
-
-/**
- * @defgroup PJMED_SES Media session
- * @ingroup PJMEDIA
- * @{
- */
-
-/** Opaque declaration of media session. */
-typedef struct pj_media_session_t pj_media_session_t;
-
-/** Media socket info. */
-typedef struct pj_media_sock_info
-{
- pj_sock_t rtp_sock, rtcp_sock;
- pj_sockaddr_in rtp_addr_name;
-} pj_media_sock_info;
-
-/** Stream info. */
-typedef struct pj_media_stream_info
-{
- pj_str_t type;
- pj_media_dir_t dir;
- pj_str_t transport;
- pj_media_sock_info sock_info;
- pj_str_t rem_addr;
- unsigned short rem_port;
- unsigned fmt_cnt;
- pj_codec_id fmt[PJSDP_MAX_FMT];
-
-} pj_media_stream_info;
-
-/** Flag for modifying stream. */
-enum
-{
- PJ_MEDIA_STREAM_MODIFY_DIR = 1,
-};
-
-/**
- * Create new session offering.
- */
-PJ_DECL(pj_media_session_t*)
-pj_media_session_create ( pj_med_mgr_t *mgr, const pj_media_sock_info *skinfo );
-
-/**
- * Create new session based on peer's offering.
- */
-PJ_DECL(pj_media_session_t*)
-pj_media_session_create_from_sdp ( pj_med_mgr_t *mgr, const pjsdp_session_desc *sdp,
- const pj_media_sock_info *skinfo);
-
-/**
- * Duplicate session. The new session is inactive.
- */
-PJ_DECL(pj_media_session_t*)
-pj_media_session_clone (const pj_media_session_t *session);
-
-/**
- * Create SDP description from the session.
- */
-PJ_DECL(pjsdp_session_desc*)
-pj_media_session_create_sdp ( const pj_media_session_t *session, pj_pool_t *pool,
- pj_bool_t only_first_fmt);
-
-/**
- * Update session with SDP answer from peer. The session must NOT active.
- */
-PJ_DECL(pj_status_t)
-pj_media_session_update ( pj_media_session_t *session,
- const pjsdp_session_desc *sdp);
-
-/**
- * Enumerate media streams in the session.
- * @return the actual number of streams.
- */
-PJ_DECL(unsigned)
-pj_media_session_enum_streams (const pj_media_session_t *session,
- unsigned count, const pj_media_stream_info *info[]);
-
-/**
- * Get stream statistics.
- */
-PJ_DECL(pj_status_t)
-pj_media_session_get_stat (const pj_media_session_t *session, unsigned index,
- pj_media_stream_stat *tx_stat,
- pj_media_stream_stat *rx_stat);
-
-/**
- * Modify stream, only when stream is inactive.
- */
-PJ_DECL(pj_status_t)
-pj_media_session_modify_stream (pj_media_session_t *session, unsigned index,
- unsigned modify_flag, const pj_media_stream_info *info);
-
-/**
- * Activate all streams in media session.
- */
-PJ_DECL(pj_status_t)
-pj_media_session_activate (pj_media_session_t *session);
-
-/**
- * Activate individual stream in media session.
- */
-PJ_DECL(pj_status_t)
-pj_media_session_activate_stream (pj_media_session_t *session, unsigned index);
-
-/**
- * Destroy media session.
- */
-PJ_DECL(pj_status_t)
-pj_media_session_destroy (pj_media_session_t *session);
-
-
-/**
- * @}
- */
-
-PJ_END_DECL
-
-#endif /* __PJMEDIA_SESSION_H__ */
+/* $Id$ */
+/*
+ * Copyright (C) 2003-2006 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 __PJMEDIA_SESSION_H__
+#define __PJMEDIA_SESSION_H__
+
+
+/**
+ * @file session.h
+ * @brief Media Session.
+ */
+
+#include <pj/types.h>
+#include <pjmedia/mediamgr.h>
+#include <pjmedia/stream.h>
+#include <pjmedia/sdp.h>
+
+PJ_BEGIN_DECL
+
+/**
+ * @defgroup PJMED_SES Media session
+ * @ingroup PJMEDIA
+ * @{
+ */
+
+/** Opaque declaration of media session. */
+typedef struct pj_media_session_t pj_media_session_t;
+
+/** Media socket info. */
+typedef struct pj_media_sock_info
+{
+ pj_sock_t rtp_sock, rtcp_sock;
+ pj_sockaddr_in rtp_addr_name;
+} pj_media_sock_info;
+
+/** Stream info. */
+typedef struct pj_media_stream_info
+{
+ pj_str_t type;
+ pj_media_dir_t dir;
+ pj_str_t transport;
+ pj_media_sock_info sock_info;
+ pj_str_t rem_addr;
+ unsigned short rem_port;
+ unsigned fmt_cnt;
+ pj_codec_id fmt[PJSDP_MAX_FMT];
+
+} pj_media_stream_info;
+
+/** Flag for modifying stream. */
+enum
+{
+ PJ_MEDIA_STREAM_MODIFY_DIR = 1,
+};
+
+/**
+ * Create new session offering.
+ */
+PJ_DECL(pj_media_session_t*)
+pj_media_session_create ( pj_med_mgr_t *mgr, const pj_media_sock_info *skinfo );
+
+/**
+ * Create new session based on peer's offering.
+ */
+PJ_DECL(pj_media_session_t*)
+pj_media_session_create_from_sdp ( pj_med_mgr_t *mgr, const pjsdp_session_desc *sdp,
+ const pj_media_sock_info *skinfo);
+
+/**
+ * Duplicate session. The new session is inactive.
+ */
+PJ_DECL(pj_media_session_t*)
+pj_media_session_clone (const pj_media_session_t *session);
+
+/**
+ * Create SDP description from the session.
+ */
+PJ_DECL(pjsdp_session_desc*)
+pj_media_session_create_sdp ( const pj_media_session_t *session, pj_pool_t *pool,
+ pj_bool_t only_first_fmt);
+
+/**
+ * Update session with SDP answer from peer. The session must NOT active.
+ */
+PJ_DECL(pj_status_t)
+pj_media_session_update ( pj_media_session_t *session,
+ const pjsdp_session_desc *sdp);
+
+/**
+ * Enumerate media streams in the session.
+ * @return the actual number of streams.
+ */
+PJ_DECL(unsigned)
+pj_media_session_enum_streams (const pj_media_session_t *session,
+ unsigned count, const pj_media_stream_info *info[]);
+
+/**
+ * Get stream statistics.
+ */
+PJ_DECL(pj_status_t)
+pj_media_session_get_stat (const pj_media_session_t *session, unsigned index,
+ pj_media_stream_stat *tx_stat,
+ pj_media_stream_stat *rx_stat);
+
+/**
+ * Modify stream, only when stream is inactive.
+ */
+PJ_DECL(pj_status_t)
+pj_media_session_modify_stream (pj_media_session_t *session, unsigned index,
+ unsigned modify_flag, const pj_media_stream_info *info);
+
+/**
+ * Activate all streams in media session.
+ */
+PJ_DECL(pj_status_t)
+pj_media_session_activate (pj_media_session_t *session);
+
+/**
+ * Activate individual stream in media session.
+ */
+PJ_DECL(pj_status_t)
+pj_media_session_activate_stream (pj_media_session_t *session, unsigned index);
+
+/**
+ * Destroy media session.
+ */
+PJ_DECL(pj_status_t)
+pj_media_session_destroy (pj_media_session_t *session);
+
+
+/**
+ * @}
+ */
+
+PJ_END_DECL
+
+#endif /* __PJMEDIA_SESSION_H__ */
diff --git a/pjmedia/src/pjmedia/sound.h b/pjmedia/src/pjmedia/sound.h
index f1dce295..3fa40db6 100644
--- a/pjmedia/src/pjmedia/sound.h
+++ b/pjmedia/src/pjmedia/sound.h
@@ -1,201 +1,201 @@
-/* $Id$ */
-/*
- * Copyright (C) 2003-2006 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 __PJMEDIA_SOUND_H__
-#define __PJMEDIA_SOUND_H__
-
-
-/**
- * @file sound.h
- * @brief Sound player and recorder device framework.
- */
-
-#include <pj/pool.h>
-
-PJ_BEGIN_DECL
-
-/**
- * @defgroup PJMED_SND Sound device abstraction.
- * @ingroup PJMEDIA
- * @{
- */
-
-/** Opaque data type for audio stream. */
-typedef struct pj_snd_stream pj_snd_stream;
-
-/**
- * Device information structure returned by #pj_snd_get_dev_info.
- */
-typedef struct pj_snd_dev_info
-{
- char name[64]; /**< Device name. */
- unsigned input_count; /**< Max number of input channels. */
- unsigned output_count; /**< Max number of output channels. */
- unsigned default_samples_per_sec;/**< Default sampling rate. */
-} pj_snd_dev_info;
-
-/**
- * Sound device parameter, to be specified when calling #pj_snd_open_recorder
- * or #pj_snd_open_player.
- */
-typedef struct pj_snd_stream_info
-{
- unsigned samples_per_sec; /* Sampling rate. */
- unsigned bits_per_sample; /* No of bits per sample. */
- unsigned samples_per_frame; /* No of samples per frame. */
- unsigned bytes_per_frame; /* No of bytes per frame. */
- unsigned frames_per_packet; /* No of frames per packet. */
-} pj_snd_stream_info;
-
-/**
- * This callback is called by player stream when it needs additional data
- * to be played by the device. Application must fill in the whole of output
- * buffer with sound samples.
- *
- * @param user_data User data associated with the stream.
- * @param timestamp Timestamp, in samples.
- * @param output Buffer to be filled out by application.
- * @param size The size requested in bytes, which will be equal to
- * the size of one whole packet.
- *
- * @return Non-zero to stop the stream.
- */
-typedef pj_status_t (*pj_snd_play_cb)(/* in */ void *user_data,
- /* in */ pj_uint32_t timestamp,
- /* out */ void *output,
- /* out */ unsigned size);
-
-/**
- * This callback is called by recorder stream when it has captured the whole
- * packet worth of audio samples.
- *
- * @param user_data User data associated with the stream.
- * @param timestamp Timestamp, in samples.
- * @param output Buffer containing the captured audio samples.
- * @param size The size of the data in the buffer, in bytes.
- *
- * @return Non-zero to stop the stream.
- */
-typedef pj_status_t (*pj_snd_rec_cb)(/* in */ void *user_data,
- /* in */ pj_uint32_t timestamp,
- /* in */ const void *input,
- /* in*/ unsigned size);
-
-/**
- * Init the sound library.
- *
- * @param factory The sound factory.
- *
- * @return Zero on success.
- */
-PJ_DECL(pj_status_t) pj_snd_init(pj_pool_factory *factory);
-
-
-/**
- * Get the number of devices detected by the library.
- *
- * @return Number of devices.
- */
-PJ_DECL(int) pj_snd_get_dev_count(void);
-
-
-/**
- * Get device info.
- *
- * @param index The index of the device, which should be in the range
- * from zero to #pj_snd_get_dev_count - 1.
- */
-PJ_DECL(const pj_snd_dev_info*) pj_snd_get_dev_info(unsigned index);
-
-
-/**
- * Create a new audio stream for audio capture purpose.
- *
- * @param index Device index, or -1 to let the library choose the first
- * available device, or -2 to use NULL device.
- * @param param Stream parameters.
- * @param rec_cb Callback to handle captured audio samples.
- * @param user_data User data to be associated with the stream.
- *
- * @return Audio stream, or NULL if failed.
- */
-PJ_DECL(pj_snd_stream*) pj_snd_open_recorder(int index,
- const pj_snd_stream_info *param,
- pj_snd_rec_cb rec_cb,
- void *user_data);
-
-/**
- * Create a new audio stream for playing audio samples.
- *
- * @param index Device index, or -1 to let the library choose the first
- * available device, or -2 to use NULL device.
- * @param param Stream parameters.
- * @param play_cb Callback to supply audio samples.
- * @param user_data User data to be associated with the stream.
- *
- * @return Audio stream, or NULL if failed.
- */
-PJ_DECL(pj_snd_stream*) pj_snd_open_player(int index,
- const pj_snd_stream_info *param,
- pj_snd_play_cb play_cb,
- void *user_data);
-
-/**
- * Start the stream.
- *
- * @param stream The recorder or player stream.
- *
- * @return Zero on success.
- */
-PJ_DECL(pj_status_t) pj_snd_stream_start(pj_snd_stream *stream);
-
-/**
- * Stop the stream.
- *
- * @param stream The recorder or player stream.
- *
- * @return Zero on success.
- */
-PJ_DECL(pj_status_t) pj_snd_stream_stop(pj_snd_stream *stream);
-
-/**
- * Destroy the stream.
- *
- * @param stream The recorder of player stream.
- *
- * @return Zero on success.
- */
-PJ_DECL(pj_status_t) pj_snd_stream_close(pj_snd_stream *stream);
-
-/**
- * Deinitialize sound library.
- *
- * @return Zero on success.
- */
-PJ_DECL(pj_status_t) pj_snd_deinit(void);
-
-
-
-/**
- * @}
- */
-
-PJ_END_DECL
-
-
-#endif /* __PJMEDIA_SOUND_H__ */
+/* $Id$ */
+/*
+ * Copyright (C) 2003-2006 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 __PJMEDIA_SOUND_H__
+#define __PJMEDIA_SOUND_H__
+
+
+/**
+ * @file sound.h
+ * @brief Sound player and recorder device framework.
+ */
+
+#include <pj/pool.h>
+
+PJ_BEGIN_DECL
+
+/**
+ * @defgroup PJMED_SND Sound device abstraction.
+ * @ingroup PJMEDIA
+ * @{
+ */
+
+/** Opaque data type for audio stream. */
+typedef struct pj_snd_stream pj_snd_stream;
+
+/**
+ * Device information structure returned by #pj_snd_get_dev_info.
+ */
+typedef struct pj_snd_dev_info
+{
+ char name[64]; /**< Device name. */
+ unsigned input_count; /**< Max number of input channels. */
+ unsigned output_count; /**< Max number of output channels. */
+ unsigned default_samples_per_sec;/**< Default sampling rate. */
+} pj_snd_dev_info;
+
+/**
+ * Sound device parameter, to be specified when calling #pj_snd_open_recorder
+ * or #pj_snd_open_player.
+ */
+typedef struct pj_snd_stream_info
+{
+ unsigned samples_per_sec; /* Sampling rate. */
+ unsigned bits_per_sample; /* No of bits per sample. */
+ unsigned samples_per_frame; /* No of samples per frame. */
+ unsigned bytes_per_frame; /* No of bytes per frame. */
+ unsigned frames_per_packet; /* No of frames per packet. */
+} pj_snd_stream_info;
+
+/**
+ * This callback is called by player stream when it needs additional data
+ * to be played by the device. Application must fill in the whole of output
+ * buffer with sound samples.
+ *
+ * @param user_data User data associated with the stream.
+ * @param timestamp Timestamp, in samples.
+ * @param output Buffer to be filled out by application.
+ * @param size The size requested in bytes, which will be equal to
+ * the size of one whole packet.
+ *
+ * @return Non-zero to stop the stream.
+ */
+typedef pj_status_t (*pj_snd_play_cb)(/* in */ void *user_data,
+ /* in */ pj_uint32_t timestamp,
+ /* out */ void *output,
+ /* out */ unsigned size);
+
+/**
+ * This callback is called by recorder stream when it has captured the whole
+ * packet worth of audio samples.
+ *
+ * @param user_data User data associated with the stream.
+ * @param timestamp Timestamp, in samples.
+ * @param output Buffer containing the captured audio samples.
+ * @param size The size of the data in the buffer, in bytes.
+ *
+ * @return Non-zero to stop the stream.
+ */
+typedef pj_status_t (*pj_snd_rec_cb)(/* in */ void *user_data,
+ /* in */ pj_uint32_t timestamp,
+ /* in */ const void *input,
+ /* in*/ unsigned size);
+
+/**
+ * Init the sound library.
+ *
+ * @param factory The sound factory.
+ *
+ * @return Zero on success.
+ */
+PJ_DECL(pj_status_t) pj_snd_init(pj_pool_factory *factory);
+
+
+/**
+ * Get the number of devices detected by the library.
+ *
+ * @return Number of devices.
+ */
+PJ_DECL(int) pj_snd_get_dev_count(void);
+
+
+/**
+ * Get device info.
+ *
+ * @param index The index of the device, which should be in the range
+ * from zero to #pj_snd_get_dev_count - 1.
+ */
+PJ_DECL(const pj_snd_dev_info*) pj_snd_get_dev_info(unsigned index);
+
+
+/**
+ * Create a new audio stream for audio capture purpose.
+ *
+ * @param index Device index, or -1 to let the library choose the first
+ * available device, or -2 to use NULL device.
+ * @param param Stream parameters.
+ * @param rec_cb Callback to handle captured audio samples.
+ * @param user_data User data to be associated with the stream.
+ *
+ * @return Audio stream, or NULL if failed.
+ */
+PJ_DECL(pj_snd_stream*) pj_snd_open_recorder(int index,
+ const pj_snd_stream_info *param,
+ pj_snd_rec_cb rec_cb,
+ void *user_data);
+
+/**
+ * Create a new audio stream for playing audio samples.
+ *
+ * @param index Device index, or -1 to let the library choose the first
+ * available device, or -2 to use NULL device.
+ * @param param Stream parameters.
+ * @param play_cb Callback to supply audio samples.
+ * @param user_data User data to be associated with the stream.
+ *
+ * @return Audio stream, or NULL if failed.
+ */
+PJ_DECL(pj_snd_stream*) pj_snd_open_player(int index,
+ const pj_snd_stream_info *param,
+ pj_snd_play_cb play_cb,
+ void *user_data);
+
+/**
+ * Start the stream.
+ *
+ * @param stream The recorder or player stream.
+ *
+ * @return Zero on success.
+ */
+PJ_DECL(pj_status_t) pj_snd_stream_start(pj_snd_stream *stream);
+
+/**
+ * Stop the stream.
+ *
+ * @param stream The recorder or player stream.
+ *
+ * @return Zero on success.
+ */
+PJ_DECL(pj_status_t) pj_snd_stream_stop(pj_snd_stream *stream);
+
+/**
+ * Destroy the stream.
+ *
+ * @param stream The recorder of player stream.
+ *
+ * @return Zero on success.
+ */
+PJ_DECL(pj_status_t) pj_snd_stream_close(pj_snd_stream *stream);
+
+/**
+ * Deinitialize sound library.
+ *
+ * @return Zero on success.
+ */
+PJ_DECL(pj_status_t) pj_snd_deinit(void);
+
+
+
+/**
+ * @}
+ */
+
+PJ_END_DECL
+
+
+#endif /* __PJMEDIA_SOUND_H__ */
diff --git a/pjmedia/src/pjmedia/stream.c b/pjmedia/src/pjmedia/stream.c
index 1a7080a4..718cace1 100644
--- a/pjmedia/src/pjmedia/stream.c
+++ b/pjmedia/src/pjmedia/stream.c
@@ -1,644 +1,644 @@
-/* $Id$ */
-/*
- * Copyright (C) 2003-2006 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 <pjmedia/stream.h>
-#include <pjmedia/rtp.h>
-#include <pjmedia/rtcp.h>
-#include <pjmedia/jbuf.h>
-#include <pj/os.h>
-#include <pj/log.h>
-#include <pj/string.h> /* memcpy() */
-#include <pj/pool.h>
-#include <stdlib.h>
-
-#define THISFILE "stream.c"
-#define ERRLEVEL 1
-
-#define PJ_MAX_FRAME_DURATION_MS 200
-#define PJ_MAX_BUFFER_SIZE_MS 2000
-#define PJ_MAX_MTU 1500
-
-struct jb_frame
-{
- unsigned size;
- void *buf;
-};
-
-#define pj_fifobuf_alloc(fifo,size) malloc(size)
-#define pj_fifobuf_unalloc(fifo,buf) free(buf)
-#define pj_fifobuf_free(fifo, buf) free(buf)
-
-enum stream_state
-{
- STREAM_STOPPED,
- STREAM_STARTED,
-};
-
-struct pj_media_stream_t
-{
- pj_media_dir_t dir;
- int pt;
- int state;
- pj_media_stream_stat stat;
- pj_media_stream_t *peer;
- pj_snd_stream_info snd_info;
- pj_snd_stream *snd_stream;
- pj_mutex_t *mutex;
- unsigned in_pkt_size;
- void *in_pkt;
- unsigned out_pkt_size;
- void *out_pkt;
- unsigned pcm_buf_size;
- void *pcm_buf;
- //pj_fifobuf_t fifobuf;
- pj_codec_mgr *codec_mgr;
- pj_codec *codec;
- pj_rtp_session rtp;
- pj_rtcp_session *rtcp;
- pj_jitter_buffer *jb;
- pj_sock_t rtp_sock;
- pj_sock_t rtcp_sock;
- pj_sockaddr_in dst_addr;
- pj_thread_t *transport_thread;
- int thread_quit_flag;
-};
-
-
-static pj_status_t play_callback(/* in */ void *user_data,
- /* in */ pj_uint32_t timestamp,
- /* out */ void *frame,
- /*inout*/ unsigned size)
-{
- pj_media_stream_t *channel = user_data;
- struct jb_frame *jb_frame;
- void *p;
- pj_uint32_t extseq;
- pj_status_t status;
- struct pj_audio_frame frame_in, frame_out;
-
- PJ_UNUSED_ARG(timestamp)
-
- /* Lock mutex */
- pj_mutex_lock (channel->mutex);
-
- if (!channel->codec) {
- pj_mutex_unlock (channel->mutex);
- return -1;
- }
-
- /* Get frame from jitter buffer. */
- status = pj_jb_get (channel->jb, &extseq, &p);
- jb_frame = p;
- if (status != 0 || jb_frame == NULL) {
- pj_memset(frame, 0, size);
- pj_mutex_unlock(channel->mutex);
- return 0;
- }
-
- /* Decode */
- frame_in.buf = jb_frame->buf;
- frame_in.size = jb_frame->size;
- frame_in.type = PJ_AUDIO_FRAME_AUDIO; /* ignored */
- frame_out.buf = channel->pcm_buf;
- status = channel->codec->op->decode (channel->codec, &frame_in,
- channel->pcm_buf_size, &frame_out);
- if (status != 0) {
- PJ_LOG(3, (THISFILE, "decode() has return error status %d",
- status));
-
- pj_memset(frame, 0, size);
- pj_fifobuf_free (&channel->fifobuf, jb_frame);
- pj_mutex_unlock(channel->mutex);
- return 0;
- }
-
- /* Put in sound buffer. */
- if (frame_out.size > size) {
- PJ_LOG(3, (THISFILE, "Sound playout buffer truncated %d bytes",
- frame_out.size - size));
- frame_out.size = size;
- }
-
- pj_memcpy(frame, frame_out.buf, size);
-
- pj_fifobuf_free (&channel->fifobuf, jb_frame);
- pj_mutex_unlock(channel->mutex);
- return 0;
-}
-
-static pj_status_t rec_callback( /* in */ void *user_data,
- /* in */ pj_uint32_t timestamp,
- /* in */ const void *frame,
- /* in */ unsigned size)
-{
- pj_media_stream_t *channel = user_data;
- pj_status_t status = 0;
- struct pj_audio_frame frame_in, frame_out;
- int ts_len;
- void *rtphdr;
- int rtphdrlen;
- int sent;
-#if 0
- static FILE *fhnd = NULL;
-#endif
-
- PJ_UNUSED_ARG(timestamp)
-
- /* Start locking channel mutex */
- pj_mutex_lock (channel->mutex);
-
- if (!channel->codec) {
- status = -1;
- goto on_return;
- }
-
- /* Encode. */
- frame_in.type = PJ_MEDIA_TYPE_AUDIO;
- frame_in.buf = (void*)frame;
- frame_in.size = size;
- frame_out.buf = ((char*)channel->out_pkt) + sizeof(pj_rtp_hdr);
- status = channel->codec->op->encode (channel->codec, &frame_in,
- channel->out_pkt_size - sizeof(pj_rtp_hdr),
- &frame_out);
- if (status != 0) {
- PJ_LOG(3,(THISFILE, "Codec encode() has returned error status %d",
- status));
- goto on_return;
- }
-
- /* Encapsulate. */
- ts_len = size / (channel->snd_info.bits_per_sample / 8);
- status = pj_rtp_encode_rtp (&channel->rtp, channel->pt, 0,
- frame_out.size, ts_len,
- (const void**)&rtphdr, &rtphdrlen);
- if (status != 0) {
- PJ_LOG(3,(THISFILE, "RTP encode_rtp() has returned error status %d",
- status));
- goto on_return;
- }
-
- if (rtphdrlen != sizeof(pj_rtp_hdr)) {
- /* We don't support RTP with extended header yet. */
- PJ_TODO(SUPPORT_SENDING_RTP_WITH_EXTENDED_HEADER);
- PJ_LOG(3,(THISFILE, "Unsupported extended RTP header for transmission"));
- goto on_return;
- }
-
- pj_memcpy(channel->out_pkt, rtphdr, sizeof(pj_rtp_hdr));
-
- /* Send. */
- sent = pj_sock_sendto (channel->rtp_sock, channel->out_pkt, frame_out.size+sizeof(pj_rtp_hdr), 0,
- &channel->dst_addr, sizeof(channel->dst_addr));
- if (sent != (int)frame_out.size + (int)sizeof(pj_rtp_hdr)) {
- pj_perror(THISFILE, "Error sending RTP packet to %s:%d",
- pj_sockaddr_get_str_addr(&channel->dst_addr),
- pj_sockaddr_get_port(&channel->dst_addr));
- goto on_return;
- }
-
- /* Update stat */
- channel->stat.pkt_tx++;
- channel->stat.oct_tx += frame_out.size+sizeof(pj_rtp_hdr);
-
-#if 0
- if (fhnd == NULL) {
- fhnd = fopen("RTP.DAT", "wb");
- if (fhnd) {
- fwrite (channel->out_pkt, frame_out.size+sizeof(pj_rtp_hdr), 1, fhnd);
- fclose(fhnd);
- }
- }
-#endif
-
-on_return:
- pj_mutex_unlock (channel->mutex);
- return status;
-}
-
-
-static void* PJ_THREAD_FUNC stream_decoder_transport_thread (void*arg)
-{
- pj_media_stream_t *channel = arg;
-
- while (!channel->thread_quit_flag) {
- int len, size;
- const pj_rtp_hdr *hdr;
- const void *payload;
- unsigned payloadlen;
- int status;
- struct jb_frame *jb_frame;
-
- /* Wait for packet. */
- fd_set fds;
- pj_time_val timeout;
-
- PJ_FD_ZERO (&fds);
- PJ_FD_SET (channel->rtp_sock, &fds);
- timeout.sec = 0;
- timeout.msec = 100;
-
- /* Wait with timeout. */
- status = pj_sock_select(channel->rtp_sock, &fds, NULL, NULL, &timeout);
- if (status != 1)
- continue;
-
- /* Get packet from socket. */
- len = pj_sock_recv (channel->rtp_sock, channel->in_pkt, channel->in_pkt_size, 0);
- if (len < 1) {
- if (pj_getlasterror() == PJ_ECONNRESET) {
- /* On Win2K SP2 (or above) and WinXP, recv() will get WSAECONNRESET
- when the sending side receives ICMP port unreachable.
- */
- continue;
- }
- pj_perror(THISFILE, "Error receiving packet from socket (len=%d)", len);
- pj_thread_sleep(1);
- continue;
- }
-
- if (channel->state != STREAM_STARTED)
- continue;
-
- if (channel->thread_quit_flag)
- break;
-
- /* Start locking the channel. */
- pj_mutex_lock (channel->mutex);
-
- /* Update RTP and RTCP session. */
- status = pj_rtp_decode_rtp (&channel->rtp, channel->in_pkt, len, &hdr, &payload, &payloadlen);
- if (status != 0) {
- pj_mutex_unlock (channel->mutex);
- PJ_LOG(4,(THISFILE, "RTP decode_rtp() has returned error status %d", status));
- continue;
- }
- status = pj_rtp_session_update (&channel->rtp, hdr);
- if (status != 0 && status != PJ_RTP_ERR_SESSION_PROBATION && status != PJ_RTP_ERR_SESSION_RESTARTED) {
- pj_mutex_unlock (channel->mutex);
- PJ_LOG(4,(THISFILE, "RTP session_update() has returned error status %d", status));
- continue;
- }
- pj_rtcp_rx_rtp (channel->rtcp, pj_ntohs(hdr->seq), pj_ntohl(hdr->ts));
-
- /* Update stat */
- channel->stat.pkt_rx++;
- channel->stat.oct_rx += len;
-
- /* Copy to FIFO buffer. */
- size = payloadlen+sizeof(struct jb_frame);
- jb_frame = pj_fifobuf_alloc (&channel->fifobuf, size);
- if (jb_frame == NULL) {
- pj_mutex_unlock (channel->mutex);
- PJ_LOG(4,(THISFILE, "Unable to allocate %d bytes FIFO buffer", size));
- continue;
- }
-
- /* Copy the payload */
- jb_frame->size = payloadlen;
- jb_frame->buf = ((char*)jb_frame) + sizeof(struct jb_frame);
- pj_memcpy (jb_frame->buf, payload, payloadlen);
-
- /* Put to jitter buffer. */
- status = pj_jb_put (channel->jb, pj_ntohs(hdr->seq), jb_frame);
- if (status != 0) {
- pj_fifobuf_unalloc (&channel->fifobuf, jb_frame);
- pj_mutex_unlock (channel->mutex);
- PJ_LOG(4,(THISFILE, "Jitter buffer put() has returned error status %d", status));
- continue;
- }
-
- pj_mutex_unlock (channel->mutex);
- }
-
- return NULL;
-}
-
-static void init_snd_param_from_codec_attr (pj_snd_stream_info *param,
- const pj_codec_attr *attr)
-{
- param->bits_per_sample = attr->pcm_bits_per_sample;
- param->bytes_per_frame = 2;
- param->frames_per_packet = attr->sample_rate * attr->ptime / 1000;
- param->samples_per_frame = 1;
- param->samples_per_sec = attr->sample_rate;
-}
-
-static pj_media_stream_t *create_channel ( pj_pool_t *pool,
- pj_media_dir_t dir,
- pj_media_stream_t *peer,
- pj_codec_id *codec_id,
- pj_media_stream_create_param *param)
-{
- pj_media_stream_t *channel;
- pj_codec_attr codec_attr;
- void *ptr;
- unsigned size;
- int status;
-
- /* Allocate memory for channel descriptor */
- size = sizeof(pj_media_stream_t);
- channel = pj_pool_calloc(pool, 1, size);
- if (!channel) {
- PJ_LOG(1,(THISFILE, "Unable to allocate %u bytes channel descriptor",
- size));
- return NULL;
- }
-
- channel->dir = dir;
- channel->pt = codec_id->pt;
- channel->peer = peer;
- channel->codec_mgr = pj_med_mgr_get_codec_mgr (param->mediamgr);
- channel->rtp_sock = param->rtp_sock;
- channel->rtcp_sock = param->rtcp_sock;
- channel->dst_addr = *param->remote_addr;
- channel->state = STREAM_STOPPED;
-
- /* Create mutex for the channel. */
- channel->mutex = pj_mutex_create(pool, NULL, PJ_MUTEX_SIMPLE);
- if (channel->mutex == NULL)
- goto err_cleanup;
-
- /* Create and initialize codec, only if peer is not present.
- We only use one codec instance for both encoder and decoder.
- */
- if (peer && peer->codec) {
- channel->codec = peer->codec;
- status = channel->codec->factory->op->default_attr(channel->codec->factory, codec_id,
- &codec_attr);
- if (status != 0) {
- goto err_cleanup;
- }
-
- } else {
- channel->codec = pj_codec_mgr_alloc_codec(channel->codec_mgr, codec_id);
- if (channel->codec == NULL) {
- goto err_cleanup;
- }
-
- status = channel->codec->factory->op->default_attr(channel->codec->factory, codec_id,
- &codec_attr);
- if (status != 0) {
- goto err_cleanup;
- }
-
- codec_attr.pt = codec_id->pt;
- status = channel->codec->op->open(channel->codec, &codec_attr);
- if (status != 0) {
- goto err_cleanup;
- }
- }
-
- /* Allocate buffer for incoming packet. */
- channel->in_pkt_size = PJ_MAX_MTU;
- channel->in_pkt = pj_pool_alloc(pool, channel->in_pkt_size);
- if (!channel->in_pkt) {
- PJ_LOG(1, (THISFILE, "Unable to allocate %u bytes incoming packet buffer",
- channel->in_pkt_size));
- goto err_cleanup;
- }
-
- /* Allocate buffer for outgoing packet. */
- channel->out_pkt_size = sizeof(pj_rtp_hdr) +
- codec_attr.avg_bps / 8 * PJ_MAX_FRAME_DURATION_MS / 1000;
- if (channel->out_pkt_size > PJ_MAX_MTU)
- channel->out_pkt_size = PJ_MAX_MTU;
- channel->out_pkt = pj_pool_alloc(pool, channel->out_pkt_size);
- if (!channel->out_pkt) {
- PJ_LOG(1, (THISFILE, "Unable to allocate %u bytes encoding buffer",
- channel->out_pkt_size));
- goto err_cleanup;
- }
-
- /* Allocate buffer for decoding to PCM */
- channel->pcm_buf_size = codec_attr.sample_rate *
- codec_attr.pcm_bits_per_sample / 8 *
- PJ_MAX_FRAME_DURATION_MS / 1000;
- channel->pcm_buf = pj_pool_alloc (pool, channel->pcm_buf_size);
- if (!channel->pcm_buf) {
- PJ_LOG(1, (THISFILE, "Unable to allocate %u bytes PCM buffer",
- channel->pcm_buf_size));
- goto err_cleanup;
- }
-
- /* Allocate buffer for frames put in jitter buffer. */
- size = codec_attr.avg_bps / 8 * PJ_MAX_BUFFER_SIZE_MS / 1000;
- ptr = pj_pool_alloc(pool, size);
- if (!ptr) {
- PJ_LOG(1, (THISFILE, "Unable to allocate %u bytes jitter buffer",
- channel->pcm_buf_size));
- goto err_cleanup;
- }
- //pj_fifobuf_init (&channel->fifobuf, ptr, size);
-
- /* Create and initialize sound device */
- init_snd_param_from_codec_attr (&channel->snd_info, &codec_attr);
-
- if (dir == PJ_MEDIA_DIR_ENCODING)
- channel->snd_stream = pj_snd_open_recorder(-1, &channel->snd_info,
- &rec_callback, channel);
- else
- channel->snd_stream = pj_snd_open_player(-1, &channel->snd_info,
- &play_callback, channel);
-
- if (!channel->snd_stream)
- goto err_cleanup;
-
- /* Create RTP and RTCP sessions. */
- if (pj_rtp_session_init(&channel->rtp, codec_id->pt, param->ssrc) != 0) {
- PJ_LOG(1, (THISFILE, "RTP session initialization error"));
- goto err_cleanup;
- }
-
- /* For decoder, create RTCP session, jitter buffer, and transport thread. */
- if (dir == PJ_MEDIA_DIR_DECODING) {
- channel->rtcp = pj_pool_calloc(pool, 1, sizeof(pj_rtcp_session));
- if (!channel->rtcp) {
- PJ_LOG(1, (THISFILE, "Unable to allocate RTCP session"));
- goto err_cleanup;
- }
-
- pj_rtcp_init(channel->rtcp, param->ssrc);
-
- channel->jb = pj_pool_calloc(pool, 1, sizeof(pj_jitter_buffer));
- if (!channel->jb) {
- PJ_LOG(1, (THISFILE, "Unable to allocate jitter buffer descriptor"));
- goto err_cleanup;
- }
- if (pj_jb_init(channel->jb, pool, param->jb_min, param->jb_max, param->jb_maxcnt)) {
- PJ_LOG(1, (THISFILE, "Unable to allocate jitter buffer"));
- goto err_cleanup;
- }
-
- channel->transport_thread = pj_thread_create(pool, "decode",
- &stream_decoder_transport_thread, channel,
- 0, NULL, 0);
- if (!channel->transport_thread) {
- pj_perror(THISFILE, "Unable to create transport thread");
- goto err_cleanup;
- }
- }
-
- /* Done. */
- return channel;
-
-err_cleanup:
- pj_media_stream_destroy(channel);
- return NULL;
-}
-
-
-PJ_DEF(pj_status_t) pj_media_stream_create (pj_pool_t *pool,
- pj_media_stream_t **enc_stream,
- pj_media_stream_t **dec_stream,
- pj_media_stream_create_param *param)
-{
- *dec_stream = *enc_stream = NULL;
-
- if (param->dir & PJ_MEDIA_DIR_DECODING) {
- *dec_stream =
- create_channel(pool, PJ_MEDIA_DIR_DECODING, NULL, param->codec_id, param);
- if (!*dec_stream)
- return -1;
- }
-
- if (param->dir & PJ_MEDIA_DIR_ENCODING) {
- *enc_stream =
- create_channel(pool, PJ_MEDIA_DIR_ENCODING, *dec_stream, param->codec_id, param);
- if (!*enc_stream) {
- if (*dec_stream) {
- pj_media_stream_destroy(*dec_stream);
- *dec_stream = NULL;
- }
- return -1;
- }
-
- if (*dec_stream) {
- (*dec_stream)->peer = *enc_stream;
- }
- }
-
- return 0;
-}
-
-PJ_DEF(pj_status_t) pj_media_stream_start (pj_media_stream_t *channel)
-{
- pj_status_t status;
-
- status = pj_snd_stream_start(channel->snd_stream);
-
- if (status == 0)
- channel->state = STREAM_STARTED;
- return status;
-}
-
-PJ_DEF(pj_status_t) pj_media_stream_get_stat (const pj_media_stream_t *stream,
- pj_media_stream_stat *stat)
-{
- if (stream->dir == PJ_MEDIA_DIR_ENCODING) {
- pj_memcpy (stat, &stream->stat, sizeof(*stat));
- } else {
- pj_rtcp_pkt *rtcp_pkt;
- int len;
-
- pj_memset (stat, 0, sizeof(*stat));
- pj_assert (stream->rtcp != 0);
- pj_rtcp_build_rtcp (stream->rtcp, &rtcp_pkt, &len);
-
- stat->pkt_rx = stream->stat.pkt_rx;
- stat->oct_rx = stream->stat.oct_rx;
-
- PJ_TODO(SUPPORT_JITTER_CALCULATION_FOR_NON_8KHZ_SAMPLE_RATE)
- stat->jitter = pj_ntohl(rtcp_pkt->rr.jitter) / 8;
- stat->pkt_lost = (rtcp_pkt->rr.total_lost_2 << 16) +
- (rtcp_pkt->rr.total_lost_1 << 8) +
- rtcp_pkt->rr.total_lost_0;
- }
- return 0;
-}
-
-PJ_DEF(pj_status_t) pj_media_stream_pause (pj_media_stream_t *channel)
-{
- PJ_UNUSED_ARG(channel)
- return -1;
-}
-
-PJ_DEF(pj_status_t) pj_media_stream_resume (pj_media_stream_t *channel)
-{
- PJ_UNUSED_ARG(channel)
- return -1;
-}
-
-PJ_DEF(pj_status_t) pj_media_stream_destroy (pj_media_stream_t *channel)
-{
- channel->thread_quit_flag = 1;
-
- pj_mutex_lock (channel->mutex);
- if (channel->peer)
- pj_mutex_lock (channel->peer->mutex);
-
- if (channel->jb) {
- /* No need to deinitialize jitter buffer. */
- }
- if (channel->transport_thread) {
- pj_thread_join(channel->transport_thread);
- pj_thread_destroy(channel->transport_thread);
- channel->transport_thread = NULL;
- }
- if (channel->snd_stream != NULL) {
- pj_mutex_unlock (channel->mutex);
- pj_snd_stream_stop(channel->snd_stream);
- pj_mutex_lock (channel->mutex);
- pj_snd_stream_close(channel->snd_stream);
- channel->snd_stream = NULL;
- }
- if (channel->codec) {
- channel->codec->op->close(channel->codec);
- pj_codec_mgr_dealloc_codec(channel->codec_mgr, channel->codec);
- channel->codec = NULL;
- }
- if (channel->peer) {
- pj_media_stream_t *peer = channel->peer;
- peer->peer = NULL;
- peer->codec = NULL;
- peer->thread_quit_flag = 1;
- if (peer->transport_thread) {
- pj_mutex_unlock (peer->mutex);
- pj_thread_join(peer->transport_thread);
- pj_mutex_lock (peer->mutex);
- pj_thread_destroy(peer->transport_thread);
- peer->transport_thread = NULL;
- }
- if (peer->snd_stream) {
- pj_mutex_unlock (peer->mutex);
- pj_snd_stream_stop(peer->snd_stream);
- pj_mutex_lock (peer->mutex);
- pj_snd_stream_close(peer->snd_stream);
- peer->snd_stream = NULL;
- }
- }
-
- channel->state = STREAM_STOPPED;
-
- if (channel->peer)
- pj_mutex_unlock (channel->peer->mutex);
- pj_mutex_unlock(channel->mutex);
- pj_mutex_destroy(channel->mutex);
-
- return 0;
-}
-
+/* $Id$ */
+/*
+ * Copyright (C) 2003-2006 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 <pjmedia/stream.h>
+#include <pjmedia/rtp.h>
+#include <pjmedia/rtcp.h>
+#include <pjmedia/jbuf.h>
+#include <pj/os.h>
+#include <pj/log.h>
+#include <pj/string.h> /* memcpy() */
+#include <pj/pool.h>
+#include <stdlib.h>
+
+#define THISFILE "stream.c"
+#define ERRLEVEL 1
+
+#define PJ_MAX_FRAME_DURATION_MS 200
+#define PJ_MAX_BUFFER_SIZE_MS 2000
+#define PJ_MAX_MTU 1500
+
+struct jb_frame
+{
+ unsigned size;
+ void *buf;
+};
+
+#define pj_fifobuf_alloc(fifo,size) malloc(size)
+#define pj_fifobuf_unalloc(fifo,buf) free(buf)
+#define pj_fifobuf_free(fifo, buf) free(buf)
+
+enum stream_state
+{
+ STREAM_STOPPED,
+ STREAM_STARTED,
+};
+
+struct pj_media_stream_t
+{
+ pj_media_dir_t dir;
+ int pt;
+ int state;
+ pj_media_stream_stat stat;
+ pj_media_stream_t *peer;
+ pj_snd_stream_info snd_info;
+ pj_snd_stream *snd_stream;
+ pj_mutex_t *mutex;
+ unsigned in_pkt_size;
+ void *in_pkt;
+ unsigned out_pkt_size;
+ void *out_pkt;
+ unsigned pcm_buf_size;
+ void *pcm_buf;
+ //pj_fifobuf_t fifobuf;
+ pj_codec_mgr *codec_mgr;
+ pj_codec *codec;
+ pj_rtp_session rtp;
+ pj_rtcp_session *rtcp;
+ pj_jitter_buffer *jb;
+ pj_sock_t rtp_sock;
+ pj_sock_t rtcp_sock;
+ pj_sockaddr_in dst_addr;
+ pj_thread_t *transport_thread;
+ int thread_quit_flag;
+};
+
+
+static pj_status_t play_callback(/* in */ void *user_data,
+ /* in */ pj_uint32_t timestamp,
+ /* out */ void *frame,
+ /*inout*/ unsigned size)
+{
+ pj_media_stream_t *channel = user_data;
+ struct jb_frame *jb_frame;
+ void *p;
+ pj_uint32_t extseq;
+ pj_status_t status;
+ struct pj_audio_frame frame_in, frame_out;
+
+ PJ_UNUSED_ARG(timestamp)
+
+ /* Lock mutex */
+ pj_mutex_lock (channel->mutex);
+
+ if (!channel->codec) {
+ pj_mutex_unlock (channel->mutex);
+ return -1;
+ }
+
+ /* Get frame from jitter buffer. */
+ status = pj_jb_get (channel->jb, &extseq, &p);
+ jb_frame = p;
+ if (status != 0 || jb_frame == NULL) {
+ pj_memset(frame, 0, size);
+ pj_mutex_unlock(channel->mutex);
+ return 0;
+ }
+
+ /* Decode */
+ frame_in.buf = jb_frame->buf;
+ frame_in.size = jb_frame->size;
+ frame_in.type = PJ_AUDIO_FRAME_AUDIO; /* ignored */
+ frame_out.buf = channel->pcm_buf;
+ status = channel->codec->op->decode (channel->codec, &frame_in,
+ channel->pcm_buf_size, &frame_out);
+ if (status != 0) {
+ PJ_LOG(3, (THISFILE, "decode() has return error status %d",
+ status));
+
+ pj_memset(frame, 0, size);
+ pj_fifobuf_free (&channel->fifobuf, jb_frame);
+ pj_mutex_unlock(channel->mutex);
+ return 0;
+ }
+
+ /* Put in sound buffer. */
+ if (frame_out.size > size) {
+ PJ_LOG(3, (THISFILE, "Sound playout buffer truncated %d bytes",
+ frame_out.size - size));
+ frame_out.size = size;
+ }
+
+ pj_memcpy(frame, frame_out.buf, size);
+
+ pj_fifobuf_free (&channel->fifobuf, jb_frame);
+ pj_mutex_unlock(channel->mutex);
+ return 0;
+}
+
+static pj_status_t rec_callback( /* in */ void *user_data,
+ /* in */ pj_uint32_t timestamp,
+ /* in */ const void *frame,
+ /* in */ unsigned size)
+{
+ pj_media_stream_t *channel = user_data;
+ pj_status_t status = 0;
+ struct pj_audio_frame frame_in, frame_out;
+ int ts_len;
+ void *rtphdr;
+ int rtphdrlen;
+ int sent;
+#if 0
+ static FILE *fhnd = NULL;
+#endif
+
+ PJ_UNUSED_ARG(timestamp)
+
+ /* Start locking channel mutex */
+ pj_mutex_lock (channel->mutex);
+
+ if (!channel->codec) {
+ status = -1;
+ goto on_return;
+ }
+
+ /* Encode. */
+ frame_in.type = PJ_MEDIA_TYPE_AUDIO;
+ frame_in.buf = (void*)frame;
+ frame_in.size = size;
+ frame_out.buf = ((char*)channel->out_pkt) + sizeof(pj_rtp_hdr);
+ status = channel->codec->op->encode (channel->codec, &frame_in,
+ channel->out_pkt_size - sizeof(pj_rtp_hdr),
+ &frame_out);
+ if (status != 0) {
+ PJ_LOG(3,(THISFILE, "Codec encode() has returned error status %d",
+ status));
+ goto on_return;
+ }
+
+ /* Encapsulate. */
+ ts_len = size / (channel->snd_info.bits_per_sample / 8);
+ status = pj_rtp_encode_rtp (&channel->rtp, channel->pt, 0,
+ frame_out.size, ts_len,
+ (const void**)&rtphdr, &rtphdrlen);
+ if (status != 0) {
+ PJ_LOG(3,(THISFILE, "RTP encode_rtp() has returned error status %d",
+ status));
+ goto on_return;
+ }
+
+ if (rtphdrlen != sizeof(pj_rtp_hdr)) {
+ /* We don't support RTP with extended header yet. */
+ PJ_TODO(SUPPORT_SENDING_RTP_WITH_EXTENDED_HEADER);
+ PJ_LOG(3,(THISFILE, "Unsupported extended RTP header for transmission"));
+ goto on_return;
+ }
+
+ pj_memcpy(channel->out_pkt, rtphdr, sizeof(pj_rtp_hdr));
+
+ /* Send. */
+ sent = pj_sock_sendto (channel->rtp_sock, channel->out_pkt, frame_out.size+sizeof(pj_rtp_hdr), 0,
+ &channel->dst_addr, sizeof(channel->dst_addr));
+ if (sent != (int)frame_out.size + (int)sizeof(pj_rtp_hdr)) {
+ pj_perror(THISFILE, "Error sending RTP packet to %s:%d",
+ pj_sockaddr_get_str_addr(&channel->dst_addr),
+ pj_sockaddr_get_port(&channel->dst_addr));
+ goto on_return;
+ }
+
+ /* Update stat */
+ channel->stat.pkt_tx++;
+ channel->stat.oct_tx += frame_out.size+sizeof(pj_rtp_hdr);
+
+#if 0
+ if (fhnd == NULL) {
+ fhnd = fopen("RTP.DAT", "wb");
+ if (fhnd) {
+ fwrite (channel->out_pkt, frame_out.size+sizeof(pj_rtp_hdr), 1, fhnd);
+ fclose(fhnd);
+ }
+ }
+#endif
+
+on_return:
+ pj_mutex_unlock (channel->mutex);
+ return status;
+}
+
+
+static void* PJ_THREAD_FUNC stream_decoder_transport_thread (void*arg)
+{
+ pj_media_stream_t *channel = arg;
+
+ while (!channel->thread_quit_flag) {
+ int len, size;
+ const pj_rtp_hdr *hdr;
+ const void *payload;
+ unsigned payloadlen;
+ int status;
+ struct jb_frame *jb_frame;
+
+ /* Wait for packet. */
+ fd_set fds;
+ pj_time_val timeout;
+
+ PJ_FD_ZERO (&fds);
+ PJ_FD_SET (channel->rtp_sock, &fds);
+ timeout.sec = 0;
+ timeout.msec = 100;
+
+ /* Wait with timeout. */
+ status = pj_sock_select(channel->rtp_sock, &fds, NULL, NULL, &timeout);
+ if (status != 1)
+ continue;
+
+ /* Get packet from socket. */
+ len = pj_sock_recv (channel->rtp_sock, channel->in_pkt, channel->in_pkt_size, 0);
+ if (len < 1) {
+ if (pj_getlasterror() == PJ_ECONNRESET) {
+ /* On Win2K SP2 (or above) and WinXP, recv() will get WSAECONNRESET
+ when the sending side receives ICMP port unreachable.
+ */
+ continue;
+ }
+ pj_perror(THISFILE, "Error receiving packet from socket (len=%d)", len);
+ pj_thread_sleep(1);
+ continue;
+ }
+
+ if (channel->state != STREAM_STARTED)
+ continue;
+
+ if (channel->thread_quit_flag)
+ break;
+
+ /* Start locking the channel. */
+ pj_mutex_lock (channel->mutex);
+
+ /* Update RTP and RTCP session. */
+ status = pj_rtp_decode_rtp (&channel->rtp, channel->in_pkt, len, &hdr, &payload, &payloadlen);
+ if (status != 0) {
+ pj_mutex_unlock (channel->mutex);
+ PJ_LOG(4,(THISFILE, "RTP decode_rtp() has returned error status %d", status));
+ continue;
+ }
+ status = pj_rtp_session_update (&channel->rtp, hdr);
+ if (status != 0 && status != PJ_RTP_ERR_SESSION_PROBATION && status != PJ_RTP_ERR_SESSION_RESTARTED) {
+ pj_mutex_unlock (channel->mutex);
+ PJ_LOG(4,(THISFILE, "RTP session_update() has returned error status %d", status));
+ continue;
+ }
+ pj_rtcp_rx_rtp (channel->rtcp, pj_ntohs(hdr->seq), pj_ntohl(hdr->ts));
+
+ /* Update stat */
+ channel->stat.pkt_rx++;
+ channel->stat.oct_rx += len;
+
+ /* Copy to FIFO buffer. */
+ size = payloadlen+sizeof(struct jb_frame);
+ jb_frame = pj_fifobuf_alloc (&channel->fifobuf, size);
+ if (jb_frame == NULL) {
+ pj_mutex_unlock (channel->mutex);
+ PJ_LOG(4,(THISFILE, "Unable to allocate %d bytes FIFO buffer", size));
+ continue;
+ }
+
+ /* Copy the payload */
+ jb_frame->size = payloadlen;
+ jb_frame->buf = ((char*)jb_frame) + sizeof(struct jb_frame);
+ pj_memcpy (jb_frame->buf, payload, payloadlen);
+
+ /* Put to jitter buffer. */
+ status = pj_jb_put (channel->jb, pj_ntohs(hdr->seq), jb_frame);
+ if (status != 0) {
+ pj_fifobuf_unalloc (&channel->fifobuf, jb_frame);
+ pj_mutex_unlock (channel->mutex);
+ PJ_LOG(4,(THISFILE, "Jitter buffer put() has returned error status %d", status));
+ continue;
+ }
+
+ pj_mutex_unlock (channel->mutex);
+ }
+
+ return NULL;
+}
+
+static void init_snd_param_from_codec_attr (pj_snd_stream_info *param,
+ const pj_codec_attr *attr)
+{
+ param->bits_per_sample = attr->pcm_bits_per_sample;
+ param->bytes_per_frame = 2;
+ param->frames_per_packet = attr->sample_rate * attr->ptime / 1000;
+ param->samples_per_frame = 1;
+ param->samples_per_sec = attr->sample_rate;
+}
+
+static pj_media_stream_t *create_channel ( pj_pool_t *pool,
+ pj_media_dir_t dir,
+ pj_media_stream_t *peer,
+ pj_codec_id *codec_id,
+ pj_media_stream_create_param *param)
+{
+ pj_media_stream_t *channel;
+ pj_codec_attr codec_attr;
+ void *ptr;
+ unsigned size;
+ int status;
+
+ /* Allocate memory for channel descriptor */
+ size = sizeof(pj_media_stream_t);
+ channel = pj_pool_calloc(pool, 1, size);
+ if (!channel) {
+ PJ_LOG(1,(THISFILE, "Unable to allocate %u bytes channel descriptor",
+ size));
+ return NULL;
+ }
+
+ channel->dir = dir;
+ channel->pt = codec_id->pt;
+ channel->peer = peer;
+ channel->codec_mgr = pj_med_mgr_get_codec_mgr (param->mediamgr);
+ channel->rtp_sock = param->rtp_sock;
+ channel->rtcp_sock = param->rtcp_sock;
+ channel->dst_addr = *param->remote_addr;
+ channel->state = STREAM_STOPPED;
+
+ /* Create mutex for the channel. */
+ channel->mutex = pj_mutex_create(pool, NULL, PJ_MUTEX_SIMPLE);
+ if (channel->mutex == NULL)
+ goto err_cleanup;
+
+ /* Create and initialize codec, only if peer is not present.
+ We only use one codec instance for both encoder and decoder.
+ */
+ if (peer && peer->codec) {
+ channel->codec = peer->codec;
+ status = channel->codec->factory->op->default_attr(channel->codec->factory, codec_id,
+ &codec_attr);
+ if (status != 0) {
+ goto err_cleanup;
+ }
+
+ } else {
+ channel->codec = pj_codec_mgr_alloc_codec(channel->codec_mgr, codec_id);
+ if (channel->codec == NULL) {
+ goto err_cleanup;
+ }
+
+ status = channel->codec->factory->op->default_attr(channel->codec->factory, codec_id,
+ &codec_attr);
+ if (status != 0) {
+ goto err_cleanup;
+ }
+
+ codec_attr.pt = codec_id->pt;
+ status = channel->codec->op->open(channel->codec, &codec_attr);
+ if (status != 0) {
+ goto err_cleanup;
+ }
+ }
+
+ /* Allocate buffer for incoming packet. */
+ channel->in_pkt_size = PJ_MAX_MTU;
+ channel->in_pkt = pj_pool_alloc(pool, channel->in_pkt_size);
+ if (!channel->in_pkt) {
+ PJ_LOG(1, (THISFILE, "Unable to allocate %u bytes incoming packet buffer",
+ channel->in_pkt_size));
+ goto err_cleanup;
+ }
+
+ /* Allocate buffer for outgoing packet. */
+ channel->out_pkt_size = sizeof(pj_rtp_hdr) +
+ codec_attr.avg_bps / 8 * PJ_MAX_FRAME_DURATION_MS / 1000;
+ if (channel->out_pkt_size > PJ_MAX_MTU)
+ channel->out_pkt_size = PJ_MAX_MTU;
+ channel->out_pkt = pj_pool_alloc(pool, channel->out_pkt_size);
+ if (!channel->out_pkt) {
+ PJ_LOG(1, (THISFILE, "Unable to allocate %u bytes encoding buffer",
+ channel->out_pkt_size));
+ goto err_cleanup;
+ }
+
+ /* Allocate buffer for decoding to PCM */
+ channel->pcm_buf_size = codec_attr.sample_rate *
+ codec_attr.pcm_bits_per_sample / 8 *
+ PJ_MAX_FRAME_DURATION_MS / 1000;
+ channel->pcm_buf = pj_pool_alloc (pool, channel->pcm_buf_size);
+ if (!channel->pcm_buf) {
+ PJ_LOG(1, (THISFILE, "Unable to allocate %u bytes PCM buffer",
+ channel->pcm_buf_size));
+ goto err_cleanup;
+ }
+
+ /* Allocate buffer for frames put in jitter buffer. */
+ size = codec_attr.avg_bps / 8 * PJ_MAX_BUFFER_SIZE_MS / 1000;
+ ptr = pj_pool_alloc(pool, size);
+ if (!ptr) {
+ PJ_LOG(1, (THISFILE, "Unable to allocate %u bytes jitter buffer",
+ channel->pcm_buf_size));
+ goto err_cleanup;
+ }
+ //pj_fifobuf_init (&channel->fifobuf, ptr, size);
+
+ /* Create and initialize sound device */
+ init_snd_param_from_codec_attr (&channel->snd_info, &codec_attr);
+
+ if (dir == PJ_MEDIA_DIR_ENCODING)
+ channel->snd_stream = pj_snd_open_recorder(-1, &channel->snd_info,
+ &rec_callback, channel);
+ else
+ channel->snd_stream = pj_snd_open_player(-1, &channel->snd_info,
+ &play_callback, channel);
+
+ if (!channel->snd_stream)
+ goto err_cleanup;
+
+ /* Create RTP and RTCP sessions. */
+ if (pj_rtp_session_init(&channel->rtp, codec_id->pt, param->ssrc) != 0) {
+ PJ_LOG(1, (THISFILE, "RTP session initialization error"));
+ goto err_cleanup;
+ }
+
+ /* For decoder, create RTCP session, jitter buffer, and transport thread. */
+ if (dir == PJ_MEDIA_DIR_DECODING) {
+ channel->rtcp = pj_pool_calloc(pool, 1, sizeof(pj_rtcp_session));
+ if (!channel->rtcp) {
+ PJ_LOG(1, (THISFILE, "Unable to allocate RTCP session"));
+ goto err_cleanup;
+ }
+
+ pj_rtcp_init(channel->rtcp, param->ssrc);
+
+ channel->jb = pj_pool_calloc(pool, 1, sizeof(pj_jitter_buffer));
+ if (!channel->jb) {
+ PJ_LOG(1, (THISFILE, "Unable to allocate jitter buffer descriptor"));
+ goto err_cleanup;
+ }
+ if (pj_jb_init(channel->jb, pool, param->jb_min, param->jb_max, param->jb_maxcnt)) {
+ PJ_LOG(1, (THISFILE, "Unable to allocate jitter buffer"));
+ goto err_cleanup;
+ }
+
+ channel->transport_thread = pj_thread_create(pool, "decode",
+ &stream_decoder_transport_thread, channel,
+ 0, NULL, 0);
+ if (!channel->transport_thread) {
+ pj_perror(THISFILE, "Unable to create transport thread");
+ goto err_cleanup;
+ }
+ }
+
+ /* Done. */
+ return channel;
+
+err_cleanup:
+ pj_media_stream_destroy(channel);
+ return NULL;
+}
+
+
+PJ_DEF(pj_status_t) pj_media_stream_create (pj_pool_t *pool,
+ pj_media_stream_t **enc_stream,
+ pj_media_stream_t **dec_stream,
+ pj_media_stream_create_param *param)
+{
+ *dec_stream = *enc_stream = NULL;
+
+ if (param->dir & PJ_MEDIA_DIR_DECODING) {
+ *dec_stream =
+ create_channel(pool, PJ_MEDIA_DIR_DECODING, NULL, param->codec_id, param);
+ if (!*dec_stream)
+ return -1;
+ }
+
+ if (param->dir & PJ_MEDIA_DIR_ENCODING) {
+ *enc_stream =
+ create_channel(pool, PJ_MEDIA_DIR_ENCODING, *dec_stream, param->codec_id, param);
+ if (!*enc_stream) {
+ if (*dec_stream) {
+ pj_media_stream_destroy(*dec_stream);
+ *dec_stream = NULL;
+ }
+ return -1;
+ }
+
+ if (*dec_stream) {
+ (*dec_stream)->peer = *enc_stream;
+ }
+ }
+
+ return 0;
+}
+
+PJ_DEF(pj_status_t) pj_media_stream_start (pj_media_stream_t *channel)
+{
+ pj_status_t status;
+
+ status = pj_snd_stream_start(channel->snd_stream);
+
+ if (status == 0)
+ channel->state = STREAM_STARTED;
+ return status;
+}
+
+PJ_DEF(pj_status_t) pj_media_stream_get_stat (const pj_media_stream_t *stream,
+ pj_media_stream_stat *stat)
+{
+ if (stream->dir == PJ_MEDIA_DIR_ENCODING) {
+ pj_memcpy (stat, &stream->stat, sizeof(*stat));
+ } else {
+ pj_rtcp_pkt *rtcp_pkt;
+ int len;
+
+ pj_memset (stat, 0, sizeof(*stat));
+ pj_assert (stream->rtcp != 0);
+ pj_rtcp_build_rtcp (stream->rtcp, &rtcp_pkt, &len);
+
+ stat->pkt_rx = stream->stat.pkt_rx;
+ stat->oct_rx = stream->stat.oct_rx;
+
+ PJ_TODO(SUPPORT_JITTER_CALCULATION_FOR_NON_8KHZ_SAMPLE_RATE)
+ stat->jitter = pj_ntohl(rtcp_pkt->rr.jitter) / 8;
+ stat->pkt_lost = (rtcp_pkt->rr.total_lost_2 << 16) +
+ (rtcp_pkt->rr.total_lost_1 << 8) +
+ rtcp_pkt->rr.total_lost_0;
+ }
+ return 0;
+}
+
+PJ_DEF(pj_status_t) pj_media_stream_pause (pj_media_stream_t *channel)
+{
+ PJ_UNUSED_ARG(channel)
+ return -1;
+}
+
+PJ_DEF(pj_status_t) pj_media_stream_resume (pj_media_stream_t *channel)
+{
+ PJ_UNUSED_ARG(channel)
+ return -1;
+}
+
+PJ_DEF(pj_status_t) pj_media_stream_destroy (pj_media_stream_t *channel)
+{
+ channel->thread_quit_flag = 1;
+
+ pj_mutex_lock (channel->mutex);
+ if (channel->peer)
+ pj_mutex_lock (channel->peer->mutex);
+
+ if (channel->jb) {
+ /* No need to deinitialize jitter buffer. */
+ }
+ if (channel->transport_thread) {
+ pj_thread_join(channel->transport_thread);
+ pj_thread_destroy(channel->transport_thread);
+ channel->transport_thread = NULL;
+ }
+ if (channel->snd_stream != NULL) {
+ pj_mutex_unlock (channel->mutex);
+ pj_snd_stream_stop(channel->snd_stream);
+ pj_mutex_lock (channel->mutex);
+ pj_snd_stream_close(channel->snd_stream);
+ channel->snd_stream = NULL;
+ }
+ if (channel->codec) {
+ channel->codec->op->close(channel->codec);
+ pj_codec_mgr_dealloc_codec(channel->codec_mgr, channel->codec);
+ channel->codec = NULL;
+ }
+ if (channel->peer) {
+ pj_media_stream_t *peer = channel->peer;
+ peer->peer = NULL;
+ peer->codec = NULL;
+ peer->thread_quit_flag = 1;
+ if (peer->transport_thread) {
+ pj_mutex_unlock (peer->mutex);
+ pj_thread_join(peer->transport_thread);
+ pj_mutex_lock (peer->mutex);
+ pj_thread_destroy(peer->transport_thread);
+ peer->transport_thread = NULL;
+ }
+ if (peer->snd_stream) {
+ pj_mutex_unlock (peer->mutex);
+ pj_snd_stream_stop(peer->snd_stream);
+ pj_mutex_lock (peer->mutex);
+ pj_snd_stream_close(peer->snd_stream);
+ peer->snd_stream = NULL;
+ }
+ }
+
+ channel->state = STREAM_STOPPED;
+
+ if (channel->peer)
+ pj_mutex_unlock (channel->peer->mutex);
+ pj_mutex_unlock(channel->mutex);
+ pj_mutex_destroy(channel->mutex);
+
+ return 0;
+}
+
diff --git a/pjmedia/src/pjmedia/stream.h b/pjmedia/src/pjmedia/stream.h
index a4b2f932..09f6acf1 100644
--- a/pjmedia/src/pjmedia/stream.h
+++ b/pjmedia/src/pjmedia/stream.h
@@ -1,99 +1,99 @@
-/* $Id$ */
-/*
- * Copyright (C) 2003-2006 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 __PJMEDIA_STREAM_H__
-#define __PJMEDIA_STREAM_H__
-
-
-/**
- * @file stream.h
- * @brief Stream of media.
- */
-
-#include <pjmedia/sound.h>
-#include <pjmedia/codec.h>
-#include <pjmedia/mediamgr.h>
-#include <pj/sock.h>
-
-PJ_BEGIN_DECL
-
-
-/**
- * @defgroup PJMED_SES Media session
- * @ingroup PJMEDIA
- * @{
- */
-
-typedef struct pj_media_stream_t pj_media_stream_t;
-
-/** Parameter for creating channel. */
-typedef struct pj_media_stream_create_param
-{
- /** Codec ID, must NOT be NULL. */
- pj_codec_id *codec_id;
-
- /** Media manager, must NOT be NULL. */
- pj_med_mgr_t *mediamgr;
-
- /** Direction: IN_OUT, or IN only, or OUT only. */
- pj_media_dir_t dir;
-
- /** RTP socket. */
- pj_sock_t rtp_sock;
-
- /** RTCP socket. */
- pj_sock_t rtcp_sock;
-
- /** Address of remote */
- pj_sockaddr_in *remote_addr;
-
- /** RTP SSRC */
- pj_uint32_t ssrc;
-
- /** Jitter buffer parameters. */
- int jb_min, jb_max, jb_maxcnt;
-
-} pj_media_stream_create_param;
-
-typedef struct pj_media_stream_stat
-{
- pj_uint32_t pkt_tx, pkt_rx; /* packets transmitted/received */
- pj_uint32_t oct_tx, oct_rx; /* octets transmitted/received */
- pj_uint32_t jitter; /* receive jitter in ms */
- pj_uint32_t pkt_lost; /* total packet lost count */
-} pj_media_stream_stat;
-
-PJ_DECL(pj_status_t) pj_media_stream_create (pj_pool_t *pool,
- pj_media_stream_t **enc_stream,
- pj_media_stream_t **dec_stream,
- pj_media_stream_create_param *param);
-PJ_DECL(pj_status_t) pj_media_stream_start (pj_media_stream_t *stream);
-PJ_DECL(pj_status_t) pj_media_stream_get_stat (const pj_media_stream_t *stream,
- pj_media_stream_stat *stat);
-PJ_DECL(pj_status_t) pj_media_stream_pause (pj_media_stream_t *stream);
-PJ_DECL(pj_status_t) pj_media_stream_resume (pj_media_stream_t *stream);
-PJ_DECL(pj_status_t) pj_media_stream_destroy (pj_media_stream_t *stream);
-
-/**
- * @}
- */
-
-PJ_END_DECL
-
-
-#endif /* __PJMEDIA_STREAM_H__ */
+/* $Id$ */
+/*
+ * Copyright (C) 2003-2006 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 __PJMEDIA_STREAM_H__
+#define __PJMEDIA_STREAM_H__
+
+
+/**
+ * @file stream.h
+ * @brief Stream of media.
+ */
+
+#include <pjmedia/sound.h>
+#include <pjmedia/codec.h>
+#include <pjmedia/mediamgr.h>
+#include <pj/sock.h>
+
+PJ_BEGIN_DECL
+
+
+/**
+ * @defgroup PJMED_SES Media session
+ * @ingroup PJMEDIA
+ * @{
+ */
+
+typedef struct pj_media_stream_t pj_media_stream_t;
+
+/** Parameter for creating channel. */
+typedef struct pj_media_stream_create_param
+{
+ /** Codec ID, must NOT be NULL. */
+ pj_codec_id *codec_id;
+
+ /** Media manager, must NOT be NULL. */
+ pj_med_mgr_t *mediamgr;
+
+ /** Direction: IN_OUT, or IN only, or OUT only. */
+ pj_media_dir_t dir;
+
+ /** RTP socket. */
+ pj_sock_t rtp_sock;
+
+ /** RTCP socket. */
+ pj_sock_t rtcp_sock;
+
+ /** Address of remote */
+ pj_sockaddr_in *remote_addr;
+
+ /** RTP SSRC */
+ pj_uint32_t ssrc;
+
+ /** Jitter buffer parameters. */
+ int jb_min, jb_max, jb_maxcnt;
+
+} pj_media_stream_create_param;
+
+typedef struct pj_media_stream_stat
+{
+ pj_uint32_t pkt_tx, pkt_rx; /* packets transmitted/received */
+ pj_uint32_t oct_tx, oct_rx; /* octets transmitted/received */
+ pj_uint32_t jitter; /* receive jitter in ms */
+ pj_uint32_t pkt_lost; /* total packet lost count */
+} pj_media_stream_stat;
+
+PJ_DECL(pj_status_t) pj_media_stream_create (pj_pool_t *pool,
+ pj_media_stream_t **enc_stream,
+ pj_media_stream_t **dec_stream,
+ pj_media_stream_create_param *param);
+PJ_DECL(pj_status_t) pj_media_stream_start (pj_media_stream_t *stream);
+PJ_DECL(pj_status_t) pj_media_stream_get_stat (const pj_media_stream_t *stream,
+ pj_media_stream_stat *stat);
+PJ_DECL(pj_status_t) pj_media_stream_pause (pj_media_stream_t *stream);
+PJ_DECL(pj_status_t) pj_media_stream_resume (pj_media_stream_t *stream);
+PJ_DECL(pj_status_t) pj_media_stream_destroy (pj_media_stream_t *stream);
+
+/**
+ * @}
+ */
+
+PJ_END_DECL
+
+
+#endif /* __PJMEDIA_STREAM_H__ */