2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-02-24 00:41:20 +01:00
|
|
|
* This project's homepage is: http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "decode.h"
|
2004-02-27 02:35:23 +01:00
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "player.h"
|
|
|
|
#include "playerData.h"
|
|
|
|
#include "pcm_utils.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "log.h"
|
2004-05-18 15:13:55 +02:00
|
|
|
#include "ls.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
/* called inside decoder_task (inputPlugins) */
|
|
|
|
void decoder_wakeup_player(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-04-12 06:18:49 +02:00
|
|
|
PlayerControl *pc = &(getPlayerData()->playerControl);
|
|
|
|
wakeup_player_nb(pc);
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
}
|
|
|
|
|
2008-04-12 06:18:43 +02:00
|
|
|
void decoder_sleep(DecoderControl * dc)
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
{
|
2008-04-12 06:18:49 +02:00
|
|
|
PlayerControl *pc = &(getPlayerData()->playerControl);
|
2008-04-12 06:14:32 +02:00
|
|
|
notifyWait(&dc->notify);
|
2008-04-12 06:18:49 +02:00
|
|
|
wakeup_player_nb(pc);
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
}
|
|
|
|
|
2008-04-12 06:18:43 +02:00
|
|
|
static void player_wakeup_decoder_nb(DecoderControl * dc)
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
{
|
2008-04-12 06:14:32 +02:00
|
|
|
notifySignal(&dc->notify);
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* called from player_task */
|
2008-04-12 06:18:49 +02:00
|
|
|
static void player_wakeup_decoder(PlayerControl * pc, DecoderControl * dc)
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
{
|
2008-04-12 06:14:32 +02:00
|
|
|
notifySignal(&dc->notify);
|
2008-04-12 06:18:49 +02:00
|
|
|
player_sleep(pc);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void stopDecode(DecoderControl * dc)
|
|
|
|
{
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
if (dc->start || dc->state != DECODE_STATE_STOP) {
|
2004-02-24 00:41:20 +01:00
|
|
|
dc->stop = 1;
|
2008-04-12 06:18:43 +02:00
|
|
|
do { player_wakeup_decoder_nb(dc); } while (dc->stop);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void quitDecode(PlayerControl * pc, DecoderControl * dc)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
stopDecode(dc);
|
|
|
|
pc->state = PLAYER_STATE_STOP;
|
2006-07-14 21:52:29 +02:00
|
|
|
dc->seek = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
pc->play = 0;
|
|
|
|
pc->stop = 0;
|
|
|
|
pc->pause = 0;
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
wakeup_main_task();
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-04-12 06:15:35 +02:00
|
|
|
static unsigned calculateCrossFadeChunks(PlayerControl * pc, AudioFormat * af,
|
|
|
|
float totalTime)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-04-12 06:18:12 +02:00
|
|
|
unsigned int buffered_chunks, chunks;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-04-12 06:16:37 +02:00
|
|
|
if (pc->crossFade == 0 || pc->crossFade >= totalTime ||
|
2008-04-12 06:14:01 +02:00
|
|
|
!isCurrentAudioFormat(af))
|
2006-07-20 18:02:40 +02:00
|
|
|
return 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-04-12 06:16:37 +02:00
|
|
|
assert(pc->crossFade > 0);
|
|
|
|
assert(af->bits > 0);
|
|
|
|
assert(af->channels > 0);
|
|
|
|
assert(af->sampleRate > 0);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
chunks = (af->sampleRate * af->bits * af->channels / 8.0 / CHUNK_SIZE);
|
|
|
|
chunks = (chunks * pc->crossFade + 0.5);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-04-12 06:18:12 +02:00
|
|
|
buffered_chunks = getPlayerData()->buffer.size;
|
2008-04-12 06:16:13 +02:00
|
|
|
assert(buffered_chunks >= buffered_before_play);
|
2008-04-12 06:16:37 +02:00
|
|
|
if (chunks > (buffered_chunks - buffered_before_play))
|
2006-07-20 18:02:40 +02:00
|
|
|
chunks = buffered_chunks - buffered_before_play;
|
2004-02-25 18:53:48 +01:00
|
|
|
|
2008-04-12 06:16:37 +02:00
|
|
|
return chunks;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int waitOnDecode(PlayerControl * pc, DecoderControl * dc,
|
|
|
|
OutputBuffer * cb, int *decodeWaitedOn)
|
2004-05-19 04:14:20 +02:00
|
|
|
{
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
while (dc->start)
|
2008-04-12 06:18:49 +02:00
|
|
|
player_wakeup_decoder(pc, dc);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-04-12 06:12:11 +02:00
|
|
|
if (dc->error != DECODE_ERROR_NOERROR) {
|
2008-04-12 06:08:29 +02:00
|
|
|
pc->errored_song = pc->current_song;
|
2004-02-24 00:41:20 +01:00
|
|
|
pc->error = PLAYER_ERROR_FILE;
|
2006-07-20 18:02:40 +02:00
|
|
|
quitDecode(pc, dc);
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-14 21:52:29 +02:00
|
|
|
pc->totalTime = pc->fileTime;
|
|
|
|
pc->bitRate = 0;
|
|
|
|
pc->sampleRate = 0;
|
|
|
|
pc->bits = 0;
|
|
|
|
pc->channels = 0;
|
|
|
|
*decodeWaitedOn = 1;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int decodeSeek(PlayerControl * pc, DecoderControl * dc,
|
|
|
|
OutputBuffer * cb, int *decodeWaitedOn, int *next)
|
2004-05-19 04:14:20 +02:00
|
|
|
{
|
2006-07-14 21:52:29 +02:00
|
|
|
int ret = -1;
|
2004-05-19 04:14:20 +02:00
|
|
|
|
2008-04-12 06:08:29 +02:00
|
|
|
if (dc->state == DECODE_STATE_STOP ||
|
2008-04-12 06:12:07 +02:00
|
|
|
dc->error != DECODE_ERROR_NOERROR ||
|
2008-04-12 06:08:29 +02:00
|
|
|
dc->current_song != pc->current_song) {
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
stopDecode(dc);
|
|
|
|
*next = -1;
|
2008-04-12 06:12:16 +02:00
|
|
|
clearOutputBuffer(cb);
|
2008-04-12 06:12:07 +02:00
|
|
|
dc->error = DECODE_ERROR_NOERROR;
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
dc->start = 1;
|
|
|
|
waitOnDecode(pc, dc, cb, decodeWaitedOn);
|
|
|
|
}
|
|
|
|
if (dc->state != DECODE_STATE_STOP && dc->seekable) {
|
|
|
|
*next = -1;
|
|
|
|
dc->seekWhere = pc->seekWhere > pc->totalTime - 0.1 ?
|
|
|
|
pc->totalTime - 0.1 : pc->seekWhere;
|
|
|
|
dc->seekWhere = 0 > dc->seekWhere ? 0 : dc->seekWhere;
|
|
|
|
dc->seekError = 0;
|
|
|
|
dc->seek = 1;
|
2008-04-12 06:18:49 +02:00
|
|
|
do { player_wakeup_decoder(pc, dc); } while (dc->seek);
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
if (!dc->seekError) {
|
|
|
|
pc->elapsedTime = dc->seekWhere;
|
|
|
|
ret = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pc->seek = 0;
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
wakeup_main_task();
|
2004-05-19 04:14:20 +02:00
|
|
|
|
2006-07-14 21:52:29 +02:00
|
|
|
return ret;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-04-12 06:12:01 +02:00
|
|
|
static void processDecodeInput(PlayerControl * pc, DecoderControl * dc,
|
|
|
|
OutputBuffer * cb,
|
|
|
|
int *pause_r, unsigned int *bbp_r,
|
|
|
|
int *doCrossFade_r,
|
|
|
|
int *decodeWaitedOn_r,
|
|
|
|
int *next_r)
|
|
|
|
{
|
|
|
|
if(pc->lockQueue) {
|
|
|
|
pc->queueLockState = PLAYER_QUEUE_LOCKED;
|
|
|
|
pc->lockQueue = 0;
|
|
|
|
wakeup_main_task();
|
|
|
|
}
|
|
|
|
if(pc->unlockQueue) {
|
|
|
|
pc->queueLockState = PLAYER_QUEUE_UNLOCKED;
|
|
|
|
pc->unlockQueue = 0;
|
|
|
|
wakeup_main_task();
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2008-04-12 06:12:01 +02:00
|
|
|
if(pc->pause) {
|
|
|
|
*pause_r = !*pause_r;
|
|
|
|
if (*pause_r) {
|
|
|
|
pc->state = PLAYER_STATE_PAUSE;
|
|
|
|
} else {
|
|
|
|
if (openAudioDevice(NULL) >= 0) {
|
|
|
|
pc->state = PLAYER_STATE_PLAY;
|
|
|
|
} else {
|
|
|
|
char tmp[MPD_PATH_MAX];
|
|
|
|
pc->errored_song = pc->current_song;
|
|
|
|
pc->error = PLAYER_ERROR_AUDIO;
|
|
|
|
ERROR("problems opening audio device "
|
|
|
|
"while playing \"%s\"\n",
|
|
|
|
get_song_url(tmp, pc->current_song));
|
|
|
|
*pause_r = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pc->pause = 0;
|
|
|
|
wakeup_main_task();
|
|
|
|
if (*pause_r == -1) {
|
|
|
|
*pause_r = 1;
|
|
|
|
} else if (*pause_r) {
|
|
|
|
dropBufferedAudio();
|
|
|
|
closeAudioDevice();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(pc->seek) {
|
|
|
|
dropBufferedAudio();
|
|
|
|
if(decodeSeek(pc,dc,cb,decodeWaitedOn_r,next_r) == 0) {
|
|
|
|
*doCrossFade_r = 0;
|
|
|
|
*bbp_r = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void decodeStart(PlayerControl * pc, OutputBuffer * cb,
|
|
|
|
DecoderControl * dc)
|
|
|
|
{
|
2006-07-14 21:52:29 +02:00
|
|
|
int ret;
|
2008-01-01 11:09:56 +01:00
|
|
|
int close_instream = 1;
|
2006-07-14 21:52:29 +02:00
|
|
|
InputStream inStream;
|
2006-07-20 18:02:40 +02:00
|
|
|
InputPlugin *plugin = NULL;
|
2008-04-12 06:08:29 +02:00
|
|
|
char path_max_fs[MPD_PATH_MAX];
|
|
|
|
char path_max_utf8[MPD_PATH_MAX];
|
2004-04-11 20:27:12 +02:00
|
|
|
|
2008-04-12 06:08:29 +02:00
|
|
|
if (!get_song_url(path_max_utf8, pc->current_song)) {
|
|
|
|
dc->error = DECODE_ERROR_FILE;
|
|
|
|
goto stop_no_close;
|
|
|
|
}
|
|
|
|
if (!isRemoteUrl(path_max_utf8)) {
|
|
|
|
rmp2amp_r(path_max_fs,
|
|
|
|
utf8_to_fs_charset(path_max_fs, path_max_utf8));
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-04-12 06:08:29 +02:00
|
|
|
dc->current_song = pc->current_song; /* NEED LOCK */
|
|
|
|
if (openInputStream(&inStream, path_max_fs) < 0) {
|
2004-05-18 15:13:55 +02:00
|
|
|
dc->error = DECODE_ERROR_FILE;
|
2008-01-01 11:09:56 +01:00
|
|
|
goto stop_no_close;
|
2006-07-14 21:52:29 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-14 21:52:29 +02:00
|
|
|
dc->state = DECODE_STATE_START;
|
2004-05-19 04:14:20 +02:00
|
|
|
dc->start = 0;
|
|
|
|
|
2006-12-23 20:57:28 +01:00
|
|
|
/* for http streams, seekable is determined in bufferInputStream */
|
|
|
|
dc->seekable = inStream.seekable;
|
2008-04-12 06:07:44 +02:00
|
|
|
|
2008-01-01 11:09:56 +01:00
|
|
|
if (dc->stop)
|
|
|
|
goto stop;
|
2004-05-31 13:42:46 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
ret = DECODE_ERROR_UNKTYPE;
|
2008-04-12 06:08:29 +02:00
|
|
|
if (isRemoteUrl(path_max_utf8)) {
|
2006-03-16 07:52:46 +01:00
|
|
|
unsigned int next = 0;
|
|
|
|
|
|
|
|
/* first we try mime types: */
|
2007-06-04 20:55:46 +02:00
|
|
|
while (ret && (plugin = getInputPluginFromMimeType(inStream.mime, next++))) {
|
2006-03-16 07:52:46 +01:00
|
|
|
if (!plugin->streamDecodeFunc)
|
|
|
|
continue;
|
|
|
|
if (!(plugin->streamTypes & INPUT_PLUGIN_STREAM_URL))
|
|
|
|
continue;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (plugin->tryDecodeFunc
|
|
|
|
&& !plugin->tryDecodeFunc(&inStream))
|
2006-03-16 07:52:46 +01:00
|
|
|
continue;
|
|
|
|
ret = plugin->streamDecodeFunc(cb, dc, &inStream);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if that fails, try suffix matching the URL: */
|
2006-07-20 18:02:40 +02:00
|
|
|
if (plugin == NULL) {
|
2008-04-12 06:08:29 +02:00
|
|
|
const char *s = getSuffix(path_max_utf8);
|
2006-03-16 07:52:46 +01:00
|
|
|
next = 0;
|
2007-06-04 20:55:46 +02:00
|
|
|
while (ret && (plugin = getInputPluginFromSuffix(s, next++))) {
|
2006-03-16 07:52:46 +01:00
|
|
|
if (!plugin->streamDecodeFunc)
|
|
|
|
continue;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!(plugin->streamTypes &
|
|
|
|
INPUT_PLUGIN_STREAM_URL))
|
2006-03-16 07:52:46 +01:00
|
|
|
continue;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (plugin->tryDecodeFunc &&
|
|
|
|
!plugin->tryDecodeFunc(&inStream))
|
2006-03-16 07:52:46 +01:00
|
|
|
continue;
|
2007-06-04 20:55:46 +02:00
|
|
|
ret = plugin->streamDecodeFunc(cb, dc, &inStream);
|
2006-03-16 07:52:46 +01:00
|
|
|
break;
|
|
|
|
}
|
2006-07-14 21:52:29 +02:00
|
|
|
}
|
2006-03-16 07:52:46 +01:00
|
|
|
/* fallback to mp3: */
|
2006-07-14 21:52:29 +02:00
|
|
|
/* this is needed for bastard streams that don't have a suffix
|
2006-07-20 18:02:40 +02:00
|
|
|
or set the mimeType */
|
|
|
|
if (plugin == NULL) {
|
2006-03-16 07:52:46 +01:00
|
|
|
/* we already know our mp3Plugin supports streams, no
|
|
|
|
* need to check for stream{Types,DecodeFunc} */
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
if ((plugin = getInputPluginFromName("mp3"))) {
|
|
|
|
ret = plugin->streamDecodeFunc(cb, dc,
|
|
|
|
&inStream);
|
|
|
|
}
|
2006-07-14 21:52:29 +02:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2006-03-16 07:52:46 +01:00
|
|
|
unsigned int next = 0;
|
2008-04-12 06:08:29 +02:00
|
|
|
const char *s = getSuffix(path_max_utf8);
|
2006-03-16 07:52:46 +01:00
|
|
|
while (ret && (plugin = getInputPluginFromSuffix(s, next++))) {
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!plugin->streamTypes & INPUT_PLUGIN_STREAM_FILE)
|
2006-03-16 07:52:46 +01:00
|
|
|
continue;
|
2007-06-25 15:22:51 +02:00
|
|
|
|
|
|
|
if (plugin->tryDecodeFunc &&
|
|
|
|
!plugin->tryDecodeFunc(&inStream))
|
2006-03-16 07:52:46 +01:00
|
|
|
continue;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2007-06-25 15:22:51 +02:00
|
|
|
if (plugin->fileDecodeFunc) {
|
2006-03-16 07:52:46 +01:00
|
|
|
closeInputStream(&inStream);
|
2008-01-01 11:09:56 +01:00
|
|
|
close_instream = 0;
|
2007-12-28 03:56:25 +01:00
|
|
|
ret = plugin->fileDecodeFunc(cb, dc,
|
2008-04-12 06:08:29 +02:00
|
|
|
path_max_fs);
|
2007-06-25 15:22:51 +02:00
|
|
|
break;
|
|
|
|
} else if (plugin->streamDecodeFunc) {
|
|
|
|
ret = plugin->streamDecodeFunc(cb, dc, &inStream);
|
|
|
|
break;
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
2004-05-31 03:21:17 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (ret < 0 || ret == DECODE_ERROR_UNKTYPE) {
|
2008-04-12 06:08:29 +02:00
|
|
|
pc->errored_song = pc->current_song;
|
2008-01-01 11:09:56 +01:00
|
|
|
if (ret != DECODE_ERROR_UNKTYPE)
|
2006-07-20 18:02:40 +02:00
|
|
|
dc->error = DECODE_ERROR_FILE;
|
2008-01-01 11:09:56 +01:00
|
|
|
else
|
2006-07-14 21:52:29 +02:00
|
|
|
dc->error = DECODE_ERROR_UNKTYPE;
|
2004-05-18 15:13:55 +02:00
|
|
|
}
|
2008-01-01 11:09:56 +01:00
|
|
|
|
|
|
|
stop:
|
|
|
|
if (close_instream)
|
|
|
|
closeInputStream(&inStream);
|
|
|
|
stop_no_close:
|
|
|
|
dc->state = DECODE_STATE_STOP;
|
|
|
|
dc->stop = 0;
|
2004-05-18 15:13:55 +02:00
|
|
|
}
|
|
|
|
|
2008-04-12 06:19:50 +02:00
|
|
|
static void * decoder_task(void *arg)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-04-12 06:19:50 +02:00
|
|
|
DecoderControl *dc = arg;
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
OutputBuffer *cb = &(getPlayerData()->buffer);
|
|
|
|
PlayerControl *pc = &(getPlayerData()->playerControl);
|
|
|
|
|
2008-04-12 06:14:32 +02:00
|
|
|
notifyEnter(&dc->notify);
|
|
|
|
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
while (1) {
|
|
|
|
if (dc->start || dc->seek) {
|
|
|
|
decodeStart(pc, cb, dc);
|
|
|
|
} else if (dc->stop) {
|
|
|
|
dc->state = DECODE_STATE_STOP;
|
|
|
|
dc->stop = 0;
|
|
|
|
decoder_wakeup_player();
|
|
|
|
} else {
|
2008-04-12 06:18:43 +02:00
|
|
|
decoder_sleep(dc);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-04-12 06:19:50 +02:00
|
|
|
void decoderInit(DecoderControl * dc)
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
{
|
|
|
|
pthread_attr_t attr;
|
|
|
|
pthread_t decoder_thread;
|
|
|
|
|
|
|
|
pthread_attr_init(&attr);
|
|
|
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
2008-04-12 06:19:50 +02:00
|
|
|
if (pthread_create(&decoder_thread, &attr, decoder_task, dc))
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
FATAL("Failed to spawn decoder task: %s\n", strerror(errno));
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-04-12 06:13:36 +02:00
|
|
|
static void crossFade(OutputBufferChunk * a, OutputBufferChunk * b,
|
|
|
|
AudioFormat * format,
|
2008-04-12 06:15:35 +02:00
|
|
|
unsigned int fadePosition, unsigned int crossFadeChunks)
|
2008-04-12 06:13:36 +02:00
|
|
|
{
|
2008-04-12 06:16:13 +02:00
|
|
|
assert(fadePosition <= crossFadeChunks);
|
|
|
|
|
2008-04-12 06:13:36 +02:00
|
|
|
pcm_mix(a->data,
|
|
|
|
b->data,
|
|
|
|
a->chunkSize,
|
|
|
|
b->chunkSize,
|
|
|
|
format,
|
|
|
|
((float)fadePosition) /
|
|
|
|
crossFadeChunks);
|
|
|
|
if (b->chunkSize > a->chunkSize)
|
|
|
|
a->chunkSize = b->chunkSize;
|
|
|
|
}
|
|
|
|
|
2008-04-12 06:13:56 +02:00
|
|
|
static int playChunk(PlayerControl * pc, OutputBufferChunk * chunk,
|
|
|
|
AudioFormat * format, double sizeToTime)
|
|
|
|
{
|
|
|
|
pc->elapsedTime = chunk->times;
|
|
|
|
pc->bitRate = chunk->bitRate;
|
|
|
|
|
|
|
|
pcm_volumeChange(chunk->data, chunk->chunkSize,
|
|
|
|
format, pc->softwareVolume);
|
|
|
|
|
|
|
|
if (playAudio(chunk->data,
|
|
|
|
chunk->chunkSize) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pc->totalPlayTime +=
|
|
|
|
sizeToTime * chunk->chunkSize;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-20 02:50:44 +02:00
|
|
|
static void decodeParent(PlayerControl * pc, DecoderControl * dc, OutputBuffer * cb)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-04-12 06:16:32 +02:00
|
|
|
int do_pause = 0;
|
2008-04-12 06:12:31 +02:00
|
|
|
int buffering = 1;
|
2008-04-12 06:06:17 +02:00
|
|
|
unsigned int bbp = buffered_before_play;
|
2008-04-12 06:12:26 +02:00
|
|
|
/** cross fading enabled for the current song? 0=must check;
|
|
|
|
1=enabled; -1=disabled */
|
2004-05-19 04:14:20 +02:00
|
|
|
int doCrossFade = 0;
|
2008-04-12 06:16:44 +02:00
|
|
|
unsigned int crossFadeChunks = 0;
|
2008-04-12 06:12:26 +02:00
|
|
|
/** the position of the next cross-faded chunk in the next
|
|
|
|
song */
|
2008-04-12 06:16:44 +02:00
|
|
|
int nextChunk = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
int decodeWaitedOn = 0;
|
2008-01-01 11:10:02 +01:00
|
|
|
static const char silence[CHUNK_SIZE];
|
2004-06-03 14:34:25 +02:00
|
|
|
double sizeToTime = 0.0;
|
2008-04-12 06:12:26 +02:00
|
|
|
/** the position of the first chunk in the next song */
|
2004-06-10 21:48:53 +02:00
|
|
|
int next = -1;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (waitOnDecode(pc, dc, cb, &decodeWaitedOn) < 0)
|
|
|
|
return;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
pc->elapsedTime = 0;
|
2004-05-19 04:14:20 +02:00
|
|
|
pc->state = PLAYER_STATE_PLAY;
|
|
|
|
pc->play = 0;
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
wakeup_main_task();
|
2004-05-20 05:44:33 +02:00
|
|
|
|
2008-04-12 06:13:05 +02:00
|
|
|
while (1) {
|
2008-04-12 06:12:01 +02:00
|
|
|
processDecodeInput(pc, dc, cb,
|
2008-04-12 06:16:32 +02:00
|
|
|
&do_pause, &bbp, &doCrossFade,
|
2008-04-12 06:13:00 +02:00
|
|
|
&decodeWaitedOn, &next);
|
2008-04-12 06:12:01 +02:00
|
|
|
if (pc->stop) {
|
|
|
|
dropBufferedAudio();
|
2008-04-12 06:14:14 +02:00
|
|
|
break;
|
2008-04-12 06:12:01 +02:00
|
|
|
}
|
|
|
|
|
2008-04-12 06:12:31 +02:00
|
|
|
if (buffering) {
|
|
|
|
if (availableOutputBuffer(cb) < bbp) {
|
|
|
|
/* not enough decoded buffer space yet */
|
2008-04-12 06:18:49 +02:00
|
|
|
player_sleep(pc);
|
2008-04-12 06:12:31 +02:00
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
/* buffering is complete */
|
|
|
|
buffering = 0;
|
2008-04-12 06:12:01 +02:00
|
|
|
}
|
|
|
|
|
2008-04-12 06:12:21 +02:00
|
|
|
if (decodeWaitedOn) {
|
|
|
|
if(dc->state!=DECODE_STATE_START &&
|
|
|
|
dc->error==DECODE_ERROR_NOERROR) {
|
2008-04-12 06:12:26 +02:00
|
|
|
/* the decoder is ready and ok */
|
2008-04-12 06:12:21 +02:00
|
|
|
decodeWaitedOn = 0;
|
|
|
|
if(openAudioDevice(&(cb->audioFormat))<0) {
|
|
|
|
char tmp[MPD_PATH_MAX];
|
|
|
|
pc->errored_song = pc->current_song;
|
|
|
|
pc->error = PLAYER_ERROR_AUDIO;
|
|
|
|
ERROR("problems opening audio device "
|
|
|
|
"while playing \"%s\"\n",
|
|
|
|
get_song_url(tmp, pc->current_song));
|
2008-04-12 06:14:14 +02:00
|
|
|
break;
|
2008-04-12 06:12:21 +02:00
|
|
|
} else {
|
2008-04-12 06:18:49 +02:00
|
|
|
player_wakeup_decoder(pc, dc);
|
2008-04-12 06:12:21 +02:00
|
|
|
}
|
2008-04-12 06:16:32 +02:00
|
|
|
if (do_pause) {
|
2008-04-12 06:12:21 +02:00
|
|
|
dropBufferedAudio();
|
|
|
|
closeAudioDevice();
|
|
|
|
}
|
|
|
|
pc->totalTime = dc->totalTime;
|
|
|
|
pc->sampleRate = dc->audioFormat.sampleRate;
|
|
|
|
pc->bits = dc->audioFormat.bits;
|
|
|
|
pc->channels = dc->audioFormat.channels;
|
2008-04-12 06:14:09 +02:00
|
|
|
sizeToTime = audioFormatSizeToTime(&cb->audioFormat);
|
2008-04-12 06:12:21 +02:00
|
|
|
}
|
|
|
|
else if(dc->state!=DECODE_STATE_START) {
|
2008-04-12 06:12:26 +02:00
|
|
|
/* the decoder failed */
|
2008-04-12 06:12:21 +02:00
|
|
|
pc->errored_song = pc->current_song;
|
|
|
|
pc->error = PLAYER_ERROR_FILE;
|
2008-04-12 06:14:14 +02:00
|
|
|
break;
|
2008-04-12 06:12:21 +02:00
|
|
|
}
|
|
|
|
else {
|
2008-04-12 06:12:26 +02:00
|
|
|
/* the decoder is not yet ready; wait
|
|
|
|
some more */
|
2008-04-12 06:18:49 +02:00
|
|
|
player_sleep(pc);
|
2008-04-12 06:12:21 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (dc->state == DECODE_STATE_STOP &&
|
|
|
|
pc->queueState == PLAYER_QUEUE_FULL &&
|
|
|
|
pc->queueLockState == PLAYER_QUEUE_UNLOCKED) {
|
2008-04-12 06:12:26 +02:00
|
|
|
/* the decoder has finished the current song;
|
|
|
|
make it decode the next song */
|
2004-06-10 21:48:53 +02:00
|
|
|
next = cb->end;
|
2004-05-19 04:14:20 +02:00
|
|
|
dc->start = 1;
|
|
|
|
pc->queueState = PLAYER_QUEUE_DECODE;
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
wakeup_main_task();
|
2008-04-12 06:18:43 +02:00
|
|
|
player_wakeup_decoder_nb(dc);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (next >= 0 && doCrossFade == 0 && !dc->start &&
|
|
|
|
dc->state != DECODE_STATE_START) {
|
2008-04-12 06:12:26 +02:00
|
|
|
/* enable cross fading in this song? if yes,
|
|
|
|
calculate how many chunks will be required
|
|
|
|
for it */
|
2008-04-12 06:14:01 +02:00
|
|
|
crossFadeChunks =
|
|
|
|
calculateCrossFadeChunks(pc,
|
|
|
|
&(cb->
|
|
|
|
audioFormat),
|
|
|
|
dc->totalTime);
|
|
|
|
if (crossFadeChunks > 0) {
|
2004-05-19 04:14:20 +02:00
|
|
|
doCrossFade = 1;
|
2008-04-12 06:13:00 +02:00
|
|
|
nextChunk = -1;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
2008-04-12 06:14:01 +02:00
|
|
|
/* cross fading is disabled or the
|
|
|
|
next song is too short */
|
2006-07-20 18:02:40 +02:00
|
|
|
doCrossFade = -1;
|
2004-05-19 04:14:20 +02:00
|
|
|
}
|
2004-06-10 21:48:53 +02:00
|
|
|
|
2008-04-12 06:16:32 +02:00
|
|
|
if (do_pause)
|
2008-04-12 06:18:49 +02:00
|
|
|
player_sleep(pc);
|
2008-04-12 06:18:28 +02:00
|
|
|
else if (!outputBufferEmpty(cb) && (int)cb->begin != next) {
|
2008-04-12 06:13:24 +02:00
|
|
|
OutputBufferChunk *beginChunk =
|
|
|
|
outputBufferGetChunk(cb, cb->begin);
|
2008-04-12 06:12:36 +02:00
|
|
|
unsigned int fadePosition;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (doCrossFade == 1 && next >= 0 &&
|
2008-04-12 06:12:53 +02:00
|
|
|
(fadePosition = outputBufferRelative(cb, next))
|
|
|
|
<= crossFadeChunks) {
|
2008-04-12 06:12:26 +02:00
|
|
|
/* perform cross fade */
|
2006-07-20 18:02:40 +02:00
|
|
|
if (nextChunk < 0) {
|
2008-04-12 06:12:26 +02:00
|
|
|
/* beginning of the cross fade
|
|
|
|
- adjust crossFadeChunks
|
|
|
|
which might be bigger than
|
|
|
|
the remaining number of
|
|
|
|
chunks in the old song */
|
2004-05-19 04:14:20 +02:00
|
|
|
crossFadeChunks = fadePosition;
|
|
|
|
}
|
2008-04-12 06:12:42 +02:00
|
|
|
nextChunk = outputBufferAbsolute(cb, crossFadeChunks);
|
|
|
|
if (nextChunk >= 0) {
|
2008-04-12 06:13:36 +02:00
|
|
|
crossFade(beginChunk,
|
|
|
|
outputBufferGetChunk(cb, nextChunk),
|
|
|
|
&(cb->audioFormat),
|
|
|
|
fadePosition,
|
|
|
|
crossFadeChunks);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2008-04-12 06:12:26 +02:00
|
|
|
/* there are not enough
|
|
|
|
decoded chunks yet */
|
2006-07-20 18:02:40 +02:00
|
|
|
if (dc->state == DECODE_STATE_STOP) {
|
2008-04-12 06:12:26 +02:00
|
|
|
/* the decoder isn't
|
|
|
|
running, abort
|
|
|
|
cross fading */
|
2004-05-19 04:14:20 +02:00
|
|
|
doCrossFade = -1;
|
2008-04-12 06:13:41 +02:00
|
|
|
} else {
|
2008-04-12 06:12:26 +02:00
|
|
|
/* wait for the
|
|
|
|
decoder */
|
2008-04-12 06:18:49 +02:00
|
|
|
player_sleep(pc);
|
2006-07-20 18:02:40 +02:00
|
|
|
continue;
|
2008-04-12 06:13:41 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
2008-04-12 06:12:26 +02:00
|
|
|
|
|
|
|
/* play the current chunk */
|
2008-04-12 06:13:56 +02:00
|
|
|
if (playChunk(pc, beginChunk, &(cb->audioFormat),
|
|
|
|
sizeToTime) < 0)
|
2008-04-12 06:13:05 +02:00
|
|
|
break;
|
2008-04-12 06:13:51 +02:00
|
|
|
outputBufferShift(cb);
|
2008-04-12 06:18:43 +02:00
|
|
|
player_wakeup_decoder_nb(dc);
|
2008-04-12 06:18:28 +02:00
|
|
|
} else if (!outputBufferEmpty(cb) && (int)cb->begin == next) {
|
2008-04-12 06:12:26 +02:00
|
|
|
/* at the beginning of a new song */
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (doCrossFade == 1 && nextChunk >= 0) {
|
2008-04-12 06:12:26 +02:00
|
|
|
/* the cross-fade is finished; skip
|
|
|
|
the section which was cross-faded
|
|
|
|
(and thus already played) */
|
2008-04-12 06:18:19 +02:00
|
|
|
output_buffer_skip(cb, crossFadeChunks);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2008-04-12 06:12:26 +02:00
|
|
|
|
2008-04-12 06:13:45 +02:00
|
|
|
doCrossFade = 0;
|
2008-04-12 06:12:01 +02:00
|
|
|
|
2008-04-12 06:13:45 +02:00
|
|
|
/* wait for the decoder to work on the new song */
|
|
|
|
if (pc->queueState == PLAYER_QUEUE_DECODE ||
|
|
|
|
pc->queueLockState == PLAYER_QUEUE_LOCKED) {
|
2008-04-12 06:18:49 +02:00
|
|
|
player_sleep(pc);
|
2008-04-12 06:13:45 +02:00
|
|
|
continue;
|
2004-05-19 04:14:20 +02:00
|
|
|
}
|
2008-04-12 06:11:51 +02:00
|
|
|
if (pc->queueState != PLAYER_QUEUE_PLAY)
|
2004-02-24 00:41:20 +01:00
|
|
|
break;
|
2008-04-12 06:11:46 +02:00
|
|
|
|
|
|
|
next = -1;
|
|
|
|
if (waitOnDecode(pc, dc, cb, &decodeWaitedOn) < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pc->queueState = PLAYER_QUEUE_EMPTY;
|
|
|
|
wakeup_main_task();
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
} else if (dc->state == DECODE_STATE_STOP && !dc->start) {
|
2004-05-19 04:14:20 +02:00
|
|
|
break;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2007-01-24 03:56:45 +01:00
|
|
|
/*DEBUG("waiting for decoded audio, play silence\n");*/
|
2006-07-20 18:02:40 +02:00
|
|
|
if (playAudio(silence, CHUNK_SIZE) < 0)
|
2008-04-12 06:11:51 +02:00
|
|
|
break;
|
2004-05-19 04:14:20 +02:00
|
|
|
}
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
quitDecode(pc, dc);
|
2004-05-19 04:14:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* decode w/ buffering
|
|
|
|
* this will fork another process
|
|
|
|
* child process does decoding
|
|
|
|
* parent process does playing audio
|
|
|
|
*/
|
2006-08-08 04:23:21 +02:00
|
|
|
void decode(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
OutputBuffer *cb;
|
|
|
|
PlayerControl *pc;
|
|
|
|
DecoderControl *dc;
|
2004-05-19 04:14:20 +02:00
|
|
|
|
|
|
|
cb = &(getPlayerData()->buffer);
|
2008-04-12 06:12:16 +02:00
|
|
|
clearOutputBuffer(cb);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-05-19 04:14:20 +02:00
|
|
|
pc = &(getPlayerData()->playerControl);
|
|
|
|
dc = &(getPlayerData()->decoderControl);
|
2008-04-12 06:12:07 +02:00
|
|
|
dc->error = DECODE_ERROR_NOERROR;
|
2006-07-14 21:52:29 +02:00
|
|
|
dc->seek = 0;
|
|
|
|
dc->stop = 0;
|
2004-05-20 05:44:33 +02:00
|
|
|
dc->start = 1;
|
2008-04-12 06:18:49 +02:00
|
|
|
do { player_wakeup_decoder(pc, dc); } while (dc->start);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-14 21:52:29 +02:00
|
|
|
decodeParent(pc, dc, cb);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|