RetroArch
rsound.h
Go to the documentation of this file.
1 /* RSound - A PCM audio client/server
2  * Copyright (C) 2010-2014 - Hans-Kristian Arntzen
3  *
4  * RSound is free software: you can redistribute it and/or modify it under the terms
5  * of the GNU General Public License as published by the Free Software Found-
6  * ation, either version 3 of the License, or (at your option) any later version.
7  *
8  * RSound is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10  * PURPOSE. See the GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License along with RSound.
13  * If not, see <http://www.gnu.org/licenses/>.
14  */
15 
16 #ifndef __RSOUND_H
17 #define __RSOUND_H
18 
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include <rthreads/rthreads.h>
22 #include <sys/time.h>
23 #include <time.h>
24 #include <stdint.h>
25 #include <stddef.h>
26 
27 #include <retro_common_api.h>
28 #include <queues/fifo_queue.h>
29 
31 
32 #ifdef _WIN32
33 #define RSD_DEFAULT_HOST "127.0.0.1" /* Stupid Windows. */
34 #else
35 #define RSD_DEFAULT_HOST "localhost"
36 #endif
37 #define RSD_DEFAULT_PORT "12345"
38 #define RSD_DEFAULT_UNIX_SOCK "/tmp/rsound"
39 #define RSD_DEFAULT_OBJECT "rsound"
40 
41 #ifndef RSD_VERSION
42 #define RSD_VERSION "1.1"
43 #endif
44 
45 /* Feature tests */
46 #define RSD_SAMPLERATE RSD_SAMPLERATE
47 #define RSD_CHANNELS RSD_CHANNELS
48 #define RSD_HOST RSD_HOST
49 #define RSD_PORT RSD_PORT
50 #define RSD_BUFSIZE RSD_BUFSIZE
51 #define RSD_LATENCY RSD_LATENCY
52 #define RSD_FORMAT RSD_FORMAT
53 #define RSD_IDENTITY RSD_IDENTITY
54 
55 #define RSD_S16_LE RSD_S16_LE
56 #define RSD_S16_BE RSD_S16_BE
57 #define RSD_U16_LE RSD_U16_LE
58 #define RSD_U16_BE RSD_U16_BE
59 #define RSD_U8 RSD_U8
60 #define RSD_S8 RSD_S8
61 #define RSD_S16_NE RSD_S16_NE
62 #define RSD_U16_NE RSD_U16_NE
63 #define RSD_ALAW RSD_ALAW
64 #define RSD_MULAW RSD_MULAW
65 
66 #define RSD_S32_LE RSD_S32_LE
67 #define RSD_S32_BE RSD_S32_BE
68 #define RSD_S32_NE RSD_S32_NE
69 #define RSD_U32_LE RSD_U32_LE
70 #define RSD_U32_BE RSD_U32_BE
71 #define RSD_U32_NE RSD_U32_NE
72 
73 #define RSD_DELAY_MS RSD_DELAY_MS
74 #define RSD_SAMPLESIZE RSD_SAMPLESIZE
75 #define RSD_EXEC RSD_EXEC
76 #define RSD_SIMPLE_START RSD_SIMPLE_START
77 
78 #define RSD_NO_FMT RSD_NO_FMT
79 #define RSD_USES_OPAQUE_TYPE RSD_USES_OPAQUE_TYPE
80 #define RSD_USES_SAMPLESIZE_MEMBER RSD_USES_SAMPLESIZE_MEMBER
81 
82 #define RSD_AUDIO_CALLBACK_T RSD_AUDIO_CALLBACK_T
83 #define RSD_ERROR_CALLBACK_T RSD_ERROR_CALLBACK_T
84 #define RSD_SET_CALLBACK RSD_SET_CALLBACK
85 #define RSD_CALLBACK_LOCK RSD_CALLBACK_LOCK
86 #define RSD_CALLBACK_UNLOCK RSD_CALLBACK_UNLOCK
87 
88 /* End feature tests */
89 
90 /* Defines sample formats available. Defaults to S16_LE should it never be set. */
92 {
93  RSD_NO_FMT = 0x0000,
94  RSD_S16_LE = 0x0001,
95  RSD_S16_BE = 0x0002,
96  RSD_U16_LE = 0x0004,
97  RSD_U16_BE = 0x0008,
98  RSD_U8 = 0x0010,
99  RSD_S8 = 0x0020,
100  RSD_S16_NE = 0x0040,
101  RSD_U16_NE = 0x0080,
102  RSD_ALAW = 0x0100,
103  RSD_MULAW = 0x0200,
104  RSD_S32_LE = 0x0400,
105  RSD_S32_BE = 0x0800,
106  RSD_S32_NE = 0x1000,
107  RSD_U32_LE = 0x2000,
108  RSD_U32_BE = 0x4000,
109  RSD_U32_NE = 0x8000,
110 };
111 
112 /* Defines operations that can be used with rsd_set_param() */
114 {
123 };
124 
125 /* Audio callback for rsd_set_callback. Return -1 to trigger an error in the stream. */
126 typedef ssize_t (*rsd_audio_callback_t)(void *data, size_t bytes, void *userdata);
127 
128 /* Error callback. Signals caller that stream has been stopped,
129  * either by audio callback returning -1 or stream was hung up. */
130 typedef void (*rsd_error_callback_t)(void *userdata);
131 
132 /* Defines the main structure for use with the API. */
133 typedef struct rsound
134 {
135  struct
136  {
137  volatile int socket;
138  volatile int ctl_socket;
139  } conn;
140 
141  char *host;
142  char *port;
143  char *buffer; /* Obsolete, but kept for backwards header compatibility. */
145 
146  volatile int buffer_pointer; /* Obsolete, but kept for backwards header compatibility. */
147  size_t buffer_size;
149 
150  volatile int thread_active;
151 
154  volatile int has_written;
158 
159  struct
160  {
163  } backend_info;
164 
165  volatile int ready_for_data;
166 
171 
172  struct
173  {
178  } thread;
179 
180  char identity[256];
181 
184  size_t cb_max_size;
185  void *cb_data;
187 } rsound_t;
188 
189 /* -- API --
190  All functions (except for rsd_write() return 0 for success, and -1 for error. errno is currently not set. */
191 
192 /* Initializes an rsound_t structure. To make sure no memory leaks occur, you need to rsd_free() it after use.
193  A typical use of the API is as follows:
194  rsound_t *rd;
195  rsd_init(&rd);
196  rsd_set_param(rd, RSD_HOST, "foohost");
197  *sets more params*
198  rsd_start(rd);
199  rsd_write(rd, buf, size);
200  rsd_stop(rd);
201  rsd_free(rd);
202  */
203 int rsd_init (rsound_t **rd);
204 
205 /* This is a simpler function that initializes an rsound struct, sets params as given,
206  and starts the stream. Should this function fail, the structure will stay uninitialized.
207  Should NULL be passed in either host, port or ident, defaults will be used. */
208 
209 int rsd_simple_start (rsound_t **rd, const char* host, const char* port, const char* ident,
210  int rate, int channels, enum rsd_format format);
211 
212 /* Sets params associated with an rsound_t. These options (int options) include:
213 
214 RSD_HOST: Server to connect to. Expects (char *) in param.
215 If not set, will default to environmental variable RSD_SERVER or "localhost".
216 
217 RSD_PORT: Set port. Expects (char *) in param.
218 If not set, will default to environmental variable RSD_PORT or "12345".
219 
220 RSD_CHANNELS: Set number of audio channels. Expects (int *) in param. Mandatory.
221 
222 RSD_SAMPLERATE: Set samplerate of audio stream. Expects (int *) in param. Mandatory.
223 
224 RSD_BUFSIZE: Sets internal buffersize for the stream.
225 Might be overridden if too small.
226 Expects (int *) in param. Optional.
227 
228 RSD_LATENCY: Sets maximum audio latency in milliseconds,
229 (must be used with rsd_delay_wait() or this will have no effect).
230 Most applications do not need this.
231 Might be overridden if too small.
232 Expects (int *) in param. Optional.
233 
234 RSD_FORMAT: Sets sample format.
235 It defaults to S16_LE, so you probably will not use this.
236 Expects (int *) in param, with available values found in the format enum.
237 If invalid format is given, param might be changed to reflect the sample format the library will use.
238 
239 RSD_IDENTITY: Sets an identity string associated with the client.
240 Takes a (char *) parameter with the stream name.
241 Will be truncated if longer than 256 bytes.
242 
243 */
244 
245 int rsd_set_param (rsound_t *rd, enum rsd_settings option, void* param);
246 
247 /* Enables use of the callback interface. This must be set when stream is not active.
248  When callback is active, use of the blocking interface is disabled.
249  Only valid functions to call after rsd_start() is stopping the stream with either rsd_pause() or rsd_stop(). Calling any other function is undefined.
250  The callback is called at regular intervals and is asynchronous, so thread safety must be ensured by the caller.
251  If not enough data can be given to the callback, librsound will fill the rest of the callback data with silence.
252  librsound will attempt to obey latency information given with RSD_LATENCY as given before calling rsd_start().
253  max_size signifies the maximum size that will ever be requested by librsound. Set this to 0 to let librsound decide the maximum size.
254  Should an error occur to the stream, err_callback will be called, and the stream will be stopped. The stream can be started again.
255 
256  Callbacks can be disabled by setting callbacks to NULL. */
257 
258 void rsd_set_callback (rsound_t *rd, rsd_audio_callback_t callback, rsd_error_callback_t err_callback, size_t max_size, void *userdata);
259 
260 /* Lock and unlock the callback. When the callback lock is aquired, the callback is guaranteed to not be executing.
261  The lock has to be unlocked afterwards.
262  Attemping to call several rsd_callback_lock() in succession might cause a deadlock.
263  The lock should be held for as short period as possible.
264  Try to avoid calling code that may block when holding the lock. */
265 void rsd_callback_lock (rsound_t *rd);
266 
267 void rsd_callback_unlock (rsound_t *rd);
268 
269 /* Establishes connection to server. Might fail if connection can't be established or that one of
270  the mandatory options isn't set in rsd_set_param(). This needs to be called after params have been set
271  with rsd_set_param(), and before rsd_write(). */
272 int rsd_start (rsound_t *rd);
273 
274 /* Shuts down the rsound data structures, but returns the file descriptor associated with the connection.
275  The control socket will be shut down. If this function returns a negative number, the exec failed,
276  but the data structures will not be teared down.
277  Should a valid file descriptor be returned, it will always be blocking.
278  This call will block until all internal buffers have been sent to the network. */
279 int rsd_exec (rsound_t *rd);
280 
281 /* Disconnects from server. All audio data still in network buffer and other buffers will be dropped.
282  To continue playing, you will need to rsd_start() again. */
283 int rsd_stop (rsound_t *rd);
284 
285 /* Writes from buf to the internal buffer. Might fail if no connection is established,
286  or there was an unexpected error. This function will block until all data has
287  been written to the buffer. This function will return the number of bytes written to the buffer,
288  or 0 should it fail (disconnection from server). You will have to restart the stream again should this occur. */
289 size_t rsd_write (rsound_t *rd, const void* buf, size_t size);
290 
291 /* Gets the position of the buffer pointer.
292  Not really interesting for normal applications.
293  Might be useful for implementing rsound on top of other blocking APIs.
294  *NOTE* This function is deprecated, it should not be used in new applications. */
295 size_t rsd_pointer (rsound_t *rd);
296 
297 /* Aquires how much data can be written to the buffer without blocking */
298 size_t rsd_get_avail (rsound_t *rd);
299 
300 /* Aquires the latency at the moment for the audio stream. It is measured in bytes. Useful for syncing video and audio. */
301 size_t rsd_delay (rsound_t *rd);
302 
303 /* Utility for returning latency in milliseconds. */
304 size_t rsd_delay_ms (rsound_t *rd);
305 
306 /* Returns bytes per sample */
307 int rsd_samplesize(rsound_t *rd);
308 
309 /* Will sleep until latency of stream reaches maximum allowed latency defined earlier by rsd_set_param - RSD_LATENCY
310  Useful for hard headed blocking I/O design where user defined latency is needed. If rsd_set_param hasn't been set
311  with RSD_LATENCY, this function will do nothing. */
312 void rsd_delay_wait(rsound_t *rd);
313 
314 /* Pauses or unpauses a stream. pause -> enable = 1
315  This function essentially calls on start() and stop(). This behavior might be changed later. */
316 int rsd_pause (rsound_t *rd, int enable);
317 
318 /* Frees an rsound_t struct. Make sure that the stream is properly closed down with rsd_stop() before calling rsd_free(). */
319 int rsd_free (rsound_t *rd);
320 
322 
323 #endif
uint32_t latency
Definition: rsound.h:161
uint32_t chunk_size
Definition: rsound.h:162
#define RSD_U16_LE
Definition: rsound.h:57
volatile int ctl_socket
Definition: rsound.h:138
char * host
Definition: rsound.h:141
int rsd_simple_start(rsound_t **rd, const char *host, const char *port, const char *ident, int rate, int channels, enum rsd_format format)
Definition: librsound.c:1587
size_t rsd_delay(rsound_t *rd)
Definition: librsound.c:1525
#define RSD_HOST
Definition: rsound.h:48
#define RETRO_BEGIN_DECLS
Definition: retro_common_api.h:41
#define RSD_S32_NE
Definition: rsound.h:68
#define RSD_U32_BE
Definition: rsound.h:70
struct rsound::@1 backend_info
#define RSD_U16_NE
Definition: rsound.h:62
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:8418
size_t rsd_get_avail(rsound_t *rd)
Definition: librsound.c:1517
Definition: rthreads.c:88
int64_t start_time
Definition: rsound.h:153
uint32_t channels
Definition: rsound.h:168
char identity[256]
Definition: rsound.h:180
rsd_error_callback_t error_callback
Definition: rsound.h:183
#define RSD_S16_BE
Definition: rsound.h:56
GLsizeiptr size
Definition: glext.h:6559
#define RSD_IDENTITY
Definition: rsound.h:53
includes all by default used to find thumbnails Please choose a single playlist first Add Entry Add Folder Select Files< multiple > Please fill out all required fields RetroArch updated successfully Please restart the application for the changes to take effect Contributors Move Down Load Remove Add Pass No shader passes Reset All Passes Download thumbnail Start on Download All Thumbnails This Playlist Configured in port
Definition: msg_hash_us.h:7699
void * cb_data
Definition: rsound.h:185
GLboolean enable
Definition: glext.h:12027
slock_t * mutex
Definition: rsound.h:175
int samplesize
Definition: rsound.h:170
sthread_t * thread
Definition: rsound.h:174
rsd_audio_callback_t audio_callback
Definition: rsound.h:182
typedef void(__stdcall *PFN_DESTRUCTION_CALLBACK)(void *pData)
void rsd_delay_wait(rsound_t *rd)
Definition: librsound.c:1482
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:6303
struct rsound::@0 conn
typedefRETRO_BEGIN_DECLS struct sthread sthread_t
Definition: rthreads.h:35
#define RSD_S16_LE
Definition: rsound.h:55
#define RSD_FORMAT
Definition: rsound.h:52
void callback(s32 result, dvdcmdblk *block)
Definition: dvd.c:2293
char * port
Definition: rsound.h:142
rsd_settings
Definition: rsound.h:113
#define RSD_PORT
Definition: rsound.h:49
#define RSD_NO_FMT
Definition: rsound.h:78
#define RSD_MULAW
Definition: rsound.h:64
rsd_format
Definition: rsound.h:91
GLfloat param
Definition: glext.h:6480
#define RSD_S32_LE
Definition: rsound.h:66
#define RSD_U32_LE
Definition: rsound.h:69
int rsd_exec(rsound_t *rd)
Definition: librsound.c:1355
char * buffer
Definition: rsound.h:143
Definition: fifo_queue.h:35
fifo_buffer_t * fifo_buffer
Definition: rsound.h:148
volatile int buffer_pointer
Definition: rsound.h:146
uint16_t format
Definition: rsound.h:169
struct rsound rsound_t
volatile int socket
Definition: rsound.h:137
#define RSD_SAMPLERATE
Definition: rsound.h:46
size_t buffer_size
Definition: rsound.h:147
int rsd_stop(rsound_t *rd)
Definition: librsound.c:1300
#define RSD_U32_NE
Definition: rsound.h:71
Definition: rthreads.c:106
void rsd_callback_lock(rsound_t *rd)
Definition: librsound.c:1634
#define RSD_S8
Definition: rsound.h:60
#define RSD_S16_NE
Definition: rsound.h:61
#define RSD_U8
Definition: rsound.h:59
Definition: getopt.h:45
size_t rsd_pointer(rsound_t *rd)
Definition: librsound.c:1507
#define RETRO_END_DECLS
Definition: retro_common_api.h:42
size_t rsd_delay_ms(rsound_t *rd)
Definition: librsound.c:1535
#define RSD_LATENCY
Definition: rsound.h:51
volatile int thread_active
Definition: rsound.h:150
slock_t * cond_mutex
Definition: rsound.h:176
int max_latency
Definition: rsound.h:157
int bytes_in_buffer
Definition: rsound.h:155
int rsd_pause(rsound_t *rd, int enable)
Definition: librsound.c:1543
#define RSD_S32_BE
Definition: rsound.h:67
int delay_offset
Definition: rsound.h:156
void rsd_callback_unlock(rsound_t *rd)
Definition: librsound.c:1639
int rsd_start(rsound_t *rd)
Definition: librsound.c:1341
int rsd_set_param(rsound_t *rd, enum rsd_settings option, void *param)
Definition: librsound.c:1408
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: glext.h:6293
volatile int has_written
Definition: rsound.h:154
uint32_t rate
Definition: rsound.h:167
Definition: rsound.h:133
signed __int64 int64_t
Definition: stdint.h:135
size_t rsd_write(rsound_t *rd, const void *buf, size_t size)
Definition: librsound.c:1315
scond_t * cond
Definition: rsound.h:177
volatile int ready_for_data
Definition: rsound.h:165
int rsd_init(rsound_t **rd)
Definition: librsound.c:1552
int conn_type
Definition: rsound.h:144
#define RSD_BUFSIZE
Definition: rsound.h:50
#define RSD_U16_BE
Definition: rsound.h:58
int rsd_free(rsound_t *rd)
Definition: librsound.c:1644
ssize_t(* rsd_audio_callback_t)(void *data, size_t bytes, void *userdata)
Definition: rsound.h:126
void rsd_set_callback(rsound_t *rd, rsd_audio_callback_t callback, rsd_error_callback_t err_callback, size_t max_size, void *userdata)
Definition: librsound.c:1619
void(* rsd_error_callback_t)(void *userdata)
Definition: rsound.h:130
#define RSD_ALAW
Definition: rsound.h:63
slock_t * cb_lock
Definition: rsound.h:186
unsigned short uint16_t
Definition: stdint.h:125
unsigned int uint32_t
Definition: stdint.h:126
int rsd_samplesize(rsound_t *rd)
Definition: librsound.c:214
int64_t total_written
Definition: rsound.h:152
size_t cb_max_size
Definition: rsound.h:184
#define RSD_CHANNELS
Definition: rsound.h:47