diff options
author | Michael Niedermayer <michaelni@gmx.at> | 2011-11-06 02:18:50 +0100 |
---|---|---|
committer | Michael Niedermayer <michaelni@gmx.at> | 2011-11-06 02:23:34 +0100 |
commit | d8cab5c26b0987f139830937e1f30b2a10822680 (patch) | |
tree | 1328facddcdaeac6881f32b7b3511806a68d2111 /libavformat | |
parent | 3b4798a87375c6090215185a87d51358cbb5af84 (diff) | |
parent | 18ae3626405a8b3d6dbb7e5b848d354cd7bf9a47 (diff) | |
download | ffmpeg-d8cab5c26b0987f139830937e1f30b2a10822680.tar.gz |
Merge remote-tracking branch 'qatar/master'
* qatar/master:
http: Remove the custom function for disabling chunked posts
rtsp: Disable chunked http post through AVOptions
movdec: Set frame_size for AMR
h264_weight: remove duplication functions.
swscale: align vertical filtersize by 2 on x86.
libavfilter: reindent.
matroskadec: empty blocks are in fact valid.
avfilter: don't abort() on zero-size allocations.
h264: improve calculation of codec delay.
movenc: Set a correct packet size for AMR-NB mode 15, "no data"
avformat: Add functions for doing global network initialization
avformat: Add the https protocol
avformat: Add the tls protocol, using OpenSSL or gnutls
avformat: Initialize gnutls in ff_tls_init()
w32threads: Wrap the mutex functions in inline functions returning int
configure: Allow linking to the gnutls library
avformat: Add ff_tls_init()/deinit() that initialize OpenSSL
configure: Allow linking to openssl
avcodec: Allow locking and unlocking an avformat specific mutex
avformat: Split out functions from network.h to a new file, network.c
Conflicts:
Changelog
configure
doc/APIchanges
libavcodec/internal.h
libavcodec/version.h
libavfilter/formats.c
libavformat/matroskadec.c
libavformat/mov.c
libavformat/version.h
Merged-by: Michael Niedermayer <michaelni@gmx.at>
Diffstat (limited to 'libavformat')
-rw-r--r-- | libavformat/Makefile | 4 | ||||
-rw-r--r-- | libavformat/allformats.c | 2 | ||||
-rw-r--r-- | libavformat/avformat.h | 15 | ||||
-rw-r--r-- | libavformat/http.c | 34 | ||||
-rw-r--r-- | libavformat/http.h | 8 | ||||
-rw-r--r-- | libavformat/matroskadec.c | 5 | ||||
-rw-r--r-- | libavformat/mov.c | 17 | ||||
-rw-r--r-- | libavformat/network.c | 176 | ||||
-rw-r--r-- | libavformat/network.h | 58 | ||||
-rw-r--r-- | libavformat/rtsp.c | 3 | ||||
-rw-r--r-- | libavformat/tls.c | 234 | ||||
-rw-r--r-- | libavformat/utils.c | 20 | ||||
-rw-r--r-- | libavformat/version.h | 2 |
13 files changed, 499 insertions, 79 deletions
diff --git a/libavformat/Makefile b/libavformat/Makefile index 94923265fb..264ab92611 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -16,6 +16,8 @@ OBJS = allformats.o \ seek.o \ utils.o \ +OBJS-$(CONFIG_NETWORK) += network.o + # muxers/demuxers OBJS-$(CONFIG_A64_MUXER) += a64.o OBJS-$(CONFIG_AAC_DEMUXER) += aacdec.o rawdec.o @@ -342,6 +344,7 @@ OBJS-$(CONFIG_CRYPTO_PROTOCOL) += crypto.o OBJS-$(CONFIG_FILE_PROTOCOL) += file.o OBJS-$(CONFIG_GOPHER_PROTOCOL) += gopher.o OBJS-$(CONFIG_HTTP_PROTOCOL) += http.o httpauth.o +OBJS-$(CONFIG_HTTPS_PROTOCOL) += http.o httpauth.o OBJS-$(CONFIG_MMSH_PROTOCOL) += mmsh.o mms.o asf.o OBJS-$(CONFIG_MMST_PROTOCOL) += mmst.o mms.o asf.o OBJS-$(CONFIG_MD5_PROTOCOL) += md5proto.o @@ -354,6 +357,7 @@ OBJS-$(CONFIG_RTMP_PROTOCOL) += $(RTMP-OBJS-yes) OBJS-$(CONFIG_RTP_PROTOCOL) += rtpproto.o OBJS-$(CONFIG_TCP_PROTOCOL) += tcp.o +OBJS-$(CONFIG_TLS_PROTOCOL) += tls.o OBJS-$(CONFIG_UDP_PROTOCOL) += udp.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index cc9a902c7d..ced4b0a217 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -258,6 +258,7 @@ void av_register_all(void) REGISTER_PROTOCOL (FILE, file); REGISTER_PROTOCOL (GOPHER, gopher); REGISTER_PROTOCOL (HTTP, http); + REGISTER_PROTOCOL (HTTPS, https); REGISTER_PROTOCOL (MMSH, mmsh); REGISTER_PROTOCOL (MMST, mmst); REGISTER_PROTOCOL (MD5, md5); @@ -271,5 +272,6 @@ void av_register_all(void) #endif REGISTER_PROTOCOL (RTP, rtp); REGISTER_PROTOCOL (TCP, tcp); + REGISTER_PROTOCOL (TLS, tls); REGISTER_PROTOCOL (UDP, udp); } diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 90cf3ad44d..0fcc38baf9 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -1855,4 +1855,19 @@ int avformat_query_codec(AVOutputFormat *ofmt, enum CodecID codec_id, int std_co */ const AVClass *avformat_get_class(void); +/** + * Do global initialization of network components. This is optional, + * but recommended, since it avoids the overhead of implicitly + * doing the setup for each session. + * + * Since the next major version bump, calling this function will become + * mandatory if using network protocols. + */ +int avformat_network_init(void); + +/** + * Undo the initialization done by avformat_network_init. + */ +int avformat_network_deinit(void); + #endif /* AVFORMAT_AVFORMAT_H */ diff --git a/libavformat/http.c b/libavformat/http.c index 76e5a0fb07..414eb8719f 100644 --- a/libavformat/http.c +++ b/libavformat/http.c @@ -77,11 +77,6 @@ void ff_http_set_headers(URLContext *h, const char *headers) av_strlcpy(s->headers, headers, sizeof(s->headers)); } -void ff_http_set_chunked_transfer_encoding(URLContext *h, int is_chunked) -{ - ((HTTPContext*)h->priv_data)->chunksize = is_chunked ? 0 : -1; -} - void ff_http_init_auth_state(URLContext *dest, const URLContext *src) { memcpy(&((HTTPContext*)dest->priv_data)->auth_state, @@ -91,8 +86,8 @@ void ff_http_init_auth_state(URLContext *dest, const URLContext *src) /* return non zero if error */ static int http_open_cnx(URLContext *h) { - const char *path, *proxy_path; - char hostname[1024], hoststr[1024]; + const char *path, *proxy_path, *lower_proto = "tcp"; + char hostname[1024], hoststr[1024], proto[10]; char auth[1024]; char path1[1024]; char buf[1024]; @@ -108,7 +103,8 @@ static int http_open_cnx(URLContext *h) /* fill the dest addr */ redo: /* needed in any case to build the host string */ - av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, + av_url_split(proto, sizeof(proto), auth, sizeof(auth), + hostname, sizeof(hostname), &port, path1, sizeof(path1), s->location); ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL); @@ -122,10 +118,15 @@ static int http_open_cnx(URLContext *h) else path = path1; } + if (!strcmp(proto, "https")) { + lower_proto = "tls"; + if (port < 0) + port = 443; + } if (port < 0) port = 80; - ff_url_join(buf, sizeof(buf), "tcp", NULL, hostname, port, NULL); + ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL); err = ffurl_open(&hd, buf, AVIO_FLAG_READ_WRITE); if (err < 0) goto fail; @@ -508,6 +509,7 @@ http_get_file_handle(URLContext *h) return ffurl_get_file_handle(s->hd); } +#if CONFIG_HTTP_PROTOCOL URLProtocol ff_http_protocol = { .name = "http", .url_open = http_open, @@ -519,3 +521,17 @@ URLProtocol ff_http_protocol = { .priv_data_size = sizeof(HTTPContext), .priv_data_class = &httpcontext_class, }; +#endif +#if CONFIG_HTTPS_PROTOCOL +URLProtocol ff_https_protocol = { + .name = "https", + .url_open = http_open, + .url_read = http_read, + .url_write = http_write, + .url_seek = http_seek, + .url_close = http_close, + .url_get_file_handle = http_get_file_handle, + .priv_data_size = sizeof(HTTPContext), + .priv_data_class = &httpcontext_class, +}; +#endif diff --git a/libavformat/http.h b/libavformat/http.h index d01a004e9f..19c4099ef0 100644 --- a/libavformat/http.h +++ b/libavformat/http.h @@ -43,14 +43,6 @@ void ff_http_set_headers(URLContext *h, const char *headers); /** - * Enable or disable chunked transfer encoding. (default is enabled) - * - * @param h URL context for this HTTP connection - * @param is_chunked 0 to disable chunking, nonzero otherwise. - */ -void ff_http_set_chunked_transfer_encoding(URLContext *h, int is_chunked); - -/** * Initialize the authentication state based on another HTTP URLContext. * This can be used to pre-initialize the authentication parameters if * they are known beforehand, to avoid having to do an initial failing diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index abedcd7bb8..b47f4de8d4 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -1750,11 +1750,12 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, size -= n; track = matroska_find_track_by_num(matroska, num); - if (size <= 3 || !track || !track->stream) { + if (!track || !track->stream) { av_log(matroska->ctx, AV_LOG_INFO, "Invalid stream %"PRIu64" or size %u\n", num, size); return res; - } + } else if (size <= 3) + return 0; st = track->stream; if (st->discard >= AVDISCARD_ALL) return res; diff --git a/libavformat/mov.c b/libavformat/mov.c index c90004cbae..e6efe27195 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -1346,17 +1346,16 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) st->codec->channels= 1; /* really needed */ break; case CODEC_ID_AMR_NB: - case CODEC_ID_AMR_WB: - st->codec->frame_size= sc->samples_per_frame; st->codec->channels= 1; /* really needed */ /* force sample rate for amr, stsd in 3gp does not store sample rate */ - if (st->codec->codec_id == CODEC_ID_AMR_NB) { - st->codec->sample_rate = 8000; - st->codec->frame_size = 160; - } else if (st->codec->codec_id == CODEC_ID_AMR_WB) { - st->codec->sample_rate = 16000; - st->codec->frame_size = 320; - } + st->codec->sample_rate = 8000; + /* force frame_size, too, samples_per_frame isn't always set properly */ + st->codec->frame_size = 160; + break; + case CODEC_ID_AMR_WB: + st->codec->channels = 1; + st->codec->sample_rate = 16000; + st->codec->frame_size = 320; break; case CODEC_ID_MP2: case CODEC_ID_MP3: diff --git a/libavformat/network.c b/libavformat/network.c new file mode 100644 index 0000000000..33435cf524 --- /dev/null +++ b/libavformat/network.c @@ -0,0 +1,176 @@ +/* + * Copyright (c) 2007 The Libav Project + * + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/avutil.h" +#include "network.h" +#include "libavcodec/internal.h" + +#define THREADS (HAVE_PTHREADS || (defined(WIN32) && !defined(__MINGW32CE__))) + +#if THREADS +#if HAVE_PTHREADS +#include <pthread.h> +#else +#include "libavcodec/w32pthreads.h" +#endif +#endif + +#if CONFIG_OPENSSL +#include <openssl/ssl.h> +static int openssl_init; +#if THREADS +#include <openssl/crypto.h> +#include "libavutil/avutil.h" +pthread_mutex_t *openssl_mutexes; +static void openssl_lock(int mode, int type, const char *file, int line) +{ + if (mode & CRYPTO_LOCK) + pthread_mutex_lock(&openssl_mutexes[type]); + else + pthread_mutex_unlock(&openssl_mutexes[type]); +} +#ifndef WIN32 +static unsigned long openssl_thread_id(void) +{ + return (intptr_t) pthread_self(); +} +#endif +#endif +#endif +#if CONFIG_GNUTLS +#include <gnutls/gnutls.h> +#if THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00 +#include <gcrypt.h> +#include <errno.h> +#undef malloc +#undef free +GCRY_THREAD_OPTION_PTHREAD_IMPL; +#endif +#endif + +void ff_tls_init(void) +{ + avpriv_lock_avformat(); +#if CONFIG_OPENSSL + if (!openssl_init) { + SSL_library_init(); + SSL_load_error_strings(); +#if THREADS + if (!CRYPTO_get_locking_callback()) { + int i; + openssl_mutexes = av_malloc(sizeof(pthread_mutex_t) * CRYPTO_num_locks()); + for (i = 0; i < CRYPTO_num_locks(); i++) + pthread_mutex_init(&openssl_mutexes[i], NULL); + CRYPTO_set_locking_callback(openssl_lock); +#ifndef WIN32 + CRYPTO_set_id_callback(openssl_thread_id); +#endif + } +#endif + } + openssl_init++; +#endif +#if CONFIG_GNUTLS +#if THREADS && GNUTLS_VERSION_NUMBER < 0x020b00 + if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0) + gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); +#endif + gnutls_global_init(); +#endif + avpriv_unlock_avformat(); +} + +void ff_tls_deinit(void) +{ + avpriv_lock_avformat(); +#if CONFIG_OPENSSL + openssl_init--; + if (!openssl_init) { +#if THREADS + if (CRYPTO_get_locking_callback() == openssl_lock) { + int i; + CRYPTO_set_locking_callback(NULL); + for (i = 0; i < CRYPTO_num_locks(); i++) + pthread_mutex_destroy(&openssl_mutexes[i]); + av_free(openssl_mutexes); + } +#endif + } +#endif +#if CONFIG_GNUTLS + gnutls_global_deinit(); +#endif + avpriv_unlock_avformat(); +} + +int ff_network_init(void) +{ +#if HAVE_WINSOCK2_H + WSADATA wsaData; + if (WSAStartup(MAKEWORD(1,1), &wsaData)) + return 0; +#endif + return 1; +} + +int ff_network_wait_fd(int fd, int write) +{ + int ev = write ? POLLOUT : POLLIN; + struct pollfd p = { .fd = fd, .events = ev, .revents = 0 }; + int ret; + ret = poll(&p, 1, 100); + return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN); +} + +void ff_network_close(void) +{ +#if HAVE_WINSOCK2_H + WSACleanup(); +#endif +} + +#if HAVE_WINSOCK2_H +int ff_neterrno(void) +{ + int err = WSAGetLastError(); + switch (err) { + case WSAEWOULDBLOCK: + return AVERROR(EAGAIN); + case WSAEINTR: + return AVERROR(EINTR); + } + return -err; +} +#endif + +int ff_is_multicast_address(struct sockaddr *addr) +{ + if (addr->sa_family == AF_INET) { + return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr)); + } +#if HAVE_STRUCT_SOCKADDR_IN6 + if (addr->sa_family == AF_INET6) { + return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr); + } +#endif + + return 0; +} + diff --git a/libavformat/network.h b/libavformat/network.h index f7e19b196e..cfbbd83ab2 100644 --- a/libavformat/network.h +++ b/libavformat/network.h @@ -36,17 +36,7 @@ #define ECONNREFUSED WSAECONNREFUSED #define EINPROGRESS WSAEINPROGRESS -static inline int ff_neterrno(void) -{ - int err = WSAGetLastError(); - switch (err) { - case WSAEWOULDBLOCK: - return AVERROR(EAGAIN); - case WSAEINTR: - return AVERROR(EINTR); - } - return -err; -} +int ff_neterrno(void); #else #include <sys/types.h> #include <sys/socket.h> @@ -66,31 +56,13 @@ static inline int ff_neterrno(void) int ff_socket_nonblock(int socket, int enable); -static inline int ff_network_init(void) -{ -#if HAVE_WINSOCK2_H - WSADATA wsaData; - if (WSAStartup(MAKEWORD(1,1), &wsaData)) - return 0; -#endif - return 1; -} - -static inline int ff_network_wait_fd(int fd, int write) -{ - int ev = write ? POLLOUT : POLLIN; - struct pollfd p = { .fd = fd, .events = ev, .revents = 0 }; - int ret; - ret = poll(&p, 1, 100); - return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN); -} - -static inline void ff_network_close(void) -{ -#if HAVE_WINSOCK2_H - WSACleanup(); -#endif -} +int ff_network_init(void); +void ff_network_close(void); + +void ff_tls_init(void); +void ff_tls_deinit(void); + +int ff_network_wait_fd(int fd, int write); int ff_inet_aton (const char * str, struct in_addr * add); @@ -191,18 +163,6 @@ const char *ff_gai_strerror(int ecode); #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff) #endif -static inline int ff_is_multicast_address(struct sockaddr *addr) -{ - if (addr->sa_family == AF_INET) { - return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr)); - } -#if HAVE_STRUCT_SOCKADDR_IN6 - if (addr->sa_family == AF_INET6) { - return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr); - } -#endif - - return 0; -} +int ff_is_multicast_address(struct sockaddr *addr); #endif /* AVFORMAT_NETWORK_H */ diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c index 5966e813b7..b23100df62 100644 --- a/libavformat/rtsp.c +++ b/libavformat/rtsp.c @@ -26,6 +26,7 @@ #include "libavutil/parseutils.h" #include "libavutil/random_seed.h" #include "libavutil/dict.h" +#include "libavutil/opt.h" #include "avformat.h" #include "avio_internal.h" @@ -1482,7 +1483,7 @@ redirect: "Expires: Sun, 9 Jan 1972 00:00:00 GMT\r\n", sessioncookie); ff_http_set_headers(rt->rtsp_hd_out, headers); - ff_http_set_chunked_transfer_encoding(rt->rtsp_hd_out, 0); + av_opt_set(rt->rtsp_hd_out->priv_data, "chunksize", "-1", 0); /* Initialize the authentication state for the POST session. The HTTP * protocol implementation doesn't properly handle multi-pass diff --git a/libavformat/tls.c b/libavformat/tls.c new file mode 100644 index 0000000000..85bf46f903 --- /dev/null +++ b/libavformat/tls.c @@ -0,0 +1,234 @@ +/* + * TLS/SSL Protocol + * Copyright (c) 2011 Martin Storsjo + * + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "avformat.h" +#include "url.h" +#include "libavutil/avstring.h" +#if CONFIG_GNUTLS +#include <gnutls/gnutls.h> +#define TLS_read(c, buf, size) gnutls_record_recv(c->session, buf, size) +#define TLS_write(c, buf, size) gnutls_record_send(c->session, buf, size) +#define TLS_shutdown(c) gnutls_bye(c->session, GNUTLS_SHUT_RDWR) +#define TLS_free(c) do { \ + if (c->session) \ + gnutls_deinit(c->session); \ + if (c->cred) \ + gnutls_certificate_free_credentials(c->cred); \ + } while (0) +#elif CONFIG_OPENSSL +#include <openssl/bio.h> +#include <openssl/ssl.h> +#include <openssl/err.h> +#define TLS_read(c, buf, size) SSL_read(c->ssl, buf, size) +#define TLS_write(c, buf, size) SSL_write(c->ssl, buf, size) +#define TLS_shutdown(c) SSL_shutdown(c->ssl) +#define TLS_free(c) do { \ + if (c->ssl) \ + SSL_free(c->ssl); \ + if (c->ctx) \ + SSL_CTX_free(c->ctx); \ + } while (0) +#endif +#include "network.h" +#include "os_support.h" +#include "internal.h" +#if HAVE_POLL_H +#include <poll.h> +#endif + +typedef struct { + const AVClass *class; + URLContext *tcp; +#if CONFIG_GNUTLS + gnutls_session_t session; + gnutls_certificate_credentials_t cred; +#elif CONFIG_OPENSSL + SSL_CTX *ctx; + SSL *ssl; +#endif + int fd; +} TLSContext; + +static int do_tls_poll(URLContext *h, int ret) +{ + TLSContext *c = h->priv_data; + struct pollfd p = { c->fd, 0, 0 }; +#if CONFIG_GNUTLS + if (ret != GNUTLS_E_AGAIN && ret != GNUTLS_E_INTERRUPTED) { + av_log(NULL, AV_LOG_ERROR, "%s\n", gnutls_strerror(ret)); + return AVERROR(EIO); + } + if (gnutls_record_get_direction(c->session)) + p.events = POLLOUT; + else + p.events = POLLIN; +#elif CONFIG_OPENSSL + ret = SSL_get_error(c->ssl, ret); + if (ret == SSL_ERROR_WANT_READ) { + p.events = POLLIN; + } else if (ret == SSL_ERROR_WANT_WRITE) { + p.events = POLLOUT; + } else { + av_log(NULL, AV_LOG_ERROR, "%s\n", ERR_error_string(ret, NULL)); + return AVERROR(EIO); + } +#endif + if (h->flags & URL_FLAG_NONBLOCK) + return AVERROR(EAGAIN); + while (1) { + int n = poll(&p, 1, 100); + if (n > 0) + break; + if (url_interrupt_cb()) + return AVERROR(EINTR); + } + return 0; +} + +static int tls_open(URLContext *h, const char *uri, int flags) +{ + TLSContext *c = h->priv_data; + int ret; + int port; + char buf[200], host[200]; + int numerichost = 0; + struct addrinfo hints = { 0 }, *ai = NULL; + + ff_tls_init(); + + av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0, uri); + ff_url_join(buf, sizeof(buf), "tcp", NULL, host, port, NULL); + + hints.ai_flags = AI_NUMERICHOST; + if (!getaddrinfo(host, NULL, &hints, &ai)) { + numerichost = 1; + freeaddrinfo(ai); + } + + ret = ffurl_open(&c->tcp, buf, AVIO_FLAG_READ_WRITE); + if (ret) + goto fail; + c->fd = ffurl_get_file_handle(c->tcp); + +#if CONFIG_GNUTLS + gnutls_init(&c->session, GNUTLS_CLIENT); + if (!numerichost) + gnutls_server_name_set(c->session, GNUTLS_NAME_DNS, host, strlen(host)); + gnutls_certificate_allocate_credentials(&c->cred); + gnutls_certificate_set_verify_flags(c->cred, 0); + gnutls_credentials_set(c->session, GNUTLS_CRD_CERTIFICATE, c->cred); + gnutls_transport_set_ptr(c->session, (gnutls_transport_ptr_t) + (intptr_t) c->fd); + gnutls_priority_set_direct(c->session, "NORMAL", NULL); + while (1) { + ret = gnutls_handshake(c->session); + if (ret == 0) + break; + if ((ret = do_tls_poll(h, ret)) < 0) + goto fail; + } +#elif CONFIG_OPENSSL + c->ctx = SSL_CTX_new(SSLv3_client_method()); + if (!c->ctx) { + av_log(NULL, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL)); + ret = AVERROR(EIO); + goto fail; + } + c->ssl = SSL_new(c->ctx); + if (!c->ssl) { + av_log(NULL, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL)); + ret = AVERROR(EIO); + goto fail; + } + SSL_set_fd(c->ssl, c->fd); + if (!numerichost) + SSL_set_tlsext_host_name(c->ssl, host); + while (1) { + ret = SSL_connect(c->ssl); + if (ret > 0) + break; + if (ret == 0) { + av_log(NULL, AV_LOG_ERROR, "Unable to negotiate TLS/SSL session\n"); + ret = AVERROR(EIO); + goto fail; + } + if ((ret = do_tls_poll(h, ret)) < 0) + goto fail; + } +#endif + return 0; +fail: + TLS_free(c); + if (c->tcp) + ffurl_close(c->tcp); + ff_tls_deinit(); + return ret; +} + +static int tls_read(URLContext *h, uint8_t *buf, int size) +{ + TLSContext *c = h->priv_data; + while (1) { + int ret = TLS_read(c, buf, size); + if (ret > 0) + return ret; + if (ret == 0) + return AVERROR(EIO); + if ((ret = do_tls_poll(h, ret)) < 0) + return ret; + } + return 0; +} + +static int tls_write(URLContext *h, const uint8_t *buf, int size) +{ + TLSContext *c = h->priv_data; + while (1) { + int ret = TLS_write(c, buf, size); + if (ret > 0) + return ret; + if (ret == 0) + return AVERROR(EIO); + if ((ret = do_tls_poll(h, ret)) < 0) + return ret; + } + return 0; +} + +static int tls_close(URLContext *h) +{ + TLSContext *c = h->priv_data; + TLS_shutdown(c); + TLS_free(c); + ffurl_close(c->tcp); + ff_tls_deinit(); + return 0; +} + +URLProtocol ff_tls_protocol = { + .name = "tls", + .url_open = tls_open, + .url_read = tls_read, + .url_write = tls_write, + .url_seek = NULL, + .url_close = tls_close, + .priv_data_size = sizeof(TLSContext), +}; diff --git a/libavformat/utils.c b/libavformat/utils.c index 43991765df..f38e084442 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -4186,3 +4186,23 @@ int avformat_query_codec(AVOutputFormat *ofmt, enum CodecID codec_id, int std_co } return AVERROR_PATCHWELCOME; } + +int avformat_network_init(void) +{ +#if CONFIG_NETWORK + int ret; + if ((ret = ff_network_init()) < 0) + return ret; + ff_tls_init(); +#endif + return 0; +} + +int avformat_network_deinit(void) +{ +#if CONFIG_NETWORK + ff_network_close(); + ff_tls_deinit(); +#endif + return 0; +} diff --git a/libavformat/version.h b/libavformat/version.h index c3c8d2b19d..bc427d3f4e 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -24,7 +24,7 @@ #include "libavutil/avutil.h" #define LIBAVFORMAT_VERSION_MAJOR 53 -#define LIBAVFORMAT_VERSION_MINOR 18 +#define LIBAVFORMAT_VERSION_MINOR 19 #define LIBAVFORMAT_VERSION_MICRO 0 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ |