chiark / gitweb /
move speaker declarations to speaker.h
[disorder] / server / speaker.c
index 1cf9146cd8bd50767389557b175f8776468dd613..172671b0ec8d994ad8042899f667b297bb8f1421 100644 (file)
@@ -18,7 +18,7 @@
  * USA
  */
 /** @file server/speaker.c
- * @brief Speaker processs
+ * @brief Speaker process
  *
  * This program is responsible for transmitting a single coherent audio stream
  * to its destination (over the network, to some sound API, to some
 #include "log.h"
 #include "defs.h"
 #include "mem.h"
-#include "speaker.h"
+#include "speaker-protocol.h"
 #include "user.h"
 #include "addr.h"
 #include "timeval.h"
 #include "rtp.h"
+#include "speaker.h"
 
 #if API_ALSA
 #include <alsa/asoundlib.h>
 #endif
 
-#ifdef WORDS_BIGENDIAN
-# define MACHINE_AO_FMT AO_FMT_BIG
-#else
-# define MACHINE_AO_FMT AO_FMT_LITTLE
-#endif
+/** @brief Linked list of all prepared tracks */
+struct track *tracks;
 
-/** @brief How many seconds of input to buffer
- *
- * While any given connection has this much audio buffered, no more reads will
- * be issued for that connection.  The decoder will have to wait.
- */
-#define BUFFER_SECONDS 5
-
-/** @brief Frame batch size
- *
- * This controls how many frames are written in one go.
- *
- * For ALSA we request a buffer of three times this size and set the low
- * watermark to this amount.  The goal is then to keep between 1 and 3 times
- * this many frames in play.
- *
- * For all backends we attempt to play up to three times this many frames per
- * shot.  In practice we will often only send much less than this.
- */
-#define FRAMES 4096
-
-/** @brief Bytes to send per network packet
- *
- * Don't make this too big or arithmetic will start to overflow.
- */
-#define NETWORK_BYTES (1024+sizeof(struct rtp_header))
-
-/** @brief Maximum RTP playahead (ms) */
-#define RTP_AHEAD_MS 1000
-
-/** @brief Maximum number of FDs to poll for */
-#define NFDS 256
-
-/** @brief Track structure
- *
- * Known tracks are kept in a linked list.  Usually there will be at most two
- * of these but rearranging the queue can cause there to be more.
- */
-static struct track {
-  struct track *next;                   /* next track */
-  int fd;                               /* input FD */
-  char id[24];                          /* ID */
-  size_t start, used;                   /* start + bytes used */
-  int eof;                              /* input is at EOF */
-  int got_format;                       /* got format yet? */
-  ao_sample_format format;              /* sample format */
-  unsigned long long played;            /* number of frames played */
-  char *buffer;                         /* sample buffer */
-  size_t size;                          /* sample buffer size */
-  int slot;                             /* poll array slot */
-} *tracks, *playing;                    /* all tracks + playing track */
+/** @brief Playing track, or NULL */
+struct track *playing;
 
 static time_t last_report;              /* when we last reported */
 static int paused;                      /* pause status */
@@ -226,77 +176,6 @@ static int idled;                       /* set when idled */
 /** @brief Error counter */
 static int audio_errors;
 
-/** @brief Structure of a backend */
-struct speaker_backend {
-  /** @brief Which backend this is
-   *
-   * @c -1 terminates the list.
-   */
-  int backend;
-
-  /** @brief Flags
-   *
-   * Possible values
-   * - @ref FIXED_FORMAT
-   */
-  unsigned flags;
-/** @brief Lock to configured sample format */
-#define FIXED_FORMAT 0x0001
-  
-  /** @brief Initialization
-   *
-   * Called once at startup.  This is responsible for one-time setup
-   * operations, for instance opening a network socket to transmit to.
-   *
-   * When writing to a native sound API this might @b not imply opening the
-   * native sound device - that might be done by @c activate below.
-   */
-  void (*init)(void);
-
-  /** @brief Activation
-   * @return 0 on success, non-0 on error
-   *
-   * Called to activate the output device.
-   *
-   * After this function succeeds, @ref ready should be non-0.  As well as
-   * opening the audio device, this function is responsible for reconfiguring
-   * if it necessary to cope with different samples formats (for backends that
-   * don't demand a single fixed sample format for the lifetime of the server).
-   */
-  int (*activate)(void);
-
-  /** @brief Play sound
-   * @param frames Number of frames to play
-   * @return Number of frames actually played
-   */
-  size_t (*play)(size_t frames);
-  
-  /** @brief Deactivation
-   *
-   * Called to deactivate the sound device.  This is the inverse of
-   * @c activate above.
-   */
-  void (*deactivate)(void);
-
-  /** @brief Called before poll()
-   *
-   * Called before the call to poll().  Should call addfd() to update the FD
-   * array and stash the slot number somewhere safe.
-   */
-  void (*beforepoll)(void);
-
-  /** @brief Called after poll()
-   * @return 0 if we could play, non-0 if not
-   *
-   * Called after the call to poll().  Should arrange to play some audio if the
-   * output device is ready.
-   *
-   * The return value should be 0 if the device was ready to play, or nonzero
-   * if it was not.
-   */
-  int (*afterpoll)(void);
-};
-
 /** @brief Selected backend */
 static const struct speaker_backend *backend;
 
@@ -1251,19 +1130,19 @@ static const struct speaker_backend backends[] = {
  *
  * The playing of audio is more complicated however.
  *
- * On the first run through when a track is ready to be played, @c ready and
+ * On the first run through when a track is ready to be played, @ref ready and
  * @ref forceplay will both be zero.  Therefore @c beforepoll is not called.
  *
  * @c afterpoll on the other hand @b is called and will return nonzero.  The
  * result is that we call @c play(0).  This will call activate(), setting
- * @c ready nonzero, but otherwise has no immediate effect.
+ * @ref ready nonzero, but otherwise has no immediate effect.
  *
  * We then deal with stdin and the decoders.
  *
  * We then reach the second place we might play some audio.  @ref forceplay is
  * 0 so nothing happens here again.
  *
- * On the next iteration through however @c ready is nonzero, and @ref
+ * On the next iteration through however @ref ready is nonzero, and @ref
  * forceplay is 0, so we call @c beforepoll.  After the @c poll() we call @c
  * afterpoll and actually get some audio played.
  *