summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuo Jinghua <sunmoon1997@gmail.com>2012-12-21 15:11:51 +0800
committerLuo Jinghua <sunmoon1997@gmail.com>2012-12-21 15:11:51 +0800
commitde7d44338dcbcb285dbd2b92c25dcdadcf6ad572 (patch)
tree95808a53a4ec095fe3dcab375c6d0976f9170153
parent1626408838eb480cbb53c8789711d7cdc56b495c (diff)
Convert the zte audio device to a threaded mixer device
-rw-r--r--src/device_zte.cpp45
-rw-r--r--src/device_zte.h8
2 files changed, 29 insertions, 24 deletions
diff --git a/src/device_zte.cpp b/src/device_zte.cpp
index 51bee75..d42808f 100644
--- a/src/device_zte.cpp
+++ b/src/device_zte.cpp
@@ -7,6 +7,7 @@
#include "MusicPlay.h"
#include "device_zte.h"
+#include "timer.h"
#include "debug.h"
@@ -35,39 +36,43 @@ namespace audiere {
}
ZteAudioDevice::ZteAudioDevice(int rate,
- int buffer_size)
- : MixerDevice(rate), m_rate(rate)
+ int buffer_size)
+ : ThreadedMixerDevice(rate, buffer_size, 8), m_rate(rate)
{
- m_buffer_size = buffer_size;
- m_buffer = new char [buffer_size];
+ m_initial_ts = GetNow();
+ m_total = 0;
}
ZteAudioDevice::~ZteAudioDevice() {
ADR_GUARD("ZteAudioDevice::~ZteAudioDevice");
+ shutdown();
Music_device_close(0);
- delete [] m_buffer;
}
void ADR_CALL
- ZteAudioDevice::update() {
- int sample_len;
- size_t sample_left;
- char* sample_buf;
-
- sample_buf = m_buffer;
- sample_len = m_buffer_size / 4;
-
- sample_left = read(sample_len, sample_buf);
- while (sample_left > 0) {
- if (Music_device_write(sample_buf, sample_left * 4) < 0) {
- AI_Sleep(2);
- } else {
- sample_left = 0;
- }
+ ZteAudioDevice::outputBuffer(const char* buf, int buf_len) {
+ const u64 total_samples = m_total / 4;
+ const u64 total_time = total_samples * 1000000 / m_rate;
+ const u64 threshold = u64(1000000) * 1024 * 2 / m_rate;
+ u64 ts = GetNow();
+
+ if (m_total == 0)
+ m_initial_ts = ts;
+
+ while (m_initial_ts + total_time > ts + threshold) {
+ u64 to_sleep = (m_initial_ts + total_time - ts - threshold) / 2000;
+ if (to_sleep < 2)
+ break;
+
+ AI_Sleep(to_sleep);
+ ts = GetNow();
}
+
+ Music_device_write((void*)buf, buf_len);
+ m_total += buf_len;
}
diff --git a/src/device_zte.h b/src/device_zte.h
index dfbc849..cf98cb2 100644
--- a/src/device_zte.h
+++ b/src/device_zte.h
@@ -8,7 +8,7 @@
namespace audiere {
- class ZteAudioDevice : public MixerDevice {
+ class ZteAudioDevice : public ThreadedMixerDevice {
public:
static ZteAudioDevice* create(const ParameterList& parameters);
@@ -18,13 +18,13 @@ namespace audiere {
~ZteAudioDevice();
public:
- void ADR_CALL update();
+ virtual void outputBuffer(const char* buf, int buf_len);
const char* ADR_CALL getName();
private:
- int m_buffer_size;
- char* m_buffer;
int m_rate;
+ u64 m_initial_ts;
+ u64 m_total;
};
}