| 1 | package Peta::Synth::Output::PipeWire; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: sound to PipeWire as it plays (pperl) | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | # Sound to PipeWire as it plays, through PipeWire's own library | ||||||||||||||||||||||||||||
| 5 | # (libpipewire-0.3, MIT licensed), called with pperl's Peta::FFI: pperl | ||||||||||||||||||||||||||||
| 6 | # only. perl5 plays through Peta::Synth::Output::Pulse. | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # libpipewire runs its loop in a thread of its own (pw_thread_loop), and C | ||||||||||||||||||||||||||||
| 9 | # never calls back into Perl: write() takes the loop's lock, takes the | ||||||||||||||||||||||||||||
| 10 | # empty buffers the stream has (pw_stream_dequeue_buffer), fills them, | ||||||||||||||||||||||||||||
| 11 | # gives them back (pw_stream_queue_buffer), and lets go of the lock; while | ||||||||||||||||||||||||||||
| 12 | # more is waiting than the stream takes, it sleeps a few milliseconds and | ||||||||||||||||||||||||||||
| 13 | # looks again. The stream's pace is the caller's. | ||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||
| 15 | # What crosses the boundary is laid out here by hand, as the headers of | ||||||||||||||||||||||||||||
| 16 | # PipeWire 1.x have it on a 64-bit machine: | ||||||||||||||||||||||||||||
| 17 | # | ||||||||||||||||||||||||||||
| 18 | # pw_stream_events uint32 version, then 11 function pointers (all NULL) | ||||||||||||||||||||||||||||
| 19 | # pw_buffer spa_buffer *buffer @0, user_data @8, uint64 size @16, | ||||||||||||||||||||||||||||
| 20 | # uint64 requested @24 | ||||||||||||||||||||||||||||
| 21 | # spa_buffer uint32 n_metas, n_datas, spa_meta *metas @8, spa_data *datas @16 | ||||||||||||||||||||||||||||
| 22 | # spa_data uint32 type, flags, int64 fd @8, uint32 mapoffset @16, | ||||||||||||||||||||||||||||
| 23 | # uint32 maxsize @20, void *data @24, spa_chunk *chunk @32 | ||||||||||||||||||||||||||||
| 24 | # spa_chunk uint32 offset, uint32 size, int32 stride, int32 flags | ||||||||||||||||||||||||||||
| 25 | # pw_time ... uint64 queued @32 ... (64 bytes) | ||||||||||||||||||||||||||||
| 26 | # | ||||||||||||||||||||||||||||
| 27 | # The stream's format is an SPA POD built here: an Object of type Format, | ||||||||||||||||||||||||||||
| 28 | # id EnumFormat, with media type audio, subtype raw, format F32LE, the rate, | ||||||||||||||||||||||||||||
| 29 | # two channels, positions FL FR. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | use Peta::Synth::Output; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | our $VERSION = '0.2610180'; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 1 | LIBRARY => 'libpipewire-0.3.so.0', | |||||||||||||||||||||||||||
| 39 | DIRECTION_OUTPUT => 1, | ||||||||||||||||||||||||||||
| 40 | ID_ANY => -1, # 0xFFFFFFFF, as the C int it is passed as | ||||||||||||||||||||||||||||
| 41 | AUTOCONNECT => 1, | ||||||||||||||||||||||||||||
| 42 | MAP_BUFFERS => 4, | ||||||||||||||||||||||||||||
| 43 | STATE_ERROR => -1, | ||||||||||||||||||||||||||||
| 44 | STATE_PAUSED => 2, | ||||||||||||||||||||||||||||
| 45 | STATE_STREAMING => 3, | ||||||||||||||||||||||||||||
| 46 | FRAME => 8, # bytes: two 32-bit floats | ||||||||||||||||||||||||||||
| 47 | POLL => 0.002, # seconds between looks at the stream | ||||||||||||||||||||||||||||
| 48 | 1 | }; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # SPA types and keys (spa/utils/type.h, spa/param/*.h). | ||||||||||||||||||||||||||||
| 51 | 1 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 1 | TYPE_ID => 3, | |||||||||||||||||||||||||||
| 53 | TYPE_INT => 4, | ||||||||||||||||||||||||||||
| 54 | TYPE_ARRAY => 13, | ||||||||||||||||||||||||||||
| 55 | TYPE_OBJECT => 15, | ||||||||||||||||||||||||||||
| 56 | OBJECT_FORMAT => 0x40003, | ||||||||||||||||||||||||||||
| 57 | PARAM_ENUMFORMAT => 3, | ||||||||||||||||||||||||||||
| 58 | FORMAT_MEDIATYPE => 1, | ||||||||||||||||||||||||||||
| 59 | FORMAT_MEDIASUBTYPE => 2, | ||||||||||||||||||||||||||||
| 60 | FORMAT_AUDIO_FORMAT => 0x10001, | ||||||||||||||||||||||||||||
| 61 | FORMAT_AUDIO_RATE => 0x10003, | ||||||||||||||||||||||||||||
| 62 | FORMAT_AUDIO_CHANNELS => 0x10004, | ||||||||||||||||||||||||||||
| 63 | FORMAT_AUDIO_POSITION => 0x10005, | ||||||||||||||||||||||||||||
| 64 | MEDIA_AUDIO => 1, | ||||||||||||||||||||||||||||
| 65 | SUBTYPE_RAW => 1, | ||||||||||||||||||||||||||||
| 66 | AUDIO_F32LE => 0x11B, | ||||||||||||||||||||||||||||
| 67 | CHANNEL_FL => 3, | ||||||||||||||||||||||||||||
| 68 | CHANNEL_FR => 4, | ||||||||||||||||||||||||||||
| 69 | 1 | }; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 1 | my ($LIB, $INIT); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub _call ($name, $signature, @args) { return Peta::FFI::call($LIB, $name, $signature, @args) } | ||||||||||||||||||||||||||
|
Subroutine
_call: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # Whether PipeWire can be played to from here: pperl's Peta::FFI and the | ||||||||||||||||||||||||||||
| 76 | # library. Leaves the reason in $@ when not. | ||||||||||||||||||||||||||||
| 77 | 1 | ▶ | sub available () { | ||||||||||||||||||||||||||
|
Subroutine
available: called CC=5 SCAR=26.2
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | return 1 if $LIB; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | return 0 if !eval { require Peta::FFI; 1 }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | $LIB = eval { Peta::FFI::dlopen(LIBRARY) }; | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | return 1 if $LIB; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $@ ||= 'no ' . LIBRARY . "\n"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$@ ||= "no libpipewire-0.3.so.0\n"
MC/DC0%0/1
$@ ||= "no libpipewire-0.3.so.0\n"
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||
| 84 | 0 | return 0; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # new(rate => 48000, latency => 0.1, title => ...): a stream, connected and | ||||||||||||||||||||||||||||
| 88 | # ready; dies with the reason when PipeWire will not have it. | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub new ($class, %opt) { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=15 SCAR=54.8
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | available() or die "PipeWire cannot be played to from here: $@"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if (!$INIT) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | _call('pw_init', '(oo)v', 0, 0); | |||||||||||||||||||||||||||
| 93 | 0 | $INIT = 1; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | my $rate = $opt{rate} // 48000; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opt{"rate"} // 48000
MC/DC0%0/1
$opt{"rate"} // 48000
$opt{"rate"}
Truth table0%0/2
A
A= $opt{"rate"}
| |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | my $latency = int(($opt{latency} // 0.1) * $rate); # in frames | ||||||||||||||||||||||||||
|
Condition0%0/2
$opt{"latency"} // "0.1"
MC/DC0%0/1
$opt{"latency"} // "0.1"
$opt{"latency"}
Truth table0%0/2
A
A= $opt{"latency"}
| |||||||||||||||||||||||||||||
| 98 | 0 | my $self = bless { | |||||||||||||||||||||||||||
| 99 | rate => $rate, | ||||||||||||||||||||||||||||
| 100 | latency => $latency, | ||||||||||||||||||||||||||||
| 101 | fifo => '', # sound not yet in the stream's buffers | ||||||||||||||||||||||||||||
| 102 | want => $latency * FRAME, # bytes a buffer takes | ||||||||||||||||||||||||||||
| 103 | written => 0, | ||||||||||||||||||||||||||||
| 104 | mem => [], # memory given to C, freed with the stream | ||||||||||||||||||||||||||||
| 105 | }, $class; | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | $self->{loop} = _call('pw_thread_loop_new', '(po)o', 'peta-synth', 0) or die "PipeWire: no thread loop\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | my $title = ($opt{title} // 'Peta::Synth') =~ tr/"\\//dr; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opt{"title"} // "Peta::Synth"
MC/DC0%0/1
$opt{"title"} // "Peta::Synth"
$opt{"title"}
Truth table0%0/2
A
A= $opt{"title"}
| |||||||||||||||||||||||||||||
| 109 | 0 | my $props = _call('pw_properties_new_string', '(p)o', | |||||||||||||||||||||||||||
| 110 | qq{media.type=Audio media.category=Playback media.role=Music node.latency=$latency/$rate } | ||||||||||||||||||||||||||||
| 111 | . qq{application.name="Peta::Synth" media.name="$title" node.name="peta-synth"}); | ||||||||||||||||||||||||||||
| 112 | 0 | my $events = $self->_alloc(pack 'L< x92', 2); # version 2, no callbacks | |||||||||||||||||||||||||||
| 113 | 0 | _call('pw_thread_loop_lock', '(o)v', $self->{loop}); | |||||||||||||||||||||||||||
| 114 | 0 | _call('pw_thread_loop_start', '(o)i', $self->{loop}); | |||||||||||||||||||||||||||
| 115 | $self->{stream} = _call('pw_stream_new_simple', '(opooo)o', | ||||||||||||||||||||||||||||
| 116 | 0 | _call('pw_thread_loop_get_loop', '(o)o', $self->{loop}), 'Peta::Synth', $props, $events, 0); | |||||||||||||||||||||||||||
| 117 | 0 | my $format = $self->_alloc(_format($rate)); | |||||||||||||||||||||||||||
| 118 | 0 | my $params = $self->_alloc(Peta::FFI::pack_ptr($format)); | |||||||||||||||||||||||||||
| 119 | my $connected = $self->{stream} | ||||||||||||||||||||||||||||
| 120 | 0 | ▶ | && _call('pw_stream_connect', '(oiiioi)i', $self->{stream}, DIRECTION_OUTPUT, ID_ANY, AUTOCONNECT | MAP_BUFFERS, $params, 1); | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{'stream'} && _call('pw_stream_connect', '(oiiioi)i', $self->{'stream'}, 1, -1, 5, $params, 1)
MC/DC0%0/2
$self->{'stream'} && _call('pw_stream_connect', '(oiiioi)i', $self->{'stream'}, 1, -1, 5, $params, 1)
$self->{'stream'}
_call('pw_stream_connect', '(oiiioi)i', $self->{'stream'}, 1, -1, 5, $params, 1)
Truth table0%0/3
A && B
A= $self->{'stream'}B= _call('pw_stream_connect', '(oiiioi)i', $self->{'stream'}, 1, -1, 5, $params, 1)
| |||||||||||||||||||||||||||||
| 121 | 0 | _call('pw_thread_loop_unlock', '(o)v', $self->{loop}); | |||||||||||||||||||||||||||
| 122 | 0 | ▶ | if (!$self->{stream} || $connected < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $self->{"stream"} or $connected < 0
MC/DC0%0/2
not $self->{"stream"} or $connected < 0
not $self->{"stream"}
$connected < 0
Truth table0%0/3
not(A) or B
A= not $self->{"stream"}B= $connected < 0
| |||||||||||||||||||||||||||||
| 123 | 0 | $self->_destroy; | |||||||||||||||||||||||||||
| 124 | 0 | die "PipeWire refused the stream\n"; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # Until it is ready to take sound, or says why not. | ||||||||||||||||||||||||||||
| 128 | 0 | my $until = Time::HiRes::time() + 5; | |||||||||||||||||||||||||||
| 129 | 0 | while (1) { | |||||||||||||||||||||||||||
| 130 | 0 | my ($state, $error) = $self->_state; | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | last if $state == STATE_STREAMING || $state == STATE_PAUSED; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$state == 3 or $state == 2
MC/DC0%0/2
$state == 3 or $state == 2
$state == 3
$state == 2
Truth table0%0/3
A or B
A= $state == 3B= $state == 2
| |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if ($state == STATE_ERROR || Time::HiRes::time() > $until) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$state == -1 or &Time::HiRes::time() > $until
MC/DC0%0/2
$state == -1 or &Time::HiRes::time() > $until
$state == -1
&Time::HiRes::time() > $until
Truth table0%0/3
A or B
A= $state == -1B= &Time::HiRes::time() > $until
| |||||||||||||||||||||||||||||
| 133 | 0 | $self->_destroy; | |||||||||||||||||||||||||||
| 134 | 0 | ▶ | die 'PipeWire: ' . ($error // 'the stream did not get ready') . "\n"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$error // "the stream did not get ready"
MC/DC0%0/1
$error // "the stream did not get ready"
$error
Truth table0%0/2
A
A= $error
| |||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 0 | Time::HiRes::sleep(0.01); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 0 | return $self; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | sub rate ($self) { return $self->{rate} } | ||||||||||||||||||||||||||
|
Subroutine
rate: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # Seconds of sound handed to the stream. | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | sub sent ($self) { return $self->{written} / FRAME / $self->{rate} } | ||||||||||||||||||||||||||
|
Subroutine
sent: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # Sound: queued as the stream takes it; waits while more is waiting than | ||||||||||||||||||||||||||||
| 147 | # it takes. | ||||||||||||||||||||||||||||
| 148 | 0 | ▶ | sub write ($self, $left, $right) { | ||||||||||||||||||||||||||
|
Subroutine
write: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | 0 | $self->{fifo} .= pack 'f<*', @{ Peta::Synth::Output::interleave($left, $right) }; | |||||||||||||||||||||||||||
| 150 | 0 | $self->_pump(0); | |||||||||||||||||||||||||||
| 151 | 0 | ▶ | while (length $self->{fifo} > $self->{want}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 0 | Time::HiRes::sleep(POLL); | |||||||||||||||||||||||||||
| 153 | 0 | $self->_pump(0); | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | 0 | return $self; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # The rest played to its end, then the stream let go. | ||||||||||||||||||||||||||||
| 159 | 0 | ▶ | sub close ($self) { | ||||||||||||||||||||||||||
|
Subroutine
close: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 160 | 0 | ▶ | return if !$self->{stream}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | while (length $self->{fifo}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | $self->_pump(1); | |||||||||||||||||||||||||||
| 163 | 0 | ▶ | Time::HiRes::sleep(POLL) if length $self->{fifo}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 0 | ▶ | my $time = $self->{time} //= $self->_alloc("\0" x 64); | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"time"} //= $self->_alloc("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")
MC/DC0%0/2
$self->{"time"} //= $self->_alloc("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")
$self->{"time"}
$self->_alloc("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")
Truth table0%0/3
A //= B
A= $self->{"time"}B= $self->_alloc("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")
| |||||||||||||||||||||||||||||
| 166 | 0 | my $until = Time::HiRes::time() + 10; | |||||||||||||||||||||||||||
| 167 | 0 | ▶ | while (Time::HiRes::time() < $until) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 0 | _call('pw_thread_loop_lock', '(o)v', $self->{loop}); | |||||||||||||||||||||||||||
| 169 | 0 | _call('pw_stream_get_time_n', '(ooL)i', $self->{stream}, $time, 64); | |||||||||||||||||||||||||||
| 170 | 0 | _call('pw_thread_loop_unlock', '(o)v', $self->{loop}); | |||||||||||||||||||||||||||
| 171 | 0 | my ($delay, $queued) = unpack 'q< Q<', Peta::FFI::peek($time + 24, 16); | |||||||||||||||||||||||||||
| 172 | 0 | ▶ | if (!$queued) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | # All is queued out; what is in the device still plays, $delay frames. | ||||||||||||||||||||||||||||
| 174 | 0 | ▶ | Time::HiRes::sleep($delay > 0 && $delay < $self->{rate} ? $delay / $self->{rate} : 0.05); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$delay > 0 and $delay < $self->{"rate"}
MC/DC0%0/2
$delay > 0 and $delay < $self->{"rate"}
$delay > 0
$delay < $self->{"rate"}
Truth table0%0/3
A and B
A= $delay > 0B= $delay < $self->{"rate"}
| |||||||||||||||||||||||||||||
| 175 | 0 | last; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | 0 | Time::HiRes::sleep(0.01); | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | 0 | $self->_destroy; | |||||||||||||||||||||||||||
| 180 | 0 | return; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | # Stops at once. | ||||||||||||||||||||||||||||
| 184 | 0 | ▶ | sub stop ($self) { $self->_destroy; return } | ||||||||||||||||||||||||||
|
Subroutine
stop: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 0 | ▶ | sub DESTROY ($self) { $self->_destroy if $self->{loop} && ${^GLOBAL_PHASE} ne 'DESTRUCT' } | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=3 SCAR=24.8
Branch: 1 branch
Condition0%0/3
$self->{"loop"} and ${^GLOBAL_PHASE} ne "DESTRUCT"
MC/DC0%0/2
$self->{"loop"} and ${^GLOBAL_PHASE} ne "DESTRUCT"
$self->{"loop"}
${^GLOBAL_PHASE} ne "DESTRUCT"
Truth table0%0/3
A and B
A= $self->{"loop"}B= ${^GLOBAL_PHASE} ne "DESTRUCT"
| |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | # Fills the empty buffers the stream has, as far as the sound waiting goes | ||||||||||||||||||||||||||||
| 189 | # (all of it, short buffers too, when $final), and while the stream holds | ||||||||||||||||||||||||||||
| 190 | # less than twice the latency: a buffer takes what PipeWire asks for, or | ||||||||||||||||||||||||||||
| 191 | # the latency's worth. | ||||||||||||||||||||||||||||
| 192 | 0 | ▶ | sub _pump ($self, $final) { | ||||||||||||||||||||||||||
|
Subroutine
_pump: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||
| 193 | 0 | ▶ | my $time = $self->{time} //= $self->_alloc("\0" x 64); | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"time"} //= $self->_alloc("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")
MC/DC0%0/2
$self->{"time"} //= $self->_alloc("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")
$self->{"time"}
$self->_alloc("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")
Truth table0%0/3
A //= B
A= $self->{"time"}B= $self->_alloc("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")
| |||||||||||||||||||||||||||||
| 194 | 0 | _call('pw_thread_loop_lock', '(o)v', $self->{loop}); | |||||||||||||||||||||||||||
| 195 | 0 | ▶ | while (length($self->{fifo}) >= ($final ? 1 : $self->{want})) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 196 | 0 | _call('pw_stream_get_time_n', '(ooL)i', $self->{stream}, $time, 64); | |||||||||||||||||||||||||||
| 197 | 0 | my ($queued) = unpack 'Q<', Peta::FFI::peek($time + 32, 8); | |||||||||||||||||||||||||||
| 198 | 0 | ▶ | last if $queued >= 2 * $self->{latency}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 0 | ▶ | my $b = _call('pw_stream_dequeue_buffer', '(o)o', $self->{stream}) or last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 0 | my $buffer = Peta::FFI::unpack_ptr(Peta::FFI::peek($b, 8)); | |||||||||||||||||||||||||||
| 201 | 0 | my $datas = Peta::FFI::unpack_ptr(Peta::FFI::peek($buffer + 16, 8)); | |||||||||||||||||||||||||||
| 202 | 0 | my ($maxsize) = unpack 'L<', Peta::FFI::peek($datas + 20, 4); | |||||||||||||||||||||||||||
| 203 | 0 | my $data = Peta::FFI::unpack_ptr(Peta::FFI::peek($datas + 24, 8)); | |||||||||||||||||||||||||||
| 204 | 0 | my $chunk = Peta::FFI::unpack_ptr(Peta::FFI::peek($datas + 32, 8)); | |||||||||||||||||||||||||||
| 205 | 0 | my ($requested) = unpack 'Q<', Peta::FFI::peek($b + 24, 8); | |||||||||||||||||||||||||||
| 206 | 0 | ▶ | my $bytes = ($requested || $self->{latency}) * FRAME; | ||||||||||||||||||||||||||
|
Condition0%0/3
$requested || $self->{'latency'}
MC/DC0%0/2
$requested || $self->{'latency'}
$requested
$self->{'latency'}
Truth table0%0/3
A || B
A= $requestedB= $self->{'latency'}
| |||||||||||||||||||||||||||||
| 207 | 0 | ▶ | $bytes = $maxsize - $maxsize % FRAME if $bytes > $maxsize; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | 0 | ▶ | $self->{want} = $bytes if $bytes > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 0 | my $part = substr $self->{fifo}, 0, $bytes, ''; | |||||||||||||||||||||||||||
| 210 | 0 | ▶ | Peta::FFI::poke($data, $part) if length $part; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 0 | Peta::FFI::poke($chunk, pack 'L< L< l< l<', 0, length $part, FRAME, 0); | |||||||||||||||||||||||||||
| 212 | 0 | Peta::FFI::poke($b + 16, pack 'Q<', length($part) / FRAME); | |||||||||||||||||||||||||||
| 213 | 0 | _call('pw_stream_queue_buffer', '(oo)i', $self->{stream}, $b); | |||||||||||||||||||||||||||
| 214 | 0 | $self->{written} += length $part; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | 0 | _call('pw_thread_loop_unlock', '(o)v', $self->{loop}); | |||||||||||||||||||||||||||
| 217 | 0 | return; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 0 | ▶ | sub _state ($self) { | ||||||||||||||||||||||||||
|
Subroutine
_state: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 221 | 0 | my $out = $self->_alloc("\0" x 8); | |||||||||||||||||||||||||||
| 222 | 0 | _call('pw_thread_loop_lock', '(o)v', $self->{loop}); | |||||||||||||||||||||||||||
| 223 | 0 | my $state = _call('pw_stream_get_state', '(oo)i', $self->{stream}, $out); | |||||||||||||||||||||||||||
| 224 | 0 | _call('pw_thread_loop_unlock', '(o)v', $self->{loop}); | |||||||||||||||||||||||||||
| 225 | 0 | my $error = Peta::FFI::unpack_ptr(Peta::FFI::peek($out, 8)); | |||||||||||||||||||||||||||
| 226 | 0 | ▶ | return ($state, $error ? Peta::FFI::peek_cstr($error) : undef); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | # Memory for C, freed with the stream. | ||||||||||||||||||||||||||||
| 230 | 0 | ▶ | sub _alloc ($self, $bytes) { | ||||||||||||||||||||||||||
|
Subroutine
_alloc: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 231 | 0 | my $p = Peta::FFI::alloc(length $bytes); | |||||||||||||||||||||||||||
| 232 | 0 | Peta::FFI::poke($p, $bytes); | |||||||||||||||||||||||||||
| 233 | 0 | push @{ $self->{mem} }, $p; | |||||||||||||||||||||||||||
| 234 | 0 | return $p; | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 0 | ▶ | sub _destroy ($self) { | ||||||||||||||||||||||||||
|
Subroutine
_destroy: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 238 | 0 | ▶ | return if !$self->{loop}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | 0 | ▶ | if ($self->{stream}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | 0 | _call('pw_thread_loop_lock', '(o)v', $self->{loop}); | |||||||||||||||||||||||||||
| 241 | 0 | _call('pw_stream_disconnect', '(o)i', $self->{stream}); | |||||||||||||||||||||||||||
| 242 | 0 | _call('pw_stream_destroy', '(o)v', $self->{stream}); | |||||||||||||||||||||||||||
| 243 | 0 | _call('pw_thread_loop_unlock', '(o)v', $self->{loop}); | |||||||||||||||||||||||||||
| 244 | 0 | delete $self->{stream}; | |||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | 0 | _call('pw_thread_loop_stop', '(o)v', $self->{loop}); | |||||||||||||||||||||||||||
| 247 | 0 | _call('pw_thread_loop_destroy', '(o)v', $self->{loop}); | |||||||||||||||||||||||||||
| 248 | 0 | delete $self->{loop}; | |||||||||||||||||||||||||||
| 249 | 0 | Peta::FFI::free($_) for @{ $self->{mem} }; | |||||||||||||||||||||||||||
| 250 | 0 | $self->{mem} = []; | |||||||||||||||||||||||||||
| 251 | 0 | return; | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | # An SPA POD: size, type, the body padded to a multiple of 8 bytes, | ||||||||||||||||||||||||||||
| 255 | # little-endian. | ||||||||||||||||||||||||||||
| 256 | 7 | ▶ | sub _pod ($type, $body) { | ||||||||||||||||||||||||||
|
Subroutine
_pod: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 257 | 7 | my $pad = (8 - length($body) % 8) % 8; | |||||||||||||||||||||||||||
| 258 | 7 | return pack('L< L<', length $body, $type) . $body . ("\0" x $pad); | |||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | # A property of an object POD: its key, no flags, its value. | ||||||||||||||||||||||||||||
| 262 | 6 | ▶ | sub _prop ($key, $pod) { return pack('L< L<', $key, 0) . $pod } | ||||||||||||||||||||||||||
|
Subroutine
_prop: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | # The stream's format: an Object of type Format, id EnumFormat. | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 1 | ▶ | sub _format ($rate) { | ||||||||||||||||||||||||||
|
Subroutine
_format: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 267 | 1 | ▶ | my $id = sub ($v) { _pod(TYPE_ID, pack 'L<', $v) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 268 | 1 | ▶ | my $int = sub ($v) { _pod(TYPE_INT, pack 'l<', $v) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 269 | 1 | my $positions = _pod(TYPE_ARRAY, pack('L< L<', 4, TYPE_ID) . pack('L< L<', CHANNEL_FL, CHANNEL_FR)); | |||||||||||||||||||||||||||
| 270 | 1 | return _pod(TYPE_OBJECT, pack('L< L<', OBJECT_FORMAT, PARAM_ENUMFORMAT) | |||||||||||||||||||||||||||
| 271 | . _prop(FORMAT_MEDIATYPE, $id->(MEDIA_AUDIO)) | ||||||||||||||||||||||||||||
| 272 | . _prop(FORMAT_MEDIASUBTYPE, $id->(SUBTYPE_RAW)) | ||||||||||||||||||||||||||||
| 273 | . _prop(FORMAT_AUDIO_FORMAT, $id->(AUDIO_F32LE)) | ||||||||||||||||||||||||||||
| 274 | . _prop(FORMAT_AUDIO_RATE, $int->($rate)) | ||||||||||||||||||||||||||||
| 275 | . _prop(FORMAT_AUDIO_CHANNELS, $int->(2)) | ||||||||||||||||||||||||||||
| 276 | . _prop(FORMAT_AUDIO_POSITION, $positions)); | ||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | 1 | 1; | |||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | __END__ | ||||||||||||||||||||||||||||