1 | /*
|
---|
2 | * Copyright (c) 2012 Jan Vesely
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup wavplay
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file PCM playback audio devices
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <assert.h>
|
---|
37 | #include <errno.h>
|
---|
38 | #include <fibril_synch.h>
|
---|
39 | #include <str_error.h>
|
---|
40 | #include <stdio.h>
|
---|
41 | #include <hound/client.h>
|
---|
42 | #include <pcm/sample_format.h>
|
---|
43 | #include <getopt.h>
|
---|
44 |
|
---|
45 | #include "dplay.h"
|
---|
46 | #include "wave.h"
|
---|
47 |
|
---|
48 | #define NAME_MAX 32
|
---|
49 | char name[NAME_MAX + 1];
|
---|
50 |
|
---|
51 | typedef struct {
|
---|
52 | FILE* source;
|
---|
53 | volatile bool playing;
|
---|
54 | fibril_mutex_t mutex;
|
---|
55 | fibril_condvar_t cv;
|
---|
56 | hound_sess_t *server;
|
---|
57 | } playback_t;
|
---|
58 |
|
---|
59 | static void playback_initialize(playback_t *pb, hound_sess_t *sess)
|
---|
60 | {
|
---|
61 | assert(pb);
|
---|
62 | pb->playing = false;
|
---|
63 | pb->source = NULL;
|
---|
64 | pb->server = sess;
|
---|
65 | fibril_mutex_initialize(&pb->mutex);
|
---|
66 | fibril_condvar_initialize(&pb->cv);
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | static void data_callback(void* arg, void *buffer, ssize_t size)
|
---|
71 | {
|
---|
72 | playback_t *pb = arg;
|
---|
73 | assert(pb);
|
---|
74 |
|
---|
75 | if (size > 0) {
|
---|
76 | const ssize_t bytes =
|
---|
77 | fread(buffer, sizeof(uint8_t), size, pb->source);
|
---|
78 | printf("%zu bytes ready\n", bytes);
|
---|
79 | if (bytes < size) {
|
---|
80 | printf(" requested: %zd ready: %zd zero: %zd\n",
|
---|
81 | size, bytes, size - bytes);
|
---|
82 | bzero(buffer + bytes, size - bytes);
|
---|
83 | }
|
---|
84 | if (bytes == 0) {
|
---|
85 | pb->playing = false;
|
---|
86 | printf("The end, nothing more to play.\n");
|
---|
87 | fibril_condvar_signal(&pb->cv);
|
---|
88 | }
|
---|
89 | } else {
|
---|
90 | printf("Got error %s.\n", str_error(size));
|
---|
91 | pb->playing = false;
|
---|
92 | fibril_condvar_signal(&pb->cv);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | static void play(playback_t *pb, unsigned channels, unsigned rate, pcm_sample_format_t format)
|
---|
97 | {
|
---|
98 | assert(pb);
|
---|
99 | /* Create playback client */
|
---|
100 | int ret = hound_register_playback(pb->server, name, channels, rate,
|
---|
101 | format, data_callback, pb);
|
---|
102 | if (ret != EOK) {
|
---|
103 | printf("Failed to register playback: %s\n", str_error(ret));
|
---|
104 | return;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /* Connect */
|
---|
108 | ret = hound_create_connection(pb->server, name, DEFAULT_SINK);
|
---|
109 | if (ret == EOK) {
|
---|
110 | fibril_mutex_lock(&pb->mutex);
|
---|
111 | for (pb->playing = true; pb->playing;
|
---|
112 | fibril_condvar_wait(&pb->cv, &pb->mutex));
|
---|
113 | fibril_mutex_unlock(&pb->mutex);
|
---|
114 |
|
---|
115 | hound_destroy_connection(pb->server, name, DEFAULT_SINK);
|
---|
116 | } else
|
---|
117 | printf("Failed to connect: %s\n", str_error(ret));
|
---|
118 |
|
---|
119 | printf("Unregistering playback\n");
|
---|
120 | hound_unregister_playback(pb->server, name);
|
---|
121 | }
|
---|
122 |
|
---|
123 | static const struct option opts[] = {
|
---|
124 | {"device", required_argument, 0, 'd'},
|
---|
125 | {"record", no_argument, 0, 'r'},
|
---|
126 | {0, 0, 0, 0}
|
---|
127 | };
|
---|
128 |
|
---|
129 |
|
---|
130 | int main(int argc, char *argv[])
|
---|
131 | {
|
---|
132 | const char *device = "default";
|
---|
133 | int idx = 0;
|
---|
134 | bool direct = false, record = false;
|
---|
135 | optind = 0;
|
---|
136 | int ret = 0;
|
---|
137 | while (ret != -1) {
|
---|
138 | ret = getopt_long(argc, argv, "d:r", opts, &idx);
|
---|
139 | switch (ret) {
|
---|
140 | case 'd':
|
---|
141 | direct = true;
|
---|
142 | device = optarg;
|
---|
143 | break;
|
---|
144 | case 'r':
|
---|
145 | record = true;
|
---|
146 | break;
|
---|
147 | };
|
---|
148 | }
|
---|
149 |
|
---|
150 | if (optind == argc) {
|
---|
151 | printf("Not enough arguments.\n");
|
---|
152 | return 1;
|
---|
153 | }
|
---|
154 | const char *file = argv[optind];
|
---|
155 |
|
---|
156 | printf("%s %s\n", record ? "Recording" : "Playing", file);
|
---|
157 | if (record) {
|
---|
158 | printf("Recording is not supported yet.\n");
|
---|
159 | return 1;
|
---|
160 | }
|
---|
161 | if (direct)
|
---|
162 | return dplay(device, file);
|
---|
163 |
|
---|
164 | task_id_t tid = task_get_id();
|
---|
165 | snprintf(name, NAME_MAX, "%s%" PRIu64 ":%s", argv[0], tid, file);
|
---|
166 |
|
---|
167 | printf("Client name: %s\n", name);
|
---|
168 |
|
---|
169 | hound_sess_t *sess = hound_get_session();
|
---|
170 | if (!sess) {
|
---|
171 | printf("Failed to connect to hound service\n");
|
---|
172 | return 1;
|
---|
173 | }
|
---|
174 |
|
---|
175 | playback_t pb;
|
---|
176 | playback_initialize(&pb, sess);
|
---|
177 | pb.source = fopen(file, "rb");
|
---|
178 | if (pb.source == NULL) {
|
---|
179 | printf("Failed to open %s.\n", file);
|
---|
180 | hound_release_session(sess);
|
---|
181 | return 1;
|
---|
182 | }
|
---|
183 | wave_header_t header;
|
---|
184 | fread(&header, sizeof(header), 1, pb.source);
|
---|
185 | unsigned rate, channels;
|
---|
186 | pcm_sample_format_t format;
|
---|
187 | const char *error;
|
---|
188 | ret = wav_parse_header(&header, NULL, NULL, &channels, &rate,
|
---|
189 | &format, &error);
|
---|
190 | if (ret != EOK) {
|
---|
191 | printf("Error parsing wav header: %s.\n", error);
|
---|
192 | fclose(pb.source);
|
---|
193 | hound_release_session(sess);
|
---|
194 | return 1;
|
---|
195 | }
|
---|
196 |
|
---|
197 | play(&pb, channels, rate, format);
|
---|
198 |
|
---|
199 | printf("Releasing session\n");
|
---|
200 | hound_release_session(sess);
|
---|
201 | return 0;
|
---|
202 | }
|
---|
203 | /**
|
---|
204 | * @}
|
---|
205 | */
|
---|