[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 |
|
---|
| 69 |
|
---|
[5cd5079] | 70 | static void data_callback(void* arg, void *buffer, ssize_t size)
|
---|
[e677b08] | 71 | {
|
---|
| 72 | playback_t *pb = arg;
|
---|
[5cd5079] | 73 | assert(pb);
|
---|
[e677b08] | 74 |
|
---|
[5cd5079] | 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);
|
---|
[e677b08] | 83 | }
|
---|
| 84 | if (bytes == 0) {
|
---|
| 85 | pb->playing = false;
|
---|
[50fa3f7] | 86 | printf("The end, nothing more to play.\n");
|
---|
[e677b08] | 87 | fibril_condvar_signal(&pb->cv);
|
---|
| 88 | }
|
---|
[5cd5079] | 89 | } else {
|
---|
| 90 | printf("Got error %s.\n", str_error(size));
|
---|
| 91 | pb->playing = false;
|
---|
| 92 | fibril_condvar_signal(&pb->cv);
|
---|
[e677b08] | 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 */
|
---|
[5cd5079] | 100 | int ret = hound_register_playback(pb->server, name, channels, rate,
|
---|
| 101 | format, data_callback, pb);
|
---|
[e677b08] | 102 | if (ret != EOK) {
|
---|
[5cd5079] | 103 | printf("Failed to register playback: %s\n", str_error(ret));
|
---|
[e677b08] | 104 | return;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | /* Connect */
|
---|
[5cd5079] | 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 |
|
---|
[50fa3f7] | 119 | printf("Unregistering playback\n");
|
---|
[5cd5079] | 120 | hound_unregister_playback(pb->server, name);
|
---|
[e677b08] | 121 | }
|
---|
| 122 |
|
---|
[aef1799] | 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 |
|
---|
[e677b08] | 130 | int main(int argc, char *argv[])
|
---|
| 131 | {
|
---|
[aef1799] | 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");
|
---|
[e677b08] | 152 | return 1;
|
---|
[aef1799] | 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);
|
---|
[e677b08] | 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 |
|
---|
[5cd5079] | 169 | hound_sess_t *sess = hound_get_session();
|
---|
[e677b08] | 170 | if (!sess) {
|
---|
| 171 | printf("Failed to connect to hound service\n");
|
---|
| 172 | return 1;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | playback_t pb;
|
---|
[5cd5079] | 176 | playback_initialize(&pb, sess);
|
---|
[e677b08] | 177 | pb.source = fopen(file, "rb");
|
---|
| 178 | if (pb.source == NULL) {
|
---|
| 179 | printf("Failed to open %s.\n", file);
|
---|
[5cd5079] | 180 | hound_release_session(sess);
|
---|
| 181 | return 1;
|
---|
[e677b08] | 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;
|
---|
[aef1799] | 188 | ret = wav_parse_header(&header, NULL, NULL, &channels, &rate,
|
---|
[5cd5079] | 189 | &format, &error);
|
---|
[e677b08] | 190 | if (ret != EOK) {
|
---|
| 191 | printf("Error parsing wav header: %s.\n", error);
|
---|
| 192 | fclose(pb.source);
|
---|
[5cd5079] | 193 | hound_release_session(sess);
|
---|
| 194 | return 1;
|
---|
[e677b08] | 195 | }
|
---|
| 196 |
|
---|
| 197 | play(&pb, channels, rate, format);
|
---|
| 198 |
|
---|
[50fa3f7] | 199 | printf("Releasing session\n");
|
---|
[5cd5079] | 200 | hound_release_session(sess);
|
---|
[e677b08] | 201 | return 0;
|
---|
| 202 | }
|
---|
| 203 | /**
|
---|
| 204 | * @}
|
---|
| 205 | */
|
---|