Mercurial > libavformat.hg
annotate rtp.c @ 1434:cfc938c2abbe libavformat
IMC decoder
author | kostya |
---|---|
date | Mon, 30 Oct 2006 13:48:48 +0000 |
parents | 2d8a17631520 |
children | 404048ea11bc |
rev | line source |
---|---|
0 | 1 /* |
2 * RTP input/output format | |
3 * Copyright (c) 2002 Fabrice Bellard. | |
4 * | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1332
diff
changeset
|
5 * This file is part of FFmpeg. |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1332
diff
changeset
|
6 * |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1332
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
0 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1332
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
0 | 11 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1332
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
0 | 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 | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1332
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
885
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 20 */ |
21 #include "avformat.h" | |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
22 #include "mpegts.h" |
774 | 23 #include "bitstream.h" |
0 | 24 |
25 #include <unistd.h> | |
26 #include <sys/types.h> | |
27 #include <sys/socket.h> | |
28 #include <netinet/in.h> | |
29 #ifndef __BEOS__ | |
30 # include <arpa/inet.h> | |
31 #else | |
32 # include "barpainet.h" | |
33 #endif | |
34 #include <netdb.h> | |
35 | |
1419 | 36 #include "rtp_internal.h" |
37 | |
38 //#define RTP_H264 | |
39 #ifdef RTP_H264 | |
40 #include "rtp_h264.h" | |
41 #endif | |
42 | |
0 | 43 //#define DEBUG |
44 | |
45 | |
46 /* TODO: - add RTCP statistics reporting (should be optional). | |
47 | |
48 - add support for h263/mpeg4 packetized output : IDEA: send a | |
49 buffer to 'rtp_write_packet' contains all the packets for ONE | |
50 frame. Each packet should have a four byte header containing | |
51 the length in big endian format (same trick as | |
885 | 52 'url_open_dyn_packet_buf') |
0 | 53 */ |
54 | |
774 | 55 /* from http://www.iana.org/assignments/rtp-parameters last updated 05 January 2005 */ |
56 AVRtpPayloadType_t AVRtpPayloadTypes[]= | |
57 { | |
58 {0, "PCMU", CODEC_TYPE_AUDIO, CODEC_ID_PCM_MULAW, 8000, 1}, | |
59 {1, "Reserved", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
60 {2, "Reserved", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
61 {3, "GSM", CODEC_TYPE_AUDIO, CODEC_ID_NONE, 8000, 1}, | |
62 {4, "G723", CODEC_TYPE_AUDIO, CODEC_ID_NONE, 8000, 1}, | |
63 {5, "DVI4", CODEC_TYPE_AUDIO, CODEC_ID_NONE, 8000, 1}, | |
64 {6, "DVI4", CODEC_TYPE_AUDIO, CODEC_ID_NONE, 16000, 1}, | |
65 {7, "LPC", CODEC_TYPE_AUDIO, CODEC_ID_NONE, 8000, 1}, | |
66 {8, "PCMA", CODEC_TYPE_AUDIO, CODEC_ID_PCM_ALAW, 8000, 1}, | |
67 {9, "G722", CODEC_TYPE_AUDIO, CODEC_ID_NONE, 8000, 1}, | |
68 {10, "L16", CODEC_TYPE_AUDIO, CODEC_ID_PCM_S16BE, 44100, 2}, | |
69 {11, "L16", CODEC_TYPE_AUDIO, CODEC_ID_PCM_S16BE, 44100, 1}, | |
1259 | 70 {12, "QCELP", CODEC_TYPE_AUDIO, CODEC_ID_QCELP, 8000, 1}, |
774 | 71 {13, "CN", CODEC_TYPE_AUDIO, CODEC_ID_NONE, 8000, 1}, |
72 {14, "MPA", CODEC_TYPE_AUDIO, CODEC_ID_MP2, 90000, -1}, | |
73 {15, "G728", CODEC_TYPE_AUDIO, CODEC_ID_NONE, 8000, 1}, | |
74 {16, "DVI4", CODEC_TYPE_AUDIO, CODEC_ID_NONE, 11025, 1}, | |
75 {17, "DVI4", CODEC_TYPE_AUDIO, CODEC_ID_NONE, 22050, 1}, | |
76 {18, "G729", CODEC_TYPE_AUDIO, CODEC_ID_NONE, 8000, 1}, | |
77 {19, "reserved", CODEC_TYPE_AUDIO, CODEC_ID_NONE, -1, -1}, | |
78 {20, "unassigned", CODEC_TYPE_AUDIO, CODEC_ID_NONE, -1, -1}, | |
79 {21, "unassigned", CODEC_TYPE_AUDIO, CODEC_ID_NONE, -1, -1}, | |
80 {22, "unassigned", CODEC_TYPE_AUDIO, CODEC_ID_NONE, -1, -1}, | |
81 {23, "unassigned", CODEC_TYPE_AUDIO, CODEC_ID_NONE, -1, -1}, | |
82 {24, "unassigned", CODEC_TYPE_VIDEO, CODEC_ID_NONE, -1, -1}, | |
83 {25, "CelB", CODEC_TYPE_VIDEO, CODEC_ID_NONE, 90000, -1}, | |
84 {26, "JPEG", CODEC_TYPE_VIDEO, CODEC_ID_MJPEG, 90000, -1}, | |
85 {27, "unassigned", CODEC_TYPE_VIDEO, CODEC_ID_NONE, -1, -1}, | |
86 {28, "nv", CODEC_TYPE_VIDEO, CODEC_ID_NONE, 90000, -1}, | |
87 {29, "unassigned", CODEC_TYPE_VIDEO, CODEC_ID_NONE, -1, -1}, | |
88 {30, "unassigned", CODEC_TYPE_VIDEO, CODEC_ID_NONE, -1, -1}, | |
89 {31, "H261", CODEC_TYPE_VIDEO, CODEC_ID_H261, 90000, -1}, | |
90 {32, "MPV", CODEC_TYPE_VIDEO, CODEC_ID_MPEG1VIDEO, 90000, -1}, | |
91 {33, "MP2T", CODEC_TYPE_DATA, CODEC_ID_MPEG2TS, 90000, -1}, | |
92 {34, "H263", CODEC_TYPE_VIDEO, CODEC_ID_H263, 90000, -1}, | |
93 {35, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
94 {36, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
95 {37, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
96 {38, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
97 {39, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
98 {40, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
99 {41, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
100 {42, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
101 {43, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
102 {44, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
103 {45, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
104 {46, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
105 {47, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
106 {48, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
107 {49, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
108 {50, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
109 {51, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
110 {52, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
111 {53, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
112 {54, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
113 {55, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
114 {56, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
115 {57, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
116 {58, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
117 {59, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
118 {60, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
119 {61, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
120 {62, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
121 {63, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
122 {64, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
123 {65, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
124 {66, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
125 {67, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
126 {68, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
127 {69, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
128 {70, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
129 {71, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
130 {72, "reserved for RTCP conflict avoidance", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
131 {73, "reserved for RTCP conflict avoidance", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
132 {74, "reserved for RTCP conflict avoidance", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
133 {75, "reserved for RTCP conflict avoidance", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
134 {76, "reserved for RTCP conflict avoidance", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
135 {77, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
136 {78, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
137 {79, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
138 {80, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
139 {81, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
140 {82, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
141 {83, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
142 {84, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
143 {85, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
144 {86, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
145 {87, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
146 {88, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
147 {89, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
148 {90, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
149 {91, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
150 {92, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
151 {93, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
152 {94, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
153 {95, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
154 {96, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
155 {97, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
156 {98, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
157 {99, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
158 {100, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
159 {101, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
160 {102, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
161 {103, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
162 {104, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
163 {105, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
164 {106, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
165 {107, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
166 {108, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
167 {109, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
168 {110, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
169 {111, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
170 {112, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
171 {113, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
172 {114, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
173 {115, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
174 {116, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
175 {117, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
176 {118, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
177 {119, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
178 {120, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
179 {121, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
180 {122, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
181 {123, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
182 {124, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
183 {125, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
184 {126, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
185 {127, "dynamic", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}, | |
186 {-1, "", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1} | |
187 }; | |
0 | 188 |
1419 | 189 /* statistics functions */ |
190 RTPDynamicProtocolHandler *RTPFirstDynamicPayloadHandler= NULL; | |
191 | |
192 static RTPDynamicProtocolHandler mp4v_es_handler= {"MP4V-ES", CODEC_TYPE_VIDEO, CODEC_ID_MPEG4}; | |
193 static RTPDynamicProtocolHandler mpeg4_generic_handler= {"mpeg4-generic", CODEC_TYPE_AUDIO, CODEC_ID_MPEG4AAC}; | |
0 | 194 |
1419 | 195 static void register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler) |
196 { | |
197 handler->next= RTPFirstDynamicPayloadHandler; | |
198 RTPFirstDynamicPayloadHandler= handler; | |
199 } | |
885 | 200 |
1419 | 201 void av_register_rtp_dynamic_payload_handlers() |
202 { | |
203 register_dynamic_payload_handler(&mp4v_es_handler); | |
204 register_dynamic_payload_handler(&mpeg4_generic_handler); | |
205 #ifdef RTP_H264 | |
206 register_dynamic_payload_handler(&ff_h264_dynamic_handler); | |
207 #endif | |
208 } | |
0 | 209 |
210 int rtp_get_codec_info(AVCodecContext *codec, int payload_type) | |
211 { | |
774 | 212 if (AVRtpPayloadTypes[payload_type].codec_id != CODEC_ID_NONE) { |
213 codec->codec_type = AVRtpPayloadTypes[payload_type].codec_type; | |
1116 | 214 codec->codec_id = AVRtpPayloadTypes[payload_type].codec_id; |
774 | 215 if (AVRtpPayloadTypes[payload_type].audio_channels > 0) |
216 codec->channels = AVRtpPayloadTypes[payload_type].audio_channels; | |
217 if (AVRtpPayloadTypes[payload_type].clock_rate > 0) | |
218 codec->sample_rate = AVRtpPayloadTypes[payload_type].clock_rate; | |
219 return 0; | |
0 | 220 } |
774 | 221 return -1; |
0 | 222 } |
223 | |
224 /* return < 0 if unknown payload type */ | |
225 int rtp_get_payload_type(AVCodecContext *codec) | |
226 { | |
774 | 227 int i, payload_type; |
0 | 228 |
229 /* compute the payload type */ | |
774 | 230 for (payload_type = -1, i = 0; AVRtpPayloadTypes[i].pt >= 0; ++i) |
231 if (AVRtpPayloadTypes[i].codec_id == codec->codec_id) { | |
232 if (codec->codec_id == CODEC_ID_PCM_S16BE) | |
233 if (codec->channels != AVRtpPayloadTypes[i].audio_channels) | |
234 continue; | |
235 payload_type = AVRtpPayloadTypes[i].pt; | |
0 | 236 } |
237 return payload_type; | |
238 } | |
239 | |
65 | 240 static inline uint32_t decode_be32(const uint8_t *p) |
0 | 241 { |
242 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; | |
243 } | |
244 | |
173 | 245 static inline uint64_t decode_be64(const uint8_t *p) |
0 | 246 { |
65 | 247 return ((uint64_t)decode_be32(p) << 32) | decode_be32(p + 4); |
0 | 248 } |
249 | |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
250 static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf, int len) |
0 | 251 { |
252 if (buf[1] != 200) | |
253 return -1; | |
254 s->last_rtcp_ntp_time = decode_be64(buf + 8); | |
173 | 255 if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE) |
256 s->first_rtcp_ntp_time = s->last_rtcp_ntp_time; | |
0 | 257 s->last_rtcp_timestamp = decode_be32(buf + 16); |
258 return 0; | |
259 } | |
260 | |
261 /** | |
1425
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
262 * some rtp servers assume client is dead if they don't hear from them... |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
263 * so we send a Receiver Report to the provided ByteIO context |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
264 * (we don't have access to the rtcp handle from here) |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
265 */ |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
266 int rtp_check_and_send_back_rr(RTPDemuxContext *s, int count) |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
267 { |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
268 ByteIOContext pb; |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
269 uint8_t *buf; |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
270 int len; |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
271 int rtcp_bytes; |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
272 |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
273 if (!s->rtp_ctx || (count < 1)) |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
274 return -1; |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
275 |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
276 /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */ |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
277 s->octet_count += count; |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
278 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) / |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
279 RTCP_TX_RATIO_DEN; |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
280 rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !? |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
281 if (rtcp_bytes < 28) |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
282 return -1; |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
283 s->last_octet_count = s->octet_count; |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
284 |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
285 if (url_open_dyn_buf(&pb) < 0) |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
286 return -1; |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
287 |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
288 // Receiver Report |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
289 put_byte(&pb, (RTP_VERSION << 6) + 1); /* 1 report block */ |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
290 put_byte(&pb, 201); |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
291 put_be16(&pb, 7); /* length in words - 1 */ |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
292 put_be32(&pb, s->ssrc); // our own SSRC |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
293 put_be32(&pb, s->ssrc); // XXX: should be the server's here! |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
294 // some placeholders we should really fill... |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
295 put_be32(&pb, ((0 << 24) | (0 & 0x0ffffff))); /* 0% lost, total 0 lost */ |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
296 put_be32(&pb, (0 << 16) | s->seq); |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
297 put_be32(&pb, 0x68); /* jitter */ |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
298 put_be32(&pb, -1); /* last SR timestamp */ |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
299 put_be32(&pb, 1); /* delay since last SR */ |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
300 |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
301 // CNAME |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
302 put_byte(&pb, (RTP_VERSION << 6) + 1); /* 1 report block */ |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
303 put_byte(&pb, 202); |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
304 len = strlen(s->hostname); |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
305 put_be16(&pb, (6 + len + 3) / 4); /* length in words - 1 */ |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
306 put_be32(&pb, s->ssrc); |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
307 put_byte(&pb, 0x01); |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
308 put_byte(&pb, len); |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
309 put_buffer(&pb, s->hostname, len); |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
310 // padding |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
311 for (len = (6 + len) % 4; len % 4; len++) { |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
312 put_byte(&pb, 0); |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
313 } |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
314 |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
315 put_flush_packet(&pb); |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
316 len = url_close_dyn_buf(&pb, &buf); |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
317 if ((len > 0) && buf) { |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
318 #if defined(DEBUG) |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
319 printf("sending %d bytes of RR\n", len); |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
320 #endif |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
321 url_write(s->rtp_ctx, buf, len); |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
322 av_free(buf); |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
323 } |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
324 return 0; |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
325 } |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
326 |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
327 /** |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
328 * open a new RTP parse context for stream 'st'. 'st' can be NULL for |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
329 * MPEG2TS streams to indicate that they should be demuxed inside the |
885 | 330 * rtp demux (otherwise CODEC_ID_MPEG2TS packets are returned) |
1431
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
331 * TODO: change this to not take rtp_payload data, and use the new dynamic payload system. |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
332 */ |
1425
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
333 RTPDemuxContext *rtp_parse_open(AVFormatContext *s1, AVStream *st, URLContext *rtpc, int payload_type, rtp_payload_data_t *rtp_payload_data) |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
334 { |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
335 RTPDemuxContext *s; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
336 |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
337 s = av_mallocz(sizeof(RTPDemuxContext)); |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
338 if (!s) |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
339 return NULL; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
340 s->payload_type = payload_type; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
341 s->last_rtcp_ntp_time = AV_NOPTS_VALUE; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
342 s->first_rtcp_ntp_time = AV_NOPTS_VALUE; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
343 s->ic = s1; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
344 s->st = st; |
774 | 345 s->rtp_payload_data = rtp_payload_data; |
346 if (!strcmp(AVRtpPayloadTypes[payload_type].enc_name, "MP2T")) { | |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
347 s->ts = mpegts_parse_open(s->ic); |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
348 if (s->ts == NULL) { |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
349 av_free(s); |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
350 return NULL; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
351 } |
305 | 352 } else { |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
777
diff
changeset
|
353 switch(st->codec->codec_id) { |
305 | 354 case CODEC_ID_MPEG1VIDEO: |
355 case CODEC_ID_MPEG2VIDEO: | |
356 case CODEC_ID_MP2: | |
357 case CODEC_ID_MP3: | |
358 case CODEC_ID_MPEG4: | |
1419 | 359 #ifdef RTP_H264 |
360 case CODEC_ID_H264: | |
361 #endif | |
305 | 362 st->need_parsing = 1; |
363 break; | |
364 default: | |
365 break; | |
366 } | |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
367 } |
1425
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
368 // needed to send back RTCP RR in RTSP sessions |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
369 s->rtp_ctx = rtpc; |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
370 gethostname(s->hostname, sizeof(s->hostname)); |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
371 return s; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
372 } |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
373 |
774 | 374 static int rtp_parse_mp4_au(RTPDemuxContext *s, const uint8_t *buf) |
375 { | |
376 int au_headers_length, au_header_size, i; | |
377 GetBitContext getbitcontext; | |
378 rtp_payload_data_t *infos; | |
379 | |
380 infos = s->rtp_payload_data; | |
381 | |
382 if (infos == NULL) | |
383 return -1; | |
384 | |
385 /* decode the first 2 bytes where are stored the AUHeader sections | |
386 length in bits */ | |
387 au_headers_length = BE_16(buf); | |
388 | |
389 if (au_headers_length > RTP_MAX_PACKET_LENGTH) | |
390 return -1; | |
391 | |
392 infos->au_headers_length_bytes = (au_headers_length + 7) / 8; | |
393 | |
394 /* skip AU headers length section (2 bytes) */ | |
395 buf += 2; | |
396 | |
397 init_get_bits(&getbitcontext, buf, infos->au_headers_length_bytes * 8); | |
398 | |
399 /* XXX: Wrong if optionnal additional sections are present (cts, dts etc...) */ | |
400 au_header_size = infos->sizelength + infos->indexlength; | |
401 if (au_header_size <= 0 || (au_headers_length % au_header_size != 0)) | |
402 return -1; | |
403 | |
404 infos->nb_au_headers = au_headers_length / au_header_size; | |
405 infos->au_headers = av_malloc(sizeof(struct AUHeaders) * infos->nb_au_headers); | |
406 | |
407 /* XXX: We handle multiple AU Section as only one (need to fix this for interleaving) | |
408 In my test, the faad decoder doesnt behave correctly when sending each AU one by one | |
409 but does when sending the whole as one big packet... */ | |
410 infos->au_headers[0].size = 0; | |
411 infos->au_headers[0].index = 0; | |
412 for (i = 0; i < infos->nb_au_headers; ++i) { | |
413 infos->au_headers[0].size += get_bits_long(&getbitcontext, infos->sizelength); | |
414 infos->au_headers[0].index = get_bits_long(&getbitcontext, infos->indexlength); | |
415 } | |
416 | |
417 infos->nb_au_headers = 1; | |
418 | |
419 return 0; | |
420 } | |
421 | |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
422 /** |
1431
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
423 * This was the second switch in rtp_parse packet. Normalizes time, if required, sets stream_index, etc. |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
424 */ |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
425 static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp) |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
426 { |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
427 switch(s->st->codec->codec_id) { |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
428 case CODEC_ID_MP2: |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
429 case CODEC_ID_MPEG1VIDEO: |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
430 if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE) { |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
431 int64_t addend; |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
432 |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
433 int delta_timestamp; |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
434 /* XXX: is it really necessary to unify the timestamp base ? */ |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
435 /* compute pts from timestamp with received ntp_time */ |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
436 delta_timestamp = timestamp - s->last_rtcp_timestamp; |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
437 /* convert to 90 kHz without overflow */ |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
438 addend = (s->last_rtcp_ntp_time - s->first_rtcp_ntp_time) >> 14; |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
439 addend = (addend * 5625) >> 14; |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
440 pkt->pts = addend + delta_timestamp; |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
441 } |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
442 break; |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
443 case CODEC_ID_MPEG4AAC: |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
444 case CODEC_ID_H264: |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
445 case CODEC_ID_MPEG4: |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
446 pkt->pts = timestamp; |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
447 break; |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
448 default: |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
449 /* no timestamp info yet */ |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
450 break; |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
451 } |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
452 pkt->stream_index = s->st->index; |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
453 } |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
454 |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
455 /** |
885 | 456 * Parse an RTP or RTCP packet directly sent as a buffer. |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
457 * @param s RTP parse context. |
0 | 458 * @param pkt returned packet |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
459 * @param buf input buffer or NULL to read the next packets |
0 | 460 * @param len buffer len |
885 | 461 * @return 0 if a packet is returned, 1 if a packet is returned and more can follow |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
462 * (use buf as NULL to read the next). -1 if no packet (error or no more packet). |
0 | 463 */ |
885 | 464 int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt, |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
465 const uint8_t *buf, int len) |
0 | 466 { |
467 unsigned int ssrc, h; | |
1431
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
468 int payload_type, seq, ret; |
0 | 469 AVStream *st; |
65 | 470 uint32_t timestamp; |
1431
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
471 int rv= 0; |
885 | 472 |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
473 if (!buf) { |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
474 /* return the next packets, if any */ |
1419 | 475 if(s->st && s->parse_packet) { |
1431
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
476 timestamp= 0; ///< Should not be used if buf is NULL, but should be set to the timestamp of the packet returned.... |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
477 rv= s->parse_packet(s, pkt, ×tamp, NULL, 0); |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
478 finalize_packet(s, pkt, timestamp); |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
479 return rv; |
1419 | 480 } else { |
1431
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
481 // TODO: Move to a dynamic packet handler (like above) |
1424
1c39ce5c6a5d
indentation fix, patch by Ryan Martell % rdm4 A martellventures P com %
gpoirier
parents:
1419
diff
changeset
|
482 if (s->read_buf_index >= s->read_buf_size) |
1c39ce5c6a5d
indentation fix, patch by Ryan Martell % rdm4 A martellventures P com %
gpoirier
parents:
1419
diff
changeset
|
483 return -1; |
1c39ce5c6a5d
indentation fix, patch by Ryan Martell % rdm4 A martellventures P com %
gpoirier
parents:
1419
diff
changeset
|
484 ret = mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index, |
1c39ce5c6a5d
indentation fix, patch by Ryan Martell % rdm4 A martellventures P com %
gpoirier
parents:
1419
diff
changeset
|
485 s->read_buf_size - s->read_buf_index); |
1c39ce5c6a5d
indentation fix, patch by Ryan Martell % rdm4 A martellventures P com %
gpoirier
parents:
1419
diff
changeset
|
486 if (ret < 0) |
1c39ce5c6a5d
indentation fix, patch by Ryan Martell % rdm4 A martellventures P com %
gpoirier
parents:
1419
diff
changeset
|
487 return -1; |
1c39ce5c6a5d
indentation fix, patch by Ryan Martell % rdm4 A martellventures P com %
gpoirier
parents:
1419
diff
changeset
|
488 s->read_buf_index += ret; |
1c39ce5c6a5d
indentation fix, patch by Ryan Martell % rdm4 A martellventures P com %
gpoirier
parents:
1419
diff
changeset
|
489 if (s->read_buf_index < s->read_buf_size) |
1c39ce5c6a5d
indentation fix, patch by Ryan Martell % rdm4 A martellventures P com %
gpoirier
parents:
1419
diff
changeset
|
490 return 1; |
1c39ce5c6a5d
indentation fix, patch by Ryan Martell % rdm4 A martellventures P com %
gpoirier
parents:
1419
diff
changeset
|
491 else |
1c39ce5c6a5d
indentation fix, patch by Ryan Martell % rdm4 A martellventures P com %
gpoirier
parents:
1419
diff
changeset
|
492 return 0; |
1419 | 493 } |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
494 } |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
495 |
0 | 496 if (len < 12) |
497 return -1; | |
498 | |
499 if ((buf[0] & 0xc0) != (RTP_VERSION << 6)) | |
500 return -1; | |
501 if (buf[1] >= 200 && buf[1] <= 204) { | |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
502 rtcp_parse_packet(s, buf, len); |
0 | 503 return -1; |
504 } | |
505 payload_type = buf[1] & 0x7f; | |
506 seq = (buf[2] << 8) | buf[3]; | |
507 timestamp = decode_be32(buf + 4); | |
508 ssrc = decode_be32(buf + 8); | |
1425
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
509 /* store the ssrc in the RTPDemuxContext */ |
00d9393a126f
make ffmpeg able to send back a RTCP receiver report.
gpoirier
parents:
1424
diff
changeset
|
510 s->ssrc = ssrc; |
885 | 511 |
0 | 512 /* NOTE: we can handle only one payload type */ |
513 if (s->payload_type != payload_type) | |
514 return -1; | |
986
f5194ed612b5
fix debug code: do not try to take st->codec if st is NULL. This makes ffplay give me pics from FreeBoxTV \o/
mmu_man
parents:
896
diff
changeset
|
515 |
f5194ed612b5
fix debug code: do not try to take st->codec if st is NULL. This makes ffplay give me pics from FreeBoxTV \o/
mmu_man
parents:
896
diff
changeset
|
516 st = s->st; |
0 | 517 #if defined(DEBUG) || 1 |
518 if (seq != ((s->seq + 1) & 0xffff)) { | |
986
f5194ed612b5
fix debug code: do not try to take st->codec if st is NULL. This makes ffplay give me pics from FreeBoxTV \o/
mmu_man
parents:
896
diff
changeset
|
519 av_log(st?st->codec:NULL, AV_LOG_ERROR, "RTP: PT=%02x: bad cseq %04x expected=%04x\n", |
0 | 520 payload_type, seq, ((s->seq + 1) & 0xffff)); |
521 } | |
774 | 522 #endif |
0 | 523 s->seq = seq; |
524 len -= 12; | |
525 buf += 12; | |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
526 |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
527 if (!st) { |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
528 /* specific MPEG2TS demux support */ |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
529 ret = mpegts_parse_packet(s->ts, pkt, buf, len); |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
530 if (ret < 0) |
0 | 531 return -1; |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
532 if (ret < len) { |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
533 s->read_buf_size = len - ret; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
534 memcpy(s->buf, buf + ret, s->read_buf_size); |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
535 s->read_buf_index = 0; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
536 return 1; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
537 } |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
538 } else { |
1419 | 539 // at this point, the RTP header has been stripped; This is ASSUMING that there is only 1 CSRC, which in't wise. |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
777
diff
changeset
|
540 switch(st->codec->codec_id) { |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
541 case CODEC_ID_MP2: |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
542 /* better than nothing: skip mpeg audio RTP header */ |
0 | 543 if (len <= 4) |
544 return -1; | |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
545 h = decode_be32(buf); |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
546 len -= 4; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
547 buf += 4; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
548 av_new_packet(pkt, len); |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
549 memcpy(pkt->data, buf, len); |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
550 break; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
551 case CODEC_ID_MPEG1VIDEO: |
305 | 552 /* better than nothing: skip mpeg video RTP header */ |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
553 if (len <= 4) |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
554 return -1; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
555 h = decode_be32(buf); |
0 | 556 buf += 4; |
557 len -= 4; | |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
558 if (h & (1 << 26)) { |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
559 /* mpeg2 */ |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
560 if (len <= 4) |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
561 return -1; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
562 buf += 4; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
563 len -= 4; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
564 } |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
565 av_new_packet(pkt, len); |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
566 memcpy(pkt->data, buf, len); |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
567 break; |
1427
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
568 // moved from below, verbatim. this is because this section handles packets, and the lower switch handles |
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
569 // timestamps. |
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
570 // TODO: Put this into a dynamic packet handler... |
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
571 case CODEC_ID_MPEG4AAC: |
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
572 if (rtp_parse_mp4_au(s, buf)) |
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
573 return -1; |
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
574 { |
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
575 rtp_payload_data_t *infos = s->rtp_payload_data; |
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
576 if (infos == NULL) |
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
577 return -1; |
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
578 buf += infos->au_headers_length_bytes + 2; |
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
579 len -= infos->au_headers_length_bytes + 2; |
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
580 |
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
581 /* XXX: Fixme we only handle the case where rtp_parse_mp4_au define |
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
582 one au_header */ |
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
583 av_new_packet(pkt, infos->au_headers[0].size); |
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
584 memcpy(pkt->data, buf, infos->au_headers[0].size); |
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
585 buf += infos->au_headers[0].size; |
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
586 len -= infos->au_headers[0].size; |
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
587 } |
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
588 s->read_buf_size = len; |
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
589 s->buf_ptr = buf; |
1431
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
590 rv= 0; |
1427
f1614c754d5b
move up (in the file) AAC audio handling moved from below
gpoirier
parents:
1425
diff
changeset
|
591 break; |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
592 default: |
1419 | 593 if(s->parse_packet) { |
1431
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
594 rv= s->parse_packet(s, pkt, ×tamp, buf, len); |
1419 | 595 } else { |
1424
1c39ce5c6a5d
indentation fix, patch by Ryan Martell % rdm4 A martellventures P com %
gpoirier
parents:
1419
diff
changeset
|
596 av_new_packet(pkt, len); |
1c39ce5c6a5d
indentation fix, patch by Ryan Martell % rdm4 A martellventures P com %
gpoirier
parents:
1419
diff
changeset
|
597 memcpy(pkt->data, buf, len); |
1419 | 598 } |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
599 break; |
0 | 600 } |
885 | 601 |
1431
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
602 // now perform timestamp things.... |
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
603 finalize_packet(s, pkt, timestamp); |
0 | 604 } |
1431
2d8a17631520
fix more dynamic protocol stuff, needed by the forthcoming h264
gpoirier
parents:
1427
diff
changeset
|
605 return rv; |
0 | 606 } |
607 | |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
608 void rtp_parse_close(RTPDemuxContext *s) |
0 | 609 { |
1419 | 610 // TODO: fold this into the protocol specific data fields. |
774 | 611 if (!strcmp(AVRtpPayloadTypes[s->payload_type].enc_name, "MP2T")) { |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
612 mpegts_parse_close(s->ts); |
0 | 613 } |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
614 av_free(s); |
0 | 615 } |
616 | |
617 /* rtp output */ | |
618 | |
619 static int rtp_write_header(AVFormatContext *s1) | |
620 { | |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
621 RTPDemuxContext *s = s1->priv_data; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
622 int payload_type, max_packet_size, n; |
0 | 623 AVStream *st; |
624 | |
625 if (s1->nb_streams != 1) | |
626 return -1; | |
627 st = s1->streams[0]; | |
628 | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
777
diff
changeset
|
629 payload_type = rtp_get_payload_type(st->codec); |
0 | 630 if (payload_type < 0) |
631 payload_type = RTP_PT_PRIVATE; /* private payload type */ | |
632 s->payload_type = payload_type; | |
633 | |
1099 | 634 // following 2 FIXMies could be set based on the current time, theres normaly no info leak, as rtp will likely be transmitted immedeatly |
1045
7f1b7f811f01
fix constraint violation: libavformat is not allowed to modify state of caller, including rng state
rfelker
parents:
986
diff
changeset
|
635 s->base_timestamp = 0; /* FIXME: was random(), what should this be? */ |
0 | 636 s->timestamp = s->base_timestamp; |
1045
7f1b7f811f01
fix constraint violation: libavformat is not allowed to modify state of caller, including rng state
rfelker
parents:
986
diff
changeset
|
637 s->ssrc = 0; /* FIXME: was random(), what should this be? */ |
0 | 638 s->first_packet = 1; |
639 | |
640 max_packet_size = url_fget_max_packet_size(&s1->pb); | |
641 if (max_packet_size <= 12) | |
642 return AVERROR_IO; | |
643 s->max_payload_size = max_packet_size - 12; | |
644 | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
777
diff
changeset
|
645 switch(st->codec->codec_id) { |
0 | 646 case CODEC_ID_MP2: |
232 | 647 case CODEC_ID_MP3: |
0 | 648 s->buf_ptr = s->buf + 4; |
649 s->cur_timestamp = 0; | |
650 break; | |
651 case CODEC_ID_MPEG1VIDEO: | |
652 s->cur_timestamp = 0; | |
653 break; | |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
654 case CODEC_ID_MPEG2TS: |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
655 n = s->max_payload_size / TS_PACKET_SIZE; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
656 if (n < 1) |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
657 n = 1; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
658 s->max_payload_size = n * TS_PACKET_SIZE; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
659 s->buf_ptr = s->buf; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
660 break; |
0 | 661 default: |
662 s->buf_ptr = s->buf; | |
663 break; | |
664 } | |
665 | |
666 return 0; | |
667 } | |
668 | |
669 /* send an rtcp sender report packet */ | |
65 | 670 static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time) |
0 | 671 { |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
672 RTPDemuxContext *s = s1->priv_data; |
0 | 673 #if defined(DEBUG) |
674 printf("RTCP: %02x %Lx %x\n", s->payload_type, ntp_time, s->timestamp); | |
675 #endif | |
676 put_byte(&s1->pb, (RTP_VERSION << 6)); | |
677 put_byte(&s1->pb, 200); | |
678 put_be16(&s1->pb, 6); /* length in words - 1 */ | |
679 put_be32(&s1->pb, s->ssrc); | |
680 put_be64(&s1->pb, ntp_time); | |
681 put_be32(&s1->pb, s->timestamp); | |
682 put_be32(&s1->pb, s->packet_count); | |
683 put_be32(&s1->pb, s->octet_count); | |
684 put_flush_packet(&s1->pb); | |
685 } | |
686 | |
687 /* send an rtp packet. sequence number is incremented, but the caller | |
688 must update the timestamp itself */ | |
826
ee7ba4017c8d
MPEG4 streaming over RTP patch by (Luca Abeni: lucabe72, email it)
michael
parents:
820
diff
changeset
|
689 static void rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m) |
0 | 690 { |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
691 RTPDemuxContext *s = s1->priv_data; |
0 | 692 |
693 #ifdef DEBUG | |
694 printf("rtp_send_data size=%d\n", len); | |
695 #endif | |
696 | |
697 /* build the RTP header */ | |
698 put_byte(&s1->pb, (RTP_VERSION << 6)); | |
826
ee7ba4017c8d
MPEG4 streaming over RTP patch by (Luca Abeni: lucabe72, email it)
michael
parents:
820
diff
changeset
|
699 put_byte(&s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7)); |
0 | 700 put_be16(&s1->pb, s->seq); |
701 put_be32(&s1->pb, s->timestamp); | |
702 put_be32(&s1->pb, s->ssrc); | |
885 | 703 |
0 | 704 put_buffer(&s1->pb, buf1, len); |
705 put_flush_packet(&s1->pb); | |
885 | 706 |
0 | 707 s->seq++; |
708 s->octet_count += len; | |
709 s->packet_count++; | |
710 } | |
711 | |
712 /* send an integer number of samples and compute time stamp and fill | |
713 the rtp send buffer before sending. */ | |
714 static void rtp_send_samples(AVFormatContext *s1, | |
241 | 715 const uint8_t *buf1, int size, int sample_size) |
0 | 716 { |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
717 RTPDemuxContext *s = s1->priv_data; |
0 | 718 int len, max_packet_size, n; |
719 | |
720 max_packet_size = (s->max_payload_size / sample_size) * sample_size; | |
721 /* not needed, but who nows */ | |
722 if ((size % sample_size) != 0) | |
723 av_abort(); | |
724 while (size > 0) { | |
725 len = (max_packet_size - (s->buf_ptr - s->buf)); | |
726 if (len > size) | |
727 len = size; | |
728 | |
729 /* copy data */ | |
730 memcpy(s->buf_ptr, buf1, len); | |
731 s->buf_ptr += len; | |
732 buf1 += len; | |
733 size -= len; | |
734 n = (s->buf_ptr - s->buf); | |
735 /* if buffer full, then send it */ | |
736 if (n >= max_packet_size) { | |
826
ee7ba4017c8d
MPEG4 streaming over RTP patch by (Luca Abeni: lucabe72, email it)
michael
parents:
820
diff
changeset
|
737 rtp_send_data(s1, s->buf, n, 0); |
0 | 738 s->buf_ptr = s->buf; |
739 /* update timestamp */ | |
740 s->timestamp += n / sample_size; | |
741 } | |
742 } | |
885 | 743 } |
0 | 744 |
745 /* NOTE: we suppose that exactly one frame is given as argument here */ | |
746 /* XXX: test it */ | |
747 static void rtp_send_mpegaudio(AVFormatContext *s1, | |
241 | 748 const uint8_t *buf1, int size) |
0 | 749 { |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
750 RTPDemuxContext *s = s1->priv_data; |
0 | 751 AVStream *st = s1->streams[0]; |
752 int len, count, max_packet_size; | |
753 | |
754 max_packet_size = s->max_payload_size; | |
755 | |
756 /* test if we must flush because not enough space */ | |
757 len = (s->buf_ptr - s->buf); | |
758 if ((len + size) > max_packet_size) { | |
759 if (len > 4) { | |
826
ee7ba4017c8d
MPEG4 streaming over RTP patch by (Luca Abeni: lucabe72, email it)
michael
parents:
820
diff
changeset
|
760 rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0); |
0 | 761 s->buf_ptr = s->buf + 4; |
762 /* 90 KHz time stamp */ | |
885 | 763 s->timestamp = s->base_timestamp + |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
777
diff
changeset
|
764 (s->cur_timestamp * 90000LL) / st->codec->sample_rate; |
0 | 765 } |
766 } | |
767 | |
768 /* add the packet */ | |
769 if (size > max_packet_size) { | |
770 /* big packet: fragment */ | |
771 count = 0; | |
772 while (size > 0) { | |
773 len = max_packet_size - 4; | |
774 if (len > size) | |
775 len = size; | |
776 /* build fragmented packet */ | |
777 s->buf[0] = 0; | |
778 s->buf[1] = 0; | |
779 s->buf[2] = count >> 8; | |
780 s->buf[3] = count; | |
781 memcpy(s->buf + 4, buf1, len); | |
826
ee7ba4017c8d
MPEG4 streaming over RTP patch by (Luca Abeni: lucabe72, email it)
michael
parents:
820
diff
changeset
|
782 rtp_send_data(s1, s->buf, len + 4, 0); |
0 | 783 size -= len; |
784 buf1 += len; | |
785 count += len; | |
786 } | |
787 } else { | |
788 if (s->buf_ptr == s->buf + 4) { | |
789 /* no fragmentation possible */ | |
790 s->buf[0] = 0; | |
791 s->buf[1] = 0; | |
792 s->buf[2] = 0; | |
793 s->buf[3] = 0; | |
794 } | |
795 memcpy(s->buf_ptr, buf1, size); | |
796 s->buf_ptr += size; | |
797 } | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
777
diff
changeset
|
798 s->cur_timestamp += st->codec->frame_size; |
0 | 799 } |
800 | |
801 /* NOTE: a single frame must be passed with sequence header if | |
802 needed. XXX: use slices. */ | |
803 static void rtp_send_mpegvideo(AVFormatContext *s1, | |
241 | 804 const uint8_t *buf1, int size) |
0 | 805 { |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
806 RTPDemuxContext *s = s1->priv_data; |
0 | 807 AVStream *st = s1->streams[0]; |
808 int len, h, max_packet_size; | |
65 | 809 uint8_t *q; |
0 | 810 |
811 max_packet_size = s->max_payload_size; | |
812 | |
813 while (size > 0) { | |
814 /* XXX: more correct headers */ | |
815 h = 0; | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
777
diff
changeset
|
816 if (st->codec->sub_id == 2) |
0 | 817 h |= 1 << 26; /* mpeg 2 indicator */ |
818 q = s->buf; | |
819 *q++ = h >> 24; | |
820 *q++ = h >> 16; | |
821 *q++ = h >> 8; | |
822 *q++ = h; | |
823 | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
777
diff
changeset
|
824 if (st->codec->sub_id == 2) { |
0 | 825 h = 0; |
826 *q++ = h >> 24; | |
827 *q++ = h >> 16; | |
828 *q++ = h >> 8; | |
829 *q++ = h; | |
830 } | |
885 | 831 |
0 | 832 len = max_packet_size - (q - s->buf); |
833 if (len > size) | |
834 len = size; | |
835 | |
836 memcpy(q, buf1, len); | |
837 q += len; | |
838 | |
839 /* 90 KHz time stamp */ | |
885 | 840 s->timestamp = s->base_timestamp + |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
777
diff
changeset
|
841 av_rescale((int64_t)s->cur_timestamp * st->codec->time_base.num, 90000, st->codec->time_base.den); //FIXME pass timestamps |
827
d064b7d0899d
mpeg1video m bit fix by (Luca Abeni: lucabe72, email it)
michael
parents:
826
diff
changeset
|
842 rtp_send_data(s1, s->buf, q - s->buf, (len == size)); |
0 | 843 |
844 buf1 += len; | |
845 size -= len; | |
846 } | |
847 s->cur_timestamp++; | |
848 } | |
849 | |
850 static void rtp_send_raw(AVFormatContext *s1, | |
241 | 851 const uint8_t *buf1, int size) |
0 | 852 { |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
853 RTPDemuxContext *s = s1->priv_data; |
0 | 854 AVStream *st = s1->streams[0]; |
855 int len, max_packet_size; | |
856 | |
857 max_packet_size = s->max_payload_size; | |
858 | |
859 while (size > 0) { | |
860 len = max_packet_size; | |
861 if (len > size) | |
862 len = size; | |
863 | |
864 /* 90 KHz time stamp */ | |
885 | 865 s->timestamp = s->base_timestamp + |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
777
diff
changeset
|
866 av_rescale((int64_t)s->cur_timestamp * st->codec->time_base.num, 90000, st->codec->time_base.den); //FIXME pass timestamps |
826
ee7ba4017c8d
MPEG4 streaming over RTP patch by (Luca Abeni: lucabe72, email it)
michael
parents:
820
diff
changeset
|
867 rtp_send_data(s1, buf1, len, (len == size)); |
0 | 868 |
869 buf1 += len; | |
870 size -= len; | |
871 } | |
872 s->cur_timestamp++; | |
873 } | |
874 | |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
875 /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */ |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
876 static void rtp_send_mpegts_raw(AVFormatContext *s1, |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
877 const uint8_t *buf1, int size) |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
878 { |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
879 RTPDemuxContext *s = s1->priv_data; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
880 int len, out_len; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
881 |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
882 while (size >= TS_PACKET_SIZE) { |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
883 len = s->max_payload_size - (s->buf_ptr - s->buf); |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
884 if (len > size) |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
885 len = size; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
886 memcpy(s->buf_ptr, buf1, len); |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
887 buf1 += len; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
888 size -= len; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
889 s->buf_ptr += len; |
885 | 890 |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
891 out_len = s->buf_ptr - s->buf; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
892 if (out_len >= s->max_payload_size) { |
826
ee7ba4017c8d
MPEG4 streaming over RTP patch by (Luca Abeni: lucabe72, email it)
michael
parents:
820
diff
changeset
|
893 rtp_send_data(s1, s->buf, out_len, 0); |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
894 s->buf_ptr = s->buf; |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
895 } |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
896 } |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
897 } |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
898 |
0 | 899 /* write an RTP packet. 'buf1' must contain a single specific frame. */ |
468 | 900 static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt) |
0 | 901 { |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
902 RTPDemuxContext *s = s1->priv_data; |
0 | 903 AVStream *st = s1->streams[0]; |
904 int rtcp_bytes; | |
65 | 905 int64_t ntp_time; |
468 | 906 int size= pkt->size; |
907 uint8_t *buf1= pkt->data; | |
885 | 908 |
0 | 909 #ifdef DEBUG |
468 | 910 printf("%d: write len=%d\n", pkt->stream_index, size); |
0 | 911 #endif |
912 | |
913 /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */ | |
885 | 914 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) / |
0 | 915 RTCP_TX_RATIO_DEN; |
916 if (s->first_packet || rtcp_bytes >= 28) { | |
917 /* compute NTP time */ | |
173 | 918 /* XXX: 90 kHz timestamp hardcoded */ |
468 | 919 ntp_time = (pkt->pts << 28) / 5625; |
885 | 920 rtcp_send_sr(s1, ntp_time); |
0 | 921 s->last_octet_count = s->octet_count; |
922 s->first_packet = 0; | |
923 } | |
924 | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
777
diff
changeset
|
925 switch(st->codec->codec_id) { |
0 | 926 case CODEC_ID_PCM_MULAW: |
927 case CODEC_ID_PCM_ALAW: | |
928 case CODEC_ID_PCM_U8: | |
929 case CODEC_ID_PCM_S8: | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
777
diff
changeset
|
930 rtp_send_samples(s1, buf1, size, 1 * st->codec->channels); |
0 | 931 break; |
932 case CODEC_ID_PCM_U16BE: | |
933 case CODEC_ID_PCM_U16LE: | |
934 case CODEC_ID_PCM_S16BE: | |
935 case CODEC_ID_PCM_S16LE: | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
777
diff
changeset
|
936 rtp_send_samples(s1, buf1, size, 2 * st->codec->channels); |
0 | 937 break; |
938 case CODEC_ID_MP2: | |
232 | 939 case CODEC_ID_MP3: |
0 | 940 rtp_send_mpegaudio(s1, buf1, size); |
941 break; | |
942 case CODEC_ID_MPEG1VIDEO: | |
943 rtp_send_mpegvideo(s1, buf1, size); | |
944 break; | |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
945 case CODEC_ID_MPEG2TS: |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
946 rtp_send_mpegts_raw(s1, buf1, size); |
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
947 break; |
0 | 948 default: |
949 /* better than nothing : send the codec raw data */ | |
950 rtp_send_raw(s1, buf1, size); | |
951 break; | |
952 } | |
953 return 0; | |
954 } | |
955 | |
956 static int rtp_write_trailer(AVFormatContext *s1) | |
957 { | |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
958 // RTPDemuxContext *s = s1->priv_data; |
0 | 959 return 0; |
960 } | |
961 | |
1167 | 962 AVOutputFormat rtp_muxer = { |
0 | 963 "rtp", |
964 "RTP output format", | |
965 NULL, | |
966 NULL, | |
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
967 sizeof(RTPDemuxContext), |
0 | 968 CODEC_ID_PCM_MULAW, |
969 CODEC_ID_NONE, | |
970 rtp_write_header, | |
971 rtp_write_packet, | |
972 rtp_write_trailer, | |
973 }; |