chiark / gitweb /
speaker protocol redesign to cope with libao re-opening
[disorder] / server / speaker.h
index 81c2ff4954b4b02529bd0248797110d9c5e2009c..9a48ca60690de70699215f7cec62bb267803456d 100644 (file)
 # define MACHINE_AO_FMT AO_FMT_LITTLE
 #endif
 
-/** @brief How many seconds of input to buffer
+/** @brief Minimum number of frames to try to play at once
  *
- * 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.
+ * The main loop will only attempt to play any audio when this many
+ * frames are available (or the current track has reached the end).
+ * The actual number of frames it attempts to play will often be
+ * larger than this (up to three times).
  *
  * 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
+ * For other 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
  * of these but rearranging the queue can cause there to be more.
  */
 struct track {
-  struct track *next;                   /* next track */
+  /** @brief Next track */
+  struct track *next;
+
+  /** @brief Input file descriptor */
   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 */
+
+  /** @brief Track ID */
+  char id[24];
+
+  /** @brief Start position of data in buffer */
+  size_t start;
+
+  /** @brief Number of bytes of data in buffer */
+  size_t used;
+
+  /** @brief Set @c fd is at EOF */
+  int eof;
+
+  /** @brief Total number of frames played */
+  unsigned long long played;
+
+  /** @brief Slot in @ref fds */
+  int slot;
+
+  /** @brief Input buffer
+   *
+   * 1Mbyte is enough for nearly 6s of 44100Hz 16-bit stereo
+   */
+  char buffer[1048576];
 };
 
 /** @brief Structure of a backend */
@@ -90,12 +104,9 @@ struct speaker_backend {
 
   /** @brief Flags
    *
-   * Possible values
-   * - @ref FIXED_FORMAT
+   * This field is currently not used and must be 0.
    */
   unsigned flags;
-/** @brief Lock to configured sample format */
-#define FIXED_FORMAT 0x0001
   
   /** @brief Initialization
    *
@@ -112,51 +123,106 @@ struct speaker_backend {
    *
    * 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).
+   * On input @ref device_state may be anything.  If it is @ref
+   * device_open then the device is already open but might be using
+   * the wrong sample format.  The device should be reconfigured to
+   * use the right sample format.
+   *
+   * If it is @ref device_error then a retry is underway and an
+   * attempt to recover or re-open the device (with the right sample
+   * format) should be made.
+   *
+   * If it is @ref device_closed then the device should be opened with
+   * the right sample format.
+   *
+   * Some devices are effectively always open and have no error state,
+   * in which case this callback can be NULL.  In this case @ref
+   * FIXED_FORMAT must be set.  Note that @ref device_state still
+   * switches between @ref device_open and @ref device_closed in this
+   * case.
    */
-  int (*activate)(void);
+  void (*activate)(void);
 
   /** @brief Play sound
    * @param frames Number of frames to play
    * @return Number of frames actually played
+   *
+   * If an error occurs (and it is not immediately recovered) this
+   * should set @ref device_state to @ref device_error.
    */
   size_t (*play)(size_t frames);
   
   /** @brief Deactivation
    *
-   * Called to deactivate the sound device.  This is the inverse of
-   * @c activate above.
+   * Called to deactivate the sound device.  This is the inverse of @c
+   * activate above.
+   *
+   * For sound devices that are open all the time and have no error
+   * state, this callback can be NULL.  Note that @ref device_state
+   * still switches between @ref device_open and @ref device_closed in
+   * this case.
    */
   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.
+   * Called before the call to poll().  Should call addfd() to update
+   * the FD array and stash the slot number somewhere safe.  This will
+   * only be called if @ref device_state = @ref device_open.
    */
   void (*beforepoll)(void);
 
   /** @brief Called after poll()
-   * @return 0 if we could play, non-0 if not
+   * @return 1 if output device ready for play, 0 otherwise
    *
-   * Called after the call to poll().  Should arrange to play some audio if the
-   * output device is ready.
+   * Called after the call to poll().  This will only be called if
+   * @ref device_state = @ref device_open.
    *
-   * The return value should be 0 if the device was ready to play, or nonzero
-   * if it was not.
+   * The return value should be 1 if the device was ready to play, or
+   * if it was not.
    */
-  int (*afterpoll)(void);
+  int (*ready)(void);
 };
 
-/** @brief Linked list of all prepared tracks */
-extern struct track *tracks;
+/** @brief Possible device states */
+enum device_states {
+  /** @brief The device is closed */
+  device_closed,
 
-/** @brief Playing track, or NULL */
+  /** @brief The device is open and ready to receive sound
+   *
+   * The current device sample format is potentially part of this state.
+   */
+  device_open,
+  
+  /** @brief An error has occurred on the device
+   *
+   * This state is used to ensure that a small interval is left
+   * between retrying the device.  If errors just set @ref
+   * device_closed then the main loop would busy-wait on broken output
+   * devices.
+   *
+   * The current device sample format is potentially part of this state.
+   */
+  device_error
+};
+
+extern enum device_states device_state;
+extern struct track *tracks;
 extern struct track *playing;
 
+extern const struct speaker_backend network_backend;
+extern const struct speaker_backend alsa_backend;
+extern const struct speaker_backend command_backend;
+
+extern struct pollfd fds[NFDS];
+extern int fdno;
+extern size_t bpf;
+extern int idled;
+
+int addfd(int fd, int events);
+void abandon(void);
+
 #endif /* SPEAKER_H */
 
 /*