player support for different sample formats

This commit is contained in:
badaix 2016-04-30 10:17:10 +02:00
parent e1a47b3b98
commit 474f1dd86f
3 changed files with 58 additions and 19 deletions

View file

@ -19,6 +19,7 @@
#include "alsaPlayer.h" #include "alsaPlayer.h"
#include "common/log.h" #include "common/log.h"
#include "common/snapException.h" #include "common/snapException.h"
#include "common/strCompat.h"
//#define BUFFER_TIME 120000 //#define BUFFER_TIME 120000
#define PERIOD_TIME 30000 #define PERIOD_TIME 30000
@ -60,7 +61,19 @@ void AlsaPlayer::initAlsa()
if ((pcm = snd_pcm_hw_params_set_access(handle_, params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) if ((pcm = snd_pcm_hw_params_set_access(handle_, params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
throw SnapException("Can't set interleaved mode: " + string(snd_strerror(pcm))); throw SnapException("Can't set interleaved mode: " + string(snd_strerror(pcm)));
if ((pcm = snd_pcm_hw_params_set_format(handle_, params, SND_PCM_FORMAT_S16_LE)) < 0) snd_pcm_format_t snd_pcm_format;
if (format.bits == 8)
snd_pcm_format = SND_PCM_FORMAT_S8;
else if (format.bits == 16)
snd_pcm_format = SND_PCM_FORMAT_S16_LE;
else if ((format.bits == 24) && (format.sampleSize == 4))
snd_pcm_format = SND_PCM_FORMAT_S24_LE;
else if (format.bits == 32)
snd_pcm_format = SND_PCM_FORMAT_S32_LE;
else
throw SnapException("Unsupported sample format: " + cpt::to_string(format.bits));
if ((pcm = snd_pcm_hw_params_set_format(handle_, params, snd_pcm_format)) < 0)
throw SnapException("Can't set format: " + string(snd_strerror(pcm))); throw SnapException("Can't set format: " + string(snd_strerror(pcm)));
if ((pcm = snd_pcm_hw_params_set_channels(handle_, params, channels)) < 0) if ((pcm = snd_pcm_hw_params_set_channels(handle_, params, channels)) < 0)
@ -100,7 +113,7 @@ void AlsaPlayer::initAlsa()
snd_pcm_hw_params_get_period_size(params, &frames_, 0); snd_pcm_hw_params_get_period_size(params, &frames_, 0);
logO << "frames: " << frames_ << "\n"; logO << "frames: " << frames_ << "\n";
buff_size = frames_ * channels * 2 /* 2 -> sample size */; buff_size = frames_ * format.frameSize; //channels * 2 /* 2 -> sample size */;
buff_ = (char *) malloc(buff_size); buff_ = (char *) malloc(buff_size);
snd_pcm_hw_params_get_period_time(params, &tmp, NULL); snd_pcm_hw_params_get_period_time(params, &tmp, NULL);

View file

@ -23,6 +23,7 @@
#include "openslPlayer.h" #include "openslPlayer.h"
#include "common/log.h" #include "common/log.h"
#include "common/snapException.h" #include "common/snapException.h"
#include "common/strCompat.h"
using namespace std; using namespace std;
@ -209,57 +210,82 @@ void OpenslPlayer::initOpensl()
result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE); result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);
throwUnsuccess("OutputMixObject::Realize", result); throwUnsuccess("OutputMixObject::Realize", result);
SLuint32 sr = SL_SAMPLINGRATE_44_1; SLuint32 samplesPerSec = SL_SAMPLINGRATE_48;
switch(format.rate) switch(format.rate)
{ {
case 8000: case 8000:
sr = SL_SAMPLINGRATE_8; samplesPerSec = SL_SAMPLINGRATE_8;
break; break;
case 11025: case 11025:
sr = SL_SAMPLINGRATE_11_025; samplesPerSec = SL_SAMPLINGRATE_11_025;
break; break;
case 16000: case 16000:
sr = SL_SAMPLINGRATE_16; samplesPerSec = SL_SAMPLINGRATE_16;
break; break;
case 22050: case 22050:
sr = SL_SAMPLINGRATE_22_05; samplesPerSec = SL_SAMPLINGRATE_22_05;
break; break;
case 24000: case 24000:
sr = SL_SAMPLINGRATE_24; samplesPerSec = SL_SAMPLINGRATE_24;
break; break;
case 32000: case 32000:
sr = SL_SAMPLINGRATE_32; samplesPerSec = SL_SAMPLINGRATE_32;
break; break;
case 44100: case 44100:
sr = SL_SAMPLINGRATE_44_1; samplesPerSec = SL_SAMPLINGRATE_44_1;
break; break;
case 48000: case 48000:
sr = SL_SAMPLINGRATE_48; samplesPerSec = SL_SAMPLINGRATE_48;
break; break;
case 64000: case 64000:
sr = SL_SAMPLINGRATE_64; samplesPerSec = SL_SAMPLINGRATE_64;
break; break;
case 88200: case 88200:
sr = SL_SAMPLINGRATE_88_2; samplesPerSec = SL_SAMPLINGRATE_88_2;
break; break;
case 96000: case 96000:
sr = SL_SAMPLINGRATE_96; samplesPerSec = SL_SAMPLINGRATE_96;
break; break;
case 192000: case 192000:
sr = SL_SAMPLINGRATE_192; samplesPerSec = SL_SAMPLINGRATE_192;
break; break;
default: default:
throw SnapException("Sample rate not supported"); throw SnapException("Sample rate not supported");
} }
SLuint32 bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
SLuint32 containerSize = SL_PCMSAMPLEFORMAT_FIXED_16;
switch(format.bits)
{
case 8:
bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_8;
containerSize = SL_PCMSAMPLEFORMAT_FIXED_8;
break;
case 16:
bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
containerSize = SL_PCMSAMPLEFORMAT_FIXED_16;
break;
case 24:
bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_24;
containerSize = SL_PCMSAMPLEFORMAT_FIXED_32;
break;
case 32:
bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_32;
containerSize = SL_PCMSAMPLEFORMAT_FIXED_32;
break;
default:
throw SnapException("Unsupported sample format: " + cpt::to_string(format.bits));
}
SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2}; SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
SLDataFormat_PCM format_pcm = SLDataFormat_PCM format_pcm =
{ {
SL_DATAFORMAT_PCM, SL_DATAFORMAT_PCM,
format.channels, format.channels,
sr, samplesPerSec,
SL_PCMSAMPLEFORMAT_FIXED_16, bitsPerSample,
SL_PCMSAMPLEFORMAT_FIXED_16, containerSize,
SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT, SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT,
SL_BYTEORDER_LITTLEENDIAN SL_BYTEORDER_LITTLEENDIAN
}; };

View file

@ -76,7 +76,7 @@ void Stream::addChunk(msg::PcmChunk* chunk)
chunks_.push(shared_ptr<msg::PcmChunk>(chunk)); chunks_.push(shared_ptr<msg::PcmChunk>(chunk));
std::unique_lock<std::mutex> lck(cvMutex_); std::unique_lock<std::mutex> lck(cvMutex_);
cv_.notify_one(); cv_.notify_one();
// logD << "new chunk: " << chunk_->getDuration() << ", Chunks: " << chunks_.size() << "\n"; // logD << "new chunk: " << chunk->duration<cs::msec>().count() << ", Chunks: " << chunks_.size() << "\n";
} }