Revision 21a569f3

View differences:

doc/protocols.texi
415 415
@item connect=@var{1|0}
416 416
Initialize the UDP socket with @code{connect()}. In this case, the
417 417
destination address can't be changed with udp_set_remote_url later.
418
If the destination address isn't known at the start, this option can
419
be specified in udp_set_remote_url, too.
418 420
This allows finding out the source address for the packets with getsockname,
419 421
and makes writes return with AVERROR(ECONNREFUSED) if "destination
420 422
unreachable" is received.
423
For receiving, this gives the benefit of only receiving packets from
424
the specified peer address/port.
421 425
@end table
422 426

  
423 427
Some usage examples of the udp protocol with @file{ffmpeg} follow.
libavformat/udp.c
245 245
int udp_set_remote_url(URLContext *h, const char *uri)
246 246
{
247 247
    UDPContext *s = h->priv_data;
248
    char hostname[256];
248
    char hostname[256], buf[10];
249 249
    int port;
250
    const char *p;
250 251

  
251 252
    av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
252 253

  
......
256 257
        return AVERROR(EIO);
257 258
    }
258 259
    s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
260
    p = strchr(uri, '?');
261
    if (p) {
262
        if (find_info_tag(buf, sizeof(buf), "connect", p)) {
263
            int was_connected = s->is_connected;
264
            s->is_connected = strtol(buf, NULL, 10);
265
            if (s->is_connected && !was_connected) {
266
                if (connect(s->udp_fd, (struct sockaddr *) &s->dest_addr,
267
                            s->dest_addr_len)) {
268
                    s->is_connected = 0;
269
                    av_log(NULL, AV_LOG_ERROR, "connect: %s\n", strerror(errno));
270
                    return AVERROR(EIO);
271
                }
272
            }
273
        }
274
    }
259 275

  
260 276
    return 0;
261 277
}

Also available in: Unified diff