chiark / gitweb /
more conservative about read() result
[disorder] / lib / speaker-protocol.h
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2005, 2007 Richard Kettlewell
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20 /** @file lib/speaker-protocol.h
21  * @brief Speaker/server protocol support
22  *
23  * This file defines the protocol by which the main server and the speaker
24  * process communicate.
25  */
26
27 #ifndef SPEAKER_PROTOCOL_H
28 #define SPEAKER_PROTOCOL_H
29
30 /** @brief A message from the main server to the speaker, or vica versa */
31 struct speaker_message {
32   /** @brief Message type
33    *
34    * Messges from the main server:
35    * - @ref SM_PLAY
36    * - @ref SM_PAUSE
37    * - @ref SM_RESUME
38    * - @ref SM_CANCEL
39    * - @ref SM_RELOAD
40    *
41    * Messages from the speaker:
42    * - @ref SM_PAUSED
43    * - @ref SM_FINISHED
44    * - @ref SM_PLAYING
45    */
46   int type;
47
48   /** @brief Message-specific data */
49   long data;
50
51   /** @brief Track ID (including 0 terminator) */
52   char id[24];                          /* ID including terminator */
53 };
54
55 /* messages from the main DisOrder server */
56
57 /** @brief Play track @c id
58  *
59  * The track must already have been prepared.
60  */
61 #define SM_PLAY 1
62
63 /** @brief Pause current track */
64 #define SM_PAUSE 2
65
66 /** @brief Resume current track */
67 #define SM_RESUME 3
68
69 /** @brief Cancel track @c id */
70 #define SM_CANCEL 4
71
72 /** @brief Reload configuration */
73 #define SM_RELOAD 5
74
75 /* messages from the speaker */
76 /** @brief Paused track @c id, @c data seconds in
77  *
78  * There is no @c SM_RESUMED, instead @ref SM_PLAYING is sent after the track
79  * starts playing again.
80  */
81 #define SM_PAUSED 128
82
83 /** @brief Finished playing track @c id */
84 #define SM_FINISHED 129
85
86 /** @brief Currently track @c id, @c data seconds in
87  *
88  * This is sent from time to time while a track is playing.
89  */
90 #define SM_PLAYING 131
91
92 void speaker_send(int fd, const struct speaker_message *sm);
93 /* Send a message. */
94
95 int speaker_recv(int fd, struct speaker_message *sm);
96 /* Receive a message.  Return 0 on EOF, +ve if a message is read, -1 on EAGAIN,
97  * terminates on any other error. */
98
99 /** @brief One chunk in a stream */
100 struct stream_header {
101   /** @brief Frames per second */
102   uint32_t rate;
103
104   /** @brief Samples per frames */
105   uint8_t channels;
106
107   /** @brief Bits per sample */
108   uint8_t bits;
109
110   /** @brief Endianness */
111   uint8_t endian;
112 #define ENDIAN_BIG 1
113 #define ENDIAN_LITTLE 2
114 #ifdef WORDS_BIGENDIAN
115 # define ENDIAN_NATIVE ENDIAN_BIG
116 #else
117 # define ENDIAN_NATIVE ENDIAN_LITTLE
118 #endif
119   
120   /** @brief Number of bytes */
121   uint32_t nbytes;
122 } attribute((packed));
123
124 static inline int formats_equal(const struct stream_header *a,
125                                 const struct stream_header *b) {
126   return (a->rate == b->rate
127           && a->channels == b->channels
128           && a->bits == b->bits
129           && a->endian == b->endian);
130 }
131
132 #endif /* SPEAKER_PROTOCOL_H */
133
134 /*
135 Local Variables:
136 c-basic-offset:2
137 comment-column:40
138 fill-column:79
139 indent-tabs-mode:nil
140 End:
141 */