Mercurial > libavformat.hg
annotate rtp.c @ 543:acf8a88674ff libavformat
cleanup
| author | michael |
|---|---|
| date | Sun, 03 Oct 2004 02:57:42 +0000 |
| parents | 60f897e8dd2d |
| children | af4e24d6310c |
| rev | line source |
|---|---|
| 0 | 1 /* |
| 2 * RTP input/output format | |
| 3 * Copyright (c) 2002 Fabrice Bellard. | |
| 4 * | |
| 5 * This library is free software; you can redistribute it and/or | |
| 6 * modify it under the terms of the GNU Lesser General Public | |
| 7 * License as published by the Free Software Foundation; either | |
| 8 * version 2 of the License, or (at your option) any later version. | |
| 9 * | |
| 10 * This library is distributed in the hope that it will be useful, | |
| 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 * Lesser General Public License for more details. | |
| 14 * | |
| 15 * You should have received a copy of the GNU Lesser General Public | |
| 16 * License along with this library; if not, write to the Free Software | |
| 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 18 */ | |
| 19 #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
|
20 #include "mpegts.h" |
| 0 | 21 |
| 22 #include <unistd.h> | |
| 23 #include <sys/types.h> | |
| 24 #include <sys/socket.h> | |
| 25 #include <netinet/in.h> | |
| 26 #ifndef __BEOS__ | |
| 27 # include <arpa/inet.h> | |
| 28 #else | |
| 29 # include "barpainet.h" | |
| 30 #endif | |
| 31 #include <netdb.h> | |
| 32 | |
| 33 //#define DEBUG | |
| 34 | |
| 35 | |
| 36 /* TODO: - add RTCP statistics reporting (should be optional). | |
| 37 | |
| 38 - add support for h263/mpeg4 packetized output : IDEA: send a | |
| 39 buffer to 'rtp_write_packet' contains all the packets for ONE | |
| 40 frame. Each packet should have a four byte header containing | |
| 41 the length in big endian format (same trick as | |
| 42 'url_open_dyn_packet_buf') | |
| 43 */ | |
| 44 | |
| 45 #define RTP_VERSION 2 | |
| 46 | |
| 47 #define RTP_MAX_SDES 256 /* maximum text length for SDES */ | |
| 48 | |
| 49 /* RTCP paquets use 0.5 % of the bandwidth */ | |
| 50 #define RTCP_TX_RATIO_NUM 5 | |
| 51 #define RTCP_TX_RATIO_DEN 1000 | |
| 52 | |
| 53 typedef enum { | |
| 54 RTCP_SR = 200, | |
| 55 RTCP_RR = 201, | |
| 56 RTCP_SDES = 202, | |
| 57 RTCP_BYE = 203, | |
| 58 RTCP_APP = 204 | |
| 59 } rtcp_type_t; | |
| 60 | |
| 61 typedef enum { | |
| 62 RTCP_SDES_END = 0, | |
| 63 RTCP_SDES_CNAME = 1, | |
| 64 RTCP_SDES_NAME = 2, | |
| 65 RTCP_SDES_EMAIL = 3, | |
| 66 RTCP_SDES_PHONE = 4, | |
| 67 RTCP_SDES_LOC = 5, | |
| 68 RTCP_SDES_TOOL = 6, | |
| 69 RTCP_SDES_NOTE = 7, | |
| 70 RTCP_SDES_PRIV = 8, | |
| 71 RTCP_SDES_IMG = 9, | |
| 72 RTCP_SDES_DOOR = 10, | |
| 73 RTCP_SDES_SOURCE = 11 | |
| 74 } rtcp_sdes_type_t; | |
| 75 | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
76 struct RTPDemuxContext { |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
77 AVFormatContext *ic; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
78 AVStream *st; |
| 0 | 79 int payload_type; |
| 65 | 80 uint32_t ssrc; |
| 81 uint16_t seq; | |
| 82 uint32_t timestamp; | |
| 83 uint32_t base_timestamp; | |
| 84 uint32_t cur_timestamp; | |
| 0 | 85 int max_payload_size; |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
86 MpegTSContext *ts; /* only used for RTP_PT_MPEG2TS payloads */ |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
87 int read_buf_index; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
88 int 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
|
89 |
| 0 | 90 /* rtcp sender statistics receive */ |
| 65 | 91 int64_t last_rtcp_ntp_time; |
| 173 | 92 int64_t first_rtcp_ntp_time; |
| 65 | 93 uint32_t last_rtcp_timestamp; |
| 0 | 94 /* rtcp sender statistics */ |
| 95 unsigned int packet_count; | |
| 96 unsigned int octet_count; | |
| 97 unsigned int last_octet_count; | |
| 98 int first_packet; | |
| 99 /* buffer for output */ | |
| 65 | 100 uint8_t buf[RTP_MAX_PACKET_LENGTH]; |
| 101 uint8_t *buf_ptr; | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
102 }; |
| 0 | 103 |
| 104 int rtp_get_codec_info(AVCodecContext *codec, int payload_type) | |
| 105 { | |
| 106 switch(payload_type) { | |
| 107 case RTP_PT_ULAW: | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
108 codec->codec_type = CODEC_TYPE_AUDIO; |
| 0 | 109 codec->codec_id = CODEC_ID_PCM_MULAW; |
| 110 codec->channels = 1; | |
| 111 codec->sample_rate = 8000; | |
| 112 break; | |
| 113 case RTP_PT_ALAW: | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
114 codec->codec_type = CODEC_TYPE_AUDIO; |
| 0 | 115 codec->codec_id = CODEC_ID_PCM_ALAW; |
| 116 codec->channels = 1; | |
| 117 codec->sample_rate = 8000; | |
| 118 break; | |
| 119 case RTP_PT_S16BE_STEREO: | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
120 codec->codec_type = CODEC_TYPE_AUDIO; |
| 0 | 121 codec->codec_id = CODEC_ID_PCM_S16BE; |
| 122 codec->channels = 2; | |
| 123 codec->sample_rate = 44100; | |
| 124 break; | |
| 125 case RTP_PT_S16BE_MONO: | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
126 codec->codec_type = CODEC_TYPE_AUDIO; |
| 0 | 127 codec->codec_id = CODEC_ID_PCM_S16BE; |
| 128 codec->channels = 1; | |
| 129 codec->sample_rate = 44100; | |
| 130 break; | |
| 131 case RTP_PT_MPEGAUDIO: | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
132 codec->codec_type = CODEC_TYPE_AUDIO; |
| 0 | 133 codec->codec_id = CODEC_ID_MP2; |
| 134 break; | |
| 135 case RTP_PT_JPEG: | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
136 codec->codec_type = CODEC_TYPE_VIDEO; |
| 0 | 137 codec->codec_id = CODEC_ID_MJPEG; |
| 138 break; | |
| 139 case RTP_PT_MPEGVIDEO: | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
140 codec->codec_type = CODEC_TYPE_VIDEO; |
| 0 | 141 codec->codec_id = CODEC_ID_MPEG1VIDEO; |
| 142 break; | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
143 case RTP_PT_MPEG2TS: |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
144 codec->codec_type = CODEC_TYPE_DATA; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
145 codec->codec_id = 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
|
146 break; |
| 0 | 147 default: |
| 148 return -1; | |
| 149 } | |
| 150 return 0; | |
| 151 } | |
| 152 | |
| 153 /* return < 0 if unknown payload type */ | |
| 154 int rtp_get_payload_type(AVCodecContext *codec) | |
| 155 { | |
| 156 int payload_type; | |
| 157 | |
| 158 /* compute the payload type */ | |
| 159 payload_type = -1; | |
| 160 switch(codec->codec_id) { | |
| 161 case CODEC_ID_PCM_MULAW: | |
| 162 payload_type = RTP_PT_ULAW; | |
| 163 break; | |
| 164 case CODEC_ID_PCM_ALAW: | |
| 165 payload_type = RTP_PT_ALAW; | |
| 166 break; | |
| 167 case CODEC_ID_PCM_S16BE: | |
| 168 if (codec->channels == 1) { | |
| 169 payload_type = RTP_PT_S16BE_MONO; | |
| 170 } else if (codec->channels == 2) { | |
| 171 payload_type = RTP_PT_S16BE_STEREO; | |
| 172 } | |
| 173 break; | |
| 174 case CODEC_ID_MP2: | |
| 232 | 175 case CODEC_ID_MP3: |
| 0 | 176 payload_type = RTP_PT_MPEGAUDIO; |
| 177 break; | |
| 178 case CODEC_ID_MJPEG: | |
| 179 payload_type = RTP_PT_JPEG; | |
| 180 break; | |
| 181 case CODEC_ID_MPEG1VIDEO: | |
| 182 payload_type = RTP_PT_MPEGVIDEO; | |
| 183 break; | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
184 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
|
185 payload_type = RTP_PT_MPEG2TS; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
186 break; |
| 0 | 187 default: |
| 188 break; | |
| 189 } | |
| 190 return payload_type; | |
| 191 } | |
| 192 | |
| 65 | 193 static inline uint32_t decode_be32(const uint8_t *p) |
| 0 | 194 { |
| 195 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; | |
| 196 } | |
| 197 | |
| 173 | 198 static inline uint64_t decode_be64(const uint8_t *p) |
| 0 | 199 { |
| 65 | 200 return ((uint64_t)decode_be32(p) << 32) | decode_be32(p + 4); |
| 0 | 201 } |
| 202 | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
203 static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf, int len) |
| 0 | 204 { |
| 205 if (buf[1] != 200) | |
| 206 return -1; | |
| 207 s->last_rtcp_ntp_time = decode_be64(buf + 8); | |
| 173 | 208 if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE) |
| 209 s->first_rtcp_ntp_time = s->last_rtcp_ntp_time; | |
| 0 | 210 s->last_rtcp_timestamp = decode_be32(buf + 16); |
| 211 return 0; | |
| 212 } | |
| 213 | |
| 214 /** | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
215 * 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
|
216 * MPEG2TS streams to indicate that they should be demuxed inside the |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
217 * rtp demux (otherwise CODEC_ID_MPEG2TS packets are returned) |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
218 */ |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
219 RTPDemuxContext *rtp_parse_open(AVFormatContext *s1, AVStream *st, int payload_type) |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
220 { |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
221 RTPDemuxContext *s; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
222 |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
223 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
|
224 if (!s) |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
225 return NULL; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
226 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
|
227 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
|
228 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
|
229 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
|
230 s->st = st; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
231 if (payload_type == RTP_PT_MPEG2TS) { |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
232 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
|
233 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
|
234 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
|
235 return NULL; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
236 } |
| 305 | 237 } else { |
| 238 switch(st->codec.codec_id) { | |
| 239 case CODEC_ID_MPEG1VIDEO: | |
| 240 case CODEC_ID_MPEG2VIDEO: | |
| 241 case CODEC_ID_MP2: | |
| 242 case CODEC_ID_MP3: | |
| 243 case CODEC_ID_MPEG4: | |
| 244 st->need_parsing = 1; | |
| 245 break; | |
| 246 default: | |
| 247 break; | |
| 248 } | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
249 } |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
250 return s; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
251 } |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
252 |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
253 /** |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
254 * Parse an RTP or RTCP packet directly sent as a buffer. |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
255 * @param s RTP parse context. |
| 0 | 256 * @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
|
257 * @param buf input buffer or NULL to read the next packets |
| 0 | 258 * @param len buffer len |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
259 * @return 0 if a packet is returned, 1 if a packet is returned and more can follow |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
260 * (use buf as NULL to read the next). -1 if no packet (error or no more packet). |
| 0 | 261 */ |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
262 int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt, |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
263 const uint8_t *buf, int len) |
| 0 | 264 { |
| 265 unsigned int ssrc, h; | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
266 int payload_type, seq, delta_timestamp, ret; |
| 0 | 267 AVStream *st; |
| 65 | 268 uint32_t timestamp; |
| 0 | 269 |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
270 if (!buf) { |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
271 /* return the next packets, if any */ |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
272 if (s->read_buf_index >= 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
|
273 return -1; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
274 ret = mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index, |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
275 s->read_buf_size - s->read_buf_index); |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
276 if (ret < 0) |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
277 return -1; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
278 s->read_buf_index += ret; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
279 if (s->read_buf_index < 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
|
280 return 1; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
281 else |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
282 return 0; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
283 } |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
284 |
| 0 | 285 if (len < 12) |
| 286 return -1; | |
| 287 | |
| 288 if ((buf[0] & 0xc0) != (RTP_VERSION << 6)) | |
| 289 return -1; | |
| 290 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
|
291 rtcp_parse_packet(s, buf, len); |
| 0 | 292 return -1; |
| 293 } | |
| 294 payload_type = buf[1] & 0x7f; | |
| 295 seq = (buf[2] << 8) | buf[3]; | |
| 296 timestamp = decode_be32(buf + 4); | |
| 297 ssrc = decode_be32(buf + 8); | |
| 298 | |
| 299 /* NOTE: we can handle only one payload type */ | |
| 300 if (s->payload_type != payload_type) | |
| 301 return -1; | |
| 302 #if defined(DEBUG) || 1 | |
| 303 if (seq != ((s->seq + 1) & 0xffff)) { | |
|
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
305
diff
changeset
|
304 av_log(&s->st->codec, AV_LOG_ERROR, "RTP: PT=%02x: bad cseq %04x expected=%04x\n", |
| 0 | 305 payload_type, seq, ((s->seq + 1) & 0xffff)); |
| 306 } | |
| 307 s->seq = seq; | |
| 308 #endif | |
| 309 len -= 12; | |
| 310 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
|
311 |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
312 st = s->st; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
313 if (!st) { |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
314 /* 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
|
315 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
|
316 if (ret < 0) |
| 0 | 317 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
|
318 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
|
319 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
|
320 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
|
321 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
|
322 return 1; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
323 } |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
324 } else { |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
325 switch(st->codec.codec_id) { |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
326 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
|
327 /* better than nothing: skip mpeg audio RTP header */ |
| 0 | 328 if (len <= 4) |
| 329 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
|
330 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
|
331 len -= 4; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
332 buf += 4; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
333 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
|
334 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
|
335 break; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
336 case CODEC_ID_MPEG1VIDEO: |
| 305 | 337 /* 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
|
338 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
|
339 return -1; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
340 h = decode_be32(buf); |
| 0 | 341 buf += 4; |
| 342 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
|
343 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
|
344 /* mpeg2 */ |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
345 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
|
346 return -1; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
347 buf += 4; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
348 len -= 4; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
349 } |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
350 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
|
351 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
|
352 break; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
353 default: |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
354 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
|
355 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
|
356 break; |
| 0 | 357 } |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
358 |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
359 switch(st->codec.codec_id) { |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
360 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
|
361 case CODEC_ID_MPEG1VIDEO: |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
362 if (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
|
363 int64_t addend; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
364 /* XXX: is it really necessary to unify the timestamp base ? */ |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
365 /* compute pts from timestamp with received ntp_time */ |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
366 delta_timestamp = timestamp - s->last_rtcp_timestamp; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
367 /* convert to 90 kHz without overflow */ |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
368 addend = (s->last_rtcp_ntp_time - s->first_rtcp_ntp_time) >> 14; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
369 addend = (addend * 5625) >> 14; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
370 pkt->pts = addend + delta_timestamp; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
371 } |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
372 break; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
373 default: |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
374 /* no timestamp info yet */ |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
375 break; |
| 173 | 376 } |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
377 pkt->stream_index = s->st->index; |
| 0 | 378 } |
| 379 return 0; | |
| 380 } | |
| 381 | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
382 void rtp_parse_close(RTPDemuxContext *s) |
| 0 | 383 { |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
384 if (s->payload_type == RTP_PT_MPEG2TS) { |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
385 mpegts_parse_close(s->ts); |
| 0 | 386 } |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
387 av_free(s); |
| 0 | 388 } |
| 389 | |
| 390 /* rtp output */ | |
| 391 | |
| 392 static int rtp_write_header(AVFormatContext *s1) | |
| 393 { | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
394 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
|
395 int payload_type, max_packet_size, n; |
| 0 | 396 AVStream *st; |
| 397 | |
| 398 if (s1->nb_streams != 1) | |
| 399 return -1; | |
| 400 st = s1->streams[0]; | |
| 401 | |
| 402 payload_type = rtp_get_payload_type(&st->codec); | |
| 403 if (payload_type < 0) | |
| 404 payload_type = RTP_PT_PRIVATE; /* private payload type */ | |
| 405 s->payload_type = payload_type; | |
| 406 | |
| 407 s->base_timestamp = random(); | |
| 408 s->timestamp = s->base_timestamp; | |
| 409 s->ssrc = random(); | |
| 410 s->first_packet = 1; | |
| 411 | |
| 412 max_packet_size = url_fget_max_packet_size(&s1->pb); | |
| 413 if (max_packet_size <= 12) | |
| 414 return AVERROR_IO; | |
| 415 s->max_payload_size = max_packet_size - 12; | |
| 416 | |
| 417 switch(st->codec.codec_id) { | |
| 418 case CODEC_ID_MP2: | |
| 232 | 419 case CODEC_ID_MP3: |
| 0 | 420 s->buf_ptr = s->buf + 4; |
| 421 s->cur_timestamp = 0; | |
| 422 break; | |
| 423 case CODEC_ID_MPEG1VIDEO: | |
| 424 s->cur_timestamp = 0; | |
| 425 break; | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
426 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
|
427 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
|
428 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
|
429 n = 1; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
430 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
|
431 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
|
432 break; |
| 0 | 433 default: |
| 434 s->buf_ptr = s->buf; | |
| 435 break; | |
| 436 } | |
| 437 | |
| 438 return 0; | |
| 439 } | |
| 440 | |
| 441 /* send an rtcp sender report packet */ | |
| 65 | 442 static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time) |
| 0 | 443 { |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
444 RTPDemuxContext *s = s1->priv_data; |
| 0 | 445 #if defined(DEBUG) |
| 446 printf("RTCP: %02x %Lx %x\n", s->payload_type, ntp_time, s->timestamp); | |
| 447 #endif | |
| 448 put_byte(&s1->pb, (RTP_VERSION << 6)); | |
| 449 put_byte(&s1->pb, 200); | |
| 450 put_be16(&s1->pb, 6); /* length in words - 1 */ | |
| 451 put_be32(&s1->pb, s->ssrc); | |
| 452 put_be64(&s1->pb, ntp_time); | |
| 453 put_be32(&s1->pb, s->timestamp); | |
| 454 put_be32(&s1->pb, s->packet_count); | |
| 455 put_be32(&s1->pb, s->octet_count); | |
| 456 put_flush_packet(&s1->pb); | |
| 457 } | |
| 458 | |
| 459 /* send an rtp packet. sequence number is incremented, but the caller | |
| 460 must update the timestamp itself */ | |
| 241 | 461 static void rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len) |
| 0 | 462 { |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
463 RTPDemuxContext *s = s1->priv_data; |
| 0 | 464 |
| 465 #ifdef DEBUG | |
| 466 printf("rtp_send_data size=%d\n", len); | |
| 467 #endif | |
| 468 | |
| 469 /* build the RTP header */ | |
| 470 put_byte(&s1->pb, (RTP_VERSION << 6)); | |
| 471 put_byte(&s1->pb, s->payload_type & 0x7f); | |
| 472 put_be16(&s1->pb, s->seq); | |
| 473 put_be32(&s1->pb, s->timestamp); | |
| 474 put_be32(&s1->pb, s->ssrc); | |
| 475 | |
| 476 put_buffer(&s1->pb, buf1, len); | |
| 477 put_flush_packet(&s1->pb); | |
| 478 | |
| 479 s->seq++; | |
| 480 s->octet_count += len; | |
| 481 s->packet_count++; | |
| 482 } | |
| 483 | |
| 484 /* send an integer number of samples and compute time stamp and fill | |
| 485 the rtp send buffer before sending. */ | |
| 486 static void rtp_send_samples(AVFormatContext *s1, | |
| 241 | 487 const uint8_t *buf1, int size, int sample_size) |
| 0 | 488 { |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
489 RTPDemuxContext *s = s1->priv_data; |
| 0 | 490 int len, max_packet_size, n; |
| 491 | |
| 492 max_packet_size = (s->max_payload_size / sample_size) * sample_size; | |
| 493 /* not needed, but who nows */ | |
| 494 if ((size % sample_size) != 0) | |
| 495 av_abort(); | |
| 496 while (size > 0) { | |
| 497 len = (max_packet_size - (s->buf_ptr - s->buf)); | |
| 498 if (len > size) | |
| 499 len = size; | |
| 500 | |
| 501 /* copy data */ | |
| 502 memcpy(s->buf_ptr, buf1, len); | |
| 503 s->buf_ptr += len; | |
| 504 buf1 += len; | |
| 505 size -= len; | |
| 506 n = (s->buf_ptr - s->buf); | |
| 507 /* if buffer full, then send it */ | |
| 508 if (n >= max_packet_size) { | |
| 509 rtp_send_data(s1, s->buf, n); | |
| 510 s->buf_ptr = s->buf; | |
| 511 /* update timestamp */ | |
| 512 s->timestamp += n / sample_size; | |
| 513 } | |
| 514 } | |
| 515 } | |
| 516 | |
| 517 /* NOTE: we suppose that exactly one frame is given as argument here */ | |
| 518 /* XXX: test it */ | |
| 519 static void rtp_send_mpegaudio(AVFormatContext *s1, | |
| 241 | 520 const uint8_t *buf1, int size) |
| 0 | 521 { |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
522 RTPDemuxContext *s = s1->priv_data; |
| 0 | 523 AVStream *st = s1->streams[0]; |
| 524 int len, count, max_packet_size; | |
| 525 | |
| 526 max_packet_size = s->max_payload_size; | |
| 527 | |
| 528 /* test if we must flush because not enough space */ | |
| 529 len = (s->buf_ptr - s->buf); | |
| 530 if ((len + size) > max_packet_size) { | |
| 531 if (len > 4) { | |
| 532 rtp_send_data(s1, s->buf, s->buf_ptr - s->buf); | |
| 533 s->buf_ptr = s->buf + 4; | |
| 534 /* 90 KHz time stamp */ | |
| 535 s->timestamp = s->base_timestamp + | |
| 536 (s->cur_timestamp * 90000LL) / st->codec.sample_rate; | |
| 537 } | |
| 538 } | |
| 539 | |
| 540 /* add the packet */ | |
| 541 if (size > max_packet_size) { | |
| 542 /* big packet: fragment */ | |
| 543 count = 0; | |
| 544 while (size > 0) { | |
| 545 len = max_packet_size - 4; | |
| 546 if (len > size) | |
| 547 len = size; | |
| 548 /* build fragmented packet */ | |
| 549 s->buf[0] = 0; | |
| 550 s->buf[1] = 0; | |
| 551 s->buf[2] = count >> 8; | |
| 552 s->buf[3] = count; | |
| 553 memcpy(s->buf + 4, buf1, len); | |
| 554 rtp_send_data(s1, s->buf, len + 4); | |
| 555 size -= len; | |
| 556 buf1 += len; | |
| 557 count += len; | |
| 558 } | |
| 559 } else { | |
| 560 if (s->buf_ptr == s->buf + 4) { | |
| 561 /* no fragmentation possible */ | |
| 562 s->buf[0] = 0; | |
| 563 s->buf[1] = 0; | |
| 564 s->buf[2] = 0; | |
| 565 s->buf[3] = 0; | |
| 566 } | |
| 567 memcpy(s->buf_ptr, buf1, size); | |
| 568 s->buf_ptr += size; | |
| 569 } | |
| 570 s->cur_timestamp += st->codec.frame_size; | |
| 571 } | |
| 572 | |
| 573 /* NOTE: a single frame must be passed with sequence header if | |
| 574 needed. XXX: use slices. */ | |
| 575 static void rtp_send_mpegvideo(AVFormatContext *s1, | |
| 241 | 576 const uint8_t *buf1, int size) |
| 0 | 577 { |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
578 RTPDemuxContext *s = s1->priv_data; |
| 0 | 579 AVStream *st = s1->streams[0]; |
| 580 int len, h, max_packet_size; | |
| 65 | 581 uint8_t *q; |
| 0 | 582 |
| 583 max_packet_size = s->max_payload_size; | |
| 584 | |
| 585 while (size > 0) { | |
| 586 /* XXX: more correct headers */ | |
| 587 h = 0; | |
| 588 if (st->codec.sub_id == 2) | |
| 589 h |= 1 << 26; /* mpeg 2 indicator */ | |
| 590 q = s->buf; | |
| 591 *q++ = h >> 24; | |
| 592 *q++ = h >> 16; | |
| 593 *q++ = h >> 8; | |
| 594 *q++ = h; | |
| 595 | |
| 596 if (st->codec.sub_id == 2) { | |
| 597 h = 0; | |
| 598 *q++ = h >> 24; | |
| 599 *q++ = h >> 16; | |
| 600 *q++ = h >> 8; | |
| 601 *q++ = h; | |
| 602 } | |
| 603 | |
| 604 len = max_packet_size - (q - s->buf); | |
| 605 if (len > size) | |
| 606 len = size; | |
| 607 | |
| 608 memcpy(q, buf1, len); | |
| 609 q += len; | |
| 610 | |
| 611 /* 90 KHz time stamp */ | |
| 612 s->timestamp = s->base_timestamp + | |
|
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
65
diff
changeset
|
613 av_rescale((int64_t)s->cur_timestamp * st->codec.frame_rate_base, 90000, st->codec.frame_rate); |
| 0 | 614 rtp_send_data(s1, s->buf, q - s->buf); |
| 615 | |
| 616 buf1 += len; | |
| 617 size -= len; | |
| 618 } | |
| 619 s->cur_timestamp++; | |
| 620 } | |
| 621 | |
| 622 static void rtp_send_raw(AVFormatContext *s1, | |
| 241 | 623 const uint8_t *buf1, int size) |
| 0 | 624 { |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
625 RTPDemuxContext *s = s1->priv_data; |
| 0 | 626 AVStream *st = s1->streams[0]; |
| 627 int len, max_packet_size; | |
| 628 | |
| 629 max_packet_size = s->max_payload_size; | |
| 630 | |
| 631 while (size > 0) { | |
| 632 len = max_packet_size; | |
| 633 if (len > size) | |
| 634 len = size; | |
| 635 | |
| 636 /* 90 KHz time stamp */ | |
| 637 s->timestamp = s->base_timestamp + | |
|
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
65
diff
changeset
|
638 av_rescale((int64_t)s->cur_timestamp * st->codec.frame_rate_base, 90000, st->codec.frame_rate); |
| 0 | 639 rtp_send_data(s1, buf1, len); |
| 640 | |
| 641 buf1 += len; | |
| 642 size -= len; | |
| 643 } | |
| 644 s->cur_timestamp++; | |
| 645 } | |
| 646 | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
647 /* 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
|
648 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
|
649 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
|
650 { |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
651 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
|
652 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
|
653 |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
654 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
|
655 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
|
656 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
|
657 len = size; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
658 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
|
659 buf1 += len; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
660 size -= len; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
661 s->buf_ptr += len; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
662 |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
663 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
|
664 if (out_len >= s->max_payload_size) { |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
665 rtp_send_data(s1, s->buf, out_len); |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
666 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
|
667 } |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
668 } |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
669 } |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
670 |
| 0 | 671 /* write an RTP packet. 'buf1' must contain a single specific frame. */ |
| 468 | 672 static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt) |
| 0 | 673 { |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
674 RTPDemuxContext *s = s1->priv_data; |
| 0 | 675 AVStream *st = s1->streams[0]; |
| 676 int rtcp_bytes; | |
| 65 | 677 int64_t ntp_time; |
| 468 | 678 int size= pkt->size; |
| 679 uint8_t *buf1= pkt->data; | |
| 0 | 680 |
| 681 #ifdef DEBUG | |
| 468 | 682 printf("%d: write len=%d\n", pkt->stream_index, size); |
| 0 | 683 #endif |
| 684 | |
| 685 /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */ | |
| 686 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) / | |
| 687 RTCP_TX_RATIO_DEN; | |
| 688 if (s->first_packet || rtcp_bytes >= 28) { | |
| 689 /* compute NTP time */ | |
| 173 | 690 /* XXX: 90 kHz timestamp hardcoded */ |
| 468 | 691 ntp_time = (pkt->pts << 28) / 5625; |
| 0 | 692 rtcp_send_sr(s1, ntp_time); |
| 693 s->last_octet_count = s->octet_count; | |
| 694 s->first_packet = 0; | |
| 695 } | |
| 696 | |
| 697 switch(st->codec.codec_id) { | |
| 698 case CODEC_ID_PCM_MULAW: | |
| 699 case CODEC_ID_PCM_ALAW: | |
| 700 case CODEC_ID_PCM_U8: | |
| 701 case CODEC_ID_PCM_S8: | |
| 702 rtp_send_samples(s1, buf1, size, 1 * st->codec.channels); | |
| 703 break; | |
| 704 case CODEC_ID_PCM_U16BE: | |
| 705 case CODEC_ID_PCM_U16LE: | |
| 706 case CODEC_ID_PCM_S16BE: | |
| 707 case CODEC_ID_PCM_S16LE: | |
| 708 rtp_send_samples(s1, buf1, size, 2 * st->codec.channels); | |
| 709 break; | |
| 710 case CODEC_ID_MP2: | |
| 232 | 711 case CODEC_ID_MP3: |
| 0 | 712 rtp_send_mpegaudio(s1, buf1, size); |
| 713 break; | |
| 714 case CODEC_ID_MPEG1VIDEO: | |
| 715 rtp_send_mpegvideo(s1, buf1, size); | |
| 716 break; | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
717 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
|
718 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
|
719 break; |
| 0 | 720 default: |
| 721 /* better than nothing : send the codec raw data */ | |
| 722 rtp_send_raw(s1, buf1, size); | |
| 723 break; | |
| 724 } | |
| 725 return 0; | |
| 726 } | |
| 727 | |
| 728 static int rtp_write_trailer(AVFormatContext *s1) | |
| 729 { | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
730 // RTPDemuxContext *s = s1->priv_data; |
| 0 | 731 return 0; |
| 732 } | |
| 733 | |
| 734 AVOutputFormat rtp_mux = { | |
| 735 "rtp", | |
| 736 "RTP output format", | |
| 737 NULL, | |
| 738 NULL, | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
739 sizeof(RTPDemuxContext), |
| 0 | 740 CODEC_ID_PCM_MULAW, |
| 741 CODEC_ID_NONE, | |
| 742 rtp_write_header, | |
| 743 rtp_write_packet, | |
| 744 rtp_write_trailer, | |
| 745 }; | |
| 746 | |
| 747 int rtp_init(void) | |
| 748 { | |
| 749 av_register_output_format(&rtp_mux); | |
| 750 return 0; | |
| 751 } |
