Skip to content
Snippets Groups Projects
ffserver.c 81.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • Fabrice Bellard's avatar
    Fabrice Bellard committed
            goto send_stats;
    
        /* open input stream */
        if (open_input_stream(c, info) < 0) {
            sprintf(msg, "Input stream corresponding to '%s' not found", url);
            goto send_error;
        }
    
        /* prepare http header */
        q = c->buffer;
        q += sprintf(q, "HTTP/1.0 200 OK\r\n");
        mime_type = c->stream->fmt->mime_type;
        if (!mime_type)
            mime_type = "application/x-octet_stream";
        q += sprintf(q, "Pragma: no-cache\r\n");
    
        /* for asf, we need extra headers */
        if (!strcmp(c->stream->fmt->name,"asf")) {
    
            /* Need to allocate a client id */
            static int wmp_session;
    
            if (!wmp_session)
                wmp_session = time(0) & 0xffffff;
    
            c->wmp_client_id = ++wmp_session;
    
            q += sprintf(q, "Server: Cougar 4.1.0.3923\r\nCache-Control: no-cache\r\nPragma: client-id=%d\r\nPragma: features=\"broadcast\"\r\n", c->wmp_client_id);
    
            mime_type = "application/octet-stream"; 
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
        q += sprintf(q, "Content-Type: %s\r\n", mime_type);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        q += sprintf(q, "\r\n");
        
        /* prepare output buffer */
        c->http_error = 0;
        c->buffer_ptr = c->buffer;
        c->buffer_end = q;
        c->state = HTTPSTATE_SEND_HEADER;
        return 0;
     send_error:
        c->http_error = 404;
        q = c->buffer;
        q += sprintf(q, "HTTP/1.0 404 Not Found\r\n");
        q += sprintf(q, "Content-type: %s\r\n", "text/html");
        q += sprintf(q, "\r\n");
        q += sprintf(q, "<HTML>\n");
        q += sprintf(q, "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\n");
        q += sprintf(q, "<BODY>%s</BODY>\n", msg);
        q += sprintf(q, "</HTML>\n");
    
        /* prepare output buffer */
        c->buffer_ptr = c->buffer;
        c->buffer_end = q;
        c->state = HTTPSTATE_SEND_HEADER;
        return 0;
     send_stats:
        compute_stats(c);
        c->http_error = 200; /* horrible : we use this value to avoid
                                going to the send data state */
        c->state = HTTPSTATE_SEND_HEADER;
        return 0;
    }
    
    static void compute_stats(HTTPContext *c)
    {
        HTTPContext *c1;
        FFStream *stream;
        char *q, *p;
        time_t ti;
        int i;
    
        char *new_buffer;
    
        new_buffer = av_malloc(65536);
        if (new_buffer) {
            av_free(c->buffer);
            c->buffer_size = 65536;
            c->buffer = new_buffer;
            c->buffer_ptr = c->buffer;
            c->buffer_end = c->buffer + c->buffer_size;
        }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
        q = c->buffer;
        q += sprintf(q, "HTTP/1.0 200 OK\r\n");
        q += sprintf(q, "Content-type: %s\r\n", "text/html");
        q += sprintf(q, "Pragma: no-cache\r\n");
        q += sprintf(q, "\r\n");
        
    
        q += sprintf(q, "<HEAD><TITLE>FFServer Status</TITLE>\n");
        if (c->stream->feed_filename) {
            q += sprintf(q, "<link rel=\"shortcut icon\" href=\"%s\">\n", c->stream->feed_filename);
        }
        q += sprintf(q, "</HEAD>\n<BODY>");
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        q += sprintf(q, "<H1>FFServer Status</H1>\n");
        /* format status */
    
        q += sprintf(q, "<H2>Available Streams</H2>\n");
    
        q += sprintf(q, "<TABLE cellspacing=0 cellpadding=4>\n");
        q += sprintf(q, "<TR><Th valign=top>Path<th align=left>Served<br>Conns<Th><br>kbytes<Th valign=top>Format<Th>Bit rate<br>kbits/s<Th align=left>Video<br>kbits/s<th><br>Codec<Th align=left>Audio<br>kbits/s<th><br>Codec<Th align=left valign=top>Feed\n");
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        stream = first_stream;
        while (stream != NULL) {
    
            char sfilename[1024];
            char *eosf;
    
    
                pstrcpy(sfilename, sizeof(sfilename) - 1, stream->filename);
    
                eosf = sfilename + strlen(sfilename);
                if (eosf - sfilename >= 4) {
                    if (strcmp(eosf - 4, ".asf") == 0) {
                        strcpy(eosf - 4, ".asx");
                    } else if (strcmp(eosf - 3, ".rm") == 0) {
                        strcpy(eosf - 3, ".ram");
                    }
    
                
                q += sprintf(q, "<TR><TD><A HREF=\"/%s\">%s</A> ", 
                             sfilename, stream->filename);
                q += sprintf(q, "<td align=right> %d <td align=right> %lld",
                            stream->conns_served, stream->bytes_served / 1000);
                switch(stream->stream_type) {
                case STREAM_TYPE_LIVE:
                    {
                        int audio_bit_rate = 0;
                        int video_bit_rate = 0;
                        char *audio_codec_name = "";
                        char *video_codec_name = "";
                        char *audio_codec_name_extra = "";
                        char *video_codec_name_extra = "";
    
                        for(i=0;i<stream->nb_streams;i++) {
                            AVStream *st = stream->streams[i];
                            AVCodec *codec = avcodec_find_encoder(st->codec.codec_id);
                            switch(st->codec.codec_type) {
                            case CODEC_TYPE_AUDIO:
                                audio_bit_rate += st->codec.bit_rate;
                                if (codec) {
                                    if (*audio_codec_name)
                                        audio_codec_name_extra = "...";
                                    audio_codec_name = codec->name;
                                }
                                break;
                            case CODEC_TYPE_VIDEO:
                                video_bit_rate += st->codec.bit_rate;
                                if (codec) {
                                    if (*video_codec_name)
                                        video_codec_name_extra = "...";
                                    video_codec_name = codec->name;
                                }
                                break;
                            default:
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                        }
    
                        q += sprintf(q, "<TD align=center> %s <TD align=right> %d <TD align=right> %d <TD> %s %s <TD align=right> %d <TD> %s %s", 
                                     stream->fmt->name,
                                     (audio_bit_rate + video_bit_rate) / 1000,
                                     video_bit_rate / 1000, video_codec_name, video_codec_name_extra,
                                     audio_bit_rate / 1000, audio_codec_name, audio_codec_name_extra);
                        if (stream->feed) {
                            q += sprintf(q, "<TD>%s", stream->feed->filename);
                        } else {
                            q += sprintf(q, "<TD>%s", stream->feed_filename);
                        }
                        q += sprintf(q, "\n");
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    }
    
                    break;
                default:
                    q += sprintf(q, "<TD align=center> - <TD align=right> - <TD align=right> - <td><td align=right> - <TD>\n");
                    break;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                }
            }
            stream = stream->next;
        }
        q += sprintf(q, "</TABLE>\n");
    
    
        stream = first_stream;
        while (stream != NULL) {
            if (stream->feed == stream) {
                q += sprintf(q, "<h2>Feed %s</h2>", stream->filename);
    
                if (stream->pid) {
                    FILE *pid_stat;
                    char ps_cmd[64];
    
                    q += sprintf(q, "Running as pid %d.\n", stream->pid);
    
    #ifdef linux
                    /* This is somewhat linux specific I guess */
                    snprintf(ps_cmd, sizeof(ps_cmd), "ps -o \"%%cpu,cputime\" --no-headers %d", stream->pid);
    
                    pid_stat = popen(ps_cmd, "r");
                    if (pid_stat) {
                        char cpuperc[10];
                        char cpuused[64];
    
                        if (fscanf(pid_stat, "%10s %64s", cpuperc, cpuused) == 2) {
                            q += sprintf(q, "Currently using %s%% of the cpu. Total time used %s.\n",
                                cpuperc, cpuused);
                        }
                        fclose(pid_stat);
                    }
    #endif
    
                    q += sprintf(q, "<p>");
                }
    
                q += sprintf(q, "<table cellspacing=0 cellpadding=4><tr><th>Stream<th>type<th>kbits/s<th align=left>codec<th align=left>Parameters\n");
    
    
                for (i = 0; i < stream->nb_streams; i++) {
                    AVStream *st = stream->streams[i];
                    AVCodec *codec = avcodec_find_encoder(st->codec.codec_id);
                    char *type = "unknown";
    
    
                    switch(st->codec.codec_type) {
                    case CODEC_TYPE_AUDIO:
                        type = "audio";
                        break;
                    case CODEC_TYPE_VIDEO:
                        type = "video";
    
                        sprintf(parameters, "%dx%d, q=%d-%d, fps=%d", st->codec.width, st->codec.height,
                                    st->codec.qmin, st->codec.qmax, st->codec.frame_rate / FRAME_RATE_BASE);
    
                    q += sprintf(q, "<tr><td align=right>%d<td>%s<td align=right>%d<td>%s<td>%s\n",
                            i, type, st->codec.bit_rate/1000, codec ? codec->name : "", parameters);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        
    #if 0
        {
            float avg;
            AVCodecContext *enc;
            char buf[1024];
            
            /* feed status */
            stream = first_feed;
            while (stream != NULL) {
                q += sprintf(q, "<H1>Feed '%s'</H1>\n", stream->filename);
                q += sprintf(q, "<TABLE>\n");
                q += sprintf(q, "<TR><TD>Parameters<TD>Frame count<TD>Size<TD>Avg bitrate (kbits/s)\n");
                for(i=0;i<stream->nb_streams;i++) {
                    AVStream *st = stream->streams[i];
                    FeedData *fdata = st->priv_data;
                    enc = &st->codec;
                
                    avcodec_string(buf, sizeof(buf), enc);
                    avg = fdata->avg_frame_size * (float)enc->rate * 8.0;
                    if (enc->codec->type == CODEC_TYPE_AUDIO && enc->frame_size > 0)
                        avg /= enc->frame_size;
                    q += sprintf(q, "<TR><TD>%s <TD> %d <TD> %Ld <TD> %0.1f\n", 
                                 buf, enc->frame_number, fdata->data_count, avg / 1000.0);
                }
                q += sprintf(q, "</TABLE>\n");
                stream = stream->next_feed;
            }
        }
    #endif
    
        /* connection status */
    
        q += sprintf(q, "<H2>Connection Status</H2>\n");
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
        q += sprintf(q, "Number of connections: %d / %d<BR>\n",
                     nb_connections, nb_max_connections);
    
    
        q += sprintf(q, "Bandwidth in use: %dk / %dk<BR>\n",
                     nb_bandwidth, nb_max_bandwidth);
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        q += sprintf(q, "<TABLE>\n");
    
        q += sprintf(q, "<TR><Th>#<Th>File<Th>IP<Th>State<Th>kbits/sec<Th>Size\n");
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        c1 = first_http_ctx;
        i = 0;
    
        while (c1 != NULL && q < (char *) c->buffer + c->buffer_size - 2048) {
            int bitrate;
            int j;
    
            bitrate = 0;
            for (j = 0; j < c1->stream->nb_streams; j++) {
                if (c1->feed_streams[j] >= 0) {
                    bitrate += c1->stream->feed->streams[c1->feed_streams[j]]->codec.bit_rate;
                }
            }
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            i++;
            p = inet_ntoa(c1->from_addr.sin_addr);
    
            q += sprintf(q, "<TR><TD><B>%d</B><TD>%s%s <TD> %s <TD> %s <td align=right> %d <TD align=right> %Ld\n", 
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                         i, c1->stream->filename, 
                         c1->state == HTTPSTATE_RECEIVE_DATA ? "(input)" : "",
                         p, 
                         http_state[c1->state],
    
                         bitrate / 1000,
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                         c1->data_count);
            c1 = c1->next;
        }
        q += sprintf(q, "</TABLE>\n");
        
        /* date */
        ti = time(NULL);
        p = ctime(&ti);
    
        q += sprintf(q, "<HR size=1 noshade>Generated at %s", p);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        q += sprintf(q, "</BODY>\n</HTML>\n");
    
        c->buffer_ptr = c->buffer;
        c->buffer_end = q;
    }
    
    
    static void http_write_packet(void *opaque, 
                                  unsigned char *buf, int size)
    {
        HTTPContext *c = opaque;
    
    
        if (c->buffer_ptr == c->buffer_end || !c->buffer_ptr)
            c->buffer_ptr = c->buffer_end = c->buffer;
    
    
        if (c->buffer_end - c->buffer + size > c->buffer_size) {
    
            int new_buffer_size = c->buffer_size * 2;
            UINT8 *new_buffer;
    
            if (new_buffer_size <= c->buffer_end - c->buffer + size) {
                new_buffer_size = c->buffer_end - c->buffer + size + c->buffer_size;
            }
    
            new_buffer = av_malloc(new_buffer_size);
    
            if (new_buffer) {
                memcpy(new_buffer, c->buffer, c->buffer_end - c->buffer);
                c->buffer_end += (new_buffer - c->buffer);
                c->buffer_ptr += (new_buffer - c->buffer);
                av_free(c->buffer);
                c->buffer = new_buffer;
    
                c->buffer_size = new_buffer_size;
    
            } else {
                av_abort();
            }
        }
    
    
        memcpy(c->buffer_end, buf, size);
        c->buffer_end += size;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    static int open_input_stream(HTTPContext *c, const char *info)
    {
        char buf[128];
        char input_filename[1024];
        AVFormatContext *s;
        int buf_size;
        INT64 stream_pos;
    
        /* find file name */
        if (c->stream->feed) {
            strcpy(input_filename, c->stream->feed->feed_filename);
            buf_size = FFM_PACKET_SIZE;
            /* compute position (absolute time) */
            if (find_info_tag(buf, sizeof(buf), "date", info)) {
                stream_pos = parse_date(buf, 0);
    
            } else if (find_info_tag(buf, sizeof(buf), "buffer", info)) {
                int prebuffer = strtol(buf, 0, 10);
                stream_pos = gettime() - prebuffer * 1000000;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            } else {
    
                stream_pos = gettime() - c->stream->prebuffer * 1000;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
        } else {
            strcpy(input_filename, c->stream->feed_filename);
            buf_size = 0;
            /* compute position (relative time) */
            if (find_info_tag(buf, sizeof(buf), "date", info)) {
                stream_pos = parse_date(buf, 1);
            } else {
                stream_pos = 0;
            }
        }
        if (input_filename[0] == '\0')
            return -1;
    
        /* open stream */
    
        if (av_open_input_file(&s, input_filename, NULL, buf_size, NULL) < 0)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            return -1;
        c->fmt_in = s;
    
    
        if (c->fmt_in->iformat->read_seek) {
            c->fmt_in->iformat->read_seek(c->fmt_in, stream_pos);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
        
        //    printf("stream %s opened pos=%0.6f\n", input_filename, stream_pos / 1000000.0);
        return 0;
    }
    
    static int http_prepare_data(HTTPContext *c)
    {
        int i;
    
        switch(c->state) {
        case HTTPSTATE_SEND_DATA_HEADER:
            memset(&c->fmt_ctx, 0, sizeof(c->fmt_ctx));
            if (c->stream->feed) {
                /* open output stream by using specified codecs */
    
                c->fmt_ctx.oformat = c->stream->fmt;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                c->fmt_ctx.nb_streams = c->stream->nb_streams;
                for(i=0;i<c->fmt_ctx.nb_streams;i++) {
                    AVStream *st;
                    st = av_mallocz(sizeof(AVStream));
                    c->fmt_ctx.streams[i] = st;
    
                    if (c->stream->feed == c->stream)
                        memcpy(st, c->stream->streams[i], sizeof(AVStream));
                    else
                        memcpy(st, c->stream->feed->streams[c->stream->feed_streams[i]], sizeof(AVStream));
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    st->codec.frame_number = 0; /* XXX: should be done in
                                                   AVStream, not in codec */
                }
    
                c->got_key_frame = 0;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            } else {
                /* open output stream by using codecs in specified file */
    
                c->fmt_ctx.oformat = c->stream->fmt;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                c->fmt_ctx.nb_streams = c->fmt_in->nb_streams;
                for(i=0;i<c->fmt_ctx.nb_streams;i++) {
                    AVStream *st;
                    st = av_mallocz(sizeof(AVStream));
                    c->fmt_ctx.streams[i] = st;
                    memcpy(st, c->fmt_in->streams[i], sizeof(AVStream));
                    st->codec.frame_number = 0; /* XXX: should be done in
                                                   AVStream, not in codec */
                }
    
                c->got_key_frame = 0;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
    
            init_put_byte(&c->fmt_ctx.pb, c->pbuffer, c->pbuffer_size,
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                          1, c, NULL, http_write_packet, NULL);
            c->fmt_ctx.pb.is_streamed = 1;
            /* prepare header */
    
            av_write_header(&c->fmt_ctx);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            c->state = HTTPSTATE_SEND_DATA;
            c->last_packet_sent = 0;
            break;
        case HTTPSTATE_SEND_DATA:
            /* find a new packet */
    #if 0
            fifo_total_size = http_fifo_write_count - c->last_http_fifo_write_count;
            if (fifo_total_size >= ((3 * FIFO_MAX_SIZE) / 4)) {
                /* overflow : resync. We suppose that wptr is at this
                   point a pointer to a valid packet */
                c->rptr = http_fifo.wptr;
    
                c->got_key_frame = 0;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
            
            start_rptr = c->rptr;
            if (fifo_read(&http_fifo, (UINT8 *)&hdr, sizeof(hdr), &c->rptr) < 0)
                return 0;
            payload_size = ntohs(hdr.payload_size);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            payload = av_malloc(payload_size);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (fifo_read(&http_fifo, payload, payload_size, &c->rptr) < 0) {
                /* cannot read all the payload */
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                av_free(payload);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                c->rptr = start_rptr;
                return 0;
            }
            
            c->last_http_fifo_write_count = http_fifo_write_count - 
                fifo_size(&http_fifo, c->rptr);
            
            if (c->stream->stream_type != STREAM_TYPE_MASTER) {
                /* test if the packet can be handled by this format */
                ret = 0;
                for(i=0;i<c->fmt_ctx.nb_streams;i++) {
                    AVStream *st = c->fmt_ctx.streams[i];
                    if (test_header(&hdr, &st->codec)) {
                        /* only begin sending when got a key frame */
                        if (st->codec.key_frame)
    
                            c->got_key_frame |= 1 << i;
                        if (c->got_key_frame & (1 << i)) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                            ret = c->fmt_ctx.format->write_packet(&c->fmt_ctx, i,
                                                                       payload, payload_size);
                        }
                        break;
                    }
                }
                if (ret) {
                    /* must send trailer now */
                    c->state = HTTPSTATE_SEND_DATA_TRAILER;
                }
            } else {
                /* master case : send everything */
                char *q;
                q = c->buffer;
                memcpy(q, &hdr, sizeof(hdr));
                q += sizeof(hdr);
                memcpy(q, payload, payload_size);
                q += payload_size;
                c->buffer_ptr = c->buffer;
                c->buffer_end = q;
            }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            av_free(payload);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    #endif
            {
                AVPacket pkt;
    
                /* read a packet from the input stream */
                if (c->stream->feed) {
                    ffm_set_write_index(c->fmt_in, 
                                        c->stream->feed->feed_write_index,
                                        c->stream->feed->feed_size);
                }
    
    
                if (c->stream->max_time && 
                    c->stream->max_time + c->start_time > time(0)) {
                    /* We have timed out */
                    c->state = HTTPSTATE_SEND_DATA_TRAILER;
                } else if (av_read_packet(c->fmt_in, &pkt) < 0) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    if (c->stream->feed && c->stream->feed->feed_opened) {
                        /* if coming from feed, it means we reached the end of the
                           ffm file, so must wait for more data */
                        c->state = HTTPSTATE_WAIT_FEED;
                        return 1; /* state changed */
                    } else {
                        /* must send trailer now because eof or error */
                        c->state = HTTPSTATE_SEND_DATA_TRAILER;
                    }
                } else {
                    /* send it to the appropriate stream */
                    if (c->stream->feed) {
                        /* if coming from a feed, select the right stream */
    
                        if (c->switch_pending) {
                            c->switch_pending = 0;
                            for(i=0;i<c->stream->nb_streams;i++) {
                                if (c->switch_feed_streams[i] == pkt.stream_index) {
                                    if (pkt.flags & PKT_FLAG_KEY) {
                                        do_switch_stream(c, i);
                                    }
                                }
                                if (c->switch_feed_streams[i] >= 0) {
                                    c->switch_pending = 1;
                                }
                            }
                        }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                        for(i=0;i<c->stream->nb_streams;i++) {
    
                            if (c->feed_streams[i] == pkt.stream_index) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                                pkt.stream_index = i;
    
                                if (pkt.flags & PKT_FLAG_KEY) {
                                    c->got_key_frame |= 1 << i;
                                }
                                /* See if we have all the key frames, then 
                                 * we start to send. This logic is not quite
                                 * right, but it works for the case of a 
                                 * single video stream with one or more
                                 * audio streams (for which every frame is 
                                 * typically a key frame). 
                                 */
    
                                if (!c->stream->send_on_key || ((c->got_key_frame + 1) >> c->stream->nb_streams)) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                            }
                        }
                    } else {
    
                        AVCodecContext *codec;
                    send_it:
                        /* Fudge here */
                        codec = &c->fmt_ctx.streams[pkt.stream_index]->codec;
    
                        codec->key_frame = ((pkt.flags & PKT_FLAG_KEY) != 0);
    
    #ifdef PJSG
                        if (codec->codec_type == CODEC_TYPE_AUDIO) {
                            codec->frame_size = (codec->sample_rate * pkt.duration + 500000) / 1000000;
                            /* printf("Calculated size %d, from sr %d, duration %d\n", codec->frame_size, codec->sample_rate, pkt.duration); */
                        }
    #endif
    
    
                        if (av_write_packet(&c->fmt_ctx, &pkt, 0))
    
                            c->state = HTTPSTATE_SEND_DATA_TRAILER;
    
                        codec->frame_number++;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    av_free_packet(&pkt);
                }
            }
            break;
        default:
        case HTTPSTATE_SEND_DATA_TRAILER:
            /* last packet test ? */
            if (c->last_packet_sent)
                return -1;
            /* prepare header */
    
            av_write_trailer(&c->fmt_ctx);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            c->last_packet_sent = 1;
            break;
        }
        return 0;
    }
    
    /* should convert the format at the same time */
    static int http_send_data(HTTPContext *c)
    {
        int len, ret;
    
        while (c->buffer_ptr >= c->buffer_end) {
            ret = http_prepare_data(c);
            if (ret < 0)
                return -1;
            else if (ret == 0) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            } else {
                /* state change requested */
                return 0;
            }
        }
    
    
        if (c->buffer_end > c->buffer_ptr) {
            len = write(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr);
            if (len < 0) {
                if (errno != EAGAIN && errno != EINTR) {
                    /* error : close connection */
                    return -1;
                }
            } else {
                c->buffer_ptr += len;
                c->data_count += len;
    
                if (c->stream)
                    c->stream->bytes_served += len;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
        }
        return 0;
    }
    
    static int http_start_receive_data(HTTPContext *c)
    {
        int fd;
    
        if (c->stream->feed_opened)
            return -1;
    
        /* open feed */
        fd = open(c->stream->feed_filename, O_RDWR);
        if (fd < 0)
            return -1;
        c->feed_fd = fd;
        
        c->stream->feed_write_index = ffm_read_write_index(fd);
        c->stream->feed_size = lseek(fd, 0, SEEK_END);
        lseek(fd, 0, SEEK_SET);
    
        /* init buffer input */
        c->buffer_ptr = c->buffer;
        c->buffer_end = c->buffer + FFM_PACKET_SIZE;
        c->stream->feed_opened = 1;
        return 0;
    }
        
    static int http_receive_data(HTTPContext *c)
    {
        HTTPContext *c1;
    
    
        if (c->buffer_end > c->buffer_ptr) {
            int len;
    
            len = read(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr);
            if (len < 0) {
                if (errno != EAGAIN && errno != EINTR) {
                    /* error : close connection */
                    goto fail;
                }
            } else if (len == 0) {
                /* end of connection : close it */
                goto fail;
            } else {
                c->buffer_ptr += len;
                c->data_count += len;
            }
        }
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (c->buffer_ptr >= c->buffer_end) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            /* a packet has been received : write it in the store, except
               if header */
            if (c->data_count > FFM_PACKET_SIZE) {
                
                //            printf("writing pos=0x%Lx size=0x%Lx\n", feed->feed_write_index, feed->feed_size);
                /* XXX: use llseek or url_seek */
                lseek(c->feed_fd, feed->feed_write_index, SEEK_SET);
                write(c->feed_fd, c->buffer, FFM_PACKET_SIZE);
                
                feed->feed_write_index += FFM_PACKET_SIZE;
                /* update file size */
                if (feed->feed_write_index > c->stream->feed_size)
                    feed->feed_size = feed->feed_write_index;
    
                /* handle wrap around if max file size reached */
                if (feed->feed_write_index >= c->stream->feed_max_size)
                    feed->feed_write_index = FFM_PACKET_SIZE;
    
                /* write index */
                ffm_write_write_index(c->feed_fd, feed->feed_write_index);
    
                /* wake up any waiting connections */
                for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
                    if (c1->state == HTTPSTATE_WAIT_FEED && 
                        c1->stream->feed == c->stream->feed) {
                        c1->state = HTTPSTATE_SEND_DATA;
                    }
                }
    
            } else {
                /* We have a header in our hands that contains useful data */
                AVFormatContext s;
    
                AVInputFormat *fmt_in;
    
                ByteIOContext *pb = &s.pb;
                int i;
    
                memset(&s, 0, sizeof(s));
    
                url_open_buf(pb, c->buffer, c->buffer_end - c->buffer, URL_RDONLY);
                pb->buf_end = c->buffer_end;        /* ?? */
                pb->is_streamed = 1;
    
    
                /* use feed output format name to find corresponding input format */
                fmt_in = av_find_input_format(feed->fmt->name);
                if (!fmt_in)
                    goto fail;
    
    
                s.priv_data = av_mallocz(fmt_in->priv_data_size);
                if (!s.priv_data)
                    goto fail;
    
    
                if (fmt_in->read_header(&s, 0) < 0) {
    
                    goto fail;
                }
    
                /* Now we have the actual streams */
                if (s.nb_streams != feed->nb_streams) {
    
                    memcpy(&feed->streams[i]->codec, 
                           &s.streams[i]->codec, sizeof(AVCodecContext));
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
            c->buffer_ptr = c->buffer;
        }
    
        return 0;
     fail:
        c->stream->feed_opened = 0;
        close(c->feed_fd);
        return -1;
    }
    
    /* return the stream number in the feed */
    int add_av_stream(FFStream *feed,
                      AVStream *st)
    {
        AVStream *fst;
        AVCodecContext *av, *av1;
        int i;
    
        av = &st->codec;
        for(i=0;i<feed->nb_streams;i++) {
            st = feed->streams[i];
            av1 = &st->codec;
    
            if (av1->codec_id == av->codec_id &&
                av1->codec_type == av->codec_type &&
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                av1->bit_rate == av->bit_rate) {
    
                switch(av->codec_type) {
                case CODEC_TYPE_AUDIO:
                    if (av1->channels == av->channels &&
                        av1->sample_rate == av->sample_rate)
                        goto found;
                    break;
                case CODEC_TYPE_VIDEO:
                    if (av1->width == av->width &&
                        av1->height == av->height &&
                        av1->frame_rate == av->frame_rate &&
                        av1->gop_size == av->gop_size)
                        goto found;
                    break;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                }
            }
        }
        
        fst = av_mallocz(sizeof(AVStream));
        if (!fst)
            return -1;
        fst->priv_data = av_mallocz(sizeof(FeedData));
        memcpy(&fst->codec, av, sizeof(AVCodecContext));
        feed->streams[feed->nb_streams++] = fst;
        return feed->nb_streams - 1;
     found:
        return i;
    }
    
    /* compute the needed AVStream for each feed */
    void build_feed_streams(void)
    {
        FFStream *stream, *feed;
        int i;
    
        /* gather all streams */
        for(stream = first_stream; stream != NULL; stream = stream->next) {
            feed = stream->feed;
            if (feed) {
                if (!stream->is_feed) {
                    for(i=0;i<stream->nb_streams;i++) {
                        stream->feed_streams[i] = add_av_stream(feed, stream->streams[i]);
                    }
    
                }
            }
        }
    
        /* gather all streams */
        for(stream = first_stream; stream != NULL; stream = stream->next) {
            feed = stream->feed;
            if (feed) {
                if (stream->is_feed) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    for(i=0;i<stream->nb_streams;i++) {
                        stream->feed_streams[i] = i;
                    }
                }
            }
        }
    
        /* create feed files if needed */
        for(feed = first_feed; feed != NULL; feed = feed->next_feed) {
            int fd;
    
            if (!url_exist(feed->feed_filename)) {
                AVFormatContext s1, *s = &s1;
    
                /* only write the header of the ffm file */
                if (url_fopen(&s->pb, feed->feed_filename, URL_WRONLY) < 0) {
                    fprintf(stderr, "Could not open output feed file '%s'\n",
                            feed->feed_filename);
                    exit(1);
                }
    
                s->oformat = feed->fmt;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                s->nb_streams = feed->nb_streams;
                for(i=0;i<s->nb_streams;i++) {
                    AVStream *st;
                    st = feed->streams[i];
                    s->streams[i] = st;
                }
    
                av_write_header(s);
                /* XXX: need better api */
                av_freep(&s->priv_data);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                url_fclose(&s->pb);
            }
            /* get feed size and write index */
            fd = open(feed->feed_filename, O_RDONLY);
            if (fd < 0) {
                fprintf(stderr, "Could not open output feed file '%s'\n",
                        feed->feed_filename);
                exit(1);
            }
    
            feed->feed_write_index = ffm_read_write_index(fd);
            feed->feed_size = lseek(fd, 0, SEEK_END);
            /* ensure that we do not wrap before the end of file */
            if (feed->feed_max_size < feed->feed_size)
                feed->feed_max_size = feed->feed_size;
    
            close(fd);
        }
    }
    
    static void get_arg(char *buf, int buf_size, const char **pp)
    {
        const char *p;
        char *q;
        int quote;
    
        p = *pp;
        while (isspace(*p)) p++;
        q = buf;
        quote = 0;
        if (*p == '\"' || *p == '\'')
            quote = *p++;
        for(;;) {
            if (quote) {
                if (*p == quote)
                    break;
            } else {
                if (isspace(*p))
                    break;
            }
            if (*p == '\0')
                break;
            if ((q - buf) < buf_size - 1)
                *q++ = *p;
            p++;
        }
        *q = '\0';
        if (quote && *p == quote)
            p++;
        *pp = p;
    }
    
    /* add a codec and set the default parameters */
    void add_codec(FFStream *stream, AVCodecContext *av)
    {
        AVStream *st;
    
        /* compute default parameters */
        switch(av->codec_type) {
        case CODEC_TYPE_AUDIO:
            if (av->bit_rate == 0)
                av->bit_rate = 64000;
            if (av->sample_rate == 0)
                av->sample_rate = 22050;
            if (av->channels == 0)
                av->channels = 1;
            break;
        case CODEC_TYPE_VIDEO:
            if (av->bit_rate == 0)
                av->bit_rate = 64000;
            if (av->frame_rate == 0)
                av->frame_rate = 5 * FRAME_RATE_BASE;
            if (av->width == 0 || av->height == 0) {
                av->width = 160;
                av->height = 128;
            }
    
            /* Bitrate tolerance is less for streaming */
    
            if (av->bit_rate_tolerance == 0)
                av->bit_rate_tolerance = av->bit_rate / 4;
            if (av->qmin == 0)
                av->qmin = 3;
            if (av->qmax == 0)
                av->qmax = 31;
            if (av->max_qdiff == 0)
                av->max_qdiff = 3;
    
            av->qcompress = 0.5;
            av->qblur = 0.5;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            break;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
        st = av_mallocz(sizeof(AVStream));
        if (!st)
            return;
        stream->streams[stream->nb_streams++] = st;
        memcpy(&st->codec, av, sizeof(AVCodecContext));
    }
    
    
    int opt_audio_codec(const char *arg)
    {
        AVCodec *p;
    
        p = first_avcodec;
        while (p) {
            if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
                break;
            p = p->next;
        }
        if (p == NULL) {
            return CODEC_ID_NONE;
        }
    
        return p->id;
    }
    
    int opt_video_codec(const char *arg)
    {
        AVCodec *p;
    
        p = first_avcodec;
        while (p) {
            if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
                break;
            p = p->next;
        }
        if (p == NULL) {
            return CODEC_ID_NONE;
        }
    
        return p->id;
    }
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    int parse_ffconfig(const char *filename)
    {
        FILE *f;
        char line[1024];
        char cmd[64];
        char arg[1024];
        const char *p;
        int val, errors, line_num;
    
        FFStream **last_stream, *stream, *redirect;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        FFStream **last_feed, *feed;
        AVCodecContext audio_enc, video_enc;
        int audio_id, video_id;
    
        f = fopen(filename, "r");
        if (!f) {
            perror(filename);
            return -1;