Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / tests / examples / rtp / server-alsasrc-PCMA.c
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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <string.h>
21 #include <math.h>
22
23 #include <gst/gst.h>
24
25 /*
26  * A simple RTP server 
27  *  sends the output of alsasrc as alaw encoded RTP on port 5002, RTCP is sent on
28  *  port 5003. The destination is 127.0.0.1.
29  *  the receiver RTCP reports are received on port 5007
30  *
31  * .-------.    .-------.    .-------.      .----------.     .-------.
32  * |alsasrc|    |alawenc|    |pcmapay|      | rtpbin   |     |udpsink|  RTP
33  * |      src->sink    src->sink    src->send_rtp send_rtp->sink     | port=5002
34  * '-------'    '-------'    '-------'      |          |     '-------'
35  *                                          |          |      
36  *                                          |          |     .-------.
37  *                                          |          |     |udpsink|  RTCP
38  *                                          |    send_rtcp->sink     | port=5003
39  *                           .-------.      |          |     '-------' sync=false
40  *                RTCP       |udpsrc |      |          |               async=false
41  *              port=5007    |     src->recv_rtcp      |                       
42  *                           '-------'      '----------'              
43  */
44
45 /* change this to send the RTP data and RTCP to another host */
46 #define DEST_HOST "127.0.0.1"
47
48 /* #define AUDIO_SRC  "alsasrc" */
49 #define AUDIO_SRC  "audiotestsrc"
50
51 /* the encoder and payloader elements */
52 #define AUDIO_ENC  "alawenc"
53 #define AUDIO_PAY  "rtppcmapay"
54
55 /* print the stats of a source */
56 static void
57 print_source_stats (GObject * source)
58 {
59   GstStructure *stats;
60   gchar *str;
61
62   /* get the source stats */
63   g_object_get (source, "stats", &stats, NULL);
64
65   /* simply dump the stats structure */
66   str = gst_structure_to_string (stats);
67   g_print ("source stats: %s\n", str);
68
69   gst_structure_free (stats);
70   g_free (str);
71 }
72
73 /* this function is called every second and dumps the RTP manager stats */
74 static gboolean
75 print_stats (GstElement * rtpbin)
76 {
77   GObject *session;
78   GValueArray *arr;
79   GValue *val;
80   guint i;
81
82   g_print ("***********************************\n");
83
84   /* get session 0 */
85   g_signal_emit_by_name (rtpbin, "get-internal-session", 0, &session);
86
87   /* print all the sources in the session, this includes the internal source */
88   g_object_get (session, "sources", &arr, NULL);
89
90   for (i = 0; i < arr->n_values; i++) {
91     GObject *source;
92
93     val = g_value_array_get_nth (arr, i);
94     source = g_value_get_object (val);
95
96     print_source_stats (source);
97   }
98   g_value_array_free (arr);
99
100   g_object_unref (session);
101
102   return TRUE;
103 }
104
105 /* build a pipeline equivalent to:
106  *
107  * gst-launch -v gstrtpbin name=rtpbin \
108  *    $AUDIO_SRC ! audioconvert ! audioresample ! $AUDIO_ENC ! $AUDIO_PAY ! rtpbin.send_rtp_sink_0  \
109  *           rtpbin.send_rtp_src_0 ! udpsink port=5002 host=$DEST                      \
110  *           rtpbin.send_rtcp_src_0 ! udpsink port=5003 host=$DEST sync=false async=false \
111  *        udpsrc port=5007 ! rtpbin.recv_rtcp_sink_0
112  */
113 int
114 main (int argc, char *argv[])
115 {
116   GstElement *audiosrc, *audioconv, *audiores, *audioenc, *audiopay;
117   GstElement *rtpbin, *rtpsink, *rtcpsink, *rtcpsrc;
118   GstElement *pipeline;
119   GMainLoop *loop;
120   GstPad *srcpad, *sinkpad;
121
122   /* always init first */
123   gst_init (&argc, &argv);
124
125   /* the pipeline to hold everything */
126   pipeline = gst_pipeline_new (NULL);
127   g_assert (pipeline);
128
129   /* the audio capture and format conversion */
130   audiosrc = gst_element_factory_make (AUDIO_SRC, "audiosrc");
131   g_assert (audiosrc);
132   audioconv = gst_element_factory_make ("audioconvert", "audioconv");
133   g_assert (audioconv);
134   audiores = gst_element_factory_make ("audioresample", "audiores");
135   g_assert (audiores);
136   /* the encoding and payloading */
137   audioenc = gst_element_factory_make (AUDIO_ENC, "audioenc");
138   g_assert (audioenc);
139   audiopay = gst_element_factory_make (AUDIO_PAY, "audiopay");
140   g_assert (audiopay);
141
142   /* add capture and payloading to the pipeline and link */
143   gst_bin_add_many (GST_BIN (pipeline), audiosrc, audioconv, audiores,
144       audioenc, audiopay, NULL);
145
146   if (!gst_element_link_many (audiosrc, audioconv, audiores, audioenc,
147           audiopay, NULL)) {
148     g_error ("Failed to link audiosrc, audioconv, audioresample, "
149         "audio encoder and audio payloader");
150   }
151
152   /* the rtpbin element */
153   rtpbin = gst_element_factory_make ("gstrtpbin", "rtpbin");
154   g_assert (rtpbin);
155
156   gst_bin_add (GST_BIN (pipeline), rtpbin);
157
158   /* the udp sinks and source we will use for RTP and RTCP */
159   rtpsink = gst_element_factory_make ("udpsink", "rtpsink");
160   g_assert (rtpsink);
161   g_object_set (rtpsink, "port", 5002, "host", DEST_HOST, NULL);
162
163   rtcpsink = gst_element_factory_make ("udpsink", "rtcpsink");
164   g_assert (rtcpsink);
165   g_object_set (rtcpsink, "port", 5003, "host", DEST_HOST, NULL);
166   /* no need for synchronisation or preroll on the RTCP sink */
167   g_object_set (rtcpsink, "async", FALSE, "sync", FALSE, NULL);
168
169   rtcpsrc = gst_element_factory_make ("udpsrc", "rtcpsrc");
170   g_assert (rtcpsrc);
171   g_object_set (rtcpsrc, "port", 5007, NULL);
172
173   gst_bin_add_many (GST_BIN (pipeline), rtpsink, rtcpsink, rtcpsrc, NULL);
174
175   /* now link all to the rtpbin, start by getting an RTP sinkpad for session 0 */
176   sinkpad = gst_element_get_request_pad (rtpbin, "send_rtp_sink_0");
177   srcpad = gst_element_get_static_pad (audiopay, "src");
178   if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK)
179     g_error ("Failed to link audio payloader to rtpbin");
180   gst_object_unref (srcpad);
181
182   /* get the RTP srcpad that was created when we requested the sinkpad above and
183    * link it to the rtpsink sinkpad*/
184   srcpad = gst_element_get_static_pad (rtpbin, "send_rtp_src_0");
185   sinkpad = gst_element_get_static_pad (rtpsink, "sink");
186   if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK)
187     g_error ("Failed to link rtpbin to rtpsink");
188   gst_object_unref (srcpad);
189   gst_object_unref (sinkpad);
190
191   /* get an RTCP srcpad for sending RTCP to the receiver */
192   srcpad = gst_element_get_request_pad (rtpbin, "send_rtcp_src_0");
193   sinkpad = gst_element_get_static_pad (rtcpsink, "sink");
194   if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK)
195     g_error ("Failed to link rtpbin to rtcpsink");
196   gst_object_unref (sinkpad);
197
198   /* we also want to receive RTCP, request an RTCP sinkpad for session 0 and
199    * link it to the srcpad of the udpsrc for RTCP */
200   srcpad = gst_element_get_static_pad (rtcpsrc, "src");
201   sinkpad = gst_element_get_request_pad (rtpbin, "recv_rtcp_sink_0");
202   if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK)
203     g_error ("Failed to link rtcpsrc to rtpbin");
204   gst_object_unref (srcpad);
205
206   /* set the pipeline to playing */
207   g_print ("starting sender pipeline\n");
208   gst_element_set_state (pipeline, GST_STATE_PLAYING);
209
210   /* print stats every second */
211   g_timeout_add (1000, (GSourceFunc) print_stats, rtpbin);
212
213   /* we need to run a GLib main loop to get the messages */
214   loop = g_main_loop_new (NULL, FALSE);
215   g_main_loop_run (loop);
216
217   g_print ("stopping sender pipeline\n");
218   gst_element_set_state (pipeline, GST_STATE_NULL);
219
220   return 0;
221 }