Simplify udp_send.c.
[paraslash.git] / udp_header.h
1 /*
2  * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file udp_header.h some macros used by udp_send.c and udp_recv.c. */
8
9 /**
10  * Number of bytes of the paraslash udp header.
11  *
12  * The udp sender prepends a header at the beginning of each data chunk. Within
13  * this header, the type of the current audio stream and the * type of this
14  * data chunk is coded.
15  */
16 #define UDP_AUDIO_HEADER_LEN 8
17
18 /** The possible stream types. */
19 enum udp_stream_type {
20         /** Used for mp3 and aac streams. */
21         UDP_PLAIN_STREAM,
22         /* Ogg vorbis streams. */
23         UDP_HEADER_STREAM
24 };
25
26 /** The possible packet types. */
27 enum udp_audio_packet_type {
28         /** Beginning of file. */
29         UDP_BOF_PACKET,
30         /** End of file. */
31         UDP_EOF_PACKET,
32         /** Combined header/data packet (ogg only). */
33         UDP_HEADER_PACKET,
34         /** Packet contains only audio file data. */
35         UDP_DATA_PACKET
36 };
37
38 /**
39  * Write the magic bytes to the beginning of a buffer.
40  *
41  * \param buf The buffer.
42  */
43 _static_inline_ void udp_write_magic(char *buf)
44 {
45         memcpy(buf, "UDPM", 4);
46 }
47
48 /**
49  * Check whether this buffer contains the magic bytes.
50  *
51  * \param buf The buffer.
52  * \param len The number of bytes of \a buf.
53  *
54  * \return Positive if \a buf contains the magic bytes,
55  * -1 otherwise.
56  */
57 _static_inline_ int udp_check_magic(char *buf, size_t len)
58 {
59         if (len < 4)
60                 return -1;
61         if (memcmp(buf, "UDPM", 4))
62                 return -1;
63         return 1;
64 }
65
66 /**
67  * Write the type of the audio stream to a buffer.
68  *
69  * \param buf The buffer.
70  * \param type The type to be written.
71  *
72  * \sa \ref udp_stream_type.
73  */
74 _static_inline_ void udp_write_stream_type(char *buf, uint8_t type)
75 {
76         write_u8(buf + 4, type);
77 }
78
79 /**
80  * Read the type of the audio stream from a buffer.
81  *
82  * \param buf The buffer.
83  *
84  * \return Either UDP_PLAIN_STREAM or UDP_HEADER_STREAM.
85  * \sa \ref udp_stream_type.
86  */
87 _static_inline_ uint8_t udp_read_stream_type(char *buf)
88 {
89         return read_u8(buf + 4);
90 }
91
92 /**
93  * Write the type of this packet to a buffer.
94  *
95  * \param buf The buffer.
96  * \param type The type to be written.
97  *
98  * \sa \ref udp_audio_packet_type.
99  */
100 _static_inline_ void udp_write_packet_type(char *buf, uint8_t type)
101 {
102         write_u8(buf + 5, type);
103 }
104
105 /**
106  * Read the type of this buffer.
107  *
108  * \param buf The buffer.
109  *
110  * \return The packet type, see \ref udp_stream_type.
111  */
112 _static_inline_ uint8_t udp_read_packet_type(char *buf)
113 {
114         return read_u8(buf + 5);
115 }
116
117 /**
118  * Write the length of the header (non-zero only for ogg streams).
119  *
120  * \param buf The buffer.
121  * \param len The length of the header in bytes.
122  */
123 _static_inline_ void udp_write_header_len(char *buf, uint16_t len)
124 {
125         write_u16(buf + 6, len);
126 }
127
128 /**
129  * Read the length of the header.
130  *
131  * \param buf The buffer.
132  * \return The header length in bytes.
133  */
134 _static_inline_ uint16_t udp_read_header_len(char *buf)
135 {
136         return read_u16(buf + 6);
137 }