summaryrefslogtreecommitdiff
path: root/ext/smoothstreaming/gstmssfragmentparser.c
blob: 5cbd3e0416bce48a11ade4b29cafb630a74b5d32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * Microsoft Smooth-Streaming fragment parsing library
 *
 * gstmssfragmentparser.h
 *
 * Copyright (C) 2016 Igalia S.L
 * Copyright (C) 2016 Metrological
 *   Author: Philippe Normand <philn@igalia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library (COPYING); if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "gstmssfragmentparser.h"
#include <gst/base/gstbytereader.h>
#include <string.h>

GST_DEBUG_CATEGORY_EXTERN (mssdemux_debug);
#define GST_CAT_DEFAULT mssdemux_debug

void
gst_mss_fragment_parser_init (GstMssFragmentParser * parser)
{
  parser->status = GST_MSS_FRAGMENT_HEADER_PARSER_INIT;
}

void
gst_mss_fragment_parser_clear (GstMssFragmentParser * parser)
{
  if (parser->moof)
    gst_isoff_moof_box_free (parser->moof);
  parser->moof = NULL;
  parser->current_fourcc = 0;
}

gboolean
gst_mss_fragment_parser_add_buffer (GstMssFragmentParser * parser,
    GstBuffer * buffer)
{
  GstByteReader reader;
  GstMapInfo info;
  guint64 size;
  guint32 fourcc;
  guint header_size;
  gboolean error = FALSE;

  if (!gst_buffer_map (buffer, &info, GST_MAP_READ)) {
    return FALSE;
  }

  gst_byte_reader_init (&reader, info.data, info.size);
  GST_TRACE ("Total buffer size: %u", gst_byte_reader_get_size (&reader));

  do {
    parser->current_fourcc = 0;

    if (!gst_isoff_parse_box_header (&reader, &fourcc, NULL, &header_size,
            &size)) {
      break;
    }

    parser->current_fourcc = fourcc;

    GST_LOG ("box %" GST_FOURCC_FORMAT " size %" G_GUINT64_FORMAT,
        GST_FOURCC_ARGS (fourcc), size);

    parser->current_fourcc = fourcc;

    if (parser->current_fourcc == GST_ISOFF_FOURCC_MOOF) {
      GstByteReader sub_reader;

      g_assert (parser->moof == NULL);
      gst_byte_reader_get_sub_reader (&reader, &sub_reader, size - header_size);
      parser->moof = gst_isoff_moof_box_parse (&sub_reader);
      if (parser->moof == NULL) {
        GST_ERROR ("Failed to parse moof");
        error = TRUE;
      }
    } else if (parser->current_fourcc == GST_ISOFF_FOURCC_MDAT) {
      goto beach;
    } else {
      gst_byte_reader_skip (&reader, size - header_size);
    }
  } while (gst_byte_reader_get_remaining (&reader) > 0);

beach:

  /* Do sanity check */
  if (parser->current_fourcc != GST_ISOFF_FOURCC_MDAT || !parser->moof ||
      parser->moof->traf->len == 0)
    error = TRUE;

  if (!error) {
    GstTrafBox *traf = &g_array_index (parser->moof->traf, GstTrafBox, 0);
    if (!traf->tfxd) {
      GST_ERROR ("no tfxd box");
      error = TRUE;
    } else if (!traf->tfrf) {
      GST_ERROR ("no tfrf box");
      error = TRUE;
    }
  }

  if (!error)
    parser->status = GST_MSS_FRAGMENT_HEADER_PARSER_FINISHED;

  GST_LOG ("Fragment parsing successful: %s", error ? "no" : "yes");
  gst_buffer_unmap (buffer, &info);
  return !error;
}