yading@11: /* yading@11: * RTP muxer chaining code yading@11: * Copyright (c) 2010 Martin Storsjo yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #include "avformat.h" yading@11: #include "avio_internal.h" yading@11: #include "rtpenc_chain.h" yading@11: #include "avio_internal.h" yading@11: #include "rtp.h" yading@11: #include "libavutil/opt.h" yading@11: yading@11: int ff_rtp_chain_mux_open(AVFormatContext **out, AVFormatContext *s, yading@11: AVStream *st, URLContext *handle, int packet_size, yading@11: int idx) yading@11: { yading@11: AVFormatContext *rtpctx = NULL; yading@11: int ret; yading@11: AVOutputFormat *rtp_format = av_guess_format("rtp", NULL, NULL); yading@11: uint8_t *rtpflags; yading@11: AVDictionary *opts = NULL; yading@11: yading@11: if (!rtp_format) { yading@11: ret = AVERROR(ENOSYS); yading@11: goto fail; yading@11: } yading@11: yading@11: /* Allocate an AVFormatContext for each output stream */ yading@11: rtpctx = avformat_alloc_context(); yading@11: if (!rtpctx) { yading@11: ret = AVERROR(ENOMEM); yading@11: goto fail; yading@11: } yading@11: yading@11: rtpctx->oformat = rtp_format; yading@11: if (!avformat_new_stream(rtpctx, NULL)) { yading@11: ret = AVERROR(ENOMEM); yading@11: goto fail; yading@11: } yading@11: /* Pass the interrupt callback on */ yading@11: rtpctx->interrupt_callback = s->interrupt_callback; yading@11: /* Copy the max delay setting; the rtp muxer reads this. */ yading@11: rtpctx->max_delay = s->max_delay; yading@11: /* Copy other stream parameters. */ yading@11: rtpctx->streams[0]->sample_aspect_ratio = st->sample_aspect_ratio; yading@11: rtpctx->flags |= s->flags & AVFMT_FLAG_MP4A_LATM; yading@11: yading@11: /* Get the payload type from the codec */ yading@11: if (st->id < RTP_PT_PRIVATE) yading@11: rtpctx->streams[0]->id = yading@11: ff_rtp_get_payload_type(s, st->codec, idx); yading@11: else yading@11: rtpctx->streams[0]->id = st->id; yading@11: yading@11: yading@11: if (av_opt_get(s, "rtpflags", AV_OPT_SEARCH_CHILDREN, &rtpflags) >= 0) yading@11: av_dict_set(&opts, "rtpflags", rtpflags, AV_DICT_DONT_STRDUP_VAL); yading@11: yading@11: /* Set the synchronized start time. */ yading@11: rtpctx->start_time_realtime = s->start_time_realtime; yading@11: yading@11: avcodec_copy_context(rtpctx->streams[0]->codec, st->codec); yading@11: yading@11: if (handle) { yading@11: ffio_fdopen(&rtpctx->pb, handle); yading@11: } else yading@11: ffio_open_dyn_packet_buf(&rtpctx->pb, packet_size); yading@11: ret = avformat_write_header(rtpctx, &opts); yading@11: av_dict_free(&opts); yading@11: yading@11: if (ret) { yading@11: if (handle) { yading@11: avio_close(rtpctx->pb); yading@11: } else { yading@11: uint8_t *ptr; yading@11: avio_close_dyn_buf(rtpctx->pb, &ptr); yading@11: av_free(ptr); yading@11: } yading@11: avformat_free_context(rtpctx); yading@11: return ret; yading@11: } yading@11: yading@11: *out = rtpctx; yading@11: return 0; yading@11: yading@11: fail: yading@11: av_free(rtpctx); yading@11: if (handle) yading@11: ffurl_close(handle); yading@11: return ret; yading@11: }