tests/examples/rtp/: Add two C examples of using gstrtpbin as a sender and a receiver.
authorWim Taymans <wim.taymans@gmail.com>
Fri, 2 Jan 2009 15:20:48 +0000 (15:20 +0000)
committerWim Taymans <wim.taymans@gmail.com>
Fri, 2 Jan 2009 15:20:48 +0000 (15:20 +0000)
Original commit message from CVS:
* tests/examples/rtp/.cvsignore:
* tests/examples/rtp/Makefile.am:
* tests/examples/rtp/client-PCMA.c: (pad_added_cb), (main):
* tests/examples/rtp/server-alsasrc-PCMA.c: (main):
Add two C examples of using gstrtpbin as a sender and a receiver.

ChangeLog
tests/examples/rtp/.gitignore [new file with mode: 0644]
tests/examples/rtp/Makefile.am
tests/examples/rtp/client-PCMA.c [new file with mode: 0755]
tests/examples/rtp/server-alsasrc-PCMA.c [new file with mode: 0755]

index 8925983..a13169c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2009-01-02  Wim Taymans  <wim.taymans@collabora.co.uk>
+
+       * tests/examples/rtp/.cvsignore:
+       * tests/examples/rtp/Makefile.am:
+       * tests/examples/rtp/client-PCMA.c: (pad_added_cb), (main):
+       * tests/examples/rtp/server-alsasrc-PCMA.c: (main):
+       Add two C examples of using gstrtpbin as a sender and a receiver.
+
 2008-12-31  Jan Schmidt  <jan.schmidt@sun.com>
 
        * ChangeLog:
diff --git a/tests/examples/rtp/.gitignore b/tests/examples/rtp/.gitignore
new file mode 100644 (file)
index 0000000..6b195b7
--- /dev/null
@@ -0,0 +1,2 @@
+client-PCMA
+server-alsasrc-PCMA
index f0b033b..f636e81 100644 (file)
@@ -1,3 +1,13 @@
+noinst_PROGRAMS = server-alsasrc-PCMA client-PCMA
+
+server_alsasrc_PCMA_SOURCES = server-alsasrc-PCMA.c
+server_alsasrc_PCMA_CFLAGS = $(GST_CFLAGS)
+server_alsasrc_PCMA_LDADD = $(GST_LIBS) $(LIBM)
+
+client_PCMA_SOURCES = client-PCMA.c
+client_PCMA_CFLAGS = $(GST_CFLAGS)
+client_PCMA_LDADD = $(GST_LIBS) $(LIBM)
+
 noinst_SCRIPTS=client-H263p-AMR.sh \
                client-H263p-PCMA.sh \
                client-H264-PCMA.sh \
diff --git a/tests/examples/rtp/client-PCMA.c b/tests/examples/rtp/client-PCMA.c
new file mode 100755 (executable)
index 0000000..0c895a2
--- /dev/null
@@ -0,0 +1,191 @@
+/* GStreamer
+ * Copyright (C) 2009 Wim Taymans <wim.taymans@gmail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library 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.
+ */
+
+#include <string.h>
+#include <math.h>
+
+#include <gst/gst.h>
+
+/*
+ * A simple RTP receiver 
+ *
+ *  receives alaw encoded RTP audio on port 5002, RTCP is received on  port 5003.
+ *  the receiver RTCP reports are sent to port 5007
+ *
+ *             .-------.      .----------.     .---------.   .-------.   .--------.
+ *  RTP        |udpsrc |      | rtpbin   |     |pcmadepay|   |alawdec|   |alsasink|
+ *  port=5002  |      src->recv_rtp recv_rtp->sink     src->sink   src->sink      |
+ *             '-------'      |          |     '---------'   '-------'   '--------'
+ *                            |          |      
+ *                            |          |     .-------.
+ *                            |          |     |udpsink|  RTCP
+ *                            |    send_rtcp->sink     | port=5007
+ *             .-------.      |          |     '-------' sync=false
+ *  RTCP       |udpsrc |      |          |               async=false
+ *  port=5003  |     src->recv_rtcp      |                       
+ *             '-------'      '----------'              
+ */
+
+/* the caps of the sender RTP stream. This is usually negotiated out of band with
+ * SDP or RTSP. */
+#define AUDIO_CAPS "application/x-rtp,media=(string)audio,clock-rate=(int)8000,encoding-name=(string)PCMA"
+
+#define AUDIO_DEPAY "rtppcmadepay"
+#define AUDIO_DEC   "alawdec"
+#define AUDIO_SINK  "autoaudiosink"
+
+/* the destination machine to send RTCP to. This is the address of the sender and
+ * is used to send back the RTCP reports of this receiver. If the data is sent
+ * from another machine, change this address. */
+#define DEST_HOST "127.0.0.1"
+
+/* will be called when rtpbin has validated a payload that we can depayload */
+static void
+pad_added_cb (GstElement * rtpbin, GstPad * new_pad, GstElement * depay)
+{
+  GstPad *sinkpad;
+  GstPadLinkReturn lres;
+
+  g_print ("new payload on pad: %s\n", GST_PAD_NAME (new_pad));
+
+  sinkpad = gst_element_get_static_pad (depay, "sink");
+  g_assert (sinkpad);
+
+  lres = gst_pad_link (new_pad, sinkpad);
+  g_assert (lres == GST_PAD_LINK_OK);
+  gst_object_unref (sinkpad);
+}
+
+/* build a pipeline equivalent to:
+ *
+ * gst-launch -v gstrtpbin name=rtpbin                                                \
+ *      udpsrc caps=$AUDIO_CAPS port=5002 ! rtpbin.recv_rtp_sink_0              \
+ *        rtpbin. ! rtppcmadepay ! alawdec ! audioconvert ! audioresample ! alsasink \
+ *      udpsrc port=5003 ! rtpbin.recv_rtcp_sink_0                              \
+ *        rtpbin.send_rtcp_src_0 ! udpsink port=5007 host=$DEST sync=false async=false
+ */
+int
+main (int argc, char *argv[])
+{
+  GstElement *rtpbin, *rtpsrc, *rtcpsrc, *rtcpsink;
+  GstElement *audiodepay, *audiodec, *audiores, *audioconv, *audiosink;
+  GstElement *pipeline;
+  GMainLoop *loop;
+  GstCaps *caps;
+  gboolean res;
+  GstPadLinkReturn lres;
+  GstPad *srcpad, *sinkpad;
+
+  /* always init first */
+  gst_init (&argc, &argv);
+
+  /* the pipeline to hold everything */
+  pipeline = gst_pipeline_new (NULL);
+  g_assert (pipeline);
+
+  /* the udp src and source we will use for RTP and RTCP */
+  rtpsrc = gst_element_factory_make ("udpsrc", "rtpsrc");
+  g_assert (rtpsrc);
+  g_object_set (rtpsrc, "port", 5002, NULL);
+  /* we need to set caps on the udpsrc for the RTP data */
+  caps = gst_caps_from_string (AUDIO_CAPS);
+  g_object_set (rtpsrc, "caps", caps, NULL);
+  gst_caps_unref (caps);
+
+  rtcpsrc = gst_element_factory_make ("udpsrc", "rtcpsrc");
+  g_assert (rtcpsrc);
+  g_object_set (rtcpsrc, "port", 5003, NULL);
+
+  rtcpsink = gst_element_factory_make ("udpsink", "rtcpsink");
+  g_assert (rtcpsink);
+  g_object_set (rtcpsink, "port", 5007, "host", DEST_HOST, NULL);
+  /* no need for synchronisation or preroll on the RTCP sink */
+  g_object_set (rtcpsink, "async", FALSE, "sync", FALSE, NULL);
+
+  gst_bin_add_many (GST_BIN (pipeline), rtpsrc, rtcpsrc, rtcpsink, NULL);
+
+  /* the depayloading and decoding */
+  audiodepay = gst_element_factory_make (AUDIO_DEPAY, "audiodepay");
+  g_assert (audiodepay);
+  audiodec = gst_element_factory_make (AUDIO_DEC, "audiodec");
+  g_assert (audiodec);
+  /* the audio playback and format conversion */
+  audioconv = gst_element_factory_make ("audioconvert", "audioconv");
+  g_assert (audioconv);
+  audiores = gst_element_factory_make ("audioresample", "audiores");
+  g_assert (audiores);
+  audiosink = gst_element_factory_make (AUDIO_SINK, "audiosink");
+  g_assert (audiosink);
+
+  /* add depayloading and playback to the pipeline and link */
+  gst_bin_add_many (GST_BIN (pipeline), audiodepay, audiodec, audioconv,
+      audiores, audiosink, NULL);
+
+  res = gst_element_link_many (audiodepay, audiodec, audioconv, audiores,
+      audiosink, NULL);
+  g_assert (res == TRUE);
+
+  /* the rtpbin element */
+  rtpbin = gst_element_factory_make ("gstrtpbin", "rtpbin");
+  g_assert (rtpbin);
+
+  gst_bin_add (GST_BIN (pipeline), rtpbin);
+
+  /* now link all to the rtpbin, start by getting an RTP sinkpad for session 0 */
+  srcpad = gst_element_get_static_pad (rtpsrc, "src");
+  sinkpad = gst_element_get_request_pad (rtpbin, "recv_rtp_sink_0");
+  lres = gst_pad_link (srcpad, sinkpad);
+  g_assert (lres == GST_PAD_LINK_OK);
+  gst_object_unref (srcpad);
+
+  /* get an RTCP sinkpad in session 0 */
+  srcpad = gst_element_get_static_pad (rtcpsrc, "src");
+  sinkpad = gst_element_get_request_pad (rtpbin, "recv_rtcp_sink_0");
+  lres = gst_pad_link (srcpad, sinkpad);
+  g_assert (lres == GST_PAD_LINK_OK);
+  gst_object_unref (srcpad);
+  gst_object_unref (sinkpad);
+
+  /* get an RTCP srcpad for sending RTCP back to the sender */
+  srcpad = gst_element_get_request_pad (rtpbin, "send_rtcp_src_0");
+  sinkpad = gst_element_get_static_pad (rtcpsink, "sink");
+  lres = gst_pad_link (srcpad, sinkpad);
+  g_assert (lres == GST_PAD_LINK_OK);
+  gst_object_unref (sinkpad);
+
+  /* the RTP pad that we have to connect to the depayloader will be created
+   * dynamically so we connect to the pad-added signal, pass the depayloader as
+   * user_data so that we can link to it. */
+  g_signal_connect (rtpbin, "pad-added", G_CALLBACK (pad_added_cb), audiodepay);
+
+  /* set the pipeline to playing */
+  g_print ("starting receiver pipeline\n");
+  gst_element_set_state (pipeline, GST_STATE_PLAYING);
+
+  /* we need to run a GLib main loop to get the messages */
+  loop = g_main_loop_new (NULL, FALSE);
+  g_main_loop_run (loop);
+
+  g_print ("stopping receiver pipeline\n");
+  gst_element_set_state (pipeline, GST_STATE_NULL);
+
+  gst_object_unref (pipeline);
+
+  return 0;
+}
diff --git a/tests/examples/rtp/server-alsasrc-PCMA.c b/tests/examples/rtp/server-alsasrc-PCMA.c
new file mode 100755 (executable)
index 0000000..3af888a
--- /dev/null
@@ -0,0 +1,168 @@
+/* GStreamer
+ * Copyright (C) 2009 Wim Taymans <wim.taymans@gmail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library 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.
+ */
+
+#include <string.h>
+#include <math.h>
+
+#include <gst/gst.h>
+
+/*
+ * A simple RTP server 
+ *  sends the output of alsasrc as alaw encoded RTP on port 5002, RTCP is sent on
+ *  port 5003. The destination is 127.0.0.1.
+ *  the receiver RTCP reports are received on port 5007
+ *
+ * .-------.    .-------.    .-------.      .----------.     .-------.
+ * |alsasrc|    |alawenc|    |pcmapay|      | rtpbin   |     |udpsink|  RTP
+ * |      src->sink    src->sink    src->send_rtp send_rtp->sink     | port=5002
+ * '-------'    '-------'    '-------'      |          |     '-------'
+ *                                          |          |      
+ *                                          |          |     .-------.
+ *                                          |          |     |udpsink|  RTCP
+ *                                          |    send_rtcp->sink     | port=5003
+ *                           .-------.      |          |     '-------' sync=false
+ *                RTCP       |udpsrc |      |          |               async=false
+ *              port=5007    |     src->recv_rtcp      |                       
+ *                           '-------'      '----------'              
+ */
+
+/* change this to send the RTP data and RTCP to another host */
+#define DEST_HOST "127.0.0.1"
+
+/* #define AUDIO_SRC  "alsasrc" */
+#define AUDIO_SRC  "audiotestsrc"
+
+/* the encoder and payloader elements */
+#define AUDIO_ENC  "alawenc"
+#define AUDIO_PAY  "rtppcmapay"
+
+/* build a pipeline equivalent to:
+ *
+ * gst-launch -v gstrtpbin name=rtpbin \
+ *    $AUDIO_SRC ! audioconvert ! audioresample ! $AUDIO_ENC ! $AUDIO_PAY ! rtpbin.send_rtp_sink_0  \
+ *           rtpbin.send_rtp_src_0 ! udpsink port=5002 host=$DEST                      \
+ *           rtpbin.send_rtcp_src_0 ! udpsink port=5003 host=$DEST sync=false async=false \
+ *        udpsrc port=5007 ! rtpbin.recv_rtcp_sink_0
+ */
+int
+main (int argc, char *argv[])
+{
+  GstElement *audiosrc, *audioconv, *audiores, *audioenc, *audiopay;
+  GstElement *rtpbin, *rtpsink, *rtcpsink, *rtcpsrc;
+  GstElement *pipeline;
+  GMainLoop *loop;
+  gboolean res;
+  GstPadLinkReturn lres;
+  GstPad *srcpad, *sinkpad;
+
+  /* always init first */
+  gst_init (&argc, &argv);
+
+  /* the pipeline to hold everything */
+  pipeline = gst_pipeline_new (NULL);
+  g_assert (pipeline);
+
+  /* the audio capture and format conversion */
+  audiosrc = gst_element_factory_make (AUDIO_SRC, "audiosrc");
+  g_assert (audiosrc);
+  audioconv = gst_element_factory_make ("audioconvert", "audioconv");
+  g_assert (audioconv);
+  audiores = gst_element_factory_make ("audioresample", "audiores");
+  g_assert (audiores);
+  /* the encoding and payloading */
+  audioenc = gst_element_factory_make (AUDIO_ENC, "audioenc");
+  g_assert (audioenc);
+  audiopay = gst_element_factory_make (AUDIO_PAY, "audiopay");
+  g_assert (audiopay);
+
+  /* add capture and payloading to the pipeline and link */
+  gst_bin_add_many (GST_BIN (pipeline), audiosrc, audioconv, audiores,
+      audioenc, audiopay, NULL);
+
+  res = gst_element_link_many (audiosrc, audioconv, audiores, audioenc,
+      audiopay, NULL);
+  g_assert (res == TRUE);
+
+  /* the rtpbin element */
+  rtpbin = gst_element_factory_make ("gstrtpbin", "rtpbin");
+  g_assert (rtpbin);
+
+  gst_bin_add (GST_BIN (pipeline), rtpbin);
+
+  /* the udp sinks and source we will use for RTP and RTCP */
+  rtpsink = gst_element_factory_make ("udpsink", "rtpsink");
+  g_assert (rtpsink);
+  g_object_set (rtpsink, "port", 5002, "host", DEST_HOST, NULL);
+
+  rtcpsink = gst_element_factory_make ("udpsink", "rtcpsink");
+  g_assert (rtcpsink);
+  g_object_set (rtcpsink, "port", 5003, "host", DEST_HOST, NULL);
+  /* no need for synchronisation or preroll on the RTCP sink */
+  g_object_set (rtcpsink, "async", FALSE, "sync", FALSE, NULL);
+
+  rtcpsrc = gst_element_factory_make ("udpsrc", "rtcpsrc");
+  g_assert (rtcpsrc);
+  g_object_set (rtcpsrc, "port", 5007, NULL);
+
+  gst_bin_add_many (GST_BIN (pipeline), rtpsink, rtcpsink, rtcpsrc, NULL);
+
+  /* now link all to the rtpbin, start by getting an RTP sinkpad for session 0 */
+  sinkpad = gst_element_get_request_pad (rtpbin, "send_rtp_sink_0");
+  srcpad = gst_element_get_static_pad (audiopay, "src");
+  lres = gst_pad_link (srcpad, sinkpad);
+  g_assert (lres == GST_PAD_LINK_OK);
+  gst_object_unref (srcpad);
+
+  /* get the RTP srcpad that was created when we requested the sinkpad above and
+   * link it to the rtpsink sinkpad*/
+  srcpad = gst_element_get_static_pad (rtpbin, "send_rtp_src_0");
+  sinkpad = gst_element_get_static_pad (rtpsink, "sink");
+  lres = gst_pad_link (srcpad, sinkpad);
+  g_assert (lres == GST_PAD_LINK_OK);
+  gst_object_unref (srcpad);
+  gst_object_unref (sinkpad);
+
+  /* get an RTCP srcpad for sending RTCP to the receiver */
+  srcpad = gst_element_get_request_pad (rtpbin, "send_rtcp_src_0");
+  sinkpad = gst_element_get_static_pad (rtcpsink, "sink");
+  lres = gst_pad_link (srcpad, sinkpad);
+  g_assert (lres == GST_PAD_LINK_OK);
+  gst_object_unref (sinkpad);
+
+  /* we also want to receive RTCP, request an RTCP sinkpad for session 0 and
+   * link it to the srcpad of the udpsrc for RTCP */
+  srcpad = gst_element_get_static_pad (rtcpsrc, "src");
+  sinkpad = gst_element_get_request_pad (rtpbin, "recv_rtcp_sink_0");
+  lres = gst_pad_link (srcpad, sinkpad);
+  g_assert (lres == GST_PAD_LINK_OK);
+  gst_object_unref (srcpad);
+
+  /* set the pipeline to playing */
+  g_print ("starting sender pipeline\n");
+  gst_element_set_state (pipeline, GST_STATE_PLAYING);
+
+  /* we need to run a GLib main loop to get the messages */
+  loop = g_main_loop_new (NULL, FALSE);
+  g_main_loop_run (loop);
+
+  g_print ("stopping sender pipeline\n");
+  gst_element_set_state (pipeline, GST_STATE_NULL);
+
+  return 0;
+}