[1599] | 1 | //ao_roar.c: |
---|
| 2 | /* |
---|
| 3 | * ao_roar - RoarAudio audio output driver for MPlayer |
---|
| 4 | * |
---|
| 5 | * Juergen Keil <jk@tools.de> |
---|
| 6 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008, 2009 |
---|
| 7 | * |
---|
| 8 | * This driver is distributed under the terms of the GPL v. 2 |
---|
| 9 | * |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | #include <sys/types.h> |
---|
| 13 | #include <sys/time.h> |
---|
| 14 | #include <sys/socket.h> |
---|
| 15 | #include <stdio.h> |
---|
| 16 | #include <string.h> |
---|
| 17 | #include <unistd.h> |
---|
| 18 | #include <errno.h> |
---|
| 19 | #include <fcntl.h> |
---|
| 20 | #include <time.h> |
---|
| 21 | #ifdef __svr4__ |
---|
| 22 | #include <stropts.h> |
---|
| 23 | #endif |
---|
| 24 | #include <roaraudio.h> |
---|
| 25 | |
---|
| 26 | #include "config.h" |
---|
| 27 | #include "audio_out.h" |
---|
| 28 | #include "audio_out_internal.h" |
---|
| 29 | #include "libaf/af_format.h" |
---|
| 30 | #include "mp_msg.h" |
---|
| 31 | #include "help_mp.h" |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | #undef ESD_DEBUG |
---|
| 35 | |
---|
| 36 | #if ESD_DEBUG |
---|
| 37 | #define dprintf(...) printf(__VA_ARGS__) |
---|
| 38 | #else |
---|
| 39 | #define dprintf(...) /**/ |
---|
| 40 | #endif |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | #define ROAR_CLIENT_NAME "MPlayer" |
---|
| 44 | |
---|
| 45 | static struct roar_connection roar_con; |
---|
| 46 | static struct roar_stream roar_stream; |
---|
| 47 | static struct roar_stream_info roar_info; |
---|
| 48 | static int roar_fh; |
---|
| 49 | |
---|
| 50 | static ao_info_t info = |
---|
| 51 | { |
---|
| 52 | "RoarAudio audio output", |
---|
| 53 | "roar", |
---|
| 54 | "Philipp 'ph3-der-loewe' Schafft <lion@lion.leolix.org>", |
---|
| 55 | "" |
---|
| 56 | }; |
---|
| 57 | |
---|
| 58 | LIBAO_EXTERN(roar) |
---|
| 59 | |
---|
| 60 | /* |
---|
| 61 | * to set/get/query special features/parameters |
---|
| 62 | */ |
---|
| 63 | static int control(int cmd, void *arg) |
---|
| 64 | { |
---|
| 65 | switch (cmd) { |
---|
| 66 | case AOCONTROL_GET_VOLUME: |
---|
| 67 | case AOCONTROL_SET_VOLUME: |
---|
| 68 | return CONTROL_OK; |
---|
| 69 | default: |
---|
| 70 | return CONTROL_UNKNOWN; |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | /* |
---|
| 76 | * open & setup audio device |
---|
| 77 | * return: 1=success 0=fail |
---|
| 78 | */ |
---|
| 79 | static int init(int rate_hz, int channels, int format, int flags) { |
---|
| 80 | int codec = ROAR_CODEC_DEFAULT; |
---|
| 81 | int bits = 16; |
---|
| 82 | |
---|
| 83 | if ( roar_simple_connect(&roar_con, NULL, ROAR_CLIENT_NAME) == -1 ) |
---|
| 84 | return 0; |
---|
| 85 | |
---|
| 86 | switch (format) { |
---|
| 87 | case AF_FORMAT_S8: |
---|
| 88 | bits = 8; |
---|
| 89 | break; |
---|
| 90 | case AF_FORMAT_U8: |
---|
| 91 | bits = 8; |
---|
| 92 | codec = ROAR_CODEC_PCM_U_LE; |
---|
| 93 | break; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | if ( (roar_fh = roar_simple_new_stream_obj(&roar_con, &roar_stream, rate_hz, channels, bits, codec, ROAR_DIR_PLAY)) == -1 ) |
---|
| 97 | return 0; |
---|
| 98 | |
---|
| 99 | if ( roar_stream_get_info(&roar_con, &roar_stream, &roar_info) == -1 ) { |
---|
| 100 | close(roar_fh); |
---|
| 101 | return 0; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | roar_socket_nonblock(roar_fh, ROAR_SOCKET_NONBLOCK); |
---|
| 105 | |
---|
| 106 | return 1; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | /* |
---|
| 111 | * close audio device |
---|
| 112 | */ |
---|
| 113 | static void uninit(int immed) { |
---|
| 114 | if ( roar_fh != -1 ) |
---|
| 115 | close(roar_fh); |
---|
| 116 | |
---|
| 117 | roar_fh = -1; |
---|
| 118 | |
---|
| 119 | roar_disconnect(&roar_con); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | /* |
---|
| 124 | * plays 'len' bytes of 'data' |
---|
| 125 | * it should round it down to outburst*n |
---|
| 126 | * return: number of bytes played |
---|
| 127 | */ |
---|
| 128 | static int play(void* data, int len, int flags) { |
---|
| 129 | int pkg = 441*2*2; |
---|
| 130 | int ret = 0; |
---|
| 131 | int r; |
---|
| 132 | |
---|
| 133 | while (len > 0) { |
---|
| 134 | if ( (r = write(roar_fh, data, pkg < len ? pkg : len)) > 0 ) { |
---|
| 135 | len -= r; |
---|
| 136 | data += r; |
---|
| 137 | ret += r; |
---|
| 138 | } else { |
---|
| 139 | break; |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | // ROAR_WARN("play(*) = %i", ret); |
---|
| 144 | |
---|
| 145 | return ret; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | /* |
---|
| 150 | * stop playing, keep buffers (for pause) |
---|
| 151 | */ |
---|
| 152 | static void audio_pause(void) |
---|
| 153 | { |
---|
| 154 | /* |
---|
| 155 | * not possible with esd. the esd daemom will continue playing |
---|
| 156 | * buffered data (not more than ESD_MAX_DELAY seconds of samples) |
---|
| 157 | */ |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | /* |
---|
| 162 | * resume playing, after audio_pause() |
---|
| 163 | */ |
---|
| 164 | static void audio_resume(void) |
---|
| 165 | { |
---|
| 166 | /* |
---|
| 167 | * not possible with esd. |
---|
| 168 | * |
---|
| 169 | * Let's hope the pause was long enough that the esd ran out of |
---|
| 170 | * buffered data; we restart our time based delay computation |
---|
| 171 | * for an audio resume. |
---|
| 172 | */ |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | |
---|
| 176 | /* |
---|
| 177 | * stop playing and empty buffers (for seeking/pause) |
---|
| 178 | */ |
---|
| 179 | static void reset(void) { |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | /* |
---|
| 184 | * return: how many bytes can be played without blocking |
---|
| 185 | */ |
---|
| 186 | static int get_space(void) { |
---|
| 187 | fd_set sl; |
---|
| 188 | struct timeval tv; |
---|
| 189 | int r; |
---|
| 190 | |
---|
| 191 | FD_ZERO(&sl); |
---|
| 192 | FD_SET(roar_fh, &sl); |
---|
| 193 | |
---|
| 194 | tv.tv_sec = 0; |
---|
| 195 | tv.tv_usec = 1; |
---|
| 196 | |
---|
| 197 | if ((r = select(roar_fh + 1, NULL, &sl, NULL, &tv)) > 0) { |
---|
| 198 | return roar_info.block_size; |
---|
| 199 | } |
---|
| 200 | return 0; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | |
---|
| 204 | /* |
---|
| 205 | * return: delay in seconds between first and last sample in buffer |
---|
| 206 | */ |
---|
| 207 | static float get_delay(void) { |
---|
| 208 | return 0.02; |
---|
| 209 | } |
---|