network.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 The Libav Project
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/avutil.h"
22 #include "network.h"
23 #include "libavcodec/internal.h"
24 #include "libavutil/mem.h"
25 #include "url.h"
26 #include "libavutil/time.h"
27 
28 #if HAVE_THREADS
29 #if HAVE_PTHREADS
30 #include <pthread.h>
31 #elif HAVE_OS2THREADS
32 #include "libavcodec/os2threads.h"
33 #else
34 #include "libavcodec/w32pthreads.h"
35 #endif
36 #endif
37 
38 #if CONFIG_OPENSSL
39 #include <openssl/ssl.h>
40 static int openssl_init;
41 #if HAVE_THREADS
42 #include <openssl/crypto.h>
43 #include "libavutil/avutil.h"
44 pthread_mutex_t *openssl_mutexes;
45 static void openssl_lock(int mode, int type, const char *file, int line)
46 {
47  if (mode & CRYPTO_LOCK)
48  pthread_mutex_lock(&openssl_mutexes[type]);
49  else
50  pthread_mutex_unlock(&openssl_mutexes[type]);
51 }
52 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
53 static unsigned long openssl_thread_id(void)
54 {
55  return (intptr_t) pthread_self();
56 }
57 #endif
58 #endif
59 #endif
60 #if CONFIG_GNUTLS
61 #include <gnutls/gnutls.h>
62 #if HAVE_THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
63 #include <gcrypt.h>
64 #include <errno.h>
65 GCRY_THREAD_OPTION_PTHREAD_IMPL;
66 #endif
67 #endif
68 
69 void ff_tls_init(void)
70 {
72 #if CONFIG_OPENSSL
73  if (!openssl_init) {
74  SSL_library_init();
75  SSL_load_error_strings();
76 #if HAVE_THREADS
77  if (!CRYPTO_get_locking_callback()) {
78  int i;
79  openssl_mutexes = av_malloc(sizeof(pthread_mutex_t) * CRYPTO_num_locks());
80  for (i = 0; i < CRYPTO_num_locks(); i++)
81  pthread_mutex_init(&openssl_mutexes[i], NULL);
82  CRYPTO_set_locking_callback(openssl_lock);
83 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
84  CRYPTO_set_id_callback(openssl_thread_id);
85 #endif
86  }
87 #endif
88  }
89  openssl_init++;
90 #endif
91 #if CONFIG_GNUTLS
92 #if HAVE_THREADS && GNUTLS_VERSION_NUMBER < 0x020b00
93  if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
94  gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
95 #endif
96  gnutls_global_init();
97 #endif
99 }
100 
101 void ff_tls_deinit(void)
102 {
104 #if CONFIG_OPENSSL
105  openssl_init--;
106  if (!openssl_init) {
107 #if HAVE_THREADS
108  if (CRYPTO_get_locking_callback() == openssl_lock) {
109  int i;
110  CRYPTO_set_locking_callback(NULL);
111  for (i = 0; i < CRYPTO_num_locks(); i++)
112  pthread_mutex_destroy(&openssl_mutexes[i]);
113  av_free(openssl_mutexes);
114  }
115 #endif
116  }
117 #endif
118 #if CONFIG_GNUTLS
119  gnutls_global_deinit();
120 #endif
122 }
123 
125 
127 {
128 #if HAVE_WINSOCK2_H
129  WSADATA wsaData;
130 #endif
131 
132  if (!ff_network_inited_globally)
133  av_log(NULL, AV_LOG_WARNING, "Using network protocols without global "
134  "network initialization. Please use "
135  "avformat_network_init(), this will "
136  "become mandatory later.\n");
137 #if HAVE_WINSOCK2_H
138  if (WSAStartup(MAKEWORD(1,1), &wsaData))
139  return 0;
140 #endif
141  return 1;
142 }
143 
144 int ff_network_wait_fd(int fd, int write)
145 {
146  int ev = write ? POLLOUT : POLLIN;
147  struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
148  int ret;
149  ret = poll(&p, 1, 100);
150  return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
151 }
152 
153 int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
154 {
155  int ret;
156  int64_t wait_start = 0;
157 
158  while (1) {
159  ret = ff_network_wait_fd(fd, write);
160  if (ret != AVERROR(EAGAIN))
161  return ret;
162  if (ff_check_interrupt(int_cb))
163  return AVERROR_EXIT;
164  if (timeout) {
165  if (!wait_start)
166  wait_start = av_gettime();
167  else if (av_gettime() - wait_start > timeout)
168  return AVERROR(ETIMEDOUT);
169  }
170  }
171 }
172 
174 {
175 #if HAVE_WINSOCK2_H
176  WSACleanup();
177 #endif
178 }
179 
180 #if HAVE_WINSOCK2_H
181 int ff_neterrno(void)
182 {
183  int err = WSAGetLastError();
184  switch (err) {
185  case WSAEWOULDBLOCK:
186  return AVERROR(EAGAIN);
187  case WSAEINTR:
188  return AVERROR(EINTR);
189  case WSAEPROTONOSUPPORT:
190  return AVERROR(EPROTONOSUPPORT);
191  case WSAETIMEDOUT:
192  return AVERROR(ETIMEDOUT);
193  case WSAECONNREFUSED:
194  return AVERROR(ECONNREFUSED);
195  case WSAEINPROGRESS:
196  return AVERROR(EINPROGRESS);
197  }
198  return -err;
199 }
200 #endif
201 
202 int ff_is_multicast_address(struct sockaddr *addr)
203 {
204  if (addr->sa_family == AF_INET) {
205  return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
206  }
207 #if HAVE_STRUCT_SOCKADDR_IN6
208  if (addr->sa_family == AF_INET6) {
209  return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
210  }
211 #endif
212 
213  return 0;
214 }
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: os2threads.h:90
int avpriv_unlock_avformat(void)
memory handling functions
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
os2threads to pthreads wrapper
external API header
void ff_network_close(void)
Definition: network.c:173
int ff_network_inited_globally
Definition: network.c:124
#define IN6_IS_ADDR_MULTICAST(a)
Definition: network.h:220
HMTX pthread_mutex_t
Definition: os2threads.h:38
int ff_network_init(void)
Definition: network.c:126
mode
Definition: f_perms.c:27
Callback for checking whether to abort blocking functions.
Definition: avio.h:51
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
const AVIOInterruptCB int_cb
Definition: ffmpeg.c:422
Definition: graph2dot.c:48
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
int avpriv_lock_avformat(void)
int ff_is_multicast_address(struct sockaddr *addr)
Definition: network.c:202
int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
This works similarly to ff_network_wait_fd, but waits up to &#39;timeout&#39; microseconds Uses ff_network_wa...
Definition: network.c:153
ret
Definition: avfilter.c:821
#define ff_neterrno()
Definition: network.h:63
FFmpeg Automated Testing Environment ************************************Table of Contents *****************FFmpeg Automated Testing Environment Introduction Using FATE from your FFmpeg source directory Submitting the results to the FFmpeg result aggregation server FATE makefile targets and variables Makefile targets Makefile variables Examples Introduction **************FATE is an extended regression suite on the client side and a means for results aggregation and presentation on the server side The first part of this document explains how you can use FATE from your FFmpeg source directory to test your ffmpeg binary The second part describes how you can run FATE to submit the results to FFmpeg s FATE server In any way you can have a look at the publicly viewable FATE results by visiting this as it can be seen if some test on some platform broke with their recent contribution This usually happens on the platforms the developers could not test on The second part of this document describes how you can run FATE to submit your results to FFmpeg s FATE server If you want to submit your results be sure to check that your combination of OS and compiler is not already listed on the above mentioned website In the third part you can find a comprehensive listing of FATE makefile targets and variables Using FATE from your FFmpeg source directory **********************************************If you want to run FATE on your machine you need to have the samples in place You can get the samples via the build target fate rsync Use this command from the top level source this will cause FATE to fail NOTE To use a custom wrapper to run the pass target exec to configure or set the TARGET_EXEC Make variable Submitting the results to the FFmpeg result aggregation server ****************************************************************To submit your results to the server you should run fate through the shell script tests fate sh from the FFmpeg sources This script needs to be invoked with a configuration file as its first argument tests fate sh path to fate_config A configuration file template with comments describing the individual configuration variables can be found at doc fate_config sh template Create a configuration that suits your based on the configuration template The slot configuration variable can be any string that is not yet but it is suggested that you name it adhering to the following pattern< arch >< os >< compiler >< compiler version > The configuration file itself will be sourced in a shell therefore all shell features may be used This enables you to setup the environment as you need it for your build For your first test runs the fate_recv variable should be empty or commented out This will run everything as normal except that it will omit the submission of the results to the server The following files should be present in $workdir as specified in the configuration file
Definition: fate.txt:34
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: os2threads.h:83
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
NULL
Definition: eval.c:55
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrup a blocking function associated with cb.
Definition: avio.c:428
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
#define IN_MULTICAST(a)
Definition: network.h:217
synthesis window for stochastic i
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
#define type
common internal api header.
static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
Definition: os2threads.h:104
void ff_tls_init(void)
Definition: network.c:69
w32threads to pthreads wrapper
void ff_tls_deinit(void)
Definition: network.c:101
int ff_network_wait_fd(int fd, int write)
Definition: network.c:144
unbuffered private I/O API
static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
Definition: os2threads.h:97
#define MAKEWORD(a, b)
Definition: windows2linux.h:56