proto: Use .priv_data_size to allocate the private context

This simplifies the open functions by avoiding one function
call that needs error checking, reducing the amount of
extra bulk code.

Signed-off-by: Martin Storsjö <martin@martin.st>
pull/2/head
Martin Storsjö 13 years ago
parent 9c6777bd93
commit 7e58050590
  1. 8
      libavformat/applehttpproto.c
  2. 10
      libavformat/concat.c
  3. 10
      libavformat/gopher.c
  4. 24
      libavformat/librtmp.c
  5. 7
      libavformat/mmsh.c
  6. 7
      libavformat/mmst.c
  7. 8
      libavformat/rtmpproto.c
  8. 10
      libavformat/rtpproto.c
  9. 10
      libavformat/tcp.c
  10. 10
      libavformat/udp.c

@ -181,23 +181,18 @@ static int applehttp_close(URLContext *h)
free_segment_list(s); free_segment_list(s);
free_variant_list(s); free_variant_list(s);
ffurl_close(s->seg_hd); ffurl_close(s->seg_hd);
av_free(s);
return 0; return 0;
} }
static int applehttp_open(URLContext *h, const char *uri, int flags) static int applehttp_open(URLContext *h, const char *uri, int flags)
{ {
AppleHTTPContext *s; AppleHTTPContext *s = h->priv_data;
int ret, i; int ret, i;
const char *nested_url; const char *nested_url;
if (flags & AVIO_FLAG_WRITE) if (flags & AVIO_FLAG_WRITE)
return AVERROR(ENOSYS); return AVERROR(ENOSYS);
s = av_mallocz(sizeof(AppleHTTPContext));
if (!s)
return AVERROR(ENOMEM);
h->priv_data = s;
h->is_streamed = 1; h->is_streamed = 1;
if (av_strstart(uri, "applehttp+", &nested_url)) { if (av_strstart(uri, "applehttp+", &nested_url)) {
@ -304,4 +299,5 @@ URLProtocol ff_applehttp_protocol = {
.url_read = applehttp_read, .url_read = applehttp_read,
.url_close = applehttp_close, .url_close = applehttp_close,
.flags = URL_PROTOCOL_FLAG_NESTED_SCHEME, .flags = URL_PROTOCOL_FLAG_NESTED_SCHEME,
.priv_data_size = sizeof(AppleHTTPContext),
}; };

@ -50,7 +50,6 @@ static av_cold int concat_close(URLContext *h)
err |= ffurl_close(nodes[i].uc); err |= ffurl_close(nodes[i].uc);
av_freep(&data->nodes); av_freep(&data->nodes);
av_freep(&h->priv_data);
return err < 0 ? -1 : 0; return err < 0 ? -1 : 0;
} }
@ -62,16 +61,11 @@ static av_cold int concat_open(URLContext *h, const char *uri, int flags)
int64_t size; int64_t size;
size_t len, i; size_t len, i;
URLContext *uc; URLContext *uc;
struct concat_data *data; struct concat_data *data = h->priv_data;
struct concat_nodes *nodes; struct concat_nodes *nodes;
av_strstart(uri, "concat:", &uri); av_strstart(uri, "concat:", &uri);
/* creating data */
if (!(data = av_mallocz(sizeof(*data))))
return AVERROR(ENOMEM);
h->priv_data = data;
for (i = 0, len = 1; uri[i]; i++) for (i = 0, len = 1; uri[i]; i++)
if (uri[i] == *AV_CAT_SEPARATOR) if (uri[i] == *AV_CAT_SEPARATOR)
/* integer overflow */ /* integer overflow */
@ -81,7 +75,6 @@ static av_cold int concat_open(URLContext *h, const char *uri, int flags)
} }
if (!(nodes = av_malloc(sizeof(*nodes) * len))) { if (!(nodes = av_malloc(sizeof(*nodes) * len))) {
av_freep(&h->priv_data);
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
} else } else
data->nodes = nodes; data->nodes = nodes;
@ -196,4 +189,5 @@ URLProtocol ff_concat_protocol = {
.url_read = concat_read, .url_read = concat_read,
.url_seek = concat_seek, .url_seek = concat_seek,
.url_close = concat_close, .url_close = concat_close,
.priv_data_size = sizeof(struct concat_data),
}; };

@ -72,24 +72,17 @@ static int gopher_close(URLContext *h)
ffurl_close(s->hd); ffurl_close(s->hd);
s->hd = NULL; s->hd = NULL;
} }
av_freep(&h->priv_data);
return 0; return 0;
} }
static int gopher_open(URLContext *h, const char *uri, int flags) static int gopher_open(URLContext *h, const char *uri, int flags)
{ {
GopherContext *s; GopherContext *s = h->priv_data;
char hostname[1024], auth[1024], path[1024], buf[1024]; char hostname[1024], auth[1024], path[1024], buf[1024];
int port, err; int port, err;
h->is_streamed = 1; h->is_streamed = 1;
s = av_malloc(sizeof(GopherContext));
if (!s) {
return AVERROR(ENOMEM);
}
h->priv_data = s;
/* needed in any case to build the host string */ /* needed in any case to build the host string */
av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
path, sizeof(path), uri); path, sizeof(path), uri);
@ -127,4 +120,5 @@ URLProtocol ff_gopher_protocol = {
.url_read = gopher_read, .url_read = gopher_read,
.url_write = gopher_write, .url_write = gopher_write,
.url_close = gopher_close, .url_close = gopher_close,
.priv_data_size = sizeof(GopherContext),
}; };

@ -52,7 +52,6 @@ static int rtmp_close(URLContext *s)
RTMP *r = s->priv_data; RTMP *r = s->priv_data;
RTMP_Close(r); RTMP_Close(r);
av_free(r);
return 0; return 0;
} }
@ -70,13 +69,9 @@ static int rtmp_close(URLContext *s)
*/ */
static int rtmp_open(URLContext *s, const char *uri, int flags) static int rtmp_open(URLContext *s, const char *uri, int flags)
{ {
RTMP *r; RTMP *r = s->priv_data;
int rc; int rc;
r = av_mallocz(sizeof(RTMP));
if (!r)
return AVERROR(ENOMEM);
switch (av_log_get_level()) { switch (av_log_get_level()) {
default: default:
case AV_LOG_FATAL: rc = RTMP_LOGCRIT; break; case AV_LOG_FATAL: rc = RTMP_LOGCRIT; break;
@ -103,11 +98,9 @@ static int rtmp_open(URLContext *s, const char *uri, int flags)
goto fail; goto fail;
} }
s->priv_data = r;
s->is_streamed = 1; s->is_streamed = 1;
return 0; return 0;
fail: fail:
av_free(r);
return rc; return rc;
} }
@ -167,7 +160,8 @@ URLProtocol ff_rtmp_protocol = {
.url_close = rtmp_close, .url_close = rtmp_close,
.url_read_pause = rtmp_read_pause, .url_read_pause = rtmp_read_pause,
.url_read_seek = rtmp_read_seek, .url_read_seek = rtmp_read_seek,
.url_get_file_handle = rtmp_get_file_handle .url_get_file_handle = rtmp_get_file_handle,
.priv_data_size = sizeof(RTMP),
}; };
URLProtocol ff_rtmpt_protocol = { URLProtocol ff_rtmpt_protocol = {
@ -178,7 +172,8 @@ URLProtocol ff_rtmpt_protocol = {
.url_close = rtmp_close, .url_close = rtmp_close,
.url_read_pause = rtmp_read_pause, .url_read_pause = rtmp_read_pause,
.url_read_seek = rtmp_read_seek, .url_read_seek = rtmp_read_seek,
.url_get_file_handle = rtmp_get_file_handle .url_get_file_handle = rtmp_get_file_handle,
.priv_data_size = sizeof(RTMP),
}; };
URLProtocol ff_rtmpe_protocol = { URLProtocol ff_rtmpe_protocol = {
@ -189,7 +184,8 @@ URLProtocol ff_rtmpe_protocol = {
.url_close = rtmp_close, .url_close = rtmp_close,
.url_read_pause = rtmp_read_pause, .url_read_pause = rtmp_read_pause,
.url_read_seek = rtmp_read_seek, .url_read_seek = rtmp_read_seek,
.url_get_file_handle = rtmp_get_file_handle .url_get_file_handle = rtmp_get_file_handle,
.priv_data_size = sizeof(RTMP),
}; };
URLProtocol ff_rtmpte_protocol = { URLProtocol ff_rtmpte_protocol = {
@ -200,7 +196,8 @@ URLProtocol ff_rtmpte_protocol = {
.url_close = rtmp_close, .url_close = rtmp_close,
.url_read_pause = rtmp_read_pause, .url_read_pause = rtmp_read_pause,
.url_read_seek = rtmp_read_seek, .url_read_seek = rtmp_read_seek,
.url_get_file_handle = rtmp_get_file_handle .url_get_file_handle = rtmp_get_file_handle,
.priv_data_size = sizeof(RTMP),
}; };
URLProtocol ff_rtmps_protocol = { URLProtocol ff_rtmps_protocol = {
@ -211,5 +208,6 @@ URLProtocol ff_rtmps_protocol = {
.url_close = rtmp_close, .url_close = rtmp_close,
.url_read_pause = rtmp_read_pause, .url_read_pause = rtmp_read_pause,
.url_read_seek = rtmp_read_seek, .url_read_seek = rtmp_read_seek,
.url_get_file_handle = rtmp_get_file_handle .url_get_file_handle = rtmp_get_file_handle,
.priv_data_size = sizeof(RTMP),
}; };

@ -68,7 +68,6 @@ static int mmsh_close(URLContext *h)
ffurl_close(mms->mms_hd); ffurl_close(mms->mms_hd);
av_free(mms->streams); av_free(mms->streams);
av_free(mms->asf_header); av_free(mms->asf_header);
av_freep(&h->priv_data);
return 0; return 0;
} }
@ -217,12 +216,9 @@ static int mmsh_open(URLContext *h, const char *uri, int flags)
char httpname[256], path[256], host[128], location[1024]; char httpname[256], path[256], host[128], location[1024];
char *stream_selection = NULL; char *stream_selection = NULL;
char headers[1024]; char headers[1024];
MMSHContext *mmsh; MMSHContext *mmsh = h->priv_data;
MMSContext *mms; MMSContext *mms;
mmsh = h->priv_data = av_mallocz(sizeof(MMSHContext));
if (!h->priv_data)
return AVERROR(ENOMEM);
mmsh->request_seq = h->is_streamed = 1; mmsh->request_seq = h->is_streamed = 1;
mms = &mmsh->mms; mms = &mmsh->mms;
av_strlcpy(location, uri, sizeof(location)); av_strlcpy(location, uri, sizeof(location));
@ -367,4 +363,5 @@ URLProtocol ff_mmsh_protocol = {
.url_open = mmsh_open, .url_open = mmsh_open,
.url_read = mmsh_read, .url_read = mmsh_read,
.url_close = mmsh_close, .url_close = mmsh_close,
.priv_data_size = sizeof(MMSHContext),
}; };

@ -470,7 +470,6 @@ static int mms_close(URLContext *h)
/* free all separately allocated pointers in mms */ /* free all separately allocated pointers in mms */
av_free(mms->streams); av_free(mms->streams);
av_free(mms->asf_header); av_free(mms->asf_header);
av_freep(&h->priv_data);
return 0; return 0;
} }
@ -502,15 +501,12 @@ static void clear_stream_buffers(MMSContext *mms)
static int mms_open(URLContext *h, const char *uri, int flags) static int mms_open(URLContext *h, const char *uri, int flags)
{ {
MMSTContext *mmst; MMSTContext *mmst = h->priv_data;
MMSContext *mms; MMSContext *mms;
int port, err; int port, err;
char tcpname[256]; char tcpname[256];
h->is_streamed = 1; h->is_streamed = 1;
mmst = h->priv_data = av_mallocz(sizeof(MMSTContext));
if (!h->priv_data)
return AVERROR(ENOMEM);
mms = &mmst->mms; mms = &mmst->mms;
// only for MMS over TCP, so set proto = NULL // only for MMS over TCP, so set proto = NULL
@ -628,4 +624,5 @@ URLProtocol ff_mmst_protocol = {
.url_open = mms_open, .url_open = mms_open,
.url_read = mms_read, .url_read = mms_read,
.url_close = mms_close, .url_close = mms_close,
.priv_data_size = sizeof(MMSTContext),
}; };

@ -784,7 +784,6 @@ static int rtmp_close(URLContext *h)
av_freep(&rt->flv_data); av_freep(&rt->flv_data);
ffurl_close(rt->stream); ffurl_close(rt->stream);
av_free(rt);
return 0; return 0;
} }
@ -799,16 +798,12 @@ static int rtmp_close(URLContext *h)
*/ */
static int rtmp_open(URLContext *s, const char *uri, int flags) static int rtmp_open(URLContext *s, const char *uri, int flags)
{ {
RTMPContext *rt; RTMPContext *rt = s->priv_data;
char proto[8], hostname[256], path[1024], *fname; char proto[8], hostname[256], path[1024], *fname;
uint8_t buf[2048]; uint8_t buf[2048];
int port; int port;
int ret; int ret;
rt = av_mallocz(sizeof(RTMPContext));
if (!rt)
return AVERROR(ENOMEM);
s->priv_data = rt;
rt->is_input = !(flags & AVIO_FLAG_WRITE); rt->is_input = !(flags & AVIO_FLAG_WRITE);
av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname), &port, av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname), &port,
@ -1002,4 +997,5 @@ URLProtocol ff_rtmp_protocol = {
.url_read = rtmp_read, .url_read = rtmp_read,
.url_write = rtmp_write, .url_write = rtmp_write,
.url_close = rtmp_close, .url_close = rtmp_close,
.priv_data_size = sizeof(RTMPContext),
}; };

@ -136,7 +136,7 @@ static void build_udp_url(char *buf, int buf_size,
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 = h->priv_data;
int rtp_port, rtcp_port, int rtp_port, rtcp_port,
ttl, connect, ttl, connect,
local_rtp_port, local_rtcp_port, max_packet_size; local_rtp_port, local_rtcp_port, max_packet_size;
@ -145,11 +145,6 @@ static int rtp_open(URLContext *h, const char *uri, int flags)
char path[1024]; char path[1024];
const char *p; const char *p;
s = av_mallocz(sizeof(RTPContext));
if (!s)
return AVERROR(ENOMEM);
h->priv_data = s;
av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port, av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
path, sizeof(path), uri); path, sizeof(path), uri);
/* extract parameters */ /* extract parameters */
@ -213,7 +208,6 @@ static int rtp_open(URLContext *h, const char *uri, int flags)
ffurl_close(s->rtp_hd); ffurl_close(s->rtp_hd);
if (s->rtcp_hd) if (s->rtcp_hd)
ffurl_close(s->rtcp_hd); ffurl_close(s->rtcp_hd);
av_free(s);
return AVERROR(EIO); return AVERROR(EIO);
} }
@ -290,7 +284,6 @@ static int rtp_close(URLContext *h)
ffurl_close(s->rtp_hd); ffurl_close(s->rtp_hd);
ffurl_close(s->rtcp_hd); ffurl_close(s->rtcp_hd);
av_free(s);
return 0; return 0;
} }
@ -336,4 +329,5 @@ URLProtocol ff_rtp_protocol = {
.url_write = rtp_write, .url_write = rtp_write,
.url_close = rtp_close, .url_close = rtp_close,
.url_get_file_handle = rtp_get_file_handle, .url_get_file_handle = rtp_get_file_handle,
.priv_data_size = sizeof(RTPContext),
}; };

@ -39,7 +39,7 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
{ {
struct addrinfo hints, *ai, *cur_ai; struct addrinfo hints, *ai, *cur_ai;
int port, fd = -1; int port, fd = -1;
TCPContext *s = NULL; TCPContext *s = h->priv_data;
int listen_socket = 0; int listen_socket = 0;
const char *p; const char *p;
char buf[256]; char buf[256];
@ -135,12 +135,6 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
goto fail; goto fail;
} }
} }
s = av_malloc(sizeof(TCPContext));
if (!s) {
freeaddrinfo(ai);
return AVERROR(ENOMEM);
}
h->priv_data = s;
h->is_streamed = 1; h->is_streamed = 1;
s->fd = fd; s->fd = fd;
freeaddrinfo(ai); freeaddrinfo(ai);
@ -193,7 +187,6 @@ static int tcp_close(URLContext *h)
{ {
TCPContext *s = h->priv_data; TCPContext *s = h->priv_data;
closesocket(s->fd); closesocket(s->fd);
av_free(s);
return 0; return 0;
} }
@ -210,4 +203,5 @@ URLProtocol ff_tcp_protocol = {
.url_write = tcp_write, .url_write = tcp_write,
.url_close = tcp_close, .url_close = tcp_close,
.url_get_file_handle = tcp_get_file_handle, .url_get_file_handle = tcp_get_file_handle,
.priv_data_size = sizeof(TCPContext),
}; };

@ -306,7 +306,7 @@ static int udp_open(URLContext *h, const char *uri, int flags)
{ {
char hostname[1024], localaddr[1024] = ""; char hostname[1024], localaddr[1024] = "";
int port, udp_fd = -1, tmp, bind_ret = -1; int port, udp_fd = -1, tmp, bind_ret = -1;
UDPContext *s = NULL; UDPContext *s = h->priv_data;
int is_output; int is_output;
const char *p; const char *p;
char buf[256]; char buf[256];
@ -319,11 +319,6 @@ static int udp_open(URLContext *h, const char *uri, int flags)
is_output = !(flags & AVIO_FLAG_READ); is_output = !(flags & AVIO_FLAG_READ);
s = av_mallocz(sizeof(UDPContext));
if (!s)
return AVERROR(ENOMEM);
h->priv_data = s;
s->ttl = 16; s->ttl = 16;
s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE; s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
@ -440,7 +435,6 @@ static int udp_open(URLContext *h, const char *uri, int flags)
fail: fail:
if (udp_fd >= 0) if (udp_fd >= 0)
closesocket(udp_fd); closesocket(udp_fd);
av_free(s);
return AVERROR(EIO); return AVERROR(EIO);
} }
@ -486,7 +480,6 @@ static int udp_close(URLContext *h)
if (s->is_multicast && (h->flags & AVIO_FLAG_READ)) if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr); udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
closesocket(s->udp_fd); closesocket(s->udp_fd);
av_free(s);
return 0; return 0;
} }
@ -497,4 +490,5 @@ URLProtocol ff_udp_protocol = {
.url_write = udp_write, .url_write = udp_write,
.url_close = udp_close, .url_close = udp_close,
.url_get_file_handle = udp_get_file_handle, .url_get_file_handle = udp_get_file_handle,
.priv_data_size = sizeof(UDPContext),
}; };

Loading…
Cancel
Save