| 1 | package Peta::Synth::Output::Pulse; | ||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: sound to the sound server as it plays (PulseAudio or PipeWire) | ||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||
| 4 | # Sound to the sound server, as it plays: a client of the PulseAudio native | ||||||||||||||||||||||||||||||||||
| 5 | # protocol, which PipeWire speaks too (pipewire-pulse) and which carries the | ||||||||||||||||||||||||||||||||||
| 6 | # sound on the socket itself. PipeWire's own protocol passes the sound in | ||||||||||||||||||||||||||||||||||
| 7 | # shared memory (memfd), its file descriptors sent over the socket | ||||||||||||||||||||||||||||||||||
| 8 | # (SCM_RIGHTS): Perl's core has no sendmsg for that, and reaches it only | ||||||||||||||||||||||||||||||||||
| 9 | # through syscall(), with each architecture's syscall numbers and struct | ||||||||||||||||||||||||||||||||||
| 10 | # layouts (pperl also through Peta::FFI). This protocol needs none of that. | ||||||||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||||||||
| 12 | # The protocol: packets of a descriptor (five 32-bit numbers: length, | ||||||||||||||||||||||||||||||||||
| 13 | # channel, offset high and low, flags) and a payload. Commands go on | ||||||||||||||||||||||||||||||||||
| 14 | # channel 0xFFFFFFFF as tagged values (a type byte, then the value); | ||||||||||||||||||||||||||||||||||
| 15 | # sound goes on the channel the server gave the stream. The server says how | ||||||||||||||||||||||||||||||||||
| 16 | # many bytes it wants (in its reply to the stream's creation, then in | ||||||||||||||||||||||||||||||||||
| 17 | # REQUEST commands); write() sends no more than that, and waits for the | ||||||||||||||||||||||||||||||||||
| 18 | # next request when it has sent it all: the server's clock paces whoever | ||||||||||||||||||||||||||||||||||
| 19 | # writes. | ||||||||||||||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||||||||||||||
| 21 | # Floats, two channels, at the synthesizer's rate; the server converts. | ||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use v5.36; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use IO::Socket::UNIX; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use IO::Select; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use Peta::Synth::Output; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||
| 28 | 1 | our $VERSION = '0.2610180'; | |||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use constant { | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 31 | 1 | VERSION => 32, # of the protocol: what this client sends | |||||||||||||||||||||||||||||||||
| 32 | CONTROL => 0xFFFFFFFF, # the channel of commands | ||||||||||||||||||||||||||||||||||
| 33 | INVALID => 0xFFFFFFFF, | ||||||||||||||||||||||||||||||||||
| 34 | FRAME => 8, # bytes: two 32-bit floats | ||||||||||||||||||||||||||||||||||
| 35 | FLOAT32LE => 5, | ||||||||||||||||||||||||||||||||||
| 36 | FRONT_LEFT => 1, | ||||||||||||||||||||||||||||||||||
| 37 | FRONT_RIGHT => 2, | ||||||||||||||||||||||||||||||||||
| 38 | VOLUME_NORM => 0x10000, | ||||||||||||||||||||||||||||||||||
| 39 | TIMEOUT => 10, # seconds a reply may take | ||||||||||||||||||||||||||||||||||
| 40 | 1 | }; | |||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||
| 42 | # The commands. | ||||||||||||||||||||||||||||||||||
| 43 | 1 | ▶ | use constant { | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 44 | 1 | ERROR => 0, | |||||||||||||||||||||||||||||||||
| 45 | REPLY => 2, | ||||||||||||||||||||||||||||||||||
| 46 | CREATE_PLAYBACK_STREAM => 3, | ||||||||||||||||||||||||||||||||||
| 47 | DELETE_PLAYBACK_STREAM => 4, | ||||||||||||||||||||||||||||||||||
| 48 | AUTH => 8, | ||||||||||||||||||||||||||||||||||
| 49 | SET_CLIENT_NAME => 9, | ||||||||||||||||||||||||||||||||||
| 50 | DRAIN_PLAYBACK_STREAM => 12, | ||||||||||||||||||||||||||||||||||
| 51 | REQUEST => 61, | ||||||||||||||||||||||||||||||||||
| 52 | PLAYBACK_STREAM_KILLED => 64, | ||||||||||||||||||||||||||||||||||
| 53 | 1 | }; | |||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||
| 55 | # Where the server is: $PULSE_SERVER (unix:/path or a path), or the socket | ||||||||||||||||||||||||||||||||||
| 56 | # in the user's runtime directory. | ||||||||||||||||||||||||||||||||||
| 57 | 3 | ▶ | sub address () { | ||||||||||||||||||||||||||||||||
|
Subroutine
address: called CC=7 SCAR=29.6
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 58 | 3 | my $server = $ENV{PULSE_SERVER}; | |||||||||||||||||||||||||||||||||
| 59 | 3 | ▶ | if (defined $server && length $server) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $server and length $server
MC/DC0%0/2
defined $server and length $server
defined $server
length $server
Truth table33%1/3
A and B
A= defined $serverB= length $server
| |||||||||||||||||||||||||||||||||||
| 60 | 3 | for my $one (split ' ', $server) { | |||||||||||||||||||||||||||||||||
| 61 | 3 | ▶ | return $1 if $one =~ m{\Aunix:(.+)\z}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | return $one if $one =~ m{\A/}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||
| 64 | 0 | die "only local servers (unix:/path) are spoken to, not '$server'\n"; | |||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | my $runtime = $ENV{XDG_RUNTIME_DIR} // "/run/user/$<"; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$ENV{"XDG_RUNTIME_DIR"} // "/run/user/$<"
MC/DC0%0/1
$ENV{"XDG_RUNTIME_DIR"} // "/run/user/$<"
$ENV{"XDG_RUNTIME_DIR"}
Truth table0%0/2
A
A= $ENV{"XDG_RUNTIME_DIR"}
| |||||||||||||||||||||||||||||||||||
| 67 | 0 | return "$runtime/pulse/native"; | |||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||
| 70 | # Whether there is a server to play to. | ||||||||||||||||||||||||||||||||||
| 71 | 1 | ▶ | sub available () { return -S address() } | ||||||||||||||||||||||||||||||||
|
Subroutine
available: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||
| 73 | # new(rate => 48000, latency => 0.1, name => 'Peta::Synth'): connected, | ||||||||||||||||||||||||||||||||||
| 74 | # authenticated, with a stream; dies with the server's word for why not. | ||||||||||||||||||||||||||||||||||
| 75 | 1 | ▶ | sub new ($class, %opt) { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 76 | 1 | my $path = address(); | |||||||||||||||||||||||||||||||||
| 77 | 1 | ▶ | my $socket = IO::Socket::UNIX->new(Peer => $path) or die "no sound server at $path: $!\n"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 78 | 1 | binmode $socket; | |||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||
| 80 | my $self = bless { | ||||||||||||||||||||||||||||||||||
| 81 | socket => $socket, | ||||||||||||||||||||||||||||||||||
| 82 | select => IO::Select->new($socket), | ||||||||||||||||||||||||||||||||||
| 83 | tag => 0, # the next command's | ||||||||||||||||||||||||||||||||||
| 84 | buffer => '', # what has come and is not yet read | ||||||||||||||||||||||||||||||||||
| 85 | 1 | ▶ | rate => $opt{rate} // 48000, | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opt{"rate"} // 48000
MC/DC0%0/1
$opt{"rate"} // 48000
$opt{"rate"}
Truth table50%1/2
A
A= $opt{"rate"}
| |||||||||||||||||||||||||||||||||||
| 86 | requested => 0, # bytes the server has asked for | ||||||||||||||||||||||||||||||||||
| 87 | written => 0, # bytes sent | ||||||||||||||||||||||||||||||||||
| 88 | }, $class; | ||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||
| 90 | 1 | my ($version) = $self->_command(AUTH, _u32(VERSION) . _arbitrary(_cookie())); | |||||||||||||||||||||||||||||||||
| 91 | 1 | ▶ | $self->{version} = ($version & 0xFFFF) < VERSION ? $version & 0xFFFF : VERSION; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 92 | 1 | ▶ | die "the sound server speaks protocol $self->{version}; at least 13 is needed\n" if $self->{version} < 13; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | 1 | ▶ | $self->_command(SET_CLIENT_NAME, _proplist('application.name' => $opt{name} // 'Peta::Synth')); | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opt{"name"} // "Peta::Synth"
MC/DC0%0/1
$opt{"name"} // "Peta::Synth"
$opt{"name"}
Truth table50%1/2
A
A= $opt{"name"}
| |||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||
| 96 | 1 | ▶ | my $body = _stream_body($self->{version}, $self->{rate}, $opt{latency} // 0.1, $opt{title} // 'Peta::Synth'); | ||||||||||||||||||||||||||||||||
|
Condition50%2/4
$opt{"latency"} // "0.1"
$opt{"title"} // "Peta::Synth"
MC/DC0%0/1
$opt{"latency"} // "0.1"
$opt{"latency"}
MC/DC0%0/1
$opt{"title"} // "Peta::Synth"
$opt{"title"}
Truth table50%1/2
A
A= $opt{"latency"}
Truth table50%1/2
A
A= $opt{"title"}
| |||||||||||||||||||||||||||||||||||
| 97 | 1 | my ($channel, undef, $requested) = $self->_command(CREATE_PLAYBACK_STREAM, $body); | |||||||||||||||||||||||||||||||||
| 98 | 1 | $self->{channel} = $channel; | |||||||||||||||||||||||||||||||||
| 99 | 1 | $self->{requested} = $requested; | |||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||
| 101 | 1 | return $self; | |||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||
| 104 | # What CREATE_PLAYBACK_STREAM asks for, as protocol $version has it: floats, | ||||||||||||||||||||||||||||||||||
| 105 | # front left and right, to the default sink; the server to keep about | ||||||||||||||||||||||||||||||||||
| 106 | # $latency seconds ahead of what it plays. | ||||||||||||||||||||||||||||||||||
| 107 | 1 | ▶ | sub _stream_body ($version, $rate, $latency, $title) { | ||||||||||||||||||||||||||||||||
|
Subroutine
_stream_body: called CC=6 SCAR=19.0
| |||||||||||||||||||||||||||||||||||
| 108 | 1 | my $target = int($latency * $rate) * FRAME; | |||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||
| 110 | my $body = join '', | ||||||||||||||||||||||||||||||||||
| 111 | _sample_spec(FLOAT32LE, 2, $rate), | ||||||||||||||||||||||||||||||||||
| 112 | _channel_map(FRONT_LEFT, FRONT_RIGHT), | ||||||||||||||||||||||||||||||||||
| 113 | _u32(INVALID), _string(undef), # the default sink | ||||||||||||||||||||||||||||||||||
| 114 | _u32(INVALID), # maxlength: the server's | ||||||||||||||||||||||||||||||||||
| 115 | _boolean(0), # not corked | ||||||||||||||||||||||||||||||||||
| 116 | _u32($target), # tlength | ||||||||||||||||||||||||||||||||||
| 117 | _u32(INVALID), _u32(INVALID), # prebuf, minreq: the server's | ||||||||||||||||||||||||||||||||||
| 118 | _u32(0), # sync id | ||||||||||||||||||||||||||||||||||
| 119 | _cvolume(VOLUME_NORM, VOLUME_NORM), | ||||||||||||||||||||||||||||||||||
| 120 | 1 | (map { _boolean(0) } 1 .. 7), # no remap, no remix, fix format, rate, channels; may move; fixed rate | |||||||||||||||||||||||||||||||||
| 121 | _boolean(0), # not muted | ||||||||||||||||||||||||||||||||||
| 122 | _boolean(1), # adjust the latency | ||||||||||||||||||||||||||||||||||
| 123 | _proplist('media.name' => $title, 'media.role' => 'music'); | ||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||
| 125 | 1 | ▶ | $body .= _boolean(0) . _boolean(0) if $version >= 14; # volume not set; no early requests | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 126 | 1 | ▶ | $body .= _boolean(0) . _boolean(0) . _boolean(0) if $version >= 15; # mute not set; may suspend; no failing on suspend | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 127 | 1 | ▶ | $body .= _boolean(0) if $version >= 17; # volume not relative | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 128 | 1 | ▶ | $body .= _boolean(0) if $version >= 18; # no passthrough | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 129 | 1 | ▶ | $body .= _u8(0) if $version >= 21; # no formats beyond the sample spec | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||
| 131 | 1 | return $body; | |||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub rate ($self) { return $self->{rate} } | ||||||||||||||||||||||||||||||||
|
Subroutine
rate: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||
| 136 | # Seconds of sound sent so far. | ||||||||||||||||||||||||||||||||||
| 137 | 1 | ▶ | sub sent ($self) { return $self->{written} / FRAME / $self->{rate} } | ||||||||||||||||||||||||||||||||
|
Subroutine
sent: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||
| 139 | # Sound: as much as the server asks for is sent, and then this waits for | ||||||||||||||||||||||||||||||||||
| 140 | # it to ask again. | ||||||||||||||||||||||||||||||||||
| 141 | 2 | ▶ | sub write ($self, $left, $right) { | ||||||||||||||||||||||||||||||||
|
Subroutine
write: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 142 | 2 | my $bytes = pack 'f<*', @{ Peta::Synth::Output::interleave($left, $right) }; | |||||||||||||||||||||||||||||||||
| 143 | 2 | ▶ | while (length $bytes) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 144 | 12 | ▶ | $self->_receive(1) while $self->{requested} < FRAME; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||
| 146 | 12 | my $part = substr $bytes, 0, $self->{requested} - $self->{requested} % FRAME, ''; | |||||||||||||||||||||||||||||||||
| 147 | 12 | $self->_send($self->{channel}, $part); | |||||||||||||||||||||||||||||||||
| 148 | 12 | $self->{requested} -= length $part; | |||||||||||||||||||||||||||||||||
| 149 | 12 | $self->{written} += length $part; | |||||||||||||||||||||||||||||||||
| 150 | 12 | $self->_receive(0); | |||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||
| 152 | 2 | return $self; | |||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||
| 155 | # Whether the server would take more now, without waiting. | ||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | sub wants ($self) { | ||||||||||||||||||||||||||||||||
|
Subroutine
wants: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 157 | 0 | $self->_receive(0); | |||||||||||||||||||||||||||||||||
| 158 | 0 | return $self->{requested} >= FRAME; | |||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||
| 161 | # The rest played to its end, the stream let go, the connection closed. | ||||||||||||||||||||||||||||||||||
| 162 | 1 | ▶ | sub close ($self) { | ||||||||||||||||||||||||||||||||
|
Subroutine
close: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 163 | 1 | ▶ | return if !$self->{socket}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 164 | 1 | eval { | |||||||||||||||||||||||||||||||||
| 165 | 1 | $self->_command(DRAIN_PLAYBACK_STREAM, _u32($self->{channel}), 60); | |||||||||||||||||||||||||||||||||
| 166 | 1 | $self->_command(DELETE_PLAYBACK_STREAM, _u32($self->{channel})); | |||||||||||||||||||||||||||||||||
| 167 | }; | ||||||||||||||||||||||||||||||||||
| 168 | 1 | my $error = $@; | |||||||||||||||||||||||||||||||||
| 169 | 1 | CORE::close $self->{socket}; | |||||||||||||||||||||||||||||||||
| 170 | 1 | delete $self->{socket}; | |||||||||||||||||||||||||||||||||
| 171 | 1 | ▶ | die $error if $error; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 172 | 1 | return; | |||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||
| 175 | # Stops at once: what the server still holds is not played. | ||||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | sub stop ($self) { | ||||||||||||||||||||||||||||||||
|
Subroutine
stop: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | return if !$self->{socket}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 178 | 0 | eval { $self->_command(DELETE_PLAYBACK_STREAM, _u32($self->{channel})) }; | |||||||||||||||||||||||||||||||||
| 179 | 0 | CORE::close $self->{socket}; | |||||||||||||||||||||||||||||||||
| 180 | 0 | delete $self->{socket}; | |||||||||||||||||||||||||||||||||
| 181 | 0 | return; | |||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||
| 184 | 1 | ▶ | sub DESTROY ($self) { $self->stop if $self->{socket} && ${^GLOBAL_PHASE} ne 'DESTRUCT' } | ||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=3 SCAR=14.2
Branch: 1 branch
Condition33%1/3
$self->{"socket"} and ${^GLOBAL_PHASE} ne "DESTRUCT"
MC/DC0%0/2
$self->{"socket"} and ${^GLOBAL_PHASE} ne "DESTRUCT"
$self->{"socket"}
${^GLOBAL_PHASE} ne "DESTRUCT"
Truth table33%1/3
A and B
A= $self->{"socket"}B= ${^GLOBAL_PHASE} ne "DESTRUCT"
| |||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||
| 186 | # A command and its reply: the values the reply holds. | ||||||||||||||||||||||||||||||||||
| 187 | 5 | ▶ | sub _command ($self, $command, $body, $timeout = TIMEOUT) { | ||||||||||||||||||||||||||||||||
|
Subroutine
_command: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||
| 188 | 5 | my $tag = $self->{tag}++; | |||||||||||||||||||||||||||||||||
| 189 | 5 | $self->_send(CONTROL, _u32($command) . _u32($tag) . $body); | |||||||||||||||||||||||||||||||||
| 190 | 5 | my $until = time + $timeout; | |||||||||||||||||||||||||||||||||
| 191 | 5 | ▶ | while (time <= $until) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 192 | 6 | ▶ | my $reply = $self->_receive(1, $tag) // next; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->_receive(1, $tag) // (next)
MC/DC100%1/1
$self->_receive(1, $tag) // (next)
$self->_receive(1, $tag)
Truth table100%2/2
A
A= $self->_receive(1, $tag)
| |||||||||||||||||||||||||||||||||||
| 193 | 5 | return @$reply; | |||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||
| 195 | 0 | die "the sound server did not answer\n"; | |||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||
| 198 | 17 | ▶ | sub _send ($self, $channel, $payload) { | ||||||||||||||||||||||||||||||||
|
Subroutine
_send: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 199 | 17 | my $packet = pack('N5', length $payload, $channel, 0, 0, 0) . $payload; | |||||||||||||||||||||||||||||||||
| 200 | 17 | ▶ | while (length $packet) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 201 | 17 | my $sent = syswrite $self->{socket}, $packet; | |||||||||||||||||||||||||||||||||
| 202 | 17 | ▶ | die "the sound server went away: $!\n" if !defined $sent; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 203 | 17 | substr($packet, 0, $sent) = ''; | |||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||
| 205 | 17 | return; | |||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||
| 208 | # Reads what has come (waiting for something if $wait), handles the | ||||||||||||||||||||||||||||||||||
| 209 | # server's commands; returns the reply to $tag when it is among them. | ||||||||||||||||||||||||||||||||||
| 210 | 29 | ▶ | sub _receive ($self, $wait, $tag = undef) { | ||||||||||||||||||||||||||||||||
|
Subroutine
_receive: called CC=17 SCAR=33.7
| |||||||||||||||||||||||||||||||||||
| 211 | 29 | my $got; | |||||||||||||||||||||||||||||||||
| 212 | 29 | ▶ | if ($self->{select}->can_read($wait ? 1 : 0)) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 213 | 17 | my $read = sysread $self->{socket}, $self->{buffer}, 65536, length $self->{buffer}; | |||||||||||||||||||||||||||||||||
| 214 | 17 | ▶ | die "the sound server went away\n" if !$read; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||
| 216 | 29 | ▶ | while (length $self->{buffer} >= 20) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 217 | 17 | my ($length, $channel) = unpack 'N2', $self->{buffer}; | |||||||||||||||||||||||||||||||||
| 218 | 17 | ▶ | last if length $self->{buffer} < 20 + $length; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 219 | 17 | my $payload = substr $self->{buffer}, 20, $length; | |||||||||||||||||||||||||||||||||
| 220 | 17 | substr($self->{buffer}, 0, 20 + $length) = ''; | |||||||||||||||||||||||||||||||||
| 221 | 17 | ▶ | next if $channel != CONTROL; # sound for us: none is asked for | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 222 | 17 | my @values = _values($payload); | |||||||||||||||||||||||||||||||||
| 223 | 17 | my ($command, $their_tag) = splice @values, 0, 2; | |||||||||||||||||||||||||||||||||
| 224 | 17 | ▶ | if ($command == REQUEST) { | ||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||
| 225 | 12 | ▶ | $self->{requested} += $values[1] if $values[0] == $self->{channel}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||
| 227 | elsif ($command == PLAYBACK_STREAM_KILLED) { | ||||||||||||||||||||||||||||||||||
| 228 | 0 | die "the sound server ended the stream\n"; | |||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||
| 230 | elsif ($command == ERROR) { | ||||||||||||||||||||||||||||||||||
| 231 | 0 | ▶ | die "the sound server refused: error $values[0]\n" if defined $tag && $their_tag == $tag; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $tag and $their_tag == $tag
MC/DC0%0/2
defined $tag and $their_tag == $tag
defined $tag
$their_tag == $tag
Truth table0%0/3
A and B
A= defined $tagB= $their_tag == $tag
| |||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||
| 233 | ▶ | elsif ($command == REPLY && defined $tag && $their_tag == $tag) { | |||||||||||||||||||||||||||||||||
|
Condition33%2/6
$command == 2 and defined $tag and $their_tag == $tag
$command == 2 and defined $tag
MC/DC0%0/3
$command == 2 and defined $tag and $their_tag == $tag
$command == 2
defined $tag
$their_tag == $tag
Truth table25%1/4
A and B and C
A= $command == 2B= defined $tagC= $their_tag == $tag
| |||||||||||||||||||||||||||||||||||
| 234 | 5 | $got = \@values; | |||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||
| 237 | 29 | return $got; | |||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||
| 240 | # The cookie that proves this user may play; a server that does not ask | ||||||||||||||||||||||||||||||||||
| 241 | # for one takes zeros. | ||||||||||||||||||||||||||||||||||
| 242 | 1 | ▶ | sub _cookie () { | ||||||||||||||||||||||||||||||||
|
Subroutine
_cookie: called CC=6 SCAR=22.9
| |||||||||||||||||||||||||||||||||||
| 243 | 1 | my @dirs = grep { defined } $ENV{XDG_CONFIG_HOME}, "$ENV{HOME}/.config"; | |||||||||||||||||||||||||||||||||
| 244 | 1 | my @files = ($ENV{PULSE_COOKIE}, map { "$_/pulse/cookie" } @dirs); | |||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||
| 246 | 1 | for my $file (@files) { | |||||||||||||||||||||||||||||||||
| 247 | 2 | ▶ | next if !defined $file || !-r $file; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $file and -r $file
MC/DC0%0/2
defined $file and -r $file
defined $file
-r $file
Truth table66%2/3
A and B
A= defined $fileB= -r $file
| |||||||||||||||||||||||||||||||||||
| 248 | 0 | ▶ | open my $fh, '<:raw', $file or next; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 249 | 0 | read $fh, my $cookie, 256; | |||||||||||||||||||||||||||||||||
| 250 | 0 | ▶ | return $cookie if length $cookie == 256; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||
| 252 | 1 | return "\0" x 256; | |||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||
| 255 | # Tagged values. | ||||||||||||||||||||||||||||||||||
| 256 | 1 | ▶ | sub _u8 ($v) { return 'B' . pack 'C', $v } | ||||||||||||||||||||||||||||||||
|
Subroutine
_u8: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 257 | 22 | ▶ | sub _u32 ($v) { return 'L' . pack 'N', $v } | ||||||||||||||||||||||||||||||||
|
Subroutine
_u32: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 258 | 17 | ▶ | sub _boolean ($v) { return $v ? '1' : '0' } | ||||||||||||||||||||||||||||||||
|
Subroutine
_boolean: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 259 | 6 | ▶ | sub _string ($s) { return defined $s ? "t$s\0" : 'N' } | ||||||||||||||||||||||||||||||||
|
Subroutine
_string: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 260 | 4 | ▶ | sub _arbitrary ($bytes) { return 'x' . pack('N', length $bytes) . $bytes } | ||||||||||||||||||||||||||||||||
|
Subroutine
_arbitrary: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 261 | 1 | ▶ | sub _sample_spec ($format, $channels, $rate) { return 'a' . pack 'C C N', $format, $channels, $rate } | ||||||||||||||||||||||||||||||||
|
Subroutine
_sample_spec: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 262 | 1 | ▶ | sub _channel_map (@positions) { return 'm' . pack 'C C*', scalar @positions, @positions } | ||||||||||||||||||||||||||||||||
|
Subroutine
_channel_map: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 263 | 1 | ▶ | sub _cvolume (@volumes) { return 'v' . pack 'C N*', scalar @volumes, @volumes } | ||||||||||||||||||||||||||||||||
|
Subroutine
_cvolume: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 264 | 2 | ▶ | sub _proplist (%props) { | ||||||||||||||||||||||||||||||||
|
Subroutine
_proplist: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 265 | 2 | my $list = 'P'; | |||||||||||||||||||||||||||||||||
| 266 | 2 | for my $key (sort keys %props) { | |||||||||||||||||||||||||||||||||
| 267 | 3 | my $value = "$props{$key}\0"; | |||||||||||||||||||||||||||||||||
| 268 | 3 | $list .= _string($key) . _u32(length $value) . _arbitrary($value); | |||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||
| 270 | 2 | return $list . _string(undef); | |||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||
| 273 | # The values of a payload, as far as they are of the kinds read here. | ||||||||||||||||||||||||||||||||||
| 274 | # | ||||||||||||||||||||||||||||||||||
| 275 | # Each type reads its value at $at and says how many bytes it took; lists | ||||||||||||||||||||||||||||||||||
| 276 | # (sample specs, channel maps, volumes, timevals) are array references. | ||||||||||||||||||||||||||||||||||
| 277 | my %VALUE = ( | ||||||||||||||||||||||||||||||||||
| 278 | 63 | ▶ | L => sub ($p, $at) { (unpack('N', substr $p, $at, 4), 4) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 279 | 0 | ▶ | B => sub ($p, $at) { (unpack('C', substr $p, $at, 1), 1) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | R => sub ($p, $at) { (unpack('Q>', substr $p, $at, 8), 8) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 281 | 0 | ▶ | r => sub ($p, $at) { (unpack('Q>', substr $p, $at, 8), 8) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 282 | 0 | ▶ | U => sub ($p, $at) { (unpack('Q>', substr $p, $at, 8), 8) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 283 | 0 | ▶ | 1 => sub ($p, $at) { ('1', 0) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 284 | 0 | ▶ | 0 => sub ($p, $at) { ('0', 0) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 285 | 0 | ▶ | N => sub ($p, $at) { (undef, 0) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 286 | 0 | ▶ | t => sub ($p, $at) { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 287 | 0 | my $end = index $p, "\0", $at; | |||||||||||||||||||||||||||||||||
| 288 | 0 | return (substr($p, $at, $end - $at), $end - $at + 1); | |||||||||||||||||||||||||||||||||
| 289 | }, | ||||||||||||||||||||||||||||||||||
| 290 | 0 | ▶ | x => sub ($p, $at) { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 291 | 0 | my $n = unpack 'N', substr $p, $at, 4; | |||||||||||||||||||||||||||||||||
| 292 | 0 | return (substr($p, $at + 4, $n), 4 + $n); | |||||||||||||||||||||||||||||||||
| 293 | }, | ||||||||||||||||||||||||||||||||||
| 294 | 0 | ▶ | a => sub ($p, $at) { ([ unpack 'C C N', substr $p, $at, 6 ], 6) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 295 | 0 | ▶ | m => sub ($p, $at) { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 296 | 0 | my $n = unpack 'C', substr $p, $at, 1; | |||||||||||||||||||||||||||||||||
| 297 | 0 | return ([ unpack "C$n", substr $p, $at + 1, $n ], 1 + $n); | |||||||||||||||||||||||||||||||||
| 298 | }, | ||||||||||||||||||||||||||||||||||
| 299 | 0 | ▶ | v => sub ($p, $at) { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 300 | 0 | my $n = unpack 'C', substr $p, $at, 1; | |||||||||||||||||||||||||||||||||
| 301 | 0 | return ([ unpack "N$n", substr $p, $at + 1, 4 * $n ], 1 + 4 * $n); | |||||||||||||||||||||||||||||||||
| 302 | }, | ||||||||||||||||||||||||||||||||||
| 303 | 0 | ▶ | T => sub ($p, $at) { ([ unpack 'N2', substr $p, $at, 8 ], 8) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 304 | 1 | ); | |||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||
| 306 | 17 | ▶ | sub _values ($payload) { | ||||||||||||||||||||||||||||||||
|
Subroutine
_values: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 307 | 17 | my @values; | |||||||||||||||||||||||||||||||||
| 308 | 17 | my $at = 0; | |||||||||||||||||||||||||||||||||
| 309 | 17 | ▶ | while ($at < length $payload) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 310 | 63 | my $type = substr $payload, $at++, 1; | |||||||||||||||||||||||||||||||||
| 311 | 63 | ▶ | my $read = $VALUE{$type} // last; # proplists and formats: nothing here needs them, and they come last | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$VALUE{$type} // (last)
MC/DC0%0/1
$VALUE{$type} // (last)
$VALUE{$type}
Truth table50%1/2
A
A= $VALUE{$type}
| |||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||
| 313 | 63 | my ($value, $bytes) = $read->($payload, $at); | |||||||||||||||||||||||||||||||||
| 314 | 63 | push @values, $value; | |||||||||||||||||||||||||||||||||
| 315 | 63 | $at += $bytes; | |||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||
| 317 | 17 | return @values; | |||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||
| 320 | 1 | 1; | |||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||
| 322 | __END__ | ||||||||||||||||||||||||||||||||||