tizen 2.3.1 release
[framework/multimedia/gst-plugins-ext0.10.git] / dashdemux / src / gstmpdparser.h
1 /*
2  * DASH MPD parsing library
3  *
4  * gstmpdparser.h
5  *
6  * Copyright (C) 2012 STMicroelectronics
7  *
8  * Authors:
9  *   Gianluca Gennari <gennarone@gmail.com>
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library (COPYING); if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  */
26
27 #ifndef __GST_MPDPARSER_H__
28 #define __GST_MPDPARSER_H__
29
30 #include <gst/gst.h>
31
32 G_BEGIN_DECLS
33
34 typedef struct _GstMpdClient              GstMpdClient;
35 typedef struct _GstActiveStream           GstActiveStream;
36 typedef struct _GstStreamPeriod           GstStreamPeriod;
37 typedef struct _GstMediaSegment           GstMediaSegment;
38 typedef struct _GstMPDNode                GstMPDNode;
39 typedef struct _GstPeriodNode             GstPeriodNode;
40 typedef struct _GstRepresentationBaseType GstRepresentationBaseType;
41 typedef struct _GstDescriptorType         GstDescriptorType;
42 typedef struct _GstContentComponentNode   GstContentComponentNode;
43 typedef struct _GstAdaptationSetNode      GstAdaptationSetNode;
44 typedef struct _GstRepresentationNode     GstRepresentationNode;
45 typedef struct _GstSubRepresentationNode  GstSubRepresentationNode;
46 typedef struct _GstSegmentListNode        GstSegmentListNode;
47 typedef struct _GstSegmentTemplateNode    GstSegmentTemplateNode;
48 typedef struct _GstSegmentURLNode         GstSegmentURLNode;
49 typedef struct _GstBaseURL                GstBaseURL;
50 typedef struct _GstRange                  GstRange;
51 typedef struct _GstRatio                  GstRatio;
52 typedef struct _GstFrameRate              GstFrameRate;
53 typedef struct _GstConditionalUintType    GstConditionalUintType;
54 typedef struct _GstSubsetNode             GstSubsetNode;
55 typedef struct _GstProgramInformationNode GstProgramInformationNode;
56 typedef struct _GstMetricsRangeNode       GstMetricsRangeNode;
57 typedef struct _GstMetricsNode            GstMetricsNode;
58 typedef struct _GstSNode                  GstSNode;
59 typedef struct _GstSegmentTimelineNode    GstSegmentTimelineNode;
60 typedef struct _GstSegmentBaseType        GstSegmentBaseType;
61 typedef struct _GstURLType                GstURLType;
62 typedef struct _GstMultSegmentBaseType    GstMultSegmentBaseType;
63
64 #define GST_MPD_CLIENT_LOCK(c) g_mutex_lock (c->lock);
65 #define GST_MPD_CLIENT_UNLOCK(c) g_mutex_unlock (c->lock);
66
67 typedef enum
68 {
69   GST_STREAM_VIDEO,           /* video stream (the main one) */
70   GST_STREAM_AUDIO,           /* audio stream (optional) */
71   GST_STREAM_APPLICATION      /* application stream (optional): for timed text/subtitles */
72 } GstStreamMimeType;
73
74 typedef enum
75 {
76   GST_MPD_FILE_TYPE_STATIC,
77   GST_MPD_FILE_TYPE_DYNAMIC
78 } GstMPDFileType;
79
80 typedef enum
81 {
82   GST_SAP_TYPE_0 = 0,
83   GST_SAP_TYPE_1,
84   GST_SAP_TYPE_2,
85   GST_SAP_TYPE_3,
86   GST_SAP_TYPE_4,
87   GST_SAP_TYPE_5,
88   GST_SAP_TYPE_6
89 } GstSAPType;
90
91 struct _GstBaseURL
92 {
93   gchar *baseURL;
94   gchar *serviceLocation;
95   gchar *byteRange;
96 };
97
98 struct _GstRange
99 {
100   guint64 first_byte_pos;
101   guint64 last_byte_pos;
102 };
103
104 struct _GstRatio
105 {
106   guint num;
107   guint den;
108 };
109
110 struct _GstFrameRate
111 {
112   guint num;
113   guint den;
114 };
115
116 struct _GstConditionalUintType
117 {
118   gboolean flag;
119   guint value;
120 };
121
122 struct _GstSNode
123 {
124   guint t;
125   guint d;
126   guint r;
127 };
128
129 struct _GstSegmentTimelineNode
130 {
131   /* list of S nodes */
132   GList *S;
133 };
134
135 struct _GstURLType
136 {
137   gchar *sourceURL;
138   GstRange *range;
139 };
140
141 struct _GstSegmentBaseType
142 {
143   guint timescale;
144   guint presentationTimeOffset;
145   gchar *indexRange;
146   gboolean indexRangeExact;
147   /* Initialization node */
148   GstURLType *Initialization;
149   /* RepresentationIndex node */
150   GstURLType *RepresentationIndex;
151 };
152
153 struct _GstMultSegmentBaseType
154 {
155   guint duration;                  /* in seconds */
156   guint startNumber;
157   /* SegmentBaseType extension */
158   GstSegmentBaseType *SegBaseType;
159   /* SegmentTimeline node */
160   GstSegmentTimelineNode *SegmentTimeline;
161   /* BitstreamSwitching node */
162   GstURLType *BitstreamSwitching;
163 };
164
165 struct _GstSegmentListNode
166 {
167   /* extension */
168   GstMultSegmentBaseType *MultSegBaseType;
169   /* list of SegmentURL nodes */
170   GList *SegmentURL;
171 };
172
173 struct _GstSegmentTemplateNode
174 {
175   /* extension */
176   GstMultSegmentBaseType *MultSegBaseType;
177   gchar *media;
178   gchar *index;
179   gchar *initialization;
180   gchar *bitstreamSwitching;
181 };
182
183 struct _GstSegmentURLNode
184 {
185   gchar *media;
186   GstRange *mediaRange;
187   gchar *index;
188   GstRange *indexRange;
189 };
190
191 struct _GstRepresentationBaseType
192 {
193   gchar *profiles;
194   guint width;
195   guint height;
196   guint bandwidth;
197   GstRatio *sar;
198   GstFrameRate *frameRate;
199   gchar *audioSamplingRate;
200   gchar *mimeType;
201   gchar *segmentProfiles;
202   gchar *codecs;
203   gdouble maximumSAPPeriod;
204   GstSAPType startWithSAP;
205   gdouble maxPlayoutRate;
206   gboolean codingDependency;
207   gchar *scanType;
208   /* list of FramePacking DescriptorType nodes */
209   GList *FramePacking;
210   /* list of AudioChannelConfiguration DescriptorType nodes */
211   GList *AudioChannelConfiguration;
212   /* list of ContentProtection DescriptorType nodes */
213   GList *ContentProtection;
214 };
215
216 struct _GstSubRepresentationNode
217 {
218   /* RepresentationBase extension */
219   GstRepresentationBaseType *RepresentationBase;
220   guint level;
221   guint *dependencyLevel;            /* UIntVectorType */
222   guint size;                        /* size of "dependencyLevel" array */
223   guint64 bandwidth;
224   gchar **contentComponent;          /* StringVectorType */
225 };
226
227 struct _GstRepresentationNode
228 {
229   gchar *id;
230   guint bandwidth;
231   guint qualityRanking;
232   gchar **dependencyId;              /* StringVectorType */
233   gchar **mediaStreamStructureId;    /* StringVectorType */
234   /* RepresentationBase extension */
235   GstRepresentationBaseType *RepresentationBase;
236   /* list of BaseURL nodes */
237   GList *BaseURLs;
238   /* list of SubRepresentation nodes */
239   GList *SubRepresentations;
240   /* SegmentBase node */
241   GstSegmentBaseType *SegmentBase;
242   /* SegmentTemplate node */
243   GstSegmentTemplateNode *SegmentTemplate;
244   /* SegmentList node */
245   GstSegmentListNode *SegmentList;
246 };
247
248 struct _GstDescriptorType
249 {
250   gchar *schemeIdUri;
251   gchar *value;
252   /* For adding ContentProtection to caps , get string of <mspr:pro> */
253   gchar *msprPro;
254 };
255
256 struct _GstContentComponentNode
257 {
258   guint id;
259   gchar *lang;                      /* LangVectorType RFC 5646 */
260   gchar *contentType;
261   GstRatio *par;
262   /* list of Accessibility DescriptorType nodes */
263   GList *Accessibility;
264   /* list of Role DescriptorType nodes */
265   GList *Role;
266   /* list of Rating DescriptorType nodes */
267   GList *Rating;
268   /* list of Viewpoint DescriptorType nodes */
269   GList *Viewpoint;
270 };
271
272 struct _GstAdaptationSetNode
273 {
274   guint id;
275   guint group;
276   gchar *lang;                      /* LangVectorType RFC 5646 */
277   gchar *contentType;
278   GstRatio *par;
279   guint minBandwidth;
280   guint maxBandwidth;
281   guint minWidth;
282   guint maxWidth;
283   guint minHeight;
284   guint maxHeight;
285   GstFrameRate *minFrameRate;
286   GstFrameRate *maxFrameRate;
287   GstConditionalUintType *segmentAlignment;
288   GstConditionalUintType *subsegmentAlignment;
289   GstSAPType subsegmentStartsWithSAP;
290   gboolean bitstreamSwitching;
291   /* list of Accessibility DescriptorType nodes */
292   GList *Accessibility;
293   /* list of Role DescriptorType nodes */
294   GList *Role;
295   /* list of Rating DescriptorType nodes */
296   GList *Rating;
297   /* list of Viewpoint DescriptorType nodes */
298   GList *Viewpoint;
299   /* RepresentationBase extension */
300   GstRepresentationBaseType *RepresentationBase;
301   /* SegmentBase node */
302   GstSegmentBaseType *SegmentBase;
303   /* SegmentList node */
304   GstSegmentListNode *SegmentList;
305   /* SegmentTemplate node */
306   GstSegmentTemplateNode *SegmentTemplate;
307   /* list of BaseURL nodes */
308   GList *BaseURLs;
309   /* list of Representation nodes */
310   GList *Representations;
311   /* list of ContentComponent nodes */
312   GList *ContentComponents;
313 };
314
315 struct _GstSubsetNode
316 {
317   guint *contains;                   /* UIntVectorType */
318   guint size;                        /* size of the "contains" array */
319 };
320
321 struct _GstPeriodNode
322 {
323   gchar *id;
324   gint64 start;                      /* [ms] */
325   gint64 duration;                   /* [ms] */
326   gboolean bitstreamSwitching;
327   /* SegmentBase node */
328   GstSegmentBaseType *SegmentBase;
329   /* SegmentList node */
330   GstSegmentListNode *SegmentList;
331   /* SegmentTemplate node */
332   GstSegmentTemplateNode *SegmentTemplate;
333   /* list of Adaptation Set nodes */
334   GList *AdaptationSets;
335   /* list of Representation nodes */
336   GList *Subsets;
337   /* list of BaseURL nodes */
338   GList *BaseURLs;
339 };
340
341 struct _GstProgramInformationNode
342 {
343   gchar *lang;                      /* LangVectorType RFC 5646 */
344   gchar *moreInformationURL;
345   /* children nodes */
346   gchar *Title;
347   gchar *Source;
348   gchar *Copyright;
349 };
350
351 struct _GstMetricsRangeNode
352 {
353   gint64 starttime;                  /* [ms] */
354   gint64 duration;                   /* [ms] */
355 };
356
357 struct _GstMetricsNode
358 {
359   gchar *metrics;
360   /* list of Metrics Range nodes */
361   GList *MetricsRanges;
362   /* list of Reporting nodes */
363   GList *Reportings;
364 };
365
366 struct _GstMPDNode
367 {
368   gchar *default_namespace;
369   gchar *namespace_xsi;
370   gchar *namespace_ext;
371   gchar *schemaLocation;
372   gchar *id;
373   gchar *profiles;
374   GstMPDFileType type;
375   GstDateTime *availabilityStartTime;
376   GstDateTime *availabilityEndTime;
377   gint64 mediaPresentationDuration;  /* [ms] */
378   gint64 minimumUpdatePeriod;        /* [ms] */
379   gint64 minBufferTime;              /* [ms] */
380   gint64 timeShiftBufferDepth;       /* [ms] */
381   gint64 suggestedPresentationDelay; /* [ms] */
382   gint64 maxSegmentDuration;         /* [ms] */
383   gint64 maxSubsegmentDuration;      /* [ms] */
384   /* list of BaseURL nodes */
385   GList *BaseURLs;
386   /* list of Location nodes */
387   GList *Locations;
388   /* List of ProgramInformation nodes */
389   GList *ProgramInfo;
390   /* list of Periods nodes */
391   GList *Periods;
392   /* list of Metrics nodes */
393   GList *Metrics;
394 };
395
396 /**
397  * GstStreamPeriod:
398  *
399  * Stream period data structure
400  */
401 struct _GstStreamPeriod
402 {
403   GstPeriodNode *period;                      /* Stream period */
404   guint number;                               /* Period number */
405   GstClockTime start;                         /* Period start time */
406   GstClockTime duration;                      /* Period duration */
407 };
408
409 /**
410  * GstMediaSegment:
411  *
412  * Media segment data structure
413  */
414 struct _GstMediaSegment
415 {
416   GstSegmentURLNode *SegmentURL;              /* this is NULL when using a SegmentTemplate */
417   guint number;                               /* segment number */
418   guint start;                                /* segment start time in timescale units */
419   GstClockTime start_time;                    /* segment start time */
420   GstClockTime duration;                      /* segment duration */
421 };
422
423 /**
424  * GstActiveStream:
425  *
426  * Active stream data structure
427  */
428 struct _GstActiveStream
429 {
430   GstStreamMimeType mimeType;                 /* video/audio/application */
431
432   guint mpd_baseURL_idx;                      /* MPD baseURL index */
433   guint period_baseURL_idx;                   /* Period baseURL index */
434   guint adaptset_baseURL_idx;                 /* AdaptationSet baseURL index */
435   guint repr_baseURL_idx;                     /* Representation baseURL index */
436
437   gchar *baseURL;                             /* active baseURL used for last request */
438   guint max_bandwidth;                        /* max bandwidth allowed for this mimeType */
439
440   GstAdaptationSetNode *cur_adapt_set;        /* active adaptation set */
441   gint representation_idx;                    /* index of current representation */
442   GstRepresentationNode *cur_representation;  /* active representation */
443   GstSegmentBaseType *cur_segment_base;       /* active segment base */
444   GstSegmentListNode *cur_segment_list;       /* active segment list */
445   GstSegmentTemplateNode *cur_seg_template;   /* active segment template */
446   guint segment_idx;                          /* index of next sequence chunk */
447   GList *segments;                            /* list of GstMediaSegment */
448 };
449
450 struct _GstMpdClient
451 {
452   GstMPDNode *mpd_node;                       /* active MPD manifest file */
453
454   GList *periods;                             /* list of GstStreamPeriod */
455   guint period_idx;                           /* index of current Period */
456
457   GList *active_streams;                      /* list of GstActiveStream */
458   guint stream_idx;                           /* currently active stream */
459
460   guint download_failed_count;
461   gchar *mpd_uri;                             /* manifest file URI */
462   GMutex *lock;
463 };
464
465 /* Basic initialization/deinitialization functions */
466 GstMpdClient *gst_mpd_client_new ();
467 void gst_active_streams_free (GstMpdClient * client);
468 void gst_mpd_client_free (GstMpdClient * client);
469
470 /* MPD file parsing */
471 gboolean gst_mpd_parse (GstMpdClient *client, const gchar *data, gint size);
472
473 /* Streaming management */
474 gboolean gst_mpd_client_setup_media_presentation (GstMpdClient *client);
475 gboolean gst_mpd_client_setup_streaming (GstMpdClient *client, GstStreamMimeType mimeType, gchar* lang);
476 gboolean gst_mpd_client_setup_representation (GstMpdClient *client, GstActiveStream *stream, GstRepresentationNode *representation);
477 gboolean gst_mpd_client_get_stream_current_position (GstMpdClient * client, guint stream_idx, GstClockTime *ts);
478 GstClockTime gst_mpd_client_get_next_fragment_duration (GstMpdClient * client);
479 GstClockTime gst_mpd_client_get_media_presentation_duration (GstMpdClient *client);
480 gboolean gst_mpd_client_get_next_fragment (GstMpdClient *client, guint indexStream, gboolean *discontinuity, gchar **uri, GstClockTime *duration, GstClockTime *timestamp);
481 gboolean gst_mpd_client_get_last_fragment_timestamp (GstMpdClient * client, guint stream_idx, GstClockTime * ts);
482 gboolean gst_mpd_client_get_next_fragment_timestamp (GstMpdClient * client, guint stream_idx, GstClockTime * ts);
483 gboolean gst_mpd_client_get_next_header (GstMpdClient *client, const gchar **uri, guint stream_idx);
484 gboolean gst_mpd_client_is_live (GstMpdClient * client);
485
486 /* Period selection */
487 gboolean gst_mpd_client_set_period_index (GstMpdClient *client, guint period_idx);
488 guint gst_mpd_client_get_period_index (GstMpdClient *client);
489 const gchar* gst_mpd_client_get_period_id (GstMpdClient * client);
490 gboolean gst_mpd_client_set_period_id (GstMpdClient * client, const gchar * period_id);
491 gboolean gst_mpd_client_has_next_period (GstMpdClient * client);
492
493 /* Representation selection */
494 gint gst_mpdparser_get_rep_idx_with_max_bandwidth (GList *Representations, guint64 max_bandwidth);
495
496 /* URL management */
497 const gchar *gst_mpdparser_get_baseURL (GstMpdClient *client);
498 gboolean gst_mpdparser_get_chunk_by_index (GstMpdClient * client, guint indexStream, guint indexChunk, GstMediaSegment *segment);
499 gboolean gst_mpd_client_set_next_baseURL_for_stream (GstMpdClient * client);
500
501 /* Active stream */
502 guint gst_mpdparser_get_nb_active_stream (GstMpdClient *client);
503 GstActiveStream *gst_mpdparser_get_active_stream_by_index (GstMpdClient *client, guint stream_idx);
504 gint gst_mpd_client_get_video_active_stream_id(GstMpdClient *client);
505 gboolean gst_mpd_client_stream_seek (GstMpdClient * client, gint stream_idx, GstClockTime ts);
506 gboolean gst_mpd_client_seek_to_time (GstMpdClient * client, GDateTime * time);
507 gint gst_mpd_client_check_time_position (GstMpdClient * client, GstActiveStream * stream, GstClockTime ts, gint64 * diff);
508 GstClockTime gst_mpd_client_stream_find_segment(GstMpdClient *client, guint stream_idx, GstClockTime seek_pos, guint *seg_num);
509
510 /* AdaptationSet */
511 guint gst_mpdparser_get_nb_adaptationSet (GstMpdClient *client);
512
513 /* Segment */
514 void gst_mpd_client_set_segment_index_for_all_streams (GstMpdClient * client, guint segment_idx);
515 void gst_mpd_client_set_segment_index (GstActiveStream * stream, guint segment_idx);
516 guint gst_mpd_client_get_segment_index (GstActiveStream * stream);
517
518 /* Get audio/video stream parameters (mimeType, width, height, rate, number of channels) */
519 const gchar *gst_mpd_client_get_stream_mimeType (GstActiveStream * stream);
520 const gboolean gst_mpd_client_get_bitstream_switching_flag (GstActiveStream * stream);
521 gboolean gst_mpd_client_get_max_video_dimensions(GstMpdClient *client, gint *max_width, gint *max_height);
522 guint gst_mpd_client_get_video_stream_width (GstActiveStream * stream);
523 guint gst_mpd_client_get_video_stream_height (GstActiveStream * stream);
524 guint gst_mpd_client_get_video_stream_bandwidth (GstActiveStream * stream);
525 guint gst_mpd_client_get_audio_stream_rate (GstActiveStream * stream);
526 guint gst_mpd_client_get_audio_stream_num_channels (GstActiveStream * stream);
527
528 /* Support multi language */
529 guint gst_mpdparser_get_list_and_nb_of_audio_language (GstMpdClient *client, GList **lang);
530 G_END_DECLS
531
532 #endif /* __GST_MPDPARSER_H__ */