mirror of
https://github.com/badaix/snapcast.git
synced 2025-06-14 16:51:43 +02:00
removed static variables
This commit is contained in:
parent
5cb962144f
commit
baaf8d980d
2 changed files with 48 additions and 51 deletions
|
@ -20,9 +20,6 @@
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include <SLES/OpenSLES.h>
|
|
||||||
#include <SLES/OpenSLES_Android.h>
|
|
||||||
|
|
||||||
#include "openslPlayer.h"
|
#include "openslPlayer.h"
|
||||||
#include "common/log.h"
|
#include "common/log.h"
|
||||||
#include "common/snapException.h"
|
#include "common/snapException.h"
|
||||||
|
@ -31,34 +28,31 @@ using namespace std;
|
||||||
|
|
||||||
// source: https://github.com/hrydgard/native/blob/master/android/native-audio-so.cpp
|
// source: https://github.com/hrydgard/native/blob/master/android/native-audio-so.cpp
|
||||||
|
|
||||||
// This is kinda ugly, but for simplicity I've left these as globals just like in the sample,
|
|
||||||
// as there's not really any use case for this where we have multiple audio devices yet.
|
|
||||||
|
|
||||||
// engine interfaces
|
|
||||||
static SLObjectItf engineObject;
|
|
||||||
static SLEngineItf engineEngine;
|
|
||||||
static SLObjectItf outputMixObject;
|
|
||||||
|
|
||||||
// buffer queue player interfaces
|
|
||||||
static SLObjectItf bqPlayerObject = NULL;
|
|
||||||
static SLPlayItf bqPlayerPlay;
|
|
||||||
static SLAndroidSimpleBufferQueueItf bqPlayerBufferQueue;
|
|
||||||
static SLMuteSoloItf bqPlayerMuteSolo;
|
|
||||||
static SLVolumeItf bqPlayerVolume;
|
|
||||||
|
|
||||||
// Double buffering.
|
|
||||||
static char *buffer[2];
|
|
||||||
static int curBuffer = 0;
|
|
||||||
static int framesPerBuffer;
|
|
||||||
int sampleRate;
|
|
||||||
|
|
||||||
//static AndroidAudioCallback audioCallback;
|
|
||||||
|
|
||||||
// This callback handler is called every time a buffer finishes playing.
|
// This callback handler is called every time a buffer finishes playing.
|
||||||
// The documentation available is very unclear about how to best manage buffers.
|
// The documentation available is very unclear about how to best manage buffers.
|
||||||
// I've chosen to this approach: Instantly enqueue a buffer that was rendered to the last time,
|
// I've chosen to this approach: Instantly enqueue a buffer that was rendered to the last time,
|
||||||
// and then render the next. Hopefully it's okay to spend time in this callback after having enqueued.
|
// and then render the next. Hopefully it's okay to spend time in this callback after having enqueued.
|
||||||
static void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
|
static void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
|
||||||
|
{
|
||||||
|
OpenslPlayer* player = (OpenslPlayer*)context;
|
||||||
|
player->playerCallback(bq);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
OpenslPlayer::OpenslPlayer(const PcmDevice& pcmDevice, Stream* stream) : Player(pcmDevice, stream), pubStream_(stream), bqPlayerObject(NULL), curBuffer(0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
OpenslPlayer::~OpenslPlayer()
|
||||||
|
{
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void OpenslPlayer::playerCallback(SLAndroidSimpleBufferQueueItf bq)
|
||||||
{
|
{
|
||||||
if (bq != bqPlayerBufferQueue)
|
if (bq != bqPlayerBufferQueue)
|
||||||
{
|
{
|
||||||
|
@ -66,8 +60,6 @@ static void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
OpenslPlayer* player = (OpenslPlayer*)context;
|
|
||||||
|
|
||||||
/* static long lastTick = 0;
|
/* static long lastTick = 0;
|
||||||
long now = chronos::getTickCount();
|
long now = chronos::getTickCount();
|
||||||
int diff = 0;
|
int diff = 0;
|
||||||
|
@ -94,13 +86,13 @@ static void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
|
||||||
// chronos::usec delay((250 - diff) * 1000);
|
// chronos::usec delay((250 - diff) * 1000);
|
||||||
chronos::usec delay(150 * 1000);
|
chronos::usec delay(150 * 1000);
|
||||||
|
|
||||||
if (player->pubStream_->getPlayerChunk(buffer[curBuffer], delay, player->frames_))
|
if (pubStream_->getPlayerChunk(buffer[curBuffer], delay, frames_))
|
||||||
{
|
{
|
||||||
|
|
||||||
SLresult result;
|
SLresult result;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, buffer[curBuffer], player->buff_size);
|
result = (*bq)->Enqueue(bq, buffer[curBuffer], buff_size);
|
||||||
if (result == SL_RESULT_BUFFER_INSUFFICIENT)
|
if (result == SL_RESULT_BUFFER_INSUFFICIENT)
|
||||||
usleep(1000);
|
usleep(1000);
|
||||||
}
|
}
|
||||||
|
@ -110,18 +102,6 @@ static void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
OpenslPlayer::OpenslPlayer(const PcmDevice& pcmDevice, Stream* stream) : Player(pcmDevice, stream), pubStream_(stream)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
OpenslPlayer::~OpenslPlayer()
|
|
||||||
{
|
|
||||||
stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void OpenslPlayer::initOpensl()
|
void OpenslPlayer::initOpensl()
|
||||||
{
|
{
|
||||||
unsigned int rate;
|
unsigned int rate;
|
||||||
|
@ -172,6 +152,8 @@ void OpenslPlayer::initOpensl()
|
||||||
sr = SL_SAMPLINGRATE_48;
|
sr = SL_SAMPLINGRATE_48;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logO << "SamplingRate: " << sr/1000 << "\n";
|
||||||
|
|
||||||
SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
|
SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
|
||||||
SLDataFormat_PCM format_pcm =
|
SLDataFormat_PCM format_pcm =
|
||||||
{
|
{
|
||||||
|
@ -191,9 +173,9 @@ void OpenslPlayer::initOpensl()
|
||||||
SLDataSink audioSnk = {&loc_outmix, NULL};
|
SLDataSink audioSnk = {&loc_outmix, NULL};
|
||||||
|
|
||||||
// create audio player
|
// create audio player
|
||||||
const SLInterfaceID ids[3] = {SL_IID_ANDROIDCONFIGURATION, SL_IID_BUFFERQUEUE, SL_IID_VOLUME};
|
const SLInterfaceID ids[4] = {SL_IID_ANDROIDCONFIGURATION, SL_IID_PLAY, SL_IID_BUFFERQUEUE, SL_IID_VOLUME};
|
||||||
const SLboolean req[3] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
|
const SLboolean req[4] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
|
||||||
result = (*engineEngine)->CreateAudioPlayer(engineEngine, &bqPlayerObject, &audioSrc, &audioSnk, 3, ids, req);
|
result = (*engineEngine)->CreateAudioPlayer(engineEngine, &bqPlayerObject, &audioSrc, &audioSnk, 4, ids, req);
|
||||||
assert(SL_RESULT_SUCCESS == result);
|
assert(SL_RESULT_SUCCESS == result);
|
||||||
|
|
||||||
SLAndroidConfigurationItf playerConfig;
|
SLAndroidConfigurationItf playerConfig;
|
||||||
|
@ -275,13 +257,11 @@ void OpenslPlayer::uninitOpensl()
|
||||||
void OpenslPlayer::start()
|
void OpenslPlayer::start()
|
||||||
{
|
{
|
||||||
initOpensl();
|
initOpensl();
|
||||||
Player::start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void OpenslPlayer::stop()
|
void OpenslPlayer::stop()
|
||||||
{
|
{
|
||||||
Player::stop();
|
|
||||||
uninitOpensl();
|
uninitOpensl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -290,10 +270,5 @@ void OpenslPlayer::stop()
|
||||||
|
|
||||||
void OpenslPlayer::worker()
|
void OpenslPlayer::worker()
|
||||||
{
|
{
|
||||||
while (active_)
|
|
||||||
{
|
|
||||||
usleep(100*1000);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,9 @@
|
||||||
#ifndef OPEN_SL_PLAYER_H
|
#ifndef OPEN_SL_PLAYER_H
|
||||||
#define OPEN_SL_PLAYER_H
|
#define OPEN_SL_PLAYER_H
|
||||||
|
|
||||||
|
#include <SLES/OpenSLES.h>
|
||||||
|
#include <SLES/OpenSLES_Android.h>
|
||||||
|
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
|
|
||||||
typedef int (*AndroidAudioCallback)(short *buffer, int num_samples);
|
typedef int (*AndroidAudioCallback)(short *buffer, int num_samples);
|
||||||
|
@ -36,6 +39,8 @@ public:
|
||||||
virtual void start();
|
virtual void start();
|
||||||
virtual void stop();
|
virtual void stop();
|
||||||
|
|
||||||
|
void playerCallback(SLAndroidSimpleBufferQueueItf bq);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
size_t frames_;
|
size_t frames_;
|
||||||
size_t buff_size;
|
size_t buff_size;
|
||||||
|
@ -47,6 +52,23 @@ protected:
|
||||||
|
|
||||||
virtual void worker();
|
virtual void worker();
|
||||||
|
|
||||||
|
// engine interfaces
|
||||||
|
SLObjectItf engineObject;
|
||||||
|
SLEngineItf engineEngine;
|
||||||
|
SLObjectItf outputMixObject;
|
||||||
|
|
||||||
|
// buffer queue player interfaces
|
||||||
|
SLObjectItf bqPlayerObject;// = NULL;
|
||||||
|
SLPlayItf bqPlayerPlay;
|
||||||
|
SLAndroidSimpleBufferQueueItf bqPlayerBufferQueue;
|
||||||
|
SLMuteSoloItf bqPlayerMuteSolo;
|
||||||
|
SLVolumeItf bqPlayerVolume;
|
||||||
|
|
||||||
|
// Double buffering.
|
||||||
|
char *buffer[2];
|
||||||
|
int framesPerBuffer;
|
||||||
|
int sampleRate;
|
||||||
|
int curBuffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue