[1563] | 1 | //stream.c: |
---|
[1559] | 2 | |
---|
| 3 | /* |
---|
[5823] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2013 |
---|
[1559] | 5 | * The code (may) include prototypes and comments (and maybe |
---|
[4038] | 6 | * other code fragements) from OpenBSD's sndio. |
---|
[4050] | 7 | * See 'Copyright for sndio' below for more information on |
---|
| 8 | * code fragments taken from OpenBSD's sndio. |
---|
| 9 | * |
---|
| 10 | * --- Copyright for sndio --- |
---|
| 11 | * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> |
---|
| 12 | * |
---|
| 13 | * Permission to use, copy, modify, and distribute this software for any |
---|
| 14 | * purpose with or without fee is hereby granted, provided that the above |
---|
| 15 | * copyright notice and this permission notice appear in all copies. |
---|
| 16 | * |
---|
| 17 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
---|
| 18 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
| 19 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
---|
| 20 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
| 21 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
| 22 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
---|
| 23 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
| 24 | * --- End of Copyright for sndio --- |
---|
[1559] | 25 | * |
---|
| 26 | * This file is part of libroaresd a part of RoarAudio, |
---|
| 27 | * a cross-platform sound system for both, home and professional use. |
---|
| 28 | * See README for details. |
---|
| 29 | * |
---|
| 30 | * This file is free software; you can redistribute it and/or modify |
---|
| 31 | * it under the terms of the GNU General Public License version 3 |
---|
| 32 | * as published by the Free Software Foundation. |
---|
| 33 | * |
---|
| 34 | * RoarAudio is distributed in the hope that it will be useful, |
---|
| 35 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 36 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 37 | * GNU General Public License for more details. |
---|
| 38 | * |
---|
| 39 | * You should have received a copy of the GNU General Public License |
---|
| 40 | * along with this software; see the file COPYING. If not, write to |
---|
[3517] | 41 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 42 | * Boston, MA 02110-1301, USA. |
---|
[1559] | 43 | * |
---|
| 44 | * NOTE for everyone want's to change something and send patches: |
---|
| 45 | * read README and HACKING! There a addition information on |
---|
| 46 | * the license of this document you need to read before you send |
---|
| 47 | * any patches. |
---|
| 48 | */ |
---|
| 49 | |
---|
[1571] | 50 | #define ROAR_USE_OWN_SNDIO_HDL |
---|
[1559] | 51 | #include "libroarsndio.h" |
---|
| 52 | |
---|
[2375] | 53 | // this is void because we do not care about if this failed... |
---|
| 54 | static void send_vol_event (struct sio_hdl * hdl) { |
---|
| 55 | int channels; |
---|
| 56 | struct roar_mixer_settings mixer; |
---|
| 57 | int i; |
---|
| 58 | unsigned vol; |
---|
| 59 | |
---|
| 60 | // in case of no event handler we do not send the event at all ;) |
---|
| 61 | if ( hdl->on_vol == NULL ) |
---|
| 62 | return; |
---|
| 63 | |
---|
| 64 | if ( roar_get_vol(&(hdl->con), roar_stream_get_id(&(hdl->stream)), &mixer, &channels) == -1 ) |
---|
| 65 | return; |
---|
| 66 | |
---|
| 67 | ROAR_DBG("send_vol_event(*): channels=%i", channels); |
---|
| 68 | |
---|
| 69 | switch (channels) { |
---|
| 70 | case 1: |
---|
| 71 | ROAR_DBG("send_vol_event(*): mixer.scale=%i, mixer.mixer={%i,...}", mixer.scale, mixer.mixer[0]); |
---|
| 72 | vol = mixer.mixer[0] * SIO_MAXVOL; |
---|
| 73 | vol /= mixer.scale; |
---|
| 74 | ROAR_DBG("send_vol_event(*): vol=%u", vol); |
---|
| 75 | break; |
---|
| 76 | case 2: |
---|
| 77 | vol = (mixer.mixer[0] + mixer.mixer[1]) * SIO_MAXVOL / mixer.scale / 2; |
---|
| 78 | break; |
---|
| 79 | default: |
---|
| 80 | vol = 0; |
---|
| 81 | for (i = 0; i < channels; i++) |
---|
| 82 | vol += mixer.mixer[i]; |
---|
| 83 | |
---|
| 84 | vol /= channels; |
---|
| 85 | vol *= SIO_MAXVOL; |
---|
| 86 | vol /= mixer.scale; |
---|
| 87 | break; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | ROAR_DBG("send_vol_event(*): vol=%u", vol); |
---|
| 91 | hdl->on_vol(hdl->on_vol_arg, vol); |
---|
| 92 | } |
---|
| 93 | |
---|
[1567] | 94 | #define _i(x) (hdl->info.x) |
---|
[1563] | 95 | int sio_start (struct sio_hdl * hdl) { |
---|
[2019] | 96 | |
---|
[2020] | 97 | // TODO: FIXME: use full VIO support here, not fh->vio! |
---|
| 98 | |
---|
[1567] | 99 | if ( hdl == NULL ) |
---|
| 100 | return 0; |
---|
| 101 | |
---|
[2019] | 102 | if ( hdl->stream_opened ) |
---|
| 103 | return 0; |
---|
| 104 | |
---|
[2843] | 105 | if ( roar_vio_simple_new_stream_obj(&(hdl->svio), &(hdl->con), &(hdl->stream), |
---|
[5289] | 106 | _i(rate), _i(channels), _i(bits), _i(codec), hdl->dir, -1) == -1 ) |
---|
[1567] | 107 | return 0; |
---|
| 108 | |
---|
[2022] | 109 | ROAR_DBG("sio_start(hdl=%p): rate=%i, channels=%i, bits=%i, codec=%i", hdl, _i(rate), _i(channels), _i(bits), _i(codec)); |
---|
| 110 | |
---|
[3237] | 111 | if ( hdl->nonblock ) { |
---|
| 112 | if ( roar_vio_nonblock(&(hdl->svio), ROAR_SOCKET_NONBLOCK) == -1 ) { |
---|
| 113 | roar_vio_close(&(hdl->svio)); |
---|
| 114 | return 0; |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | |
---|
[2375] | 118 | send_vol_event(hdl); |
---|
[2019] | 119 | |
---|
| 120 | hdl->stream_opened = 1; |
---|
[3238] | 121 | hdl->ioerror = 0; |
---|
[1567] | 122 | |
---|
| 123 | return 1; |
---|
[1563] | 124 | } |
---|
[1567] | 125 | #undef _i |
---|
[1563] | 126 | |
---|
| 127 | int sio_stop (struct sio_hdl * hdl) { |
---|
| 128 | |
---|
| 129 | if ( hdl == NULL ) |
---|
| 130 | return 0; |
---|
| 131 | |
---|
[2019] | 132 | if ( !hdl->stream_opened ) |
---|
[1563] | 133 | return 0; |
---|
| 134 | |
---|
[2019] | 135 | roar_vio_close(&(hdl->svio)); |
---|
[1563] | 136 | |
---|
[2019] | 137 | hdl->stream_opened = -1; |
---|
[1563] | 138 | |
---|
| 139 | return 1; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | size_t sio_read (struct sio_hdl * hdl, void * addr, size_t nbytes) { |
---|
| 143 | ssize_t ret; |
---|
| 144 | |
---|
| 145 | if ( hdl == NULL ) |
---|
| 146 | return 0; |
---|
| 147 | |
---|
[2019] | 148 | if ( !hdl->stream_opened ) |
---|
[1563] | 149 | return 0; |
---|
| 150 | |
---|
[3238] | 151 | if ( (ret = roar_vio_read(&(hdl->svio), addr, nbytes)) < 0 ) { |
---|
| 152 | hdl->ioerror = 1; |
---|
[1563] | 153 | return 0; |
---|
[3238] | 154 | } |
---|
| 155 | |
---|
| 156 | if ( hdl->nonblock ) |
---|
| 157 | hdl->ioerror = 0; |
---|
[1563] | 158 | |
---|
| 159 | return ret; |
---|
| 160 | } |
---|
[2574] | 161 | size_t sio_write (struct sio_hdl * hdl, const void * addr, size_t nbytes) { |
---|
[1563] | 162 | ssize_t ret; |
---|
| 163 | |
---|
| 164 | if ( hdl == NULL ) |
---|
| 165 | return 0; |
---|
| 166 | |
---|
[2019] | 167 | if ( !hdl->stream_opened ) |
---|
[1563] | 168 | return 0; |
---|
| 169 | |
---|
[3238] | 170 | if ( (ret = roar_vio_write(&(hdl->svio), (void*) addr, nbytes)) < 0 ) { |
---|
| 171 | hdl->ioerror = 1; |
---|
[1563] | 172 | return 0; |
---|
[3238] | 173 | } |
---|
| 174 | |
---|
| 175 | if ( hdl->nonblock ) |
---|
| 176 | hdl->ioerror = 0; |
---|
[1563] | 177 | |
---|
[1577] | 178 | if ( hdl->on_move != NULL ) { |
---|
| 179 | hdl->on_move(hdl->on_move_arg, 8*ret/(hdl->info.channels * hdl->info.bits)); |
---|
| 180 | } |
---|
| 181 | |
---|
[1563] | 182 | return ret; |
---|
| 183 | } |
---|
| 184 | |
---|
[1559] | 185 | //ll |
---|