diff options
author | Michał Cichoń <michcic@gmail.com> | 2015-08-27 19:49:52 +0200 |
---|---|---|
committer | Michał Cichoń <michcic@gmail.com> | 2015-08-27 19:49:52 +0200 |
commit | d25764daa0c4c1e4a5887ca1599d4975a7a799c6 (patch) | |
tree | acf64d3ad2343bf19aaa2a7d843cec1c232a0f84 | |
parent | aa5b13651682f6dc0513d424e42dfcaa00e01ac1 (diff) | |
download | pianobar-windows-d25764daa0c4c1e4a5887ca1599d4975a7a799c6.tar.gz pianobar-windows-d25764daa0c4c1e4a5887ca1599d4975a7a799c6.tar.bz2 pianobar-windows-d25764daa0c4c1e4a5887ca1599d4975a7a799c6.zip |
player2: Fix IsFinished state detection and do cleanup when shuting down.
-rw-r--r-- | src/player2.c | 53 | ||||
-rw-r--r-- | src/player2.h | 4 |
2 files changed, 51 insertions, 6 deletions
diff --git a/src/player2.c b/src/player2.c index 906fa39..01fe33c 100644 --- a/src/player2.c +++ b/src/player2.c @@ -1,6 +1,6 @@ /* -Copyright (c) 2008-2014 - Lars-Dominik Braun <lars@6xq.net> +Copyright (c) 2015 + Micha³ Cichoñ <thedmd@interia.pl> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -23,6 +23,8 @@ THE SOFTWARE. /* receive/play audio stream */ +/* based on DShow example player */ + #include "config.h" #include "player2.h" #define COBJMACROS @@ -35,6 +37,12 @@ THE SOFTWARE. enum { NO_GRAPH, RUNNING, PAUSED, STOPPED }; +static struct _player_static_t { + bool done; + bool initialized; + bool hasCOM; +} BarPlayerGlobal = { 0 }; + struct _player_t { int state; IGraphBuilder* graph; @@ -46,6 +54,40 @@ struct _player_t { float gain; // dB }; +static bool BarPlayer2StaticInit(); +static void BarPlayer2StaticTerm(void); + +static bool BarPlayer2StaticInit () { + if (BarPlayerGlobal.done) + return BarPlayerGlobal.initialized; + + BarPlayerGlobal.done = true; + + atexit(BarPlayer2StaticTerm); + + if (FAILED(CoInitializeEx(NULL, COINIT_MULTITHREADED))) + return false; + + BarPlayerGlobal.hasCOM = true; + + BarPlayerGlobal.initialized = true; + + return true; +} + +static void BarPlayer2StaticTerm(void) { + if (!BarPlayerGlobal.done) + return; + + if (BarPlayerGlobal.hasCOM) { + CoUninitialize(); + BarPlayerGlobal.hasCOM = false; + } + + BarPlayerGlobal.initialized = false; + BarPlayerGlobal.done = false; +} + static void BarPlayer2ApplyVolume(player2_t player) { long v = (long)((player->volume + player->gain) * 100.0f); @@ -196,7 +238,7 @@ done: bool BarPlayer2Init(player2_t* player) { - if (FAILED(CoInitializeEx(NULL, COINIT_MULTITHREADED))) + if (!BarPlayer2StaticInit ()) return false; player2_t out = malloc(sizeof(struct _player_t)); @@ -353,9 +395,12 @@ bool BarPlayer2IsFinished(player2_t player) { LONGLONG time; LONGLONG duration; - if (!player->media) + if (!player->media || player->state == NO_GRAPH) return true; + if (player->state != RUNNING && player->state != STOPPED) + return false; + if (FAILED(IMediaSeeking_GetDuration(player->media, &duration)) || FAILED(IMediaSeeking_GetCurrentPosition(player->media, &time))) return true; diff --git a/src/player2.h b/src/player2.h index ac40eb4..b426ee2 100644 --- a/src/player2.h +++ b/src/player2.h @@ -1,6 +1,6 @@ /* -Copyright (c) 2008-2014 - Lars-Dominik Braun <lars@6xq.net> +Copyright (c) 2015 + Micha³ Cichoñ <thedmd@interia.pl> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal |