aboutsummaryrefslogtreecommitdiff
path: root/lib/Arctica/Services/Audio/PulseAudio/PAVirtualDevices.pm
blob: 4c7435f5051552a7f614e8ca439d16a5273f3c05 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
################################################################################
#          _____ _
#         |_   _| |_  ___
#           | | | ' \/ -_)
#           |_| |_||_\___|
#                   _   _             ____            _           _
#    / \   _ __ ___| |_(_) ___ __ _  |  _ \ _ __ ___ (_) ___  ___| |_
#   / _ \ | '__/ __| __| |/ __/ _` | | |_) | '__/ _ \| |/ _ \/ __| __|
#  / ___ \| | | (__| |_| | (_| (_| | |  __/| | | (_) | |  __/ (__| |_
# /_/   \_\_|  \___|\__|_|\___\__,_| |_|   |_|  \___// |\___|\___|\__|
#                                                  |__/
#          The Arctica Modular Remote Computing Framework
#
################################################################################
#
# Copyright (C) 2015-2016 The Arctica Project
# http://arctica-project.org/
#
# This code is dual licensed: strictly GPL-2 or AGPL-3+
#
# GPL-2
# -----
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
#
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#
# AGPL-3+
# -------
# This programm is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This programm is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Copyright (C) 2015-2017 Guangzhou Nianguan Electronics Technology Co.Ltd.
#                         <opensource@gznianguan.com>
# Copyright (C) 2015-2017 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
#
################################################################################
package Arctica::Services::Audio::PulseAudio::PAVirtualDevices;
use strict;
use Exporter qw(import);
use Arctica::Core::BugOUT::Basics qw( BugOUT );
use Arctica::Core::Mother::Forker;
use Data::Dumper;# Remove this before release! (unless we're still dependant)

# Be very selective about what (if any) gets exported by default:
our @EXPORT = qw( );
# And be mindfull of what we lett the caller request here too:
our @EXPORT_OK = qw( getLIST_amap_by_name );

my $arctica_core_object;

sub new {
	BugOUT(9,"PAVirtualDevices new->ENTER");
	my $class_name = $_[0];# Be EXPLICIT!! DON'T SHIFT OR "@_";
	$arctica_core_object = $_[1];
	my $self = {
		isArctica => 1, # Declare that this is a Arctica "something"
		aobject_name => "PulseAudio_Devices",
		hook_device_state => $_[2]->{'hook_device_state'},
		pa_timeout => 5,
	};


	bless($self, $class_name);
# Set up initial "required" default devices... (Do this cleaner in the future!?)
# At some point we may add support for dynamically creating new devices.
	$self->{'pa_vdev'}{'input'}{0}{'pa_sink_name'} = "arctica.input0";
	$self->{'pa_vdev'}{'input'}{0}{'pa_source_name'} = "arctica.mic0";
	$self->{'pa_vdev'}{'input'}{0}{'pa_state'} = "S";
	$self->{'pa_vdev'}{'input'}{0}{'pa_idle_since'} = 0;
	$self->{'pa_vdev'}{'input'}{0}{'our_state'} = "S";
	$self->{'pa_vdev'}{'input'}{0}{'gst_thread'} = 0;
	$self->{'pa_vdev'}{'output'}{0}{'pa_sink_name'} = "arctica.output0";
	$self->{'pa_vdev'}{'output'}{0}{'pa_state'} = "S";
	$self->{'pa_vdev'}{'output'}{0}{'pa_idle_since'} = 0;
	$self->{'pa_vdev'}{'output'}{0}{'our_state'} = "S";
	$self->{'pa_vdev'}{'output'}{0}{'gst_thread'} = 0;

# "action_map" may go away.... (limited usefullness if any in current itteration of code.)
	$self->{'pa_vdev'}{'action_map'}{'by_name'}{'arctica.mic0'} = 
							{
								type  => "input",
								idnum => 0,
							};
	$self->{'pa_vdev'}{'action_map'}{'by_name'}{'arctica.output0'} = 	
							{
								type  => "output",
								idnum => 0,
							};
# Start Subscribing to PA Events
# FIXME Structure of output from pactl is not guaranteed to be reliable but works ok for now.
# Replace with custom PA client in the future.
	$self->{'subscribe_pulse_events'} = Arctica::Core::Mother::Forker->new($arctica_core_object,{# FIXME Clean up this one!
		child_name	=>	'pulse_events',
		fork_style	=>	'interactive_pty',
		handle_stdeoc	=>	sub {$self->_pulse_event_handler(@_)},
		return_stdin	=>	0,
		exec_hold	=>	0,
		env_strict	=>	0,
		env_pass	=> {
			'ARCTICA'	=> 1,
			'USER'		=> 1,
		},
		exec_path	=>	"/usr/bin/pactl",# FIXME  Make this configurable!
		exec_cl_argv	=>	["subscribe"],

	});	

	$arctica_core_object->{'aobj'}{'AudioServer'}{'PA_Virtual_Devices'} = \$self;

	$self->{'GlibTimeout_suspend_idle'} = Glib::Timeout->add (1000, sub {$self->_suspend_idle}, undef, 1 );

	BugOUT(9,"PAVirtualDevices new->DONE");

	return $self;
}

sub _suspend_idle {# Cause we can't always rely on PulseAudio for this.... (PA BUG?)
	my $self = $_[0];
	foreach my $idnum (keys %{$self->{'pa_vdev'}{'input'}}) {
		if (($self->{'pa_vdev'}{'input'}{$idnum}{'pa_state'} eq "I") and ($self->{'pa_vdev'}{'input'}{$idnum}{'our_state'} ne "S")) {
			if ($self->{'pa_vdev'}{'input'}{$idnum}{'pa_idle_since'} < (time - $self->{'pa_timeout'})) {
				$self->_set_device_our_state("input",$idnum,$self->{'pa_vdev'}{'input'}{$idnum}{'pa_source_name'},"S");
			}

		}
	}

	foreach my $idnum (keys %{$self->{'pa_vdev'}{'output'}}) {
		if (($self->{'pa_vdev'}{'output'}{$idnum}{'pa_state'} eq "I") and ($self->{'pa_vdev'}{'output'}{$idnum}{'our_state'} ne "S")) {
			if ($self->{'pa_vdev'}{'output'}{$idnum}{'pa_idle_since'} < (time - $self->{'pa_timeout'})) {
				$self->_set_device_our_state("output",$idnum,$self->{'pa_vdev'}{'output'}{$idnum}{'pa_sink_name'},"S");
			}

		}
	}
	return 1;
}

sub _set_device_our_state {
	BugOUT(9,"set_device_our_state ENTER");
	my $self = $_[0];
	my $type = $_[1];
	my $idnum = $_[2];
	my $name = $_[3];
	my $new_state = $_[4];

	if ($type eq "input") {
		BugOUT(8,"INPUT");
		if ($self->{'pa_vdev'}{'input'}{$idnum}{'pa_source_name'} eq $name) {# overkill but sanity checks can be good...

			unless ($self->{'pa_vdev'}{'input'}{$idnum}{'our_state'} eq $new_state) {
				$self->{'pa_vdev'}{'input'}{$idnum}{'our_state'} = $new_state;
#print "$name\t$new_state\n";
#				if ($new_state eq "R") {
#				} elsif ($new_state eq "I") {
#				} elsif ($new_state eq "S") {
#				}
				$self->{'hook_device_state'}($type,$idnum,$name,$new_state);
			} else {
					# DO NOTHING !
			}

		}

	} elsif ($type eq "output") {
		BugOUT(8,"OUTPUT");
		if ($self->{'pa_vdev'}{'output'}{$idnum}{'pa_sink_name'} eq $name) {

			unless ($self->{'pa_vdev'}{'output'}{$idnum}{'our_state'} eq $new_state) {
				$self->{'pa_vdev'}{'output'}{$idnum}{'our_state'} = $new_state;
#print "$name\t$new_state\n";
#				if ($new_state eq "R") {
#				} elsif ($new_state eq "I") {
#				} elsif ($new_state eq "S") {
#				}
				$self->{'hook_device_state'}($type,$idnum,$name,$new_state);
			}

		}


	} 
	

	BugOUT(9,"set_device_our_state DONE");
}

sub _set_device_pa_state {
	BugOUT(9,"set_device_pa_state ENTER");
	my $self = $_[0];
	my $type = $_[1];
	my $idnum = $_[2];
	my $name = $_[3];
	my $new_state = $_[4];

	if ($type eq "input") {
#		BugOUT(8,"INPUT");
		if ($self->{'pa_vdev'}{'input'}{$idnum}{'pa_source_name'} eq $name) {# overkill but sanity checks can be good...

			unless ($self->{'pa_vdev'}{'input'}{$idnum}{'pa_state'} eq $new_state) {
				$self->{'pa_vdev'}{'input'}{$idnum}{'pa_state'} = $new_state;

				if ($new_state eq "R") {
					$self->{'pa_vdev'}{'input'}{$idnum}{'pa_idle_since'} = 0;
					$self->_set_device_our_state($type,$idnum,$name,$new_state);

				} elsif ($new_state eq "I") {
					$self->{'pa_vdev'}{'input'}{$idnum}{'pa_idle_since'} = time();
#					$self->_set_device_our_state($type,$idnum,$name,$new_state);

				} elsif ($new_state eq "S") {
					$self->_set_device_our_state($type,$idnum,$name,$new_state);

				}

			} else {
				# DO NOTHING?
			}

		}

	} elsif ($type eq "output") {
#		BugOUT(8,"OUTPUT");
		if ($self->{'pa_vdev'}{'output'}{$idnum}{'pa_sink_name'} eq $name) {

			unless ($self->{'pa_vdev'}{'output'}{$idnum}{'pa_state'} eq $new_state) {
				$self->{'pa_vdev'}{'output'}{$idnum}{'pa_state'} = $new_state;
#print "$name\t$new_state\n";
				if ($new_state eq "R") {
					$self->{'pa_vdev'}{'output'}{$idnum}{'pa_idle_since'} = 0;
					$self->_set_device_our_state($type,$idnum,$name,$new_state);

				} elsif ($new_state eq "I") {
					$self->{'pa_vdev'}{'output'}{$idnum}{'pa_idle_since'} = time();
#					$self->_set_device_our_state($type,$idnum,$name,$new_state);

				} elsif ($new_state eq "S") {
					$self->_set_device_our_state($type,$idnum,$name,$new_state);

				}

			} else {
				# DO NOTHING?
			}

		}


	} 
	BugOUT(9,"set_device_pa_state DONE");

}

sub _pulse_event_handler {
#	BugOUT(9,"_pulse_event_handler: ENTER");
	my $self = $_[0];
	if ($_[1] =~ /Event\s*\'change\'\s*on\s*(\w{4,6})\s/) {
		my $chWhere = $1;
		if (($chWhere eq "source") or ($chWhere eq "sink")) {
#			BugOUT(9,"_pulse_event_handler: device is $chWhere");
			my $devices = $self->get_list('action_map','by_name');
			open(PACTL, "-|", "/usr/bin/pactl","list","$chWhere"."s","short");# FIXME Use mother:forker::light  when its ready!?
			while (<PACTL>) {
				if ($_ =~ /.*\s*(arctica\.[^\s]*)(.*)\n/) {
					my $name = $1;
					if ($devices->{$name}) {
						if ($2=~ /(IDLE)$/) {
							$self->_set_device_pa_state($devices->{$name}{'type'},$devices->{$name}{'idnum'},$name,"I");
						} elsif ($2 =~ /(SUSPENDED)$/) {
							$self->_set_device_pa_state($devices->{$name}{'type'},$devices->{$name}{'idnum'},$name,"S");
						} elsif ($2 =~ /(RUNNING)$/) {	
							$self->_set_device_pa_state($devices->{$name}{'type'},$devices->{$name}{'idnum'},$name,"R");
						}
					}						
				}	
			}
			close(PACTL);
		} else {
			BugOUT(9,"pulse_event_handler: event we currently don't care about... (Yawn..)");
		}

	}
	return 1;
#	BugOUT(9,"_pulse_event_handler: DONE");
}


sub get_list {
	BugOUT(8,"PAVirtualDevices getLIST");
	my $self = $_[0];
	if ($_[1] eq 'action_map') {
		if ($_[2] eq 'by_name') {
			return $self->{'pa_vdev'}{'action_map'}{'by_name'};
		}
	}
}  

1;