diff --git a/output_rtp.c b/output_rtp.c index f665406..d045c78 100644 --- a/output_rtp.c +++ b/output_rtp.c @@ -218,8 +218,11 @@ static void output_init_rtp_rtcp(struct output_s *o) { #endif int output_init_rtp(struct output_s *o) { - o->sockfd=socket_open(NULL, 0); + + if (o->sockfd < 0) + return -1; + socket_set_nonblock(o->sockfd); /* Join multicast group if its a multicast address */ diff --git a/output_udp.c b/output_udp.c index f160dc9..519b840 100644 --- a/output_udp.c +++ b/output_udp.c @@ -14,17 +14,18 @@ #define UDP_MAX_TS ((1500-40)/TS_PACKET_SIZE) int output_init_udp(struct output_s *o) { - struct sockaddr_in rsin; - - memset(&rsin, 0, sizeof(struct sockaddr_in)); - o->buffer=sb_init(UDP_MAX_TS, TS_PACKET_SIZE, 0); - if (o->buffer == NULL) - return 0; + if (!o->buffer) + goto errout1; o->sockfd=socket_open(NULL, 0); + if (o->sockfd < 0) + goto errout2; + socket_set_nonblock(o->sockfd); - socket_connect(o->sockfd, o->remoteaddr, o->remoteport); + + if (socket_connect(o->sockfd, o->remoteaddr, o->remoteport)) + goto errout3; /* Join Multicast group if its a multicast destination */ socket_join_multicast(o->sockfd, o->remoteaddr); @@ -43,6 +44,13 @@ int output_init_udp(struct output_s *o) { o->receiver=1; return 1; + +errout3: + socket_close(o->sockfd); +errout2: + sb_free(o->buffer); +errout1: + return 0; } void output_send_udp(struct output_s *o, uint8_t *tsp) { diff --git a/socket.c b/socket.c index bd04f45..532a777 100644 --- a/socket.c +++ b/socket.c @@ -13,6 +13,10 @@ #include "output.h" #include "simplebuffer.h" +void socket_close(int sock) { + close(sock); +} + int socket_open(char *laddr, int port) { struct sockaddr_in lsin; int sock; diff --git a/socket.h b/socket.h index a8db819..4c544f6 100644 --- a/socket.h +++ b/socket.h @@ -3,4 +3,5 @@ int socket_join_multicast(int sock, char *addr); int socket_set_ttl(int sock, int ttl); int socket_set_nonblock(int sock); int socket_open(char *laddr, int port); +void socket_close(int sock); int socket_connect(int sock, char *addr, int port);