| 1 | /* |
| 2 | * This file is part of DisOrder |
| 3 | * Copyright (C) 2005, 2006, 2007 Richard Kettlewell |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 18 | * USA |
| 19 | */ |
| 20 | /** @file server/speaker.h |
| 21 | * @brief Speaker process |
| 22 | */ |
| 23 | #ifndef SPEAKER_H |
| 24 | #define SPEAKER_H |
| 25 | |
| 26 | #ifdef WORDS_BIGENDIAN |
| 27 | # define MACHINE_AO_FMT AO_FMT_BIG |
| 28 | #else |
| 29 | # define MACHINE_AO_FMT AO_FMT_LITTLE |
| 30 | #endif |
| 31 | |
| 32 | /** @brief How many seconds of input to buffer |
| 33 | * |
| 34 | * While any given connection has this much audio buffered, no more reads will |
| 35 | * be issued for that connection. The decoder will have to wait. |
| 36 | */ |
| 37 | #define BUFFER_SECONDS 5 |
| 38 | |
| 39 | /** @brief Frame batch size |
| 40 | * |
| 41 | * This controls how many frames are written in one go. |
| 42 | * |
| 43 | * For ALSA we request a buffer of three times this size and set the low |
| 44 | * watermark to this amount. The goal is then to keep between 1 and 3 times |
| 45 | * this many frames in play. |
| 46 | * |
| 47 | * For all backends we attempt to play up to three times this many frames per |
| 48 | * shot. In practice we will often only send much less than this. |
| 49 | */ |
| 50 | #define FRAMES 4096 |
| 51 | |
| 52 | /** @brief Bytes to send per network packet |
| 53 | * |
| 54 | * Don't make this too big or arithmetic will start to overflow. |
| 55 | */ |
| 56 | #define NETWORK_BYTES (1024+sizeof(struct rtp_header)) |
| 57 | |
| 58 | /** @brief Maximum RTP playahead (ms) */ |
| 59 | #define RTP_AHEAD_MS 1000 |
| 60 | |
| 61 | /** @brief Maximum number of FDs to poll for */ |
| 62 | #define NFDS 256 |
| 63 | |
| 64 | /** @brief Track structure |
| 65 | * |
| 66 | * Known tracks are kept in a linked list. Usually there will be at most two |
| 67 | * of these but rearranging the queue can cause there to be more. |
| 68 | */ |
| 69 | struct track { |
| 70 | struct track *next; /* next track */ |
| 71 | int fd; /* input FD */ |
| 72 | char id[24]; /* ID */ |
| 73 | size_t start, used; /* start + bytes used */ |
| 74 | int eof; /* input is at EOF */ |
| 75 | int got_format; /* got format yet? */ |
| 76 | ao_sample_format format; /* sample format */ |
| 77 | unsigned long long played; /* number of frames played */ |
| 78 | char *buffer; /* sample buffer */ |
| 79 | size_t size; /* sample buffer size */ |
| 80 | int slot; /* poll array slot */ |
| 81 | }; |
| 82 | |
| 83 | /** @brief Structure of a backend */ |
| 84 | struct speaker_backend { |
| 85 | /** @brief Which backend this is |
| 86 | * |
| 87 | * @c -1 terminates the list. |
| 88 | */ |
| 89 | int backend; |
| 90 | |
| 91 | /** @brief Flags |
| 92 | * |
| 93 | * Possible values |
| 94 | * - @ref FIXED_FORMAT |
| 95 | */ |
| 96 | unsigned flags; |
| 97 | /** @brief Lock to configured sample format */ |
| 98 | #define FIXED_FORMAT 0x0001 |
| 99 | |
| 100 | /** @brief Initialization |
| 101 | * |
| 102 | * Called once at startup. This is responsible for one-time setup |
| 103 | * operations, for instance opening a network socket to transmit to. |
| 104 | * |
| 105 | * When writing to a native sound API this might @b not imply opening the |
| 106 | * native sound device - that might be done by @c activate below. |
| 107 | */ |
| 108 | void (*init)(void); |
| 109 | |
| 110 | /** @brief Activation |
| 111 | * @return 0 on success, non-0 on error |
| 112 | * |
| 113 | * Called to activate the output device. |
| 114 | * |
| 115 | * After this function succeeds, @ref ready should be non-0. As well as |
| 116 | * opening the audio device, this function is responsible for reconfiguring |
| 117 | * if it necessary to cope with different samples formats (for backends that |
| 118 | * don't demand a single fixed sample format for the lifetime of the server). |
| 119 | */ |
| 120 | int (*activate)(void); |
| 121 | |
| 122 | /** @brief Play sound |
| 123 | * @param frames Number of frames to play |
| 124 | * @return Number of frames actually played |
| 125 | */ |
| 126 | size_t (*play)(size_t frames); |
| 127 | |
| 128 | /** @brief Deactivation |
| 129 | * |
| 130 | * Called to deactivate the sound device. This is the inverse of |
| 131 | * @c activate above. |
| 132 | */ |
| 133 | void (*deactivate)(void); |
| 134 | |
| 135 | /** @brief Called before poll() |
| 136 | * |
| 137 | * Called before the call to poll(). Should call addfd() to update the FD |
| 138 | * array and stash the slot number somewhere safe. |
| 139 | */ |
| 140 | void (*beforepoll)(void); |
| 141 | |
| 142 | /** @brief Called after poll() |
| 143 | * @return 0 if we could play, non-0 if not |
| 144 | * |
| 145 | * Called after the call to poll(). Should arrange to play some audio if the |
| 146 | * output device is ready. |
| 147 | * |
| 148 | * The return value should be 0 if the device was ready to play, or nonzero |
| 149 | * if it was not. |
| 150 | */ |
| 151 | int (*afterpoll)(void); |
| 152 | }; |
| 153 | |
| 154 | /** @brief Linked list of all prepared tracks */ |
| 155 | extern struct track *tracks; |
| 156 | |
| 157 | /** @brief Playing track, or NULL */ |
| 158 | extern struct track *playing; |
| 159 | |
| 160 | #endif /* SPEAKER_H */ |
| 161 | |
| 162 | /* |
| 163 | Local Variables: |
| 164 | c-basic-offset:2 |
| 165 | comment-column:40 |
| 166 | fill-column:79 |
| 167 | indent-tabs-mode:nil |
| 168 | End: |
| 169 | */ |