+/*
+ * UDP prototype streaming system
+ * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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.
+ *
+ * FFmpeg 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file libavformat/udp.c
+ * UDP protocol
+ */
+
+#include "avformat.h"
+#include <unistd.h>
+#include "network.h"
+#include "os_support.h"
+#if HAVE_SYS_SELECT_H
+#include <sys/select.h>
+#endif
+#include <sys/time.h>
+
+#include <frsh.h>
+#include <frsh_core_types.h>
+
+long int udp_budget, udp_period;
+
+typedef struct {
+ int udp_fd;
+ int ttl;
+ int buffer_size;
+ int is_multicast;
+ int local_port;
+ int reuse_socket;
+ struct sockaddr_in dest_addr;
+ int dest_addr_len;
+
+ frsh_send_endpoint_t sepoint;
+ frsh_send_endpoint_t repoint;
+ frsh_vres_id_t vres;
+ frsh_send_endpoint_protocol_info_t send_pinfo;
+ frsh_contract_t contract;
+ frsh_contract_label_t label;
+ frsh_rel_time_t budget, period;
+} UDPContext;
+
+#define UDP_TX_BUF_SIZE 32768
+#define UDP_MAX_PKT_SIZE 65536
+
+static int udp_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr)
+{
+ return 0;
+}
+
+static int udp_join_multicast_group(int sockfd, struct sockaddr *addr)
+{
+ return 0;
+}
+
+static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr)
+{
+ return 0;
+}
+
+static int udp_set_url(struct sockaddr_in *addr, const char *hostname, int port)
+{
+ /* set the destination address */
+ if (resolve_host(&addr->sin_addr, hostname) < 0)
+ return AVERROR(EIO);
+ addr->sin_family = AF_INET;
+ addr->sin_port = htons(port);
+
+ return sizeof(struct sockaddr_in);
+}
+
+static int is_multicast_address(struct sockaddr_in *addr)
+{
+ return 0;
+}
+
+static int
+udp_socket_create(UDPContext *s, struct sockaddr_in *addr, int *addr_len)
+{
+ static long int netcont_num = 0;
+ int ret,udp_fd;
+ char netcont_name[20];
+
+ addr->sin_family = AF_INET;
+ addr->sin_addr.s_addr = htonl (INADDR_ANY);
+ addr->sin_port = htons(s->local_port);
+ *addr_len = sizeof(struct sockaddr_in);
+
+ /* set params for contract */
+ frsh_network_bytes_to_budget(FRSH_NETPF_FWP, udp_budget, &s->budget);
+ s->period = fosa_msec_to_rel_time(udp_period);
+ s->send_pinfo.body = NULL;
+
+ udp_fd = frsh_send_endpoint_create(FRSH_NETPF_FWP,
+ s->dest_addr.sin_addr.s_addr,
+ ntohs(s->dest_addr.sin_port), s->send_pinfo,
+ &s->sepoint);
+ if (udp_fd < 0) {
+
+ return -1;
+ }
+
+ /* Contract negotiation */
+ ret = frsh_contract_init(&s->contract);
+ //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_init");
+ if (ret) return -1;
+
+ ret = frsh_contract_set_basic_params(&s->contract,
+ &s->budget,
+ &s->period,
+ FRSH_WT_BOUNDED,
+ FRSH_CT_REGULAR);
+ //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_basic_params");
+ if (ret) return -1;
+ snprintf(netcont_name, sizeof(netcont_name), "ffmpeg%02d", ++netcont_num);
+ ret = frsh_contract_set_resource_and_label(&s->contract,FRSH_RT_NETWORK,
+ FRSH_NETPF_FWP,
+ s->label[0] ? s->label : netcont_name);
+ //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_resource_and_label");
+ if (ret) return -1;
+
+ ret = frsh_contract_negotiate(&s->contract, &s->vres);
+ //if (ret) PERROR_AND_EXIT(ret, "frsh_contract_negotiate");
+ if (ret) return -1;
+
+ printf("Send endpoint created\n");
+ frsh_send_endpoint_bind(s->vres, s->sepoint);
+ printf("Send endpoint bounded\n");
+
+ //fwp_endpoint_get_params(s->sepoint->protocol_info.body, &node,
+ // &port, &attr, &fd);
+
+ return udp_fd;
+}
+
+static int udp_port(struct sockaddr_in *addr, int len)
+{
+ return ntohs(addr->sin_port);
+}
+
+/**
+ * If no filename is given to av_open_input_file because you want to
+ * get the local port first, then you must call this function to set
+ * the remote server address.
+ *
+ * url syntax: udp://host:port[?option=val...]
+ * option: 'ttl=n' : set the ttl value (for multicast only)
+ * 'localport=n' : set the local port
+ * 'pkt_size=n' : set max packet size
+ * 'reuse=1' : enable reusing the socket
+ *
+ * @param s1 media file context
+ * @param uri of the remote server
+ * @return zero if no error.
+ */
+int udp_set_remote_url(URLContext *h, const char *uri)
+{
+ UDPContext *s = h->priv_data;
+ char hostname[256];
+ int port;
+
+ url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
+
+ /* set the destination address */
+ s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port);
+ if (s->dest_addr_len < 0) {
+ return AVERROR(EIO);
+ }
+ s->is_multicast = is_multicast_address(&s->dest_addr);
+
+ return 0;
+}
+
+/**
+ * Return the local port used by the UDP connexion
+ * @param s1 media file context
+ * @return the local port number
+ */
+int udp_get_local_port(URLContext *h)
+{
+ UDPContext *s = h->priv_data;
+ return s->local_port;
+}
+
+/**
+ * Return the udp file handle for select() usage to wait for several RTP
+ * streams at the same time.
+ * @param h media file context
+ */
+#if (LIBAVFORMAT_VERSION_MAJOR >= 53)
+static
+#endif
+int udp_get_file_handle(URLContext *h)
+{
+ UDPContext *s = h->priv_data;
+ return s->udp_fd;
+}
+
+/* put it in UDP context */
+/* return non zero if error */
+static int udp_open(URLContext *h, const char *uri, int flags)
+{
+ char hostname[1024];
+ int port, udp_fd = -1, tmp;
+ UDPContext *s = NULL;
+ int is_output;
+ const char *p;
+ char buf[256];
+ struct sockaddr_in my_addr;
+ int len;
+
+ h->is_streamed = 1;
+ h->max_packet_size = 1472;
+
+ is_output = (flags & URL_WRONLY);
+
+ if(!ff_network_init())
+ return AVERROR(EIO);
+
+ s = av_mallocz(sizeof(UDPContext));
+ if (!s)
+ return AVERROR(ENOMEM);
+
+ h->priv_data = s;
+ s->ttl = 16;
+ s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
+
+ p = strchr(uri, '?');
+ if (p) {
+ s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
+ if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
+ s->ttl = strtol(buf, NULL, 10);
+ }
+ if (find_info_tag(buf, sizeof(buf), "localport", p)) {
+ s->local_port = strtol(buf, NULL, 10);
+ }
+ if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
+ h->max_packet_size = strtol(buf, NULL, 10);
+ }
+ if (find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
+ s->buffer_size = strtol(buf, NULL, 10);
+ }
+ if (!find_info_tag(s->label, sizeof(s->label),
+ "contract_label", p)) {
+ s->label[0]=0;
+ }
+ }
+
+ /* fill the dest addr */
+ url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
+
+ /* XXX: fix url_split */
+ if (hostname[0] == '\0' || hostname[0] == '?') {
+ /* only accepts null hostname if input */
+ if (flags & URL_WRONLY)
+ goto fail;
+ } else {
+ udp_set_remote_url(h, uri);
+ }
+
+ udp_fd = udp_socket_create(s, &my_addr, &len);
+ if (udp_fd < 0)
+ goto fail;
+#if 0
+ if (s->reuse_socket)
+ if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
+ goto fail;
+
+ /* bind to the local address if not multicast or if the multicast
+ * bind failed */
+ /*if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0)
+ goto fail;*/
+#endif
+
+ len = sizeof(my_addr);
+ getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
+ s->local_port = udp_port(&my_addr, len);
+
+ if (is_output) {
+ /* limit the tx buf size to limit latency */
+ tmp = s->buffer_size;
+ if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
+ av_log(NULL, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno));
+ goto fail;
+ }
+ } else {
+ /* set udp recv buffer size to the largest possible udp packet size to
+ * avoid losing data on OSes that set this too low by default. */
+ tmp = s->buffer_size;
+ if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
+ av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_RECVBUF): %s\n", strerror(errno));
+ }
+ /* make the socket non-blocking */
+ ff_socket_nonblock(udp_fd, 1);
+ }
+ s->udp_fd = udp_fd;
+ return 0;
+
+ fail:
+ if (udp_fd >= 0)
+ closesocket(udp_fd);
+ av_free(s);
+ return AVERROR(EIO);
+}
+
+static int udp_read(URLContext *h, uint8_t *buf, int size)
+{
+ UDPContext *s = h->priv_data;
+ unsigned int from;
+ int len;
+
+ return frsh_receive_sync(s->repoint, buf, size, &len, &from);
+}
+
+static int udp_write(URLContext *h, uint8_t *buf, int size)
+{
+ UDPContext *s = h->priv_data;
+ int ret;
+
+ ret = frsh_send_async(s->sepoint, buf, size);
+ return ret;
+}
+
+static int udp_close(URLContext *h)
+{
+ UDPContext *s = h->priv_data;
+ int ret;
+
+ frsh_send_endpoint_unbind(s->sepoint);
+ ret = frsh_contract_cancel(s->vres);
+ return ret;
+}
+
+URLProtocol udp_protocol = {
+ "udp",
+ udp_open,
+ udp_read,
+ udp_write,
+ NULL, /* seek */
+ udp_close,
+ .url_get_file_handle = udp_get_file_handle,
+};