examples: Add a simple example of network synch for live streams.
[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, GParamSpec * pspec)
30 {
31   GstElement *source;
32
33   g_object_get (pipe, "source", &source, NULL);
34   g_assert (source != NULL);
35
36   g_object_set (source, "latency", PLAYBACK_DELAY_MS,
37       "use-pipeline-clock", TRUE, "buffer-mode", 1, NULL);
38
39   gst_object_unref (source);
40 }
41
42 int
43 main (int argc, char *argv[])
44 {
45   GstClock *net_clock;
46   gchar *server;
47   gint clock_port;
48   GstElement *pipe;
49   GstMessage *msg;
50
51   gst_init (&argc, &argv);
52
53   if (argc < 2) {
54     g_print ("usage: %s rtsp://URI clock-IP clock-PORT\n"
55         "example: %s rtsp://localhost:8554/test 127.0.0.1 8554\n",
56         argv[0], argv[0]);
57     return -1;
58   }
59
60   server = argv[2];
61   clock_port = atoi (argv[3]);
62
63   net_clock = gst_net_client_clock_new ("net_clock", server, clock_port, 0);
64   if (net_clock == NULL) {
65     g_print ("Failed to create net clock client for %s:%d\n",
66         server, clock_port);
67     return 1;
68   }
69
70   /* Wait 0.5 seconds for the clock to stabilise */
71   g_usleep (G_USEC_PER_SEC / 2);
72
73   pipe = gst_element_factory_make ("playbin", NULL);
74   g_object_set (pipe, "uri", argv[1], NULL);
75   g_signal_connect (pipe, "notify::source", (GCallback) source_created, NULL);
76
77   gst_element_set_start_time (pipe, GST_CLOCK_TIME_NONE);
78   gst_element_set_base_time (pipe, 0);
79   gst_pipeline_use_clock (GST_PIPELINE (pipe), net_clock);
80
81   if (gst_element_set_state (pipe,
82           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
83     g_print ("Failed to set state to PLAYING\n");
84     goto exit;
85   };
86
87   msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (pipe),
88       GST_CLOCK_TIME_NONE, GST_MESSAGE_EOS | GST_MESSAGE_ERROR);
89
90   if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
91     GError *err = NULL;
92     gchar *debug = NULL;
93     gst_message_parse_error (msg, &err, &debug);
94     g_print ("\nERROR: %s\n%s\n\n", err->message, debug);
95     g_error_free (err);
96     g_free (debug);
97   }
98   gst_message_unref (msg);
99
100 exit:
101   gst_element_set_state (pipe, GST_STATE_NULL);
102   gst_object_unref (pipe);
103
104   return 0;
105 }