* this header, the type of the current audio stream and the * type of this
* data chunk is coded.
*/
-#define UDP_AUDIO_HEADER_LEN 8
+#define UDP_AUDIO_HEADER_LEN 16
/** The possible stream types. */
enum udp_stream_type {
/** Used for mp3 and aac streams. */
UDP_PLAIN_STREAM,
- /* Ogg vorbis streams. */
- UDP_HEADER_STREAM
+ /** Ogg vorbis streams. */
+ UDP_HEADER_STREAM,
+ /** stream type not yet known. */
+ UDP_UNKNOWN_STREAM
};
/** The possible packet types. */
/** Combined header/data packet (ogg only). */
UDP_HEADER_PACKET,
/** Packet contains only audio file data. */
- UDP_DATA_PACKET
+ UDP_DATA_PACKET,
+ /** Invalid packet type. */
+ UDP_UNKNOWN_PACKET
+};
+
+/** The contents of an udp audio header. */
+struct udp_audio_header {
+ /** see \ref udp_stream_type. */
+ uint8_t stream_type;
+ /** see \ref udp_audio_packet_type. */
+ uint8_t packet_type;
+ /** Non-zero only for header packets. */
+ uint16_t header_len;
+ /** Length of header plus audio file data. */
+ uint16_t payload_len;
};
/**
- * Write the magic bytes to the beginning of a buffer.
+ * Write a struct udp_audio_header to a buffer.
+ *
+ * \param buf The buffer to write to.
+ * \param h The audio header to write.
+ *
+ * Used by the udp sender.
*
- * \param buf The buffer.
*/
-_static_inline_ void udp_write_magic(char *buf)
+_static_inline_ void write_udp_audio_header(char *buf, struct udp_audio_header *h)
{
memcpy(buf, "UDPM", 4);
+ write_u8(buf + 4, h->stream_type);
+ write_u8(buf + 5, h->packet_type);
+ write_u16(buf + 6, h->header_len);
+ write_u16(buf + 8, h->payload_len);
+ memset(buf + 10, 0, 6);
}
/**
- * Check whether this buffer contains the magic bytes.
+ * Used by the udp receiver to read a struct udp_audio_header from a buffer.
*
- * \param buf The buffer.
- * \param len The number of bytes of \a buf.
+ * \param buf The buffer to read from.
+ * \param len The length of \a buf.
+ * \param h Result pointer.
*
- * \return Positive if \a buf contains the magic bytes,
- * -1 otherwise.
+ * \return 1 if \a buf contains a valid udp audio header, -1 else.
*/
-_static_inline_ int udp_check_magic(char *buf, size_t len)
+_static_inline_ int read_udp_audio_header(char *buf, size_t len,
+ struct udp_audio_header *h)
{
if (len < 4)
- return -1;
+ goto err;
if (memcmp(buf, "UDPM", 4))
- return -1;
+ goto err;
+ h->stream_type = read_u8(buf + 4);
+ h->packet_type = read_u8(buf + 5);
+ h->header_len = read_u16(buf + 6);
+ h->payload_len = read_u16(buf + 8);
return 1;
-}
-
-/**
- * Write the type of the audio stream to a buffer.
- *
- * \param buf The buffer.
- * \param type The type to be written.
- *
- * \sa \ref udp_stream_type.
- */
-_static_inline_ void udp_write_stream_type(char *buf, uint8_t type)
-{
- write_u8(buf + 4, type);
-}
-
-/**
- * Read the type of the audio stream from a buffer.
- *
- * \param buf The buffer.
- *
- * \return Either UDP_PLAIN_STREAM or UDP_HEADER_STREAM.
- * \sa \ref udp_stream_type.
- */
-_static_inline_ uint8_t udp_read_stream_type(char *buf)
-{
- return read_u8(buf + 4);
-}
-
-/**
- * Write the type of this packet to a buffer.
- *
- * \param buf The buffer.
- * \param type The type to be written.
- *
- * \sa \ref udp_audio_packet_type.
- */
-_static_inline_ void udp_write_packet_type(char *buf, uint8_t type)
-{
- write_u8(buf + 5, type);
-}
-
-/**
- * Read the type of this buffer.
- *
- * \param buf The buffer.
- *
- * \return The packet type, see \ref udp_stream_type.
- */
-_static_inline_ uint8_t udp_read_packet_type(char *buf)
-{
- return read_u8(buf + 5);
-}
-
-/**
- * Write the length of the header (non-zero only for ogg streams).
- *
- * \param buf The buffer.
- * \param len The length of the header in bytes.
- */
-_static_inline_ void udp_write_header_len(char *buf, uint16_t len)
-{
- write_u16(buf + 6, len);
-}
-
-/**
- * Read the length of the header.
- *
- * \param buf The buffer.
- * \return The header length in bytes.
- */
-_static_inline_ uint16_t udp_read_header_len(char *buf)
-{
- return read_u16(buf + 6);
+err:
+ h->stream_type = UDP_UNKNOWN_STREAM;
+ h->packet_type = UDP_UNKNOWN_PACKET;
+ h->header_len = h->payload_len = 0;
+ return -1;
}