· 7 years ago · Oct 05, 2018, 12:02 PM
1/* GStreamer
2 * Copyright (C) 2008 Wim Taymans <wim.taymans at 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#include <stdio.h>
21#include <stdlib.h>
22
23#include <gst/gst.h>
24
25#include <gst/rtsp-server/rtsp-server.h>
26
27typedef struct
28{
29 GstClockTime timestamp;
30 FILE *file;
31} MyContext;
32
33
34/* called when we need to give data to appsrc */
35static void
36need_data (GstElement * appsrc, guint unused, MyContext * ctx)
37{
38 gchar *contents = NULL;
39 gsize length = 0;
40 GError *error = NULL;
41
42 if (!g_file_get_contents ("/home/fyadmin/gstreamer_training/hermione.jpg", &contents, &length, &error))
43 {
44 GST_ERROR ("Damn son");
45 } else {
46 GstFlowReturn ret;
47 GstBuffer *buffer = gst_buffer_new_allocate (NULL, length, NULL);
48
49 gst_buffer_fill (buffer, 0, contents, length);
50 GST_BUFFER_DURATION (buffer) = 40 * GST_MSECOND;
51 GST_BUFFER_PTS (buffer) = ctx->timestamp;
52 GST_BUFFER_DTS (buffer) = GST_BUFFER_PTS (buffer);
53 ctx->timestamp += GST_BUFFER_DURATION (buffer);
54 g_signal_emit_by_name (appsrc, "push-buffer", buffer, &ret);
55 gst_buffer_unref (buffer);
56 }
57}
58
59static void
60media_free_ctx(MyContext *ctx)
61{
62 g_free(ctx);
63}
64
65/* called when a new media pipeline is constructed. We can query the
66 * pipeline and configure our appsrc */
67static void
68media_configure (GstRTSPMediaFactory * factory, GstRTSPMedia * media,
69 gpointer user_data)
70{
71 GstElement *element, *appsrc;
72 MyContext *ctx;
73
74 /* get the element used for providing the streams of the media */
75 element = gst_rtsp_media_get_element (media);
76
77 /* get our appsrc, we named it 'mysrc' with the name property */
78 appsrc = gst_bin_get_by_name_recurse_up (GST_BIN (element), "mysrc");
79
80 /* configure the caps of the video */
81 g_object_set (G_OBJECT (appsrc), "caps",
82 gst_caps_new_simple ("image/jpeg", NULL), NULL);
83
84 ctx = g_new0 (MyContext, 1);
85 ctx->timestamp = 0;
86
87 /* make sure ther datais freed when the media is gone */
88 g_object_set_data_full (G_OBJECT (media), "my-extra-data", ctx,
89 (GDestroyNotify) media_free_ctx);
90
91 GST_ERROR("Media configured");
92
93 /* install the callback that will be called when a buffer is needed */
94 g_signal_connect (appsrc, "need-data", (GCallback) need_data, ctx);
95 gst_object_unref (appsrc);
96 gst_object_unref (element);
97}
98
99int
100main (int argc, char *argv[])
101{
102 GMainLoop *loop;
103 GstRTSPServer *server;
104 GstRTSPMountPoints *mounts;
105 GstRTSPMediaFactory *factory;
106
107 gst_init (&argc, &argv);
108
109 loop = g_main_loop_new (NULL, FALSE);
110
111 /* create a server instance */
112 server = gst_rtsp_server_new ();
113
114 /* get the mount points for this server, every server has a default object
115 * that be used to map uri mount points to media factories */
116 mounts = gst_rtsp_server_get_mount_points (server);
117
118 /* make a media factory for a test stream. The default media factory can use
119 * gst-launch syntax to create pipelines.
120 * any launch line works as long as it contains elements named pay%d. Each
121 * element with pay%d names will be a stream */
122 factory = gst_rtsp_media_factory_new ();
123 gst_rtsp_media_factory_set_launch (factory,
124 "( appsrc name=mysrc ! jpegparse ! rtpjpegpay name=pay0 )");
125
126 /* notify when our media is ready, This is called whenever someone asks for
127 * the media and a new pipeline with our appsrc is created */
128 g_signal_connect (factory, "media-configure", (GCallback) media_configure,
129 NULL);
130
131 /* attach the test factory to the /test url */
132 gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
133
134 /* don't need the ref to the mounts anymore */
135 g_object_unref (mounts);
136
137 /* set server port */
138 gst_rtsp_server_set_service (server, "5002");
139
140 /* attach the server to the default maincontext */
141 gst_rtsp_server_attach (server, NULL);
142
143 /* start serving */
144 g_print ("stream ready at rtsp://127.0.0.1:5002/test\n");
145 g_main_loop_run (loop);
146
147 return 0;
148}