• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  *
3  * Copyright (C) 2019 Collabora Ltd.
4  *   Author: Stéphane Cerveau <scerveau@collabora.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
19  *
20  */
21 #include "gstmpdrootnode.h"
22 #include "gstmpdparser.h"
23 
24 G_DEFINE_TYPE (GstMPDRootNode, gst_mpd_root_node, GST_TYPE_MPD_NODE);
25 
26 enum
27 {
28   PROP_MPD_ROOT_0,
29   PROP_MPD_ROOT_DEFAULT_NAMESPACE,
30   PROP_MPD_ROOT_NAMESPACE_XSI,
31   PROP_MPD_ROOT_NAMESPACE_EXT,
32   PROP_MPD_ROOT_SCHEMA_LOCATION,
33   PROP_MPD_ROOT_ID,
34   PROP_MPD_ROOT_PROFILES,
35   PROP_MPD_ROOT_TYPE,
36   PROP_MPD_ROOT_PUBLISH_TIME,
37   PROP_MPD_ROOT_AVAILABILTY_START_TIME,
38   PROP_MPD_ROOT_AVAILABILTY_END_TIME,
39   PROP_MPD_ROOT_MEDIA_PRESENTATION_DURATION,
40   PROP_MPD_ROOT_MINIMUM_UPDATE_PERIOD,
41   PROP_MPD_ROOT_MIN_BUFFER_TIME,
42   PROP_MPD_ROOT_TIMESHIFT_BUFFER_DEPTH,
43   PROP_MPD_ROOT_SUGGESTED_PRESENTATION_DELAY,
44   PROP_MPD_ROOT_MAX_SEGMENT_DURATION,
45   PROP_MPD_ROOT_MAX_SUBSEGMENT_DURATION,
46 };
47 
48 /* GObject VMethods */
49 
50 static void
gst_mpd_root_node_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)51 gst_mpd_root_node_set_property (GObject * object, guint prop_id,
52     const GValue * value, GParamSpec * pspec)
53 {
54   GstMPDRootNode *self = GST_MPD_ROOT_NODE (object);
55   switch (prop_id) {
56     case PROP_MPD_ROOT_DEFAULT_NAMESPACE:
57       g_free (self->default_namespace);
58       self->default_namespace = g_value_dup_string (value);
59       break;
60     case PROP_MPD_ROOT_NAMESPACE_XSI:
61       g_free (self->namespace_xsi);
62       self->namespace_xsi = g_value_dup_string (value);
63       break;
64     case PROP_MPD_ROOT_NAMESPACE_EXT:
65       g_free (self->namespace_ext);
66       self->namespace_ext = g_value_dup_string (value);
67       break;
68     case PROP_MPD_ROOT_SCHEMA_LOCATION:
69       g_free (self->schemaLocation);
70       self->schemaLocation = g_value_dup_string (value);
71       break;
72     case PROP_MPD_ROOT_ID:
73       g_free (self->id);
74       self->id = g_value_dup_string (value);
75       break;
76     case PROP_MPD_ROOT_PROFILES:
77       g_free (self->profiles);
78       self->profiles = g_value_dup_string (value);
79       break;
80     case PROP_MPD_ROOT_TYPE:
81       self->type = (GstMPDFileType) g_value_get_int (value);
82       break;
83     case PROP_MPD_ROOT_AVAILABILTY_START_TIME:
84       if (self->availabilityStartTime)
85         gst_date_time_unref (self->availabilityStartTime);
86       self->availabilityStartTime = g_value_dup_boxed (value);
87       break;
88     case PROP_MPD_ROOT_AVAILABILTY_END_TIME:
89       if (self->availabilityEndTime)
90         gst_date_time_unref (self->availabilityEndTime);
91       self->availabilityEndTime = g_value_dup_boxed (value);
92       break;
93     case PROP_MPD_ROOT_PUBLISH_TIME:
94       if (self->publishTime)
95         gst_date_time_unref (self->publishTime);
96       self->publishTime = g_value_dup_boxed (value);
97       break;
98     case PROP_MPD_ROOT_MEDIA_PRESENTATION_DURATION:
99       self->mediaPresentationDuration = g_value_get_uint64 (value);
100       break;
101     case PROP_MPD_ROOT_MINIMUM_UPDATE_PERIOD:
102       self->minimumUpdatePeriod = g_value_get_uint64 (value);
103       break;
104     case PROP_MPD_ROOT_MIN_BUFFER_TIME:
105       self->minBufferTime = g_value_get_uint64 (value);
106       break;
107     default:
108       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
109       break;
110   }
111 }
112 
113 static void
gst_mpd_root_node_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)114 gst_mpd_root_node_get_property (GObject * object, guint prop_id,
115     GValue * value, GParamSpec * pspec)
116 {
117   GstMPDRootNode *self = GST_MPD_ROOT_NODE (object);
118   switch (prop_id) {
119     case PROP_MPD_ROOT_DEFAULT_NAMESPACE:
120       g_value_set_string (value, self->default_namespace);
121       break;
122     case PROP_MPD_ROOT_NAMESPACE_XSI:
123       g_value_set_string (value, self->namespace_xsi);
124       break;
125     case PROP_MPD_ROOT_NAMESPACE_EXT:
126       g_value_set_string (value, self->namespace_ext);
127       break;
128     case PROP_MPD_ROOT_SCHEMA_LOCATION:
129       g_value_set_string (value, self->schemaLocation);
130       break;
131     case PROP_MPD_ROOT_ID:
132       g_value_set_string (value, self->id);
133       break;
134     case PROP_MPD_ROOT_PROFILES:
135       g_value_set_string (value, self->profiles);
136       break;
137     case PROP_MPD_ROOT_TYPE:
138       g_value_set_int (value, self->type);
139       break;
140     case PROP_MPD_ROOT_AVAILABILTY_START_TIME:
141       g_value_set_boxed (value, self->availabilityStartTime);
142       break;
143     case PROP_MPD_ROOT_AVAILABILTY_END_TIME:
144       g_value_set_boxed (value, self->availabilityEndTime);
145       break;
146     case PROP_MPD_ROOT_PUBLISH_TIME:
147       g_value_set_boxed (value, self->publishTime);
148       break;
149     case PROP_MPD_ROOT_MEDIA_PRESENTATION_DURATION:
150       g_value_set_uint64 (value, self->mediaPresentationDuration);
151       break;
152     case PROP_MPD_ROOT_MINIMUM_UPDATE_PERIOD:
153       g_value_set_uint64 (value, self->minimumUpdatePeriod);
154       break;
155     case PROP_MPD_ROOT_MIN_BUFFER_TIME:
156       g_value_set_uint64 (value, self->minBufferTime);
157       break;
158     default:
159       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160       break;
161   }
162 }
163 
164 static void
gst_mpd_root_node_finalize(GObject * object)165 gst_mpd_root_node_finalize (GObject * object)
166 {
167   GstMPDRootNode *self = GST_MPD_ROOT_NODE (object);
168 
169   g_free (self->default_namespace);
170   g_free (self->namespace_xsi);
171   g_free (self->namespace_ext);
172   g_free (self->schemaLocation);
173   g_free (self->id);
174   g_free (self->profiles);
175 
176   if (self->availabilityStartTime)
177     gst_date_time_unref (self->availabilityStartTime);
178   if (self->availabilityEndTime)
179     gst_date_time_unref (self->availabilityEndTime);
180   if (self->publishTime)
181     gst_date_time_unref (self->publishTime);
182 
183   g_list_free_full (self->ProgramInfos,
184       (GDestroyNotify) gst_mpd_program_information_node_free);
185   g_list_free_full (self->BaseURLs, (GDestroyNotify) gst_mpd_baseurl_node_free);
186   g_list_free_full (self->Locations,
187       (GDestroyNotify) gst_mpd_location_node_free);
188   g_list_free_full (self->Periods, (GDestroyNotify) gst_mpd_period_node_free);
189   g_list_free_full (self->Metrics, (GDestroyNotify) gst_mpd_metrics_node_free);
190   g_list_free_full (self->UTCTimings,
191       (GDestroyNotify) gst_mpd_utctiming_node_free);
192 
193 
194   G_OBJECT_CLASS (gst_mpd_root_node_parent_class)->finalize (object);
195 }
196 
197 /* Base class */
198 
199 static xmlNodePtr
gst_mpd_root_get_xml_node(GstMPDNode * node)200 gst_mpd_root_get_xml_node (GstMPDNode * node)
201 {
202   xmlNodePtr root_xml_node;
203   GstMPDRootNode *self = GST_MPD_ROOT_NODE (node);
204 
205   root_xml_node = xmlNewNode (NULL, (xmlChar *) "MPD");
206 
207   gst_xml_helper_set_prop_string (root_xml_node, "xmlns",
208       self->default_namespace);
209   gst_xml_helper_set_prop_string (root_xml_node, "profiles", self->profiles);
210   gst_xml_helper_set_prop_string (root_xml_node, "schemaLocation",
211       self->schemaLocation);
212   gst_xml_helper_set_prop_string (root_xml_node, "xmlns:xsi",
213       self->namespace_xsi);
214   gst_xml_helper_set_prop_string (root_xml_node, "xmlns:ext",
215       self->namespace_ext);
216   gst_xml_helper_set_prop_string (root_xml_node, "id", self->id);
217 
218   if (self->type == GST_MPD_FILE_TYPE_STATIC)
219     gst_xml_helper_set_prop_string (root_xml_node, "type", (gchar *) "static");
220   else
221     gst_xml_helper_set_prop_string (root_xml_node, "type", (gchar *) "dynamic");
222 
223 
224   gst_xml_helper_set_prop_date_time (root_xml_node, "availabilityStartTime",
225       self->availabilityStartTime);
226 
227   gst_xml_helper_set_prop_date_time (root_xml_node, "availabilityEndTime",
228       self->availabilityEndTime);
229   gst_xml_helper_set_prop_date_time (root_xml_node, "publishTime",
230       self->publishTime);
231 
232   if (self->mediaPresentationDuration)
233     gst_xml_helper_set_prop_duration (root_xml_node,
234         "mediaPresentationDuration", self->mediaPresentationDuration);
235   if (self->minimumUpdatePeriod)
236     gst_xml_helper_set_prop_duration (root_xml_node, "minimumUpdatePeriod",
237         self->minimumUpdatePeriod);
238   if (self->minBufferTime)
239     gst_xml_helper_set_prop_duration (root_xml_node, "minBufferTime",
240         self->minBufferTime);
241   if (self->timeShiftBufferDepth)
242     gst_xml_helper_set_prop_duration (root_xml_node, "timeShiftBufferDepth",
243         self->timeShiftBufferDepth);
244   if (self->suggestedPresentationDelay)
245     gst_xml_helper_set_prop_duration (root_xml_node,
246         "suggestedPresentationDelay", self->suggestedPresentationDelay);
247   if (self->maxSegmentDuration)
248     gst_xml_helper_set_prop_duration (root_xml_node, "maxSegmentDuration",
249         self->maxSegmentDuration);
250   if (self->maxSubsegmentDuration)
251     gst_xml_helper_set_prop_duration (root_xml_node, "maxSubsegmentDuration",
252         self->maxSubsegmentDuration);
253 
254   g_list_foreach (self->BaseURLs, gst_mpd_node_get_list_item, root_xml_node);
255   g_list_foreach (self->Locations, gst_mpd_node_get_list_item, root_xml_node);
256   g_list_foreach (self->ProgramInfos, gst_mpd_node_get_list_item,
257       root_xml_node);
258   g_list_foreach (self->Periods, gst_mpd_node_get_list_item, root_xml_node);
259   g_list_foreach (self->Metrics, gst_mpd_node_get_list_item, root_xml_node);
260   g_list_foreach (self->UTCTimings, gst_mpd_node_get_list_item, root_xml_node);
261 
262   return root_xml_node;
263 }
264 
265 static gboolean
gst_mpd_root_get_xml_buffer(GstMPDNode * node,gchar ** doc_content,gint * doc_size)266 gst_mpd_root_get_xml_buffer (GstMPDNode * node, gchar ** doc_content,
267     gint * doc_size)
268 {
269   xmlDocPtr doc;
270   xmlNodePtr root_xml_node;
271   xmlChar *xmlbody;
272 
273   doc = xmlNewDoc ((xmlChar *) "1.0");
274   root_xml_node = gst_mpd_root_get_xml_node (node);
275   xmlDocSetRootElement (doc, root_xml_node);
276 
277   xmlDocDumpMemory (doc, &xmlbody, doc_size);
278   *doc_content = g_strndup ((gchar *) xmlbody, *doc_size);
279   xmlFree (xmlbody);
280 
281   xmlFreeDoc (doc);
282   return TRUE;
283 }
284 
285 static void
gst_mpd_root_node_class_init(GstMPDRootNodeClass * klass)286 gst_mpd_root_node_class_init (GstMPDRootNodeClass * klass)
287 {
288   GObjectClass *object_class;
289   GstMPDNodeClass *m_klass;
290 
291 
292   object_class = G_OBJECT_CLASS (klass);
293   m_klass = GST_MPD_NODE_CLASS (klass);
294 
295   object_class->finalize = gst_mpd_root_node_finalize;
296   object_class->set_property = gst_mpd_root_node_set_property;
297   object_class->get_property = gst_mpd_root_node_get_property;
298 
299   m_klass->get_xml_buffer = gst_mpd_root_get_xml_buffer;
300   m_klass->get_xml_node = gst_mpd_root_get_xml_node;
301 
302   g_object_class_install_property (object_class,
303       PROP_MPD_ROOT_DEFAULT_NAMESPACE, g_param_spec_string ("default-namespace",
304           "default namespace", "default namespace", NULL,
305           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
306   g_object_class_install_property (object_class, PROP_MPD_ROOT_NAMESPACE_XSI,
307       g_param_spec_string ("namespace-xsi", "namespace xsi", "namespace xsi",
308           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
309   g_object_class_install_property (object_class, PROP_MPD_ROOT_NAMESPACE_EXT,
310       g_param_spec_string ("namespace-ext", "namespace ext", "namespace ext",
311           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
312   g_object_class_install_property (object_class, PROP_MPD_ROOT_SCHEMA_LOCATION,
313       g_param_spec_string ("schema-location", "schema location",
314           "schema location for period", NULL,
315           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
316   g_object_class_install_property (object_class, PROP_MPD_ROOT_ID,
317       g_param_spec_string ("id", "id", "unique id for period", NULL,
318           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
319   g_object_class_install_property (object_class, PROP_MPD_ROOT_PROFILES,
320       g_param_spec_string ("profiles", "profiles", "profiles", NULL,
321           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
322   g_object_class_install_property (object_class, PROP_MPD_ROOT_TYPE,
323       g_param_spec_int ("type", "MPD type",
324           "MPD type",
325           GST_MPD_FILE_TYPE_STATIC, GST_MPD_FILE_TYPE_DYNAMIC,
326           GST_MPD_FILE_TYPE_STATIC,
327           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
328   g_object_class_install_property (object_class,
329       PROP_MPD_ROOT_AVAILABILTY_START_TIME,
330       g_param_spec_boxed ("availability-start-time", "Availability start time",
331           "MPD availability start time", GST_TYPE_DATE_TIME,
332           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
333   g_object_class_install_property (object_class,
334       PROP_MPD_ROOT_AVAILABILTY_END_TIME,
335       g_param_spec_boxed ("availability-end-time", "Availability end time",
336           "MPD availability end time", GST_TYPE_DATE_TIME,
337           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
338   g_object_class_install_property (object_class,
339       PROP_MPD_ROOT_PUBLISH_TIME,
340       g_param_spec_boxed ("publish-time", "publish time",
341           "MPD publish time", GST_TYPE_DATE_TIME,
342           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
343   g_object_class_install_property (object_class,
344       PROP_MPD_ROOT_MEDIA_PRESENTATION_DURATION,
345       g_param_spec_uint64 ("media-presentation-duration",
346           "media presentation duration", "media presentation duration", 0,
347           G_MAXUINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
348   g_object_class_install_property (object_class,
349       PROP_MPD_ROOT_MINIMUM_UPDATE_PERIOD,
350       g_param_spec_uint64 ("minimum-update-period",
351           "minimum update period", "minimum update period", 0,
352           G_MAXUINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
353   g_object_class_install_property (object_class,
354       PROP_MPD_ROOT_MIN_BUFFER_TIME,
355       g_param_spec_uint64 ("min-buffer-time", "mininim buffer time",
356           "mininim buffer time", 0,
357           G_MAXUINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
358 }
359 
360 static void
gst_mpd_root_node_init(GstMPDRootNode * self)361 gst_mpd_root_node_init (GstMPDRootNode * self)
362 {
363   self->default_namespace = NULL;
364   self->namespace_xsi = NULL;
365   self->namespace_ext = NULL;
366   self->schemaLocation = NULL;
367   self->id = NULL;
368   self->profiles = NULL;
369   self->type = GST_MPD_FILE_TYPE_STATIC;
370   self->availabilityStartTime = NULL;
371   self->availabilityEndTime = NULL;
372   self->publishTime = NULL;
373   self->mediaPresentationDuration = 0;  /* [ms] */
374   self->minimumUpdatePeriod = 0;        /* [ms] */
375   self->minBufferTime = 2000;   /* [ms] */
376   self->timeShiftBufferDepth = 0;       /* [ms] */
377   self->suggestedPresentationDelay = 0; /* [ms] */
378   self->maxSegmentDuration = 0; /* [ms] */
379   self->maxSubsegmentDuration = 0;      /* [ms] */
380   /* list of BaseURL nodes */
381   self->BaseURLs = NULL;
382   /* list of Location nodes */
383   self->Locations = NULL;
384   /* List of ProgramInformation nodes */
385   self->ProgramInfos = NULL;
386   /* list of Periods nodes */
387   self->Periods = NULL;
388   /* list of Metrics nodes */
389   self->Metrics = NULL;
390   /* list of GstUTCTimingNode nodes */
391   self->UTCTimings = NULL;
392 }
393 
394 GstMPDRootNode *
gst_mpd_root_node_new(void)395 gst_mpd_root_node_new (void)
396 {
397   return g_object_new (GST_TYPE_MPD_ROOT_NODE, NULL);
398 }
399 
400 void
gst_mpd_root_node_free(GstMPDRootNode * self)401 gst_mpd_root_node_free (GstMPDRootNode * self)
402 {
403   if (self)
404     gst_object_unref (self);
405 }
406