moved alsaPlayer into player subdir

This commit is contained in:
badaix 2015-11-10 20:08:59 +01:00
parent 2036477446
commit 734adb8852
9 changed files with 162 additions and 67 deletions

View file

@ -7,7 +7,7 @@ CFLAGS = -std=c++0x -Wall -Wno-unused-function -O3 -pthread -DVERSION=\"$(VERSI
#CFLAGS = -std=c++0x -Wall -Wno-unused-function -O3 -pthread -DVERSION=\"$(VERSION)\" -I.. -static-libgcc -static-libstdc++
LDFLAGS = -lrt -lboost_system -lboost_program_options -lasound -logg -lvorbis -lvorbisenc -lFLAC -lavahi-client -lavahi-common
OBJ = snapClient.o stream.o alsaPlayer.o clientConnection.o timeProvider.o decoder/oggDecoder.o decoder/pcmDecoder.o decoder/flacDecoder.o controller.o browseAvahi.o ../message/pcmChunk.o ../common/log.o ../message/sampleFormat.o
OBJ = snapClient.o stream.o clientConnection.o timeProvider.o player/player.o player/alsaPlayer.o decoder/oggDecoder.o decoder/pcmDecoder.o decoder/flacDecoder.o controller.o browseAvahi.o ../message/pcmChunk.o ../common/log.o ../message/sampleFormat.o
BIN = snapclient
all: $(TARGET)

View file

@ -178,7 +178,7 @@ void Controller::worker()
stream_ = new Stream(*sampleFormat_);
stream_->setBufferLen(serverSettings->bufferMs - latency_);
player_.reset(new Player(pcmDevice_, stream_));
player_.reset(new AlsaPlayer(pcmDevice_, stream_));
player_->setVolume(serverSettings->volume / 100.);
player_->start();

View file

@ -23,10 +23,10 @@
#include <atomic>
#include "decoder/decoder.h"
#include "message/message.h"
#include "player/pcmDevice.h"
#include "player/alsaPlayer.h"
#include "clientConnection.h"
#include "stream.h"
#include "pcmDevice.h"
#include "alsaPlayer.h"
/// Forwards PCM data to the audio player

View file

@ -19,27 +19,18 @@
#include "alsaPlayer.h"
#include "common/log.h"
#include "common/snapException.h"
#include <alsa/asoundlib.h>
#include <iostream>
//#define BUFFER_TIME 120000
#define PERIOD_TIME 30000
using namespace std;
Player::Player(const PcmDevice& pcmDevice, Stream* stream) :
handle_(NULL),
buff_(NULL),
active_(false),
stream_(stream),
pcmDevice_(pcmDevice),
volume_(1.0),
muted_(false)
AlsaPlayer::AlsaPlayer(const PcmDevice& pcmDevice, Stream* stream) : Player(pcmDevice, stream), handle_(NULL), buff_(NULL)
{
}
void Player::initAlsa()
void AlsaPlayer::initAlsa()
{
unsigned int tmp, rate;
int pcm, channels;
@ -126,7 +117,7 @@ void Player::initAlsa()
}
void Player::uninitAlsa()
void AlsaPlayer::uninitAlsa()
{
if (handle_ != NULL)
{
@ -143,32 +134,27 @@ void Player::uninitAlsa()
}
void Player::start()
void AlsaPlayer::start()
{
initAlsa();
active_ = true;
playerThread_ = thread(&Player::worker, this);
Player::start();
}
Player::~Player()
AlsaPlayer::~AlsaPlayer()
{
stop();
}
void Player::stop()
void AlsaPlayer::stop()
{
if (active_)
{
active_ = false;
playerThread_.join();
}
Player::stop();
uninitAlsa();
}
void Player::worker()
void AlsaPlayer::worker()
{
snd_pcm_sframes_t pcm;
snd_pcm_sframes_t framesDelay;
@ -229,21 +215,7 @@ void Player::worker()
void Player::setVolume(double volume)
{
volume_ = volume;
}
void Player::setMute(bool mute)
{
muted_ = mute;
}
vector<PcmDevice> Player::pcm_list(void)
vector<PcmDevice> AlsaPlayer::pcm_list(void)
{
void **hints, **n;
char *name, *descr, *io;

View file

@ -16,41 +16,36 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#ifndef PLAYER_H
#define PLAYER_H
#ifndef ALSA_PLAYER_H
#define ALSA_PLAYER_H
#include <string>
#include <thread>
#include <atomic>
#include <vector>
#include "player.h"
#include <alsa/asoundlib.h>
#include "stream.h"
#include "pcmDevice.h"
/// Audio Player
/**
* Audio player implementation using Alsa
*/
class Player
class AlsaPlayer : public Player
{
public:
Player(const PcmDevice& pcmDevice, Stream* stream);
virtual ~Player();
AlsaPlayer(const PcmDevice& pcmDevice, Stream* stream);
virtual ~AlsaPlayer();
/// Set audio volume in range [0..1]
void setVolume(double volume);
void setMute(bool mute);
void start();
void stop();
virtual void start();
virtual void stop();
/// List the system's audio output devices
static std::vector<PcmDevice> pcm_list(void);
protected:
virtual void worker();
private:
void initAlsa();
void uninitAlsa();
void worker();
template <typename T>
void adjustVolume(char *buffer, size_t count, double volume)
@ -64,12 +59,6 @@ private:
snd_pcm_uframes_t frames_;
char *buff_;
std::atomic<bool> active_;
Stream* stream_;
std::thread playerThread_;
PcmDevice pcmDevice_;
double volume_;
bool muted_;
};

74
client/player/player.cpp Normal file
View file

@ -0,0 +1,74 @@
/***
This file is part of snapcast
Copyright (C) 2015 Johannes Pohl
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 3 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, see <http://www.gnu.org/licenses/>.
***/
#include <iostream>
#include "player.h"
using namespace std;
Player::Player(const PcmDevice& pcmDevice, Stream* stream) :
active_(false),
stream_(stream),
pcmDevice_(pcmDevice),
volume_(1.0),
muted_(false)
{
}
void Player::start()
{
active_ = true;
playerThread_ = thread(&Player::worker, this);
}
Player::~Player()
{
stop();
}
void Player::stop()
{
if (active_)
{
active_ = false;
playerThread_.join();
}
}
void Player::setVolume(double volume)
{
volume_ = volume;
}
void Player::setMute(bool mute)
{
muted_ = mute;
}

60
client/player/player.h Normal file
View file

@ -0,0 +1,60 @@
/***
This file is part of snapcast
Copyright (C) 2015 Johannes Pohl
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 3 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, see <http://www.gnu.org/licenses/>.
***/
#ifndef PLAYER_H
#define PLAYER_H
#include <string>
#include <thread>
#include <atomic>
#include <vector>
#include <alsa/asoundlib.h>
#include "../stream.h"
#include "pcmDevice.h"
/// Audio Player
/**
* Abstract audio player implementation
*/
class Player
{
public:
Player(const PcmDevice& pcmDevice, Stream* stream);
virtual ~Player();
/// Set audio volume in range [0..1]
virtual void setVolume(double volume);
virtual void setMute(bool mute);
virtual void start();
virtual void stop();
protected:
virtual void worker() = 0;
std::atomic<bool> active_;
Stream* stream_;
std::thread playerThread_;
PcmDevice pcmDevice_;
double volume_;
bool muted_;
};
#endif

View file

@ -25,7 +25,7 @@
#include "common/log.h"
#include "common/signalHandler.h"
#include "controller.h"
#include "alsaPlayer.h"
#include "player/alsaPlayer.h"
#include "browseAvahi.h"
@ -36,7 +36,7 @@ bool g_terminated = false;
PcmDevice getPcmDevice(const std::string& soundcard)
{
vector<PcmDevice> pcmDevices = Player::pcm_list();
vector<PcmDevice> pcmDevices = AlsaPlayer::pcm_list();
int soundcardIdx = -1;
try
@ -108,7 +108,7 @@ int main (int argc, char *argv[])
if (listPcmDevices)
{
vector<PcmDevice> pcmDevices = Player::pcm_list();
vector<PcmDevice> pcmDevices = AlsaPlayer::pcm_list();
for (auto dev: pcmDevices)
{
cout << dev.idx << ": " << dev.name << "\n"