forked from zao/foo_wave_seekbar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cc
157 lines (136 loc) · 3.73 KB
/
Player.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "PchSeekbar.h"
#include "Player.h"
#include "Cache.h"
#include "Helpers.h"
#include <set>
#include <uv.h>
namespace wave
{
struct player_impl : player
{
player_impl();
~player_impl();
virtual void register_waveform_listener(waveform_listener* p) override
{
uv_mutex_lock(&_m);
_listeners.insert(p);
uv_mutex_unlock(&_m);
}
virtual void deregister_waveform_listener(waveform_listener* p) override
{
uv_mutex_lock(&_m);
_listeners.erase(p);
uv_mutex_unlock(&_m);
}
virtual void enumerate_listeners(std::function<void (waveform_listener*)> f) const override
{
uv_mutex_lock(&_m);
for (auto listener : _listeners)
f(listener);
uv_mutex_unlock(&_m);
}
mutable uv_mutex_t _m;
std::set<waveform_listener*> _listeners;
};
struct callbacks : play_callback_impl_base, playlist_callback_impl_base
{
callbacks()
: play_callback_impl_base(play_callback::flag_on_playback_all)
, playlist_callback_impl_base(playlist_callback::flag_on_playback_order_changed)
{}
void on_waveform_result(playable_location_impl loc, std::shared_ptr<get_response> resp)
{
in_main_thread([=]{
util::EventArgs ea;
ea["valid_bucket_count"] = std::to_string(resp->valid_bucket_count);
util::ScopedEvent se("Player", "on_waveform_result", &ea);
static_api_ptr_t<player> p;
static_api_ptr_t<playback_control_v2> pc;
service_ptr_t<metadb_handle> meta;
if (pc->get_now_playing(meta) && meta->get_location() == loc) {
p->enumerate_listeners([&](waveform_listener* l) {
l->on_waveform(resp->waveform);
});
}
});
}
virtual void on_playback_new_track(metadb_handle_ptr meta) override
{
auto duration = meta->get_length();
auto const& loc = meta->get_location();
ref_ptr<waveform> wf;
static_api_ptr_t<player> p;
static_api_ptr_t<cache> c;
if (! c->get_waveform_sync(loc, wf) && ! c->is_location_forbidden(loc)) {
// if not, schedule a scan
auto req = std::make_shared<get_request>();
req->completion_handler = std::bind(&callbacks::on_waveform_result, this, playable_location_impl(loc), std::placeholders::_1);
req->location = loc;
req->user_requested = false;
c->get_waveform(req);
}
else {
auto resp = std::make_shared<get_response>();
resp->waveform = wf;
on_waveform_result(loc, resp);
}
p->enumerate_listeners([&](waveform_listener* l) {
l->on_duration(duration);
l->on_time(0.0);
l->on_location(loc);
l->on_play();
});
}
void update_time(double t)
{
static_api_ptr_t<player> p;
p->enumerate_listeners([&](waveform_listener* l) {
l->on_time(t);
});
}
virtual void on_playback_seek(double t) override
{
update_time(t);
}
virtual void on_playback_stop(playback_control::t_stop_reason) override
{
static_api_ptr_t<player> p;
p->enumerate_listeners([&](waveform_listener* l) {
l->on_stop();
});
}
virtual void on_playback_time(double t) override
{
update_time(t);
}
// uninteresting callbacks
virtual void on_playback_starting(playback_control::t_track_command,bool) override {}
virtual void on_playback_pause(bool) override {}
virtual void on_playback_dynamic_info(const file_info &) override {}
virtual void on_playback_dynamic_info_track(const file_info &) override {}
virtual void on_playback_edited(metadb_handle_ptr) override {}
virtual void on_volume_change(float) override {}
};
player_impl::player_impl()
{
uv_mutex_init(&_m);
}
player_impl::~player_impl()
{
uv_mutex_destroy(&_m);
}
static callbacks* g_callbacks = nullptr;
struct callbacks_iq : initquit
{
virtual void on_init() override
{
wave::g_callbacks = new wave::callbacks();
}
virtual void on_quit() override
{
delete wave::g_callbacks;
}
};
}
static service_factory_single_t<wave::player_impl> g_player;
static initquit_factory_t<wave::callbacks_iq> g_sadf;