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