[e677b08] | 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>
|
---|
[5cd5079] | 39 | #include <str_error.h>
|
---|
[e677b08] | 40 | #include <stdio.h>
|
---|
[5cd5079] | 41 | #include <hound/client.h>
|
---|
[ea6c838] | 42 | #include <pcm/sample_format.h>
|
---|
[aef1799] | 43 | #include <getopt.h>
|
---|
[e677b08] | 44 |
|
---|
[aef1799] | 45 | #include "dplay.h"
|
---|
[e677b08] | 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;
|
---|
[5cd5079] | 56 | hound_sess_t *server;
|
---|
[e677b08] | 57 | } playback_t;
|
---|
| 58 |
|
---|
[5cd5079] | 59 | static void playback_initialize(playback_t *pb, hound_sess_t *sess)
|
---|
[e677b08] | 60 | {
|
---|
| 61 | assert(pb);
|
---|
| 62 | pb->playing = false;
|
---|
| 63 | pb->source = NULL;
|
---|
[5cd5079] | 64 | pb->server = sess;
|
---|
[e677b08] | 65 | fibril_mutex_initialize(&pb->mutex);
|
---|
| 66 | fibril_condvar_initialize(&pb->cv);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[5cd5079] | 69 | static void data_callback(void* arg, void *buffer, ssize_t size)
|
---|
[e677b08] | 70 | {
|
---|
| 71 | playback_t *pb = arg;
|
---|
[5cd5079] | 72 | assert(pb);
|
---|
[e677b08] | 73 |
|
---|
[5cd5079] | 74 | if (size > 0) {
|
---|
| 75 | const ssize_t bytes =
|
---|
| 76 | fread(buffer, sizeof(uint8_t), size, pb->source);
|
---|
| 77 | printf("%zu bytes ready\n", bytes);
|
---|
| 78 | if (bytes < size) {
|
---|
| 79 | printf(" requested: %zd ready: %zd zero: %zd\n",
|
---|
| 80 | size, bytes, size - bytes);
|
---|
| 81 | bzero(buffer + bytes, size - bytes);
|
---|
[e677b08] | 82 | }
|
---|
| 83 | if (bytes == 0) {
|
---|
| 84 | pb->playing = false;
|
---|
[50fa3f7] | 85 | printf("The end, nothing more to play.\n");
|
---|
[e677b08] | 86 | fibril_condvar_signal(&pb->cv);
|
---|
| 87 | }
|
---|
[5cd5079] | 88 | } else {
|
---|
| 89 | printf("Got error %s.\n", str_error(size));
|
---|
| 90 | pb->playing = false;
|
---|
| 91 | fibril_condvar_signal(&pb->cv);
|
---|
[e677b08] | 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | static void play(playback_t *pb, unsigned channels, unsigned rate, pcm_sample_format_t format)
|
---|
| 96 | {
|
---|
| 97 | assert(pb);
|
---|
| 98 | /* Create playback client */
|
---|
[5cd5079] | 99 | int ret = hound_register_playback(pb->server, name, channels, rate,
|
---|
| 100 | format, data_callback, pb);
|
---|
[e677b08] | 101 | if (ret != EOK) {
|
---|
[5cd5079] | 102 | printf("Failed to register playback: %s\n", str_error(ret));
|
---|
[e677b08] | 103 | return;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /* Connect */
|
---|
[5cd5079] | 107 | ret = hound_create_connection(pb->server, name, DEFAULT_SINK);
|
---|
| 108 | if (ret == EOK) {
|
---|
| 109 | fibril_mutex_lock(&pb->mutex);
|
---|
| 110 | for (pb->playing = true; pb->playing;
|
---|
| 111 | fibril_condvar_wait(&pb->cv, &pb->mutex));
|
---|
| 112 | fibril_mutex_unlock(&pb->mutex);
|
---|
| 113 |
|
---|
| 114 | hound_destroy_connection(pb->server, name, DEFAULT_SINK);
|
---|
| 115 | } else
|
---|
| 116 | printf("Failed to connect: %s\n", str_error(ret));
|
---|
| 117 |
|
---|
[50fa3f7] | 118 | printf("Unregistering playback\n");
|
---|
[5cd5079] | 119 | hound_unregister_playback(pb->server, name);
|
---|
[e677b08] | 120 | }
|
---|
| 121 |
|
---|
[aef1799] | 122 | static const struct option opts[] = {
|
---|
| 123 | {"device", required_argument, 0, 'd'},
|
---|
| 124 | {"record", no_argument, 0, 'r'},
|
---|
[86fe9d1] | 125 | {"help", no_argument, 0, 'h'},
|
---|
[aef1799] | 126 | {0, 0, 0, 0}
|
---|
| 127 | };
|
---|
| 128 |
|
---|
[86fe9d1] | 129 | static void print_help(const char* name)
|
---|
| 130 | {
|
---|
| 131 | printf("Usage: %s [options] file\n", name);
|
---|
| 132 | printf("supported options:\n");
|
---|
| 133 | printf("\t -h, --help\t Print this help.\n");
|
---|
| 134 | printf("\t -r, --record\t Start recording instead of playback.\n");
|
---|
| 135 | printf("\t -d, --device\t Use specified device instead of sound "
|
---|
| 136 | "service. Use location path or special device `default'\n");
|
---|
| 137 | }
|
---|
[aef1799] | 138 |
|
---|
[e677b08] | 139 | int main(int argc, char *argv[])
|
---|
| 140 | {
|
---|
[aef1799] | 141 | const char *device = "default";
|
---|
| 142 | int idx = 0;
|
---|
| 143 | bool direct = false, record = false;
|
---|
| 144 | optind = 0;
|
---|
| 145 | int ret = 0;
|
---|
| 146 | while (ret != -1) {
|
---|
[86fe9d1] | 147 | ret = getopt_long(argc, argv, "d:rh", opts, &idx);
|
---|
[aef1799] | 148 | switch (ret) {
|
---|
| 149 | case 'd':
|
---|
| 150 | direct = true;
|
---|
| 151 | device = optarg;
|
---|
| 152 | break;
|
---|
| 153 | case 'r':
|
---|
| 154 | record = true;
|
---|
| 155 | break;
|
---|
[86fe9d1] | 156 | case 'h':
|
---|
| 157 | print_help(*argv);
|
---|
| 158 | return 0;
|
---|
[aef1799] | 159 | };
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | if (optind == argc) {
|
---|
| 163 | printf("Not enough arguments.\n");
|
---|
[86fe9d1] | 164 | print_help(*argv);
|
---|
[e677b08] | 165 | return 1;
|
---|
[aef1799] | 166 | }
|
---|
| 167 | const char *file = argv[optind];
|
---|
| 168 |
|
---|
| 169 | printf("%s %s\n", record ? "Recording" : "Playing", file);
|
---|
| 170 | if (record) {
|
---|
| 171 | printf("Recording is not supported yet.\n");
|
---|
| 172 | return 1;
|
---|
| 173 | }
|
---|
| 174 | if (direct)
|
---|
| 175 | return dplay(device, file);
|
---|
[e677b08] | 176 |
|
---|
| 177 | task_id_t tid = task_get_id();
|
---|
| 178 | snprintf(name, NAME_MAX, "%s%" PRIu64 ":%s", argv[0], tid, file);
|
---|
| 179 |
|
---|
| 180 | printf("Client name: %s\n", name);
|
---|
| 181 |
|
---|
[5cd5079] | 182 | hound_sess_t *sess = hound_get_session();
|
---|
[e677b08] | 183 | if (!sess) {
|
---|
| 184 | printf("Failed to connect to hound service\n");
|
---|
| 185 | return 1;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | playback_t pb;
|
---|
[5cd5079] | 189 | playback_initialize(&pb, sess);
|
---|
[e677b08] | 190 | pb.source = fopen(file, "rb");
|
---|
| 191 | if (pb.source == NULL) {
|
---|
| 192 | printf("Failed to open %s.\n", file);
|
---|
[5cd5079] | 193 | hound_release_session(sess);
|
---|
| 194 | return 1;
|
---|
[e677b08] | 195 | }
|
---|
| 196 | wave_header_t header;
|
---|
| 197 | fread(&header, sizeof(header), 1, pb.source);
|
---|
| 198 | unsigned rate, channels;
|
---|
| 199 | pcm_sample_format_t format;
|
---|
| 200 | const char *error;
|
---|
[aef1799] | 201 | ret = wav_parse_header(&header, NULL, NULL, &channels, &rate,
|
---|
[5cd5079] | 202 | &format, &error);
|
---|
[e677b08] | 203 | if (ret != EOK) {
|
---|
| 204 | printf("Error parsing wav header: %s.\n", error);
|
---|
| 205 | fclose(pb.source);
|
---|
[5cd5079] | 206 | hound_release_session(sess);
|
---|
| 207 | return 1;
|
---|
[e677b08] | 208 | }
|
---|
| 209 |
|
---|
| 210 | play(&pb, channels, rate, format);
|
---|
| 211 |
|
---|
[50fa3f7] | 212 | printf("Releasing session\n");
|
---|
[5cd5079] | 213 | hound_release_session(sess);
|
---|
[e677b08] | 214 | return 0;
|
---|
| 215 | }
|
---|
| 216 | /**
|
---|
| 217 | * @}
|
---|
| 218 | */
|
---|