chiark / gitweb /
simplify RTP transmission
[disorder] / server / speaker.h
index 53230cb7cb220d6d1158edb1cba4eabb66ec818b..2786735c6d80c9e5a4e853496e90a9843e6a90a4 100644 (file)
 # define MACHINE_AO_FMT AO_FMT_LITTLE
 #endif
 
-/** @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 Minimum number of frames to try to play at once
  *
  * The main loop will only attempt to play any audio when this many
 #define FRAMES 4096
 
 /** @brief Bytes to send per network packet
+ *
+ * This is the maximum number of bytes we pass to write(2); to determine actual
+ * packet sizes, add a UDP header and an IP header (and a link layer header if
+ * it's the link layer size you care about).
  *
  * 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
+#define NETWORK_BYTES (1500-8/*UDP*/-40/*IP*/-8/*conservatism*/)
 
 /** @brief Maximum number of FDs to poll for */
 #define NFDS 256
  * 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 Set when playable
+   *
+   * A track becomes playable whenever it fills its buffer or reaches EOF; it
+   * stops being playable when it entirely empties its buffer.  Tracks start
+   * out life not playable.
+   */
+  int playable;
+  
+  /** @brief Input buffer
+   *
+   * 1Mbyte is enough for nearly 6s of 44100Hz 16-bit stereo
+   */
+  char buffer[1048576];
 };
 
 /** @brief Structure of a backend */
@@ -93,12 +113,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
    *
@@ -127,14 +144,9 @@ struct speaker_backend {
    * If it is @ref device_closed then the device should be opened with
    * the right sample format.
    *
-   * If the @ref FIXED_FORMAT flag is not set then @ref device_format
-   * must be set on success.
-   *
-   * 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_closd in this
-   * case.
+   * Some devices are effectively always open and have no error state, in which
+   * case this callback can be NULL.  Note that @ref device_state still
+   * switches between @ref device_open and @ref device_closed in this case.
    */
   void (*activate)(void);
 
@@ -154,18 +166,24 @@ struct speaker_backend {
    *
    * 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_closd in
+   * still switches between @ref device_open and @ref device_closed in
    * this case.
    */
   void (*deactivate)(void);
 
   /** @brief Called before poll()
+   * @param timeoutp Pointer to timeout
+   *
+   * Called before the call to poll().
+   *
+   * If desirable, should call addfd() to update the FD array and stash the
+   * slot number somewhere safe.  This will only be called if @ref device_state
+   * is @ref device_open.
    *
-   * 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.
+   * @p timeoutp points to the poll timeout value in milliseconds.  It may be
+   * reduced, but never increased.
    */
-  void (*beforepoll)(void);
+  void (*beforepoll)(int *timeoutp);
 
   /** @brief Called after poll()
    * @return 1 if output device ready for play, 0 otherwise
@@ -203,22 +221,21 @@ enum device_states {
 };
 
 extern enum device_states device_state;
-extern ao_sample_format device_format;
 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 const struct speaker_backend coreaudio_backend;
+extern const struct speaker_backend oss_backend;
 
 extern struct pollfd fds[NFDS];
 extern int fdno;
-extern size_t device_bpf;
+extern size_t bpf;
 extern int idled;
 
 int addfd(int fd, int events);
-int formats_equal(const ao_sample_format *a,
-                  const ao_sample_format *b);
 void abandon(void);
 
 #endif /* SPEAKER_H */