Commit Graph

1868 Commits

Author SHA1 Message Date
Max Kellermann 33aec0d673 client: added client_write() and client_puts()
client_write() writes a buffer to the client and buffers it if
required.  client_puts() does the same for a C string.  The next patch
will add more tools which will replace fdprintf() later.
2008-09-07 13:24:51 +02:00
Max Kellermann a34e1d2b84 include cleanup
As usual, include only headers which are really needed.
2008-09-06 20:28:31 +02:00
Eric Wong 092bdf3d32 tag: fix segfault on update
clearMpdTag could be called on a tag that was still in a
tag_begin_add transaction before tag_end_add is called.  This
was causing free() to attempt to operate on bulk.items; which is
un-free()-able.  Now instead we unmark the bulk.busy to avoid
committing the tags to the heap only to be immediately freed.

Additionally, we need to remember to call tag_end_add() when
a song is updated before we NULL song->tag to avoid tripping
an assertion the next time tag_begin_add() is called.
2008-09-06 15:31:55 +02:00
Max Kellermann 6146d4f5bb client: don't check FD_ISSET(client->fd) on expired client
client->fd becomes -1 when the client expires.  Don't use FD_ISSET()
with this expired client; doing so would cause a crash due to SIGBUS.
2008-09-06 15:31:55 +02:00
Max Kellermann 77b1671662 client: removed assert(client->fd)>=0
Since client->fd==-1 has become our "expired" flag, it may already be
-1 when client_close() is called.  Don't assert that it is still
non-negative, and call client_set_expired() instead.
2008-09-06 15:31:55 +02:00
Max Kellermann 35c0b84f08 dbUtils, playlist, directory: pass constant pointers
The usual bunch of const pointer conversions.
2008-09-06 15:28:31 +02:00
Max Kellermann d38d2bc353 tag: optimize tag_dup(), copy item references
Don't call tag_pool_get_item() for duplicating tags, just increase the
item's reference counter instead.
2008-08-29 15:04:49 +02:00
Max Kellermann d8ad109e10 oggflac: fix GCC warnings
Fix lots of "unused parameter" warnings in the OggFLAC decoder
plugin.  Not sure if anybody uses it anymore, since newer libflac
obsoletes it.
2008-08-29 15:03:09 +02:00
Max Kellermann 01f9684f70 tag: fix the shout and oggflac plugins
During the tag library refactoring, the shout plugin was disabled, and
I forgot about adapting it to the new API.  Apply the same fixes to
the oggflac decoder plugin.
2008-08-29 15:02:49 +02:00
Max Kellermann 37d77caa3c const pointers
Yet another patch which converts pointer arguments to "const".
2008-08-29 14:48:39 +02:00
Max Kellermann 5bd5551630 tag: static directory name
While parsing the tag cache, don't allocate the directory name from
the heap, but copy it into a buffer on the stack.  This reduces heap
fragmentation by 1%.
2008-08-29 09:39:12 +02:00
Max Kellermann 1aa3457346 tag: try not to reallocate tag.items in every add() call
If many tag_items are added at once while the tag cache is being
loaded, manage these items in a static fixed list, instead of
reallocating the list with every newly created item.  This reduces
heap fragmentation.

Massif results again:

 mk before:  total 12,837,632; useful 10,626,383; extra 2,211,249
 mk now:     total 12,736,720; useful 10,626,383; extra 2,110,337

The "useful" value is the same since this patch only changes the way
we allocate the same amount of memory, but heap fragmentation was
reduced by 5%.
2008-08-29 09:39:08 +02:00
Max Kellermann 031522060a song: don't export newNullSong()
The function newNullSong() is only used internally in song.c.
2008-08-29 09:39:07 +02:00
Max Kellermann a208d654b3 tag: try not to duplicate the input string
Try to detect if the string needs Latin1-UTF8 conversion, or
whitespace cleanup.  If not, we don't need to allocate temporary
memory, leading to decreased heap fragmentation.
2008-08-29 09:39:04 +02:00
Max Kellermann 9352fc9e66 tag: pass length to fix_utf8()
Same as the previous patch, prepare the function fix_utf8() this time.
2008-08-29 09:39:01 +02:00
Max Kellermann 43c389b961 added "length" parameter to validUtf8String()
At several places, we create temporary copies of non-null-terminated
strings, just to use them in functions like validUtf8String().  We can
save this temporary allocation and avoid heap fragmentation if we
add a length parameter instead of expecting a null-terminated string.
2008-08-29 09:38:58 +02:00
Max Kellermann 92b757674e assert value!=NULL in fix_utf8()
We must never pass value==NULL to fix_utf().  Replace the run-time
check with an assertion.
2008-08-29 09:38:56 +02:00
Max Kellermann f99fe80811 tag: converted macro fixUtf8() to an inline function
Since the inline function cannot modify its caller's variables (which
is a good thing for code readability), the new string pointer is the
return value.  The resulting binary should be the same as with the
macro.
2008-08-29 09:38:54 +02:00
Max Kellermann c855415c73 tag: added a pool for tag items
The new source tag_pool.c manages a pool of reference counted tag_item
objects.  This is used to merge tag items of the same type and value,
saving lots of memory.  Formerly, only the value itself was pooled,
wasting memory for all the pointers and tag_item structs.

The following results were measured with massif.  Started MPD on
amd64, typed "mpc", no song being played.  My music database contains
35k tagged songs.  The results are what massif reports as "peak".

 0.13.2:     total 14,131,392; useful 11,408,972; extra 2,722,420
 eric:       total 18,370,696; useful 15,648,182; extra 2,722,514
 mk f34f694: total 15,833,952; useful 13,111,470; extra 2,722,482
 mk now:     total 12,837,632; useful 10,626,383; extra 2,211,249

This patch set saves 20% memory, and does a good job in reducing heap
fragmentation.
2008-08-29 09:38:37 +02:00
Max Kellermann e5a7879892 tag: converted tag_item.value to a char array
The value is stored in the same memory allocation as the tag_item
struct; this saves memory because we do not store the value pointer
anymore.  Also remove the getTagItemString()/removeTagItemString()
dummies.
2008-08-29 09:38:33 +02:00
Max Kellermann b731bbe93a removed tree.c
This patch makes MPD consume much more memory because string pooling
is disabled, but it prepares the next bunch of patches.  Replace the
code in tagTracker.c with naive algorithms without the tree code.  For
now, this should do; later we should find better algorithms,
especially for getNumberOfTagItems(), which has become wasteful with
temporary memory.
2008-08-29 09:38:31 +02:00
Max Kellermann ad0e09b2db tag: converted MpdTag.items to a pointer list
This prepares the following patches, which aim to reduce MPD's memory
usage: we plan to share tag_item instances, instead of just their
values.
2008-08-29 09:38:29 +02:00
Max Kellermann 6f72fe3ecf tag: moved code to tag_id3.c
The ID3 code uses only the public tag API, but is otherwise
unrelated.  Move it to a separate source file.
2008-08-29 09:38:27 +02:00
Max Kellermann 055f4a41c5 wavpack: tag_new() cannot fail
Since tag_new() uses xmalloc(), it cannot fail - if we're really out
of memory, the process will abort.
2008-08-29 09:38:25 +02:00
Max Kellermann 5e1feb8fa3 tag: converted tag_add_item() to an inline function 2008-08-29 09:38:24 +02:00
Max Kellermann 91502cd71e tag: renamed functions, no CamelCase 2008-08-29 09:38:21 +02:00
Max Kellermann d0556dc983 tag: renamed MpdTag and MpdTagItem to struct tag, struct mpd_tag_item
Getting rid of CamelCase; not having typedefs also allows us to
forward-declare the structures.
2008-08-29 09:38:11 +02:00
Max Kellermann f42de62aa2 added xfree() which takes a const pointer
Unfortunately, the C standard postulates that the argument to free()
must be non-const.  This does not makes sense, and virtually prevents
every pointer which must be freed at some time to be non-const.  Use
the deconst hack (sorry for that) to allow us to free constant
pointers.
2008-08-29 09:38:08 +02:00
Max Kellermann 8811c0e059 export the function client_is_expired()
Instead of passing the pointer to the "expired" flag to
processListOfCommands(), this function should use the client API to
check this flag.  We can now remove the "global_expired" hack
introduced recently.
2008-08-29 09:37:11 +02:00
Max Kellermann 8b1b82b363 client: pass the client struct to processCommand()
Start exporting the client struct as an opaque struct.  For now, pass
it only to processCommand() and processListOfCommands(), and provide a
function to extract the socket handle.  Later, we will propagate the
pointer to all command implementations, and of course to
client_print() etc.
2008-08-29 09:36:42 +02:00
Max Kellermann 2c8aa8efde client: reorder function declarations
Change the order of function declarations in client.h, to make it well
arranged and readable.
2008-08-29 09:36:40 +02:00
Max Kellermann e743d71b89 client: check "expired" after command execution
The old code tried to write a response to the client, without even
checking if it was already closed.  Now that we have added more
assertions, these may fail...  perform the "expired" check earlier.
2008-08-29 09:36:40 +02:00
Max Kellermann 76ecc30243 client: added global "expired" flag
Patch bdeb8e14 ("client: moved "expired" accesses into inline
function") was created under the wrong assumption that
processListOfCommands() could modify the expired flag, which is not
the case.  Although "expired" is a non-const pointer,
processListOfCommands() just reads it, using it as the break condition
in a "while" loop.  I will address this issue with a better overall
solution, but for now provide a pointer to a global "expired" flag.
2008-08-29 09:36:38 +02:00
Max Kellermann 12bcba8b89 pass constant pointers
And again, convert arguments to const.
2008-08-29 09:01:53 +02:00
Max Kellermann d8a8fa63b4 client: removed superfluous assertion
client_defer_output() was modified so that it can create the
deferred_send list.  With this patch, the assertion on
"deferred_send!=NULL" has become invalid.  Remove it.
2008-08-29 06:17:54 +02:00
Eric Wong 7858081eda log.c: thread-safety for warning log
I'm really no fan of the warning log, it's too complex
for how little it gets used; but fixing it is another
problem.
2008-08-28 20:40:26 +02:00
Max Kellermann 08c9de7b68 client: more assertions 2008-08-28 20:23:22 +02:00
Max Kellermann 4e17ab11a8 client: replace "expired" flag with fd==-1
Why waste 4 bytes for a flag which we can hide in another variable.
2008-08-28 20:20:10 +02:00
Max Kellermann bdeb8e148e client: moved "expired" accesses into inline function
Hiding this flag allows us later to remove it easily.
2008-08-28 20:20:10 +02:00
Max Kellermann e0fd63ecf8 moved code to sockaddr_to_tmp_string()
Unclutter the client_new() constructor by moving unrelated complex
code into a separate function.
2008-08-28 20:20:10 +02:00
Max Kellermann 34735fab66 client: no while loop in client_manager_io()
The last patch removed the "continue" directive, and now the while
loop is without function.  Remove it.  Also make client_manager_io()
return 0.
2008-08-28 20:20:10 +02:00
Max Kellermann f6a7dd2b45 client: select() errors are fatal
Previously, when select() failed, we assumed that there was an invalid
file descriptor in one of the client structs.  Thus we tried select()
one by one.  This is bogus, because we should never have invalid file
descriptors.  Remove it, and make select() errors fatal.
2008-08-28 20:20:10 +02:00
Max Kellermann 32bb19d03f client: use client_defer_output() in client_write()
Eliminate duplicated code, call client_defer_output() which we
splitted from client_write_output() earlier.
2008-08-28 20:20:04 +02:00
Max Kellermann 709b795e4a moved code to client_write()
Move the second part of client_write_output() into a separate
function.
2008-08-28 20:03:58 +02:00
Max Kellermann 4516bd117c client: client_defer_output() can create the first defer buffer
client_defer_output() was designed to add new buffers to an existing
deferred_send buffer.  Tweak it and allow it to create a new buffer
list.
2008-08-28 20:03:56 +02:00
Max Kellermann 20f06162dd client: return early on error in client_defer_output()
Exit the function when an error occurs, and move the rest of the
following code one indent level left.
2008-08-28 20:03:54 +02:00
Max Kellermann 7774cd2774 client: moved code to client_defer_output()
Split the large function client_write_output() into two parts; this is
the first code moving patch.
2008-08-28 20:03:51 +02:00
Max Kellermann 4448b17e2e don't free client resources except in client_close()
All of the client's resources are freed in client_close().  It is
enough to set the "expired" flag, no need to duplicate lots of
destruction code again and again.
2008-08-28 20:03:49 +02:00
Max Kellermann 61443c13e6 client: allocate clients dynamically
Due to the large buffers in the client struct, the static client array
eats several megabytes of RAM with a maximum of only 10 clients.  Stop
this waste and allocate each client struct from the heap.
2008-08-28 20:03:48 +02:00
Max Kellermann a091c148e6 client: added function client_by_fd()
The code becomes less complex and more readable when we move this
linear search into a separate mini function.
2008-08-28 20:03:06 +02:00
Max Kellermann d15e1e09a2 client: return early in client_new()
This saves one level of indent.
2008-08-28 20:03:03 +02:00
Max Kellermann c0197c58ba client: renamed all public functions
Functions which operate on the whole client list are prefixed with
"client_manager_", and functions which handle just one client just get
"client_".
2008-08-28 20:03:02 +02:00
Max Kellermann 6d18e00099 client: renamed internal functions and variables
Rename all static functions, variables and macros which have
"interface" in their name to something nicer prefixed with "client_".
2008-08-28 20:02:59 +02:00
Max Kellermann fe03cb9549 client: renamed Interface to struct client
Second patch: rename the internal struct name.  We will eventually
export this type as an opaque forward-declared struct later, so we
can pass a struct pointer instead of a file descriptor, which would
save us an expensive linear lookup.
2008-08-28 20:02:58 +02:00
Max Kellermann deb29e0884 renamed interface.c to client.c
I don't believe "interface" is a good name for something like
"connection by a client to MPD", let's call it "client".  This is the
first patch in the series which changes the name, beginning with the
file name.
2008-08-28 20:02:43 +02:00
Max Kellermann 9340e291f0 imported list.h from the Linux kernel sources
linux/list.h is a nice doubly linked list library - it is lightweight
and powerful at the same time.  It will be useful later, when we begin
to allocate client structures dynamically.  Import it, and strip out
all the stuff which we are not going to use.
2008-08-28 20:02:20 +02:00
Max Kellermann bc1c8835c6 const pointers
The usual bunch of pointer arguments which should be const.
2008-08-28 20:02:17 +02:00
Max Kellermann 801c71ed1c unsigned integers and size_t
Use "unsigned int" whenever negative values are not meaningful.  Use
size_t whenever we are going to describe buffer sizes.
2008-08-28 20:02:16 +02:00
Max Kellermann 1560a70b01 include cleanup
Only include headers which are really needed.
2008-08-28 20:01:08 +02:00
Max Kellermann b7861868cf protect locate.h from double inclusion 2008-08-28 20:01:08 +02:00
Max Kellermann f34f694ec3 moved player_command_finished() to player_thread.c 2008-08-26 08:45:15 +02:00
Max Kellermann e2c8b960de moved code to pc_init(), dc_init() 2008-08-26 08:45:14 +02:00
Max Kellermann 5e51fa020d renamed player.c to player_control.c
Give player.c a better name, meaning that the code is used to control
the player thread.
2008-08-26 08:44:38 +02:00
Max Kellermann b616dff77d no commandError() in playerSeek()
We should avoid having protocol specific code in player.c.  Just
return success or failure, and let the caller send the error code to
the MPD client.
2008-08-26 08:44:34 +02:00
Max Kellermann bf4af19f54 don't initialize "i" if we overwrite it anyway 2008-08-26 08:44:33 +02:00
Max Kellermann a2b24462e8 renamed decoderInit() to decoder_thread_start() 2008-08-26 08:44:29 +02:00
Max Kellermann 56cdce6946 renamed decode.h to decoder_control.h 2008-08-26 08:44:19 +02:00
Max Kellermann dff8c6450b renamed decode.c to decoder_thread.c
It should be obvious in which thread or context a function is being
executed at runtime.  The code which was left in decode.c is for the
decoder thread itself; give the file a better name.
2008-08-26 08:44:12 +02:00
Max Kellermann 4255bba0f7 moved global variable "pc" to player.h
This is the last of the three variables.  Now we don't need
playerData.h anymore in most sources.
2008-08-26 08:41:05 +02:00
Max Kellermann a94845ee00 moved global variable "ob" to outputBuffer.h
This releases several include file dependencies.  As a side effect,
"CHUNK_SIZE" isn't defined by decoder_api.h anymore, so we have to
define it directly in the plugins which need it.  It just isn't worth
it to add it to the decoder plugin API.
2008-08-26 08:41:05 +02:00
Max Kellermann 15c9352bb6 moved enum decoder_command to decoder_api.h
The decoder plugins need this type, so export it in the public API.
This allows is to remove "decode.h" from "decoder_api.h", uncluttering
the API namespace some more.
2008-08-26 08:41:05 +02:00
Max Kellermann 1c03c721ea moved variable "dc" to decode.h
Now that "dc" is available here, we don't have to pass it to
decoder_is_idle() and decoder_is_starting() anymore.
2008-08-26 08:40:47 +02:00
Max Kellermann 9521c92f66 player_thread: removed decode(), renamed decodeParent()
decode() is a trivial wrapper for decodeParent().  Merge both and
rename them to do_play().
2008-08-26 08:29:37 +02:00
Max Kellermann 70904adf18 hide DecoderControl accesses in inline functions
Unfortunately, we have to pass the DecoderControl pointer to these
inline functions, because the global variable "dc" may not be
available here.  This will be fixed later.
2008-08-26 08:29:35 +02:00
Max Kellermann a1ce999978 check for decoder error before state!=START
When dc->error!=NOERROR, we do not need to check state!=START.
Simplify the checks by moving the error check to the top.
2008-08-26 08:27:18 +02:00
Max Kellermann c6035ea332 don't reset dc->command in quitDecode()
The decoder thread is responsible for resetting dc->command after a
command was executed.  As a consequence, we can assume that
dc->command is already NONE after decoder_stop().
2008-08-26 08:27:18 +02:00
Max Kellermann 8a4970f863 added decoder_control.c
The source "decoder_control.c" provides an API for controlling the
decoder.  This replaces various direct accesses to the DecoderControl
struct.
2008-08-26 08:27:18 +02:00
Max Kellermann 7d3429a6d8 removed the "queue" parameter from syncPlaylistWithQueue()
There is only one caller which passes "true", so we can move the
queueNextSongInPlaylist() invocation there.
2008-08-26 08:27:18 +02:00
Max Kellermann a9f0d85115 merge the playlist lock functions into clearPlayerQueue()
There is no unlocked caller of clearPlayerQueue(), and the functions
lockPlaylistInteraction() and unlockPlaylistInteraction() are trivial
- merge them.
2008-08-26 08:27:17 +02:00
Max Kellermann 2a06e2dafa use switch/case in syncPlaylistWithQueue() 2008-08-26 08:27:17 +02:00
Max Kellermann 110cef6fda don't call playerStop() before playerPlay()
Since playerPlay() already calls playerStop(), we can remove its
invocation of playerStop() from playPlaylistOrderNumber().

We can also make playerStop a static function.
2008-08-26 08:27:17 +02:00
Max Kellermann 7125fdc4f2 assert locked/unlocked in queue lock functions
There are no nested queue locks in mpd, thus replace the locked checks
in playerQueueLock(), playerQueueUnlock() with assertions.
2008-08-26 08:27:17 +02:00
Max Kellermann f168695699 don't unlock player queue in playerStop(), playerWait()
There is no caller of these two functions which locks the player
queue; replace the playerQueueUnlock() call with an assertion.
2008-08-26 08:27:17 +02:00
Max Kellermann 113c1c0af5 queueSong() cannot fail
All (indirect) callers of queueSong() ensure that the queue state is
BLANK, so there is no need to check it in queueSong() again.  As a
side effect, queueSong() cannot fail anymore, and can return void.
Also, playlist_queueError and all its error handling can go away.
2008-08-26 08:27:17 +02:00
Max Kellermann b94af79134 fix a comment regarding the player queue 2008-08-26 08:27:17 +02:00
Max Kellermann 4613115bec document the PLAYER_QUEUE_ constants 2008-08-26 08:27:17 +02:00
Max Kellermann 55377fbd9f added enum player_queue_state 2008-08-26 08:27:16 +02:00
Max Kellermann ae00330285 rewrote playerKill()
playerKill() was marked as deprecated, but it seems like a good idea
to do proper cleanup in all threads (e.g. for usable valgrind
results).  Introduce the command "EXIT" which makes the player thread
exit cleanly.
2008-08-26 08:27:16 +02:00
Max Kellermann 92d9797b8c player: don't call STOP before CLOSE_AUDIO
playerWait() stops the player thread (twice!) and closes the output
device.  It should be well enough to just send CLOSE_AUDIO, without
STOP.

This requires a tiny change to the player thread code: make it break
when CLOSE_AUDIO is sent.
2008-08-26 08:27:16 +02:00
Max Kellermann cff923b9d8 playlist: eliminate unused fd parameters
Again, remove file descriptor parameters, which are not actually
used.  These functions can also be converted to return void.
2008-08-26 08:27:16 +02:00
Max Kellermann 3db333b5a4 player: no "fd" and no return value
Most player*() functions do not actually use the file descriptor, and
always return 0 (success).  Eliminate them to get a leaner interface.
2008-08-26 08:27:16 +02:00
Max Kellermann 6df980a996 flac: decoder command means EOF
It was possible for the decoder thread to go into an endless loop
(flac and oggflac decoders): when a "STOP" command arrived, the Read()
callback would return 0, but the EOF() callback returned false.  Fix:
when decoder_get_command()!=NONE, return EOF==true.
2008-08-26 08:27:16 +02:00
Max Kellermann 8a5109483d made "sample_size" static const
sample_size is a variable which is computed at compile time.  Declare
it "static const", so the compiler can optimize it away.
2008-08-26 08:27:16 +02:00
Max Kellermann e056ff2b02 moved jack configuration to the JackData struct
Storing local configuration in global (static) variables is obviously
a bad idea.  Move all those variables into the JackData struct,
including the locks.
2008-08-26 08:27:16 +02:00
Max Kellermann 479d02da0e jack: removed unused macros 2008-08-26 08:27:15 +02:00
Max Kellermann edd7e2f94e jack: don't set audioOutput->data=NULL
There is only one caller of freeJackData() left: jack_finishDriver().
This function is called by the mpd core, and is called exactly once
for every successful jack_initDriver().  We do not need to clear
audioOutput->data, since this variable is invalidated anyway.
2008-08-26 08:27:15 +02:00
Max Kellermann 806a9f02a1 jack: initialize JackData in jack_initDriver()
Over the lifetime of the jack AudioOutput object, we want a single
valid JackData object, so we can persistently store data there
(configuration etc.).  Allocate JackData in jack_initDriver().  After
that, we can safely remove all audioOutput->data==NULL checks (and
replace them with assertions).
2008-08-26 08:27:15 +02:00
Max Kellermann 83215bf9ce jack: added freeJackClient()
No need to destroy the JackData object when an error occurs, since
jack_finishDriver() already frees it.  Only deinitialize the jack
library, introduce freeJackClient() for that, and move code from
freeJackData().
2008-08-26 08:27:15 +02:00
Max Kellermann dc989987ab jack: initialize jd->client after !jd check
Prepare the next patch: make the "!jd" check independent of the
jd->client initialization.  This way we can change the "jd"
initialization semantics later.
2008-08-26 08:27:15 +02:00
Max Kellermann 2383231123 jack: eliminate superfluous freeJackData() calls
connect_jack() invokes freeJackData() in every error handler, although
its caller also invokes this function after a failure.  We can save a
lot of lines in connect_jack() by removing these redundant
freeJackData() invocations.
2008-08-26 08:27:15 +02:00
Max Kellermann f46de2c32f mp3, flac: check for seek command after decoder_read()
When we introduced decoder_read(), we added code which aborts the read
operation when a decoder command arrives.  Several plugins however did
not expect that when they were converted to decoder_read().  Add
proper checks to the mp3 and flac decoder plugins.
2008-08-26 08:27:15 +02:00
Max Kellermann e530181e23 check decoder_command!=NONE instead of decoder_command==STOP
The code said "decoder_command==STOP" because that was a conversion
from the old "dc->stop" test.  As we can now check for all commands in
one test, we can simply rewrite that to decoder_command!=NONE.
2008-08-26 08:27:15 +02:00
Max Kellermann 4515ac5ecb mp3: converted the MUTEFRAME_ macros to an enum
Also introduce MUTEFRAME_NONE; previously, the code used "0".
2008-08-26 08:27:14 +02:00
Max Kellermann 95fff55d7e mp3: converted the DECODE_ constants to an enum 2008-08-26 08:27:14 +02:00
Max Kellermann 1c196478b6 added flag "decoder.seeking"
This flag is used internally; it is set by decoder_seek_where(), and
indicates that the decoder plugin has begun the seek process.  It is
used for the case that the decoder plugin has to read data during the
seek process.  Before this patch, that was impossible, because
decoder_read() would refuse to read data unless dc->command is NONE.
This patch is kind of a dirty workaround, and needs to be redesigned
later.
2008-08-26 08:27:14 +02:00
Max Kellermann cf139dc012 wavpack: don't use "isp" before initialization
The old code called can_seek() with the uninitialized pointer
"isp.is".  Has this ever worked?  Anyway, initialize "isp" first, then
call can_seek(&isp).
2008-08-26 08:27:14 +02:00
Max Kellermann 2e822a577d wavpack: moved code to wavpack_open_wvc()
Move everything related to finding and initializing the WVC stream to
wavpack_open_wvc().  This greatly simplifies its error handling and
the function wavpack_streamdecode().
2008-08-26 08:27:14 +02:00
Max Kellermann af58de6543 simplified code in the ogg decoder plugin
Return early when the player thread sent us a command.  This saves one
level of indentation.
2008-08-26 08:27:14 +02:00
Max Kellermann 940ecf5345 added decoder_read()
On our way to stabilize the decoder API, we will one day remove the
input stream functions.  The most basic function, read() will be
provided by decoder_api.h with this patch.  It already contains a loop
(still with manual polling), error/eof handling and decoder command
checks.  This kind of code used to be duplicated in all decoder
plugins.
2008-08-26 08:27:14 +02:00
Max Kellermann d80260ab4e wavpack: added InputStreamPlus.decoder
The "decoder" object reference will be used by another patch.
2008-08-26 08:27:14 +02:00
Max Kellermann a1b430cb88 oggvorbis: don't detect OGG header if stream is not seekable
If the input stream is not seekable, the try_decode() function
consumes valuable data, which is not available to the decode()
function anymore.  This means that the decode() function does not
parse the header correctly.  Better skip the detection if we cannot
seek.  Or implement better buffering, something like unread() or
buffered rewind().
2008-08-26 08:27:13 +02:00
Max Kellermann 7bbca0842d added AacBuffer.decoder
We need the decoder object at several places in the AAC plugin.  Add
it to mp3DecodeData, so we don't have to pass it around in every
function.
2008-08-26 08:27:13 +02:00
Max Kellermann 468f61d587 mp3: added mp3DecodeData.decoder
We need the decoder object at several places in the mp3 plugin.  Add
it to mp3DecodeData, so we don't have to pass it around in every
function.
2008-08-26 08:27:13 +02:00
Max Kellermann 7653ab434e mp3: audio_linear_dither() returns mpd_sint16
The return value of audio_linear_dither() is always casted to
mpd_sint16.  Returning long does not make sense, and consumed 8 bytes
on a 64 bit platform.
2008-08-26 08:27:13 +02:00
Max Kellermann 9c823d67a7 mp3: changed outputBuffer's type to mpd_sint16[]
The output buffer always contains mpd_sint16; declaring it with that
type saves several casts.
2008-08-26 08:27:13 +02:00
Max Kellermann 2a9608536c mp3: moved num_samples calculation out of the loop
The previous patch removed all loop specific dependencies from the
num_samples formula; we can now calculate it before entering the loop.
2008-08-26 08:27:13 +02:00
Max Kellermann 3f55b5a1e4 mp3: eliminated outputPtr
The output buffer is always flushed after being appended to, which
allows us to assume it is always empty.  Always start writing at
outputBuffer, don't remember outputPtr.
2008-08-26 08:27:13 +02:00
Max Kellermann f0bcb4a44a mp3: don't do a second flush in mp3_decode()
The previous patch made mp3Read() flush the output buffer in every
iteration, which means we can eliminate the flush check after invoking
mp3Read().
2008-08-26 08:27:13 +02:00
Max Kellermann 2e8bd3ae1d mp3: always flush directly after decoding/dithering
Since we try to fill the buffer in every iteration, we assume that we
should flush the output buffer at the end of each iteration.
2008-08-26 08:27:12 +02:00
Max Kellermann af83ac5ec6 mp3: dither a whole block at a time
Fill the whole output buffer at a time by using dither_buffer()'s
ability to decode blocks.  Calculate how many samples fit into the
output buffer before each invocation.
2008-08-26 08:27:12 +02:00
Max Kellermann e99536e8eb mp3: moved dropSamplesAtEnd check out of the loop
Simplifying loops for performance: why check dropSamplesAtEnd in every
iteration, when we could modify the loop boundary?  The (writable)
variable samplesLeft can be eliminated; add a write-once variable
pcm_length instead, which is used for the loop condition.
2008-08-26 08:27:12 +02:00
Max Kellermann e4c6c01903 mp3: make samplesPerFrame more local
The variable samplesPerFrame is used only in one single closure.  Make
it local to this closure.  The compiler will probably convert it to a
register anyway.
2008-08-26 08:27:12 +02:00
Max Kellermann 60a155624c mp3: unsigned integers 2008-08-26 08:27:12 +02:00
Max Kellermann f667da1b46 mp3: removed double cmd==STOP check
cmd has already been checked before, it cannot have changed meanwhile
because it is a local variable.
2008-08-26 08:27:12 +02:00
Max Kellermann 09fbbdc366 mp3: moved code to dither_buffer()
Preparing for simplifying and thus speeding up the dithering code:
moved dithering to a separate function which contains a trivial loop.
With this patch, only one sample is dithered at a time, but the
following patches will allow us to dither a whole block at a time,
without complicated buffer length checks.
2008-08-26 08:27:12 +02:00
Max Kellermann d9583aa95b mp3: don't check dropSamplesAtStart in the loop
Performance improvement by moving stuff out of a loop: skip part of
the first frame before entering the loop.
2008-08-26 08:27:12 +02:00
Max Kellermann df251a9960 assert song->url != NULL 2008-08-26 08:27:11 +02:00
Max Kellermann e99333167e aac: support decoding AAC streams
Copy some code from aac_decode() to aac_stream_decode() and apply
necessary changes to allow streaming audio data.  Both functions might
be merged later.
2008-08-26 08:27:11 +02:00
Max Kellermann 5300f79ca9 aac: splitted aac_parse_header() from initAacBuffer()
initAacBuffer() should really only initialize the buffer; currently,
it also reads data from the input stream and parses the header.  All
of the AAC buffer code should probably be moved to a separate library
anyway.
2008-08-26 08:27:11 +02:00
Max Kellermann 351dda01bd aac: check buffer lengths
The AAC plugin sometimes does not check the length of available data
when checking for magic prefixes.  Add length checks.
2008-08-26 08:27:11 +02:00
Max Kellermann 9131f9ebfe aac: use fillAacBuffer() instead of manual reading
Eliminate some duplicated code by using fillAacBuffer().
2008-08-26 08:27:11 +02:00
Max Kellermann 00c47b3c85 find AAC frames
Find AAC frames in the input and skip invalid data.  This prepares AAC
streaming.
2008-08-26 08:27:11 +02:00
Max Kellermann f43e39047d aac: moved code to adts_check_frame()
adts_check_frame() checks whether the buffer head is an AAC frame, and
returns the frame length.
2008-08-26 08:27:11 +02:00
Max Kellermann b7ad3e4121 aac: moved code to aac_buffer_shift()
Shifting from the buffer queue is a common operation, and should be
provided as a separate function.  Move code to aac_buffer_shift() and
add a bunch of assertions.
2008-08-26 08:27:11 +02:00
Max Kellermann a3cc928c71 aac: use inputStreamAtEOF()
When checking for EOF, we should not check whether the read request
has been fully satisified.  The InputStream API does not guarantee
that readFromInputStream() always fills the whole buffer, if EOF is
not reached.  Since there is the function inputStreamAtEOF() dedicated
for this purpose, we should use it for EOF checking after
readFromInputStream()==0.
2008-08-26 08:27:10 +02:00
Max Kellermann 35858dfe3a aac: don't depend on consumed data in fillAacBuffer()
Fill the AacBuffer even when nothing has been consumed yet.  The
function should not check for consumed data, but for free space at the
end of the buffer.
2008-08-26 08:27:10 +02:00
Max Kellermann 2a14141121 aac: simplified fillAacBuffer()
Return instead of putting all the code into a if-closure.  That saves
one level of indentation.
2008-08-26 08:27:10 +02:00
Max Kellermann 1d18f0089a aac: make adtsParse() void
adtsParse() always returns 1, and its caller does not use the return
value.
2008-08-26 08:27:10 +02:00
Max Kellermann 0ca8f9ac49 aac: use size_t 2008-08-26 08:27:10 +02:00
Max Kellermann a6332fd13b aac: removed unused initAacBuffer() parameters
Since we eliminated the parameters retFileread and retTagsize in all
callers, we can now safely remove it from the function prototype.
2008-08-26 08:27:10 +02:00
Max Kellermann 4ca24b960b eliminate unused variables in the AAC decoder 2008-08-26 08:27:10 +02:00
Max Kellermann 82ca4cf8ce added InputStream.ready
The flag "ready" indicates whether the input stream is ready and it
has parsed all meta data.  Previously, it was impossible for
decodeStart() to see the content type of HTTP input streams, because
at that time, the HTTP response wasn't parsed yet.
2008-08-26 08:27:10 +02:00
Max Kellermann 6120635f06 added decoder_plugin_register()
With the functions decoder_plugin_register() and
decoder_plugin_unregister(), decoder plugins can register a
"secondary" plugin, like the flac input plugin does this for
"oggflac".
2008-08-26 08:27:09 +02:00
Max Kellermann a68ef497f5 don't call quitDecode() in waitOnDecode()
To make the code more consistent, call quitDecode() only at the end of
decodeParent().
2008-08-26 08:27:09 +02:00
Max Kellermann e8bd9ddc9f moved code to player_thread.c
Move code which runs in the player thread to player_thread.c.  Having
a lot of player thread code in decode.c isn't easy to understand.
2008-08-26 08:27:09 +02:00
Max Kellermann 6104e9690e moved code to crossfade.c
decode.c should be a lot smaller; start by moving all code which
handles cross-fading to crossfade.c.  Also includes camelCase
conversion.
2008-08-26 08:27:09 +02:00
Max Kellermann 2650b9eb31 added inline function audio_format_time_to_size()
Make the code more readable by hiding big formulas in an inline
function with a nice name.
2008-08-26 08:27:09 +02:00
Max Kellermann 0aedf7dd5a pass max_chunks to calculateCrossFadeChunks()
Make calculateCrossFadeChunks() more generic and portable by
eliminating global variable access.
2008-08-26 08:27:09 +02:00
Max Kellermann 0fc2422cd0 converted MpdTagItem.type to an enum
Don't use CPP macros when you can use C enum... this also allows
better type checking.
2008-08-26 08:27:09 +02:00
Max Kellermann e754ed01a7 renamed functions in decoder_list.h
InputPlugin to decoder_plugin, and no camelCase.
2008-08-26 08:27:09 +02:00
Max Kellermann 772d3da98a no camel case in struct decoder_plugin 2008-08-26 08:27:08 +02:00
Max Kellermann f1a014d094 renamed the InputPlugin function types
Continuing the effort to rename InputPlugin to decoder_plugin...
2008-08-26 08:27:08 +02:00
Max Kellermann 41c5fbbb07 renamed inputPlugin.* to decoder_list.*
Since inputPlugin.c manages the list of registered decoders, we should
rename the source file.
2008-08-26 08:27:08 +02:00
Max Kellermann f5711e28da moved decoder externs to inputPlugin.c
The decoder_plugin instances are only used inputPlugin.c, so move them
from the header file.
2008-08-26 08:27:08 +02:00
Max Kellermann e41be362a1 renamed InputPlugin to struct decoder_plugin
"decoder plugin" is a better name than "input plugin", since the
plugin does not actually do the input - InputStream does.  Also don't
use typedef, so we can forward-declare it if required.
2008-08-26 08:27:08 +02:00
Max Kellermann cdaa26c81d no busy-waiting for the player process
The function player_command() inherited the busy-waiting algorithm
from the old code; throw in a wait_main_task() to do idle waiting.
2008-08-26 08:27:08 +02:00
Max Kellermann b1de50f994 converted PlayerControl.state to enum
Don't write CPP when you can express the same in C...  macros vs enum
is a good example for that.
2008-08-26 08:27:08 +02:00
Max Kellermann efde884a13 added PlayerControl.command
PlayerControl.command replaces the old attributes play, stop, pause,
closeAudio, lockQueue, unlockQueue, seek.  The main thread waits for
each command synchronously, so there can only be one command enabled
at a time anyway.
2008-08-26 08:27:07 +02:00
Max Kellermann c7384b65ac eliminate OUTPUT_BUFFER_DC_STOP, OUTPUT_BUFFER_DC_SEEK
(Ab)use the decoder_command enumeration, which has nearly the same
values and the same meaning.
2008-08-26 08:27:07 +02:00
Max Kellermann 67bf4b448d added decoder_get_url()
The wavpack decoder plugin implements a hack, and it needs the song
URL for that.  This API (and the hack) should be revised later, but
add that function for now.
2008-08-26 08:27:07 +02:00
Max Kellermann c60209ff04 don't set dc->seekable in wavpack plugin
dc->seekable is already set by decodeStart().
2008-08-26 08:27:07 +02:00
Max Kellermann dc4d774481 use a local "initialized" flag instead of dc->state
Since we want to hide mpd internals from the decoder plugins, the
plugins should not check dc->state whether they have already called
decoder_initialized().  Use a local variable to track that.
2008-08-26 08:27:07 +02:00
Max Kellermann 17e9cc84c5 added decoder_seek_where() and decoder_seek_error()
Provide access to seeking for the decoder plugins; they have to know
where to seek, and they need a way to tell us that seeking has failed.
2008-08-26 08:27:07 +02:00
Max Kellermann 78c55e2432 added decoder_command_finished() to decoder_api.h
Some decoder commands are implemented in the decoder plugins, thus
they need to have an API call to signal that their current command has
been finished.  Let them use the new decoder_command_finished()
instead of the internal dc_command_finished().
2008-08-26 08:27:07 +02:00
Max Kellermann 817a68b2b2 added decoder_get_command()
Another big patch which hides internal mpd APIs from decoder plugins:
decoder plugins regularly poll dc->command; expose it with a
decoder_api.h function.
2008-08-26 08:27:07 +02:00
Max Kellermann 2e9169de9d moved convState to struct decoder
Since we moved all PCM conversions to decoder_data(), the attribute
convState isn't being used anymore by the OutputBuffer code.  Move it
to struct decoder.
2008-08-26 08:27:06 +02:00
Max Kellermann 1b845f94a0 moved struct AudioFormat to audio_format.h
We want to expose the AudioFormat structure to plugins; remove some
clutter by moving its declaration to a separate header file.
2008-08-26 08:27:06 +02:00
Max Kellermann 08bdb43bef moved InputPlugin to decoder_api.h
InputPlugin is the API which is implemented by a decoder plugin.  This
belongs to the public API/ABI, so move it to decoder_api.h.  It will
later be renamed to something like "decoder_plugin".
2008-08-26 08:27:06 +02:00
Max Kellermann 4a87f1e815 remove one indent level from audiofile plugin
Anonymous code blocks just to declare variables look ugly.  Move the
variable declarations up and disband the code block.
2008-08-26 08:27:06 +02:00
Max Kellermann d186260016 use break instead of local variable "eof"
Similar to previous patch: eliminate one variable by using "break".
This also simplifies the code since we can remove one level of indent.
2008-08-26 08:27:06 +02:00
Max Kellermann 4c06624988 removed local variable "eof" because it is unused
"break" is so much easier than "eof=1; continue;", when "!eof" is the
loop condition.
2008-08-26 08:27:06 +02:00
Max Kellermann ee3bbb5028 simplify several dc->command checks
Since we have merged dc->stop, dc->seek into one variable, we don't
have to check both conditions at a time; we can replace "!stop &&
!seek" with "none".
2008-08-26 08:27:06 +02:00
Max Kellermann 0d8b551c5a added parameter total_time to decoder_initialized()
Similar to the previous patch: pass total_time instead of manipulating
dc->totalTime directly.
2008-08-26 08:27:05 +02:00
Max Kellermann 4590a98f0e added audio_format parameter to decoder_initialized()
dc->audioFormat is set once by the decoder plugins before invoking
decoder_initialized(); hide dc->audioFormat and let the decoder pass
an AudioFormat pointer to decoder_initialized().
2008-08-26 08:27:05 +02:00
Max Kellermann 0d45870cea added decoder_clear() and decoder_flush()
We are now beginning to remove direct structure accesses from the
decoder plugins.  decoder_clear() and decoder_flush() mask two very
common buffer functions.
2008-08-26 08:27:05 +02:00
Max Kellermann ea51f73285 tail_chunk() returns ob_chunk pointer
Code simplification: since we are not using in-band signalling with
the chunk index anymore, we can just return a pointer to the tail
chunk instead of the index.
2008-08-26 08:27:05 +02:00
Max Kellermann 5df6ff8d22 added OutputBuffer.notify
OutputBuffer should be a more generic low-level library, without
dependencies to the other headers.  This patch adds the field
"notify", which is used to signal the player thread.  It is passed in
the constructor, and removes the need to compile with the decode.h
header.
2008-08-26 08:27:05 +02:00
Max Kellermann 2a83ccdb8f added decoder_data()
Moved all of the player-waiting code to decoder_data(), to make
OutputBuffer more generic.
2008-08-26 08:27:05 +02:00
Max Kellermann 8814c0c898 use the notify library in main_notify.c
Avoid some duplicated code in main_notify.c.
2008-08-26 08:27:05 +02:00
Max Kellermann 4f80f53c29 do not sleep after openAudioDevice()
After the decoder has been initialized and the audio device has been
opened, don't sleep.  The decoder plugin won't do anything special nor
will it care to wake us up for some reason.
2008-08-26 08:27:04 +02:00
Max Kellermann 2bf7ec4f39 added decoder_initialized()
decoder_initialized() sets the state to DECODE_STATE_DECODE and wakes
up the player thread.  It is called by the decoder plugin after its
internal initialization is finished.  More arguments will be added
later to prevent direct accesses to the DecoderControl struct.
2008-08-26 08:27:04 +02:00
Max Kellermann 154aa496e8 added struct decoder
The decoder struct should later be made opaque to the decoder plugin,
because maintaining a stable struct ABI is quite difficult.  The ABI
should only consist of a small number of stable functions.
2008-08-26 08:27:04 +02:00
Max Kellermann 241cd043ca invoke the notify API directly
Don't use wrappers like player_wakeup_decoder_nb().  These have been
wrappers calling notify.c functions, for compatibility with the
existing code when we migrated to notify.c.
2008-08-26 08:27:04 +02:00
Max Kellermann 87beded44f removed "else"
The "if" block breaked out of the loop.  That means we can move the
code out of the "else" block.
2008-08-26 08:27:04 +02:00
Max Kellermann 9e0f7dcd1a added dc_command_finished()
dc_command_finished() is invoked by the decoder thread when it has
finished a command (sent by the player thread).  It resets dc.command
and wakes up the player thread.  This combination was used at a lot of
places, and by introducing this function, the code will be more
readable.
2008-08-26 08:27:04 +02:00
Max Kellermann 726c6e86d3 don't busy wait for the decoder thread
Busy wait loops are a bad thing, especially when the response time can
be very long - busy waits eat a lot of CPU, and thus slow down the
other thread.  Since the other thread will notify us when it's ready,
we can use notify_wait() instead.
2008-08-26 08:27:04 +02:00
Max Kellermann 8d3942e0c3 merged start, stop, seek into DecoderControl.command
Much of the existing code queries all three variables sequentially.
Since only one of them can be set at a time, this can be optimized and
unified by merging all of them into one enum variable.  Later, the
"command" checks can be expressed in a "switch" statement.
2008-08-26 08:27:04 +02:00
Max Kellermann 180d78a8e6 don't set pc->errored_song in decodeStart()
pc->errored_song is already set by decodeParent() in the player thread
when we set dc->error; no need to set it in the decoder thread.
2008-08-26 08:27:03 +02:00
Max Kellermann d507ff28c8 added dc.next_song, renamed pc.current_song
Since pc->current_song denotes the song which the decoder should use
next, we should move it to DecoderControl.  This removes one internal
PlayerControl struct access from the decoder code.

Also add pc.next_song, which is manipulated by the playlist code, and
gets copied to dc.next_song as soon as the decoder is started.
2008-08-26 08:27:03 +02:00
Max Kellermann b6909da758 clean up CPP includes
Include only headers which are really required.  This speeds up
compilation and helps detect cross-layer accesses.
2008-08-26 08:27:03 +02:00
Max Kellermann a383f45117 enable -Wpointer-arith, -Wstrict-prototypes
Also enable -Wunused-parameter - this forces us to add the gcc
"unused" attribute to a lot of parameters (mostly library callback
functions), but it's worth it during code refactorizations.
2008-08-26 08:27:02 +02:00
Max Kellermann c003778454 fix warnings in the HTTP client
Fix a "unused argument" warning, and several warnings regarding void
pointer calculation.
2008-08-25 15:49:06 +02:00
Max Kellermann 35918c9901 fixed ringbuf.c warnings
Fix a "signed/unsigned comparison warning", and several void pointer
math warnings.
2008-08-25 15:49:06 +02:00
Eric Wong a889684acd http: hopefully allow seeking to work on static files
git-svn-id: https://svn.musicpd.org/mpd/trunk@7398 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-30 02:43:36 +00:00
Max Kellermann c7d1ce07f3 don't call seekInputStream(0) if r==0
If nothing has been read from the input stream, we don't have to
rewind it.

git-svn-id: https://svn.musicpd.org/mpd/trunk@7397 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-30 02:43:32 +00:00
Max Kellermann 7e99a0b0a9 eliminated local variable "to_read"
The variable "to_read" is never modified except in the last iteration
of the while loop.  This means the while condition will never become
false, as the body will break before that may be checked.

git-svn-id: https://svn.musicpd.org/mpd/trunk@7396 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-30 02:43:27 +00:00
Eric Wong 06bdc5bf25 introduce struct condition as a more correct version of Notify
Start using it in the HTTP code

git-svn-id: https://svn.musicpd.org/mpd/trunk@7395 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-30 02:43:22 +00:00
Eric Wong d82a4aff25 utils: pthread_{mutex,cond}_init can fail, so check for it
git-svn-id: https://svn.musicpd.org/mpd/trunk@7394 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-30 02:43:17 +00:00
Eric Wong 2a5dcba5ed http: initial rewrite using ringbuffer + pthreads
This institutes the usage of a separate thread to buffer HTTP
input.  It is basically practice code for using the ringbuffer
code which I plan on reusing for the OutputBuffer as well as
further input buffering for disk (networked filesystems over
WAN, laptops on battery, etc).

Each readFromInputStream() call on an HTTP stream can take
several seconds to complete, short reads are avoided.

A single-threaded solution for systems supporting large enough
SO_RCVBUF values should also be possible and will likely be done
in the future; but this lock-free(except when full/empty)
ringbuffer is cool :)

git-svn-id: https://svn.musicpd.org/mpd/trunk@7393 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-30 02:43:13 +00:00
Eric Wong c71cfbac7a http: fix some small memory leaks when hitting redirects
git-svn-id: https://svn.musicpd.org/mpd/trunk@7392 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-30 02:43:08 +00:00
Eric Wong 8426740f44 utils: add new unforgiving utility functions
We'll be using pipes when waiting for I/O, and condition
variables at other times.

git-svn-id: https://svn.musicpd.org/mpd/trunk@7391 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-30 02:43:04 +00:00
Eric Wong c593152967 ringbuf: add thread-safe, thread-specific reset functions
This will allow both the reader and writer threads to
reset the ringbuffer in a thread-safe fashion.

git-svn-id: https://svn.musicpd.org/mpd/trunk@7390 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-30 02:42:59 +00:00
Eric Wong 9873e07c5a ringbuf: get_{write,read}_vector returns total bytes in both vec elts
This will eliminate unnecessary calls to ringbuf_{read,write}_space

git-svn-id: https://svn.musicpd.org/mpd/trunk@7389 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-30 02:42:55 +00:00
Eric Wong aa828c1b25 http: split out logic around getaddrinfo() and connect()
Makes code easier to read and modularize

git-svn-id: https://svn.musicpd.org/mpd/trunk@7388 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-30 02:42:49 +00:00
Eric Wong ec1eeeeaff http: split out auth code since it's mostly uninteresting for now
The auth code also has some ugly usages of string generation
which I will eventually replace with something nicer...

git-svn-id: https://svn.musicpd.org/mpd/trunk@7387 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-30 02:42:45 +00:00
Eric Wong bec0835836 Add a generic ring buffer implementation
This piece of code is from the JACK Audio Connection Kit
(trimmed down a bit for better readability).

The vector functions now reuse the common iovec struct used by
writev/readv instead of reinventing an identical but
differently-named struct.

From the comments:
> ISO/POSIX C version of Paul Davis's lock free ringbuffer C++ code.
> This is safe for the case of one read thread and one write thread.

License is LGPL 2.1 or later

git-svn-id: https://svn.musicpd.org/mpd/trunk@7386 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-30 02:42:40 +00:00
Eric Wong 3a932bf388 os_compat.h: add sys/uio.h for writev/readv
vectored I/O will be useful with our ring buffer lib

git-svn-id: https://svn.musicpd.org/mpd/trunk@7385 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-30 02:42:34 +00:00
Max Kellermann d1b4112f45 jack: initialize audioOutput->data
Initialize audioOutput->data with NULL in jack_initDriver().
Previously, this was never initialized, although the other functions
relied on it being NULL prior to jack_openDevice().

This patch addresses bug 0001641[1].  In contrast to the patch provided
by the bug reporter, it moves the initialization before the "!param"
check.

[1] - http://musicpd.org/mantis/view.php?id=1641

git-svn-id: https://svn.musicpd.org/mpd/trunk@7375 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-13 07:39:11 +00:00
Hans de Goede 4f247ca4da mod: fix crashing on modtracker files
This patch was taken from
http://bugzilla.livna.org/show_bug.cgi?id=1987 and addresses bug
0001693[1]

[1] - http://musicpd.org/mantis/view.php?id=1693

git-svn-id: https://svn.musicpd.org/mpd/trunk@7374 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-13 07:39:05 +00:00
Eric Wong 29df78fe1e audio.c: avoid magic numbers even if they have comments :)
git-svn-id: https://svn.musicpd.org/mpd/trunk@7373 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-02 10:10:32 +00:00
Eric Wong f57be50d73 remove audioDeviceStates from playerData and getPlayerData
git-svn-id: https://svn.musicpd.org/mpd/trunk@7372 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-02 10:10:26 +00:00
Max Kellermann 6ac1d797af fix stream URL
Streaming was broken, beacuse the stream URL was never copied to
path_max_fs.

[ew: replaced strcpy with pathcpy_trunc for ease of auditing]

git-svn-id: https://svn.musicpd.org/mpd/trunk@7371 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-01 22:25:20 +00:00
Eric Wong 56aab937d0 oggflac_plugin: fix build with libOggFLAC lib (libFLAC <= 7)
git-svn-id: https://svn.musicpd.org/mpd/trunk@7370 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-01 22:25:07 +00:00
Max Kellerman 7f6a44ef8e make DecoderControl.state an enum
git-svn-id: https://svn.musicpd.org/mpd/trunk@7369 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-01 22:25:00 +00:00
Max Kellerman 97698bd4aa notify: don't use camelCase in notify.[ch]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7367 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-01 22:24:44 +00:00
Max Kellerman e0be3aad20 assert dc.state == DECODE_STATE_STOP
During the decoder thread main loop, dc.state must be
DECODE_STATE_STOP.  Explicitly assigning it after the "dc.stop" check
is redundant.

git-svn-id: https://svn.musicpd.org/mpd/trunk@7366 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-06-01 22:24:35 +00:00
Max Kellermann 3c3620b301 fix race condition in main_notify.c
The function wait_main_task() is racy: if the function
wakeup_via_cond() sees the mutex is locked just before
wait_main_task() executes pthread_cond_wait(), the main thread blocks
forever.

Work around this issue by adding a "pending" flag just like in my
notify.c code.  A standards-compliant solution should be implemented
later.

git-svn-id: https://svn.musicpd.org/mpd/trunk@7365 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-17 03:05:23 +00:00
Max Kellermann 7c952c4f4b added ob_set_lazy()
In lazy mode (previously the default), outputBuffer.c only wakes up
the player when it was previously empty.  That caused a deadlock when
the player was waiting for buffered_before_play, since the decoder
wouldn't wake up the player when buffered_before_play was reached.  In
non-lazy mode, always wake up the player when a new chunk was decoded.

git-svn-id: https://svn.musicpd.org/mpd/trunk@7364 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-15 05:57:22 +00:00
Max Kellermann 071c8f4ac7 use dc.current_song instead of pc.current_song
When we are in an input plugin, dc.current_song should already be
set.  Use it instead of pc.current_song.

git-svn-id: https://svn.musicpd.org/mpd/trunk@7363 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-15 05:57:14 +00:00
Max Kellermann 119f457cff const pointers in decode.c
git-svn-id: https://svn.musicpd.org/mpd/trunk@7362 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-15 05:57:01 +00:00
Max Kellermann d548deb45b const pointers in normalize.c
git-svn-id: https://svn.musicpd.org/mpd/trunk@7361 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-15 05:56:54 +00:00
Max Kellermann f201f2c922 more const pointers in pcm_utils.[ch]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7360 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-15 05:56:35 +00:00
Max Kellermann df34beb98d fix XFADE_DISABLED=-1
git-svn-id: https://svn.musicpd.org/mpd/trunk@7359 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-15 05:56:18 +00:00
Eric Wong b44506084e decode.c: make the crossfade state variable self-documenting
git-svn-id: https://svn.musicpd.org/mpd/trunk@7358 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-14 06:41:05 +00:00
Eric Wong 93fee6fc85 command.c: remove dead commented code
We have revision control for a reason :)

git-svn-id: https://svn.musicpd.org/mpd/trunk@7357 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-14 06:41:00 +00:00
Eric Wong 0f040a7030 Always compile ioops.h, since main_notify uses it now
git-svn-id: https://svn.musicpd.org/mpd/trunk@7356 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-13 18:04:02 +00:00
Eric Wong 412ce8bdc4 Make the OutputBuffer API more consistent
We had functions names varied between
outputBufferFoo, fooOutputBuffer, and output_buffer_foo

That was too confusing for my little brain to handle.
And the global variable was somehow named 'cb' instead of
the more obvious 'ob'...

git-svn-id: https://svn.musicpd.org/mpd/trunk@7355 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-13 01:16:27 +00:00
Eric Wong c1963ed483 Stop passing our single OutputBuffer object everywhere
All of our main singleton data structures are implicitly shared,
so there's no reason to keep passing them around and around in
the stack and making our internal API harder to deal with.

git-svn-id: https://svn.musicpd.org/mpd/trunk@7354 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-13 01:16:15 +00:00
Eric Wong dec6b1612e Stop passing our single DecoderControl object everywhere
This at least makes the argument list to a lot of our plugin
functions shorter and removes a good amount of line nois^W^Wcode,
hopefully making things easier to read and follow.

git-svn-id: https://svn.musicpd.org/mpd/trunk@7353 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-13 01:16:03 +00:00
Eric Wong 98acfa8ac5 Get rid of PlayerControl inside the PlayerData struct
It actually increases our image size a small bit and may even
hurt performance a very small bit, but makes the code less
verbose and easier to manage.

I don't see a reason for mpd to ever support playing multiple
files at the same time (users can run multiple instances of mpd
if they really want to play Zaireeka, but that's such an edge
case it's not worth ever supporting in our code).

git-svn-id: https://svn.musicpd.org/mpd/trunk@7352 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-13 01:15:50 +00:00
Eric Wong ae1335753a main_notify: just use cond_signal to wakeup, no need to trylock
pthread_cond_signal is a no-op if nothing is waiting on it

git-svn-id: https://svn.musicpd.org/mpd/trunk@7351 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-13 01:15:43 +00:00
Eric Wong 78370a9759 main_notify: use pthread_cond_signal instead of broadcast
signal is all we need since we only have one waiter and
likely faster

git-svn-id: https://svn.musicpd.org/mpd/trunk@7349 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 10:27:57 +00:00
Eric Wong 30bd8b2c0f Fix a deadlock after pause/next
Oops, forgot to unlock a mutex

git-svn-id: https://svn.musicpd.org/mpd/trunk@7348 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 10:27:53 +00:00
Eric Wong baf9b94ecf Fix the problem of songs not advancing without client activity
The select() in the main event loop blocks now (saving us many
unnecessary wakeups).  This interacted badly with the threads
that were trying to wakeup the main task via
pthread_cond_signal() since the main task was not blocked
on a condition variable, but on select().

So now if we detect a need to wakeup the player, we write
to a pipe which select() is watching instead of blindly
calling pthread_cond_signal().

git-svn-id: https://svn.musicpd.org/mpd/trunk@7347 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 09:46:11 +00:00
Eric Wong 804088f590 command.h: include sllist.h which defines struct strnode
git-svn-id: https://svn.musicpd.org/mpd/trunk@7346 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 04:27:09 +00:00
Max Kellermann dec2a063cd const pointers in audioOutput.c
git-svn-id: https://svn.musicpd.org/mpd/trunk@7345 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 04:21:43 +00:00
Max Kellermann b4ea6e52b2 const pointers in audio.c
git-svn-id: https://svn.musicpd.org/mpd/trunk@7344 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 04:21:39 +00:00
Max Kellermann ac0ac9d4d8 flush after loop in sendDataToOutputBuffer()
Since tailChunk() automatically flushes full buffers, we do not have
to check this in every iteration of sendDataToOutputBuffer().

git-svn-id: https://svn.musicpd.org/mpd/trunk@7343 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 04:21:34 +00:00
Max Kellermann e4746b82c9 constant pointers in replayGain.c
git-svn-id: https://svn.musicpd.org/mpd/trunk@7342 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 04:21:28 +00:00
Max Kellermann 1308290c0d yet another migration to size_t
We can also get rid of one the two variables.

git-svn-id: https://svn.musicpd.org/mpd/trunk@7341 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 04:21:22 +00:00
Max Kellermann bc7e60cbcd check cb->stop in the while loop
Checking dc->stop in the while condition and again after the while
loop costs some CPU cycles we should save.

git-svn-id: https://svn.musicpd.org/mpd/trunk@7340 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 04:21:18 +00:00
Max Kellermann f0e78138d1 eliminate OutputBuffer.currentChunk
OutputBuffer.currentChunk contains redundant data: it is either -1
when there is no chunk which is currently being written, or it equals
"end".  If we always keep chunk[end] in a valid state, we can remove
OutputBuffer.currentChunk.

This patch may look a bit clumsy, especially flushOutputBuffer(), but
that will be fixed later with an major OutputBuffer API overhaul.

git-svn-id: https://svn.musicpd.org/mpd/trunk@7339 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 04:21:13 +00:00
Max Kellermann 3105280f26 added output_buffer_expand()
output_buffer_expand() moves the cb->end to the new position (only its
current successor is allowed) and wakes up the player if is waiting
for the decoder.  This simplifies flushOutputBuffer().

git-svn-id: https://svn.musicpd.org/mpd/trunk@7338 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 04:21:09 +00:00
Max Kellermann 5cfb1cf46e initialize all elements in initOutputBuffer()
The current OutputBuffer object is allocated statically, i.e. it is
zeroed.  To be safe for other cases in the future, also initialize the
other elements.

git-svn-id: https://svn.musicpd.org/mpd/trunk@7337 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 04:21:04 +00:00
Max Kellermann 7dc1f3117c wake up player on demand
The decoder should not wake up the player when it did not produce a
flushed chunk.  Move the decoder_wakeup_player() call to
flushOutputBuffer() and invoke it only if the buffer was previously
empty.

git-svn-id: https://svn.musicpd.org/mpd/trunk@7336 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 04:21:00 +00:00
Max Kellermann 91b43b481d unlink the socket before bind()
If a unix domain socket already exists, bind() fails.  Unlink the
socket file.

git-svn-id: https://svn.musicpd.org/mpd/trunk@7335 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 04:20:56 +00:00
Max Kellermann 3919a87525 replaced assertion with check
During my tests, it happened that data->position>newPosition.  I have
not yet fully understood why this can happen; for now, replace this
with a run-time check.

git-svn-id: https://svn.musicpd.org/mpd/trunk@7334 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 04:20:50 +00:00
Max Kellermann 1db6478491 fix several errors/warnings which only appear with -O3
git-svn-id: https://svn.musicpd.org/mpd/trunk@7333 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 04:20:45 +00:00
Max Kellermann a25a7624f0 multiply num_samples with bytes_per_channel
The patch "convert blocks until the buffer is full" did not update
data->chunk_length correctly: it added the number of samples, not the
number of bytes.  Multiply that with bytes_per_channel

git-svn-id: https://svn.musicpd.org/mpd/trunk@7332 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 04:20:40 +00:00
Max Kellermann f4252ee795 missing num_channels check in previous patch
In the patch "special optimized case for 16bit stereo", the check for
"num_channels==2" was missing.

git-svn-id: https://svn.musicpd.org/mpd/trunk@7331 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 04:20:34 +00:00