rtsp-client: Add unit test of SETUP for RTSP/RTP/TCP
[platform/upstream/gstreamer.git] / examples / test-netclock-client.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  * Copyright (C) 2014 Jan Schmidt <jan@centricular.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include <stdlib.h>
22
23 #include <gst/gst.h>
24 #include <gst/net/gstnet.h>
25
26 #define PLAYBACK_DELAY_MS 40
27
28 static void
29 source_created (GstElement * pipe, GstElement * source)
30 {
31   g_object_set (source, "latency", PLAYBACK_DELAY_MS,
32       "ntp-time-source", 3, "buffer-mode", 4, "ntp-sync", TRUE, NULL);
33 }
34
35 static gboolean
36 message (GstBus * bus, GstMessage * message, gpointer user_data)
37 {
38   GMainLoop *loop = user_data;
39
40   switch (GST_MESSAGE_TYPE (message)) {
41     case GST_MESSAGE_ERROR:{
42       GError *err = NULL;
43       gchar *name, *debug = NULL;
44
45       name = gst_object_get_path_string (message->src);
46       gst_message_parse_error (message, &err, &debug);
47
48       g_printerr ("ERROR: from element %s: %s\n", name, err->message);
49       if (debug != NULL)
50         g_printerr ("Additional debug info:\n%s\n", debug);
51
52       g_error_free (err);
53       g_free (debug);
54       g_free (name);
55
56       g_main_loop_quit (loop);
57       break;
58     }
59     case GST_MESSAGE_WARNING:{
60       GError *err = NULL;
61       gchar *name, *debug = NULL;
62
63       name = gst_object_get_path_string (message->src);
64       gst_message_parse_warning (message, &err, &debug);
65
66       g_printerr ("ERROR: from element %s: %s\n", name, err->message);
67       if (debug != NULL)
68         g_printerr ("Additional debug info:\n%s\n", debug);
69
70       g_error_free (err);
71       g_free (debug);
72       g_free (name);
73       break;
74     }
75     case GST_MESSAGE_EOS:
76       g_print ("Got EOS\n");
77       g_main_loop_quit (loop);
78       break;
79     default:
80       break;
81   }
82
83   return TRUE;
84 }
85
86 int
87 main (int argc, char *argv[])
88 {
89   GstClock *net_clock;
90   gchar *server;
91   gint clock_port;
92   GstElement *pipe;
93   GMainLoop *loop;
94
95   gst_init (&argc, &argv);
96
97   if (argc < 2) {
98     g_print ("usage: %s rtsp://URI clock-IP clock-PORT\n"
99         "example: %s rtsp://localhost:8554/test 127.0.0.1 8554\n",
100         argv[0], argv[0]);
101     return -1;
102   }
103
104   server = argv[2];
105   clock_port = atoi (argv[3]);
106
107   net_clock = gst_net_client_clock_new ("net_clock", server, clock_port, 0);
108   if (net_clock == NULL) {
109     g_print ("Failed to create net clock client for %s:%d\n",
110         server, clock_port);
111     return 1;
112   }
113
114   /* Wait for the clock to stabilise */
115   gst_clock_wait_for_sync (net_clock, GST_CLOCK_TIME_NONE);
116
117   loop = g_main_loop_new (NULL, FALSE);
118
119   pipe = gst_element_factory_make ("playbin", NULL);
120   g_object_set (pipe, "uri", argv[1], NULL);
121   g_signal_connect (pipe, "source-setup", G_CALLBACK (source_created), NULL);
122
123   gst_pipeline_use_clock (GST_PIPELINE (pipe), net_clock);
124
125   /* Set this high enough so that it's higher than the minimum latency
126    * on all receivers */
127   gst_pipeline_set_latency (GST_PIPELINE (pipe), 500 * GST_MSECOND);
128
129   if (gst_element_set_state (pipe,
130           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
131     g_print ("Failed to set state to PLAYING\n");
132     goto exit;
133   };
134
135   gst_bus_add_signal_watch (GST_ELEMENT_BUS (pipe));
136   g_signal_connect (GST_ELEMENT_BUS (pipe), "message", G_CALLBACK (message),
137       loop);
138
139   g_main_loop_run (loop);
140
141 exit:
142   gst_element_set_state (pipe, GST_STATE_NULL);
143   gst_object_unref (pipe);
144   g_main_loop_unref (loop);
145
146   return 0;
147 }