Asterisk - The Open Source Telephony Project  18.5.0
streamplayer.c File Reference

A utility for reading from a raw TCP stream. More...

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
Include dependency graph for streamplayer.c:

Go to the source code of this file.


int main (int argc, char *argv[])

Detailed Description

A utility for reading from a raw TCP stream.

Russell Bryant russe[email protected][email protected][email protected]igium[email protected].com

This application is intended for use when a raw TCP stream is desired to be used as a music on hold source for Asterisk. Some devices are capable of taking some kind of audio input and provide it as a raw TCP stream over the network, which is what inspired someone to fund this to be written. However, it would certainly be possible to write your own server application to provide music over a TCP stream from a centralized location.

This application is quite simple. It just reads the data from the TCP stream and dumps it straight to stdout. Due to the way Asterisk handles music on hold sources, this application checks to make sure writing to stdout will not be a blocking operation before doing so. If so, the data is just thrown away. This ensures that the stream will continue to be serviced, even if Asterisk is not currently using the source.

Update this application to be able to connect to a stream via HTTP, since that is the #1 most requested feature, and it would be quite useful. A lot of people think that is what this is for and email me when it does not work. :)

Definition in file streamplayer.c.

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 

Definition at line 62 of file streamplayer.c.

References buf, FD_SET, FD_ZERO, gethostbyname, hp, and NULL.

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;
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 (\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  }
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  }
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);
94  if (s < 0) {
95  fprintf(stderr, "Unable to allocate socket!\n");
96  exit(1);
97  }
99  res = connect(s, (struct sockaddr *)&sin, sizeof(sin));
101  if (res) {
102  fprintf(stderr, "Unable to connect to host!\n");
103  close(s);
104  exit(1);
105  }
107  while (1) {
108  res = read(s, buf, sizeof(buf));
110  if (res < 1)
111  break;
113  memset(&tv, 0, sizeof(tv));
114  FD_ZERO(&wfds);
115  FD_SET(1, &wfds);
117  select(2, NULL, &wfds, NULL, &tv);
119  if (FD_ISSET(1, &wfds)) {
120  if (write(1, buf, res) < 1) {
121  break;
122  }
123  }
124  }
126  close(s);
127  exit(res);
128 }
#define gethostbyname
Definition: lock.h:637
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#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