[DEV] correct android multithreading

This commit is contained in:
Edouard DUPIN 2015-06-26 16:32:58 +02:00
parent 70d95816ee
commit 404fc89275
3 changed files with 50 additions and 21 deletions

View File

@ -15,21 +15,33 @@
#include <audio/ess/decOgg.h> #include <audio/ess/decOgg.h>
#include <unistd.h> #include <unistd.h>
static void threadCallback2(void* _userData) { #if defined(__TARGET_OS__Android)
static void* threadCallback2(void* _userData) {
#else
static void threadCallback2(void* _userData) {
#endif
etk::thread::setName("ewolSA decoder"); etk::thread::setName("ewolSA decoder");
audio::ess::LoadedFile* decodeFile = static_cast<audio::ess::LoadedFile*>(_userData); audio::ess::LoadedFile* decodeFile = static_cast<audio::ess::LoadedFile*>(_userData);
if (decodeFile != nullptr) { if (decodeFile != nullptr) {
decodeFile->decode(); decodeFile->decode();
} }
#if defined(__TARGET_OS__Android)
return nullptr;
#endif
} }
void audio::ess::LoadedFile::decode() { void audio::ess::LoadedFile::decode() {
EWOLSA_INFO("Start decode OGG : " << m_file);
m_data = audio::ess::ogg::loadAudioFile(m_file, m_nbChanRequested); m_data = audio::ess::ogg::loadAudioFile(m_file, m_nbChanRequested);
m_nbSamples = m_data.size(); m_nbSamples = m_data.size();
EWOLSA_INFO("End decode OGG : " << m_file << " size=" << m_nbSamples);
} }
audio::ess::LoadedFile::LoadedFile(const std::string& _fileName, int8_t _nbChanRequested) : audio::ess::LoadedFile::LoadedFile(const std::string& _fileName, int8_t _nbChanRequested) :
m_thread(nullptr), #if !defined(__TARGET_OS__Android)
m_thread(nullptr),
#endif
m_file(_fileName), m_file(_fileName),
m_nbSamples(0), m_nbSamples(0),
m_nbChanRequested(_nbChanRequested), m_nbChanRequested(_nbChanRequested),
@ -40,15 +52,21 @@ audio::ess::LoadedFile::LoadedFile(const std::string& _fileName, int8_t _nbChanR
m_data = audio::ess::wav::loadAudioFile(m_file, m_nbChanRequested); m_data = audio::ess::wav::loadAudioFile(m_file, m_nbChanRequested);
m_nbSamples = m_data.size(); m_nbSamples = m_data.size();
} else if (etk::end_with(tmpName, ".ogg") == true) { } else if (etk::end_with(tmpName, ".ogg") == true) {
/* #if 1
EWOLSA_DEBUG("create thread"); EWOLSA_DEBUG("create thread");
m_thread = new std::thread(&threadCallback2, this); #if defined(__TARGET_OS__Android)
EWOLSA_DEBUG("done 1"); pthread_create(&m_thread, nullptr, &threadCallback2, this);
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); #else
EWOLSA_DEBUG("done 2"); m_thread = new std::thread(&threadCallback2, this);
*/ #endif
// TODO: Set this back to have music decode(); EWOLSA_DEBUG("done 1");
// TODO: This is removed to test faster ==> pb on android to create thread ... std::this_thread::sleep_for(std::chrono::milliseconds(100));
EWOLSA_DEBUG("done 2");
#else
EWOLSA_DEBUG("done 1");
decode();
EWOLSA_DEBUG("done 2");
#endif
} else { } else {
EWOLSA_ERROR("Extention not managed '" << m_file << "' Sopported extention : .wav / .ogg"); EWOLSA_ERROR("Extention not managed '" << m_file << "' Sopported extention : .wav / .ogg");
return; return;
@ -63,9 +81,13 @@ audio::ess::LoadedFile::LoadedFile(const std::string& _fileName, int8_t _nbChanR
audio::ess::LoadedFile::~LoadedFile() { audio::ess::LoadedFile::~LoadedFile() {
if (m_thread != nullptr) { #if defined(__TARGET_OS__Android)
delete m_thread; // TODO : ...
} #else
if (m_thread != nullptr) {
delete m_thread;
}
#endif
} }

View File

@ -11,14 +11,21 @@
#define __EWOLSA_LOADED_FILE_H__ #define __EWOLSA_LOADED_FILE_H__
#include <etk/types.h> #include <etk/types.h>
#include <thread> #if defined(__TARGET_OS__Android)
#include <pthread.h> #include <pthread.h>
#else
#include <thread>
#endif
namespace audio { namespace audio {
namespace ess { namespace ess {
class LoadedFile { class LoadedFile {
private: private:
std::thread* m_thread; #if defined(__TARGET_OS__Android)
pthread_t m_thread;
#else
std::thread* m_thread;
#endif
public: public:
LoadedFile(const std::string& _fileName, int8_t _nbChanRequested=1); LoadedFile(const std::string& _fileName, int8_t _nbChanRequested=1);
~LoadedFile(); ~LoadedFile();

View File

@ -62,20 +62,20 @@ std::vector<int16_t> audio::ess::ogg::loadAudioFile(const std::string& _filename
// Start loading the XML : // Start loading the XML :
//EWOLSA_DEBUG("open file (OGG) \"" << fileAccess << "\""); //EWOLSA_DEBUG("open file (OGG) \"" << fileAccess << "\"");
if (false == fileAccess->exist()) { if (false == fileAccess->exist()) {
//EWOLSA_ERROR("File Does not exist : \"" << fileAccess << "\""); EWOLSA_ERROR("File Does not exist : \"" << *fileAccess << "\"");
return std::vector<int16_t>(); return std::vector<int16_t>();
} }
int32_t fileSize = fileAccess->fileSize(); int32_t fileSize = fileAccess->fileSize();
if (0 == fileSize) { if (0 == fileSize) {
//EWOLSA_ERROR("This file is empty : \"" << fileAccess << "\""); EWOLSA_ERROR("This file is empty : \"" << *fileAccess << "\"");
return std::vector<int16_t>(); return std::vector<int16_t>();
} }
if (false == fileAccess->fileOpenRead()) { if (false == fileAccess->fileOpenRead()) {
//EWOLSA_ERROR("Can not open the file : \"" << fileAccess << "\""); EWOLSA_ERROR("Can not open the file : \"" << *fileAccess << "\"");
return std::vector<int16_t>(); return std::vector<int16_t>();
} }
if (ov_open_callbacks(&(*fileAccess), &vf, nullptr, 0, tmpCallback) < 0) { if (ov_open_callbacks(&(*fileAccess), &vf, nullptr, 0, tmpCallback) < 0) {
//EWOLSA_ERROR("Input does not appear to be an Ogg bitstream."); EWOLSA_ERROR("Input does not appear to be an Ogg bitstream.");
return std::vector<int16_t>(); return std::vector<int16_t>();
} }
vorbis_info *vi=ov_info(&vf,-1); vorbis_info *vi=ov_info(&vf,-1);