]> git.sesse.net Git - ffmpeg/blob - libavformat/rtmppkt.c
4ca12a00418effcbc88ab1b262d5cae705698250
[ffmpeg] / libavformat / rtmppkt.c
1 /*
2  * RTMP input format
3  * Copyright (c) 2009 Kostya Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavcodec/bytestream.h"
23 #include "libavutil/avstring.h"
24 #include "avformat.h"
25
26 #include "rtmppkt.h"
27 #include "flv.h"
28
29 void ff_amf_write_bool(uint8_t **dst, int val)
30 {
31     bytestream_put_byte(dst, AMF_DATA_TYPE_BOOL);
32     bytestream_put_byte(dst, val);
33 }
34
35 void ff_amf_write_number(uint8_t **dst, double val)
36 {
37     bytestream_put_byte(dst, AMF_DATA_TYPE_NUMBER);
38     bytestream_put_be64(dst, av_dbl2int(val));
39 }
40
41 void ff_amf_write_string(uint8_t **dst, const char *str)
42 {
43     bytestream_put_byte(dst, AMF_DATA_TYPE_STRING);
44     bytestream_put_be16(dst, strlen(str));
45     bytestream_put_buffer(dst, str, strlen(str));
46 }
47
48 void ff_amf_write_null(uint8_t **dst)
49 {
50     bytestream_put_byte(dst, AMF_DATA_TYPE_NULL);
51 }
52
53 void ff_amf_write_object_start(uint8_t **dst)
54 {
55     bytestream_put_byte(dst, AMF_DATA_TYPE_OBJECT);
56 }
57
58 void ff_amf_write_field_name(uint8_t **dst, const char *str)
59 {
60     bytestream_put_be16(dst, strlen(str));
61     bytestream_put_buffer(dst, str, strlen(str));
62 }
63
64 void ff_amf_write_object_end(uint8_t **dst)
65 {
66     /* first two bytes are field name length = 0,
67      * AMF object should end with it and end marker
68      */
69     bytestream_put_be24(dst, AMF_DATA_TYPE_OBJECT_END);
70 }
71
72 int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p,
73                         int chunk_size, RTMPPacket *prev_pkt)
74 {
75     uint8_t hdr, t, buf[16];
76     int channel_id, timestamp, data_size, offset = 0;
77     uint32_t extra = 0;
78     uint8_t type;
79
80     if (url_read(h, &hdr, 1) != 1)
81         return AVERROR(EIO);
82     channel_id = hdr & 0x3F;
83
84     data_size = prev_pkt[channel_id].data_size;
85     type      = prev_pkt[channel_id].type;
86     extra     = prev_pkt[channel_id].extra;
87
88     hdr >>= 6;
89     if (hdr == RTMP_PS_ONEBYTE) {
90         timestamp = prev_pkt[channel_id].timestamp;
91     } else {
92         if (url_read_complete(h, buf, 3) != 3)
93             return AVERROR(EIO);
94         timestamp = AV_RB24(buf);
95         if (hdr != RTMP_PS_FOURBYTES) {
96             if (url_read_complete(h, buf, 3) != 3)
97                 return AVERROR(EIO);
98             data_size = AV_RB24(buf);
99             if (url_read_complete(h, &type, 1) != 1)
100                 return AVERROR(EIO);
101             if (hdr == RTMP_PS_TWELVEBYTES) {
102                 if (url_read_complete(h, buf, 4) != 4)
103                     return AVERROR(EIO);
104                 extra = AV_RL32(buf);
105             }
106         }
107     }
108     if (ff_rtmp_packet_create(p, channel_id, type, timestamp, data_size))
109         return -1;
110     p->extra = extra;
111     // save history
112     prev_pkt[channel_id].channel_id = channel_id;
113     prev_pkt[channel_id].type       = type;
114     prev_pkt[channel_id].data_size  = data_size;
115     prev_pkt[channel_id].timestamp  = timestamp;
116     prev_pkt[channel_id].extra      = extra;
117     while (data_size > 0) {
118         int toread = FFMIN(data_size, chunk_size);
119         if (url_read_complete(h, p->data + offset, toread) != toread) {
120             ff_rtmp_packet_destroy(p);
121             return AVERROR(EIO);
122         }
123         data_size -= chunk_size;
124         offset    += chunk_size;
125         if (data_size > 0) {
126             url_read_complete(h, &t, 1); //marker
127             if (t != (0xC0 + channel_id))
128                 return -1;
129         }
130     }
131     return 0;
132 }
133
134 int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt,
135                          int chunk_size, RTMPPacket *prev_pkt)
136 {
137     uint8_t pkt_hdr[16], *p = pkt_hdr;
138     int mode = RTMP_PS_TWELVEBYTES;
139     int off = 0;
140
141     //TODO: header compression
142     bytestream_put_byte(&p, pkt->channel_id | (mode << 6));
143     if (mode != RTMP_PS_ONEBYTE) {
144         bytestream_put_be24(&p, pkt->timestamp);
145         if (mode != RTMP_PS_FOURBYTES) {
146             bytestream_put_be24(&p, pkt->data_size);
147             bytestream_put_byte(&p, pkt->type);
148             if (mode == RTMP_PS_TWELVEBYTES)
149                 bytestream_put_le32(&p, pkt->extra);
150         }
151     }
152     url_write(h, pkt_hdr, p-pkt_hdr);
153     while (off < pkt->data_size) {
154         int towrite = FFMIN(chunk_size, pkt->data_size - off);
155         url_write(h, pkt->data + off, towrite);
156         off += towrite;
157         if (off < pkt->data_size) {
158             uint8_t marker = 0xC0 | pkt->channel_id;
159             url_write(h, &marker, 1);
160         }
161     }
162     return 0;
163 }
164
165 int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type,
166                           int timestamp, int size)
167 {
168     pkt->data = av_malloc(size);
169     if (!pkt->data)
170         return AVERROR(ENOMEM);
171     pkt->data_size  = size;
172     pkt->channel_id = channel_id;
173     pkt->type       = type;
174     pkt->timestamp  = timestamp;
175     pkt->extra      = 0;
176
177     return 0;
178 }
179
180 void ff_rtmp_packet_destroy(RTMPPacket *pkt)
181 {
182     if (!pkt)
183         return;
184     av_freep(&pkt->data);
185     pkt->data_size = 0;
186 }
187
188 int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end)
189 {
190     const uint8_t *base = data;
191
192     if (data >= data_end)
193         return -1;
194     switch (*data++) {
195     case AMF_DATA_TYPE_NUMBER:      return 9;
196     case AMF_DATA_TYPE_BOOL:        return 2;
197     case AMF_DATA_TYPE_STRING:      return 3 + AV_RB16(data);
198     case AMF_DATA_TYPE_LONG_STRING: return 5 + AV_RB32(data);
199     case AMF_DATA_TYPE_NULL:        return 1;
200     case AMF_DATA_TYPE_ARRAY:
201         data += 4;
202     case AMF_DATA_TYPE_OBJECT:
203         for (;;) {
204             int size = bytestream_get_be16(&data);
205             int t;
206             if (!size) {
207                 data++;
208                 break;
209             }
210             if (data + size >= data_end || data + size < data)
211                 return -1;
212             data += size;
213             t = ff_amf_tag_size(data, data_end);
214             if (t < 0 || data + t >= data_end)
215                 return -1;
216             data += t;
217         }
218         return data - base;
219     case AMF_DATA_TYPE_OBJECT_END:  return 1;
220     default:                        return -1;
221     }
222 }
223
224 int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
225                            const uint8_t *name, uint8_t *dst, int dst_size)
226 {
227     int namelen = strlen(name);
228     int len;
229
230     if (data_end - data < 3)
231         return -1;
232     if (*data++ != AMF_DATA_TYPE_OBJECT)
233         return -1;
234     for (;;) {
235         int size = bytestream_get_be16(&data);
236         if (!size)
237             break;
238         if (data + size >= data_end || data + size < data)
239             return -1;
240         data += size;
241         if (size == namelen && !memcmp(data-size, name, namelen)) {
242             switch (*data++) {
243             case AMF_DATA_TYPE_NUMBER:
244                 snprintf(dst, dst_size, "%g", av_int2dbl(AV_RB64(data)));
245                 break;
246             case AMF_DATA_TYPE_BOOL:
247                 snprintf(dst, dst_size, "%s", *data ? "true" : "false");
248                 break;
249             case AMF_DATA_TYPE_STRING:
250                 len = bytestream_get_be16(&data);
251                 av_strlcpy(dst, data, FFMIN(len+1, dst_size));
252                 break;
253             default:
254                 return -1;
255             }
256             return 0;
257         }
258         len = ff_amf_tag_size(data, data_end);
259         if (len < 0 || data + len >= data_end || data + len < data)
260             return -1;
261         data += len;
262     }
263     return -1;
264 }