Blame tests/examples/rtp/client-PCMA.c

Packit 1f69a5
/* GStreamer
Packit 1f69a5
 * Copyright (C) 2009 Wim Taymans <wim.taymans@gmail.com>
Packit 1f69a5
 *
Packit 1f69a5
 * This library is free software; you can redistribute it and/or
Packit 1f69a5
 * modify it under the terms of the GNU Library General Public
Packit 1f69a5
 * License as published by the Free Software Foundation; either
Packit 1f69a5
 * version 2 of the License, or (at your option) any later version.
Packit 1f69a5
 *
Packit 1f69a5
 * This library is distributed in the hope that it will be useful,
Packit 1f69a5
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 1f69a5
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 1f69a5
 * Library General Public License for more details.
Packit 1f69a5
 *
Packit 1f69a5
 * You should have received a copy of the GNU Library General Public
Packit 1f69a5
 * License along with this library; if not, write to the
Packit 1f69a5
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 1f69a5
 * Boston, MA 02110-1301, USA.
Packit 1f69a5
 */
Packit 1f69a5
Packit 1f69a5
#include <string.h>
Packit 1f69a5
#include <math.h>
Packit 1f69a5
Packit 1f69a5
#include <gst/gst.h>
Packit 1f69a5
Packit 1f69a5
/*
Packit 1f69a5
 * A simple RTP receiver 
Packit 1f69a5
 *
Packit 1f69a5
 *  receives alaw encoded RTP audio on port 5002, RTCP is received on  port 5003.
Packit 1f69a5
 *  the receiver RTCP reports are sent to port 5007
Packit 1f69a5
 *
Packit 1f69a5
 *             .-------.      .----------.     .---------.   .-------.   .--------.
Packit 1f69a5
 *  RTP        |udpsrc |      | rtpbin   |     |pcmadepay|   |alawdec|   |alsasink|
Packit 1f69a5
 *  port=5002  |      src->recv_rtp recv_rtp->sink     src->sink   src->sink      |
Packit 1f69a5
 *             '-------'      |          |     '---------'   '-------'   '--------'
Packit 1f69a5
 *                            |          |      
Packit 1f69a5
 *                            |          |     .-------.
Packit 1f69a5
 *                            |          |     |udpsink|  RTCP
Packit 1f69a5
 *                            |    send_rtcp->sink     | port=5007
Packit 1f69a5
 *             .-------.      |          |     '-------' sync=false
Packit 1f69a5
 *  RTCP       |udpsrc |      |          |               async=false
Packit 1f69a5
 *  port=5003  |     src->recv_rtcp      |                       
Packit 1f69a5
 *             '-------'      '----------'              
Packit 1f69a5
 */
Packit 1f69a5
Packit 1f69a5
/* the caps of the sender RTP stream. This is usually negotiated out of band with
Packit 1f69a5
 * SDP or RTSP. */
Packit 1f69a5
#define AUDIO_CAPS "application/x-rtp,media=(string)audio,clock-rate=(int)8000,encoding-name=(string)PCMA"
Packit 1f69a5
Packit 1f69a5
#define AUDIO_DEPAY "rtppcmadepay"
Packit 1f69a5
#define AUDIO_DEC   "alawdec"
Packit 1f69a5
#define AUDIO_SINK  "autoaudiosink"
Packit 1f69a5
Packit 1f69a5
/* the destination machine to send RTCP to. This is the address of the sender and
Packit 1f69a5
 * is used to send back the RTCP reports of this receiver. If the data is sent
Packit 1f69a5
 * from another machine, change this address. */
Packit 1f69a5
#define DEST_HOST "127.0.0.1"
Packit 1f69a5
Packit 1f69a5
/* print the stats of a source */
Packit 1f69a5
static void
Packit 1f69a5
print_source_stats (GObject * source)
Packit 1f69a5
{
Packit 1f69a5
  GstStructure *stats;
Packit 1f69a5
  gchar *str;
Packit 1f69a5
Packit 1f69a5
  g_return_if_fail (source != NULL);
Packit 1f69a5
Packit 1f69a5
  /* get the source stats */
Packit 1f69a5
  g_object_get (source, "stats", &stats, NULL);
Packit 1f69a5
Packit 1f69a5
  /* simply dump the stats structure */
Packit 1f69a5
  str = gst_structure_to_string (stats);
Packit 1f69a5
  g_print ("source stats: %s\n", str);
Packit 1f69a5
Packit 1f69a5
  gst_structure_free (stats);
Packit 1f69a5
  g_free (str);
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
/* will be called when rtpbin signals on-ssrc-active. It means that an RTCP
Packit 1f69a5
 * packet was received from another source. */
Packit 1f69a5
static void
Packit 1f69a5
on_ssrc_active_cb (GstElement * rtpbin, guint sessid, guint ssrc,
Packit 1f69a5
    GstElement * depay)
Packit 1f69a5
{
Packit 1f69a5
  GObject *session, *osrc;
Packit 1f69a5
Packit 1f69a5
  g_print ("got RTCP from session %u, SSRC %u\n", sessid, ssrc);
Packit 1f69a5
Packit 1f69a5
  /* get the right session */
Packit 1f69a5
  g_signal_emit_by_name (rtpbin, "get-internal-session", sessid, &session);
Packit 1f69a5
Packit 1f69a5
#if 0
Packit 1f69a5
  /* FIXME: This is broken in rtpbin */
Packit 1f69a5
  /* get the internal source (the SSRC allocated to us, the receiver */
Packit 1f69a5
  g_object_get (session, "internal-source", &isrc, NULL);
Packit 1f69a5
  print_source_stats (isrc);
Packit 1f69a5
  g_object_unref (isrc);
Packit 1f69a5
#endif
Packit 1f69a5
Packit 1f69a5
  /* get the remote source that sent us RTCP */
Packit 1f69a5
  g_signal_emit_by_name (session, "get-source-by-ssrc", ssrc, &osrc);
Packit 1f69a5
  print_source_stats (osrc);
Packit 1f69a5
  g_object_unref (osrc);
Packit 1f69a5
  g_object_unref (session);
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
/* will be called when rtpbin has validated a payload that we can depayload */
Packit 1f69a5
static void
Packit 1f69a5
pad_added_cb (GstElement * rtpbin, GstPad * new_pad, GstElement * depay)
Packit 1f69a5
{
Packit 1f69a5
  GstPad *sinkpad;
Packit 1f69a5
  GstPadLinkReturn lres;
Packit 1f69a5
Packit 1f69a5
  g_print ("new payload on pad: %s\n", GST_PAD_NAME (new_pad));
Packit 1f69a5
Packit 1f69a5
  sinkpad = gst_element_get_static_pad (depay, "sink");
Packit 1f69a5
  g_assert (sinkpad);
Packit 1f69a5
Packit 1f69a5
  lres = gst_pad_link (new_pad, sinkpad);
Packit 1f69a5
  g_assert (lres == GST_PAD_LINK_OK);
Packit 1f69a5
  gst_object_unref (sinkpad);
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
/* build a pipeline equivalent to:
Packit 1f69a5
 *
Packit 1f69a5
 * gst-launch-1.0 -v rtpbin name=rtpbin                                                \
Packit 1f69a5
 *      udpsrc caps=$AUDIO_CAPS port=5002 ! rtpbin.recv_rtp_sink_0              \
Packit 1f69a5
 *        rtpbin. ! rtppcmadepay ! alawdec ! audioconvert ! audioresample ! autoaudiosink \
Packit 1f69a5
 *      udpsrc port=5003 ! rtpbin.recv_rtcp_sink_0                              \
Packit 1f69a5
 *        rtpbin.send_rtcp_src_0 ! udpsink port=5007 host=$DEST sync=false async=false
Packit 1f69a5
 */
Packit 1f69a5
int
Packit 1f69a5
main (int argc, char *argv[])
Packit 1f69a5
{
Packit 1f69a5
  GstElement *rtpbin, *rtpsrc, *rtcpsrc, *rtcpsink;
Packit 1f69a5
  GstElement *audiodepay, *audiodec, *audiores, *audioconv, *audiosink;
Packit 1f69a5
  GstElement *pipeline;
Packit 1f69a5
  GMainLoop *loop;
Packit 1f69a5
  GstCaps *caps;
Packit 1f69a5
  gboolean res;
Packit 1f69a5
  GstPadLinkReturn lres;
Packit 1f69a5
  GstPad *srcpad, *sinkpad;
Packit 1f69a5
Packit 1f69a5
  /* always init first */
Packit 1f69a5
  gst_init (&argc, &argv);
Packit 1f69a5
Packit 1f69a5
  /* the pipeline to hold everything */
Packit 1f69a5
  pipeline = gst_pipeline_new (NULL);
Packit 1f69a5
  g_assert (pipeline);
Packit 1f69a5
Packit 1f69a5
  /* the udp src and source we will use for RTP and RTCP */
Packit 1f69a5
  rtpsrc = gst_element_factory_make ("udpsrc", "rtpsrc");
Packit 1f69a5
  g_assert (rtpsrc);
Packit 1f69a5
  g_object_set (rtpsrc, "port", 5002, NULL);
Packit 1f69a5
  /* we need to set caps on the udpsrc for the RTP data */
Packit 1f69a5
  caps = gst_caps_from_string (AUDIO_CAPS);
Packit 1f69a5
  g_object_set (rtpsrc, "caps", caps, NULL);
Packit 1f69a5
  gst_caps_unref (caps);
Packit 1f69a5
Packit 1f69a5
  rtcpsrc = gst_element_factory_make ("udpsrc", "rtcpsrc");
Packit 1f69a5
  g_assert (rtcpsrc);
Packit 1f69a5
  g_object_set (rtcpsrc, "port", 5003, NULL);
Packit 1f69a5
Packit 1f69a5
  rtcpsink = gst_element_factory_make ("udpsink", "rtcpsink");
Packit 1f69a5
  g_assert (rtcpsink);
Packit 1f69a5
  g_object_set (rtcpsink, "port", 5007, "host", DEST_HOST, NULL);
Packit 1f69a5
  /* no need for synchronisation or preroll on the RTCP sink */
Packit 1f69a5
  g_object_set (rtcpsink, "async", FALSE, "sync", FALSE, NULL);
Packit 1f69a5
Packit 1f69a5
  gst_bin_add_many (GST_BIN (pipeline), rtpsrc, rtcpsrc, rtcpsink, NULL);
Packit 1f69a5
Packit 1f69a5
  /* the depayloading and decoding */
Packit 1f69a5
  audiodepay = gst_element_factory_make (AUDIO_DEPAY, "audiodepay");
Packit 1f69a5
  g_assert (audiodepay);
Packit 1f69a5
  audiodec = gst_element_factory_make (AUDIO_DEC, "audiodec");
Packit 1f69a5
  g_assert (audiodec);
Packit 1f69a5
  /* the audio playback and format conversion */
Packit 1f69a5
  audioconv = gst_element_factory_make ("audioconvert", "audioconv");
Packit 1f69a5
  g_assert (audioconv);
Packit 1f69a5
  audiores = gst_element_factory_make ("audioresample", "audiores");
Packit 1f69a5
  g_assert (audiores);
Packit 1f69a5
  audiosink = gst_element_factory_make (AUDIO_SINK, "audiosink");
Packit 1f69a5
  g_assert (audiosink);
Packit 1f69a5
Packit 1f69a5
  /* add depayloading and playback to the pipeline and link */
Packit 1f69a5
  gst_bin_add_many (GST_BIN (pipeline), audiodepay, audiodec, audioconv,
Packit 1f69a5
      audiores, audiosink, NULL);
Packit 1f69a5
Packit 1f69a5
  res = gst_element_link_many (audiodepay, audiodec, audioconv, audiores,
Packit 1f69a5
      audiosink, NULL);
Packit 1f69a5
  g_assert (res == TRUE);
Packit 1f69a5
Packit 1f69a5
  /* the rtpbin element */
Packit 1f69a5
  rtpbin = gst_element_factory_make ("rtpbin", "rtpbin");
Packit 1f69a5
  g_assert (rtpbin);
Packit 1f69a5
Packit 1f69a5
  gst_bin_add (GST_BIN (pipeline), rtpbin);
Packit 1f69a5
Packit 1f69a5
  /* now link all to the rtpbin, start by getting an RTP sinkpad for session 0 */
Packit 1f69a5
  srcpad = gst_element_get_static_pad (rtpsrc, "src");
Packit 1f69a5
  sinkpad = gst_element_get_request_pad (rtpbin, "recv_rtp_sink_0");
Packit 1f69a5
  lres = gst_pad_link (srcpad, sinkpad);
Packit 1f69a5
  g_assert (lres == GST_PAD_LINK_OK);
Packit 1f69a5
  gst_object_unref (srcpad);
Packit 1f69a5
Packit 1f69a5
  /* get an RTCP sinkpad in session 0 */
Packit 1f69a5
  srcpad = gst_element_get_static_pad (rtcpsrc, "src");
Packit 1f69a5
  sinkpad = gst_element_get_request_pad (rtpbin, "recv_rtcp_sink_0");
Packit 1f69a5
  lres = gst_pad_link (srcpad, sinkpad);
Packit 1f69a5
  g_assert (lres == GST_PAD_LINK_OK);
Packit 1f69a5
  gst_object_unref (srcpad);
Packit 1f69a5
  gst_object_unref (sinkpad);
Packit 1f69a5
Packit 1f69a5
  /* get an RTCP srcpad for sending RTCP back to the sender */
Packit 1f69a5
  srcpad = gst_element_get_request_pad (rtpbin, "send_rtcp_src_0");
Packit 1f69a5
  sinkpad = gst_element_get_static_pad (rtcpsink, "sink");
Packit 1f69a5
  lres = gst_pad_link (srcpad, sinkpad);
Packit 1f69a5
  g_assert (lres == GST_PAD_LINK_OK);
Packit 1f69a5
  gst_object_unref (sinkpad);
Packit 1f69a5
Packit 1f69a5
  /* the RTP pad that we have to connect to the depayloader will be created
Packit 1f69a5
   * dynamically so we connect to the pad-added signal, pass the depayloader as
Packit 1f69a5
   * user_data so that we can link to it. */
Packit 1f69a5
  g_signal_connect (rtpbin, "pad-added", G_CALLBACK (pad_added_cb), audiodepay);
Packit 1f69a5
Packit 1f69a5
  /* give some stats when we receive RTCP */
Packit 1f69a5
  g_signal_connect (rtpbin, "on-ssrc-active", G_CALLBACK (on_ssrc_active_cb),
Packit 1f69a5
      audiodepay);
Packit 1f69a5
Packit 1f69a5
  /* set the pipeline to playing */
Packit 1f69a5
  g_print ("starting receiver pipeline\n");
Packit 1f69a5
  gst_element_set_state (pipeline, GST_STATE_PLAYING);
Packit 1f69a5
Packit 1f69a5
  /* we need to run a GLib main loop to get the messages */
Packit 1f69a5
  loop = g_main_loop_new (NULL, FALSE);
Packit 1f69a5
  g_main_loop_run (loop);
Packit 1f69a5
Packit 1f69a5
  g_print ("stopping receiver pipeline\n");
Packit 1f69a5
  gst_element_set_state (pipeline, GST_STATE_NULL);
Packit 1f69a5
Packit 1f69a5
  gst_object_unref (pipeline);
Packit 1f69a5
Packit 1f69a5
  return 0;
Packit 1f69a5
}