FFmpeg
Loading...
Searching...
No Matches
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
35typedef struct UnixContext {
36 const AVClass *class;
37 struct sockaddr_un addr;
39 int listen;
40 int type;
41 int fd;
44
45#define OFFSET(x) offsetof(UnixContext, x)
46#define ED AV_OPT_FLAG_DECODING_PARAM|AV_OPT_FLAG_ENCODING_PARAM
47static 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
58static 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
65static 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 if (s->type == SOCK_DGRAM) {
82 ret = bind(fd, (struct sockaddr *)&s->addr, sizeof(s->addr));
83 if (ret) {
84 ret = ff_neterrno();
85 goto fail;
86 }
87 } else {
88 ret = ff_listen_bind(fd, (struct sockaddr *)&s->addr,
89 sizeof(s->addr), s->timeout, h);
90 if (ret < 0)
91 goto fail;
92 fd = ret;
93 }
94 } else {
95 ret = ff_listen_connect(fd, (struct sockaddr *)&s->addr,
96 sizeof(s->addr), s->timeout, h, 0);
97 if (ret < 0)
98 goto fail;
99 }
100
101 s->fd = fd;
102 h->is_streamed = 1;
103
104 if ((s->type == SOCK_DGRAM || s->type == SOCK_SEQPACKET) && s->pkt_size > 0)
105 h->max_packet_size = s->pkt_size;
106
107 return 0;
108
109fail:
110 if (s->listen && AVUNERROR(ret) != EADDRINUSE)
111 unlink(s->addr.sun_path);
112 if (fd >= 0)
113 closesocket(fd);
114 return ret;
115}
116
117static int unix_read(URLContext *h, uint8_t *buf, int size)
118{
119 UnixContext *s = h->priv_data;
120 int ret;
121
122 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
123 ret = ff_network_wait_fd(s->fd, 0);
124 if (ret < 0)
125 return ret;
126 }
127 ret = recv(s->fd, buf, size, 0);
128 if (!ret && s->type == SOCK_STREAM)
129 return AVERROR_EOF;
130 return ret < 0 ? ff_neterrno() : ret;
131}
132
133static int unix_write(URLContext *h, const uint8_t *buf, int size)
134{
135 UnixContext *s = h->priv_data;
136 int ret;
137
138 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
139 ret = ff_network_wait_fd(s->fd, 1);
140 if (ret < 0)
141 return ret;
142 }
143 ret = send(s->fd, buf, size, MSG_NOSIGNAL);
144 return ret < 0 ? ff_neterrno() : ret;
145}
146
148{
149 UnixContext *s = h->priv_data;
150 if (s->listen)
151 unlink(s->addr.sun_path);
152 closesocket(s->fd);
153 return 0;
154}
155
157{
158 UnixContext *s = h->priv_data;
159 return s->fd;
160}
161
163 .name = "unix",
164 .url_open = unix_open,
165 .url_read = unix_read,
166 .url_write = unix_write,
167 .url_close = unix_close,
168 .url_get_file_handle = unix_get_file_handle,
169 .priv_data_size = sizeof(UnixContext),
170 .priv_data_class = &unix_class,
172};
#define AVIO_FLAG_NONBLOCK
Use non-blocking mode.
Definition avio.h:636
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
#define fail
Definition test.h:479
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#define AVUNERROR(e)
Definition error.h:46
#define AVERROR_EOF
End of file.
Definition error.h:57
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
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
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
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
cl_device_type type
int ff_socket(int af, int type, int proto, void *logctx)
Definition network.c:188
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:263
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:251
int ff_network_wait_fd(int fd, int write)
Definition network.c:74
#define MSG_NOSIGNAL
Definition network.h:133
#define ff_neterrno()
Definition network.h:68
AVOptions.
miscellaneous OS support macros and functions.
const URLProtocol ff_unix_protocol
Definition unix.c:162
Describe the class of an AVClass context structure.
Definition log.h:76
AVOption.
Definition opt.h:428
int listen
Definition unix.c:39
int fd
Definition unix.c:41
int timeout
Definition unix.c:38
int pkt_size
Definition unix.c:42
int type
Definition unix.c:40
struct sockaddr_un addr
Definition unix.c:37
int size
static int unix_get_file_handle(URLContext *h)
Definition unix.c:156
#define ED
Definition unix.c:46
static int unix_open(URLContext *h, const char *filename, int flags)
Definition unix.c:65
static int unix_close(URLContext *h)
Definition unix.c:147
static int unix_write(URLContext *h, const uint8_t *buf, int size)
Definition unix.c:133
static const AVClass unix_class
Definition unix.c:58
static int unix_read(URLContext *h, uint8_t *buf, int size)
Definition unix.c:117
#define OFFSET(x)
Definition unix.c:45
static const AVOption unix_options[]
Definition unix.c:47
unbuffered private I/O API
#define URL_PROTOCOL_FLAG_NETWORK
Definition url.h:33