Fix a typo in gui.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 16
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         /** stream type not yet known. */
25         UDP_UNKNOWN_STREAM
26 };
27
28 /** The possible packet types. */
29 enum udp_audio_packet_type {
30         /** Beginning of file. */
31         UDP_BOF_PACKET,
32         /** End of file. */
33         UDP_EOF_PACKET,
34         /** Combined header/data packet (ogg only). */
35         UDP_HEADER_PACKET,
36         /** Packet contains only audio file data. */
37         UDP_DATA_PACKET,
38         /** Invalid packet type. */
39         UDP_UNKNOWN_PACKET
40 };
41
42 /** The contents of an udp audio header. */
43 struct udp_audio_header {
44         /** see \ref udp_stream_type. */
45         uint8_t stream_type;
46         /** see \ref udp_audio_packet_type. */
47         uint8_t packet_type;
48         /** Non-zero only for header packets. */
49         uint16_t header_len;
50         /** Length of header plus audio file data. */
51         uint16_t payload_len;
52 };
53
54 /**
55  * Write a struct udp_audio_header to a buffer.
56  *
57  * \param buf The buffer to write to.
58  * \param h The audio header to write.
59  *
60  * Used by the udp sender.
61  *
62  */
63 _static_inline_ void write_udp_audio_header(char *buf, struct udp_audio_header *h)
64 {
65         memcpy(buf, "UDPM", 4);
66         write_u8(buf + 4, h->stream_type);
67         write_u8(buf + 5, h->packet_type);
68         write_u16(buf + 6, h->header_len);
69         write_u16(buf + 8, h->payload_len);
70         memset(buf + 10, 0, 6);
71 }
72
73 /**
74  * Used by the udp receiver to read a struct udp_audio_header from a buffer.
75  *
76  * \param buf The buffer to read from.
77  * \param len The length of \a buf.
78  * \param h Result pointer.
79  *
80  * \return 1 if \a buf contains a valid udp audio header, -1 else.
81  */
82 _static_inline_ int read_udp_audio_header(char *buf, size_t len,
83                 struct udp_audio_header *h)
84 {
85         if (len < 4)
86                 goto err;
87         if (memcmp(buf, "UDPM", 4))
88                 goto err;
89         h->stream_type = read_u8(buf + 4);
90         h->packet_type = read_u8(buf + 5);
91         h->header_len = read_u16(buf + 6);
92         h->payload_len = read_u16(buf + 8);
93         return 1;
94 err:
95         h->stream_type = UDP_UNKNOWN_STREAM;
96         h->packet_type = UDP_UNKNOWN_PACKET;
97         h->header_len = h->payload_len = 0;
98         return -1;
99 }