1 /* GStreamer RTMP Library
2 * Copyright (C) 2017 Make.TV, Inc. <info@make.tv>
3 * Contact: Jan Alexander Steffens (heftig) <jsteffens@make.tv>
4 *
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.
9 *
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.
14 *
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.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "amf.h"
26 #include "rtmpmessage.h"
27 #include "rtmpchunkstream.h"
28
29 GST_DEBUG_CATEGORY_STATIC (gst_rtmp_message_debug_category);
30 #define GST_CAT_DEFAULT gst_rtmp_message_debug_category
31
32 gboolean
gst_rtmp_message_type_is_valid(GstRtmpMessageType type)33 gst_rtmp_message_type_is_valid (GstRtmpMessageType type)
34 {
35 switch (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:
52 return TRUE;
53 default:
54 return FALSE;
55 }
56 }
57
58 gboolean
gst_rtmp_message_type_is_protocol_control(GstRtmpMessageType type)59 gst_rtmp_message_type_is_protocol_control (GstRtmpMessageType type)
60 {
61 switch (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:
67 return TRUE;
68
69 default:
70 return FALSE;
71 }
72 }
73
74 const gchar *
gst_rtmp_message_type_get_nick(GstRtmpMessageType type)75 gst_rtmp_message_type_get_nick (GstRtmpMessageType type)
76 {
77 switch (type) {
78 case GST_RTMP_MESSAGE_TYPE_INVALID:
79 return "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:
93 return "audio";
94 case GST_RTMP_MESSAGE_TYPE_VIDEO:
95 return "video";
96 case GST_RTMP_MESSAGE_TYPE_DATA_AMF3:
97 return "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:
103 return "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:
109 return "aggregate";
110 default:
111 return "unknown";
112 }
113 }
114
115 const gchar *
gst_rtmp_user_control_type_get_nick(GstRtmpUserControlType type)116 gst_rtmp_user_control_type_get_nick (GstRtmpUserControlType type)
117 {
118 switch (type) {
119 case GST_RTMP_USER_CONTROL_TYPE_STREAM_BEGIN:
120 return "stream-begin";
121 case GST_RTMP_USER_CONTROL_TYPE_STREAM_EOF:
122 return "stream-eof";
123 case GST_RTMP_USER_CONTROL_TYPE_STREAM_DRY:
124 return "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";
141 default:
142 return "unknown";
143 }
144 }
145
146 GType
gst_rtmp_meta_api_get_type(void)147 gst_rtmp_meta_api_get_type (void)
148 {
149 static GType type = 0;
150 static const gchar *tags[] = {
151 NULL
152 };
153
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);
159 }
160 return type;
161 }
162
163 static gboolean
gst_rtmp_meta_init(GstMeta * meta,gpointer params,GstBuffer * buffer)164 gst_rtmp_meta_init (GstMeta * meta, gpointer params, GstBuffer * buffer)
165 {
166 GstRtmpMeta *emeta = (GstRtmpMeta *) meta;
167
168 emeta->cstream = 0;
169 emeta->ts_delta = 0;
170 emeta->size = 0;
171 emeta->type = GST_RTMP_MESSAGE_TYPE_INVALID;
172 emeta->mstream = 0;
173
174 return TRUE;
175 }
176
177 static gboolean
gst_rtmp_meta_transform(GstBuffer * dest,GstMeta * meta,GstBuffer * buffer,GQuark type,gpointer data)178 gst_rtmp_meta_transform (GstBuffer * dest, GstMeta * meta, GstBuffer * buffer,
179 GQuark type, gpointer data)
180 {
181 GstRtmpMeta *smeta, *dmeta;
182
183 if (!GST_META_TRANSFORM_IS_COPY (type)) {
184 /* We only support copy transforms */
185 return FALSE;
186 }
187
188 smeta = (GstRtmpMeta *) meta;
189 dmeta = gst_buffer_get_rtmp_meta (dest);
190 if (!dmeta) {
191 dmeta = gst_buffer_add_rtmp_meta (dest);
192 }
193
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;
199
200 return TRUE;
201 }
202
203 const GstMetaInfo *
gst_rtmp_meta_get_info(void)204 gst_rtmp_meta_get_info (void)
205 {
206 static const GstMetaInfo *rtmp_meta_info = NULL;
207
208 if (g_once_init_enter (&rtmp_meta_info)) {
209 const GstMetaInfo *meta = gst_meta_register (GST_RTMP_META_API_TYPE,
210 "GstRtmpMeta", sizeof (GstRtmpMeta), gst_rtmp_meta_init, NULL,
211 gst_rtmp_meta_transform);
212 g_once_init_leave (&rtmp_meta_info, meta);
213 }
214 return rtmp_meta_info;
215 }
216
217 GstRtmpMeta *
gst_buffer_add_rtmp_meta(GstBuffer * buffer)218 gst_buffer_add_rtmp_meta (GstBuffer * buffer)
219 {
220 GstRtmpMeta *meta;
221
222 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
223
224 meta = (GstRtmpMeta *) gst_buffer_add_meta (buffer, GST_RTMP_META_INFO, NULL);
225 g_assert (meta != NULL);
226
227 return meta;
228 }
229
230 GstBuffer *
gst_rtmp_message_new(GstRtmpMessageType type,guint32 cstream,guint32 mstream)231 gst_rtmp_message_new (GstRtmpMessageType type, guint32 cstream, guint32 mstream)
232 {
233 GstBuffer *buffer = gst_buffer_new ();
234 GstRtmpMeta *meta = gst_buffer_add_rtmp_meta (buffer);
235
236 meta->type = type;
237 meta->cstream = cstream;
238 meta->mstream = mstream;
239
240 return buffer;
241 }
242
243 GstBuffer *
gst_rtmp_message_new_wrapped(GstRtmpMessageType type,guint32 cstream,guint32 mstream,guint8 * data,gsize size)244 gst_rtmp_message_new_wrapped (GstRtmpMessageType type, guint32 cstream,
245 guint32 mstream, guint8 * data, gsize size)
246 {
247 GstBuffer *message = gst_rtmp_message_new (type, cstream, mstream);
248
249 gst_buffer_append_memory (message,
250 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
251
252 return message;
253 }
254
255 void
gst_rtmp_buffer_dump(GstBuffer * buffer,const gchar * prefix)256 gst_rtmp_buffer_dump (GstBuffer * buffer, const gchar * prefix)
257 {
258 GstRtmpMeta *meta;
259 GstMapInfo map;
260
261 if (G_LIKELY (GST_LEVEL_LOG > _gst_debug_min || GST_LEVEL_LOG >
262 gst_debug_category_get_threshold (GST_CAT_DEFAULT))) {
263 return;
264 }
265
266 g_return_if_fail (GST_IS_BUFFER (buffer));
267 g_return_if_fail (prefix);
268
269 GST_LOG ("%s %" GST_PTR_FORMAT, prefix, buffer);
270
271 meta = gst_buffer_get_rtmp_meta (buffer);
272 if (meta) {
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));
277 }
278
279 if (G_LIKELY (GST_LEVEL_MEMDUMP > _gst_debug_min || GST_LEVEL_MEMDUMP >
280 gst_debug_category_get_threshold (GST_CAT_DEFAULT))) {
281 return;
282 }
283
284 if (!gst_buffer_map (buffer, &map, GST_MAP_READ)) {
285 GST_ERROR ("Failed to map %" GST_PTR_FORMAT " for memdump", buffer);
286 return;
287 }
288
289 if (map.size > 0) {
290 GST_MEMDUMP (prefix, map.data, map.size);
291 }
292
293 gst_buffer_unmap (buffer, &map);
294 }
295
296 GstRtmpMessageType
gst_rtmp_message_get_type(GstBuffer * buffer)297 gst_rtmp_message_get_type (GstBuffer * buffer)
298 {
299 GstRtmpMeta *meta = gst_buffer_get_rtmp_meta (buffer);
300 g_return_val_if_fail (meta, GST_RTMP_MESSAGE_TYPE_INVALID);
301 return meta->type;
302 }
303
304 gboolean
gst_rtmp_message_is_protocol_control(GstBuffer * buffer)305 gst_rtmp_message_is_protocol_control (GstBuffer * buffer)
306 {
307 GstRtmpMeta *meta = gst_buffer_get_rtmp_meta (buffer);
308
309 g_return_val_if_fail (meta, FALSE);
310
311 if (!gst_rtmp_message_type_is_protocol_control (meta->type)) {
312 return FALSE;
313 }
314
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);
318 }
319
320 if (meta->mstream != 0) {
321 GST_WARNING ("Protocol control message on message stream %"
322 G_GUINT32_FORMAT ", not 0", meta->mstream);
323 }
324
325 return TRUE;
326 }
327
328 gboolean
gst_rtmp_message_is_user_control(GstBuffer * buffer)329 gst_rtmp_message_is_user_control (GstBuffer * buffer)
330 {
331 GstRtmpMeta *meta = gst_buffer_get_rtmp_meta (buffer);
332
333 g_return_val_if_fail (meta, FALSE);
334
335 if (meta->type != GST_RTMP_MESSAGE_TYPE_USER_CONTROL) {
336 return FALSE;
337 }
338
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);
342 }
343
344 if (meta->mstream != 0) {
345 GST_WARNING ("User control message on message stream %"
346 G_GUINT32_FORMAT ", not 0", meta->mstream);
347 }
348
349 return TRUE;
350 }
351
352 static inline gboolean
pc_has_param2(GstRtmpMessageType type)353 pc_has_param2 (GstRtmpMessageType type)
354 {
355 return type == GST_RTMP_MESSAGE_TYPE_SET_PEER_BANDWIDTH;
356 }
357
358 gboolean
gst_rtmp_message_parse_protocol_control(GstBuffer * buffer,GstRtmpProtocolControl * out)359 gst_rtmp_message_parse_protocol_control (GstBuffer * buffer,
360 GstRtmpProtocolControl * out)
361 {
362 GstRtmpMeta *meta = gst_buffer_get_rtmp_meta (buffer);
363 GstMapInfo map;
364 GstRtmpProtocolControl pc;
365 gsize pc_size = 4;
366 gboolean ret = FALSE;
367
368 g_return_val_if_fail (meta, FALSE);
369 g_return_val_if_fail (gst_rtmp_message_type_is_protocol_control (meta->type),
370 FALSE);
371
372 if (!gst_buffer_map (buffer, &map, GST_MAP_READ)) {
373 GST_ERROR ("can't map protocol control message");
374 return FALSE;
375 }
376
377 pc.type = meta->type;
378 pc_size = pc_has_param2 (pc.type) ? 5 : 4;
379
380 if (map.size < pc_size) {
381 GST_ERROR ("can't read protocol control param");
382 goto err;
383 } else if (map.size > pc_size) {
384 GST_WARNING ("overlength protocol control: %" G_GSIZE_FORMAT " > %"
385 G_GSIZE_FORMAT, map.size, pc_size);
386 }
387
388 pc.param = GST_READ_UINT32_BE (map.data);
389 pc.param2 = pc_has_param2 (pc.type) ? GST_READ_UINT8 (map.data + 4) : 0;
390
391 ret = TRUE;
392 if (out) {
393 *out = pc;
394 }
395
396 err:
397 gst_buffer_unmap (buffer, &map);
398 return ret;
399 }
400
401 GstBuffer *
gst_rtmp_message_new_protocol_control(GstRtmpProtocolControl * pc)402 gst_rtmp_message_new_protocol_control (GstRtmpProtocolControl * pc)
403 {
404 guint8 *data;
405 gsize size;
406
407 g_return_val_if_fail (pc, NULL);
408 g_return_val_if_fail (gst_rtmp_message_type_is_protocol_control (pc->type),
409 NULL);
410
411 size = pc_has_param2 (pc->type) ? 5 : 4;
412
413 data = g_malloc (size);
414 GST_WRITE_UINT32_BE (data, pc->param);
415 if (pc_has_param2 (pc->type)) {
416 GST_WRITE_UINT8 (data + 4, pc->param2);
417 }
418
419 return gst_rtmp_message_new_wrapped (pc->type,
420 GST_RTMP_CHUNK_STREAM_PROTOCOL, 0, data, size);
421 }
422
423 static inline gboolean
uc_has_param2(GstRtmpUserControlType type)424 uc_has_param2 (GstRtmpUserControlType type)
425 {
426 return type == GST_RTMP_USER_CONTROL_TYPE_SET_BUFFER_LENGTH;
427 }
428
429 gboolean
gst_rtmp_message_parse_user_control(GstBuffer * buffer,GstRtmpUserControl * out)430 gst_rtmp_message_parse_user_control (GstBuffer * buffer,
431 GstRtmpUserControl * out)
432 {
433 GstRtmpMeta *meta = gst_buffer_get_rtmp_meta (buffer);
434 GstMapInfo map;
435 GstRtmpUserControl uc;
436 gsize uc_size;
437 gboolean ret = FALSE;
438
439 g_return_val_if_fail (meta, FALSE);
440 g_return_val_if_fail (meta->type == GST_RTMP_MESSAGE_TYPE_USER_CONTROL,
441 FALSE);
442
443 if (!gst_buffer_map (buffer, &map, GST_MAP_READ)) {
444 GST_ERROR ("can't map user control message");
445 return FALSE;
446 }
447
448 if (map.size < 2) {
449 GST_ERROR ("can't read user control type");
450 goto err;
451 }
452
453 uc.type = GST_READ_UINT16_BE (map.data);
454 uc_size = uc_has_param2 (uc.type) ? 10 : 6;
455
456 if (map.size < uc_size) {
457 GST_ERROR ("can't read user control param");
458 goto err;
459 } else if (map.size > uc_size) {
460 GST_WARNING ("overlength user control: %" G_GSIZE_FORMAT " > %"
461 G_GSIZE_FORMAT, map.size, uc_size);
462 }
463
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;
466
467 ret = TRUE;
468 if (out) {
469 *out = uc;
470 }
471
472 err:
473 gst_buffer_unmap (buffer, &map);
474 return ret;
475 }
476
477 GstBuffer *
gst_rtmp_message_new_user_control(GstRtmpUserControl * uc)478 gst_rtmp_message_new_user_control (GstRtmpUserControl * uc)
479 {
480 guint8 *data;
481 gsize size;
482
483 g_return_val_if_fail (uc, NULL);
484
485 size = uc_has_param2 (uc->type) ? 10 : 6;
486
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);
492 }
493
494 return gst_rtmp_message_new_wrapped (GST_RTMP_MESSAGE_TYPE_USER_CONTROL,
495 GST_RTMP_CHUNK_STREAM_PROTOCOL, 0, data, size);
496 }
497
498 gboolean
gst_rtmp_message_is_metadata(GstBuffer * buffer)499 gst_rtmp_message_is_metadata (GstBuffer * buffer)
500 {
501 GstRtmpMeta *meta = gst_buffer_get_rtmp_meta (buffer);
502 GstMapInfo map;
503 GstAmfNode *node;
504 gboolean ret = FALSE;
505
506 g_return_val_if_fail (meta, FALSE);
507
508 if (meta->type != GST_RTMP_MESSAGE_TYPE_DATA_AMF0) {
509 return FALSE;
510 }
511
512 if (!gst_buffer_map (buffer, &map, GST_MAP_READ)) {
513 GST_ERROR ("can't map metadata message");
514 return FALSE;
515 }
516
517 node = gst_amf_node_parse (map.data, map.size, NULL);
518 if (!node) {
519 GST_ERROR ("can't read metadata name");
520 goto err;
521 }
522
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);
528 break;
529 }
530
531 default:
532 break;
533 }
534
535 gst_amf_node_free (node);
536
537 err:
538 gst_buffer_unmap (buffer, &map);
539 return ret;
540 }
541