summaryrefslogtreecommitdiff
path: root/rtmp/rtmpchunk.c
blob: 2a8fe7b3b7a16822a3dd7296dc8b3762a3746091 (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
/* GStreamer RTMP Library
 * Copyright (C) 2013 David Schleef <ds@schleef.org>
 *
 * 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 Street, Suite 500,
 * Boston, MA 02110-1335, USA.
 */

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

#include <gst/gst.h>
#include "rtmpchunk.h"
#include "rtmputils.h"
#include <string.h>

GST_DEBUG_CATEGORY_STATIC (gst_rtmp_chunk_debug_category);
#define GST_CAT_DEFAULT gst_rtmp_chunk_debug_category

/* prototypes */


static void gst_rtmp_chunk_set_property (GObject * object,
    guint property_id, const GValue * value, GParamSpec * pspec);
static void gst_rtmp_chunk_get_property (GObject * object,
    guint property_id, GValue * value, GParamSpec * pspec);
static void gst_rtmp_chunk_dispose (GObject * object);
static void gst_rtmp_chunk_finalize (GObject * object);


enum
{
  PROP_0
};

/* class initialization */

G_DEFINE_TYPE_WITH_CODE (GstRtmpChunk, gst_rtmp_chunk, G_TYPE_OBJECT,
    GST_DEBUG_CATEGORY_INIT (gst_rtmp_chunk_debug_category, "rtmpchunk", 0,
        "debug category for rtmpchunk element"));

static void
gst_rtmp_chunk_class_init (GstRtmpChunkClass * klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->set_property = gst_rtmp_chunk_set_property;
  gobject_class->get_property = gst_rtmp_chunk_get_property;
  gobject_class->dispose = gst_rtmp_chunk_dispose;
  gobject_class->finalize = gst_rtmp_chunk_finalize;

}

static void
gst_rtmp_chunk_init (GstRtmpChunk * rtmpchunk)
{
}

void
gst_rtmp_chunk_set_property (GObject * object, guint property_id,
    const GValue * value, GParamSpec * pspec)
{
  GstRtmpChunk *rtmpchunk = GST_RTMP_CHUNK (object);

  GST_DEBUG_OBJECT (rtmpchunk, "set_property");

  switch (property_id) {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
  }
}

void
gst_rtmp_chunk_get_property (GObject * object, guint property_id,
    GValue * value, GParamSpec * pspec)
{
  GstRtmpChunk *rtmpchunk = GST_RTMP_CHUNK (object);

  GST_DEBUG_OBJECT (rtmpchunk, "get_property");

  switch (property_id) {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
  }
}

void
gst_rtmp_chunk_dispose (GObject * object)
{
  GstRtmpChunk *rtmpchunk = GST_RTMP_CHUNK (object);

  GST_DEBUG_OBJECT (rtmpchunk, "dispose");

  /* clean up as possible.  may be called multiple times */

  G_OBJECT_CLASS (gst_rtmp_chunk_parent_class)->dispose (object);
}

void
gst_rtmp_chunk_finalize (GObject * object)
{
  GstRtmpChunk *rtmpchunk = GST_RTMP_CHUNK (object);

  GST_DEBUG_OBJECT (rtmpchunk, "finalize");

  /* clean up object here */

  G_OBJECT_CLASS (gst_rtmp_chunk_parent_class)->finalize (object);
}

GstRtmpChunk *
gst_rtmp_chunk_new (void)
{
  return g_object_new (GST_TYPE_RTMP_CHUNK, NULL);
}

static GstRtmpChunkParseStatus
chunk_parse (GstRtmpChunk * chunk, GBytes * bytes, gsize * needed_bytes)
{
  int offset;
  const guint8 *data;
  gsize size;
  int header_fmt;

  if (*needed_bytes)
    *needed_bytes = 0;

  data = g_bytes_get_data (bytes, &size);
  if (size < 1)
    return GST_RTMP_CHUNK_PARSE_UNKNOWN;

  header_fmt = data[0] >> 6;    /* librtmp: m_headerType */
  chunk->stream_id = data[0] & 0x3f;    /* librtmp: m_nChannel */
  offset = 1;
  if (chunk->stream_id == 0) {
    if (size < 2)
      return GST_RTMP_CHUNK_PARSE_UNKNOWN;
    chunk->stream_id = 64 + data[1];
    offset = 2;
  } else if (chunk->stream_id == 1) {
    if (size < 3)
      return GST_RTMP_CHUNK_PARSE_UNKNOWN;
    chunk->stream_id = 64 + data[1] + (data[2] << 8);
    offset = 3;
  }
  if (header_fmt == 0) {
    if (size < offset + 11)
      return GST_RTMP_CHUNK_PARSE_UNKNOWN;
    /* librtmp: m_nTimeStamp */
    chunk->timestamp =
        (data[offset] << 16) | (data[offset + 1] << 8) | data[offset + 2];
    offset += 3;
    /* librtmp: m_nBodySize */
    chunk->message_length =
        (data[offset] << 16) | (data[offset + 1] << 8) | data[offset + 2];
    offset += 3;
    if (chunk->timestamp == 0xffffff) {
      if (size < offset + 4 + 1 + 4)
        return GST_RTMP_CHUNK_PARSE_UNKNOWN;
      chunk->timestamp = (data[offset] << 24) | (data[offset + 1] << 16) |
          (data[offset + 2] << 8) | data[offset + 3];
      offset += 4;
    }
    chunk->message_type_id = data[offset];      /* librtmp: m_packetType */
    offset += 1;

    /* librtmp: m_nInfoField2 */
    chunk->info = (data[offset] << 24) | (data[offset + 1] << 16) |
        (data[offset + 2] << 8) | data[offset + 3];
    offset += 4;
  } else if (header_fmt == 1) {
    if (size < offset + 7)
      return GST_RTMP_CHUNK_PARSE_UNKNOWN;
    GST_ERROR ("unimplemented: need previous chunk");
    chunk->timestamp =
        (data[offset] << 16) | (data[offset + 1] << 8) | data[offset + 2];
    offset += 3;
    chunk->message_length =
        (data[offset] << 16) | (data[offset + 1] << 8) | data[offset + 2];
    offset += 3;
  } else if (header_fmt == 2) {
    if (size < offset + 3)
      return GST_RTMP_CHUNK_PARSE_UNKNOWN;
    chunk->timestamp = 0;
    GST_ERROR ("unimplemented: need previous chunk");
    return GST_RTMP_CHUNK_PARSE_ERROR;
  } else {
    chunk->timestamp = 0;
    GST_ERROR ("unimplemented: need previous chunk");
    return GST_RTMP_CHUNK_PARSE_ERROR;
  }

  if (needed_bytes)
    *needed_bytes = offset + chunk->message_length;
  if (size < offset + chunk->message_length)
    return GST_RTMP_CHUNK_PARSE_NEED_BYTES;

#if 0
  {
    GBytes *b;
    GST_ERROR ("PARSED CHUNK:");
    b = g_bytes_new_from_bytes (bytes, 0, offset + chunk->message_length);
    gst_rtmp_dump_data (b);
    g_bytes_unref (b);
  }
#endif

  chunk->payload =
      g_bytes_new_from_bytes (bytes, offset, chunk->message_length);
  return GST_RTMP_CHUNK_PARSE_OK;
}

GstRtmpChunkParseStatus
gst_rtmp_chunk_can_parse (GBytes * bytes, gsize * chunk_size)
{
  GstRtmpChunk *chunk;
  GstRtmpChunkParseStatus status;

  chunk = gst_rtmp_chunk_new ();
  status = chunk_parse (chunk, bytes, chunk_size);
  g_object_unref (chunk);

  return status;
}

GstRtmpChunk *
gst_rtmp_chunk_new_parse (GBytes * bytes, gsize * chunk_size)
{
  GstRtmpChunk *chunk;
  GstRtmpChunkParseStatus status;

  chunk = gst_rtmp_chunk_new ();
  status = chunk_parse (chunk, bytes, chunk_size);
  if (status == GST_RTMP_CHUNK_PARSE_OK)
    return chunk;

  g_object_unref (chunk);
  return NULL;
}

GBytes *
gst_rtmp_chunk_serialize (GstRtmpChunk * chunk)
{
  guint8 *data;
  const guint8 *chunkdata;
  gsize chunksize;
  int header_fmt;

  /* FIXME this is incomplete and inefficient */
  chunkdata = g_bytes_get_data (chunk->payload, &chunksize);
  data = g_malloc (chunksize + 12);
  header_fmt = 0;
  g_assert (chunk->stream_id < 64);
  data[0] = (header_fmt << 6) | (chunk->stream_id);
  g_assert (chunk->timestamp < 0xffffff);
  data[1] = (chunk->timestamp >> 16) & 0xff;
  data[2] = (chunk->timestamp >> 8) & 0xff;
  data[3] = chunk->timestamp & 0xff;
  data[4] = (chunk->message_length >> 16) & 0xff;
  data[5] = (chunk->message_length >> 8) & 0xff;
  data[6] = chunk->message_length & 0xff;
  data[7] = chunk->message_type_id;
  data[8] = 0;
  data[9] = 0;
  data[10] = 0;
  data[11] = 0;
  memcpy (data + 12, chunkdata, chunksize);

  return g_bytes_new_take (data, chunksize + 12);
}

void
gst_rtmp_chunk_set_stream_id (GstRtmpChunk * chunk, guint32 stream_id)
{
  chunk->stream_id = stream_id;
}

void
gst_rtmp_chunk_set_timestamp (GstRtmpChunk * chunk, guint32 timestamp)
{
  chunk->timestamp = timestamp;
}

void
gst_rtmp_chunk_set_payload (GstRtmpChunk * chunk, GBytes * payload)
{
  if (chunk->payload) {
    g_bytes_unref (chunk->payload);
  }
  chunk->payload = payload;
}

guint32
gst_rtmp_chunk_get_stream_id (GstRtmpChunk * chunk)
{
  return chunk->stream_id;
}

guint32
gst_rtmp_chunk_get_timestamp (GstRtmpChunk * chunk)
{
  return chunk->timestamp;
}

GBytes *
gst_rtmp_chunk_get_payload (GstRtmpChunk * chunk)
{
  return chunk->payload;
}