Asterisk - The Open Source Telephony Project  18.5.0
streamplayer.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Russell Bryant <[email protected]>
7  *
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.
13  *
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.
17  */
18 
19 /*!
20  * \file
21  * \author Russell Bryant <[email protected]>
22  *
23  * \brief A utility for reading from a raw TCP stream
24  *
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.
31  *
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.
38  *
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. :)
43  */
44 
45 /*** MODULEINFO
46  <support_level>extended</support_level>
47  ***/
48 
49 #include <stdlib.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <netdb.h>
53 #include <unistd.h>
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>
58 #endif
59 #include <sys/time.h>
60 
61 
62 int main(int argc, char *argv[])
63 {
64  struct sockaddr_in sin;
65  struct hostent *hp;
66  int s;
67  int res;
68  char buf[2048];
69  fd_set wfds;
70  struct timeval tv;
71 
72  if (argc != 3) {
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");
77  exit(1);
78  }
79 
80  hp = gethostbyname(argv[1]);
81  if (!hp) {
82  fprintf(stderr, "Unable to lookup IP for host '%s'\n", argv[1]);
83  exit(1);
84  }
85 
86  memset(&sin, 0, sizeof(sin));
87 
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));
91 
92  s = socket(AF_INET, SOCK_STREAM, 0);
93 
94  if (s < 0) {
95  fprintf(stderr, "Unable to allocate socket!\n");
96  exit(1);
97  }
98 
99  res = connect(s, (struct sockaddr *)&sin, sizeof(sin));
100 
101  if (res) {
102  fprintf(stderr, "Unable to connect to host!\n");
103  close(s);
104  exit(1);
105  }
106 
107  while (1) {
108  res = read(s, buf, sizeof(buf));
109 
110  if (res < 1)
111  break;
112 
113  memset(&tv, 0, sizeof(tv));
114  FD_ZERO(&wfds);
115  FD_SET(1, &wfds);
116 
117  select(2, NULL, &wfds, NULL, &tv);
118 
119  if (FD_ISSET(1, &wfds)) {
120  if (write(1, buf, res) < 1) {
121  break;
122  }
123  }
124  }
125 
126  close(s);
127  exit(res);
128 }
#define gethostbyname
Definition: lock.h:637
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
int main(int argc, char *argv[])
Definition: streamplayer.c:62
#define NULL
Definition: resample.c:96
#define FD_ZERO(a)
Definition: select.h:49
#define FD_SET(fd, fds)
Definition: select.h:58
static struct hostent * hp
Definition: chan_skinny.c:1236