driver: Fold eos() into the driver

With this patch I no longer need a Driver :: init() function to handle
picking the next track.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2014-12-20 11:04:14 -05:00
parent 882da67480
commit a4ad0aa79b
4 changed files with 19 additions and 48 deletions

View File

@ -19,6 +19,9 @@ static File f_cur_track("cur_track", 0);
static Driver *cur_driver = NULL; static Driver *cur_driver = NULL;
static void _load_track(Track *, bool);
static bool continue_playback();
Driver :: Driver() Driver :: Driver()
{ {
@ -30,6 +33,16 @@ Driver :: ~Driver()
cur_driver = NULL; cur_driver = NULL;
} }
void Driver :: eos()
{
if (cur_track) {
cur_track->played();
library :: get_queue()->updated(cur_track);
}
_load_track(deck :: next(), continue_playback());
}
static void save_state() static void save_state()
{ {
@ -74,21 +87,10 @@ static bool continue_playback()
return ret; return ret;
} }
static void on_eos()
{
if (cur_track) {
cur_track->played();
library :: get_queue()->updated(cur_track);
}
_load_track(deck :: next(), continue_playback());
}
void audio :: init() void audio :: init()
{ {
unsigned int id; unsigned int id;
cur_driver->init(on_eos);
if (f_cur_track.exists()) { if (f_cur_track.exists()) {
f_cur_track.open(OPEN_READ); f_cur_track.open(OPEN_READ);
f_cur_track >> id; f_cur_track >> id;

View File

@ -35,12 +35,6 @@ private:
std::string cur_file; std::string cur_file;
public: public:
/**
* Called to initialize the GStreamer audio driver.
* @param eos_cb End-of-stream callback function.
*/
void init(void (*)());
/** /**
* Load a track into the gstreamer pipeline. * Load a track into the gstreamer pipeline.
* @param filepath The file to be loaded. * @param filepath The file to be loaded.
@ -113,11 +107,6 @@ static void parse_gst_error(GstMessage *error, const std::string filepath)
g_free(debug); g_free(debug);
} }
void GSTDriver :: init(void (*eos_cb)())
{
on_eos = eos_cb;
}
void GSTDriver :: load(const std::string &filepath) void GSTDriver :: load(const std::string &filepath)
{ {
gchar *uri; gchar *uri;
@ -176,7 +165,7 @@ void GSTDriver :: on_message(GstMessage *message)
audio :: next(); audio :: next();
break; break;
case GST_MESSAGE_EOS: case GST_MESSAGE_EOS:
on_eos(); gst_driver->eos();
break; break;
default: default:
break; break;

View File

@ -17,20 +17,12 @@ static const unsigned long O_SECOND = 1000000000;
*/ */
class Driver { class Driver {
protected: protected:
void (*on_eos) (); /**< End-of-stream callback function. */
void (*on_error) (); /**< Error handling callback function. */ void (*on_error) (); /**< Error handling callback function. */
public: public:
Driver(); /**< Default Driver constructor. */ Driver(); /**< Default Driver constructor. */
virtual ~Driver(); /**< Driver destructor. */ virtual ~Driver(); /**< Driver destructor. */
/**
* Initialize an audio driver.
*
* @param eos_cb End-of-stream callback function.
*/
virtual void init(void (*eos_cb)()) = 0;
/** /**
* Loads an audio file for playback. * Loads an audio file for playback.
@ -81,6 +73,11 @@ public:
* @return The duration of the current track, in nanoseconds. * @return The duration of the current track, in nanoseconds.
*/ */
virtual long duration() = 0; virtual long duration() = 0;
/**
* Called to handle reaching the end-of-stream.
*/
void eos();
}; };
#endif /* OCARINA_CORE_DRIVER_H */ #endif /* OCARINA_CORE_DRIVER_H */

View File

@ -8,7 +8,6 @@
#include <tests/test.h> #include <tests/test.h>
Track *TRACK_NULL = NULL; Track *TRACK_NULL = NULL;
static unsigned int eos_count = 0;
class TestDriver : public Driver class TestDriver : public Driver
@ -22,11 +21,6 @@ public:
TestDriver() : playing(false), cur_pos(0), cur_duration(0) {} TestDriver() : playing(false), cur_pos(0), cur_duration(0) {}
~TestDriver() {}; ~TestDriver() {};
void init(void (*eos_cb)())
{
on_eos = eos_cb;
}
void load(const std::string &file) void load(const std::string &file)
{ {
cur_file = file; cur_file = file;
@ -51,14 +45,8 @@ public:
void seek_to(long offset) { cur_pos = offset; } void seek_to(long offset) { cur_pos = offset; }
long position() { return cur_pos; } long position() { return cur_pos; }
long duration() { return cur_duration; } long duration() { return cur_duration; }
void eos() { on_eos(); }
}; };
void on_eos()
{
eos_count++;
}
void test_driver() void test_driver()
{ {
@ -70,8 +58,6 @@ void test_driver()
test_not_equal(audio :: get_driver(), (Driver *)NULL); test_not_equal(audio :: get_driver(), (Driver *)NULL);
test_equal((Driver *)&driver, audio :: get_driver()); test_equal((Driver *)&driver, audio :: get_driver());
driver.init(on_eos);
driver.load(file); driver.load(file);
test_equal(driver.cur_file, file); test_equal(driver.cur_file, file);
@ -90,9 +76,6 @@ void test_driver()
driver.cur_duration = 424242; driver.cur_duration = 424242;
test_equal(driver.duration(), (long)424242); test_equal(driver.duration(), (long)424242);
driver.eos();
test_equal(eos_count, (unsigned)1);
driver.play(); driver.play();
driver.seek_to(4242); driver.seek_to(4242);
driver.load(file); driver.load(file);