· 7 years ago · Oct 05, 2018, 12:50 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/sydu/Pictures/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 g_free (contents);
56 gst_buffer_unref (buffer);
57 }
58}
59
60static void
61media_free_ctx(MyContext *ctx)
62{
63 g_free(ctx);
64}
65
66/* called when a new media pipeline is constructed. We can query the
67 * pipeline and configure our appsrc */
68static void
69media_configure (GstRTSPMediaFactory * factory, GstRTSPMedia * media,
70 gpointer user_data)
71{
72 GstElement *element, *appsrc;
73 MyContext *ctx;
74
75 /* get the element used for providing the streams of the media */
76 element = gst_rtsp_media_get_element (media);
77
78 /* get our appsrc, we named it 'mysrc' with the name property */
79 appsrc = gst_bin_get_by_name_recurse_up (GST_BIN (element), "mysrc");
80
81 /* configure the caps of the video */
82 g_object_set (G_OBJECT (appsrc), "caps",
83 gst_caps_new_simple ("image/jpeg", NULL), NULL);
84
85 ctx = g_new0 (MyContext, 1);
86 ctx->timestamp = 0;
87
88 /* make sure ther datais freed when the media is gone */
89 g_object_set_data_full (G_OBJECT (media), "my-extra-data", ctx,
90 (GDestroyNotify) media_free_ctx);
91
92 GST_ERROR("Media configured");
93
94 /* install the callback that will be called when a buffer is needed */
95 g_signal_connect (appsrc, "need-data", (GCallback) need_data, ctx);
96 gst_object_unref (appsrc);
97 gst_object_unref (element);
98}
99
100int
101main (int argc, char *argv[])
102{
103 GMainLoop *loop;
104 GstRTSPServer *server;
105 GstRTSPMountPoints *mounts;
106 GstRTSPMediaFactory *factory;
107
108 gst_init (&argc, &argv);
109
110 loop = g_main_loop_new (NULL, FALSE);
111
112 /* create a server instance */
113 server = gst_rtsp_server_new ();
114
115 /* get the mount points for this server, every server has a default object
116 * that be used to map uri mount points to media factories */
117 mounts = gst_rtsp_server_get_mount_points (server);
118
119 /* make a media factory for a test stream. The default media factory can use
120 * gst-launch syntax to create pipelines.
121 * any launch line works as long as it contains elements named pay%d. Each
122 * element with pay%d names will be a stream */
123 factory = gst_rtsp_media_factory_new ();
124 gst_rtsp_media_factory_set_launch (factory,
125 "( appsrc name=mysrc ! jpegparse ! rtpjpegpay name=pay0 )");
126
127 /* notify when our media is ready, This is called whenever someone asks for
128 * the media and a new pipeline with our appsrc is created */
129 g_signal_connect (factory, "media-configure", (GCallback) media_configure,
130 NULL);
131
132 /* attach the test factory to the /test url */
133 gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
134
135 /* don't need the ref to the mounts anymore */
136 g_object_unref (mounts);
137
138 /* set server port */
139 gst_rtsp_server_set_service (server, "5002");
140
141 /* attach the server to the default maincontext */
142 gst_rtsp_server_attach (server, NULL);
143
144 /* start serving */
145 g_print ("stream ready at rtsp://127.0.0.1:5002/test\n");
146 g_main_loop_run (loop);
147
148 return 0;
149}