Merge changes from team/russell/issue_9520
[asterisk-bristuff.git] / utils / streamplayer.c
blobfb0d055a24efb45150551e95f31e5e084c4315a2
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Russell Bryant <russell@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
19 /*!
20 * \file
21 * \author Russell Bryant <russell@digium.com>
23 * \brief A utility for reading from a raw TCP stream
25 * This application is intended for use when a raw TCP stream is desired to be
26 * used as a music on hold source for Asterisk. Some devices are capable of
27 * taking some kind of audio input and provide it as a raw TCP stream over the
28 * network, which is what inspired someone to fund this to be written.
29 * However, it would certainly be possible to write your own server application
30 * to provide music over a TCP stream from a centralized location.
32 * This application is quite simple. It just reads the data from the TCP
33 * stream and dumps it straight to stdout. Due to the way Asterisk handles
34 * music on hold sources, this application checks to make sure writing
35 * to stdout will not be a blocking operation before doing so. If so, the data
36 * is just thrown away. This ensures that the stream will continue to be
37 * serviced, even if Asterisk is not currently using the source.
39 * \todo Update this application to be able to connect to a stream via HTTP,
40 * since that is the #1 most requested feature, and it would be quite useful.
41 * A lot of people think that is what this is for and email me when it does
42 * not work. :)
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <netdb.h>
49 #include <unistd.h>
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__Darwin__) || defined(__CYGWIN__)
53 #include <netinet/in.h>
54 #endif
55 #include <sys/time.h>
58 int main(int argc, char *argv[])
60 struct sockaddr_in sin;
61 struct hostent *hp;
62 int s;
63 int res;
64 char buf[2048];
65 fd_set wfds;
66 struct timeval tv;
68 if (argc != 3) {
69 fprintf(stderr, "streamplayer -- A utility for reading from a raw TCP stream.\n");
70 fprintf(stderr, "Written for use with Asterisk (http://www.asterisk.org)\n");
71 fprintf(stderr, "Copyright (C) 2005 -- Russell Bryant -- Digium, Inc.\n\n");
72 fprintf(stderr, "Usage: ./streamplayer <ip> <port>\n");
73 exit(1);
76 hp = gethostbyname(argv[1]);
77 if (!hp) {
78 fprintf(stderr, "Unable to lookup IP for host '%s'\n", argv[1]);
79 exit(1);
82 memset(&sin, 0, sizeof(sin));
84 sin.sin_family = AF_INET;
85 sin.sin_port = htons(atoi(argv[2]));
86 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
88 s = socket(AF_INET, SOCK_STREAM, 0);
90 if (s < 0) {
91 fprintf(stderr, "Unable to allocate socket!\n");
92 exit(1);
95 res = connect(s, (struct sockaddr *)&sin, sizeof(sin));
97 if (res) {
98 fprintf(stderr, "Unable to connect to host!\n");
99 close(s);
100 exit(1);
103 while (1) {
104 res = read(s, buf, sizeof(buf));
106 if (res < 1)
107 break;
109 memset(&tv, 0, sizeof(tv));
110 FD_ZERO(&wfds);
111 FD_SET(1, &wfds);
113 select(2, NULL, &wfds, NULL, &tv);
115 if (FD_ISSET(1, &wfds))
116 write(1, buf, res);
119 close(s);
120 exit(res);