1 /* GStreamer RTMP Library
2 * Copyright (C) 2017 Make.TV, Inc. <info@make.tv>
3 * Contact: Jan Alexander Steffens (heftig) <jsteffens@make.tv>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
18 * Boston, MA 02110-1335, USA.
25 #include "rtmpchunkstream.h"
26 #include "rtmputils.h"
28 GST_DEBUG_CATEGORY_STATIC (gst_rtmp_chunk_stream_debug_category);
29 #define GST_CAT_DEFAULT gst_rtmp_chunk_stream_debug_category
34 static volatile gsize done = 0;
35 if (g_once_init_enter (&done)) {
36 GST_DEBUG_CATEGORY_INIT (gst_rtmp_chunk_stream_debug_category,
37 "rtmpchunkstream", 0, "debug category for rtmp chunk streams");
38 g_once_init_leave (&done, 1);
44 CHUNK_BYTE_TWOBYTE = 0,
45 CHUNK_BYTE_THREEBYTE = 1,
46 CHUNK_BYTE_MASK = 0x3f,
47 CHUNK_STREAM_MIN_TWOBYTE = 0x40,
48 CHUNK_STREAM_MIN_THREEBYTE = 0x140,
49 CHUNK_STREAM_MAX_THREEBYTE = 0x1003f,
60 static const gsize chunk_header_sizes[4] = { 11, 7, 3, 0 };
62 struct _GstRtmpChunkStream
66 GstMapInfo map; /* Only used for parsing */
72 struct _GstRtmpChunkStreams
77 static inline gboolean
78 chunk_stream_is_open (GstRtmpChunkStream * cstream)
80 return cstream->map.data != NULL;
84 chunk_stream_take_buffer (GstRtmpChunkStream * cstream, GstBuffer * buffer)
86 GstRtmpMeta *meta = gst_buffer_get_rtmp_meta (buffer);
88 g_assert (cstream->buffer == NULL);
89 cstream->buffer = buffer;
94 chunk_stream_clear (GstRtmpChunkStream * cstream)
96 if (chunk_stream_is_open (cstream)) {
97 gst_buffer_unmap (cstream->buffer, &cstream->map);
98 cstream->map.data = NULL;
101 gst_buffer_replace (&cstream->buffer, NULL);
102 cstream->meta = NULL;
107 chunk_stream_next_size (GstRtmpChunkStream * cstream, guint32 chunk_size)
109 guint32 size, offset;
111 size = cstream->meta->size;
112 offset = cstream->offset;
114 g_return_val_if_fail (offset <= size, 0);
115 return MIN (size - offset, chunk_size);
118 static inline gboolean
119 needs_ext_ts (GstRtmpMeta * meta)
121 return meta->ts_delta >= 0xffffff;
126 dts_to_abs_ts (GstBuffer * buffer)
128 GstClockTime dts = GST_BUFFER_DTS (buffer);
131 if (GST_CLOCK_TIME_IS_VALID (dts)) {
132 ret = gst_util_uint64_scale_round (dts, 1, GST_MSECOND);
135 GST_TRACE ("Converted DTS %" GST_TIME_FORMAT " into abs TS %"
136 G_GUINT32_FORMAT " ms", GST_TIME_ARGS (dts), ret);
141 dts_diff_to_delta_ts (GstBuffer * old_buffer, GstBuffer * buffer,
144 GstClockTime dts = GST_BUFFER_DTS (buffer),
145 old_dts = GST_BUFFER_DTS (old_buffer);
146 guint32 abs_ts, old_abs_ts, delta_32 = 0;
148 if (!GST_CLOCK_TIME_IS_VALID (dts) || !GST_CLOCK_TIME_IS_VALID (old_dts)) {
149 GST_LOG ("Timestamps not valid; using delta TS 0");
153 if (ABS (GST_CLOCK_DIFF (old_dts, dts)) > GST_MSECOND * G_MAXINT32) {
154 GST_WARNING ("Timestamp delta too large: %" GST_TIME_FORMAT " -> %"
155 GST_TIME_FORMAT, GST_TIME_ARGS (old_dts), GST_TIME_ARGS (dts));
159 abs_ts = gst_util_uint64_scale_round (dts, 1, GST_MSECOND);
160 old_abs_ts = gst_util_uint64_scale_round (old_dts, 1, GST_MSECOND);
162 /* underflow wraps around */
163 delta_32 = abs_ts - old_abs_ts;
165 GST_TRACE ("Converted DTS %" GST_TIME_FORMAT " (%" G_GUINT32_FORMAT
166 " ms) -> %" GST_TIME_FORMAT " (%" G_GUINT32_FORMAT " ms) into delta TS %"
167 G_GUINT32_FORMAT " ms", GST_TIME_ARGS (old_dts), old_abs_ts,
168 GST_TIME_ARGS (dts), abs_ts, delta_32);
176 select_chunk_type (GstRtmpChunkStream * cstream, GstBuffer * buffer)
178 GstBuffer *old_buffer = cstream->buffer;
179 GstRtmpMeta *meta, *old_meta;
181 g_return_val_if_fail (buffer, -1);
183 meta = gst_buffer_get_rtmp_meta (buffer);
185 g_return_val_if_fail (meta, -1);
186 g_return_val_if_fail (gst_rtmp_message_type_is_valid (meta->type), -1);
188 meta->size = gst_buffer_get_size (buffer);
189 meta->cstream = cstream->id;
192 GST_TRACE ("Picking header 0: no previous header");
193 meta->ts_delta = dts_to_abs_ts (buffer);
197 old_meta = gst_buffer_get_rtmp_meta (old_buffer);
199 if (old_meta->mstream != meta->mstream) {
200 GST_TRACE ("Picking header 0: stream mismatch; "
201 "want %" G_GUINT32_FORMAT " got %" G_GUINT32_FORMAT,
202 old_meta->mstream, meta->mstream);
203 meta->ts_delta = dts_to_abs_ts (buffer);
207 if (!dts_diff_to_delta_ts (old_buffer, buffer, &meta->ts_delta)) {
208 GST_TRACE ("Picking header 0: timestamp delta overflow");
209 meta->ts_delta = dts_to_abs_ts (buffer);
213 /* now at least type 1 */
215 if (old_meta->type != meta->type) {
216 GST_TRACE ("Picking header 1: type mismatch; want %d got %d",
217 old_meta->type, meta->type);
221 if (old_meta->size != meta->size) {
222 GST_TRACE ("Picking header 1: size mismatch; "
223 "want %" G_GUINT32_FORMAT " got %" G_GUINT32_FORMAT,
224 old_meta->size, meta->size);
228 /* now at least type 2 */
230 if (old_meta->ts_delta != meta->ts_delta) {
231 GST_TRACE ("Picking header 2: timestamp delta mismatch; "
232 "want %" G_GUINT32_FORMAT " got %" G_GUINT32_FORMAT,
233 old_meta->ts_delta, meta->ts_delta);
237 /* now at least type 3 */
239 GST_TRACE ("Picking header 3");
244 serialize_next (GstRtmpChunkStream * cstream, guint32 chunk_size,
247 GstRtmpMeta *meta = cstream->meta;
248 guint8 small_stream_id;
249 gsize header_size = chunk_header_sizes[type], offset;
254 GST_TRACE ("Serializing a chunk of type %d, offset %" G_GUINT32_FORMAT,
255 type, cstream->offset);
257 if (cstream->id < CHUNK_STREAM_MIN_TWOBYTE) {
258 small_stream_id = cstream->id;
260 } else if (cstream->id < CHUNK_STREAM_MIN_THREEBYTE) {
261 small_stream_id = CHUNK_BYTE_TWOBYTE;
264 small_stream_id = CHUNK_BYTE_THREEBYTE;
268 ext_ts = needs_ext_ts (meta);
273 GST_TRACE ("Allocating buffer, header size %" G_GSIZE_FORMAT, header_size);
275 ret = gst_buffer_new_allocate (NULL, header_size, NULL);
277 GST_ERROR ("Failed to allocate chunk buffer");
281 if (!gst_buffer_map (ret, &map, GST_MAP_WRITE)) {
282 GST_ERROR ("Failed to map %" GST_PTR_FORMAT, ret);
283 gst_buffer_unref (ret);
287 /* Chunk Basic Header */
288 GST_WRITE_UINT8 (map.data, (type << 6) | small_stream_id);
291 switch (small_stream_id) {
292 case CHUNK_BYTE_TWOBYTE:
293 GST_WRITE_UINT8 (map.data + 1, cstream->id - CHUNK_STREAM_MIN_TWOBYTE);
297 case CHUNK_BYTE_THREEBYTE:
298 GST_WRITE_UINT16_LE (map.data + 1,
299 cstream->id - CHUNK_STREAM_MIN_TWOBYTE);
306 /* SRSLY: "Message stream ID is stored in little-endian format." */
307 GST_WRITE_UINT32_LE (map.data + offset + 7, meta->mstream);
310 GST_WRITE_UINT24_BE (map.data + offset + 3, meta->size);
311 GST_WRITE_UINT8 (map.data + offset + 6, meta->type);
314 GST_WRITE_UINT24_BE (map.data + offset,
315 ext_ts ? 0xffffff : meta->ts_delta);
318 offset += chunk_header_sizes[type];
321 GST_WRITE_UINT32_BE (map.data + offset, meta->ts_delta);
326 g_assert (offset == header_size);
327 GST_MEMDUMP (">>> chunk header", map.data, offset);
329 gst_buffer_unmap (ret, &map);
331 GST_BUFFER_OFFSET (ret) = GST_BUFFER_OFFSET_IS_VALID (cstream->buffer) ?
332 GST_BUFFER_OFFSET (cstream->buffer) + cstream->offset : cstream->bytes;
333 GST_BUFFER_OFFSET_END (ret) = GST_BUFFER_OFFSET (ret);
335 if (meta->size > 0) {
336 guint32 payload_size = chunk_stream_next_size (cstream, chunk_size);
338 GST_TRACE ("Appending %" G_GUINT32_FORMAT " bytes of payload",
341 gst_buffer_copy_into (ret, cstream->buffer, GST_BUFFER_COPY_MEMORY,
342 cstream->offset, payload_size);
344 GST_BUFFER_OFFSET_END (ret) += payload_size;
345 cstream->offset += payload_size;
346 cstream->bytes += payload_size;
348 GST_TRACE ("Chunk has no payload");
351 gst_rtmp_buffer_dump (ret, ">>> chunk");
357 gst_rtmp_chunk_stream_clear (GstRtmpChunkStream * cstream)
359 g_return_if_fail (cstream);
360 GST_LOG ("Clearing chunk stream %" G_GUINT32_FORMAT, cstream->id);
361 chunk_stream_clear (cstream);
365 gst_rtmp_chunk_stream_parse_id (const guint8 * data, gsize size)
370 GST_TRACE ("Not enough bytes to read ID");
374 ret = GST_READ_UINT8 (data) & CHUNK_BYTE_MASK;
377 case CHUNK_BYTE_TWOBYTE:
379 GST_TRACE ("Not enough bytes to read two-byte ID");
383 ret = GST_READ_UINT8 (data + 1) + CHUNK_STREAM_MIN_TWOBYTE;
386 case CHUNK_BYTE_THREEBYTE:
388 GST_TRACE ("Not enough bytes to read three-byte ID");
392 ret = GST_READ_UINT16_LE (data + 1) + CHUNK_STREAM_MIN_TWOBYTE;
396 GST_TRACE ("Parsed chunk stream ID %" G_GUINT32_FORMAT, ret);
401 gst_rtmp_chunk_stream_parse_header (GstRtmpChunkStream * cstream,
402 const guint8 * data, gsize size)
406 const guint8 *message_header;
409 gboolean has_abs_timestamp = FALSE;
411 g_return_val_if_fail (cstream, 0);
412 g_return_val_if_fail (cstream->id == gst_rtmp_chunk_stream_parse_id (data,
415 type = GST_READ_UINT8 (data) >> 6;
416 GST_TRACE ("Parsing chunk stream %" G_GUINT32_FORMAT " header type %d",
419 switch (GST_READ_UINT8 (data) & CHUNK_BYTE_MASK) {
420 case CHUNK_BYTE_TWOBYTE:
423 case CHUNK_BYTE_THREEBYTE:
431 message_header = data + header_size;
432 header_size += chunk_header_sizes[type];
434 if (cstream->buffer) {
435 buffer = cstream->buffer;
436 meta = cstream->meta;
437 g_assert (meta->cstream == cstream->id);
439 buffer = gst_buffer_new ();
440 GST_BUFFER_DTS (buffer) = 0;
441 GST_BUFFER_OFFSET (buffer) = cstream->bytes;
442 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
444 meta = gst_buffer_add_rtmp_meta (buffer);
445 meta->cstream = cstream->id;
447 chunk_stream_take_buffer (cstream, buffer);
448 GST_DEBUG ("Starting parse with new %" GST_PTR_FORMAT, buffer);
451 if (size < header_size) {
452 GST_TRACE ("not enough bytes to read header");
458 has_abs_timestamp = TRUE;
459 /* SRSLY: "Message stream ID is stored in little-endian format." */
460 meta->mstream = GST_READ_UINT32_LE (message_header + 7);
463 meta->type = GST_READ_UINT8 (message_header + 6);
464 meta->size = GST_READ_UINT24_BE (message_header + 3);
467 meta->ts_delta = GST_READ_UINT24_BE (message_header);
470 if (needs_ext_ts (meta)) {
473 if (size < header_size + 4) {
474 GST_TRACE ("not enough bytes to read extended timestamp");
475 return header_size + 4;
478 GST_TRACE ("Reading extended timestamp");
479 timestamp = GST_READ_UINT32_BE (data + header_size);
481 if (type == 3 && meta->ts_delta != timestamp) {
482 GST_WARNING ("Type 3 extended timestamp does not match expected"
483 " timestamp (want %" G_GUINT32_FORMAT " got %" G_GUINT32_FORMAT
484 "); assuming it's not present", meta->ts_delta, timestamp);
486 meta->ts_delta = timestamp;
492 GST_MEMDUMP ("<<< chunk header", data, header_size);
494 if (!chunk_stream_is_open (cstream)) {
495 GstClockTime dts = GST_BUFFER_DTS (buffer);
496 guint32 delta_32, abs_32;
499 if (has_abs_timestamp) {
500 abs_32 = meta->ts_delta;
501 delta_32 = abs_32 - dts / GST_MSECOND;
503 delta_32 = meta->ts_delta;
504 abs_32 = delta_32 + dts / GST_MSECOND;
507 GST_TRACE ("Timestamp delta is %" G_GUINT32_FORMAT " (absolute %"
508 G_GUINT32_FORMAT ")", delta_32, abs_32);
510 /* emulate signed overflow */
512 if (delta_64 > G_MAXINT32) {
513 delta_64 -= G_MAXUINT32;
517 delta_64 *= GST_MSECOND;
519 if (G_LIKELY (delta_64 >= 0)) {
520 /* Normal advancement */
521 } else if (G_LIKELY ((guint64) (-delta_64) <= dts)) {
522 /* In-bounds regression */
523 GST_WARNING ("Timestamp regression: %" GST_STIME_FORMAT,
524 GST_STIME_ARGS (delta_64));
526 /* Out-of-bounds regression */
527 GST_WARNING ("Timestamp regression: %" GST_STIME_FORMAT ", offsetting",
528 GST_STIME_ARGS (delta_64));
529 delta_64 = delta_32 * GST_MSECOND;
532 GST_BUFFER_DTS (buffer) += delta_64;
534 GST_TRACE ("Adjusted buffer DTS (%" GST_TIME_FORMAT ") by %"
535 GST_STIME_FORMAT " to %" GST_TIME_FORMAT, GST_TIME_ARGS (dts),
536 GST_STIME_ARGS (delta_64), GST_TIME_ARGS (GST_BUFFER_DTS (buffer)));
538 GST_TRACE ("Message payload already started; not touching timestamp");
545 gst_rtmp_chunk_stream_parse_payload (GstRtmpChunkStream * cstream,
546 guint32 chunk_size, guint8 ** data)
550 g_return_val_if_fail (cstream, 0);
551 g_return_val_if_fail (cstream->buffer, 0);
553 if (!chunk_stream_is_open (cstream)) {
554 guint32 size = cstream->meta->size;
556 GST_TRACE ("Allocating buffer, payload size %" G_GUINT32_FORMAT, size);
558 mem = gst_allocator_alloc (NULL, size, 0);
560 GST_ERROR ("Failed to allocate buffer for payload size %"
561 G_GUINT32_FORMAT, size);
565 gst_buffer_append_memory (cstream->buffer, mem);
566 gst_buffer_map (cstream->buffer, &cstream->map, GST_MAP_WRITE);
569 g_return_val_if_fail (cstream->map.size == cstream->meta->size, 0);
572 *data = cstream->map.data + cstream->offset;
575 return chunk_stream_next_size (cstream, chunk_size);
579 gst_rtmp_chunk_stream_wrote_payload (GstRtmpChunkStream * cstream,
584 g_return_val_if_fail (cstream, FALSE);
585 g_return_val_if_fail (chunk_stream_is_open (cstream), FALSE);
587 size = chunk_stream_next_size (cstream, chunk_size);
588 cstream->offset += size;
589 cstream->bytes += size;
591 return chunk_stream_next_size (cstream, chunk_size);
595 gst_rtmp_chunk_stream_parse_finish (GstRtmpChunkStream * cstream)
597 GstBuffer *buffer, *empty;
599 g_return_val_if_fail (cstream, NULL);
600 g_return_val_if_fail (cstream->buffer, NULL);
602 buffer = gst_buffer_ref (cstream->buffer);
603 GST_BUFFER_OFFSET_END (buffer) = cstream->bytes;
605 gst_rtmp_buffer_dump (buffer, "<<< message");
607 chunk_stream_clear (cstream);
609 empty = gst_buffer_new ();
611 if (!gst_buffer_copy_into (empty, buffer, GST_BUFFER_COPY_META, 0, 0)) {
612 GST_ERROR ("copy_into failed");
616 GST_BUFFER_DTS (empty) = GST_BUFFER_DTS (buffer);
617 GST_BUFFER_OFFSET (empty) = GST_BUFFER_OFFSET_END (buffer);
619 chunk_stream_take_buffer (cstream, empty);
625 gst_rtmp_chunk_stream_serialize_start (GstRtmpChunkStream * cstream,
626 GstBuffer * buffer, guint32 chunk_size)
630 g_return_val_if_fail (cstream, NULL);
631 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
633 type = select_chunk_type (cstream, buffer);
634 g_return_val_if_fail (type >= 0, NULL);
636 GST_TRACE ("Starting serialization of message %" GST_PTR_FORMAT
637 " into stream %" G_GUINT32_FORMAT, buffer, cstream->id);
639 gst_rtmp_buffer_dump (buffer, ">>> message");
641 chunk_stream_clear (cstream);
642 chunk_stream_take_buffer (cstream, buffer);
644 return serialize_next (cstream, chunk_size, type);
648 gst_rtmp_chunk_stream_serialize_next (GstRtmpChunkStream * cstream,
651 g_return_val_if_fail (cstream, NULL);
652 g_return_val_if_fail (cstream->buffer, NULL);
654 if (chunk_stream_next_size (cstream, chunk_size) == 0) {
655 GST_TRACE ("Message serialization finished");
659 GST_TRACE ("Continuing serialization of message %" GST_PTR_FORMAT
660 " into stream %" G_GUINT32_FORMAT, cstream->buffer, cstream->id);
662 return serialize_next (cstream, chunk_size, CHUNK_TYPE_3);
665 GstRtmpChunkStreams *
666 gst_rtmp_chunk_streams_new (void)
668 GstRtmpChunkStreams *cstreams;
672 cstreams = g_slice_new (GstRtmpChunkStreams);
673 cstreams->array = g_array_new (FALSE, TRUE, sizeof (GstRtmpChunkStream));
674 g_array_set_clear_func (cstreams->array,
675 (GDestroyNotify) gst_rtmp_chunk_stream_clear);
680 gst_rtmp_chunk_streams_free (gpointer ptr)
682 GstRtmpChunkStreams *cstreams = ptr;
683 g_clear_pointer (&cstreams->array, g_array_unref);
684 g_slice_free (GstRtmpChunkStreams, cstreams);
688 gst_rtmp_chunk_streams_get (GstRtmpChunkStreams * cstreams, guint32 id)
691 GstRtmpChunkStream *entry;
694 g_return_val_if_fail (cstreams, NULL);
695 g_return_val_if_fail (id > CHUNK_BYTE_THREEBYTE, NULL);
696 g_return_val_if_fail (id <= CHUNK_STREAM_MAX_THREEBYTE, NULL);
698 array = cstreams->array;
700 for (i = 0; i < array->len; i++) {
701 entry = &g_array_index (array, GstRtmpChunkStream, i);
702 if (entry->id == id) {
703 GST_TRACE ("Obtaining chunk stream %" G_GUINT32_FORMAT, id);
708 GST_DEBUG ("Allocating chunk stream %" G_GUINT32_FORMAT, id);
710 g_array_set_size (array, i + 1);
711 entry = &g_array_index (array, GstRtmpChunkStream, i);