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 St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
26 #include "rtmpmessage.h"
27 #include "rtmpchunkstream.h"
29 GST_DEBUG_CATEGORY_STATIC (gst_rtmp_message_debug_category);
30 #define GST_CAT_DEFAULT gst_rtmp_message_debug_category
33 gst_rtmp_message_type_is_valid (GstRtmpMessageType type)
36 case GST_RTMP_MESSAGE_TYPE_INVALID:
37 case GST_RTMP_MESSAGE_TYPE_SET_CHUNK_SIZE:
38 case GST_RTMP_MESSAGE_TYPE_ABORT_MESSAGE:
39 case GST_RTMP_MESSAGE_TYPE_ACKNOWLEDGEMENT:
40 case GST_RTMP_MESSAGE_TYPE_USER_CONTROL:
41 case GST_RTMP_MESSAGE_TYPE_WINDOW_ACK_SIZE:
42 case GST_RTMP_MESSAGE_TYPE_SET_PEER_BANDWIDTH:
43 case GST_RTMP_MESSAGE_TYPE_AUDIO:
44 case GST_RTMP_MESSAGE_TYPE_VIDEO:
45 case GST_RTMP_MESSAGE_TYPE_DATA_AMF3:
46 case GST_RTMP_MESSAGE_TYPE_SHARED_OBJECT_AMF3:
47 case GST_RTMP_MESSAGE_TYPE_COMMAND_AMF3:
48 case GST_RTMP_MESSAGE_TYPE_DATA_AMF0:
49 case GST_RTMP_MESSAGE_TYPE_SHARED_OBJECT_AMF0:
50 case GST_RTMP_MESSAGE_TYPE_COMMAND_AMF0:
51 case GST_RTMP_MESSAGE_TYPE_AGGREGATE:
59 gst_rtmp_message_type_is_protocol_control (GstRtmpMessageType type)
62 case GST_RTMP_MESSAGE_TYPE_SET_CHUNK_SIZE:
63 case GST_RTMP_MESSAGE_TYPE_ABORT_MESSAGE:
64 case GST_RTMP_MESSAGE_TYPE_ACKNOWLEDGEMENT:
65 case GST_RTMP_MESSAGE_TYPE_WINDOW_ACK_SIZE:
66 case GST_RTMP_MESSAGE_TYPE_SET_PEER_BANDWIDTH:
75 gst_rtmp_message_type_get_nick (GstRtmpMessageType type)
78 case GST_RTMP_MESSAGE_TYPE_INVALID:
80 case GST_RTMP_MESSAGE_TYPE_SET_CHUNK_SIZE:
81 return "set-chunk-size";
82 case GST_RTMP_MESSAGE_TYPE_ABORT_MESSAGE:
83 return "abort-message";
84 case GST_RTMP_MESSAGE_TYPE_ACKNOWLEDGEMENT:
85 return "acknowledgement";
86 case GST_RTMP_MESSAGE_TYPE_USER_CONTROL:
87 return "user-control";
88 case GST_RTMP_MESSAGE_TYPE_WINDOW_ACK_SIZE:
89 return "window-ack-size";
90 case GST_RTMP_MESSAGE_TYPE_SET_PEER_BANDWIDTH:
91 return "set-peer-bandwidth";
92 case GST_RTMP_MESSAGE_TYPE_AUDIO:
94 case GST_RTMP_MESSAGE_TYPE_VIDEO:
96 case GST_RTMP_MESSAGE_TYPE_DATA_AMF3:
98 case GST_RTMP_MESSAGE_TYPE_SHARED_OBJECT_AMF3:
99 return "shared-object-amf3";
100 case GST_RTMP_MESSAGE_TYPE_COMMAND_AMF3:
101 return "command-amf3";
102 case GST_RTMP_MESSAGE_TYPE_DATA_AMF0:
104 case GST_RTMP_MESSAGE_TYPE_SHARED_OBJECT_AMF0:
105 return "shared-object-amf0";
106 case GST_RTMP_MESSAGE_TYPE_COMMAND_AMF0:
107 return "command-amf0";
108 case GST_RTMP_MESSAGE_TYPE_AGGREGATE:
116 gst_rtmp_user_control_type_get_nick (GstRtmpUserControlType type)
119 case GST_RTMP_USER_CONTROL_TYPE_STREAM_BEGIN:
120 return "stream-begin";
121 case GST_RTMP_USER_CONTROL_TYPE_STREAM_EOF:
123 case GST_RTMP_USER_CONTROL_TYPE_STREAM_DRY:
125 case GST_RTMP_USER_CONTROL_TYPE_SET_BUFFER_LENGTH:
126 return "set-buffer-length";
127 case GST_RTMP_USER_CONTROL_TYPE_STREAM_IS_RECORDED:
128 return "stream-is-recorded";
129 case GST_RTMP_USER_CONTROL_TYPE_PING_REQUEST:
130 return "ping-request";
131 case GST_RTMP_USER_CONTROL_TYPE_PING_RESPONSE:
132 return "ping-response";
133 case GST_RTMP_USER_CONTROL_TYPE_SWF_VERIFICATION_REQUEST:
134 return "swf-verification-request";
135 case GST_RTMP_USER_CONTROL_TYPE_SWF_VERIFICATION_RESPONSE:
136 return "swf-verification-response";
137 case GST_RTMP_USER_CONTROL_TYPE_BUFFER_EMPTY:
138 return "buffer-empty";
139 case GST_RTMP_USER_CONTROL_TYPE_BUFFER_READY:
140 return "buffer-ready";
147 gst_rtmp_meta_api_get_type (void)
149 static GType type = 0;
150 static const gchar *tags[] = {
154 if (g_once_init_enter (&type)) {
155 GType _type = gst_meta_api_type_register ("GstRtmpMetaAPI", tags);
156 GST_DEBUG_CATEGORY_INIT (gst_rtmp_message_debug_category,
157 "rtmpmessage", 0, "debug category for rtmp messages");
158 g_once_init_leave (&type, _type);
164 gst_rtmp_meta_init (GstMeta * meta, gpointer params, GstBuffer * buffer)
166 GstRtmpMeta *emeta = (GstRtmpMeta *) meta;
171 emeta->type = GST_RTMP_MESSAGE_TYPE_INVALID;
178 gst_rtmp_meta_transform (GstBuffer * dest, GstMeta * meta, GstBuffer * buffer,
179 GQuark type, gpointer data)
181 GstRtmpMeta *smeta, *dmeta;
183 if (!GST_META_TRANSFORM_IS_COPY (type)) {
184 /* We only support copy transforms */
188 smeta = (GstRtmpMeta *) meta;
189 dmeta = gst_buffer_get_rtmp_meta (dest);
191 dmeta = gst_buffer_add_rtmp_meta (dest);
194 dmeta->cstream = smeta->cstream;
195 dmeta->ts_delta = smeta->ts_delta;
196 dmeta->size = smeta->size;
197 dmeta->type = smeta->type;
198 dmeta->mstream = smeta->mstream;
204 gst_rtmp_meta_get_info (void)
206 static const GstMetaInfo *rtmp_meta_info = NULL;
208 if (g_once_init_enter (&rtmp_meta_info)) {
209 const GstMetaInfo *meta = gst_meta_register (GST_RTMP_META_API_TYPE,
210 "GstRtmpMeta", sizeof *meta, gst_rtmp_meta_init, NULL,
211 gst_rtmp_meta_transform);
212 g_once_init_leave (&rtmp_meta_info, meta);
214 return rtmp_meta_info;
218 gst_buffer_add_rtmp_meta (GstBuffer * buffer)
222 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
224 meta = (GstRtmpMeta *) gst_buffer_add_meta (buffer, GST_RTMP_META_INFO, NULL);
225 g_assert (meta != NULL);
231 gst_rtmp_message_new (GstRtmpMessageType type, guint32 cstream, guint32 mstream)
233 GstBuffer *buffer = gst_buffer_new ();
234 GstRtmpMeta *meta = gst_buffer_add_rtmp_meta (buffer);
237 meta->cstream = cstream;
238 meta->mstream = mstream;
244 gst_rtmp_message_new_wrapped (GstRtmpMessageType type, guint32 cstream,
245 guint32 mstream, guint8 * data, gsize size)
247 GstBuffer *message = gst_rtmp_message_new (type, cstream, mstream);
249 gst_buffer_append_memory (message,
250 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
256 gst_rtmp_buffer_dump (GstBuffer * buffer, const gchar * prefix)
261 if (G_LIKELY (GST_LEVEL_LOG > _gst_debug_min || GST_LEVEL_LOG >
262 gst_debug_category_get_threshold (GST_CAT_DEFAULT))) {
266 g_return_if_fail (GST_IS_BUFFER (buffer));
267 g_return_if_fail (prefix);
269 GST_LOG ("%s %" GST_PTR_FORMAT, prefix, buffer);
271 meta = gst_buffer_get_rtmp_meta (buffer);
273 GST_LOG ("%s cstream:%-4" G_GUINT32_FORMAT " mstream:%-4" G_GUINT32_FORMAT
274 " ts:%-8" G_GUINT32_FORMAT " len:%-6" G_GUINT32_FORMAT " type:%s",
275 prefix, meta->cstream, meta->mstream, meta->ts_delta, meta->size,
276 gst_rtmp_message_type_get_nick (meta->type));
279 if (G_LIKELY (GST_LEVEL_MEMDUMP > _gst_debug_min || GST_LEVEL_MEMDUMP >
280 gst_debug_category_get_threshold (GST_CAT_DEFAULT))) {
284 if (!gst_buffer_map (buffer, &map, GST_MAP_READ)) {
285 GST_ERROR ("Failed to map %" GST_PTR_FORMAT " for memdump", buffer);
290 GST_MEMDUMP (prefix, map.data, map.size);
293 gst_buffer_unmap (buffer, &map);
297 gst_rtmp_message_get_type (GstBuffer * buffer)
299 GstRtmpMeta *meta = gst_buffer_get_rtmp_meta (buffer);
300 g_return_val_if_fail (meta, GST_RTMP_MESSAGE_TYPE_INVALID);
305 gst_rtmp_message_is_protocol_control (GstBuffer * buffer)
307 GstRtmpMeta *meta = gst_buffer_get_rtmp_meta (buffer);
309 g_return_val_if_fail (meta, FALSE);
311 if (!gst_rtmp_message_type_is_protocol_control (meta->type)) {
315 if (meta->cstream != GST_RTMP_CHUNK_STREAM_PROTOCOL) {
316 GST_WARNING ("Protocol control message on chunk stream %"
317 G_GUINT32_FORMAT ", not 2", meta->cstream);
320 if (meta->mstream != 0) {
321 GST_WARNING ("Protocol control message on message stream %"
322 G_GUINT32_FORMAT ", not 0", meta->mstream);
329 gst_rtmp_message_is_user_control (GstBuffer * buffer)
331 GstRtmpMeta *meta = gst_buffer_get_rtmp_meta (buffer);
333 g_return_val_if_fail (meta, FALSE);
335 if (meta->type != GST_RTMP_MESSAGE_TYPE_USER_CONTROL) {
339 if (meta->cstream != GST_RTMP_CHUNK_STREAM_PROTOCOL) {
340 GST_WARNING ("User control message on chunk stream %"
341 G_GUINT32_FORMAT ", not 2", meta->cstream);
344 if (meta->mstream != 0) {
345 GST_WARNING ("User control message on message stream %"
346 G_GUINT32_FORMAT ", not 0", meta->mstream);
352 static inline gboolean
353 pc_has_param2 (GstRtmpMessageType type)
355 return type == GST_RTMP_MESSAGE_TYPE_SET_PEER_BANDWIDTH;
359 gst_rtmp_message_parse_protocol_control (GstBuffer * buffer,
360 GstRtmpProtocolControl * out)
362 GstRtmpMeta *meta = gst_buffer_get_rtmp_meta (buffer);
364 GstRtmpProtocolControl pc;
366 gboolean ret = FALSE;
368 g_return_val_if_fail (meta, FALSE);
369 g_return_val_if_fail (gst_rtmp_message_type_is_protocol_control (meta->type),
372 if (!gst_buffer_map (buffer, &map, GST_MAP_READ)) {
373 GST_ERROR ("can't map protocol control message");
377 pc.type = meta->type;
378 pc_size = pc_has_param2 (pc.type) ? 5 : 4;
380 if (map.size < pc_size) {
381 GST_ERROR ("can't read protocol control param");
383 } else if (map.size > pc_size) {
384 GST_WARNING ("overlength protocol control: %" G_GSIZE_FORMAT " > %"
385 G_GSIZE_FORMAT, map.size, pc_size);
388 pc.param = GST_READ_UINT32_BE (map.data);
389 pc.param2 = pc_has_param2 (pc.type) ? GST_READ_UINT8 (map.data + 4) : 0;
397 gst_buffer_unmap (buffer, &map);
402 gst_rtmp_message_new_protocol_control (GstRtmpProtocolControl * pc)
407 g_return_val_if_fail (pc, NULL);
408 g_return_val_if_fail (gst_rtmp_message_type_is_protocol_control (pc->type),
411 size = pc_has_param2 (pc->type) ? 5 : 4;
413 data = g_malloc (size);
414 GST_WRITE_UINT32_BE (data, pc->param);
415 if (pc_has_param2 (pc->type)) {
416 GST_WRITE_UINT32_BE (data + 4, pc->param2);
419 return gst_rtmp_message_new_wrapped (pc->type,
420 GST_RTMP_CHUNK_STREAM_PROTOCOL, 0, data, size);
423 static inline gboolean
424 uc_has_param2 (GstRtmpUserControlType type)
426 return type == GST_RTMP_USER_CONTROL_TYPE_SET_BUFFER_LENGTH;
430 gst_rtmp_message_parse_user_control (GstBuffer * buffer,
431 GstRtmpUserControl * out)
433 GstRtmpMeta *meta = gst_buffer_get_rtmp_meta (buffer);
435 GstRtmpUserControl uc;
437 gboolean ret = FALSE;
439 g_return_val_if_fail (meta, FALSE);
440 g_return_val_if_fail (meta->type == GST_RTMP_MESSAGE_TYPE_USER_CONTROL,
443 if (!gst_buffer_map (buffer, &map, GST_MAP_READ)) {
444 GST_ERROR ("can't map user control message");
449 GST_ERROR ("can't read user control type");
453 uc.type = GST_READ_UINT16_BE (map.data);
454 uc_size = uc_has_param2 (uc.type) ? 10 : 6;
456 if (map.size < uc_size) {
457 GST_ERROR ("can't read user control param");
459 } else if (map.size > uc_size) {
460 GST_WARNING ("overlength user control: %" G_GSIZE_FORMAT " > %"
461 G_GSIZE_FORMAT, map.size, uc_size);
464 uc.param = GST_READ_UINT32_BE (map.data + 2);
465 uc.param2 = uc_has_param2 (uc.type) ? GST_READ_UINT32_BE (map.data + 6) : 0;
473 gst_buffer_unmap (buffer, &map);
478 gst_rtmp_message_new_user_control (GstRtmpUserControl * uc)
483 g_return_val_if_fail (uc, NULL);
485 size = uc_has_param2 (uc->type) ? 10 : 6;
487 data = g_malloc (size);
488 GST_WRITE_UINT16_BE (data, uc->type);
489 GST_WRITE_UINT32_BE (data + 2, uc->param);
490 if (uc_has_param2 (uc->type)) {
491 GST_WRITE_UINT32_BE (data + 6, uc->param2);
494 return gst_rtmp_message_new_wrapped (GST_RTMP_MESSAGE_TYPE_USER_CONTROL,
495 GST_RTMP_CHUNK_STREAM_PROTOCOL, 0, data, size);
499 gst_rtmp_message_is_metadata (GstBuffer * buffer)
501 GstRtmpMeta *meta = gst_buffer_get_rtmp_meta (buffer);
504 gboolean ret = FALSE;
506 g_return_val_if_fail (meta, FALSE);
508 if (meta->type != GST_RTMP_MESSAGE_TYPE_DATA_AMF0) {
512 if (!gst_buffer_map (buffer, &map, GST_MAP_READ)) {
513 GST_ERROR ("can't map metadata message");
517 node = gst_amf_node_parse (map.data, map.size, NULL);
519 GST_ERROR ("can't read metadata name");
523 switch (gst_amf_node_get_type (node)) {
524 case GST_AMF_TYPE_STRING:
525 case GST_AMF_TYPE_LONG_STRING:{
526 const gchar *name = gst_amf_node_peek_string (node, NULL);
527 ret = (strcmp (name, "onMetaData") == 0);
535 gst_amf_node_free (node);
538 gst_buffer_unmap (buffer, &map);