aboutsummaryrefslogtreecommitdiff
path: root/bin/AudioTestServer
blob: 2d572862c8bff18efb119654716f1bcc2abd6e4c (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
#!/usr/bin/perl  -T -I /audiotest/perlmodules/
################################################################################
#          _____ _
#         |_   _| |_  ___
#           | | | ' \/ -_)
#           |_| |_||_\___|
#                   _   _             ____            _           _
#    / \   _ __ ___| |_(_) ___ __ _  |  _ \ _ __ ___ (_) ___  ___| |_
#   / _ \ | '__/ __| __| |/ __/ _` | | |_) | '__/ _ \| |/ _ \/ __| __|
#  / ___ \| | | (__| |_| | (_| (_| | |  __/| | | (_) | |  __/ (__| |_
# /_/   \_\_|  \___|\__|_|\___\__,_| |_|   |_|  \___// |\___|\___|\__|
#                                                  |__/
#          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>
#
################################################################################
use strict;
use Data::Dumper;
use Arctica::Core::eventInit qw(genARandom BugOUT);
use Arctica::Core::JABus::Socket;
use Arctica::Services::Audio::Server::PulseAudio::PAVirtualDevices;
use Arctica::Services::Audio::Server::PulseAudio::PulseAudio2GST;
#FIXME ADD  something that checks for active PulseAudio before we start doing our stuff....

my $ACO = Arctica::Core::eventInit->new({
	app_name=>'audio-server-test',
	app_class =>'amoduletester',
	app_version=>'0.0.1.1'});


my $JABusServer = Arctica::Core::JABus::Socket->new($ACO,{
	type	=>	"unix",
	destination =>	"local",
	is_server => 1,
	handle_in_dispatch => {
#			heartbeat => \&heartbeat, #FIXME heartbeat will be intergrated in RTT and persistency code in JABus...
			gstctl => \&pa2gst_ctl,# Client facing stuff must be NON pulse centric
			init => \&client_init,
#			cmd => \&client_init,# JABus runtime control
#			sub => \&subsrvc,# JABus runtime notifications service
	},
});

open(SID,">$ACO->{'a_dirs'}{'tmp_adir'}/audiotest_socet_id");
print SID "$JABusServer->{'_socket_id'}\n";
close(SID);

my $PA2GST = Arctica::Services::Audio::Server::PulseAudio::PulseAudio2GST->new($ACO,$JABusServer);

if (@ARGV) {
	foreach my $arg (@ARGV) {
		BugOUT(8,"ARG:\t$arg\t:ARG\n");

		if ($arg =~ /^\-bitrate=([0-9\:]{1,})/) {
			$PA2GST->set_bitrate($1);
		} elsif ($arg =~ /^\-dgst_soc_port\=([io]\d*)\:(\d*)$/) {# FIXME by the time we're doing something with unix sockets this will be looooooong gone...
			$PA2GST->set_device_gst_port($1,$2);
		} elsif ($arg =~ /^\-dgst_soc_type\=([a-z]{3,5})/) {
			$PA2GST->set_device_socket_type($1);
		}

	}
} else {
	BugOUT(0,"NO ARGS?");
}




my $PA_VDev = Arctica::Services::Audio::Server::PulseAudio::PAVirtualDevices->new($ACO,{
		hook_device_state => \&handle_PA_device_events,
	});


my $wtf = Glib::Timeout->add (500, \&chk_bitrate_file, undef, 1 );# FIXME DIRTY HACK... WILL BE HANDELED ON JABus in the TeKi enabled version




$ACO->{'Glib'}{'MainLoop'}->run;


sub pa2gst_ctl {
	my $JDATA = $_[0];
	my (undef,$device_soc_style) = $PA2GST->get_device_socket_type();
#	print "SOCK STYLE: $device_soc_style\n";
#	print "GSTCTL_JSON:\n",Dumper($JDATA);
#	print "Action:\t$JDATA->{'action'}\n";
	if  ($JDATA->{'action'} eq "ready") {
		if ($JDATA->{'type'} eq "output") {
			if ($JDATA->{'idnum'} =~ /^(\d{1,})$/) {
				my $idnum = $1;
				if ($device_soc_style eq "stream") {
					$PA2GST->start_output($idnum,$PA_VDev->{'pa_vdev'}{'output'}{$idnum}{'pa_sink_name'});
				}

			}
		}
	}
	return 1;
}



sub handle_PA_device_events {# FIXME this has moved into PulseAudio2GST...: But then it came back out here...
	print "-------------------------------\n";
	print Dumper(@_),"\n";

	my $type = $_[0];
	my $idnum = $_[1];
	my $name = $_[2];
	my $new_state = $_[3];
	my $clientID = $PA2GST->get_active_client_id();
	my (undef,$device_soc_style) = $PA2GST->get_device_socket_type();

	if ($clientID) {

		if ($type eq "input") {
			if ($new_state eq "R") {
				# START LOCAL THEN CLIENTSIDE
				$PA2GST->start_input($idnum,$PA_VDev->{'pa_vdev'}{$type}{$idnum}{'pa_sink_name'},sub {
					$JABusServer->server_send($clientID,"gstctl",{
											action => "start",
											type   => $type,
											idnum  => $idnum,
											bitrate  => $PA2GST->get_bitrate("input"),
											});
				});

				open(HM,">/tmp/hotmic");print HM time;close(HM);# FIXME DIRTY HACK... WILL BE HANDELED ON JABus in the TeKi enabled version

			} elsif($new_state eq "S") {
				$PA2GST->stop_input($idnum);

				$JABusServer->server_send($clientID,"gstctl",{
										action => "stop",
										type   => $type,
										idnum  => $idnum,
										});

				if (-f "/tmp/hotmic")  {unlink("/tmp/hotmic");}# FIXME DIRTY HACK... WILL BE HANDELED ON JABus in the TeKi enabled version

			}

		} elsif ($type eq "output") {
			if ($new_state eq "R") {

				unless ($device_soc_style ne "datagram") {
					$PA2GST->start_output($idnum,$PA_VDev->{'pa_vdev'}{$type}{$idnum}{'pa_sink_name'});
				}

				$JABusServer->server_send($clientID,"gstctl",{
										action => "start",
										type   => $type,
										idnum  => $idnum,
										});

			} elsif ($new_state eq "S") {
				$JABusServer->server_send($clientID,"gstctl",{
										action => "stop",
										type   => $type,
										idnum  => $idnum,
										});
				$PA2GST->stop_output($idnum);
			}

		}
	}
}



sub client_init {
	my $JSONDATA = $_[0];
	my $client_ID = $_[1];
	my $TheJBUS = $_[2];
	BugOUT(8,"NEW CLIENT! ( $client_ID )");
	if ($ACO->{'aobj'}{'AudioServer'}{'client_ID'}) {
		client_cleanup($ACO->{'aobj'}{'AudioServer'}{'client_ID'});
	}

	$ACO->{'aobj'}{'AudioServer'}{'client_ID'} = $client_ID;
	$PA2GST->set_jbus_client_id($client_ID);
# FIXME Add something to force check of pulse vdev status at this point....
	$PA_VDev->force_chk_dev_state();
#	$TheJBUS->server_send($client_ID,'init','GOOD TO GO!');
	return 1;
}

sub chk_bitrate_file {# FIXME DIRTY HACK... WILL BE HANDELED ON JABus in the TeKi enabled version
	if (-f "/tmp/ch_bitrate") {
		open(BR,"/tmp/ch_bitrate");
		my ($rate,undef) = <BR>;
		close(BR);
		if ($rate =~ /^([0-9\:]{1,})/) {
			BugOUT(1,"CHBITRATE: $rate");
			$PA2GST->set_bitrate($rate);
			my $clientID = $PA2GST->get_active_client_id();
			if ($clientID) {
				my $input_rate = $PA2GST->get_bitrate("input");
				$JABusServer->server_send($clientID,"gstctl",{
							action => "ch_input_bitrate",
							bitrate   => $input_rate ,
						});

			}
		}
		unlink("/tmp/ch_bitrate");
	}
	return 1;
}

sub client_cleanup {
#FIXME Reset everything to a clean state when new client connection is initiated!
# ( Cause you'r mom ain't no perl sub!  )
}