aboutsummaryrefslogtreecommitdiff
path: root/lib/Arctica/Services/Audio/Server/PulseAudio
diff options
context:
space:
mode:
authorGZNGET FOSS Team <opensource@gznianguan.com>2017-06-21 12:21:00 +0200
committerMike Gabriel <mike.gabriel@das-netzwerkteam.de>2017-06-21 12:21:25 +0200
commitefc72357fc13a6029538787dbb9379fda2e30795 (patch)
treec8bb5fa46ae22c345b5fc18b06e4e0deec3e0478 /lib/Arctica/Services/Audio/Server/PulseAudio
parentb7ba968d62e9424ad6285f4fe382c315cc9462a7 (diff)
downloadperl-Arctica-Services-Audio-Server-PulseAudio-efc72357fc13a6029538787dbb9379fda2e30795.tar.gz
perl-Arctica-Services-Audio-Server-PulseAudio-efc72357fc13a6029538787dbb9379fda2e30795.tar.bz2
perl-Arctica-Services-Audio-Server-PulseAudio-efc72357fc13a6029538787dbb9379fda2e30795.zip
Update to development state dating 20170421.
Diffstat (limited to 'lib/Arctica/Services/Audio/Server/PulseAudio')
-rw-r--r--lib/Arctica/Services/Audio/Server/PulseAudio/PAVirtualDevices.pm35
-rw-r--r--lib/Arctica/Services/Audio/Server/PulseAudio/PulseAudio2GST.pm360
-rw-r--r--lib/Arctica/Services/Audio/Server/PulseAudio/ThreadGST_server.pm477
3 files changed, 859 insertions, 13 deletions
diff --git a/lib/Arctica/Services/Audio/Server/PulseAudio/PAVirtualDevices.pm b/lib/Arctica/Services/Audio/Server/PulseAudio/PAVirtualDevices.pm
index 4c7435f..9e027ca 100644
--- a/lib/Arctica/Services/Audio/Server/PulseAudio/PAVirtualDevices.pm
+++ b/lib/Arctica/Services/Audio/Server/PulseAudio/PAVirtualDevices.pm
@@ -57,7 +57,7 @@
# Copyright (C) 2015-2017 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
#
################################################################################
-package Arctica::Services::Audio::PulseAudio::PAVirtualDevices;
+package Arctica::Services::Audio::Server::PulseAudio::PAVirtualDevices;
use strict;
use Exporter qw(import);
use Arctica::Core::BugOUT::Basics qw( BugOUT );
@@ -99,12 +99,12 @@ sub new {
$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'} =
+ $self->{'pa_vdev'}{'action_map'}{'by_name'}{'arctica.mic0'} =
{
type => "input",
idnum => 0,
};
- $self->{'pa_vdev'}{'action_map'}{'by_name'}{'arctica.output0'} =
+ $self->{'pa_vdev'}{'action_map'}{'by_name'}{'arctica.output0'} =
{
type => "output",
idnum => 0,
@@ -126,7 +126,7 @@ sub new {
exec_path => "/usr/bin/pactl",# FIXME Make this configurable!
exec_cl_argv => ["subscribe"],
- });
+ });
$arctica_core_object->{'aobj'}{'AudioServer'}{'PA_Virtual_Devices'} = \$self;
@@ -137,6 +137,7 @@ sub new {
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'}}) {
@@ -202,8 +203,8 @@ sub _set_device_our_state {
}
- }
-
+ }
+
BugOUT(9,"set_device_our_state DONE");
}
@@ -269,13 +270,13 @@ sub _set_device_pa_state {
}
- }
+ }
BugOUT(9,"set_device_pa_state DONE");
}
sub _pulse_event_handler {
-# BugOUT(9,"_pulse_event_handler: ENTER");
+ BugOUT(9,"_pulse_event_handler: ENTER");
my $self = $_[0];
if ($_[1] =~ /Event\s*\'change\'\s*on\s*(\w{4,6})\s/) {
my $chWhere = $1;
@@ -291,11 +292,11 @@ sub _pulse_event_handler {
$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)$/) {
+ } elsif ($2 =~ /(RUNNING)$/) {
$self->_set_device_pa_state($devices->{$name}{'type'},$devices->{$name}{'idnum'},$name,"R");
}
- }
- }
+ }
+ }
}
close(PACTL);
} else {
@@ -303,10 +304,18 @@ sub _pulse_event_handler {
}
}
+ BugOUT(9,"_pulse_event_handler: DONE");
return 1;
-# BugOUT(9,"_pulse_event_handler: DONE");
}
+sub force_chk_dev_state {
+ BugOUT(8,"USE OF/(THE?) FORCE!!!: ENTER");
+ my $self = $_[0];
+# (W)HACKY BUT WORKS FINE FOR NOW
+ $self->_pulse_event_handler("Event 'change' on source ");
+ $self->_pulse_event_handler("Event 'change' on sink ");
+ BugOUT(8,"THE LAST JEDI RETIRES...");
+}
sub get_list {
BugOUT(8,"PAVirtualDevices getLIST");
@@ -316,7 +325,7 @@ sub get_list {
return $self->{'pa_vdev'}{'action_map'}{'by_name'};
}
}
-}
+}
1;
diff --git a/lib/Arctica/Services/Audio/Server/PulseAudio/PulseAudio2GST.pm b/lib/Arctica/Services/Audio/Server/PulseAudio/PulseAudio2GST.pm
new file mode 100644
index 0000000..9ac5de0
--- /dev/null
+++ b/lib/Arctica/Services/Audio/Server/PulseAudio/PulseAudio2GST.pm
@@ -0,0 +1,360 @@
+################################################################################
+# _____ _
+# |_ _| |_ ___
+# | | | ' \/ -_)
+# |_| |_||_\___|
+# _ _ ____ _ _
+# / \ _ __ ___| |_(_) ___ __ _ | _ \ _ __ ___ (_) ___ ___| |_
+# / _ \ | '__/ __| __| |/ __/ _` | | |_) | '__/ _ \| |/ _ \/ __| __|
+# / ___ \| | | (__| |_| | (_| (_| | | __/| | | (_) | | __/ (__| |_
+# /_/ \_\_| \___|\__|_|\___\__,_| |_| |_| \___// |\___|\___|\__|
+# |__/
+# 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-2016 Guangzhou Nianguan Electronics Technology Co.Ltd.
+# <opensource@gznianguan.com>
+# Copyright (C) 2015-2016 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
+#
+################################################################################
+package Arctica::Services::Audio::Server::PulseAudio::PulseAudio2GST;
+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( );
+
+my $arctica_core_object;
+
+sub new {
+ BugOUT(9,"PulseAudio2GST new->ENTER");
+ my $class_name = $_[0];# Be EXPLICIT!! DON'T SHIFT OR "@_";
+ $arctica_core_object = $_[1];
+ my $JBUS_Server = $_[2];
+ my $self = {
+ isArctica => 1, # Declare that this is a Arctica "something"
+ aobject_name => "PulseAudio2GST",
+ JBUS_Server => $JBUS_Server,
+ _defaults => {
+ output_bitrate => 64,
+ input_bitrate => 32,
+ },
+ };
+
+ bless($self, $class_name);
+
+
+
+
+
+ $arctica_core_object->{'aobj'}{'AudioServer'}{'PulseAudio2GST'} = \$self;
+
+ BugOUT(9,"PulseAudio2GST new->DONE");
+
+ return $self;
+}
+
+
+sub start_output {
+ BugOUT(9,"PulseAudio2GST start_output->ENTER");
+ my $self = $_[0];
+ my $id_num = $_[1];
+ my $pa_dev = $_[2];
+ BugOUT(8,"Starting OUTPUT:\tAIOD#$id_num\tPA: $pa_dev");
+ if ($self->{'vdev'}{'output'}{$id_num}{'port'} and $self->{'_settings'}{'socket_type'}) {
+ my $pa_dev_monitor = $pa_dev;
+ unless ($pa_dev =~ /\.monitor$/) {
+ $pa_dev_monitor = "$pa_dev.monitor";
+ BugOUT(9,"Append '.monitor' to $pa_dev_monitor");
+ }
+ my $bitrate = $self->get_bitrate("output");
+ $self->{'vdev'}{'output'}{$id_num}{'running'} = 1;
+ $self->{'vdev'}{'output'}{$id_num}{'gst_thread'} = Arctica::Core::Mother::Forker->new($arctica_core_object,{
+ child_name => 'thread_gst',
+ fork_style => 'interactive_pty',
+ handle_stdeoc => sub {return 1;},
+ return_stdin => 1,
+ exec_hold => 0,
+ exec_path => "/audiotest/bin/launch_server_ThreadGST",# FIXME GET FULL PATH FROM CFG OR SOMETHING LIKE THAT
+ exec_cl_argv => [
+ "-src=$self->{'_settings'}{'socket_type'}",
+ "-port=$self->{'vdev'}{'output'}{$id_num}{'port'}",
+ "-pa_device_name=$pa_dev_monitor",
+ "-start_bitrate=$bitrate",
+ ],
+ });
+ } else {
+ BugOUT(1,"PulseAudio2GST start_output port and socket type not set?!! WTF?!");
+ }
+ BugOUT(9,"PulseAudio2GST start_output->DONE");
+}
+
+
+sub stop_output {
+ BugOUT(9,"PulseAudio2GST stop_output->ENTER");
+ my $self = $_[0];
+ my $id_num = $_[1];
+ if ($self->{'vdev'}{'output'}{$id_num}{'gst_thread'}) {
+ $self->{'vdev'}{'output'}{$id_num}{'gst_thread'}->send("cmd:stop:");
+# FIXME FORCE DESTRUCTION OF Mother::Forker object here?
+ $self->{'vdev'}{'output'}{$id_num}{'running'} = 0;
+ }
+ BugOUT(9,"PulseAudio2GST stop_output->DONE");
+}
+
+
+sub start_input {
+ BugOUT(9,"PulseAudio2GST start_input->ENTER");
+ my $self = $_[0];
+ my $id_num = $_[1];
+ my $pa_dev = $_[2];
+ my $run_on_ready = $_[3];
+ BugOUT(8,"Starting INPUT:\tAIOD#$id_num\tPA: $pa_dev");
+ if ($self->{'vdev'}{'input'}{$id_num}{'port'} and $self->{'_settings'}{'socket_type'}) {
+ $self->{'vdev'}{'input'}{$id_num}{'running'} = 1;
+ $self->{'vdev'}{'input'}{$id_num}{'gst_thread'} = Arctica::Core::Mother::Forker->new($arctica_core_object,{
+ child_name => 'thread_gst',
+ fork_style => 'interactive_pty',
+ handle_stdeoc => sub {
+ if ($_[0] =~ /^status:sink_ready:(\d{1,3}):$/) {
+ $run_on_ready->($1);
+ }
+ },
+ return_stdin => 1,
+ exec_hold => 0,
+ exec_path => "/audiotest/bin/launch_server_ThreadGST",# FIXME GET FULL PATH FROM CFG OR SOMETHING LIKE THAT
+ exec_cl_argv => [
+ "-snk=$self->{'_settings'}{'socket_type'}",
+ "-port=$self->{'vdev'}{'input'}{$id_num}{'port'}",
+ "-pa_device_name=$pa_dev",
+ ],
+ });
+ } else {
+ BugOUT(1,"PulseAudio2GST start_input port and socket type not set?!! WTF?!");
+ }
+ BugOUT(9,"PulseAudio2GST start_input->DONE");
+}
+
+
+sub stop_input {
+ BugOUT(9,"PulseAudio2GST stop_input->ENTER");
+ my $self = $_[0];
+ my $id_num = $_[1];
+ if ($self->{'vdev'}{'input'}{$id_num}{'gst_thread'}) {
+ $self->{'vdev'}{'input'}{$id_num}{'gst_thread'}->send("cmd:stop:");
+# FIXME FORCE DESTRUCTION OF Mother::Forker object here?
+ $self->{'vdev'}{'input'}{$id_num}{'running'} = 0;
+ }
+ BugOUT(9,"PulseAudio2GST stop_input->DONE");
+}
+
+
+
+sub thread_cmd {
+ BugOUT(9,"PulseAudio2GST thread_cmd->ENTER");
+ my $self = $_[0];
+ my $type = $_[1];
+ my $idnum= $_[2];
+ my $cmd = $_[3];
+ BugOUT(9,"$type\t$idnum\t$cmd\n");
+ if ($self->{'vdev'}{$type}{$idnum}{'gst_thread'}) {
+ BugOUT(9,"sTEP 2; $type\t$idnum\t$cmd\n");
+ if ($cmd =~ /^([a-z]{1,10})$/) {
+ BugOUT(9,"sTEP 3; $type\t$idnum\t$cmd\n");
+ $self->{'vdev'}{$type}{$idnum}{'gst_thread'}->send("cmd:$1:");
+ BugOUT(8,"PulseAudio2GST: thread_cmd: Sent '$1' to $type #$idnum");
+ }
+ }
+ BugOUT(9,"PulseAudio2GST thread_cmd->DONE");
+ return 1;
+}
+
+
+
+sub set_jbus_client_id {
+ my $self = $_[0];
+ $self->{'jbus_client_id'} = $_[1];
+}
+
+
+sub set_device_socket_type {
+ BugOUT(9,"PulseAudio2GST set_device_socket_type->ENTER");
+ my $self = $_[0];
+
+ if (($_[1] eq "tcp") or ($_[1] eq "unixs")) {
+ $self->{'_settings'}{'socket_type'} = $_[1];
+ $self->{'_settings'}{'com_style'} = "stream";
+ BugOUT(9,"set_device_socket_type: socket type set to $_[1]/stream");
+ } elsif (($_[1] eq "udp") or ($_[1] eq "unixd")) {
+ $self->{'_settings'}{'socket_type'} = $_[1];
+ $self->{'_settings'}{'com_style'} = "datagram";
+ BugOUT(9,"set_device_socket_type: socket type set to $_[1]/datagram");
+ } else {
+ BugOUT(0,"set_device_socket_type: '$_[1]' is not a valid socket_type");
+ }
+
+ BugOUT(9,"PulseAudio2GST set_device_socket_type->DONE");
+}
+
+
+
+sub get_device_socket_type {
+ my $self = $_[0];
+ if ($self->{'_settings'}{'socket_type'} and $self->{'_settings'}{'com_style'}) {
+ return ($self->{'_settings'}{'socket_type'},$self->{'_settings'}{'com_style'});
+ } else {
+ return (0,0);
+ }
+}
+
+
+sub set_device_gst_port {
+ BugOUT(9,"PulseAudio2GST set_device_gst_port->ENTER");
+ my $self = $_[0];
+ my $device = $_[1];
+ my $port = $_[2];
+
+ if ($device =~ /^o(\d{1,})/) {
+ $self->{'vdev'}{'output'}{$1}{'port'} = $port;
+ BugOUT(9,"set_device_gst_port: output:$1:$port");
+ } elsif ($device =~ /^i(\d{1,})/) {
+ $self->{'vdev'}{'input'}{$1}{'port'} = $port;
+ BugOUT(9,"set_device_gst_port: input:$1:$port");
+ } else {
+ BugOUT(2,"set_device_gst_port: Failed to set device '$device' port to '$port'");
+ }
+
+ BugOUT(9,"PulseAudio2GST set_device_gst_port->DONE");
+}
+
+
+sub set_bitrate {
+ BugOUT(9,"PulseAudio2GST set_bitrate->ENTER");
+ my $self = $_[0];
+ my $new_output_rate = 0;
+ my $new_input_rate = 0;
+# We only check that things are somewhat sane here... If we're above or bellow the accepted range, the closest supported range is used
+# Redundant sanity checks are good, but would like to avoid having redundant decission making... (decision is made in the GST thread).
+ if ($_[1] =~ /^(\d{1,})\:(\d{1,})$/) {
+ $new_output_rate = $1;
+ $new_input_rate = $2;
+ BugOUT(9,"Got asymetrical I/O BW ($1 : $2)");
+ } elsif ($1 =~ /^(\d{1,})$/) {
+ $new_output_rate = $1;
+ $new_input_rate = $1;
+ BugOUT(9,"Symetrical I/O BW? ($1)");
+ } else {
+ BugOUT(8,"Weird bitrate format... Using previously set or default BW...");
+ }
+
+ if (($new_output_rate > 0) and ($new_input_rate > 0)) {
+ if (($new_output_rate > 1000) or($new_input_rate > 1000)) {
+ BugOUT(1,"Bitrates0 ($new_output_rate : $new_input_rate) seem high, expecting KILO bit values so maybe knock of a few zeros?");
+ }
+
+ if ($self->{'_settings'}{'output_bitrate'} ne $new_output_rate) {
+ $self->{'_settings'}{'output_bitrate'} = $new_output_rate;
+ foreach my $idnum (keys %{$self->{'vdev'}{'output'}}) {
+
+ if ($self->{'vdev'}{'output'}{$idnum}{'running'}) {
+ if ($self->{'vdev'}{'output'}{$idnum}{'gst_thread'}) {
+ $self->{'vdev'}{'output'}{$idnum}{'gst_thread'}->send("set:bitrate:$new_output_rate");
+ }
+ }
+
+ }
+ BugOUT(9,"Output bitrate set to $new_output_rate");
+ } else {
+ BugOUT(9,"Output bitrate is unchanged...");
+ }
+
+ if ($self->{'_settings'}{'input_bitrate'} ne $new_input_rate) {
+ $self->{'_settings'}{'input_bitrate'} = $new_input_rate;
+# FIXME Add function to brodcast rate change to "live" input threads
+ BugOUT(9,"Input bitrate set to $new_input_rate");
+ } else {
+ BugOUT(9,"Input bitrate is unchanged...");
+ }
+
+
+ }
+ BugOUT(9,"PulseAudio2GST set_bitrate->DONE");
+}
+
+
+sub get_bitrate {
+ my $self = $_[0];
+ if ($_[1] eq "output") {
+ if ($self->{'_settings'}{'output_bitrate'}) {
+ return $self->{'_settings'}{'output_bitrate'};
+ } else {
+ return $self->{'_defaults'}{'output_bitrate'};
+ }
+ } elsif ($_[1] eq "input") {
+ if ($self->{'_settings'}{'input_bitrate'}) {
+ return $self->{'_settings'}{'input_bitrate'};
+ } else {
+ return $self->{'_defaults'}{'input_bitrate'};
+ }
+ } else {
+ BugOUT(2,"And you want bitrate for what? ($_[1])");
+ }
+}
+
+
+sub get_active_client_id {
+ my $self = $_[0];
+ if ($self->{'jbus_client_id'}) {# FIXME Add stuff to chek if this client is still really truly active.
+ return $self->{'jbus_client_id'};
+ } else {
+ return 0;
+ }
+}
+
+1;
+
diff --git a/lib/Arctica/Services/Audio/Server/PulseAudio/ThreadGST_server.pm b/lib/Arctica/Services/Audio/Server/PulseAudio/ThreadGST_server.pm
new file mode 100644
index 0000000..f8b9b37
--- /dev/null
+++ b/lib/Arctica/Services/Audio/Server/PulseAudio/ThreadGST_server.pm
@@ -0,0 +1,477 @@
+################################################################################
+# _____ _
+# |_ _| |_ ___
+# | | | ' \/ -_)
+# |_| |_||_\___|
+# _ _ ____ _ _
+# / \ _ __ ___| |_(_) ___ __ _ | _ \ _ __ ___ (_) ___ ___| |_
+# / _ \ | '__/ __| __| |/ __/ _` | | |_) | '__/ _ \| |/ _ \/ __| __|
+# / ___ \| | | (__| |_| | (_| (_| | | __/| | | (_) | | __/ (__| |_
+# /_/ \_\_| \___|\__|_|\___\__,_| |_| |_| \___// |\___|\___|\__|
+# |__/
+# 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-2016 Guangzhou Nianguan Electronics Technology Co.Ltd.
+# <opensource@gznianguan.com>
+# Copyright (C) 2015-2016 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
+#
+################################################################################
+package Arctica::Services::Audio::Server::PulseAudio::ThreadGST_server;
+use strict;
+use Exporter qw(import);
+use Arctica::Core::BugOUT::Basics qw( BugOUT );
+#use Arctica::Core::Mother::Forker;
+use IO::Handle;
+use Time::HiRes qw( usleep );
+use GStreamer1;
+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( );
+
+my $ACO;
+
+GStreamer1::init([ $0 ]);# Initiate GST
+
+
+sub new {
+ BugOUT(9,"ThreadGST new->ENTER");
+ my $class_name = $_[0];# Be EXPLICIT!! DON'T SHIFT OR "@_";
+ $ACO = $_[1];
+
+ my $self = {
+ isArctica => 1, # Declare that this is a Arctica "something"
+ aobject_name => "ThreadGST",
+ _default_out_rate => 64,# FIXME Look for defaults in a config file somewhere?
+ _default_in_rate => 32,# FIXME (We have a cfg file handler module somewhere.... Use it soon?)
+ _default_pasuspender_fpath => "/usr/bin/pasuspender",# FIXME ^^^^^^^^^^
+ };
+ bless($self, $class_name);
+
+ if ($_[2]) {# FIXME!!!! We got some fancypants module somewhere that handles this.... switch to using that one, some day...!?
+ foreach my $arg (@{$_[2]}) {
+ BugOUT(8,"ARGY:\t$arg\t:ARG\n");
+ if (($arg =~ /^\-(src)\=([a-z]{3,5})/) or ($arg =~ /^\-(snk)\=([a-z]{3,5})/) ){
+ # print "1:\t$1\n2:\t$2\n";
+ if ($1 eq "src") {
+ $self->_set_argv("src_or_snk","src");
+ } elsif ($1 eq "snk") {
+ $self->_set_argv("src_or_snk","snk");
+ } else {
+ BugOUT(9,"Not a server nor a client... what then...?");
+ }
+ if (($2 eq "tcp") or ($2 eq "unixs")) {
+ $self->_set_argv("socket_type",$2);
+ $self->_set_argv("com_style","stream");
+ } elsif (($2 eq "udp") or ($2 eq "unixd")) {
+ $self->_set_argv("socket_type",$2);
+ $self->_set_argv("com_style","datagram");
+ } else {
+ BugOUT(0,"We can't be a '$2' $self->{'s_or_c'}");
+ }
+
+ } elsif ($arg =~ /^\-oo_([a-z]{2,10})\=([a-z0-9]*)/) {
+ $self->_set_argv("opus_$1",$2);
+ } elsif ($arg =~ /^\-port\=([a-z0-9]*)/) {
+ $self->_set_argv("port_or_unix-socket",$1);
+ } elsif ($arg =~ /^\-pa_device_name\=([a-zA-Z0-9\.\_\-]*)/) {
+ BugOUT(8,"pa_device_name", $1);
+ $self->_set_argv("pa_device_name", $1);
+ } elsif ($arg =~ /^\-clientside\=([a-z]*)/) {
+ if ($1 eq "pulseaudio") {
+ $self->_set_argv("clientside","pulseaudio");
+ } else {
+ if ($1 ne "autoaudio") {
+ BugOUT(1,"-clientside=$1 ? wtf? So we're going to try to use autoaudio...");
+ }
+ $self->_set_argv("clientside","autoaudio");
+ }
+
+ } elsif ($arg =~ /^\-wait\=1/) {
+ $self->_set_argv("wait",1);
+ } elsif ($arg =~ /^\-start_bitrate\=(\d{1,3})/) {
+ $self->_set_argv("bitrate",$1);
+ }
+ }
+
+ } else {
+ BugOUT(0,"NO ARGS?");
+ }
+
+ $ACO->{'aobj'}{'ThreadGST'} = \$self;
+
+ BugOUT(9,"ThreadGST new->DONE");
+ return $self;
+}
+
+
+sub ch_options {
+ my $self = $_[0];
+ my $o_name = $_[1];
+ my $o_value = $_[2];
+ if ($o_name eq "bitrate") {
+ if ($o_value =~ /^(\d{1,})$/) {
+ $self->_tune_gstpipe($1);
+ }
+ } else {
+ BugOUT(2,"WTF '$o_name'?");
+ }
+}
+
+sub _set_argv {
+ my $self = $_[0];
+ my $arg_name = $_[1];
+ my $arg_value = $_[2];
+ # FIXME DO A BUNCH OF SANETIZING HERE?!
+ $self->{'_argv'}{$arg_name} = $arg_value;
+}
+
+sub _get_argv {
+ my $self = $_[0];
+ my $arg_name = $_[1];
+ if ($self->{'_argv'}{$arg_name}) {
+ return $self->{'_argv'}{$arg_name};
+ } else {
+ return 0;
+ }
+}
+
+
+sub _tune_gstpipe {
+ my $self = $_[0];
+ my $bitrate = $_[1];
+
+ if ($self->{'main'}{'elements'}{'opusenc'}) {
+ my $opusenc_element = $self->{'main'}{'elements'}{'opusenc'};
+ $bitrate =~ s/\D//g;
+ if ($bitrate > 384) {
+ $bitrate = 384;
+ } elsif ($bitrate < 4) {
+ $bitrate = 4;
+ }
+ $opusenc_element->set("bitrate" => ($bitrate * 1000));
+ BugOUT(1,"\t\tTWEAK IT:\t$bitrate");
+ $opusenc_element->set("inband-fec" => 1);
+ $opusenc_element->set("max-payload-size" => 200);
+ $opusenc_element->set("frame-size" => 20);
+ #FIXME INSERT MORE COMPLEX PERFORMANCE TUNING MATRIX STUFF HERE!
+ }
+}
+
+sub _start_gstsrc {
+ my $self = $_[0];
+ # Cleanup stuff for collecting garbage from serverside pulseaudio
+ $self->{'garbage'}{'pipeline'} = GStreamer1::Pipeline->new('garbagepipe');
+ $self->{'garbage'}{'elements'}{'pasrc'} = GStreamer1::ElementFactory::make( pulsesrc => 'garbage_pasrc' );
+
+ if ($self->_get_argv("pa_device_name")) {
+ my $device_name = $self->_get_argv("pa_device_name");
+ BugOUT(8,"PA_DEVICE_NAME: $device_name");
+ $self->{'garbage'}{'elements'}{'pasrc'}->set('device' => $device_name);# 'arctica.output0.monitor'
+
+ } else {
+ BugOUT(8,"PA_DEVICE_NAME: USING DEFAULT DEVICE");
+ }
+
+ $self->{'garbage'}{'elements'}{'pasrc'}->set('client-name' => 'Arctica Garbage Collector');
+ $self->{'garbage'}{'elements'}{'sink'} = GStreamer1::ElementFactory::make( fakesink => 'garbage_sink' );
+ $self->{'garbage'}{'pipeline'}->add($self->{'garbage'}{'elements'}{'pasrc'});
+ $self->{'garbage'}{'pipeline'}->add($self->{'garbage'}{'elements'}{'sink'});
+ $self->{'garbage'}{'elements'}{'pasrc'}->link($self->{'garbage'}{'elements'}{'sink'});
+ $self->{'garbage'}{'pipeline'}->set_state( "playing" );
+ BugOUT(8,"Garbage collection initiated");
+
+ my $pasuspender_fpath = $self->{'_default_pasuspender_fpath'};# FIXME FIX THIS AFTER CFG FILES HAVE BEEN PROPERLY IMPLEMENTED
+
+ if (-X $pasuspender_fpath) {
+ BugOUT(9,"pasuspender full path: $pasuspender_fpath");
+ my $tpath = $ENV{'PATH'};
+ $ENV{'PATH'} = "/bin:/usr/bin";
+ system($pasuspender_fpath,"true");# FIXME Use Mother::Forker::Light.
+ $ENV{'PATH'} = $tpath;
+ } else {
+ BugOUT(1,"NO pasuspender at full path: $pasuspender_fpath");# Not super critical but some audible junk may occur...
+ }
+
+
+
+ $self->{'main'}{'pipeline'} = GStreamer1::Pipeline->new('pipeline');
+
+ $self->{'main'}{'elements'}{'queue1'} = GStreamer1::ElementFactory::make( queue => 'queue1' );
+ $self->{'main'}{'elements'}{'queue1'}->set("silent" => 0);
+ $self->{'main'}{'elements'}{'queue1'}->set("leaky" => "downstream");
+ $self->{'main'}{'elements'}{'queue1'}->set("max-size-time" => "30000000");
+ $self->{'main'}{'pipeline'}->add($self->{'main'}{'elements'}{'queue1'});
+
+
+ $self->{'main'}{'elements'}{'pasrc'} = GStreamer1::ElementFactory::make( pulsesrc => 'pasrc' );
+
+ if ($self->_get_argv("pa_device_name")) {
+ $self->{'main'}{'elements'}{'pasrc'}->set('device' => $self->_get_argv("pa_device_name"));# 'arctica.output0.monitor'
+ }
+
+ $self->{'main'}{'elements'}{'pasrc'}->set('client-name' => 'Arctica Audio Services');
+
+ $self->{'main'}{'pipeline'}->add($self->{'main'}{'elements'}{'pasrc'});
+ $self->{'main'}{'elements'}{'pasrc'}->link($self->{'main'}{'elements'}{'queue1'});
+
+
+ $self->{'main'}{'elements'}{'opusenc'} = GStreamer1::ElementFactory::make( opusenc => 'opusenc' );
+ $self->_tune_gstpipe($self->_get_argv("bitrate"));
+ $self->{'main'}{'pipeline'}->add($self->{'main'}{'elements'}{'opusenc'});
+ $self->{'main'}{'elements'}{'queue1'}->link($self->{'main'}{'elements'}{'opusenc'});
+
+ if ($self->_get_argv("com_style") eq "datagram") {
+
+ $self->{'main'}{'elements'}{'rtpopuspay'} = GStreamer1::ElementFactory::make( rtpopuspay => 'rtpopuspay' );
+ $self->{'main'}{'pipeline'}->add($self->{'main'}{'elements'}{'rtpopuspay'});
+ $self->{'main'}{'elements'}{'opusenc'}->link($self->{'main'}{'elements'}{'rtpopuspay'});
+
+ if ($self->_get_argv("socket_type") eq "udp") {
+
+ $self->{'main'}{'elements'}{'udpsink'} = GStreamer1::ElementFactory::make( udpsink => 'udpsink' );
+ $self->{'main'}{'pipeline'}->add($self->{'main'}{'elements'}{'udpsink'});
+ $self->{'main'}{'elements'}{'rtpopuspay'}->link($self->{'main'}{'elements'}{'udpsink'});
+
+ $self->{'main'}{'elements'}{'udpsink'}->set('port' => $self->_get_argv("port_or_unix-socket"));
+ $self->{'main'}{'elements'}{'udpsink'}->set('host' => 'localhost');
+
+ } elsif ($self->_get_argv("socket_type") eq "unixd") {
+ BugOUT(0,"NOT YET IMPLEMENTED!");
+ } else {
+ BugOUT(0,"This should never happen!");
+ }
+
+ } elsif ($self->_get_argv("com_style") eq "stream") {
+
+ $self->{'main'}{'elements'}{'gdppay'} = GStreamer1::ElementFactory::make( gdppay => 'gdppay' );
+ $self->{'main'}{'pipeline'}->add($self->{'main'}{'elements'}{'gdppay'});
+ $self->{'main'}{'elements'}{'opusenc'}->link($self->{'main'}{'elements'}{'gdppay'});
+
+ $self->{'main'}{'elements'}{'queue2'} = GStreamer1::ElementFactory::make( queue => 'queue2' );
+ $self->{'main'}{'elements'}{'queue2'}->set("silent" => 0);
+ $self->{'main'}{'elements'}{'queue2'}->set("leaky" => "downstream");
+ $self->{'main'}{'elements'}{'queue2'}->set("max-size-time" => "30000000");
+ $self->{'main'}{'pipeline'}->add($self->{'main'}{'elements'}{'queue2'});
+
+ $self->{'main'}{'elements'}{'gdppay'}->link($self->{'main'}{'elements'}{'queue2'});
+
+ if ($self->_get_argv("socket_type") eq "tcp") {
+ $self->{'main'}{'elements'}{'tcpclientsink'} = GStreamer1::ElementFactory::make( tcpclientsink => 'tcpclientsink' );
+ $self->{'main'}{'elements'}{'tcpclientsink'}->set('port' => $self->_get_argv("port_or_unix-socket"));
+ $self->{'main'}{'elements'}{'tcpclientsink'}->set('host' => 'localhost');
+ $self->{'main'}{'pipeline'}->add($self->{'main'}{'elements'}{'tcpclientsink'});
+ $self->{'main'}{'elements'}{'queue2'}->link($self->{'main'}{'elements'}{'tcpclientsink'});
+
+ } elsif ($self->_get_argv("socket_type") eq "unixs") {
+ BugOUT(0,"NOT YET IMPLEMENTED!");
+ } else {
+ BugOUT(0,"This should never happen!");
+ }
+
+ } else {
+ BugOUT(0,"com_style missing? WTF this should never happen at this point!");
+ }
+
+
+ usleep(5000);
+ $self->{'main'}{'pipeline'}->set_state("paused");
+ usleep(1000);
+ $self->{'main'}{'pipeline'}->set_state("playing");
+ BugOUT(8,"Main pipeline initiated");
+ usleep(100000);
+ $self->{'garbage'}{'pipeline'}->set_state("paused");
+ $self->{'garbage'}{'pipeline'}->set_state("null");
+ BugOUT(8,"Garbage collection done.");
+
+}
+
+
+
+sub _start_gstsnk {
+ BugOUT(9,"ThreadGST _start_gstsnk->ENTER");
+ my $self = $_[0];
+##############################
+#
+ $self->{'garbage'}{'pipeline'} = GStreamer1::Pipeline->new('garbagepipe');
+ $self->{'garbage'}{'elements'}{'pasrc'} = GStreamer1::ElementFactory::make( pulsesrc => 'garbage_pasrc' );
+
+# if ($self->_get_argv("pa_device_name")) {
+
+ my $device_name = $self->_get_argv("pa_device_name");
+ BugOUT(8,"PA_DEVICE_NAME: $device_name");
+ print "INPUT: PA_DEVICE_NAME: $device_name";
+ $self->{'garbage'}{'elements'}{'pasrc'}->set('device' => "arctica.mic0");# 'arctica.output0.monitor'
+
+# } else {
+# BugOUT(8,"PA_DEVICE_NAME: USING DEFAULT DEVICE");
+# }
+
+ $self->{'garbage'}{'elements'}{'pasrc'}->set('client-name' => 'Arctica Garbage Collector');
+ $self->{'garbage'}{'elements'}{'sink'} = GStreamer1::ElementFactory::make( fakesink => 'garbage_sink' );
+ $self->{'garbage'}{'pipeline'}->add($self->{'garbage'}{'elements'}{'pasrc'});
+ $self->{'garbage'}{'pipeline'}->add($self->{'garbage'}{'elements'}{'sink'});
+ $self->{'garbage'}{'elements'}{'pasrc'}->link($self->{'garbage'}{'elements'}{'sink'});
+ $self->{'garbage'}{'pipeline'}->set_state( "playing" );
+ BugOUT(8,"Garbage collection initiated");
+ my $pasuspender_fpath = $self->{'_default_pasuspender_fpath'};# FIXME FIX THIS AFTER CFG FILES HAVE BEEN PROPERLY IMPLEMENTED
+
+ if (-X $pasuspender_fpath) {
+ BugOUT(9,"pasuspender full path: $pasuspender_fpath");
+ my $tpath = $ENV{'PATH'};
+ $ENV{'PATH'} = "/bin:/usr/bin";
+ system($pasuspender_fpath,"true");# FIXME Use Mother::Forker::Light.
+ $ENV{'PATH'} = $tpath;
+ } else {
+ BugOUT(1,"NO pasuspender at full path: $pasuspender_fpath");# Not super critical but some audible junk may occur...
+ }
+#
+##############################
+ $self->{'main'}{'pipeline'} = GStreamer1::Pipeline->new('pipeline');
+
+ $self->{'main'}{'elements'}{'opusdec'} = GStreamer1::ElementFactory::make( opusdec => 'opusdec' );
+ $self->{'main'}{'pipeline'}->add($self->{'main'}{'elements'}{'opusdec'});
+
+ if ($self->_get_argv("com_style") eq "datagram") {
+ if ($self->_get_argv("socket_type") eq "udp") {
+
+ $self->{'main'}{'elements'}{'udpsrc'} = GStreamer1::ElementFactory::make( udpsrc => 'udpsrc' );
+ $self->{'main'}{'elements'}{'udpsrc'}->set('port' => $self->_get_argv("port_or_unix-socket"));
+ $self->{'main'}{'elements'}{'udpsrc'}->set( caps => GStreamer1::Caps::Simple->new(
+ 'application/x-rtp',
+ 'media' => 'Glib::String' => 'audio',
+ 'clock-rate' => 'Glib::Int' => 48000,
+ 'encoding-name' => 'Glib::String' => 'X-GST-OPUS-DRAFT-SPITTKA-00'));
+
+ $self->{'main'}{'pipeline'}->add($self->{'main'}{'elements'}{'udpsrc'});
+
+ $self->{'main'}{'elements'}{'rtpopusdepay'} = GStreamer1::ElementFactory::make( rtpopusdepay => 'rtpopusdepay' );
+ $self->{'main'}{'pipeline'}->add($self->{'main'}{'elements'}{'rtpopusdepay'});
+
+ $self->{'main'}{'elements'}{'udpsrc'}->link($self->{'main'}{'elements'}{'rtpopusdepay'});
+ $self->{'main'}{'elements'}{'rtpopusdepay'}->link($self->{'main'}{'elements'}{'opusdec'});
+
+ } elsif ($self->_get_argv("socket_type") eq "unixd") {
+ BugOUT(0,"NOT YET IMPLEMENTED!");
+ } else {
+ BugOUT(0,"This should never happen!");
+ }
+ } elsif ($self->_get_argv("com_style") eq "stream") {
+ if ($self->_get_argv("socket_type") eq "tcp") {
+
+ $self->{'main'}{'elements'}{'tcpserversrc'} = GStreamer1::ElementFactory::make( tcpserversrc => 'tcpserversrc' );
+ $self->{'main'}{'elements'}{'tcpserversrc'}->set('port' => $self->_get_argv("port_or_unix-socket"));
+ $self->{'main'}{'pipeline'}->add($self->{'main'}{'elements'}{'tcpserversrc'});
+
+ $self->{'main'}{'elements'}{'queue1'} = GStreamer1::ElementFactory::make( queue => 'queue1' );
+ $self->{'main'}{'elements'}{'queue1'}->set("silent" => 0);
+ $self->{'main'}{'elements'}{'queue1'}->set("leaky" => "downstream");
+ $self->{'main'}{'elements'}{'queue1'}->set("max-size-time" => "30000000");
+ $self->{'main'}{'pipeline'}->add($self->{'main'}{'elements'}{'queue1'});
+
+ $self->{'main'}{'elements'}{'tcpserversrc'}->link($self->{'main'}{'elements'}{'queue1'});
+
+ $self->{'main'}{'elements'}{'gdpdepay'} = GStreamer1::ElementFactory::make( gdpdepay => 'gdpdepay' );
+ $self->{'main'}{'pipeline'}->add($self->{'main'}{'elements'}{'gdpdepay'});
+
+ $self->{'main'}{'elements'}{'queue1'}->link($self->{'main'}{'elements'}{'gdpdepay'});
+ $self->{'main'}{'elements'}{'gdpdepay'}->link($self->{'main'}{'elements'}{'opusdec'});
+
+ } elsif ($self->_get_argv("socket_type") eq "unixs") {
+ BugOUT(0,"NOT YET IMPLEMENTED!");
+ } else {
+ BugOUT(0,"This should never happen!");
+ }
+ }
+
+
+
+ $self->{'main'}{'elements'}{'pasink'} = GStreamer1::ElementFactory::make( pulsesink => 'pasink' );
+
+# if ($self->_get_argv("pa_device_name")) {
+# $self->{'main'}{'elements'}{'pasink'}->set('device' => $self->_get_argv("pa_device_name"));
+# }
+
+ $self->{'main'}{'elements'}{'pasink'}->set('device' => "arctica.input0");
+
+ $self->{'main'}{'elements'}{'pasink'}->set('client-name' => 'Arctica Audio Services');
+
+ $self->{'main'}{'pipeline'}->add($self->{'main'}{'elements'}{'pasink'});
+ $self->{'main'}{'elements'}{'opusdec'}->link($self->{'main'}{'elements'}{'pasink'});
+
+ usleep(10000);
+ $self->{'main'}{'pipeline'}->set_state("playing");
+ BugOUT(8,"Main pipeline initiated");
+ print "status:sink_ready:",$self->_get_argv("idnum"),":\n";
+ usleep(100000);
+ $self->{'garbage'}{'pipeline'}->set_state( "paused" );
+ $self->{'garbage'}{'pipeline'}->set_state( "null" );
+
+ BugOUT(9,"ThreadGST _start_gstsnk->DONE");
+}
+
+sub start {
+ my $self = $_[0];
+ if ($self->_get_argv("src_or_snk") eq "src") {
+ $self->_start_gstsrc;
+ } elsif ($self->_get_argv("src_or_snk") eq "snk") {
+ $self->_start_gstsnk;
+ } else {
+ BugOUT(0,"WTF? (src_or_snk!!)")
+ }
+}
+
+sub terminate {
+ my $self = $_[0];
+ if ($self->{'main'}{'pipeline'}) {
+ $self->{'main'}{'pipeline'}->set_state("paused");
+ $self->{'main'}{'pipeline'}->set_state("null");
+ }
+ # FIXME! Do something else too??
+}
+
+1;
+