[FFmpeg-devel] [RFC] switch to poll()

Ronald S. Bultje rsbultje
Sun Nov 14 18:35:26 CET 2010


Hi,

On Sun, Nov 14, 2010 at 5:22 PM, Luca Barbato <lu_zero at gentoo.org> wrote:
> On 11/14/2010 12:22 PM, Luca Barbato wrote:
>> The only place in which we risk to add more code is rtsp, now I'm trying
>> to figure out a saner way to check there.
>
> Gave up trying to go to the saner route first.
[..]
> --- a/libavformat/rtsp.c
> +++ b/libavformat/rtsp.c
> @@ -1439,55 +1439,55 @@ static int udp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
>  {
>      RTSPState *rt = s->priv_data;
>      RTSPStream *rtsp_st;
> -    fd_set rfds;
> -    int fd, fd_rtcp, fd_max, n, i, ret, tcp_fd, timeout_cnt = 0;
> -    struct timeval tv;
> -
> +    int fd, fd_rtcp, n, i, ret, tcp_fd, timeout_cnt = 0;
> +    int max_p = 0;
> +    //FIXME use malloc
> +    struct pollfd p[2*rt->nb_rtsp_streams+1];

Indeed.

>      for (;;) {
>          if (url_interrupt_cb())
>              return AVERROR(EINTR);
>          if (wait_end && wait_end - av_gettime() < 0)
>              return AVERROR(EAGAIN);
> -        FD_ZERO(&rfds);
>          if (rt->rtsp_hd) {
> -            tcp_fd = fd_max = url_get_file_handle(rt->rtsp_hd);
> -            FD_SET(tcp_fd, &rfds);
> +            tcp_fd = url_get_file_handle(rt->rtsp_hd);
> +            p[max_p].fd = tcp_fd;
> +            p[max_p++].events = POLLIN;
>          } else {
> -            fd_max = 0;
>              tcp_fd = -1;
>          }
>          for (i = 0; i < rt->nb_rtsp_streams; i++) {
>              rtsp_st = rt->rtsp_streams[i];
>              if (rtsp_st->rtp_handle) {
> -                fd = url_get_file_handle(rtsp_st->rtp_handle);
> -                fd_rtcp = rtp_get_rtcp_file_handle(rtsp_st->rtp_handle);
> -                if (FFMAX(fd, fd_rtcp) > fd_max)
> -                    fd_max = FFMAX(fd, fd_rtcp);
> -                FD_SET(fd, &rfds);
> -                FD_SET(fd_rtcp, &rfds);
> +                p[max_p].fd = url_get_file_handle(rtsp_st->rtp_handle);
> +                p[max_p++].events = POLLIN;
> +                p[max_p].fd = rtp_get_rtcp_file_handle(rtsp_st->rtp_handle);
> +                p[max_p++].events = POLLIN;
>              }
>          }
> -        tv.tv_sec = 0;
> -        tv.tv_usec = SELECT_TIMEOUT_MS * 1000;
> -        n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
> +        n = poll(p, max_p, SELECT_TIMEOUT_MS);
>          if (n > 0) {
>              timeout_cnt = 0;
> +            //FIXME this loop should be simplified
>              for (i = 0; i < rt->nb_rtsp_streams; i++) {
>                  rtsp_st = rt->rtsp_streams[i];
>                  if (rtsp_st->rtp_handle) {
> +                    int j = 1;
>                      fd = url_get_file_handle(rtsp_st->rtp_handle);
>                      fd_rtcp = rtp_get_rtcp_file_handle(rtsp_st->rtp_handle);
> -                    if (FD_ISSET(fd_rtcp, &rfds) || FD_ISSET(fd, &rfds)) {
> +                    for(j = 1; j<max_p; j++) {
> +                    if ((p[j].revents & POLLIN) &&
> +                        (p[j].fd == fd_rtcp || p[j].fd == fd)) {
>                          ret = url_read(rtsp_st->rtp_handle, buf, buf_size);
>                          if (ret > 0) {
>                              *prtsp_st = rtsp_st;
>                              return ret;
>                          }
>                      }
> +                    }
>                  }
>              }

int j = 1 - (tcp_fd == -1);
for (i = 0; i < rt->nb_rtsp_streams; i++) {
    if (rtsp_st->rtp_handle) {
        if (p[j].revents & POLLIN) /* rtp fd */ ||
            p[j+1].revents & POLLIN) /* rtcp fd */) {
            ..
        }
        j += 2;
    }
}

Right?

Rest is fine.

Ronald



More information about the ffmpeg-devel mailing list