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.
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
46 <support_level>extended</support_level>
54 #include <sys/types.h>
55 #include <sys/socket.h>
56 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__Darwin__) || defined(__CYGWIN__) || defined(__DragonFly__)
57 #include <netinet/in.h>
62 int main(int argc, char *argv[])
64 struct sockaddr_in sin;
73 fprintf(stderr, "streamplayer -- A utility for reading from a raw TCP stream.\n");
74 fprintf(stderr, "Written for use with Asterisk (http://www.asterisk.org)\n");
75 fprintf(stderr, "Copyright (C) 2005 -- Russell Bryant -- Digium, Inc.\n\n");
76 fprintf(stderr, "Usage: ./streamplayer <ip> <port>\n");
80 hp = gethostbyname(argv[1]);
82 fprintf(stderr, "Unable to lookup IP for host '%s'\n", argv[1]);
86 memset(&sin, 0, sizeof(sin));
88 sin.sin_family = AF_INET;
89 sin.sin_port = htons(atoi(argv[2]));
90 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
92 s = socket(AF_INET, SOCK_STREAM, 0);
95 fprintf(stderr, "Unable to allocate socket!\n");
99 res = connect(s, (struct sockaddr *)&sin, sizeof(sin));
102 fprintf(stderr, "Unable to connect to host!\n");
108 res = read(s, buf, sizeof(buf));
113 memset(&tv, 0, sizeof(tv));
117 select(2, NULL, &wfds, NULL, &tv);
119 if (FD_ISSET(1, &wfds)) {
120 if (write(1, buf, res) < 1) {