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
22 #include "gstmpdhelper.h"
23 #include "gstmpdbaseurlnode.h"
24
25 gboolean
gst_mpd_helper_get_mpd_type(xmlNode * a_node,const gchar * property_name,GstMPDFileType * property_value)26 gst_mpd_helper_get_mpd_type (xmlNode * a_node,
27 const gchar * property_name, GstMPDFileType * property_value)
28 {
29 xmlChar *prop_string;
30 gboolean exists = FALSE;
31
32 *property_value = GST_MPD_FILE_TYPE_STATIC; /* default */
33 prop_string = xmlGetProp (a_node, (const xmlChar *) property_name);
34 if (prop_string) {
35 if (xmlStrcmp (prop_string, (xmlChar *) "OnDemand") == 0
36 || xmlStrcmp (prop_string, (xmlChar *) "static") == 0) {
37 exists = TRUE;
38 *property_value = GST_MPD_FILE_TYPE_STATIC;
39 GST_LOG (" - %s: static", property_name);
40 } else if (xmlStrcmp (prop_string, (xmlChar *) "Live") == 0
41 || xmlStrcmp (prop_string, (xmlChar *) "dynamic") == 0) {
42 exists = TRUE;
43 *property_value = GST_MPD_FILE_TYPE_DYNAMIC;
44 GST_LOG (" - %s: dynamic", property_name);
45 } else {
46 GST_WARNING ("failed to parse MPD type property %s from xml string %s",
47 property_name, prop_string);
48 }
49 xmlFree (prop_string);
50 }
51
52 return exists;
53 }
54
55 gboolean
gst_mpd_helper_get_SAP_type(xmlNode * a_node,const gchar * property_name,GstMPDSAPType * property_value)56 gst_mpd_helper_get_SAP_type (xmlNode * a_node,
57 const gchar * property_name, GstMPDSAPType * property_value)
58 {
59 xmlChar *prop_string;
60 guint prop_SAP_type = 0;
61 gboolean exists = FALSE;
62
63 prop_string = xmlGetProp (a_node, (const xmlChar *) property_name);
64 if (prop_string) {
65 if (sscanf ((gchar *) prop_string, "%u", &prop_SAP_type) == 1
66 && prop_SAP_type <= 6) {
67 exists = TRUE;
68 *property_value = (GstMPDSAPType) prop_SAP_type;
69 GST_LOG (" - %s: %u", property_name, prop_SAP_type);
70 } else {
71 GST_WARNING
72 ("failed to parse unsigned integer property %s from xml string %s",
73 property_name, prop_string);
74 }
75 xmlFree (prop_string);
76 }
77
78 return exists;
79 }
80
81 const gchar *
gst_mpd_helper_get_audio_codec_from_mime(GstCaps * caps)82 gst_mpd_helper_get_audio_codec_from_mime (GstCaps * caps)
83 {
84 GstStructure *s;
85 const gchar *name = "";
86 const gchar *codec_name = NULL;
87
88 if (!caps)
89 return NULL;
90 s = gst_caps_get_structure (caps, 0);
91 if (!s)
92 goto done;
93 name = gst_structure_get_name (s);
94 if (!g_strcmp0 (name, "audio/mpeg")) {
95 gint mpeg_version;
96 if (gst_structure_get_int (s, "mpegversion", &mpeg_version)) {
97 if (mpeg_version == 4)
98 return "mp4a";
99 }
100
101 } else {
102 GST_DEBUG ("No codecs for this caps name %s", name);
103 }
104
105 done:
106 return codec_name;
107 }
108
109 const gchar *
gst_mpd_helper_get_video_codec_from_mime(GstCaps * caps)110 gst_mpd_helper_get_video_codec_from_mime (GstCaps * caps)
111 {
112 GstStructure *s;
113 const gchar *name = "";
114 const gchar *codec_name = NULL;
115
116 if (!caps)
117 return NULL;
118
119 s = gst_caps_get_structure (caps, 0);
120 if (!s)
121 goto done;
122 name = gst_structure_get_name (s);
123 if (!g_strcmp0 (name, "video/x-h264")) {
124 return "avc1";
125 } else if (!g_strcmp0 (name, "video/x-h265")) {
126 return "hvc1";
127 } else {
128 GST_DEBUG ("No codecs for this caps name %s", name);
129 }
130
131 done:
132 return codec_name;
133 }
134
135
136 const gchar *
gst_mpd_helper_mimetype_to_caps(const gchar * mimeType)137 gst_mpd_helper_mimetype_to_caps (const gchar * mimeType)
138 {
139 if (mimeType == NULL)
140 return NULL;
141 if (strcmp (mimeType, "video/mp2t") == 0) {
142 return "video/mpegts, systemstream=(bool) true";
143 } else if (strcmp (mimeType, "video/mp4") == 0) {
144 return "video/quicktime";
145 } else if (strcmp (mimeType, "audio/mp4") == 0) {
146 return "audio/x-m4a";
147 } else if (strcmp (mimeType, "text/vtt") == 0) {
148 return "application/x-subtitle-vtt";
149 } else
150 return mimeType;
151 }
152
153 /*
154 * Combine a base url with the current stream base url from the list of
155 * baseURLs. Takes ownership of base and returns a new base.
156 */
157 GstUri *
gst_mpd_helper_combine_urls(GstUri * base,GList * list,gchar ** query,guint idx)158 gst_mpd_helper_combine_urls (GstUri * base, GList * list, gchar ** query,
159 guint idx)
160 {
161 GstMPDBaseURLNode *baseURL;
162 GstUri *ret = base;
163
164 if (list != NULL) {
165 baseURL = g_list_nth_data (list, idx);
166 if (!baseURL) {
167 baseURL = list->data;
168 }
169
170 ret = gst_uri_from_string_with_base (base, baseURL->baseURL);
171 gst_uri_unref (base);
172
173 if (ret && query) {
174 g_free (*query);
175 *query = gst_uri_get_query_string (ret);
176 if (*query) {
177 ret = gst_uri_make_writable (ret);
178 gst_uri_set_query_table (ret, NULL);
179 }
180 }
181 }
182
183 return ret;
184 }
185
186 /* comparison functions */
187 int
gst_mpd_helper_strncmp_ext(const char * s1,const char * s2)188 gst_mpd_helper_strncmp_ext (const char *s1, const char *s2)
189 {
190 if (s1 == NULL && s2 == NULL)
191 return 0;
192 if (s1 == NULL && s2 != NULL)
193 return 1;
194 if (s2 == NULL && s1 != NULL)
195 return 1;
196 return strncmp (s1, s2, strlen (s2));
197 }
198