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