Commit Graph

1856 Commits

Author SHA1 Message Date
Eric Wong
18fc10a926 audio_format: volatile removal
volatile provides absolutely no guarantee thread-safety in SMP
environments.  volatile was designed to access memory locations
in peripheral hardware directly; not for SMP.  If volatile is
needed to work properly on SMP, then it is only hiding subtle
bugs.

volatile only prevents the /compiler/ from making optimizations
when accessing variables.  CPUs do their own optimizations at
runtime so it cannot guarantee registers of CPUs are flushed
to memory cache-coherent access on different CPUs.

Furthermore, the thread-communication via condition variables
between threads sharing audio formats already results in memory
barriers.
2008-09-07 19:14:50 +02:00
Eric Wong
a5f68b3cfc tag: oops, of course items is now ** and not *
Gah, it seems like doing sizeof here either way is error
prone.  Too easy to leave out a '*' character we can
forget.
2008-09-07 19:14:47 +02:00
Eric Wong
3c4de5b560 tag: lock all accesses to tag_pool
The tag pool is a shared global resource that is infrequently
modified.  However, it can occasionally be modified by several
threads, especially by the metadata_pipe for streaming metadata
(both reading/writing).

The bulk tag_item pool is NOT locked as currently only the
update thread uses it.
2008-09-07 19:14:45 +02:00
Eric Wong
194c8c3c0f tag: introduce handy items_size() function
Trying to read or remember
  "tag->numOfItems * sizeof(*tag->items)"
requires too much thinking and mental effort on my part.

Also, favor "sizeof(struct mpd_tag)" over "sizeof(*tag->items)"
because the former is easier to read and follow, even though
the latter is easier to modify if the items member changes
to a different type.
2008-09-07 19:14:43 +02:00
Max Kellermann
4dd9d4b2fd fix -Wcast-qual -Wwrite-strings warnings
The previous patch enabled these warnings.  In Eric's branch, they
were worked around with a generic deconst_ptr() function.  There are
several places where we can add "const" to pointers, and in others,
libraries want non-const strings.  In the latter, convert string
literals to "static char[]" variables - this takes the same space, and
seems safer than deconsting a string literal.
2008-09-07 19:14:39 +02:00
Max Kellermann
86d261bdb5 removed fdprintf() and client_print()
All callers of fdprintf() have been converted to client_printf() or
fprintf(); it is time to remove this clumsy hack now.  We can also
remove client_print() which took a file descriptor as parameter.
2008-09-07 14:08:37 +02:00
Max Kellermann
322e908893 client: removed client_get_fd()
Now that we have removed all invocations of client_get_fd(), we can
safely remove this transitional function.  All access to the file
descriptor is now hidden behind the interface declared in client.h.
2008-09-07 14:05:02 +02:00
Max Kellermann
4d8438e63d audio: don't pass "fd" to printAudioDevices()
Pass the client struct instead.
2008-09-07 14:04:16 +02:00
Max Kellermann
a6c5928c75 stats: don't pass "fd" to printStats()
Pass the client struct instead of the raw file descriptor.
2008-09-07 14:02:57 +02:00
Max Kellermann
93e6d4c3ad playlist: don't pass "fd" to showPlaylist(), playlistChangesPosId()
Pass the client struct instead of the raw file descriptor.
2008-09-07 14:02:52 +02:00
Max Kellermann
709ec6fa39 playlist: added playlist_save()
The shared code in showPlaylist() isn't worth it, because we aim to
remove fdprintf().  Duplicate this small function, and enable stdio
buffering for saved playlists.
2008-09-07 14:02:43 +02:00
Max Kellermann
438b56f0ba ls: don't pass "fd" to lsPlaylists(), printRemoteUrlHandlers()
Pass the client struct instead of the raw file descriptor.
2008-09-07 14:02:40 +02:00
Max Kellermann
4665f2bf32 tag: don't pass "fd" to printVisitedInTagTracker()
Pass the client struct instead of the raw file descriptor.
2008-09-07 13:57:58 +02:00
Max Kellermann
94293149b1 command: concatenate strings at compile time
String literals (including those defined in CPP macros) can be
concatenated at compile time.  This saves some CPU cycles in
vsnprintf() at run time.
2008-09-07 13:57:43 +02:00
Max Kellermann
b332e1cbc8 command: removed commandError()
commandError() has been superseded by command_error(), and is not
being used anymore.  Remove it.
2008-09-07 13:57:37 +02:00
Max Kellermann
f59986fad5 playlist: pass struct client to loadPlaylist()
The function loadPlaylist() wants to report incremental errors to the
client, for this reason we cannot remove its protocol dependency right
now.  Instead, make it use the client struct instead of the raw file
descriptor.
2008-09-07 13:57:26 +02:00
Max Kellermann
dc8b64fdef pass "struct client" to dbUtils.c, song.c, tag_print.c
Don't pass the raw file descriptor around.  This migration patch is
rather large, because all of the sources have inter dependencies - we
have to change all of them at the same time.
2008-09-07 13:53:55 +02:00
Max Kellermann
5609a1fcd0 command: pass struct client to all commands
Pass the client struct to CommandHandlerFunction and
CommandListHandlerFunction.  Most commands cannot take real advantage
of that yet, since most of them still work with the raw file
descriptor.
2008-09-07 13:52:48 +02:00
Max Kellermann
d2543f03f5 command: pass struct client to getCommandEntryAnd...()
Instead of passing the file descriptor, pass the client struct to
getCommandEntryAndCheckArgcAndPermission().
2008-09-07 13:52:36 +02:00
Max Kellermann
54371add13 command: added command_success() and command_error()
These two functions take a client struct instead of the file
descriptor.  We will now begin passing the client struct around
instead of a raw file descriptor (which needed a linear lookup in the
client list to be useful).
2008-09-07 13:51:59 +02:00
Max Kellermann
4ddc0a48e2 audio: don't pass "fd" to {en,dis}ableAudioDevice()
No protocol code in the audio output library.
2008-09-07 13:51:50 +02:00
Max Kellermann
f7e414d934 volume: don't pass "fd" to changeVolumeLevel()
The "volume" library shouldn't talk to the client.  Move error
handling to command.c.
2008-09-07 13:50:16 +02:00
Max Kellermann
8e3c40f032 directory: don't pass "fd" to updateInit()
Again, move error handling to command.c.
2008-09-07 13:50:06 +02:00
Max Kellermann
17b6491bcf directory: printDirectoryInfo() does not call commandError()
Move another ocurrence of error handling over to command.c.
2008-09-07 13:49:01 +02:00
Max Kellermann
f320c9fa1d directory: don't pass fd to traverseAllIn()
This patch continues the work of the previous patch: don't pass a file
descriptor at all to traverseAllIn().  Since this fd was only used to
report "directory not found" errors, we can easily move that check to
the caller.  This is a great relief, since it removes the dependency
on a client connection from a lot of enumeration functions.
2008-09-07 13:48:37 +02:00
Max Kellermann
528be8a0a9 directory: don't pass fd to traverseAllIn() callbacks
Database traversal should be generic, and not bound to a client
connection.  This is the first step: no file descriptor for the
callback functions forEachSong() and forEachDir().  If a callback
needs the file descriptor, it has to be passed in the void*data
pointer somehow; some callbacks might need a new struct for passing
more than one parameter.  This might look a bit cumbersome right now,
but our goal is to have a clean API.
2008-09-07 13:48:24 +02:00
Max Kellermann
d8ef33b710 playlist: PlaylistInfo() does not call commandError()
Continuing the effort of removing protocol specific calls from the
core libraries: let the command.c code call commandError() based on
PlaylistInfo's return value.
2008-09-07 13:44:20 +02:00
Max Kellermann
a8b225f947 playlist: don't pass "fd" to storedPlaylist.c functions
Return an "enum playlist_result" value instead of calling
commandError() in storedPlaylist.c.
2008-09-07 13:44:12 +02:00
Max Kellermann
8d2830b3f9 playlist: don't pass "fd" to playlist.c functions
The playlist library shouldn't talk to the client if possible.
Introduce the "enum playlist_result" type which the caller
(i.e. command.c) may use to generate an error message.
2008-09-07 13:39:31 +02:00
Max Kellermann
20feb0cbba playlist: showPlaylist() and shufflePlaylist() cannot fail
Make them both return void.
2008-09-07 13:39:19 +02:00
Max Kellermann
e1bf96672e playlist: moved "repeat" and "random" value checks to command.c
Client's input values should be validated by the command
implementation, and the core libraries shouldn't talk to the client
directly if possible.  Thus, setPlaylistRepeatStatus() and
setPlaylistRandomStatus() don't get the file descriptor, and cannot
fail (return void).
2008-09-07 13:38:59 +02:00
Max Kellermann
d1df71ebbc playlist: fix FILE* leak in appendSongToStoredPlaylistByPath()
When an error occurs after the file has been opened, the function will
never close the FILE object.
2008-09-07 13:37:33 +02:00
Max Kellermann
54c8e3daaf playlist: replaced run-time check with assertion
The "fspath" argument of writeStoredPlaylistToPath() must never be
NULL.  There should be an assertion on that, instead of a run-time
check.
2008-09-07 13:37:20 +02:00
Max Kellermann
dc353eca80 playlist: added is_valid_playlist_name()
The function valid_playlist_name() checks the name, but it insists on
reporting an eventual error to the client.  The new function
is_valid_playlist_name() is more generic: it just returns a boolean,
and does not care what the caller will use it for.  The old function
valid_playlist_name() will be removed later.
2008-09-07 13:37:04 +02:00
Max Kellermann
59efed3e8e tag: added buffered versions of the tag_print.c code
Currently, when the tag cache is being serialized to hard disk, the
stdio buffer is flushed before every song, because tag_print.c
performs unbuffered writes on the raw file descriptor.  Unfortunately,
the fdprintf() API allows buffered I/O only for a client connection by
looking up the client pointer owning the file descriptor - for stdio,
this is not possible.  To re-enable proper stdio buffering, we have to
duplicate the tag_print.c code without fprintf() instead of our custom
fdprintf() hack.  Add this duplicated code to tag_save.c.
2008-09-07 13:36:05 +02:00
Max Kellermann
75aa8dad4c song: moved code to song_print.c, song_save.c
Move everything which dumps song information (via tag_print.c) to a
separate source file.  song_print.c gets code which writes song data
to the client; song_save.c is responsible for serializing songs from
the tag cache.
2008-09-07 13:35:01 +02:00
Max Kellermann
386c303121 tag: moved code to tag_print.c
Move everything which dumps a tag to a file descriptor to tag_print.c.
This relaxes dependencies and splits the code into smaller parts.
2008-09-07 13:28:01 +02:00
Max Kellermann
f73319c048 client: added client_printf()
Based on client_puts(), client_printf() is the successor of
fdprintf().  As soon as all fdprintf() callers have been rewritten to
use client_printf(), we can remove fdprintf().
2008-09-07 13:25:54 +02:00
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