Commit 83608eed authored by bellard's avatar bellard

added local port option


git-svn-id: file:///var/local/repositories/ffmpeg/trunk@791 9553f0bf-9b14-0410-a0b8-cfaf0461ba5b
parent d0c772c8
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "avformat.h" #include "avformat.h"
#include <unistd.h> #include <unistd.h>
#include <stdarg.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
...@@ -39,10 +40,6 @@ typedef struct RTPContext { ...@@ -39,10 +40,6 @@ typedef struct RTPContext {
* get the local port first, then you must call this function to set * get the local port first, then you must call this function to set
* the remote server address. * the remote server address.
* *
* url syntax: rtp://host:port[?option=val...]
* option: 'multicast=1' : enable multicast
* 'ttl=n' : set the ttl value (for multicast only)
*
* @param s1 media file context * @param s1 media file context
* @param uri of the remote server * @param uri of the remote server
* @return zero if no error. * @return zero if no error.
...@@ -52,6 +49,7 @@ int rtp_set_remote_url(URLContext *h, const char *uri) ...@@ -52,6 +49,7 @@ int rtp_set_remote_url(URLContext *h, const char *uri)
RTPContext *s = h->priv_data; RTPContext *s = h->priv_data;
char hostname[256]; char hostname[256];
int port; int port;
char buf[1024]; char buf[1024];
char path[1024]; char path[1024];
...@@ -67,14 +65,51 @@ int rtp_set_remote_url(URLContext *h, const char *uri) ...@@ -67,14 +65,51 @@ int rtp_set_remote_url(URLContext *h, const char *uri)
} }
/* add option to url of the form:
"http://host:port/path?option1=val1&option2=val2... */
void url_add_option(char *buf, int buf_size, const char *fmt, ...)
{
char buf1[1024];
va_list ap;
va_start(ap, fmt);
if (strchr(buf, '?'))
pstrcat(buf, buf_size, "&");
else
pstrcat(buf, buf_size, "?");
vsnprintf(buf1, sizeof(buf1), fmt, ap);
pstrcat(buf, buf_size, buf1);
va_end(ap);
}
void build_udp_url(char *buf, int buf_size,
const char *hostname, int port,
int local_port, int multicast, int ttl)
{
snprintf(buf, buf_size, "udp://%s:%d", hostname, port);
if (local_port >= 0)
url_add_option(buf, buf_size, "localport=%d", local_port);
if (multicast)
url_add_option(buf, buf_size, "multicast=1", multicast);
if (ttl >= 0)
url_add_option(buf, buf_size, "ttl=%d", ttl);
}
/*
* url syntax: rtp://host:port[?option=val...]
* option: 'multicast=1' : enable multicast
* 'ttl=n' : set the ttl value (for multicast only)
* 'localport=n' : set the local port to n
*
*/
static int rtp_open(URLContext *h, const char *uri, int flags) static int rtp_open(URLContext *h, const char *uri, int flags)
{ {
RTPContext *s; RTPContext *s;
int port, is_output, local_port; int port, is_output, is_multicast, ttl, local_port;
char hostname[256]; char hostname[256];
char buf[1024]; char buf[1024];
char path[1024]; char path[1024];
URLContext *tmp_hd; const char *p;
is_output = (flags & URL_WRONLY); is_output = (flags & URL_WRONLY);
...@@ -85,9 +120,23 @@ static int rtp_open(URLContext *h, const char *uri, int flags) ...@@ -85,9 +120,23 @@ static int rtp_open(URLContext *h, const char *uri, int flags)
url_split(NULL, 0, hostname, sizeof(hostname), &port, url_split(NULL, 0, hostname, sizeof(hostname), &port,
path, sizeof(path), uri); path, sizeof(path), uri);
/* extract parameters */
is_multicast = 0;
ttl = -1;
local_port = -1;
p = strchr(uri, '?');
if (p) {
is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
ttl = strtol(buf, NULL, 10);
}
if (find_info_tag(buf, sizeof(buf), "localport", p)) {
local_port = strtol(buf, NULL, 10);
}
}
snprintf(buf, sizeof(buf), "udp://%s:%d%s", build_udp_url(buf, sizeof(buf),
hostname, port, path); hostname, port, local_port, is_multicast, ttl);
if (url_open(&s->rtp_hd, buf, flags) < 0) if (url_open(&s->rtp_hd, buf, flags) < 0)
goto fail; goto fail;
local_port = udp_get_local_port(s->rtp_hd); local_port = udp_get_local_port(s->rtp_hd);
...@@ -95,10 +144,8 @@ static int rtp_open(URLContext *h, const char *uri, int flags) ...@@ -95,10 +144,8 @@ static int rtp_open(URLContext *h, const char *uri, int flags)
/* well, should suppress localport in path */ /* well, should suppress localport in path */
snprintf(buf, sizeof(buf), "udp://%s:%d%s%clocalport=%d", build_udp_url(buf, sizeof(buf),
hostname, port + 1, path, hostname, port + 1, local_port + 1, is_multicast, ttl);
strchr(path, '?') != NULL ? '&' : '?',
local_port + 1);
if (url_open(&s->rtcp_hd, buf, flags) < 0) if (url_open(&s->rtcp_hd, buf, flags) < 0)
goto fail; goto fail;
...@@ -182,7 +229,7 @@ static int rtp_read(URLContext *h, UINT8 *buf, int size) ...@@ -182,7 +229,7 @@ static int rtp_read(URLContext *h, UINT8 *buf, int size)
static int rtp_write(URLContext *h, UINT8 *buf, int size) static int rtp_write(URLContext *h, UINT8 *buf, int size)
{ {
RTPContext *s = h->priv_data; RTPContext *s = h->priv_data;
int ret, fd; int ret;
URLContext *hd; URLContext *hd;
if (buf[1] >= 200 && buf[1] <= 204) { if (buf[1] >= 200 && buf[1] <= 204) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment