FFmpeg
unix.c
Go to the documentation of this file.
1 /*
2  * Unix socket protocol
3  * Copyright (c) 2013 Luca Barbato
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  *
25  * Unix socket url_protocol
26  */
27 
28 #include "libavutil/avstring.h"
29 #include "libavutil/opt.h"
30 #include "os_support.h"
31 #include "network.h"
32 #include <sys/un.h>
33 #include "url.h"
34 
35 typedef struct UnixContext {
36  const AVClass *class;
37  struct sockaddr_un addr;
38  int timeout;
39  int listen;
40  int type;
41  int fd;
42  int pkt_size;
43 } UnixContext;
44 
45 #define OFFSET(x) offsetof(UnixContext, x)
46 #define ED AV_OPT_FLAG_DECODING_PARAM|AV_OPT_FLAG_ENCODING_PARAM
47 static const AVOption unix_options[] = {
48  { "listen", "Open socket for listening", OFFSET(listen), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ED },
49  { "timeout", "Timeout in ms", OFFSET(timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, ED },
50  { "type", "Socket type", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = SOCK_STREAM }, INT_MIN, INT_MAX, ED, .unit = "type" },
51  { "stream", "Stream (reliable stream-oriented)", 0, AV_OPT_TYPE_CONST, { .i64 = SOCK_STREAM }, INT_MIN, INT_MAX, ED, .unit = "type" },
52  { "datagram", "Datagram (unreliable packet-oriented)", 0, AV_OPT_TYPE_CONST, { .i64 = SOCK_DGRAM }, INT_MIN, INT_MAX, ED, .unit = "type" },
53  { "seqpacket", "Seqpacket (reliable packet-oriented", 0, AV_OPT_TYPE_CONST, { .i64 = SOCK_SEQPACKET }, INT_MIN, INT_MAX, ED, .unit = "type" },
54  { "pkt_size", "Maximum packet size", OFFSET(pkt_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, ED },
55  { NULL }
56 };
57 
58 static const AVClass unix_class = {
59  .class_name = "unix",
60  .item_name = av_default_item_name,
61  .option = unix_options,
62  .version = LIBAVUTIL_VERSION_INT,
63 };
64 
65 static int unix_open(URLContext *h, const char *filename, int flags)
66 {
67  UnixContext *s = h->priv_data;
68  int fd, ret;
69 
70  av_strstart(filename, "unix:", &filename);
71  s->addr.sun_family = AF_UNIX;
72  av_strlcpy(s->addr.sun_path, filename, sizeof(s->addr.sun_path));
73 
74  if ((fd = ff_socket(AF_UNIX, s->type, 0, h)) < 0)
75  return ff_neterrno();
76 
77  if (s->timeout < 0 && h->rw_timeout)
78  s->timeout = h->rw_timeout / 1000;
79 
80  if (s->listen) {
81  ret = ff_listen_bind(fd, (struct sockaddr *)&s->addr,
82  sizeof(s->addr), s->timeout, h);
83  if (ret < 0)
84  goto fail;
85  fd = ret;
86  } else {
87  ret = ff_listen_connect(fd, (struct sockaddr *)&s->addr,
88  sizeof(s->addr), s->timeout, h, 0);
89  if (ret < 0)
90  goto fail;
91  }
92 
93  s->fd = fd;
94  h->is_streamed = 1;
95 
96  if ((s->type == SOCK_DGRAM || s->type == SOCK_SEQPACKET) && s->pkt_size > 0)
97  h->max_packet_size = s->pkt_size;
98 
99  return 0;
100 
101 fail:
102  if (s->listen && AVUNERROR(ret) != EADDRINUSE)
103  unlink(s->addr.sun_path);
104  if (fd >= 0)
105  closesocket(fd);
106  return ret;
107 }
108 
109 static int unix_read(URLContext *h, uint8_t *buf, int size)
110 {
111  UnixContext *s = h->priv_data;
112  int ret;
113 
114  if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
115  ret = ff_network_wait_fd(s->fd, 0);
116  if (ret < 0)
117  return ret;
118  }
119  ret = recv(s->fd, buf, size, 0);
120  if (!ret && s->type == SOCK_STREAM)
121  return AVERROR_EOF;
122  return ret < 0 ? ff_neterrno() : ret;
123 }
124 
125 static int unix_write(URLContext *h, const uint8_t *buf, int size)
126 {
127  UnixContext *s = h->priv_data;
128  int ret;
129 
130  if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
131  ret = ff_network_wait_fd(s->fd, 1);
132  if (ret < 0)
133  return ret;
134  }
135  ret = send(s->fd, buf, size, MSG_NOSIGNAL);
136  return ret < 0 ? ff_neterrno() : ret;
137 }
138 
139 static int unix_close(URLContext *h)
140 {
141  UnixContext *s = h->priv_data;
142  if (s->listen)
143  unlink(s->addr.sun_path);
144  closesocket(s->fd);
145  return 0;
146 }
147 
149 {
150  UnixContext *s = h->priv_data;
151  return s->fd;
152 }
153 
155  .name = "unix",
156  .url_open = unix_open,
157  .url_read = unix_read,
158  .url_write = unix_write,
159  .url_close = unix_close,
160  .url_get_file_handle = unix_get_file_handle,
161  .priv_data_size = sizeof(UnixContext),
162  .priv_data_class = &unix_class,
164 };
flags
const SwsFlags flags[]
Definition: swscale.c:61
UnixContext::fd
int fd
Definition: unix.c:41
UnixContext::timeout
int timeout
Definition: unix.c:38
opt.h
URL_PROTOCOL_FLAG_NETWORK
#define URL_PROTOCOL_FLAG_NETWORK
Definition: url.h:33
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
ED
#define ED
Definition: unix.c:46
AVOption
AVOption.
Definition: opt.h:429
unix_close
static int unix_close(URLContext *h)
Definition: unix.c:139
UnixContext::listen
int listen
Definition: unix.c:39
URLProtocol
Definition: url.h:51
os_support.h
AVUNERROR
#define AVUNERROR(e)
Definition: error.h:46
fail
#define fail()
Definition: checkasm.h:214
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
ff_listen_bind
int ff_listen_bind(int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h)
Bind to a file descriptor and poll for a connection.
Definition: network.c:243
unix_class
static const AVClass unix_class
Definition: unix.c:58
s
#define s(width, name)
Definition: cbs_vp9.c:198
unix_options
static const AVOption unix_options[]
Definition: unix.c:47
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
NULL
#define NULL
Definition: coverity.c:32
OFFSET
#define OFFSET(x)
Definition: unix.c:45
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:241
ff_listen_connect
int ff_listen_connect(int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h, int will_try_next)
Connect to a file descriptor and poll for result.
Definition: network.c:255
ff_neterrno
#define ff_neterrno()
Definition: network.h:68
unix_get_file_handle
static int unix_get_file_handle(URLContext *h)
Definition: unix.c:148
unix_open
static int unix_open(URLContext *h, const char *filename, int flags)
Definition: unix.c:65
ff_unix_protocol
const URLProtocol ff_unix_protocol
Definition: unix.c:154
size
int size
Definition: twinvq_data.h:10344
URLProtocol::name
const char * name
Definition: url.h:52
av_strstart
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:36
UnixContext
Definition: unix.c:35
URLContext
Definition: url.h:35
UnixContext::type
int type
Definition: unix.c:40
UnixContext::addr
struct sockaddr_un addr
Definition: unix.c:37
UnixContext::pkt_size
int pkt_size
Definition: unix.c:42
url.h
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:81
network.h
unix_write
static int unix_write(URLContext *h, const uint8_t *buf, int size)
Definition: unix.c:125
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
MSG_NOSIGNAL
#define MSG_NOSIGNAL
Definition: network.h:133
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:327
AVIO_FLAG_NONBLOCK
#define AVIO_FLAG_NONBLOCK
Use non-blocking mode.
Definition: avio.h:636
av_strlcpy
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:85
h
h
Definition: vp9dsp_template.c:2070
ff_socket
int ff_socket(int af, int type, int proto, void *logctx)
Definition: network.c:180
avstring.h
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
unix_read
static int unix_read(URLContext *h, uint8_t *buf, int size)
Definition: unix.c:109
ff_network_wait_fd
int ff_network_wait_fd(int fd, int write)
Definition: network.c:66