summaryrefslogtreecommitdiff
path: root/gst/rtmp2/rtmp/rtmpmessage.c
blob: 95225e68f7c6a74dab892ee91622f3d9fe9258fe (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/* GStreamer RTMP Library
 * Copyright (C) 2017 Make.TV, Inc. <info@make.tv>
 *   Contact: Jan Alexander Steffens (heftig) <jsteffens@make.tv>
 *
 * 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 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; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "amf.h"
#include "rtmpmessage.h"
#include "rtmpchunkstream.h"

GST_DEBUG_CATEGORY_STATIC (gst_rtmp_message_debug_category);
#define GST_CAT_DEFAULT gst_rtmp_message_debug_category

gboolean
gst_rtmp_message_type_is_valid (GstRtmpMessageType type)
{
  switch (type) {
    case GST_RTMP_MESSAGE_TYPE_INVALID:
    case GST_RTMP_MESSAGE_TYPE_SET_CHUNK_SIZE:
    case GST_RTMP_MESSAGE_TYPE_ABORT_MESSAGE:
    case GST_RTMP_MESSAGE_TYPE_ACKNOWLEDGEMENT:
    case GST_RTMP_MESSAGE_TYPE_USER_CONTROL:
    case GST_RTMP_MESSAGE_TYPE_WINDOW_ACK_SIZE:
    case GST_RTMP_MESSAGE_TYPE_SET_PEER_BANDWIDTH:
    case GST_RTMP_MESSAGE_TYPE_AUDIO:
    case GST_RTMP_MESSAGE_TYPE_VIDEO:
    case GST_RTMP_MESSAGE_TYPE_DATA_AMF3:
    case GST_RTMP_MESSAGE_TYPE_SHARED_OBJECT_AMF3:
    case GST_RTMP_MESSAGE_TYPE_COMMAND_AMF3:
    case GST_RTMP_MESSAGE_TYPE_DATA_AMF0:
    case GST_RTMP_MESSAGE_TYPE_SHARED_OBJECT_AMF0:
    case GST_RTMP_MESSAGE_TYPE_COMMAND_AMF0:
    case GST_RTMP_MESSAGE_TYPE_AGGREGATE:
      return TRUE;
    default:
      return FALSE;
  }
}

gboolean
gst_rtmp_message_type_is_protocol_control (GstRtmpMessageType type)
{
  switch (type) {
    case GST_RTMP_MESSAGE_TYPE_SET_CHUNK_SIZE:
    case GST_RTMP_MESSAGE_TYPE_ABORT_MESSAGE:
    case GST_RTMP_MESSAGE_TYPE_ACKNOWLEDGEMENT:
    case GST_RTMP_MESSAGE_TYPE_WINDOW_ACK_SIZE:
    case GST_RTMP_MESSAGE_TYPE_SET_PEER_BANDWIDTH:
      return TRUE;

    default:
      return FALSE;
  }
}

const gchar *
gst_rtmp_message_type_get_nick (GstRtmpMessageType type)
{
  switch (type) {
    case GST_RTMP_MESSAGE_TYPE_INVALID:
      return "invalid";
    case GST_RTMP_MESSAGE_TYPE_SET_CHUNK_SIZE:
      return "set-chunk-size";
    case GST_RTMP_MESSAGE_TYPE_ABORT_MESSAGE:
      return "abort-message";
    case GST_RTMP_MESSAGE_TYPE_ACKNOWLEDGEMENT:
      return "acknowledgement";
    case GST_RTMP_MESSAGE_TYPE_USER_CONTROL:
      return "user-control";
    case GST_RTMP_MESSAGE_TYPE_WINDOW_ACK_SIZE:
      return "window-ack-size";
    case GST_RTMP_MESSAGE_TYPE_SET_PEER_BANDWIDTH:
      return "set-peer-bandwidth";
    case GST_RTMP_MESSAGE_TYPE_AUDIO:
      return "audio";
    case GST_RTMP_MESSAGE_TYPE_VIDEO:
      return "video";
    case GST_RTMP_MESSAGE_TYPE_DATA_AMF3:
      return "data-amf3";
    case GST_RTMP_MESSAGE_TYPE_SHARED_OBJECT_AMF3:
      return "shared-object-amf3";
    case GST_RTMP_MESSAGE_TYPE_COMMAND_AMF3:
      return "command-amf3";
    case GST_RTMP_MESSAGE_TYPE_DATA_AMF0:
      return "data-amf0";
    case GST_RTMP_MESSAGE_TYPE_SHARED_OBJECT_AMF0:
      return "shared-object-amf0";
    case GST_RTMP_MESSAGE_TYPE_COMMAND_AMF0:
      return "command-amf0";
    case GST_RTMP_MESSAGE_TYPE_AGGREGATE:
      return "aggregate";
    default:
      return "unknown";
  }
}

const gchar *
gst_rtmp_user_control_type_get_nick (GstRtmpUserControlType type)
{
  switch (type) {
    case GST_RTMP_USER_CONTROL_TYPE_STREAM_BEGIN:
      return "stream-begin";
    case GST_RTMP_USER_CONTROL_TYPE_STREAM_EOF:
      return "stream-eof";
    case GST_RTMP_USER_CONTROL_TYPE_STREAM_DRY:
      return "stream-dry";
    case GST_RTMP_USER_CONTROL_TYPE_SET_BUFFER_LENGTH:
      return "set-buffer-length";
    case GST_RTMP_USER_CONTROL_TYPE_STREAM_IS_RECORDED:
      return "stream-is-recorded";
    case GST_RTMP_USER_CONTROL_TYPE_PING_REQUEST:
      return "ping-request";
    case GST_RTMP_USER_CONTROL_TYPE_PING_RESPONSE:
      return "ping-response";
    case GST_RTMP_USER_CONTROL_TYPE_SWF_VERIFICATION_REQUEST:
      return "swf-verification-request";
    case GST_RTMP_USER_CONTROL_TYPE_SWF_VERIFICATION_RESPONSE:
      return "swf-verification-response";
    case GST_RTMP_USER_CONTROL_TYPE_BUFFER_EMPTY:
      return "buffer-empty";
    case GST_RTMP_USER_CONTROL_TYPE_BUFFER_READY:
      return "buffer-ready";
    default:
      return "unknown";
  }
}

GType
gst_rtmp_meta_api_get_type (void)
{
  static GType type = 0;
  static const gchar *tags[] = {
    NULL
  };

  if (g_once_init_enter (&type)) {
    GType _type = gst_meta_api_type_register ("GstRtmpMetaAPI", tags);
    GST_DEBUG_CATEGORY_INIT (gst_rtmp_message_debug_category,
        "rtmpmessage", 0, "debug category for rtmp messages");
    g_once_init_leave (&type, _type);
  }
  return type;
}

static gboolean
gst_rtmp_meta_init (GstMeta * meta, gpointer params, GstBuffer * buffer)
{
  GstRtmpMeta *emeta = (GstRtmpMeta *) meta;

  emeta->cstream = 0;
  emeta->ts_delta = 0;
  emeta->size = 0;
  emeta->type = GST_RTMP_MESSAGE_TYPE_INVALID;
  emeta->mstream = 0;

  return TRUE;
}

static gboolean
gst_rtmp_meta_transform (GstBuffer * dest, GstMeta * meta, GstBuffer * buffer,
    GQuark type, gpointer data)
{
  GstRtmpMeta *smeta, *dmeta;

  if (!GST_META_TRANSFORM_IS_COPY (type)) {
    /* We only support copy transforms */
    return FALSE;
  }

  smeta = (GstRtmpMeta *) meta;
  dmeta = gst_buffer_get_rtmp_meta (dest);
  if (!dmeta) {
    dmeta = gst_buffer_add_rtmp_meta (dest);
  }

  dmeta->cstream = smeta->cstream;
  dmeta->ts_delta = smeta->ts_delta;
  dmeta->size = smeta->size;
  dmeta->type = smeta->type;
  dmeta->mstream = smeta->mstream;

  return TRUE;
}

const GstMetaInfo *
gst_rtmp_meta_get_info (void)
{
  static const GstMetaInfo *rtmp_meta_info = NULL;

  if (g_once_init_enter (&rtmp_meta_info)) {
    const GstMetaInfo *meta = gst_meta_register (GST_RTMP_META_API_TYPE,
        "GstRtmpMeta", sizeof *meta, gst_rtmp_meta_init, NULL,
        gst_rtmp_meta_transform);
    g_once_init_leave (&rtmp_meta_info, meta);
  }
  return rtmp_meta_info;
}

GstRtmpMeta *
gst_buffer_add_rtmp_meta (GstBuffer * buffer)
{
  GstRtmpMeta *meta;

  g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);

  meta = (GstRtmpMeta *) gst_buffer_add_meta (buffer, GST_RTMP_META_INFO, NULL);
  g_assert (meta != NULL);

  return meta;
}

GstBuffer *
gst_rtmp_message_new (GstRtmpMessageType type, guint32 cstream, guint32 mstream)
{
  GstBuffer *buffer = gst_buffer_new ();
  GstRtmpMeta *meta = gst_buffer_add_rtmp_meta (buffer);

  meta->type = type;
  meta->cstream = cstream;
  meta->mstream = mstream;

  return buffer;
}

GstBuffer *
gst_rtmp_message_new_wrapped (GstRtmpMessageType type, guint32 cstream,
    guint32 mstream, guint8 * data, gsize size)
{
  GstBuffer *message = gst_rtmp_message_new (type, cstream, mstream);

  gst_buffer_append_memory (message,
      gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));

  return message;
}

void
gst_rtmp_buffer_dump (GstBuffer * buffer, const gchar * prefix)
{
  GstRtmpMeta *meta;
  GstMapInfo map;

  if (G_LIKELY (GST_LEVEL_LOG > _gst_debug_min || GST_LEVEL_LOG >
          gst_debug_category_get_threshold (GST_CAT_DEFAULT))) {
    return;
  }

  g_return_if_fail (GST_IS_BUFFER (buffer));
  g_return_if_fail (prefix);

  GST_LOG ("%s %" GST_PTR_FORMAT, prefix, buffer);

  meta = gst_buffer_get_rtmp_meta (buffer);
  if (meta) {
    GST_LOG ("%s cstream:%-4" G_GUINT32_FORMAT " mstream:%-4" G_GUINT32_FORMAT
        " ts:%-8" G_GUINT32_FORMAT " len:%-6" G_GUINT32_FORMAT " type:%s",
        prefix, meta->cstream, meta->mstream, meta->ts_delta, meta->size,
        gst_rtmp_message_type_get_nick (meta->type));
  }

  if (G_LIKELY (GST_LEVEL_MEMDUMP > _gst_debug_min || GST_LEVEL_MEMDUMP >
          gst_debug_category_get_threshold (GST_CAT_DEFAULT))) {
    return;
  }

  if (!gst_buffer_map (buffer, &map, GST_MAP_READ)) {
    GST_ERROR ("Failed to map %" GST_PTR_FORMAT " for memdump", buffer);
    return;
  }

  if (map.size > 0) {
    GST_MEMDUMP (prefix, map.data, map.size);
  }

  gst_buffer_unmap (buffer, &map);
}

GstRtmpMessageType
gst_rtmp_message_get_type (GstBuffer * buffer)
{
  GstRtmpMeta *meta = gst_buffer_get_rtmp_meta (buffer);
  g_return_val_if_fail (meta, GST_RTMP_MESSAGE_TYPE_INVALID);
  return meta->type;
}

gboolean
gst_rtmp_message_is_protocol_control (GstBuffer * buffer)
{
  GstRtmpMeta *meta = gst_buffer_get_rtmp_meta (buffer);

  g_return_val_if_fail (meta, FALSE);

  if (!gst_rtmp_message_type_is_protocol_control (meta->type)) {
    return FALSE;
  }

  if (meta->cstream != GST_RTMP_CHUNK_STREAM_PROTOCOL) {
    GST_WARNING ("Protocol control message on chunk stream %"
        G_GUINT32_FORMAT ", not 2", meta->cstream);
  }

  if (meta->mstream != 0) {
    GST_WARNING ("Protocol control message on message stream %"
        G_GUINT32_FORMAT ", not 0", meta->mstream);
  }

  return TRUE;
}

gboolean
gst_rtmp_message_is_user_control (GstBuffer * buffer)
{
  GstRtmpMeta *meta = gst_buffer_get_rtmp_meta (buffer);

  g_return_val_if_fail (meta, FALSE);

  if (meta->type != GST_RTMP_MESSAGE_TYPE_USER_CONTROL) {
    return FALSE;
  }

  if (meta->cstream != GST_RTMP_CHUNK_STREAM_PROTOCOL) {
    GST_WARNING ("User control message on chunk stream %"
        G_GUINT32_FORMAT ", not 2", meta->cstream);
  }

  if (meta->mstream != 0) {
    GST_WARNING ("User control message on message stream %"
        G_GUINT32_FORMAT ", not 0", meta->mstream);
  }

  return TRUE;
}

static inline gboolean
pc_has_param2 (GstRtmpMessageType type)
{
  return type == GST_RTMP_MESSAGE_TYPE_SET_PEER_BANDWIDTH;
}

gboolean
gst_rtmp_message_parse_protocol_control (GstBuffer * buffer,
    GstRtmpProtocolControl * out)
{
  GstRtmpMeta *meta = gst_buffer_get_rtmp_meta (buffer);
  GstMapInfo map;
  GstRtmpProtocolControl pc;
  gsize pc_size = 4;
  gboolean ret = FALSE;

  g_return_val_if_fail (meta, FALSE);
  g_return_val_if_fail (gst_rtmp_message_type_is_protocol_control (meta->type),
      FALSE);

  if (!gst_buffer_map (buffer, &map, GST_MAP_READ)) {
    GST_ERROR ("can't map protocol control message");
    return FALSE;
  }

  pc.type = meta->type;
  pc_size = pc_has_param2 (pc.type) ? 5 : 4;

  if (map.size < pc_size) {
    GST_ERROR ("can't read protocol control param");
    goto err;
  } else if (map.size > pc_size) {
    GST_WARNING ("overlength protocol control: %" G_GSIZE_FORMAT " > %"
        G_GSIZE_FORMAT, map.size, pc_size);
  }

  pc.param = GST_READ_UINT32_BE (map.data);
  pc.param2 = pc_has_param2 (pc.type) ? GST_READ_UINT8 (map.data + 4) : 0;

  ret = TRUE;
  if (out) {
    *out = pc;
  }

err:
  gst_buffer_unmap (buffer, &map);
  return ret;
}

GstBuffer *
gst_rtmp_message_new_protocol_control (GstRtmpProtocolControl * pc)
{
  guint8 *data;
  gsize size;

  g_return_val_if_fail (pc, NULL);
  g_return_val_if_fail (gst_rtmp_message_type_is_protocol_control (pc->type),
      NULL);

  size = pc_has_param2 (pc->type) ? 5 : 4;

  data = g_malloc (size);
  GST_WRITE_UINT32_BE (data, pc->param);
  if (pc_has_param2 (pc->type)) {
    GST_WRITE_UINT32_BE (data + 4, pc->param2);
  }

  return gst_rtmp_message_new_wrapped (pc->type,
      GST_RTMP_CHUNK_STREAM_PROTOCOL, 0, data, size);
}

static inline gboolean
uc_has_param2 (GstRtmpUserControlType type)
{
  return type == GST_RTMP_USER_CONTROL_TYPE_SET_BUFFER_LENGTH;
}

gboolean
gst_rtmp_message_parse_user_control (GstBuffer * buffer,
    GstRtmpUserControl * out)
{
  GstRtmpMeta *meta = gst_buffer_get_rtmp_meta (buffer);
  GstMapInfo map;
  GstRtmpUserControl uc;
  gsize uc_size;
  gboolean ret = FALSE;

  g_return_val_if_fail (meta, FALSE);
  g_return_val_if_fail (meta->type == GST_RTMP_MESSAGE_TYPE_USER_CONTROL,
      FALSE);

  if (!gst_buffer_map (buffer, &map, GST_MAP_READ)) {
    GST_ERROR ("can't map user control message");
    return FALSE;
  }

  if (map.size < 2) {
    GST_ERROR ("can't read user control type");
    goto err;
  }

  uc.type = GST_READ_UINT16_BE (map.data);
  uc_size = uc_has_param2 (uc.type) ? 10 : 6;

  if (map.size < uc_size) {
    GST_ERROR ("can't read user control param");
    goto err;
  } else if (map.size > uc_size) {
    GST_WARNING ("overlength user control: %" G_GSIZE_FORMAT " > %"
        G_GSIZE_FORMAT, map.size, uc_size);
  }

  uc.param = GST_READ_UINT32_BE (map.data + 2);
  uc.param2 = uc_has_param2 (uc.type) ? GST_READ_UINT32_BE (map.data + 6) : 0;

  ret = TRUE;
  if (out) {
    *out = uc;
  }

err:
  gst_buffer_unmap (buffer, &map);
  return ret;
}

GstBuffer *
gst_rtmp_message_new_user_control (GstRtmpUserControl * uc)
{
  guint8 *data;
  gsize size;

  g_return_val_if_fail (uc, NULL);

  size = uc_has_param2 (uc->type) ? 10 : 6;

  data = g_malloc (size);
  GST_WRITE_UINT16_BE (data, uc->type);
  GST_WRITE_UINT32_BE (data + 2, uc->param);
  if (uc_has_param2 (uc->type)) {
    GST_WRITE_UINT32_BE (data + 6, uc->param2);
  }

  return gst_rtmp_message_new_wrapped (GST_RTMP_MESSAGE_TYPE_USER_CONTROL,
      GST_RTMP_CHUNK_STREAM_PROTOCOL, 0, data, size);
}

gboolean
gst_rtmp_message_is_metadata (GstBuffer * buffer)
{
  GstRtmpMeta *meta = gst_buffer_get_rtmp_meta (buffer);
  GstMapInfo map;
  GstAmfNode *node;
  gboolean ret = FALSE;

  g_return_val_if_fail (meta, FALSE);

  if (meta->type != GST_RTMP_MESSAGE_TYPE_DATA_AMF0) {
    return FALSE;
  }

  if (!gst_buffer_map (buffer, &map, GST_MAP_READ)) {
    GST_ERROR ("can't map metadata message");
    return FALSE;
  }

  node = gst_amf_node_parse (map.data, map.size, NULL);
  if (!node) {
    GST_ERROR ("can't read metadata name");
    goto err;
  }

  switch (gst_amf_node_get_type (node)) {
    case GST_AMF_TYPE_STRING:
    case GST_AMF_TYPE_LONG_STRING:{
      const gchar *name = gst_amf_node_peek_string (node, NULL);
      ret = (strcmp (name, "onMetaData") == 0);
      break;
    }

    default:
      break;
  }

  gst_amf_node_free (node);

err:
  gst_buffer_unmap (buffer, &map);
  return ret;
}