• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2009 Wim Taymans <wim.taymans@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
21  * with newer GLib versions (>= 2.31.0) */
22 #define GLIB_DISABLE_DEPRECATION_WARNINGS
23 
24 #include <string.h>
25 #include <math.h>
26 
27 #include <gst/gst.h>
28 
29 /*
30  * A simple RTP server
31  *  sends the output of alsasrc as alaw encoded RTP on port 5002, RTCP is sent on
32  *  port 5003. The destination is 127.0.0.1.
33  *  the receiver RTCP reports are received on port 5007
34  *
35  * .-------.    .-------.    .-------.      .----------.     .-------.
36  * |alsasrc|    |alawenc|    |pcmapay|      | rtpbin   |     |udpsink|  RTP
37  * |      src->sink    src->sink    src->send_rtp send_rtp->sink     | port=5002
38  * '-------'    '-------'    '-------'      |          |     '-------'
39  *                                          |          |
40  *                                          |          |     .-------.
41  *                                          |          |     |udpsink|  RTCP
42  *                                          |    send_rtcp->sink     | port=5003
43  *                           .-------.      |          |     '-------' sync=false
44  *                RTCP       |udpsrc |      |          |               async=false
45  *              port=5007    |     src->recv_rtcp      |
46  *                           '-------'      '----------'
47  */
48 
49 /* change this to send the RTP data and RTCP to another host */
50 #define DEST_HOST "127.0.0.1"
51 
52 /* #define AUDIO_SRC  "alsasrc" */
53 #define AUDIO_SRC  "audiotestsrc"
54 
55 /* the encoder and payloader elements */
56 #define AUDIO_ENC  "alawenc"
57 #define AUDIO_PAY  "rtppcmapay"
58 
59 /* print the stats of a source */
60 static void
print_source_stats(GObject * source)61 print_source_stats (GObject * source)
62 {
63   GstStructure *stats;
64   gchar *str;
65 
66   /* get the source stats */
67   g_object_get (source, "stats", &stats, NULL);
68 
69   /* simply dump the stats structure */
70   str = gst_structure_to_string (stats);
71   g_print ("source stats: %s\n", str);
72 
73   gst_structure_free (stats);
74   g_free (str);
75 }
76 
77 /* this function is called every second and dumps the RTP manager stats */
78 static gboolean
print_stats(GstElement * rtpbin)79 print_stats (GstElement * rtpbin)
80 {
81   GObject *session;
82   GValueArray *arr;
83   GValue *val;
84   guint i;
85 
86   g_print ("***********************************\n");
87 
88   /* get session 0 */
89   g_signal_emit_by_name (rtpbin, "get-internal-session", 0, &session);
90 
91   /* print all the sources in the session, this includes the internal source */
92   g_object_get (session, "sources", &arr, NULL);
93 
94   for (i = 0; i < arr->n_values; i++) {
95     GObject *source;
96 
97     val = g_value_array_get_nth (arr, i);
98     source = g_value_get_object (val);
99 
100     print_source_stats (source);
101   }
102   g_value_array_free (arr);
103 
104   g_object_unref (session);
105 
106   return TRUE;
107 }
108 
109 /* build a pipeline equivalent to:
110  *
111  * gst-launch-1.0 -v rtpbin name=rtpbin \
112  *    $AUDIO_SRC ! audioconvert ! audioresample ! $AUDIO_ENC ! $AUDIO_PAY ! rtpbin.send_rtp_sink_0  \
113  *           rtpbin.send_rtp_src_0 ! udpsink port=5002 host=$DEST                      \
114  *           rtpbin.send_rtcp_src_0 ! udpsink port=5003 host=$DEST sync=false async=false \
115  *        udpsrc port=5007 ! rtpbin.recv_rtcp_sink_0
116  */
117 int
main(int argc,char * argv[])118 main (int argc, char *argv[])
119 {
120   GstElement *audiosrc, *audioconv, *audiores, *audioenc, *audiopay;
121   GstElement *rtpbin, *rtpsink, *rtcpsink, *rtcpsrc;
122   GstElement *pipeline;
123   GMainLoop *loop;
124   GstPad *srcpad, *sinkpad;
125 
126   /* always init first */
127   gst_init (&argc, &argv);
128 
129   /* the pipeline to hold everything */
130   pipeline = gst_pipeline_new (NULL);
131   g_assert (pipeline);
132 
133   /* the audio capture and format conversion */
134   audiosrc = gst_element_factory_make (AUDIO_SRC, "audiosrc");
135   g_assert (audiosrc);
136   audioconv = gst_element_factory_make ("audioconvert", "audioconv");
137   g_assert (audioconv);
138   audiores = gst_element_factory_make ("audioresample", "audiores");
139   g_assert (audiores);
140   /* the encoding and payloading */
141   audioenc = gst_element_factory_make (AUDIO_ENC, "audioenc");
142   g_assert (audioenc);
143   audiopay = gst_element_factory_make (AUDIO_PAY, "audiopay");
144   g_assert (audiopay);
145 
146   /* add capture and payloading to the pipeline and link */
147   gst_bin_add_many (GST_BIN (pipeline), audiosrc, audioconv, audiores,
148       audioenc, audiopay, NULL);
149 
150   if (!gst_element_link_many (audiosrc, audioconv, audiores, audioenc,
151           audiopay, NULL)) {
152     g_error ("Failed to link audiosrc, audioconv, audioresample, "
153         "audio encoder and audio payloader");
154   }
155 
156   /* the rtpbin element */
157   rtpbin = gst_element_factory_make ("rtpbin", "rtpbin");
158   g_assert (rtpbin);
159 
160   gst_bin_add (GST_BIN (pipeline), rtpbin);
161 
162   /* the udp sinks and source we will use for RTP and RTCP */
163   rtpsink = gst_element_factory_make ("udpsink", "rtpsink");
164   g_assert (rtpsink);
165   g_object_set (rtpsink, "port", 5002, "host", DEST_HOST, NULL);
166 
167   rtcpsink = gst_element_factory_make ("udpsink", "rtcpsink");
168   g_assert (rtcpsink);
169   g_object_set (rtcpsink, "port", 5003, "host", DEST_HOST, NULL);
170   /* no need for synchronisation or preroll on the RTCP sink */
171   g_object_set (rtcpsink, "async", FALSE, "sync", FALSE, NULL);
172 
173   rtcpsrc = gst_element_factory_make ("udpsrc", "rtcpsrc");
174   g_assert (rtcpsrc);
175   g_object_set (rtcpsrc, "port", 5007, NULL);
176 
177   gst_bin_add_many (GST_BIN (pipeline), rtpsink, rtcpsink, rtcpsrc, NULL);
178 
179   /* now link all to the rtpbin, start by getting an RTP sinkpad for session 0 */
180   sinkpad = gst_element_request_pad_simple (rtpbin, "send_rtp_sink_0");
181   srcpad = gst_element_get_static_pad (audiopay, "src");
182   if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK)
183     g_error ("Failed to link audio payloader to rtpbin");
184   gst_object_unref (srcpad);
185 
186   /* get the RTP srcpad that was created when we requested the sinkpad above and
187    * link it to the rtpsink sinkpad*/
188   srcpad = gst_element_get_static_pad (rtpbin, "send_rtp_src_0");
189   sinkpad = gst_element_get_static_pad (rtpsink, "sink");
190   if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK)
191     g_error ("Failed to link rtpbin to rtpsink");
192   gst_object_unref (srcpad);
193   gst_object_unref (sinkpad);
194 
195   /* get an RTCP srcpad for sending RTCP to the receiver */
196   srcpad = gst_element_request_pad_simple (rtpbin, "send_rtcp_src_0");
197   sinkpad = gst_element_get_static_pad (rtcpsink, "sink");
198   if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK)
199     g_error ("Failed to link rtpbin to rtcpsink");
200   gst_object_unref (sinkpad);
201 
202   /* we also want to receive RTCP, request an RTCP sinkpad for session 0 and
203    * link it to the srcpad of the udpsrc for RTCP */
204   srcpad = gst_element_get_static_pad (rtcpsrc, "src");
205   sinkpad = gst_element_request_pad_simple (rtpbin, "recv_rtcp_sink_0");
206   if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK)
207     g_error ("Failed to link rtcpsrc to rtpbin");
208   gst_object_unref (srcpad);
209 
210   /* set the pipeline to playing */
211   g_print ("starting sender pipeline\n");
212   gst_element_set_state (pipeline, GST_STATE_PLAYING);
213 
214   /* print stats every second */
215   g_timeout_add_seconds (1, (GSourceFunc) print_stats, rtpbin);
216 
217   /* we need to run a GLib main loop to get the messages */
218   loop = g_main_loop_new (NULL, FALSE);
219   g_main_loop_run (loop);
220 
221   g_print ("stopping sender pipeline\n");
222   gst_element_set_state (pipeline, GST_STATE_NULL);
223 
224   return 0;
225 }
226