Libav 0.7.1
libavformat/rtpenc_chain.c
Go to the documentation of this file.
00001 /*
00002  * RTP muxer chaining code
00003  * Copyright (c) 2010 Martin Storsjo
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "avformat.h"
00023 #include "avio_internal.h"
00024 #include "rtpenc_chain.h"
00025 #include "avio_internal.h"
00026 #include "libavutil/opt.h"
00027 
00028 AVFormatContext *ff_rtp_chain_mux_open(AVFormatContext *s, AVStream *st,
00029                                        URLContext *handle, int packet_size)
00030 {
00031     AVFormatContext *rtpctx;
00032     int ret;
00033     AVOutputFormat *rtp_format = av_guess_format("rtp", NULL, NULL);
00034 
00035     if (!rtp_format)
00036         return NULL;
00037 
00038     /* Allocate an AVFormatContext for each output stream */
00039     rtpctx = avformat_alloc_context();
00040     if (!rtpctx)
00041         return NULL;
00042 
00043     rtpctx->oformat = rtp_format;
00044     if (!av_new_stream(rtpctx, 0)) {
00045         av_free(rtpctx);
00046         return NULL;
00047     }
00048     /* Copy the max delay setting; the rtp muxer reads this. */
00049     rtpctx->max_delay = s->max_delay;
00050     /* Copy other stream parameters. */
00051     rtpctx->streams[0]->sample_aspect_ratio = st->sample_aspect_ratio;
00052 
00053     av_set_parameters(rtpctx, NULL);
00054     /* Copy the rtpflags values straight through */
00055     if (s->oformat->priv_class &&
00056         av_find_opt(s->priv_data, "rtpflags", NULL, 0, 0))
00057         av_set_int(rtpctx->priv_data, "rtpflags",
00058                    av_get_int(s->priv_data, "rtpflags", NULL));
00059 
00060     /* Set the synchronized start time. */
00061     rtpctx->start_time_realtime = s->start_time_realtime;
00062 
00063     avcodec_copy_context(rtpctx->streams[0]->codec, st->codec);
00064 
00065     if (handle) {
00066         ffio_fdopen(&rtpctx->pb, handle);
00067     } else
00068         ffio_open_dyn_packet_buf(&rtpctx->pb, packet_size);
00069     ret = avformat_write_header(rtpctx, NULL);
00070 
00071     if (ret) {
00072         if (handle) {
00073             avio_close(rtpctx->pb);
00074         } else {
00075             uint8_t *ptr;
00076             avio_close_dyn_buf(rtpctx->pb, &ptr);
00077             av_free(ptr);
00078         }
00079         avformat_free_context(rtpctx);
00080         return NULL;
00081     }
00082 
00083     return rtpctx;
00084 }
00085