1 /* GStreamer
2  * Copyright (C) <2005,2006> Wim Taymans <wim@fluendo.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 /* Element-Checklist-Version: 5 */
20 
21 /**
22  * SECTION:element-rtspwms
23  *
24  * A WMS RTSP extension
25  */
26 
27 #include <string.h>
28 
29 #include <gst/rtsp/gstrtspextension.h>
30 
31 #include "gstrtspwms.h"
32 
33 GST_DEBUG_CATEGORY_STATIC (rtspwms_debug);
34 #define GST_CAT_DEFAULT (rtspwms_debug)
35 
36 #define SERVER_PREFIX "WMServer/"
37 #define HEADER_PREFIX "data:application/vnd.ms.wms-hdr.asfv1;base64,"
38 #define EXTENSION_CMD "application/x-wms-extension-cmd"
39 
40 static GstRTSPResult
gst_rtsp_wms_before_send(GstRTSPExtension * ext,GstRTSPMessage * request)41 gst_rtsp_wms_before_send (GstRTSPExtension * ext, GstRTSPMessage * request)
42 {
43   GstRTSPWMS *ctx = (GstRTSPWMS *) ext;
44 
45   GST_DEBUG_OBJECT (ext, "before send");
46 
47   switch (request->type_data.request.method) {
48     case GST_RTSP_OPTIONS:
49     {
50       /* activate ourselves with the first request */
51       ctx->active = TRUE;
52       break;
53     }
54     default:
55       break;
56   }
57   return GST_RTSP_OK;
58 }
59 
60 static GstRTSPResult
gst_rtsp_wms_after_send(GstRTSPExtension * ext,GstRTSPMessage * req,GstRTSPMessage * resp)61 gst_rtsp_wms_after_send (GstRTSPExtension * ext, GstRTSPMessage * req,
62     GstRTSPMessage * resp)
63 {
64   GstRTSPWMS *ctx = (GstRTSPWMS *) ext;
65 
66   GST_DEBUG_OBJECT (ext, "after send");
67 
68   switch (req->type_data.request.method) {
69     case GST_RTSP_OPTIONS:
70     {
71       gchar *server = NULL;
72 
73       gst_rtsp_message_get_header (resp, GST_RTSP_HDR_SERVER, &server, 0);
74       if (server && g_str_has_prefix (server, SERVER_PREFIX))
75         ctx->active = TRUE;
76       else
77         ctx->active = FALSE;
78       break;
79     }
80     default:
81       break;
82   }
83   return GST_RTSP_OK;
84 }
85 
86 
87 static GstRTSPResult
gst_rtsp_wms_parse_sdp(GstRTSPExtension * ext,GstSDPMessage * sdp,GstStructure * props)88 gst_rtsp_wms_parse_sdp (GstRTSPExtension * ext, GstSDPMessage * sdp,
89     GstStructure * props)
90 {
91   const gchar *config, *maxps;
92   gint i;
93   GstRTSPWMS *ctx = (GstRTSPWMS *) ext;
94 
95   if (!ctx->active)
96     return GST_RTSP_OK;
97 
98   for (i = 0; (config = gst_sdp_message_get_attribute_val_n (sdp, "pgmpu", i));
99       i++) {
100     if (g_str_has_prefix (config, HEADER_PREFIX)) {
101       config += strlen (HEADER_PREFIX);
102       gst_structure_set (props, "config", G_TYPE_STRING, config, NULL);
103       break;
104     }
105   }
106   if (config == NULL)
107     goto no_config;
108 
109   gst_structure_set (props, "config", G_TYPE_STRING, config, NULL);
110 
111   maxps = gst_sdp_message_get_attribute_val (sdp, "maxps");
112   if (maxps)
113     gst_structure_set (props, "maxps", G_TYPE_STRING, maxps, NULL);
114 
115   gst_structure_set (props, "encoding-name", G_TYPE_STRING, "X-ASF-PF", NULL);
116   gst_structure_set (props, "media", G_TYPE_STRING, "application", NULL);
117 
118   return GST_RTSP_OK;
119 
120   /* ERRORS */
121 no_config:
122   {
123     GST_DEBUG_OBJECT (ctx, "Could not find config SDP field, deactivating.");
124     ctx->active = FALSE;
125     return GST_RTSP_OK;
126   }
127 }
128 
129 static gboolean
gst_rtsp_wms_configure_stream(GstRTSPExtension * ext,GstCaps * caps)130 gst_rtsp_wms_configure_stream (GstRTSPExtension * ext, GstCaps * caps)
131 {
132   GstRTSPWMS *ctx;
133   GstStructure *s;
134   const gchar *encoding;
135 
136   ctx = (GstRTSPWMS *) ext;
137   s = gst_caps_get_structure (caps, 0);
138   encoding = gst_structure_get_string (s, "encoding-name");
139 
140   if (!encoding)
141     return TRUE;
142 
143   GST_DEBUG_OBJECT (ctx, "%" GST_PTR_FORMAT " encoding-name: %s", caps,
144       encoding);
145 
146   /* rtx streams do not need to be configured */
147   if (!strcmp (encoding, "X-WMS-RTX"))
148     return FALSE;
149 
150   return TRUE;
151 }
152 
153 static GstRTSPResult
gst_rtsp_wms_receive_request(GstRTSPExtension * ext,GstRTSPMessage * request)154 gst_rtsp_wms_receive_request (GstRTSPExtension * ext, GstRTSPMessage * request)
155 {
156   GstRTSPWMS *ctx;
157   GstRTSPResult res = GST_RTSP_ENOTIMPL;
158   GstRTSPMessage response = { 0 };
159 
160   ctx = (GstRTSPWMS *) ext;
161 
162   GST_DEBUG_OBJECT (ext, "before send");
163 
164   switch (request->type_data.request.method) {
165     case GST_RTSP_SET_PARAMETER:
166     {
167       gchar *content_type = NULL;
168 
169       gst_rtsp_message_get_header (request, GST_RTSP_HDR_CONTENT_TYPE,
170           &content_type, 0);
171 
172       if (content_type && !g_ascii_strcasecmp (content_type, EXTENSION_CMD)) {
173         /* parse the command */
174 
175         /* default implementation, send OK */
176         res = gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
177             request);
178         if (res < 0)
179           goto send_error;
180 
181         GST_DEBUG_OBJECT (ctx, "replying with OK");
182 
183         /* send reply */
184         if ((res = gst_rtsp_extension_send (ext, request, &response)) < 0)
185           goto send_error;
186 
187         res = GST_RTSP_EEOF;
188       }
189       break;
190     }
191     default:
192       break;
193   }
194   return res;
195 
196 send_error:
197   {
198     return res;
199   }
200 }
201 
202 static void gst_rtsp_wms_extension_init (gpointer g_iface, gpointer iface_data);
203 
204 G_DEFINE_TYPE_WITH_CODE (GstRTSPWMS, gst_rtsp_wms, GST_TYPE_ELEMENT,
205     G_IMPLEMENT_INTERFACE (GST_TYPE_RTSP_EXTENSION,
206         gst_rtsp_wms_extension_init));
207 
208 static void
gst_rtsp_wms_class_init(GstRTSPWMSClass * g_class)209 gst_rtsp_wms_class_init (GstRTSPWMSClass * g_class)
210 {
211   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
212 
213   GST_DEBUG_CATEGORY_INIT (rtspwms_debug, "rtspwms", 0, "WMS RTSP extension");
214 
215   gst_element_class_set_static_metadata (element_class, "WMS RTSP Extension",
216       "Network/Extension/Protocol",
217       "Extends RTSP so that it can handle WMS setup",
218       "Wim Taymans <wim.taymans@gmail.com>");
219 }
220 
221 static void
gst_rtsp_wms_init(GstRTSPWMS * rtspwms)222 gst_rtsp_wms_init (GstRTSPWMS * rtspwms)
223 {
224 }
225 
226 static void
gst_rtsp_wms_extension_init(gpointer g_iface,gpointer iface_data)227 gst_rtsp_wms_extension_init (gpointer g_iface, gpointer iface_data)
228 {
229   GstRTSPExtensionInterface *iface = (GstRTSPExtensionInterface *) g_iface;
230 
231   iface->parse_sdp = gst_rtsp_wms_parse_sdp;
232   iface->before_send = gst_rtsp_wms_before_send;
233   iface->after_send = gst_rtsp_wms_after_send;
234   iface->configure_stream = gst_rtsp_wms_configure_stream;
235   iface->receive_request = gst_rtsp_wms_receive_request;
236 }
237