| 1 |
/* GStreamer |
| 2 |
* Copyright (C) 2016 Jan Schmidt <[email protected]> |
| 3 |
* Copyright (C) 2016 Tim-Philipp Müller <[email protected]> |
| 4 |
* |
| 5 |
* gsthlsdemux-util.c: |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Library General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Library General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Library General Public |
| 18 |
* License along with this library; if not, write to the |
| 19 |
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 20 |
* Boston, MA 02110-1301, USA. |
| 21 |
*/ |
| 22 |
#ifdef HAVE_CONFIG_H |
| 23 |
# include "config.h" |
| 24 |
#endif |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
|
| 28 |
#include <gmodule.h> |
| 29 |
|
| 30 |
#include <gst/gst.h> |
| 31 |
#include <gst/tag/tag.h> |
| 32 |
#include <string.h> |
| 33 |
|
| 34 |
#include "gsthlsdemux.h" |
| 35 |
#include "gsthlsdemux-stream.h" |
| 36 |
|
| 37 |
GST_DEBUG_CATEGORY_EXTERN (gst_hls_demux2_debug); |
| 38 |
#define GST_CAT_DEFAULT gst_hls_demux2_debug |
| 39 |
|
| 40 |
|
| 41 |
/* Mpeg-TS Packet */ |
| 42 |
#define TS_PACKET_SYNC_BYTE 0x47 |
| 43 |
|
| 44 |
#define TS_PACKET_TRANSPORT_ERROR_INDICATOR(packet) \ |
| 45 |
((packet)[1] & 0x80) |
| 46 |
#define TS_PACKET_PAYLOAD_UNIT_START(packet) \ |
| 47 |
((packet)[1] & 0x40) |
| 48 |
|
| 49 |
#define TS_PACKET_PID(packet) \ |
| 50 |
((guint16) ((packet)[1] & 0x1f) << 8 | (packet)[2]) |
| 51 |
|
| 52 |
#define TS_PACKET_TRANSPORT_SCRAMBLING_CONTROL(packet) \ |
| 53 |
((packet)[3] & 0xc0) |
| 54 |
#define TS_PACKET_HAS_ADAPTATION_FIELD(packet) \ |
| 55 |
((packet)[3] & 0x20) |
| 56 |
#define TS_PACKET_HAS_PAYLOAD(packet) \ |
| 57 |
((packet)[3] & 0x10) |
| 58 |
#define TS_PACKET_CONTINUITY_COUNTER(pacet) \ |
| 59 |
((packet)[3] & 0x0f) |
| 60 |
|
| 61 |
#define TS_PACKET_ADAPTATION_FIELD(packet) \ |
| 62 |
(TS_PACKET_HAS_ADAPTATION_FIELD(packet) ? \ |
| 63 |
(packet) + 4 : NULL) |
| 64 |
|
| 65 |
/* Adaptation field size. Note: can be 0 */ |
| 66 |
#define TS_PACKET_ADAPTATION_FIELD_SIZE(packet) \ |
| 67 |
(packet)[4] |
| 68 |
|
| 69 |
|
| 70 |
#define TS_PACKET_PAYLOAD_OFFSET(packet) \ |
| 71 |
(TS_PACKET_HAS_ADAPTATION_FIELD (packet) ? \ |
| 72 |
4 + TS_PACKET_ADAPTATION_FIELD_SIZE (packet) + 1 : \ |
| 73 |
4) |
| 74 |
|
| 75 |
#define TS_PACKET_PAYLOAD(packet) \ |
| 76 |
(TS_PACKET_HAS_PAYLOAD (packet) ? \ |
| 77 |
(packet) + TS_PACKET_PAYLOAD_OFFSET(packet) : \ |
| 78 |
NULL) |
| 79 |
|
| 80 |
/* PES Packet */ |
| 81 |
|
| 82 |
#define PES_IS_VALID(pes) ((pes)[0] == 0x00 && \ |
| 83 |
(pes)[1] == 0x00 && \ |
| 84 |
(pes)[2] == 0x01) |
| 85 |
|
| 86 |
#define PES_STREAM_ID(pes) ((pes)[3]) |
| 87 |
|
| 88 |
#define PES_PACKET_LENGTH(pes) \ |
| 89 |
((guint16) (((pes)[4] << 8) | (pes)[5])) |
| 90 |
|
| 91 |
#define PES_STREAM_TYPE_HAS_HEADER(stream_type) \ |
| 92 |
(stream_type != 0xac) |
| 93 |
|
| 94 |
#define PES_HEADER_DATA_LENGTH(pes) ((pes)[8]) |
| 95 |
#define PES_PAYLOAD_DATA_OFFSET(pes) \ |
| 96 |
(9 + PES_HEADER_DATA_LENGTH (pes)) |
| 97 |
|
| 98 |
#define PES_HAS_PTS(pes) ((pes)[7] & 0x80) |
| 99 |
#define PES_HAS_DTS(pes) ((pes)[7] & 0x40) |
| 100 |
|
| 101 |
/* SI/PSI Packet */ |
| 102 |
|
| 103 |
#define TS_SECTION_POINTER(payload) ((payload)[0]) |
| 104 |
#define TS_PACKET_GET_SECTION(payload) ((payload) + TS_SECTION_POINTER(payload)) |
| 105 |
|
| 106 |
/* PAT section */ |
| 107 |
#define PAT_PROGRAM_OFFSET(pat, idx) \ |
| 108 |
(7 + (idx) * 4) |
| 109 |
#define PAT_PROGRAM_PID_OFFSET(pat, idx) \ |
| 110 |
(PAT_PROGRAM_PID_OFFSET(pat,idx) + 2) |
| 111 |
#define PAT_GET_PROGRAM_PID(pat, idx) \ |
| 112 |
(pat[ + PAT_PROGRAM_OFFSET(pat, idx) + 2) |
| 113 |
|
| 114 |
static inline gboolean |
| 115 |
read_ts (const guint8 * data, guint64 * target) |
| 116 |
{ |
| 117 |
/* sync:4 == 00xx ! pts:3 ! 1 ! pts:15 ! 1 | pts:15 ! 1 */ |
| 118 |
if ((*data & 0x01) != 0x01) |
| 119 |
return FALSE; |
| 120 |
*target = ((guint64) (*data++ & 0x0E)) << 29; |
| 121 |
*target |= ((guint64) (*data++)) << 22; |
| 122 |
if ((*data & 0x01) != 0x01) |
| 123 |
return FALSE; |
| 124 |
*target |= ((guint64) (*data++ & 0xFE)) << 14; |
| 125 |
*target |= ((guint64) (*data++)) << 7; |
| 126 |
if ((*data & 0x01) != 0x01) |
| 127 |
return FALSE; |
| 128 |
*target |= ((guint64) (*data++ & 0xFE)) >> 1; |
| 129 |
|
| 130 |
return TRUE; |
| 131 |
} |
| 132 |
|
| 133 |
#define PES_PTS_OFFSET(pes) (9) |
| 134 |
#define PES_PTS(pes, dest) (read_ts ((pes) + PES_PTS_OFFSET(pes), dest)) |
| 135 |
|
| 136 |
#define PES_DTS_OFFSET(pes) (PES_HAS_PTS(pes) ? 9 + 5 : 9) |
| 137 |
#define PES_DTS(pes, dest) (read_ts ((pes) + PES_DTS_OFFSET(pes), dest)) |
| 138 |
|
| 139 |
|
| 140 |
/* Check for sync byte, error_indicator == 0 and packet has payload. |
| 141 |
* Adaptation control field (data[3] & 0x30) may be zero for TS packets with |
| 142 |
* null PIDs. Still, these streams are valid TS streams (for null packets, |
| 143 |
* AFC is supposed to be 0x1, but the spec also says decoders should just |
| 144 |
* discard any packets with AFC = 0x00) */ |
| 145 |
#define IS_MPEGTS_HEADER(data) (data[0] == 0x47 && \ |
| 146 |
(data[1] & 0x80) == 0x00 && \ |
| 147 |
((data[3] & 0x30) != 0x00 || \ |
| 148 |
((data[3] & 0x30) == 0x00 && (data[1] & 0x1f) == 0x1f && (data[2] & 0xff) == 0xff))) |
| 149 |
|
| 150 |
#define PCRTIME_TO_GSTTIME(t) (((t) * (guint64)1000) / 27) |
| 151 |
#define MPEGTIME_TO_GSTTIME(t) (((t) * (guint64)100000) / 9) |
| 152 |
|
| 153 |
static gboolean |
| 154 |
have_ts_sync (const guint8 * data, guint size, guint packet_size, guint num) |
| 155 |
{ |
| 156 |
while (num-- > 0) { |
| 157 |
if (size < packet_size) |
| 158 |
return FALSE; |
| 159 |
if (!IS_MPEGTS_HEADER (data)) |
| 160 |
return FALSE; |
| 161 |
data += packet_size; |
| 162 |
size -= packet_size; |
| 163 |
} |
| 164 |
return TRUE; |
| 165 |
} |
| 166 |
|
| 167 |
#define GST_MPEGTS_TYPEFIND_MIN_HEADERS 4 |
| 168 |
|
| 169 |
static gint |
| 170 |
find_offset (const guint8 * data, guint size, guint * out_packet_size) |
| 171 |
{ |
| 172 |
guint sync_points = CLAMP (size / 188, GST_MPEGTS_TYPEFIND_MIN_HEADERS, 100); |
| 173 |
guint off; |
| 174 |
const gint packet_size = 188; |
| 175 |
|
| 176 |
/* FIXME: check 192 as well, and maybe also 204, 208 */ |
| 177 |
for (off = 0; off < MIN (size, 1024); ++off) { |
| 178 |
if (have_ts_sync (data + off, size - off, packet_size, sync_points)) { |
| 179 |
*out_packet_size = packet_size; |
| 180 |
return off; |
| 181 |
} |
| 182 |
} |
| 183 |
return -1; |
| 184 |
} |
| 185 |
|
| 186 |
static gboolean |
| 187 |
handle_pmt (const guint8 * data, guint size, guint packet_size) |
| 188 |
{ |
| 189 |
const guint8 *p = data; |
| 190 |
guint32 hdr = GST_READ_UINT32_BE (p); |
| 191 |
guint slen, pcr_pid, pilen; |
| 192 |
|
| 193 |
GST_MEMDUMP ("PMT", data, size); |
| 194 |
data = p + 4; |
| 195 |
if ((hdr & 0x00000020) != 0) /* has_adaptation_field */ |
| 196 |
data += 1 + p[4]; /* adaptation_field_len */ |
| 197 |
data += 1 + data[0]; /* pointer_field */ |
| 198 |
if (data[0] != 0x02) /* table_id */ |
| 199 |
return FALSE; |
| 200 |
//gst_util_dump_mem (data, 8); |
| 201 |
/* we assume the entire PMT fits into a single packet and this is it */ |
| 202 |
if (data[6] != 0 || data[6] != data[7]) |
| 203 |
return FALSE; |
| 204 |
slen = GST_READ_UINT16_BE (data + 1) & 0x0FFF; |
| 205 |
if (slen > (gsize) (p + packet_size - (data + 1 + 2)) || slen < 5 + 2 + 4) |
| 206 |
return FALSE; |
| 207 |
data += 3 + 5; |
| 208 |
slen -= 5; /* bytes after section_length field itself */ |
| 209 |
slen -= 4; /* crc at end */ |
| 210 |
pcr_pid = GST_READ_UINT16_BE (data) & 0x1fff; |
| 211 |
if (pcr_pid != 0x1fff) { |
| 212 |
GST_DEBUG ("pcr_pid: %04x", pcr_pid); |
| 213 |
} |
| 214 |
data += 2; |
| 215 |
/* Skip global descriptors */ |
| 216 |
pilen = GST_READ_UINT16_BE (data + 1) & 0x0FFF; |
| 217 |
data += 2 + pilen; |
| 218 |
|
| 219 |
|
| 220 |
return FALSE; |
| 221 |
} |
| 222 |
|
| 223 |
static gboolean |
| 224 |
pat_get_pmt_pid (const guint8 * data, guint size, guint packet_size, |
| 225 |
gint * pmt_pid) |
| 226 |
{ |
| 227 |
const guint8 *p = data; |
| 228 |
guint32 hdr = GST_READ_UINT32_BE (p); |
| 229 |
guint slen; |
| 230 |
|
| 231 |
data = p + 4; |
| 232 |
if ((hdr & 0x00000020) != 0) /* has_adaptation_field */ |
| 233 |
data += 1 + p[4]; /* adaptation_field_len */ |
| 234 |
data += 1 + data[0]; /* pointer_field */ |
| 235 |
if (data[0] != 0) /* table_id */ |
| 236 |
return FALSE; |
| 237 |
/* we assume the entire PAT fits into a single packet and this is it */ |
| 238 |
if (data[6] != 0 || data[6] != data[7]) |
| 239 |
return FALSE; |
| 240 |
slen = GST_READ_UINT16_BE (data + 1) & 0x0FFF; |
| 241 |
if (slen > (gsize) (p + packet_size - (data + 1 + 2)) || slen < 5 + 4 + 4) |
| 242 |
return FALSE; |
| 243 |
data += 3 + 5; |
| 244 |
slen -= 5; /* bytes after section_length field itself */ |
| 245 |
slen -= 4; /* crc at end */ |
| 246 |
while (slen >= 4) { |
| 247 |
guint program_num = GST_READ_UINT16_BE (data); |
| 248 |
guint val = GST_READ_UINT16_BE (data + 2) & 0x1fff; |
| 249 |
if (program_num != 0) { |
| 250 |
GST_DEBUG (" program %04x: pmt_pid : %04x", program_num, val); |
| 251 |
*pmt_pid = val; |
| 252 |
return TRUE; |
| 253 |
} |
| 254 |
data += 4; |
| 255 |
slen -= 4; |
| 256 |
} |
| 257 |
|
| 258 |
return FALSE; |
| 259 |
} |
| 260 |
|
| 261 |
static GstClockTime |
| 262 |
get_first_mpegts_time (const guint8 * data, gsize size, guint packet_size) |
| 263 |
{ |
| 264 |
GstClockTime internal_time = GST_CLOCK_TIME_NONE; |
| 265 |
const guint8 *p; |
| 266 |
gint pmt_pid = -1; |
| 267 |
|
| 268 |
for (p = data; size >= packet_size; p += packet_size, size -= packet_size) { |
| 269 |
if (p[0] != TS_PACKET_SYNC_BYTE) { |
| 270 |
GST_WARNING ("Lost sync"); |
| 271 |
break; |
| 272 |
} |
| 273 |
|
| 274 |
/* We only care about start packets which have some form of payload (pes or |
| 275 |
section) */ |
| 276 |
if (TS_PACKET_PAYLOAD_UNIT_START (p) && TS_PACKET_HAS_PAYLOAD (p)) { |
| 277 |
guint16 pid; |
| 278 |
const guint8 *payload; |
| 279 |
const guint8 *afc; |
| 280 |
|
| 281 |
/* Skip packets which have error indicator set or are scrambled */ |
| 282 |
if (G_UNLIKELY (TS_PACKET_TRANSPORT_ERROR_INDICATOR (p) || |
| 283 |
TS_PACKET_TRANSPORT_SCRAMBLING_CONTROL (p))) |
| 284 |
continue; |
| 285 |
|
| 286 |
pid = TS_PACKET_PID (p); |
| 287 |
payload = TS_PACKET_PAYLOAD (p); |
| 288 |
afc = TS_PACKET_ADAPTATION_FIELD (p); |
| 289 |
|
| 290 |
GST_LOG ("PID 0x%04x", pid); |
| 291 |
if (afc && afc[0]) |
| 292 |
GST_MEMDUMP ("afc", afc, afc[0]); |
| 293 |
GST_MEMDUMP ("payload", payload, 32); |
| 294 |
if (pmt_pid != -1 && PES_IS_VALID (payload)) { |
| 295 |
guint64 ts; |
| 296 |
GstClockTime pts, dts; |
| 297 |
|
| 298 |
pts = dts = GST_CLOCK_TIME_NONE; |
| 299 |
|
| 300 |
GST_DEBUG ("PID 0x%04x stream_id 0x%02x PES start", pid, |
| 301 |
PES_STREAM_ID (payload)); |
| 302 |
GST_MEMDUMP ("PES data", payload + PES_PAYLOAD_DATA_OFFSET (payload), |
| 303 |
32); |
| 304 |
|
| 305 |
/* Grab PTS/DTS */ |
| 306 |
if (PES_HAS_PTS (payload) && PES_PTS (payload, &ts)) { |
| 307 |
pts = MPEGTIME_TO_GSTTIME (ts); |
| 308 |
GST_LOG ("PID 0x%04x PTS %" G_GUINT64_FORMAT " %" GST_TIME_FORMAT, |
| 309 |
pid, ts, GST_TIME_ARGS (pts)); |
| 310 |
} |
| 311 |
if (PES_HAS_DTS (payload) && PES_DTS (payload, &ts)) { |
| 312 |
dts = MPEGTIME_TO_GSTTIME (ts); |
| 313 |
GST_LOG ("PID 0x%04x DTS %" G_GUINT64_FORMAT " %" GST_TIME_FORMAT, |
| 314 |
pid, ts, GST_TIME_ARGS (dts)); |
| 315 |
} |
| 316 |
|
| 317 |
/* Pick the lowest value */ |
| 318 |
if (GST_CLOCK_TIME_IS_VALID (dts)) { |
| 319 |
if (GST_CLOCK_TIME_IS_VALID (pts)) { |
| 320 |
/* Only take the PTS if it's lower than the dts and does not differ |
| 321 |
* by more than a second (which would indicate bogus values) */ |
| 322 |
if (pts < dts && (dts - pts) < GST_SECOND) |
| 323 |
internal_time = pts; |
| 324 |
else |
| 325 |
internal_time = dts; |
| 326 |
} else { |
| 327 |
internal_time = dts; |
| 328 |
} |
| 329 |
goto out; |
| 330 |
} else if (GST_CLOCK_TIME_IS_VALID (pts)) { |
| 331 |
internal_time = pts; |
| 332 |
goto out; |
| 333 |
} |
| 334 |
} else if (pid == 0x00) { |
| 335 |
GST_DEBUG ("PAT !"); |
| 336 |
if (!pat_get_pmt_pid (p, packet_size, packet_size, &pmt_pid)) { |
| 337 |
GST_WARNING ("Invalid PAT"); |
| 338 |
goto out; |
| 339 |
} |
| 340 |
} else if (pmt_pid != -1 && pid == pmt_pid) { |
| 341 |
GST_DEBUG ("PMT !"); |
| 342 |
/* FIXME : Grab the list of *actual* elementary stream PID to make sure |
| 343 |
* we have checked the first PTS of each stream (and not just the first |
| 344 |
* one we saw, which might not be the smallest */ |
| 345 |
handle_pmt (p, packet_size, packet_size); |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
out: |
| 351 |
return internal_time; |
| 352 |
} |
| 353 |
|
| 354 |
GstHLSParserResult |
| 355 |
gst_hlsdemux_handle_content_mpegts (GstHLSDemux * demux, |
| 356 |
GstHLSDemuxStream * hls_stream, gboolean draining, GstBuffer ** buffer) |
| 357 |
{ |
| 358 |
GstMapInfo info; |
| 359 |
gint offset; |
| 360 |
const guint8 *data; |
| 361 |
GstClockTime internal_time = GST_CLOCK_TIME_NONE; |
| 362 |
guint packet_size; |
| 363 |
gsize size; |
| 364 |
|
| 365 |
if (!gst_buffer_map (*buffer, &info, GST_MAP_READ)) |
| 366 |
return GST_HLS_PARSER_RESULT_ERROR; |
| 367 |
|
| 368 |
data = info.data; |
| 369 |
size = info.size; |
| 370 |
|
| 371 |
offset = find_offset (data, size, &packet_size); |
| 372 |
if (offset < 0) { |
| 373 |
gst_buffer_unmap (*buffer, &info); |
| 374 |
return GST_HLS_PARSER_RESULT_ERROR; |
| 375 |
} |
| 376 |
|
| 377 |
GST_LOG ("TS packet start offset: %d", offset); |
| 378 |
|
| 379 |
data += offset; |
| 380 |
size -= offset; |
| 381 |
|
| 382 |
internal_time = get_first_mpegts_time (data, size, packet_size); |
| 383 |
|
| 384 |
GST_DEBUG_OBJECT (hls_stream, "Using internal time %" GST_TIME_FORMAT, |
| 385 |
GST_TIME_ARGS (internal_time)); |
| 386 |
|
| 387 |
gst_buffer_unmap (*buffer, &info); |
| 388 |
|
| 389 |
if (!GST_CLOCK_TIME_IS_VALID (internal_time)) |
| 390 |
return GST_HLS_PARSER_RESULT_NEED_MORE_DATA; |
| 391 |
|
| 392 |
/* We have the first internal time, figure out if we are in sync or not */ |
| 393 |
return gst_hlsdemux_stream_handle_internal_time (hls_stream, internal_time); |
| 394 |
} |
| 395 |
|
| 396 |
GstHLSParserResult |
| 397 |
gst_hlsdemux_handle_content_isobmff (GstHLSDemux * demux, |
| 398 |
GstHLSDemuxStream * hls_stream, gboolean draining, GstBuffer ** buffer) |
| 399 |
{ |
| 400 |
GstMapInfo info; |
| 401 |
GstByteReader br, sub; |
| 402 |
guint32 box_type; |
| 403 |
guint header_size; |
| 404 |
guint64 box_size; |
| 405 |
GstHLSParserResult ret = GST_HLS_PARSER_RESULT_NEED_MORE_DATA; |
| 406 |
GstClockTime smallest_ts = GST_CLOCK_TIME_NONE; |
| 407 |
|
| 408 |
if (!gst_buffer_map (*buffer, &info, GST_MAP_READ)) |
| 409 |
return GST_HLS_PARSER_RESULT_ERROR; |
| 410 |
|
| 411 |
gst_byte_reader_init (&br, info.data, info.size); |
| 412 |
|
| 413 |
while (gst_byte_reader_get_remaining (&br) && |
| 414 |
gst_isoff_parse_box_header (&br, &box_type, NULL, &header_size, |
| 415 |
&box_size)) { |
| 416 |
GST_DEBUG ("box %" GST_FOURCC_FORMAT " size:%" G_GUINT64_FORMAT, |
| 417 |
GST_FOURCC_ARGS (box_type), box_size); |
| 418 |
|
| 419 |
GST_MEMDUMP ("box content", br.data + br.byte, MIN (256, |
| 420 |
box_size - header_size)); |
| 421 |
|
| 422 |
switch (box_type) { |
| 423 |
case GST_ISOFF_FOURCC_MOOV: |
| 424 |
{ |
| 425 |
GstMoovBox *moov; |
| 426 |
gst_byte_reader_get_sub_reader (&br, &sub, box_size - header_size); |
| 427 |
moov = gst_isoff_moov_box_parse (&sub); |
| 428 |
|
| 429 |
if (moov) { |
| 430 |
GST_DEBUG ("Got moov box"); |
| 431 |
if (hls_stream->moov) |
| 432 |
gst_isoff_moov_box_free (hls_stream->moov); |
| 433 |
hls_stream->moov = moov; |
| 434 |
} |
| 435 |
break; |
| 436 |
} |
| 437 |
case GST_ISOFF_FOURCC_MOOF: |
| 438 |
{ |
| 439 |
GstMoofBox *moof; |
| 440 |
|
| 441 |
if (hls_stream->moov == NULL) { |
| 442 |
GST_WARNING ("Received moof with moov in iso-ff stream"); |
| 443 |
break; |
| 444 |
} |
| 445 |
|
| 446 |
gst_byte_reader_get_sub_reader (&br, &sub, box_size - header_size); |
| 447 |
|
| 448 |
moof = gst_isoff_moof_box_parse (&sub); |
| 449 |
|
| 450 |
if (moof) { |
| 451 |
guint i, j; |
| 452 |
GST_DEBUG ("Got moof box"); |
| 453 |
/* Use the track information from stream->moov */ |
| 454 |
for (i = 0; i < hls_stream->moov->trak->len; i++) { |
| 455 |
GstTrakBox *trak = |
| 456 |
&g_array_index (hls_stream->moov->trak, GstTrakBox, i); |
| 457 |
GST_LOG ("trak #%d %p", i, trak); |
| 458 |
for (j = 0; j < moof->traf->len; j++) { |
| 459 |
GstTrafBox *traf = &g_array_index (moof->traf, GstTrafBox, j); |
| 460 |
if (traf->tfhd.track_id == trak->tkhd.track_id) { |
| 461 |
GstClockTime ts = 0; |
| 462 |
guint64 decode_time = traf->tfdt.decode_time; |
| 463 |
|
| 464 |
if (decode_time != GST_CLOCK_TIME_NONE) |
| 465 |
ts = gst_util_uint64_scale (decode_time, GST_SECOND, |
| 466 |
trak->mdia.mdhd.timescale); |
| 467 |
|
| 468 |
GST_LOG ("Found decode_time %" GST_TIME_FORMAT " for trak %d", |
| 469 |
GST_TIME_ARGS (ts), traf->tfhd.track_id); |
| 470 |
if (smallest_ts == GST_CLOCK_TIME_NONE || ts < smallest_ts) |
| 471 |
smallest_ts = ts; |
| 472 |
} |
| 473 |
} |
| 474 |
} |
| 475 |
gst_isoff_moof_box_free (moof); |
| 476 |
} else { |
| 477 |
GST_WARNING ("Failed to parse moof"); |
| 478 |
} |
| 479 |
if (smallest_ts != GST_CLOCK_TIME_NONE) |
| 480 |
goto out; |
| 481 |
break; |
| 482 |
} |
| 483 |
case GST_ISOFF_FOURCC_MDAT: |
| 484 |
GST_DEBUG ("Reached `mdat`, returning"); |
| 485 |
goto out; |
| 486 |
break; |
| 487 |
default: |
| 488 |
GST_LOG ("Skipping unhandled box %" GST_FOURCC_FORMAT, |
| 489 |
GST_FOURCC_ARGS (box_type)); |
| 490 |
gst_byte_reader_skip (&br, box_size - header_size); |
| 491 |
break; |
| 492 |
} |
| 493 |
|
| 494 |
} |
| 495 |
|
| 496 |
out: |
| 497 |
gst_buffer_unmap (*buffer, &info); |
| 498 |
|
| 499 |
if (smallest_ts != GST_CLOCK_TIME_NONE) { |
| 500 |
ret = gst_hlsdemux_stream_handle_internal_time (hls_stream, smallest_ts); |
| 501 |
} |
| 502 |
|
| 503 |
return ret; |
| 504 |
} |
| 505 |
|
| 506 |
GstHLSParserResult |
| 507 |
gst_hlsdemux_handle_content_id3 (GstHLSDemux * demux, |
| 508 |
GstHLSDemuxStream * hls_stream, gboolean draining, GstBuffer ** buffer) |
| 509 |
{ |
| 510 |
GstMapInfo info; |
| 511 |
guint32 tag_size; |
| 512 |
gsize size; |
| 513 |
GstTagList *taglist; |
| 514 |
GstSample *priv_data = NULL; |
| 515 |
GstBuffer *tag_buf; |
| 516 |
guint64 pts; |
| 517 |
GstHLSParserResult ret = GST_HLS_PARSER_RESULT_DONE; |
| 518 |
GstClockTime internal; |
| 519 |
|
| 520 |
/* We need at least 10 bytes, starting with "ID3" for the header */ |
| 521 |
size = gst_buffer_get_size (*buffer); |
| 522 |
if (size < 10) |
| 523 |
return GST_HLS_PARSER_RESULT_NEED_MORE_DATA; |
| 524 |
|
| 525 |
/* Read the tag size */ |
| 526 |
tag_size = gst_tag_get_id3v2_tag_size (*buffer); |
| 527 |
|
| 528 |
/* Check we've collected that much */ |
| 529 |
if (size < tag_size) |
| 530 |
return GST_HLS_PARSER_RESULT_NEED_MORE_DATA; |
| 531 |
|
| 532 |
/* Parse the tag */ |
| 533 |
taglist = gst_tag_list_from_id3v2_tag (*buffer); |
| 534 |
if (taglist == NULL) { |
| 535 |
return GST_HLS_PARSER_RESULT_ERROR; /* Invalid tag, stop trying */ |
| 536 |
} |
| 537 |
|
| 538 |
/* Extract the timestamps */ |
| 539 |
if (!gst_tag_list_get_sample (taglist, GST_TAG_PRIVATE_DATA, &priv_data)) |
| 540 |
goto out; |
| 541 |
|
| 542 |
if (g_strcmp0 ("com.apple.streaming.transportStreamTimestamp", |
| 543 |
gst_structure_get_string (gst_sample_get_info (priv_data), "owner"))) |
| 544 |
goto out; |
| 545 |
|
| 546 |
/* OK, now as per section 3, the tag contains a 33-bit PCR inside a 64-bit |
| 547 |
* BE-word */ |
| 548 |
tag_buf = gst_sample_get_buffer (priv_data); |
| 549 |
if (tag_buf == NULL) |
| 550 |
goto out; |
| 551 |
|
| 552 |
if (!gst_buffer_map (tag_buf, &info, GST_MAP_READ)) |
| 553 |
goto out; |
| 554 |
GST_MEMDUMP ("id3 tag", info.data, info.size); |
| 555 |
|
| 556 |
pts = GST_READ_UINT64_BE (info.data); |
| 557 |
internal = MPEGTIME_TO_GSTTIME (pts); |
| 558 |
|
| 559 |
GST_LOG ("Got internal PTS from ID3: %" G_GUINT64_FORMAT " (%" GST_TIME_FORMAT |
| 560 |
")", pts, GST_TIME_ARGS (internal)); |
| 561 |
|
| 562 |
gst_buffer_unmap (tag_buf, &info); |
| 563 |
|
| 564 |
ret = gst_hlsdemux_stream_handle_internal_time (hls_stream, internal); |
| 565 |
|
| 566 |
out: |
| 567 |
if (priv_data) |
| 568 |
gst_sample_unref (priv_data); |
| 569 |
if (taglist) |
| 570 |
gst_tag_list_unref (taglist); |
| 571 |
|
| 572 |
return ret; |
| 573 |
} |
| 574 |
|
| 575 |
/* Grabs the next numerical value from the bytereader, skipping any spaces. |
| 576 |
* |
| 577 |
* It will stop/return at the next non-digit/non-space position */ |
| 578 |
static gboolean |
| 579 |
byte_reader_get_next_uint_string (GstByteReader * br, guint * out) |
| 580 |
{ |
| 581 |
guint value = 0; |
| 582 |
gboolean res = FALSE; |
| 583 |
|
| 584 |
while (gst_byte_reader_get_remaining (br)) { |
| 585 |
guint8 d = gst_byte_reader_peek_uint8_unchecked (br); |
| 586 |
|
| 587 |
if (g_ascii_isdigit (d)) { |
| 588 |
value = value * 10 + (d - '0'); |
| 589 |
res = TRUE; |
| 590 |
} else if (d != ' ' && d != '\t') { |
| 591 |
/* we're done and not advancing */ |
| 592 |
break; |
| 593 |
} |
| 594 |
gst_byte_reader_skip_unchecked (br, 1); |
| 595 |
} |
| 596 |
|
| 597 |
if (res) |
| 598 |
*out = value; |
| 599 |
|
| 600 |
return res; |
| 601 |
} |
| 602 |
|
| 603 |
/* Grabs the next numerical value from the bytereader, skipping any spaces. |
| 604 |
* |
| 605 |
* It will stop/return at the next non-digit/non-space position */ |
| 606 |
static gboolean |
| 607 |
byte_reader_get_next_uint64_string (GstByteReader * br, guint64 * out) |
| 608 |
{ |
| 609 |
guint64 value = 0; |
| 610 |
gboolean res = FALSE; |
| 611 |
|
| 612 |
while (gst_byte_reader_get_remaining (br)) { |
| 613 |
guint8 d = gst_byte_reader_peek_uint8_unchecked (br); |
| 614 |
|
| 615 |
if (g_ascii_isdigit (d)) { |
| 616 |
value = value * 10 + (d - '0'); |
| 617 |
res = TRUE; |
| 618 |
} else if (d != ' ' && d != '\t') { |
| 619 |
/* we're done and not advancing */ |
| 620 |
break; |
| 621 |
} |
| 622 |
gst_byte_reader_skip_unchecked (br, 1); |
| 623 |
} |
| 624 |
|
| 625 |
if (res) |
| 626 |
*out = value; |
| 627 |
|
| 628 |
return res; |
| 629 |
} |
| 630 |
|
| 631 |
static gboolean |
| 632 |
parse_webvtt_time (GstByteReader * br, GstClockTime * t, |
| 633 |
const gchar ** remainder) |
| 634 |
{ |
| 635 |
GstClockTime val = 0; |
| 636 |
gboolean res = FALSE; |
| 637 |
|
| 638 |
while (!res && gst_byte_reader_get_remaining (br)) { |
| 639 |
guint numval; |
| 640 |
if (byte_reader_get_next_uint_string (br, &numval)) { |
| 641 |
guint8 next = gst_byte_reader_peek_uint8_unchecked (br); |
| 642 |
|
| 643 |
if (next == ':' || next == '.') { |
| 644 |
/* value was hours, minutes or seconds */ |
| 645 |
val = val * 60 + numval; |
| 646 |
gst_byte_reader_skip (br, 1); |
| 647 |
} else { |
| 648 |
/* Reached the milliseconds, convert to GstClockTime */ |
| 649 |
val = val * GST_SECOND + numval * GST_MSECOND; |
| 650 |
res = TRUE; |
| 651 |
} |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
if (res) { |
| 656 |
*t = val; |
| 657 |
if (remainder) { |
| 658 |
if (gst_byte_reader_get_remaining (br)) |
| 659 |
*remainder = (const gchar *) gst_byte_reader_peek_data_unchecked (br); |
| 660 |
else |
| 661 |
*remainder = NULL; |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
return res; |
| 666 |
} |
| 667 |
|
| 668 |
static inline void |
| 669 |
br_skipwhitespace (GstByteReader * br) |
| 670 |
{ |
| 671 |
while (gst_byte_reader_get_remaining (br)) { |
| 672 |
guint8 d = gst_byte_reader_peek_uint8_unchecked (br); |
| 673 |
if (d != ' ' && d != '\t') |
| 674 |
return; |
| 675 |
gst_byte_reader_skip_unchecked (br, 1); |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
/* Returns TRUE if br starts with str |
| 680 |
* |
| 681 |
* Skips any spaces/tabs before and after str */ |
| 682 |
static gboolean |
| 683 |
br_startswith (GstByteReader * br, const gchar * str, gboolean skip_ws) |
| 684 |
{ |
| 685 |
guint len = strlen (str); |
| 686 |
const guint8 *data; |
| 687 |
|
| 688 |
if (skip_ws) |
| 689 |
br_skipwhitespace (br); |
| 690 |
if (!gst_byte_reader_peek_data (br, len, &data)) |
| 691 |
return FALSE; |
| 692 |
if (strncmp ((gchar *) data, str, len)) |
| 693 |
return FALSE; |
| 694 |
gst_byte_reader_skip_unchecked (br, len); |
| 695 |
if (skip_ws) |
| 696 |
br_skipwhitespace (br); |
| 697 |
|
| 698 |
return TRUE; |
| 699 |
} |
| 700 |
|
| 701 |
static gboolean |
| 702 |
gst_hls_demux_webvtt_read_x_timestamp_map (gchar * data, GstClockTime * local, |
| 703 |
GstClockTime * mpegts) |
| 704 |
{ |
| 705 |
GstByteReader br; |
| 706 |
|
| 707 |
gst_byte_reader_init (&br, (guint8 *) data, strlen (data)); |
| 708 |
|
| 709 |
if (!br_startswith (&br, "X-TIMESTAMP-MAP=", FALSE)) |
| 710 |
return FALSE; |
| 711 |
|
| 712 |
if (br_startswith (&br, "MPEGTS:", TRUE)) { |
| 713 |
if (!byte_reader_get_next_uint64_string (&br, mpegts)) |
| 714 |
return FALSE; |
| 715 |
/* Convert to GstClockTime */ |
| 716 |
*mpegts = MPEGTIME_TO_GSTTIME (*mpegts); |
| 717 |
if (!br_startswith (&br, ",", TRUE)) |
| 718 |
return FALSE; |
| 719 |
if (!br_startswith (&br, "LOCAL:", TRUE)) |
| 720 |
return FALSE; |
| 721 |
if (!parse_webvtt_time (&br, local, NULL)) |
| 722 |
return FALSE; |
| 723 |
} else if (br_startswith (&br, "LOCAL:", TRUE)) { |
| 724 |
if (!parse_webvtt_time (&br, local, NULL)) |
| 725 |
return FALSE; |
| 726 |
if (!br_startswith (&br, ",", TRUE)) |
| 727 |
return FALSE; |
| 728 |
if (!br_startswith (&br, "MPEGTS:", TRUE)) |
| 729 |
return FALSE; |
| 730 |
if (!byte_reader_get_next_uint64_string (&br, mpegts)) |
| 731 |
return FALSE; |
| 732 |
/* Convert to GstClockTime */ |
| 733 |
*mpegts = MPEGTIME_TO_GSTTIME (*mpegts); |
| 734 |
} else { |
| 735 |
return FALSE; |
| 736 |
} |
| 737 |
|
| 738 |
GST_DEBUG ("local time:%" GST_TIME_FORMAT ", mpegts time:%" GST_TIME_FORMAT, |
| 739 |
GST_TIME_ARGS (*local), GST_TIME_ARGS (*mpegts)); |
| 740 |
|
| 741 |
return TRUE; |
| 742 |
} |
| 743 |
|
| 744 |
static gboolean |
| 745 |
utf8_string_contains_alnum (gchar * string) |
| 746 |
{ |
| 747 |
gunichar c; |
| 748 |
|
| 749 |
while ((c = g_utf8_get_char (string))) { |
| 750 |
if (g_unichar_isalnum (c)) |
| 751 |
return TRUE; |
| 752 |
string = g_utf8_next_char (string); |
| 753 |
} |
| 754 |
|
| 755 |
return FALSE; |
| 756 |
} |
| 757 |
|
| 758 |
#define T_H(t) ((t) / (GST_SECOND * 60 * 60)) |
| 759 |
#define T_M(t) ((t) / (GST_SECOND * 60) % 60) |
| 760 |
#define T_S(t) ((t) / GST_SECOND % 60) |
| 761 |
#define WEBVTT_TIME_FORMAT "02u:%02u:%02u.%03u" |
| 762 |
#define WEBVTT_TIME_ARGS(t) \ |
| 763 |
(guint) ((t) / (GST_SECOND * 60 * 60)) , \ |
| 764 |
(guint) ((t) / (GST_SECOND * 60) % 60), \ |
| 765 |
(guint) ((t) / GST_SECOND % 60), \ |
| 766 |
(guint) ((t) / GST_MSECOND % 1000) |
| 767 |
static gboolean |
| 768 |
process_webvtt_cue_timing_setting_line (const gchar * input, |
| 769 |
GstClockTime * start, GstClockTime * stop, const gchar ** cue_settings) |
| 770 |
{ |
| 771 |
GstByteReader br; |
| 772 |
|
| 773 |
gst_byte_reader_init (&br, (guint8 *) input, strlen (input)); |
| 774 |
|
| 775 |
/* Handle cue timing start */ |
| 776 |
if (!parse_webvtt_time (&br, start, NULL)) |
| 777 |
return FALSE; |
| 778 |
|
| 779 |
/* --> */ |
| 780 |
if (gst_byte_reader_get_remaining (&br) < 12 || |
| 781 |
g_ascii_strncasecmp ((const gchar *) |
| 782 |
gst_byte_reader_peek_data_unchecked (&br), "-->", 3)) |
| 783 |
return FALSE; |
| 784 |
|
| 785 |
gst_byte_reader_skip (&br, 4); |
| 786 |
|
| 787 |
/* Handle cue timing stop */ |
| 788 |
if (!parse_webvtt_time (&br, stop, cue_settings)) |
| 789 |
return FALSE; |
| 790 |
|
| 791 |
return TRUE; |
| 792 |
} |
| 793 |
|
| 794 |
static GstClockTimeDiff |
| 795 |
convert_webvtt_to_stream_time (GstHLSTimeMap * map, GstClockTime localtime, |
| 796 |
GstClockTime mpegtime, GstClockTime vtt_value) |
| 797 |
{ |
| 798 |
GstClockTimeDiff res; |
| 799 |
|
| 800 |
if (localtime == GST_CLOCK_TIME_NONE || mpegtime == GST_CLOCK_TIME_NONE) { |
| 801 |
GST_DEBUG ("No X-TIMESTAMP-MAP, assuming values are MPEG-TS values"); |
| 802 |
res = gst_hls_internal_to_stream_time (map, vtt_value); |
| 803 |
|
| 804 |
/* VTT only uses positive values */ |
| 805 |
if (res < 0) |
| 806 |
res = 0; |
| 807 |
} else { |
| 808 |
GST_DEBUG ("Converting %" GST_TIME_FORMAT, |
| 809 |
GST_TIME_ARGS (vtt_value + mpegtime - localtime)); |
| 810 |
res = |
| 811 |
gst_hls_internal_to_stream_time (map, vtt_value + mpegtime - localtime); |
| 812 |
if (res == GST_CLOCK_STIME_NONE) { |
| 813 |
GST_WARNING ("Couldn't convert value, using original value %" |
| 814 |
GST_TIME_FORMAT, GST_TIME_ARGS (vtt_value)); |
| 815 |
res = vtt_value; |
| 816 |
} else if (res < 0) { |
| 817 |
res = 0; |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
return res; |
| 822 |
} |
| 823 |
|
| 824 |
GstHLSParserResult |
| 825 |
gst_hlsdemux_handle_content_webvtt (GstHLSDemux * demux, |
| 826 |
GstHLSDemuxStream * hls_stream, gboolean draining, GstBuffer ** buffer) |
| 827 |
{ |
| 828 |
GstHLSParserResult ret = GST_HLS_PARSER_RESULT_DONE; |
| 829 |
gchar *original_content; |
| 830 |
guint i, nb; |
| 831 |
gchar **original_lines; |
| 832 |
GstClockTime localtime = GST_CLOCK_TIME_NONE; |
| 833 |
GstClockTime mpegtime = GST_CLOCK_TIME_NONE; |
| 834 |
GstClockTimeDiff low_stream_time = GST_CLOCK_STIME_NONE; |
| 835 |
GstClockTimeDiff high_stream_time = GST_CLOCK_STIME_NONE; |
| 836 |
gboolean found_timing = FALSE; |
| 837 |
gboolean found_text = FALSE; |
| 838 |
GPtrArray *builder; |
| 839 |
GstM3U8MediaSegment *current_segment = hls_stream->current_segment; |
| 840 |
GstClockTimeDiff segment_start, segment_end; |
| 841 |
GstClockTimeDiff tolerance; |
| 842 |
gboolean out_of_bounds = FALSE; |
| 843 |
GstHLSTimeMap *map; |
| 844 |
|
| 845 |
/* We only process full webvtt fragments */ |
| 846 |
if (!draining) |
| 847 |
return GST_HLS_PARSER_RESULT_NEED_MORE_DATA; |
| 848 |
|
| 849 |
original_content = gst_hls_buf_to_utf8_text (*buffer); |
| 850 |
|
| 851 |
if (!original_content) |
| 852 |
return GST_HLS_PARSER_RESULT_ERROR; |
| 853 |
|
| 854 |
segment_start = current_segment->stream_time; |
| 855 |
segment_end = segment_start + current_segment->duration; |
| 856 |
tolerance = MAX (current_segment->duration / 2, 500 * GST_MSECOND); |
| 857 |
|
| 858 |
map = gst_hls_demux_find_time_map (demux, current_segment->discont_sequence); |
| 859 |
|
| 860 |
builder = g_ptr_array_new_with_free_func (g_free); |
| 861 |
|
| 862 |
original_lines = g_strsplit_set (original_content, "\n\r", 0); |
| 863 |
nb = g_strv_length (original_lines); |
| 864 |
|
| 865 |
for (i = 0; i < nb; i++) { |
| 866 |
gchar *line = original_lines[i]; |
| 867 |
|
| 868 |
GST_LOG ("Line: %s", line); |
| 869 |
|
| 870 |
if (g_str_has_prefix (line, "X-TIMESTAMP-MAP=")) { |
| 871 |
if (!gst_hls_demux_webvtt_read_x_timestamp_map (line, &localtime, |
| 872 |
&mpegtime)) { |
| 873 |
GST_WARNING ("webvtt timestamp map isn't valid"); |
| 874 |
ret = GST_HLS_PARSER_RESULT_ERROR; |
| 875 |
goto out; |
| 876 |
} |
| 877 |
g_ptr_array_add (builder, g_strdup (line)); |
| 878 |
} else if (strstr (line, " --> ")) { |
| 879 |
GstClockTime start, stop; |
| 880 |
const gchar *leftover; |
| 881 |
if (process_webvtt_cue_timing_setting_line (line, &start, &stop, |
| 882 |
&leftover)) { |
| 883 |
GstClockTimeDiff start_stream, stop_stream; |
| 884 |
gchar *newline; |
| 885 |
|
| 886 |
GST_LOG ("Found time line %" GST_TIME_FORMAT " -> %" GST_TIME_FORMAT, |
| 887 |
GST_TIME_ARGS (start), GST_TIME_ARGS (stop)); |
| 888 |
|
| 889 |
start_stream = |
| 890 |
convert_webvtt_to_stream_time (map, localtime, mpegtime, start); |
| 891 |
stop_stream = |
| 892 |
convert_webvtt_to_stream_time (map, localtime, mpegtime, stop); |
| 893 |
|
| 894 |
GST_LOG ("Stream time %" GST_STIME_FORMAT " --> %" GST_STIME_FORMAT, |
| 895 |
GST_STIME_ARGS (start_stream), GST_STIME_ARGS (stop_stream)); |
| 896 |
|
| 897 |
if (stop_stream < (segment_start - tolerance) || |
| 898 |
start_stream > (segment_end + tolerance)) { |
| 899 |
GST_WARNING ("Out of bounds"); |
| 900 |
out_of_bounds = TRUE; |
| 901 |
} |
| 902 |
if (low_stream_time == GST_CLOCK_STIME_NONE |
| 903 |
|| stop_stream < low_stream_time) |
| 904 |
low_stream_time = stop_stream; |
| 905 |
if (high_stream_time == GST_CLOCK_STIME_NONE |
| 906 |
|| start_stream > high_stream_time) |
| 907 |
high_stream_time = start_stream; |
| 908 |
|
| 909 |
/* Apply the stream presentation offset */ |
| 910 |
start_stream += hls_stream->presentation_offset; |
| 911 |
stop_stream += hls_stream->presentation_offset; |
| 912 |
|
| 913 |
/* Create the time-shifted WebVTT cue line */ |
| 914 |
if (leftover) { |
| 915 |
newline = |
| 916 |
g_strdup_printf ("%" WEBVTT_TIME_FORMAT " --> %" |
| 917 |
WEBVTT_TIME_FORMAT " %s", WEBVTT_TIME_ARGS (start_stream), |
| 918 |
WEBVTT_TIME_ARGS (stop_stream), leftover); |
| 919 |
} else { |
| 920 |
newline = |
| 921 |
g_strdup_printf ("%" WEBVTT_TIME_FORMAT " --> %" |
| 922 |
WEBVTT_TIME_FORMAT, WEBVTT_TIME_ARGS (start_stream), |
| 923 |
WEBVTT_TIME_ARGS (stop_stream)); |
| 924 |
} |
| 925 |
GST_LOG ("Generated line '%s'", newline); |
| 926 |
g_ptr_array_add (builder, newline); |
| 927 |
found_timing = TRUE; |
| 928 |
} else { |
| 929 |
GST_WARNING ("Failed to parse time line '%s'", line); |
| 930 |
/* Abort ? */ |
| 931 |
} |
| 932 |
} else if (found_timing && !found_text) { |
| 933 |
gchar *linecopy = g_strdup (line); |
| 934 |
g_ptr_array_add (builder, linecopy); |
| 935 |
if (utf8_string_contains_alnum (linecopy)) { |
| 936 |
GST_DEBUG ("Non-empty line '%s'", line); |
| 937 |
found_text = TRUE; |
| 938 |
} |
| 939 |
} else { |
| 940 |
g_ptr_array_add (builder, g_strdup (line)); |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
out: |
| 945 |
if (ret) { |
| 946 |
gchar *newfile; |
| 947 |
|
| 948 |
/* Ensure file always ends with an empty newline by adding an empty |
| 949 |
* line. This helps downstream parsers properly detect entries */ |
| 950 |
g_ptr_array_add (builder, g_strdup ("\n")); |
| 951 |
/* Add NULL-terminator to string list */ |
| 952 |
g_ptr_array_add (builder, NULL); |
| 953 |
newfile = g_strjoinv ("\n", (gchar **) builder->pdata); |
| 954 |
GST_DEBUG ("newfile:\n%s", newfile); |
| 955 |
gst_buffer_unref (*buffer); |
| 956 |
*buffer = gst_buffer_new_wrapped (newfile, strlen (newfile)); |
| 957 |
} |
| 958 |
|
| 959 |
GST_DEBUG_OBJECT (hls_stream, |
| 960 |
"Stream time %" GST_STIME_FORMAT " -> %" GST_STIME_FORMAT, |
| 961 |
GST_STIME_ARGS (low_stream_time), GST_STIME_ARGS (high_stream_time)); |
| 962 |
|
| 963 |
g_ptr_array_unref (builder); |
| 964 |
|
| 965 |
g_strfreev (original_lines); |
| 966 |
g_free (original_content); |
| 967 |
|
| 968 |
if (out_of_bounds) { |
| 969 |
|
| 970 |
/* The computed stream time falls outside of the guesstimated stream time, |
| 971 |
* reassess which segment we really are in */ |
| 972 |
GST_WARNING ("Cue %" GST_STIME_FORMAT " -> %" GST_STIME_FORMAT |
| 973 |
" is outside of segment %" GST_STIME_FORMAT " -> %" |
| 974 |
GST_STIME_FORMAT, |
| 975 |
GST_STIME_ARGS (low_stream_time), |
| 976 |
GST_STIME_ARGS (high_stream_time), |
| 977 |
GST_STIME_ARGS (current_segment->stream_time), |
| 978 |
GST_STIME_ARGS ((GstClockTimeDiff) (current_segment->stream_time + |
| 979 |
current_segment->duration))); |
| 980 |
|
| 981 |
GstM3U8SeekResult seek_result; |
| 982 |
|
| 983 |
if (gst_hls_media_playlist_find_position (hls_stream->playlist, |
| 984 |
low_stream_time, hls_stream->in_partial_segments, &seek_result)) { |
| 985 |
g_assert (seek_result.segment != current_segment); |
| 986 |
GST_DEBUG_OBJECT (hls_stream, |
| 987 |
"Stream time corresponds to segment %" GST_STIME_FORMAT |
| 988 |
" duration %" GST_TIME_FORMAT, |
| 989 |
GST_STIME_ARGS (seek_result.segment->stream_time), |
| 990 |
GST_TIME_ARGS (seek_result.segment->duration)); |
| 991 |
|
| 992 |
/* When we land in the middle of a partial segment, actually |
| 993 |
* use the full segment position to resync the playlist */ |
| 994 |
if (seek_result.found_partial_segment) { |
| 995 |
hls_stream->current_segment->stream_time = |
| 996 |
seek_result.segment->stream_time; |
| 997 |
} else { |
| 998 |
hls_stream->current_segment->stream_time = seek_result.stream_time; |
| 999 |
} |
| 1000 |
|
| 1001 |
/* Recalculate everything and ask parent class to restart */ |
| 1002 |
gst_hls_media_playlist_recalculate_stream_time (hls_stream->playlist, |
| 1003 |
hls_stream->current_segment); |
| 1004 |
gst_m3u8_media_segment_unref (seek_result.segment); |
| 1005 |
ret = GST_HLS_PARSER_RESULT_RESYNC; |
| 1006 |
} |
| 1007 |
} |
| 1008 |
|
| 1009 |
if (!found_text) { |
| 1010 |
GST_DEBUG_OBJECT (hls_stream, "Replacing buffer with droppable buffer"); |
| 1011 |
|
| 1012 |
GST_BUFFER_PTS (*buffer) = |
| 1013 |
current_segment->stream_time + hls_stream->presentation_offset; |
| 1014 |
GST_BUFFER_DURATION (*buffer) = current_segment->duration; |
| 1015 |
|
| 1016 |
gst_buffer_set_flags (*buffer, GST_BUFFER_FLAG_DROPPABLE); |
| 1017 |
} |
| 1018 |
|
| 1019 |
return ret; |
| 1020 |
} |
| 1021 |
|
| 1022 |
/* Get a utf8-validated string of the contents of the buffer */ |
| 1023 |
gchar * |
| 1024 |
gst_hls_buf_to_utf8_text (GstBuffer * buf) |
| 1025 |
{ |
| 1026 |
GstMapInfo info; |
| 1027 |
gchar *playlist; |
| 1028 |
|
| 1029 |
if (!gst_buffer_map (buf, &info, GST_MAP_READ)) |
| 1030 |
goto map_error; |
| 1031 |
|
| 1032 |
if (!g_utf8_validate ((gchar *) info.data, info.size, NULL)) |
| 1033 |
goto validate_error; |
| 1034 |
|
| 1035 |
/* alloc size + 1 to end with a null character */ |
| 1036 |
playlist = g_malloc0 (info.size + 1); |
| 1037 |
memcpy (playlist, info.data, info.size); |
| 1038 |
|
| 1039 |
gst_buffer_unmap (buf, &info); |
| 1040 |
return playlist; |
| 1041 |
|
| 1042 |
validate_error: |
| 1043 |
gst_buffer_unmap (buf, &info); |
| 1044 |
map_error: |
| 1045 |
return NULL; |
| 1046 |
} |