Mercurial > libavformat.hg
annotate rtp.c @ 294:6091b76cfc2a libavformat
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
| author | bellard |
|---|---|
| date | Wed, 29 Oct 2003 14:25:27 +0000 |
| parents | 3d92f793fd67 |
| children | ef53bff8bf23 |
| 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 } |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
237 } |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
238 return s; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
239 } |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
240 |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
241 /** |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
242 * 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
|
243 * @param s RTP parse context. |
| 0 | 244 * @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
|
245 * @param buf input buffer or NULL to read the next packets |
| 0 | 246 * @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
|
247 * @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
|
248 * (use buf as NULL to read the next). -1 if no packet (error or no more packet). |
| 0 | 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 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
|
251 const uint8_t *buf, int len) |
| 0 | 252 { |
| 253 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
|
254 int payload_type, seq, delta_timestamp, ret; |
| 0 | 255 AVStream *st; |
| 65 | 256 uint32_t timestamp; |
| 0 | 257 |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
258 if (!buf) { |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
259 /* 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
|
260 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
|
261 return -1; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
262 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
|
263 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
|
264 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
|
265 return -1; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
266 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
|
267 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
|
268 return 1; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
269 else |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
270 return 0; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
271 } |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
272 |
| 0 | 273 if (len < 12) |
| 274 return -1; | |
| 275 | |
| 276 if ((buf[0] & 0xc0) != (RTP_VERSION << 6)) | |
| 277 return -1; | |
| 278 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
|
279 rtcp_parse_packet(s, buf, len); |
| 0 | 280 return -1; |
| 281 } | |
| 282 payload_type = buf[1] & 0x7f; | |
| 283 seq = (buf[2] << 8) | buf[3]; | |
| 284 timestamp = decode_be32(buf + 4); | |
| 285 ssrc = decode_be32(buf + 8); | |
| 286 | |
| 287 /* NOTE: we can handle only one payload type */ | |
| 288 if (s->payload_type != payload_type) | |
| 289 return -1; | |
| 290 #if defined(DEBUG) || 1 | |
| 291 if (seq != ((s->seq + 1) & 0xffff)) { | |
| 292 printf("RTP: PT=%02x: bad cseq %04x expected=%04x\n", | |
| 293 payload_type, seq, ((s->seq + 1) & 0xffff)); | |
| 294 } | |
| 295 s->seq = seq; | |
| 296 #endif | |
| 297 len -= 12; | |
| 298 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
|
299 |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
300 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
|
301 if (!st) { |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
302 /* 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
|
303 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
|
304 if (ret < 0) |
| 0 | 305 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
|
306 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
|
307 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
|
308 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
|
309 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
|
310 return 1; |
|
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 } else { |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
313 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
|
314 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
|
315 /* better than nothing: skip mpeg audio RTP header */ |
| 0 | 316 if (len <= 4) |
| 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 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
|
319 len -= 4; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
320 buf += 4; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
321 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
|
322 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
|
323 break; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
324 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
|
325 /* better than nothing: skip mpeg audio RTP header */ |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
326 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
|
327 return -1; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
328 h = decode_be32(buf); |
| 0 | 329 buf += 4; |
| 330 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
|
331 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
|
332 /* mpeg2 */ |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
333 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
|
334 return -1; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
335 buf += 4; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
336 len -= 4; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
337 } |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
338 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
|
339 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
|
340 break; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
341 default: |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
342 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
|
343 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
|
344 break; |
| 0 | 345 } |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
346 |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
347 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
|
348 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
|
349 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
|
350 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
|
351 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
|
352 /* 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
|
353 /* 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
|
354 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
|
355 /* 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
|
356 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
|
357 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
|
358 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
|
359 } |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
360 break; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
361 default: |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
362 /* 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
|
363 break; |
| 173 | 364 } |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
365 pkt->stream_index = s->st->index; |
| 0 | 366 } |
| 367 return 0; | |
| 368 } | |
| 369 | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
370 void rtp_parse_close(RTPDemuxContext *s) |
| 0 | 371 { |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
372 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
|
373 mpegts_parse_close(s->ts); |
| 0 | 374 } |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
375 av_free(s); |
| 0 | 376 } |
| 377 | |
| 378 /* rtp output */ | |
| 379 | |
| 380 static int rtp_write_header(AVFormatContext *s1) | |
| 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 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
|
383 int payload_type, max_packet_size, n; |
| 0 | 384 AVStream *st; |
| 385 | |
| 386 if (s1->nb_streams != 1) | |
| 387 return -1; | |
| 388 st = s1->streams[0]; | |
| 389 | |
| 390 payload_type = rtp_get_payload_type(&st->codec); | |
| 391 if (payload_type < 0) | |
| 392 payload_type = RTP_PT_PRIVATE; /* private payload type */ | |
| 393 s->payload_type = payload_type; | |
| 394 | |
| 395 s->base_timestamp = random(); | |
| 396 s->timestamp = s->base_timestamp; | |
| 397 s->ssrc = random(); | |
| 398 s->first_packet = 1; | |
| 399 | |
| 400 max_packet_size = url_fget_max_packet_size(&s1->pb); | |
| 401 if (max_packet_size <= 12) | |
| 402 return AVERROR_IO; | |
| 403 s->max_payload_size = max_packet_size - 12; | |
| 404 | |
| 405 switch(st->codec.codec_id) { | |
| 406 case CODEC_ID_MP2: | |
| 232 | 407 case CODEC_ID_MP3: |
| 0 | 408 s->buf_ptr = s->buf + 4; |
| 409 s->cur_timestamp = 0; | |
| 410 break; | |
| 411 case CODEC_ID_MPEG1VIDEO: | |
| 412 s->cur_timestamp = 0; | |
| 413 break; | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
414 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
|
415 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
|
416 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
|
417 n = 1; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
418 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
|
419 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
|
420 break; |
| 0 | 421 default: |
| 422 s->buf_ptr = s->buf; | |
| 423 break; | |
| 424 } | |
| 425 | |
| 426 return 0; | |
| 427 } | |
| 428 | |
| 429 /* send an rtcp sender report packet */ | |
| 65 | 430 static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time) |
| 0 | 431 { |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
432 RTPDemuxContext *s = s1->priv_data; |
| 0 | 433 #if defined(DEBUG) |
| 434 printf("RTCP: %02x %Lx %x\n", s->payload_type, ntp_time, s->timestamp); | |
| 435 #endif | |
| 436 put_byte(&s1->pb, (RTP_VERSION << 6)); | |
| 437 put_byte(&s1->pb, 200); | |
| 438 put_be16(&s1->pb, 6); /* length in words - 1 */ | |
| 439 put_be32(&s1->pb, s->ssrc); | |
| 440 put_be64(&s1->pb, ntp_time); | |
| 441 put_be32(&s1->pb, s->timestamp); | |
| 442 put_be32(&s1->pb, s->packet_count); | |
| 443 put_be32(&s1->pb, s->octet_count); | |
| 444 put_flush_packet(&s1->pb); | |
| 445 } | |
| 446 | |
| 447 /* send an rtp packet. sequence number is incremented, but the caller | |
| 448 must update the timestamp itself */ | |
| 241 | 449 static void rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len) |
| 0 | 450 { |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
451 RTPDemuxContext *s = s1->priv_data; |
| 0 | 452 |
| 453 #ifdef DEBUG | |
| 454 printf("rtp_send_data size=%d\n", len); | |
| 455 #endif | |
| 456 | |
| 457 /* build the RTP header */ | |
| 458 put_byte(&s1->pb, (RTP_VERSION << 6)); | |
| 459 put_byte(&s1->pb, s->payload_type & 0x7f); | |
| 460 put_be16(&s1->pb, s->seq); | |
| 461 put_be32(&s1->pb, s->timestamp); | |
| 462 put_be32(&s1->pb, s->ssrc); | |
| 463 | |
| 464 put_buffer(&s1->pb, buf1, len); | |
| 465 put_flush_packet(&s1->pb); | |
| 466 | |
| 467 s->seq++; | |
| 468 s->octet_count += len; | |
| 469 s->packet_count++; | |
| 470 } | |
| 471 | |
| 472 /* send an integer number of samples and compute time stamp and fill | |
| 473 the rtp send buffer before sending. */ | |
| 474 static void rtp_send_samples(AVFormatContext *s1, | |
| 241 | 475 const uint8_t *buf1, int size, int sample_size) |
| 0 | 476 { |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
477 RTPDemuxContext *s = s1->priv_data; |
| 0 | 478 int len, max_packet_size, n; |
| 479 | |
| 480 max_packet_size = (s->max_payload_size / sample_size) * sample_size; | |
| 481 /* not needed, but who nows */ | |
| 482 if ((size % sample_size) != 0) | |
| 483 av_abort(); | |
| 484 while (size > 0) { | |
| 485 len = (max_packet_size - (s->buf_ptr - s->buf)); | |
| 486 if (len > size) | |
| 487 len = size; | |
| 488 | |
| 489 /* copy data */ | |
| 490 memcpy(s->buf_ptr, buf1, len); | |
| 491 s->buf_ptr += len; | |
| 492 buf1 += len; | |
| 493 size -= len; | |
| 494 n = (s->buf_ptr - s->buf); | |
| 495 /* if buffer full, then send it */ | |
| 496 if (n >= max_packet_size) { | |
| 497 rtp_send_data(s1, s->buf, n); | |
| 498 s->buf_ptr = s->buf; | |
| 499 /* update timestamp */ | |
| 500 s->timestamp += n / sample_size; | |
| 501 } | |
| 502 } | |
| 503 } | |
| 504 | |
| 505 /* NOTE: we suppose that exactly one frame is given as argument here */ | |
| 506 /* XXX: test it */ | |
| 507 static void rtp_send_mpegaudio(AVFormatContext *s1, | |
| 241 | 508 const uint8_t *buf1, int size) |
| 0 | 509 { |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
510 RTPDemuxContext *s = s1->priv_data; |
| 0 | 511 AVStream *st = s1->streams[0]; |
| 512 int len, count, max_packet_size; | |
| 513 | |
| 514 max_packet_size = s->max_payload_size; | |
| 515 | |
| 516 /* test if we must flush because not enough space */ | |
| 517 len = (s->buf_ptr - s->buf); | |
| 518 if ((len + size) > max_packet_size) { | |
| 519 if (len > 4) { | |
| 520 rtp_send_data(s1, s->buf, s->buf_ptr - s->buf); | |
| 521 s->buf_ptr = s->buf + 4; | |
| 522 /* 90 KHz time stamp */ | |
| 523 s->timestamp = s->base_timestamp + | |
| 524 (s->cur_timestamp * 90000LL) / st->codec.sample_rate; | |
| 525 } | |
| 526 } | |
| 527 | |
| 528 /* add the packet */ | |
| 529 if (size > max_packet_size) { | |
| 530 /* big packet: fragment */ | |
| 531 count = 0; | |
| 532 while (size > 0) { | |
| 533 len = max_packet_size - 4; | |
| 534 if (len > size) | |
| 535 len = size; | |
| 536 /* build fragmented packet */ | |
| 537 s->buf[0] = 0; | |
| 538 s->buf[1] = 0; | |
| 539 s->buf[2] = count >> 8; | |
| 540 s->buf[3] = count; | |
| 541 memcpy(s->buf + 4, buf1, len); | |
| 542 rtp_send_data(s1, s->buf, len + 4); | |
| 543 size -= len; | |
| 544 buf1 += len; | |
| 545 count += len; | |
| 546 } | |
| 547 } else { | |
| 548 if (s->buf_ptr == s->buf + 4) { | |
| 549 /* no fragmentation possible */ | |
| 550 s->buf[0] = 0; | |
| 551 s->buf[1] = 0; | |
| 552 s->buf[2] = 0; | |
| 553 s->buf[3] = 0; | |
| 554 } | |
| 555 memcpy(s->buf_ptr, buf1, size); | |
| 556 s->buf_ptr += size; | |
| 557 } | |
| 558 s->cur_timestamp += st->codec.frame_size; | |
| 559 } | |
| 560 | |
| 561 /* NOTE: a single frame must be passed with sequence header if | |
| 562 needed. XXX: use slices. */ | |
| 563 static void rtp_send_mpegvideo(AVFormatContext *s1, | |
| 241 | 564 const uint8_t *buf1, int size) |
| 0 | 565 { |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
566 RTPDemuxContext *s = s1->priv_data; |
| 0 | 567 AVStream *st = s1->streams[0]; |
| 568 int len, h, max_packet_size; | |
| 65 | 569 uint8_t *q; |
| 0 | 570 |
| 571 max_packet_size = s->max_payload_size; | |
| 572 | |
| 573 while (size > 0) { | |
| 574 /* XXX: more correct headers */ | |
| 575 h = 0; | |
| 576 if (st->codec.sub_id == 2) | |
| 577 h |= 1 << 26; /* mpeg 2 indicator */ | |
| 578 q = s->buf; | |
| 579 *q++ = h >> 24; | |
| 580 *q++ = h >> 16; | |
| 581 *q++ = h >> 8; | |
| 582 *q++ = h; | |
| 583 | |
| 584 if (st->codec.sub_id == 2) { | |
| 585 h = 0; | |
| 586 *q++ = h >> 24; | |
| 587 *q++ = h >> 16; | |
| 588 *q++ = h >> 8; | |
| 589 *q++ = h; | |
| 590 } | |
| 591 | |
| 592 len = max_packet_size - (q - s->buf); | |
| 593 if (len > size) | |
| 594 len = size; | |
| 595 | |
| 596 memcpy(q, buf1, len); | |
| 597 q += len; | |
| 598 | |
| 599 /* 90 KHz time stamp */ | |
| 600 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
|
601 av_rescale((int64_t)s->cur_timestamp * st->codec.frame_rate_base, 90000, st->codec.frame_rate); |
| 0 | 602 rtp_send_data(s1, s->buf, q - s->buf); |
| 603 | |
| 604 buf1 += len; | |
| 605 size -= len; | |
| 606 } | |
| 607 s->cur_timestamp++; | |
| 608 } | |
| 609 | |
| 610 static void rtp_send_raw(AVFormatContext *s1, | |
| 241 | 611 const uint8_t *buf1, int size) |
| 0 | 612 { |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
613 RTPDemuxContext *s = s1->priv_data; |
| 0 | 614 AVStream *st = s1->streams[0]; |
| 615 int len, max_packet_size; | |
| 616 | |
| 617 max_packet_size = s->max_payload_size; | |
| 618 | |
| 619 while (size > 0) { | |
| 620 len = max_packet_size; | |
| 621 if (len > size) | |
| 622 len = size; | |
| 623 | |
| 624 /* 90 KHz time stamp */ | |
| 625 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
|
626 av_rescale((int64_t)s->cur_timestamp * st->codec.frame_rate_base, 90000, st->codec.frame_rate); |
| 0 | 627 rtp_send_data(s1, buf1, len); |
| 628 | |
| 629 buf1 += len; | |
| 630 size -= len; | |
| 631 } | |
| 632 s->cur_timestamp++; | |
| 633 } | |
| 634 | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
635 /* 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
|
636 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
|
637 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
|
638 { |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
639 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
|
640 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
|
641 |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
642 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
|
643 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
|
644 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
|
645 len = size; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
646 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
|
647 buf1 += len; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
648 size -= len; |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
649 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
|
650 |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
651 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
|
652 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
|
653 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
|
654 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
|
655 } |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
656 } |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
657 } |
|
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
658 |
| 0 | 659 /* write an RTP packet. 'buf1' must contain a single specific frame. */ |
| 660 static int rtp_write_packet(AVFormatContext *s1, int stream_index, | |
| 241 | 661 const uint8_t *buf1, int size, int64_t pts) |
| 0 | 662 { |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
663 RTPDemuxContext *s = s1->priv_data; |
| 0 | 664 AVStream *st = s1->streams[0]; |
| 665 int rtcp_bytes; | |
| 65 | 666 int64_t ntp_time; |
| 0 | 667 |
| 668 #ifdef DEBUG | |
| 669 printf("%d: write len=%d\n", stream_index, size); | |
| 670 #endif | |
| 671 | |
| 672 /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */ | |
| 673 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) / | |
| 674 RTCP_TX_RATIO_DEN; | |
| 675 if (s->first_packet || rtcp_bytes >= 28) { | |
| 676 /* compute NTP time */ | |
| 173 | 677 /* XXX: 90 kHz timestamp hardcoded */ |
| 241 | 678 ntp_time = (pts << 28) / 5625; |
| 0 | 679 rtcp_send_sr(s1, ntp_time); |
| 680 s->last_octet_count = s->octet_count; | |
| 681 s->first_packet = 0; | |
| 682 } | |
| 683 | |
| 684 switch(st->codec.codec_id) { | |
| 685 case CODEC_ID_PCM_MULAW: | |
| 686 case CODEC_ID_PCM_ALAW: | |
| 687 case CODEC_ID_PCM_U8: | |
| 688 case CODEC_ID_PCM_S8: | |
| 689 rtp_send_samples(s1, buf1, size, 1 * st->codec.channels); | |
| 690 break; | |
| 691 case CODEC_ID_PCM_U16BE: | |
| 692 case CODEC_ID_PCM_U16LE: | |
| 693 case CODEC_ID_PCM_S16BE: | |
| 694 case CODEC_ID_PCM_S16LE: | |
| 695 rtp_send_samples(s1, buf1, size, 2 * st->codec.channels); | |
| 696 break; | |
| 697 case CODEC_ID_MP2: | |
| 232 | 698 case CODEC_ID_MP3: |
| 0 | 699 rtp_send_mpegaudio(s1, buf1, size); |
| 700 break; | |
| 701 case CODEC_ID_MPEG1VIDEO: | |
| 702 rtp_send_mpegvideo(s1, buf1, size); | |
| 703 break; | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
704 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
|
705 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
|
706 break; |
| 0 | 707 default: |
| 708 /* better than nothing : send the codec raw data */ | |
| 709 rtp_send_raw(s1, buf1, size); | |
| 710 break; | |
| 711 } | |
| 712 return 0; | |
| 713 } | |
| 714 | |
| 715 static int rtp_write_trailer(AVFormatContext *s1) | |
| 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 return 0; |
| 719 } | |
| 720 | |
| 721 AVOutputFormat rtp_mux = { | |
| 722 "rtp", | |
| 723 "RTP output format", | |
| 724 NULL, | |
| 725 NULL, | |
|
294
6091b76cfc2a
added MPEG2TS support in RTP, SDP and RTSP - replaced fake RTP demux by a specific API
bellard
parents:
241
diff
changeset
|
726 sizeof(RTPDemuxContext), |
| 0 | 727 CODEC_ID_PCM_MULAW, |
| 728 CODEC_ID_NONE, | |
| 729 rtp_write_header, | |
| 730 rtp_write_packet, | |
| 731 rtp_write_trailer, | |
| 732 }; | |
| 733 | |
| 734 int rtp_init(void) | |
| 735 { | |
| 736 av_register_output_format(&rtp_mux); | |
| 737 return 0; | |
| 738 } |
