File: flow1.c

package info (click to toggle)
rplay 3.3.2-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 3,208 kB
  • ctags: 3,536
  • sloc: ansic: 42,124; makefile: 1,309; perl: 742; tcl: 345; sh: 301; java: 220
file content (112 lines) | stat: -rw-r--r-- 2,241 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* $Id: flow1.c,v 1.3 1998/11/07 21:15:12 boyns Exp $ */

/* usage: flow1 soundfile */

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <rplay.h>

main(int argc, char **argv)
{
    FILE *fp;
    int rptp_fd, size, n, nwritten;
    struct stat st;
    char *id;
    char line[RPTP_MAX_LINE];
    char response[RPTP_MAX_LINE];
    char buf[8000];

    /* First determine how big the audio file is. */
    if (stat(argv[1], &st) < 0)
    {
	perror(argv[1]);
	exit(1);
    }
    size = st.st_size;

    fp = fopen(argv[1], "r");
    if (fp == NULL)
    {
	perror(argv[1]);
	exit(1);
    }

    /* Connect to the audio server. */
    rptp_fd = rptp_open(rplay_default_host(), RPTP_PORT, response, sizeof(response));
    if (rptp_fd < 0)
    {
	rptp_perror(rplay_default_host());
	exit(1);
    }

    /* Start the flow using `input-storage=none'. */
    sprintf(line, "play input=flow input-storage=none sound=%s", argv[1]);
    switch (rptp_command(rptp_fd, line, response, sizeof(response)))
    {
    case -1:
	rptp_perror(argv[0]);
	exit(1);

    case 1:
	fprintf(stderr, "%s\n", rptp_parse(response, "error"));
	exit(1);

    case 0:
	break;
    }

    /* Save the spool id so `put' can use it later. */
    id = rptp_parse(response, "id");

    /* Read chunks of audio from the file and send them to rplayd.
       rplayd will deal with flow-control. */
    while (size > 0)
    {
	n = fread(buf, 1, sizeof(buf), fp);

	/* Use `put' to send the audio data. */
	sprintf(line, "put id=%s size=%d", id, n);
	switch (rptp_command(rptp_fd, line, response, sizeof(response)))
	{
	case -1:
	    rptp_perror(argv[0]);
	    exit(1);

	case 1:
	    fprintf(stderr, "%s\n", rptp_parse(response, "error"));
	    exit(1);

	case 0:
	    break;
	}

	nwritten = rptp_write(rptp_fd, buf, n);
	if (nwritten != n)
	{
	    rptp_perror("flow");
	    break;
	}
	size -= nwritten;
    }

    fclose(fp);

    /* Always send `done' when the flow is over. */
    sprintf(line, "done id=%s", id);
    switch (rptp_command(rptp_fd, line, response, sizeof(response)))
    {
    case -1:
	rptp_perror(argv[0]);
	exit(1);

    case 1:
	fprintf(stderr, "%s\n", rptp_parse(response, "error"));
	exit(1);

    case 0:
	break;
    }

    exit(0);
}