2011-06-12 02:45:02 +02:00
|
|
|
// Copyright (c) 2011, Niels Martin Hansen
|
2007-04-22 23:59:35 +02:00
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file audio_player_alsa.cpp
|
|
|
|
/// @brief ALSA-based audio output
|
|
|
|
/// @ingroup audio_output
|
|
|
|
///
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2008-01-21 21:57:20 +01:00
|
|
|
#ifdef WITH_ALSA
|
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "audio_player_alsa.h"
|
2012-01-08 02:33:39 +01:00
|
|
|
|
|
|
|
#include "audio_controller.h"
|
2011-11-07 06:24:46 +01:00
|
|
|
#include "include/aegisub/audio_provider.h"
|
2010-05-24 03:46:04 +02:00
|
|
|
#include "compat.h"
|
2007-04-22 23:59:35 +02:00
|
|
|
#include "frame_main.h"
|
2013-01-07 02:50:09 +01:00
|
|
|
#include "options.h"
|
2011-11-07 06:24:46 +01:00
|
|
|
|
2013-10-24 22:17:53 +02:00
|
|
|
#include <libaegisub/log.h>
|
|
|
|
#include <libaegisub/util.h>
|
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
#include <algorithm>
|
2012-06-25 16:21:32 +02:00
|
|
|
|
|
|
|
#include <inttypes.h>
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
class PthreadMutexLocker {
|
|
|
|
pthread_mutex_t &mutex;
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
PthreadMutexLocker(const PthreadMutexLocker &); // uncopyable
|
|
|
|
PthreadMutexLocker(); // no default
|
2012-02-20 19:22:37 +01:00
|
|
|
PthreadMutexLocker& operator=(PthreadMutexLocker const&);
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
public:
|
|
|
|
explicit PthreadMutexLocker(pthread_mutex_t &mutex) : mutex(mutex)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
}
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
~PthreadMutexLocker()
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
}
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
int WaitCondition(pthread_cond_t &cond)
|
|
|
|
{
|
|
|
|
return pthread_cond_wait(&cond, &mutex);
|
|
|
|
}
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
int WaitConditionTimeout(pthread_cond_t &cond, int ms)
|
|
|
|
{
|
|
|
|
timespec abstime;
|
|
|
|
clock_gettime(CLOCK_REALTIME, &abstime);
|
|
|
|
abstime.tv_nsec += ms * 1000000;
|
|
|
|
return pthread_cond_timedwait(&cond, &mutex, &abstime);
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
2011-06-12 02:45:02 +02:00
|
|
|
};
|
2007-04-22 23:59:35 +02:00
|
|
|
|
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
class ScopedAliveFlag {
|
2012-02-20 19:22:37 +01:00
|
|
|
bool &flag;
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
ScopedAliveFlag(const ScopedAliveFlag &); // uncopyable
|
|
|
|
ScopedAliveFlag(); // no default
|
2012-03-25 06:05:06 +02:00
|
|
|
ScopedAliveFlag& operator=(ScopedAliveFlag const&);
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
public:
|
2012-02-20 19:22:37 +01:00
|
|
|
explicit ScopedAliveFlag(bool &var) : flag(var) { flag = true; }
|
2011-06-12 02:45:02 +02:00
|
|
|
~ScopedAliveFlag() { flag = false; }
|
|
|
|
};
|
2007-04-26 20:34:36 +02:00
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
struct PlaybackState {
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
pthread_cond_t cond;
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2012-02-20 19:22:37 +01:00
|
|
|
bool playing;
|
|
|
|
bool alive;
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2012-02-20 19:22:37 +01:00
|
|
|
bool signal_start;
|
|
|
|
bool signal_stop;
|
|
|
|
bool signal_close;
|
|
|
|
bool signal_volume;
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2012-02-20 19:22:37 +01:00
|
|
|
double volume;
|
|
|
|
int64_t start_position;
|
|
|
|
int64_t end_position;
|
2007-04-26 22:11:48 +02:00
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
AudioProvider *provider;
|
|
|
|
std::string device_name;
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
int64_t last_position;
|
|
|
|
timespec last_position_time;
|
|
|
|
|
|
|
|
PlaybackState()
|
|
|
|
{
|
|
|
|
pthread_mutex_init(&mutex, 0);
|
|
|
|
pthread_cond_init(&cond, 0);
|
|
|
|
|
|
|
|
Reset();
|
|
|
|
volume = 1.0;
|
2007-04-26 20:34:36 +02:00
|
|
|
}
|
2010-06-09 01:21:39 +02:00
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
~PlaybackState()
|
|
|
|
{
|
|
|
|
pthread_cond_destroy(&cond);
|
|
|
|
pthread_mutex_destroy(&mutex);
|
2007-04-26 20:34:36 +02:00
|
|
|
}
|
2011-06-12 02:45:02 +02:00
|
|
|
|
|
|
|
void Reset()
|
|
|
|
{
|
|
|
|
playing = false;
|
|
|
|
alive = false;
|
|
|
|
signal_start = false;
|
|
|
|
signal_stop = false;
|
|
|
|
signal_close = false;
|
|
|
|
signal_volume = false;
|
|
|
|
start_position = 0;
|
|
|
|
end_position = 0;
|
|
|
|
last_position = 0;
|
2012-10-15 06:37:14 +02:00
|
|
|
memset(&last_position_time, 0, sizeof last_position_time);
|
2011-06-12 02:45:02 +02:00
|
|
|
provider = 0;
|
2007-04-26 20:34:36 +02:00
|
|
|
}
|
2011-06-12 02:45:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void *playback_thread(void *arg)
|
|
|
|
{
|
|
|
|
// This is exception-free territory!
|
|
|
|
// Return a pointer to a static string constant describing the error, or 0 on no error
|
|
|
|
PlaybackState &ps = *(PlaybackState*)arg;
|
|
|
|
|
|
|
|
PthreadMutexLocker ml(ps.mutex);
|
|
|
|
ScopedAliveFlag alive_flag(ps.alive);
|
|
|
|
|
|
|
|
snd_pcm_t *pcm = 0;
|
|
|
|
if (snd_pcm_open(&pcm, ps.device_name.c_str(), SND_PCM_STREAM_PLAYBACK, 0) != 0)
|
2011-11-07 06:24:46 +01:00
|
|
|
return (void*)"snd_pcm_open";
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "opened pcm";
|
2011-06-12 02:45:02 +02:00
|
|
|
|
|
|
|
do_setup:
|
|
|
|
snd_pcm_format_t pcm_format;
|
|
|
|
switch (ps.provider->GetBytesPerSample())
|
|
|
|
{
|
|
|
|
case 1:
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "format U8";
|
2011-06-12 02:45:02 +02:00
|
|
|
pcm_format = SND_PCM_FORMAT_U8;
|
|
|
|
break;
|
|
|
|
case 2:
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "format S16_LE";
|
2011-06-12 02:45:02 +02:00
|
|
|
pcm_format = SND_PCM_FORMAT_S16_LE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
snd_pcm_close(pcm);
|
2011-11-07 06:24:46 +01:00
|
|
|
return (void*)"snd_pcm_format_t";
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
2011-06-12 02:45:02 +02:00
|
|
|
if (snd_pcm_set_params(pcm,
|
|
|
|
pcm_format,
|
|
|
|
SND_PCM_ACCESS_RW_INTERLEAVED,
|
|
|
|
ps.provider->GetChannels(),
|
|
|
|
ps.provider->GetSampleRate(),
|
|
|
|
1, // allow resample
|
|
|
|
100*1000 // 100 milliseconds latency
|
|
|
|
) != 0)
|
2011-11-07 06:24:46 +01:00
|
|
|
return (void*)"snd_pcm_set_params";
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "set pcm params";
|
2011-06-12 02:45:02 +02:00
|
|
|
|
|
|
|
size_t framesize = ps.provider->GetChannels() * ps.provider->GetBytesPerSample();
|
|
|
|
|
|
|
|
ps.signal_close = false;
|
|
|
|
while (ps.signal_close == false)
|
|
|
|
{
|
|
|
|
// Wait for condition to trigger
|
|
|
|
if (!ps.signal_start)
|
|
|
|
ml.WaitCondition(ps.cond);
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "outer loop, condition happened";
|
2011-06-12 02:45:02 +02:00
|
|
|
|
|
|
|
if (ps.signal_start == false || ps.end_position <= ps.start_position)
|
|
|
|
{
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "nothing to play, rewaiting";
|
2012-01-22 19:18:07 +01:00
|
|
|
ps.signal_start = false;
|
2011-06-12 02:45:02 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "starting playback";
|
2011-06-12 02:45:02 +02:00
|
|
|
int64_t position = ps.start_position;
|
|
|
|
|
|
|
|
// Playback position
|
|
|
|
ps.last_position = position;
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ps.last_position_time);
|
|
|
|
|
|
|
|
// Initial buffer-fill
|
|
|
|
snd_pcm_sframes_t avail = std::min(snd_pcm_avail(pcm), (snd_pcm_sframes_t)(ps.end_position-position));
|
|
|
|
char *buf = new char[avail*framesize];
|
|
|
|
ps.provider->GetAudioWithVolume(buf, position, avail, ps.volume);
|
|
|
|
snd_pcm_sframes_t written = 0;
|
|
|
|
while (written <= 0)
|
|
|
|
{
|
|
|
|
written = snd_pcm_writei(pcm, buf, avail);
|
|
|
|
if (written == -ESTRPIPE)
|
|
|
|
{
|
|
|
|
snd_pcm_recover(pcm, written, 0);
|
|
|
|
}
|
|
|
|
else if (written <= 0)
|
|
|
|
{
|
|
|
|
delete[] buf;
|
|
|
|
snd_pcm_close(pcm);
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "error filling buffer";
|
2011-11-07 06:24:46 +01:00
|
|
|
return (void*)"snd_pcm_writei";
|
2011-06-12 02:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
delete[] buf;
|
|
|
|
position += written;
|
|
|
|
|
|
|
|
// Start playback
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "initial buffer filled, hitting start";
|
2011-06-12 02:45:02 +02:00
|
|
|
snd_pcm_start(pcm);
|
|
|
|
|
|
|
|
ps.signal_start = false;
|
|
|
|
ps.signal_stop = false;
|
|
|
|
|
|
|
|
while (ps.signal_stop == false)
|
|
|
|
{
|
2012-05-15 16:06:39 +02:00
|
|
|
int64_t orig_position = position;
|
|
|
|
int64_t orig_ps_end_position = ps.end_position;
|
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
ScopedAliveFlag playing_flag(ps.playing);
|
|
|
|
|
|
|
|
// Sleep a bit, or until an event
|
|
|
|
ml.WaitConditionTimeout(ps.cond, 50);
|
2012-02-20 19:22:37 +01:00
|
|
|
//LOG_D("audio/player/alsa") << "playback loop, out of wait";
|
2011-06-12 02:45:02 +02:00
|
|
|
|
|
|
|
// Check for stop signal
|
|
|
|
if (ps.signal_stop == true)
|
|
|
|
{
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "playback loop, stop signal";
|
2011-06-12 02:45:02 +02:00
|
|
|
snd_pcm_drop(pcm);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Playback position
|
|
|
|
snd_pcm_sframes_t delay;
|
|
|
|
if (snd_pcm_delay(pcm, &delay) == 0)
|
|
|
|
{
|
|
|
|
ps.last_position = position - delay;
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ps.last_position_time);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill buffer
|
2012-10-15 06:37:14 +02:00
|
|
|
snd_pcm_sframes_t tmp_pcm_avail = snd_pcm_avail(pcm);
|
2012-05-15 16:06:39 +02:00
|
|
|
if (tmp_pcm_avail == -EPIPE)
|
2012-02-20 19:22:43 +01:00
|
|
|
{
|
|
|
|
if (snd_pcm_recover(pcm, -EPIPE, 1) < 0)
|
|
|
|
{
|
|
|
|
LOG_D("audio/player/alsa") << "failed to recover from underrun";
|
|
|
|
return (void*)"snd_pcm_avail";
|
|
|
|
}
|
2012-05-15 16:06:39 +02:00
|
|
|
tmp_pcm_avail = snd_pcm_avail(pcm);
|
|
|
|
}
|
|
|
|
avail = std::min(tmp_pcm_avail, (snd_pcm_sframes_t)(ps.end_position-position));
|
|
|
|
if (avail < 0)
|
|
|
|
{
|
2012-10-15 06:37:14 +02:00
|
|
|
printf("\n--------- avail was less than 0: %ld\n", (long)avail);
|
|
|
|
printf("snd_pcm_avail(pcm): %ld\n", (long)tmp_pcm_avail);
|
2012-06-25 16:21:32 +02:00
|
|
|
printf("original position: %" PRId64 "\n", orig_position);
|
|
|
|
printf("current position: %" PRId64 "\n", position);
|
|
|
|
printf("original ps.end_position: %" PRId64 "\n", orig_ps_end_position);
|
|
|
|
printf("current ps.end_position: %" PRId64 "\n", ps.end_position);
|
2012-05-15 16:06:39 +02:00
|
|
|
printf("---------\n\n");
|
|
|
|
continue;
|
2012-02-20 19:22:43 +01:00
|
|
|
}
|
2011-06-12 02:45:02 +02:00
|
|
|
buf = new char[avail*framesize];
|
|
|
|
ps.provider->GetAudioWithVolume(buf, position, avail, ps.volume);
|
|
|
|
written = 0;
|
|
|
|
while (written <= 0)
|
|
|
|
{
|
|
|
|
written = snd_pcm_writei(pcm, buf, avail);
|
|
|
|
if (written == -ESTRPIPE || written == -EPIPE)
|
|
|
|
{
|
|
|
|
snd_pcm_recover(pcm, written, 0);
|
|
|
|
}
|
|
|
|
else if (written == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (written < 0)
|
|
|
|
{
|
|
|
|
delete[] buf;
|
|
|
|
snd_pcm_close(pcm);
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "error filling buffer, written=" << written;
|
2011-11-07 06:24:46 +01:00
|
|
|
return (void*)"snd_pcm_writei";
|
2011-06-12 02:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
delete[] buf;
|
|
|
|
position += written;
|
2012-02-20 19:22:37 +01:00
|
|
|
//LOG_D("audio/player/alsa") << "playback loop, filled buffer";
|
2011-06-12 02:45:02 +02:00
|
|
|
|
|
|
|
// Check for end of playback
|
|
|
|
if (position >= ps.end_position)
|
|
|
|
{
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "playback loop, past end, draining";
|
2011-06-12 02:45:02 +02:00
|
|
|
snd_pcm_drain(pcm);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ps.signal_stop = false;
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "out of playback loop";
|
2011-06-12 02:45:02 +02:00
|
|
|
|
|
|
|
switch (snd_pcm_state(pcm))
|
|
|
|
{
|
|
|
|
case SND_PCM_STATE_OPEN:
|
|
|
|
// no clue what could have happened here, but start over
|
|
|
|
ps.signal_start = false;
|
|
|
|
ps.signal_stop = false;
|
|
|
|
goto do_setup;
|
|
|
|
|
|
|
|
case SND_PCM_STATE_SETUP:
|
|
|
|
// we lost the preparedness?
|
|
|
|
snd_pcm_prepare(pcm);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_PCM_STATE_DISCONNECTED:
|
|
|
|
// lost device, close the handle and return error
|
|
|
|
snd_pcm_close(pcm);
|
2011-11-07 06:24:46 +01:00
|
|
|
return (void*)"SND_PCM_STATE_DISCONNECTED";
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
default:
|
|
|
|
// everything else should either be fine or impossible (here)
|
|
|
|
break;
|
|
|
|
}
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
2011-06-12 02:45:02 +02:00
|
|
|
ps.signal_close = false;
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "out of outer loop";
|
2011-06-12 02:45:02 +02:00
|
|
|
|
|
|
|
snd_pcm_close(pcm);
|
2007-04-25 00:29:27 +02:00
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
return 0;
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
|
2012-03-20 00:31:33 +01:00
|
|
|
AlsaPlayer::AlsaPlayer(AudioProvider *provider)
|
|
|
|
: AudioPlayer(provider)
|
2013-10-23 23:43:05 +02:00
|
|
|
, ps(agi::util::make_unique<PlaybackState>())
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2012-03-25 06:05:31 +02:00
|
|
|
ps->provider = provider;
|
2007-04-25 00:29:27 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
ps->device_name = OPT_GET("Player/Audio/ALSA/Device")->GetString();
|
2010-01-15 23:58:51 +01:00
|
|
|
|
2012-03-20 00:31:33 +01:00
|
|
|
if (pthread_create(&thread, 0, &playback_thread, ps.get()) != 0)
|
2012-01-08 02:33:39 +01:00
|
|
|
throw agi::AudioPlayerOpenError("AlsaPlayer: Creating the playback thread failed", 0);
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
|
2012-03-20 00:31:33 +01:00
|
|
|
AlsaPlayer::~AlsaPlayer()
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2011-06-12 02:45:02 +02:00
|
|
|
{
|
2011-11-07 06:24:46 +01:00
|
|
|
PthreadMutexLocker ml(ps->mutex);
|
|
|
|
ps->signal_stop = true;
|
|
|
|
ps->signal_close = true;
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "close stream, stop+close signal";
|
2011-11-07 06:24:46 +01:00
|
|
|
pthread_cond_signal(&ps->cond);
|
2011-06-12 02:45:02 +02:00
|
|
|
}
|
2007-04-25 00:29:27 +02:00
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
pthread_join(thread, 0); // FIXME: check for errors
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
|
|
|
|
void AlsaPlayer::Play(int64_t start, int64_t count)
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2012-03-25 06:05:44 +02:00
|
|
|
PthreadMutexLocker ml(ps->mutex);
|
|
|
|
ps->signal_start = true;
|
|
|
|
ps->signal_stop = true; // make sure to stop any ongoing playback first
|
|
|
|
ps->start_position = start;
|
|
|
|
ps->end_position = start + count;
|
|
|
|
pthread_cond_signal(&ps->cond);
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
|
2012-03-25 06:05:44 +02:00
|
|
|
void AlsaPlayer::Stop()
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2012-03-25 06:05:44 +02:00
|
|
|
PthreadMutexLocker ml(ps->mutex);
|
|
|
|
ps->signal_stop = true;
|
|
|
|
LOG_D("audio/player/alsa") << "stop stream, stop signal";
|
|
|
|
pthread_cond_signal(&ps->cond);
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
bool AlsaPlayer::IsPlaying()
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2012-02-20 19:22:37 +01:00
|
|
|
PthreadMutexLocker ml(ps->mutex);
|
2012-03-20 00:31:33 +01:00
|
|
|
return ps->playing;
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
|
2007-08-31 16:11:35 +02:00
|
|
|
void AlsaPlayer::SetEndPosition(int64_t pos)
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2011-11-07 06:24:46 +01:00
|
|
|
PthreadMutexLocker ml(ps->mutex);
|
|
|
|
ps->end_position = pos;
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
2007-08-31 16:11:35 +02:00
|
|
|
int64_t AlsaPlayer::GetEndPosition()
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2012-02-20 19:22:37 +01:00
|
|
|
PthreadMutexLocker ml(ps->mutex);
|
2011-11-07 06:24:46 +01:00
|
|
|
return ps->end_position;
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
|
2007-08-31 16:11:35 +02:00
|
|
|
int64_t AlsaPlayer::GetCurrentPosition()
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2011-06-12 02:45:02 +02:00
|
|
|
int64_t lastpos;
|
|
|
|
timespec lasttime;
|
|
|
|
int64_t samplerate;
|
|
|
|
|
|
|
|
{
|
2012-02-20 19:22:37 +01:00
|
|
|
PthreadMutexLocker ml(ps->mutex);
|
2011-11-07 06:24:46 +01:00
|
|
|
lastpos = ps->last_position;
|
|
|
|
lasttime = ps->last_position_time;
|
|
|
|
samplerate = ps->provider->GetSampleRate();
|
2011-06-12 02:45:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
timespec now;
|
|
|
|
clock_gettime(CLOCK_REALTIME, &now);
|
|
|
|
|
|
|
|
const double NANO = 1000000000; // nano- is 10^-9
|
|
|
|
|
|
|
|
double now_sec = now.tv_sec + now.tv_nsec/NANO;
|
|
|
|
double last_sec = lasttime.tv_sec + lasttime.tv_nsec/NANO;
|
|
|
|
double diff_sec = now_sec - last_sec;
|
|
|
|
|
|
|
|
int64_t pos = lastpos + (int64_t)(diff_sec * samplerate);
|
|
|
|
//printf("AlsaPlayer: current position = %lld\n", pos);
|
|
|
|
|
|
|
|
return pos;
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
void AlsaPlayer::SetVolume(double vol)
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2011-11-07 06:24:46 +01:00
|
|
|
PthreadMutexLocker ml(ps->mutex);
|
|
|
|
ps->volume = vol;
|
|
|
|
ps->signal_volume = true;
|
|
|
|
pthread_cond_signal(&ps->cond);
|
2011-06-12 02:45:02 +02:00
|
|
|
}
|
2007-04-25 00:29:27 +02:00
|
|
|
|
2008-01-21 21:57:20 +01:00
|
|
|
#endif // WITH_ALSA
|