From 2e82f691bba3cf428724d7bb12806d1b4f5c0c17 Mon Sep 17 00:00:00 2001 From: GZNGET FOSS Team Date: Mon, 27 Mar 2017 12:52:08 +0800 Subject: Initial Commit --- etc/arctica/pulseaudio/daemon.conf | 86 ++++++ etc/arctica/pulseaudio/default.pa | 59 ++++ .../Services/Audio/PulseAudio/PAVirtualDevices.pm | 322 +++++++++++++++++++++ 3 files changed, 467 insertions(+) create mode 100644 etc/arctica/pulseaudio/daemon.conf create mode 100644 etc/arctica/pulseaudio/default.pa create mode 100644 lib/Arctica/Services/Audio/PulseAudio/PAVirtualDevices.pm diff --git a/etc/arctica/pulseaudio/daemon.conf b/etc/arctica/pulseaudio/daemon.conf new file mode 100644 index 0000000..2371057 --- /dev/null +++ b/etc/arctica/pulseaudio/daemon.conf @@ -0,0 +1,86 @@ +# This file is part of PulseAudio. +# +# PulseAudio is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# PulseAudio 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 Lesser General Public License +# along with PulseAudio; if not, see . + +## Configuration file for the PulseAudio daemon. See pulse-daemon.conf(5) for +## more information. Default values are commented out. Use either ; or # for +## commenting. + +daemonize = no +fail = yes +allow-module-loading = no +allow-exit = no +use-pid-file = no +system-instance = no +;local-server-type = user +; enable-shm = yes +; shm-size-bytes = 0 # setting this 0 will use the system-default, usually 64 MiB +; lock-memory = no +; cpu-limit = no + +; high-priority = yes +; nice-level = -11 + +; realtime-scheduling = yes +; realtime-priority = 5 + +; exit-idle-time = 20 +; scache-idle-time = 20 + +; dl-search-path = (depends on architecture) + +load-default-script-file = yes +default-script-file = /etc/arctica/pulse/default.pa + +; log-target = auto +; log-level = notice +; log-meta = no +; log-time = no +; log-backtrace = 0 + +; resample-method = speex-float-1 +; enable-remixing = yes +; enable-lfe-remixing = yes +; lfe-crossover-freq = 120 + +flat-volumes = no + +; rlimit-fsize = -1 +; rlimit-data = -1 +; rlimit-stack = -1 +; rlimit-core = -1 +; rlimit-as = -1 +; rlimit-rss = -1 +; rlimit-nproc = -1 +; rlimit-nofile = 256 +; rlimit-memlock = -1 +; rlimit-locks = -1 +; rlimit-sigpending = -1 +; rlimit-msgqueue = -1 +; rlimit-nice = 31 +; rlimit-rtprio = 9 +; rlimit-rttime = 200000 + +; default-sample-format = s16le +; default-sample-rate = 44100 +; alternate-sample-rate = 48000 +; default-sample-channels = 2 +; default-channel-map = front-left,front-right + +; default-fragments = 4 +; default-fragment-size-msec = 25 + +; enable-deferred-volume = yes +deferred-volume-safety-margin-usec = 1 +; deferred-volume-extra-delay-usec = 0 diff --git a/etc/arctica/pulseaudio/default.pa b/etc/arctica/pulseaudio/default.pa new file mode 100644 index 0000000..464df41 --- /dev/null +++ b/etc/arctica/pulseaudio/default.pa @@ -0,0 +1,59 @@ +#!/usr/bin/pulseaudio -nF +.fail +load-module module-stream-restore +### Automatically augment property information from .desktop files +### stored in /usr/share/application +load-module module-augment-properties + +### Load several protocols +.ifexists module-esound-protocol-unix.so +load-module module-esound-protocol-unix +.endif +load-module module-native-protocol-unix + +load-module module-null-sink sink_name=arctica.output0 channels=2 sink_properties=device.description="VirtualOutput" +load-module module-null-sink sink_name=arctica.input0 channels=1 sink_properties=device.description=".VirtualInputSink" +load-module module-remap-source master=arctica.input0.monitor source_name=arctica.mic0 source_properties=device.description="VirtualMicrophone" + + +### Honour intended role device property +load-module module-intended-roles + +### Automatically suspend sinks/sources that become idle for too long +load-module module-suspend-on-idle timeout=10 + +### If autoexit on idle is enabled we want to make sure we only quit +### when no local session needs us anymore. +.ifexists module-console-kit.so +load-module module-console-kit +.endif +#.ifexists module-systemd-login.so +#load-module module-systemd-login +#.endif + +### Enable positioned event sounds +#load-module module-position-event-sounds + +### Modules to allow autoloading of filters (such as echo cancellation) +### on demand. module-filter-heuristics tries to determine what filters +### make sense, and module-filter-apply does the heavy-lifting of +### loading modules and rerouting streams. +load-module module-filter-heuristics +load-module module-filter-apply + +### Load X11 bell module +#load-module module-x11-bell sample=x11-bell + +### Register ourselves in the X11 session manager +load-module module-x11-xsmp + +### Publish connection data in the X11 root window +.ifexists module-x11-publish.so +.nofail +load-module module-x11-publish +.fail +.endif + +### Make some devices default +set-default-sink arctica.output0 +set-default-source arctica.mic0 diff --git a/lib/Arctica/Services/Audio/PulseAudio/PAVirtualDevices.pm b/lib/Arctica/Services/Audio/PulseAudio/PAVirtualDevices.pm new file mode 100644 index 0000000..4c7435f --- /dev/null +++ b/lib/Arctica/Services/Audio/PulseAudio/PAVirtualDevices.pm @@ -0,0 +1,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. +# +# Copyright (C) 2015-2017 Mike Gabriel +# +################################################################################ +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 () { + 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; + -- cgit v1.2.3