static void close_connection(HTTPContext *c)
{
    HTTPContext **cp, *c1;
    int i, nb_streams;
    AVFormatContext *ctx;
    URLContext *h;
    AVStream *st;
    cp = &first_http_ctx;
    while ((*cp) != NULL) {
        c1 = *cp;
        if (c1 == c)
            *cp = c->next;
        else
            cp = &c1->next;
    }
    for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
        if (c1->rtsp_c == c)
            c1->rtsp_c = NULL;
    }
    if (c->fd >= 0)
        closesocket(c->fd);
    if (c->fmt_in) {
        for(i=0;i<c->fmt_in->nb_streams;i++) {
            st = c->fmt_in->streams[i];
            if (st->codec->codec)
                avcodec_close(st->codec);
        }
        av_close_input_file(c->fmt_in);
    }
    nb_streams = 0;
    if (c->stream)
        nb_streams = c->stream->nb_streams;
    for(i=0;i<nb_streams;i++) {
        ctx = c->rtp_ctx[i];
        if (ctx) {
            av_write_trailer(ctx);
            av_metadata_free(&ctx->metadata);
            av_free(ctx->streams[0]);
            av_free(ctx);
        }
        h = c->rtp_handles[i];
        if (h)
            url_close(h);
    }
    ctx = &c->fmt_ctx;
    if (!c->last_packet_sent && c->state == HTTPSTATE_SEND_DATA_TRAILER) {
        if (ctx->oformat) {
            if (avio_open_dyn_buf(&ctx->pb) >= 0) {
                av_write_trailer(ctx);
                av_freep(&c->pb_buffer);
                avio_close_dyn_buf(ctx->pb, &c->pb_buffer);
            }
        }
    }
    for(i=0; i<ctx->nb_streams; i++)
        av_free(ctx->streams[i]);
    if (c->stream && !c->post && c->stream->stream_type == STREAM_TYPE_LIVE)
        current_bandwidth -= c->stream->bandwidth;
    if (c->state == HTTPSTATE_RECEIVE_DATA && c->stream) {
        c->stream->feed_opened = 0;
        close(c->feed_fd);
    }
    av_freep(&c->pb_buffer);
    av_freep(&c->packet_buffer);
    av_free(c->buffer);
    av_free(c);
    nb_connections--;
}
['static void rtsp_cmd_teardown(HTTPContext *c, const char *url, RTSPMessageHeader *h)\n{\n    HTTPContext *rtp_c;\n    rtp_c = find_rtp_session_with_url(url, h->session_id);\n    if (!rtp_c) {\n        rtsp_reply_error(c, RTSP_STATUS_SESSION);\n        return;\n    }\n    rtsp_reply_header(c, RTSP_STATUS_OK);\n    avio_printf(c->pb, "Session: %s\\r\\n", rtp_c->session_id);\n    avio_printf(c->pb, "\\r\\n");\n    close_connection(rtp_c);\n}', 'static void close_connection(HTTPContext *c)\n{\n    HTTPContext **cp, *c1;\n    int i, nb_streams;\n    AVFormatContext *ctx;\n    URLContext *h;\n    AVStream *st;\n    cp = &first_http_ctx;\n    while ((*cp) != NULL) {\n        c1 = *cp;\n        if (c1 == c)\n            *cp = c->next;\n        else\n            cp = &c1->next;\n    }\n    for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {\n        if (c1->rtsp_c == c)\n            c1->rtsp_c = NULL;\n    }\n    if (c->fd >= 0)\n        closesocket(c->fd);\n    if (c->fmt_in) {\n        for(i=0;i<c->fmt_in->nb_streams;i++) {\n            st = c->fmt_in->streams[i];\n            if (st->codec->codec)\n                avcodec_close(st->codec);\n        }\n        av_close_input_file(c->fmt_in);\n    }\n    nb_streams = 0;\n    if (c->stream)\n        nb_streams = c->stream->nb_streams;\n    for(i=0;i<nb_streams;i++) {\n        ctx = c->rtp_ctx[i];\n        if (ctx) {\n            av_write_trailer(ctx);\n            av_metadata_free(&ctx->metadata);\n            av_free(ctx->streams[0]);\n            av_free(ctx);\n        }\n        h = c->rtp_handles[i];\n        if (h)\n            url_close(h);\n    }\n    ctx = &c->fmt_ctx;\n    if (!c->last_packet_sent && c->state == HTTPSTATE_SEND_DATA_TRAILER) {\n        if (ctx->oformat) {\n            if (avio_open_dyn_buf(&ctx->pb) >= 0) {\n                av_write_trailer(ctx);\n                av_freep(&c->pb_buffer);\n                avio_close_dyn_buf(ctx->pb, &c->pb_buffer);\n            }\n        }\n    }\n    for(i=0; i<ctx->nb_streams; i++)\n        av_free(ctx->streams[i]);\n    if (c->stream && !c->post && c->stream->stream_type == STREAM_TYPE_LIVE)\n        current_bandwidth -= c->stream->bandwidth;\n    if (c->state == HTTPSTATE_RECEIVE_DATA && c->stream) {\n        c->stream->feed_opened = 0;\n        close(c->feed_fd);\n    }\n    av_freep(&c->pb_buffer);\n    av_freep(&c->packet_buffer);\n    av_free(c->buffer);\n    av_free(c);\n    nb_connections--;\n}']