-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathStream.vala
166 lines (141 loc) · 4.4 KB
/
Stream.vala
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
158
159
160
161
162
163
164
165
166
// Smoothing function adapted from https://gitlab.gnome.org/GNOME/gnome-control-center/-/blob/ea8bafc5d1ebb945a0806b03dea0c3abf29c58d9/panels/sound/cc-level-bar.c
public class Tuba.Widgets.Audio.Stream : GLib.Object {
const double SMOOTHING = 0.2;
Gst.Bin pipeline;
Gst.Bus bus;
Gst.Element uridecodebin;
public signal void ended ();
public double level { get; private set; default=0.0; }
public double volume { get; set; default=1.0; }
public bool muted { get; set; default=false; }
public bool ready { get; private set; default=false; }
public double progress {
set {
pipeline.seek_simple (Gst.Format.TIME, Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT, (int64) (value / 10 * duration));
update_metadata ();
}
}
public int64 duration { get; private set; default=0; }
public int64 current { get; private set; default=0; }
private string _url = "";
public string url {
get {
return _url;
}
set {
_url = value;
uridecodebin.set_property ("uri", value);
}
}
private Gst.State _state = Gst.State.NULL;
public Gst.State state {
get { return _state; }
set {
pipeline.set_state (value);
}
}
private bool update_metadata () {
if (bus == null) return false;
if (this.state <= Gst.State.PAUSED) return true;
update_current ();
return true;
}
private bool bus_callback (Gst.Bus bus, Gst.Message message) {
switch (message.type) {
case Gst.MessageType.ERROR:
GLib.Error err;
string debug_log;
message.parse_error (out err, out debug_log);
critical (@"Gst Error: $(err.message)");
debug (@"Gst Error Debug: $debug_log");
break;
case Gst.MessageType.EOS:
pipeline.seek_simple (Gst.Format.TIME, Gst.SeekFlags.FLUSH, 0);
ended ();
break;
case Gst.MessageType.STATE_CHANGED:
message.parse_state_changed (null, out _state, null);
if (!ready && (_state == Gst.State.READY || _state == Gst.State.PLAYING)) this.ready = true;
break;
case Gst.MessageType.ELEMENT:
unowned Gst.Structure s = message.get_structure ();
if (s != null && s.get_name () == "level") {
unowned GLib.Value peaks = s.get_value ("peak");
unowned GLib.ValueArray value_array = (GLib.ValueArray) peaks.get_boxed ();
double level_sum = 0;
foreach (var val in value_array) {
level_sum += val.get_double ();
}
var levels = Math.pow (10, (level_sum / value_array.n_values) / 20);
level = (levels * SMOOTHING) + (level * (1.0 - SMOOTHING));
}
break;
case Gst.MessageType.ASYNC_DONE:
if (this.duration == 0) {
update_duration ();
}
break;
case Gst.MessageType.DURATION_CHANGED:
update_duration ();
break;
default:
break;
}
return true;
}
private void update_duration () {
int64 t_duration = 0;
if (!pipeline.query_duration (Gst.Format.TIME, out t_duration)) {
debug (@"Couldn't get duration of $url");
return;
}
if (t_duration != this.duration) this.duration = t_duration;
}
private void update_current () {
int64 t_current = 0;
if (!pipeline.query_position (Gst.Format.TIME, out t_current)) {
debug (@"Couldn't get current position of $url");
return;
}
if (t_current != this.current) this.current = t_current;
}
uint timeout_id = -1;
construct {
string pipestr = "uridecodebin name=uridecodebin ! audioconvert ! audio/x-raw,channels=2 ! volume name=volume ! level name=level interval=50000000 ! autoaudiosink name=sink";
try {
pipeline = (Gst.Bin) Gst.parse_launch (pipestr);
uridecodebin = pipeline.get_by_name ("uridecodebin");
var volume = pipeline.get_by_name ("volume");
this.bind_property ("volume", volume, "volume", BindingFlags.SYNC_CREATE);
// this.bind_property ("muted", volume, "muted", BindingFlags.SYNC_CREATE);
var level = pipeline.get_by_name ("level");
level.set_property ("post-messages", true);
var sink = pipeline.get_by_name ("sink");
sink.set_property ("sync", true);
bus = pipeline.get_bus ();
bus.add_watch (0, bus_callback);
timeout_id = GLib.Timeout.add_seconds (1, update_metadata);
} catch (Error e) {
critical (@"Error while constructing pipeline: $(e.message)");
}
}
// Without disconnecting everything
// it leaks.
public void destroy () {
if (timeout_id > 0) {
GLib.Source.remove (timeout_id);
timeout_id = 0;
}
if (bus != null) {
bus.remove_watch ();
bus = null;
}
if (pipeline != null) {
this.state = Gst.State.NULL;
pipeline = null;
}
}
~Stream () {
debug ("Destroying AudioStream");
}
}