YAACC - UPNP Client and Server
Loading...
Searching...
No Matches
de.yaacc.snapcast.feed.PcmFeeder Class Reference

Feeds raw PCM into the tcp source of a Snapcast server in real time. More...

Inheritance diagram for de.yaacc.snapcast.feed.PcmFeeder:
Inheritance graph
Collaboration diagram for de.yaacc.snapcast.feed.PcmFeeder:
Collaboration graph

Classes

class  Config
 The settings; the defaults are the ones of the design.
 
enum  Failure
 Why a feeder failed. More...
 
interface  Listener
 Told once when the feeder fails (not when it is closed). More...
 
enum  State
 The state of a feeder. More...
 

Public Member Functions

boolean write (byte[] data, int offset, int length)
 Copies PCM into the queue.
 
void discard ()
 Drops what is queued (a seek or a stop); the writer keeps its clock and writes silence if nothing follows.
 
void close ()
 Stops the threads, closes the socket and drops the queue.
 
State getState ()
 
Failure getFailure ()
 Why the feeder failed; null unless the state is FAILED (or CLOSED after a failure).
 
int getChunkBytes ()
 The size of a chunk in bytes.
 
int getQueuedBytes ()
 Bytes in the queue now.
 
int getQueueCapacity ()
 The most bytes the queue can hold.
 
long getChunksWritten ()
 Chunks handed to the connection (including silence).
 
long getBytesWritten ()
 Bytes handed to the connection (including silence).
 
long getSilenceChunks ()
 Chunks of silence written because the producer had nothing.
 
long getDroppedBytes ()
 Bytes that write had to drop.
 
long getLeadNanos ()
 How far the written audio is ahead of real time now (negative: behind).
 
long getMaxLeadNanos ()
 The largest lead seen right after a write.
 
long getLateChunks ()
 Chunks that were written more than one chunk after they were due.
 
long getReanchors ()
 How often the pacer re-anchored after a stall.
 
- Public Member Functions inherited from de.yaacc.multiroom.feed.PcmSink

Static Public Member Functions

static PcmFeeder connect (InetAddress address, int port, PcmFormat format, Config config, FeedConnection.Factory factory, FeedClock clock, Listener listener) throws IOException
 Connects and starts the threads.
 

Static Public Attributes

static final int MAX_QUEUE_CHUNKS = 8
 The most chunks the queue between the producer and the writer thread can hold.
 

Detailed Description

Feeds raw PCM into the tcp source of a Snapcast server in real time.

Where it connects. connect takes an InetAddress and a port and connects to exactly that: the address of the control connection's peer (SnapcastRpc.getPeerAddress()) and the port that YAACC itself asked for in Stream.AddStream. A host, port or URI that a server reply contains is never a connect target (the server reports 0.0.0.0:<port>; connecting to that would reach the phone itself, so the wildcard and multicast addresses are refused). The address is not logged here and not put into an exception.

How it writes. The producer thread calls write, which copies the bytes into a queue of at most {@value MAX_QUEUE_CHUNKS} chunks. A writer thread of its own takes one chunk at a time and writes it at the deadline given by a RealTimePacer (absolute deadlines, a look-ahead of 3 chunks, a re-anchor instead of a burst after a stall). The socket gets a small send buffer and TCP_NODELAY, so what is in flight stays a few chunks and a pause or stop is heard soon. The server reads one chunk per chunk_ms and resyncs when a read is late, so the writer never underruns: when the queue is empty at the time a chunk is due it writes silence (an all-zero chunk, which the server counts as silent), for at most silenceFillChunks chunks; after that it waits without writing until audio arrives and anchors the clock again. A chunk that is only partly queued is waited for (it is padded with zeros only when it is due and its first bytes have been waiting for half a chunk), and the writer wakes when audio arrives, so zeros never land in the middle of audio that is being handed over.

Back pressure. write waits up to writeWaitMillis for room in the queue and then reports failure (and counts the dropped bytes); the queue never grows.

A stalled peer. Java sockets have no write timeout. A watchdog thread closes the connection when one write has not finished after stallTimeoutMillis (default 10 s); the feeder is then FAILED with Failure#STALLED and the listener is told once.

close() stops both threads and closes the socket. Everything is safe to call from any thread.

Member Function Documentation

◆ close()

void de.yaacc.snapcast.feed.PcmFeeder.close ( )

Stops the threads, closes the socket and drops the queue.

Safe to call twice and from the listener.

Referenced by de.yaacc.snapcast.feed.PcmFeederOutput.close().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ connect()

static PcmFeeder de.yaacc.snapcast.feed.PcmFeeder.connect ( InetAddress address,
int port,
PcmFormat format,
Config config,
FeedConnection.Factory factory,
FeedClock clock,
Listener listener ) throws IOException
static

Connects and starts the threads.

Blocking (up to the connect timeout): never call it on the main thread.

Parameters
addressthe peer address of the control connection, nothing else
portthe port that YAACC asked for in Stream.AddStream
formatthe sample format of the stream
configthe settings, or null for the defaults
factoryopens the connection, see SocketFeedConnection#FACTORY
clockthe time source, FeedClock#SYSTEM in the app
listenertold once if the feeder fails; may be null
Exceptions
IllegalArgumentExceptionfor no address, the wildcard or a multicast address, or a port outside 1 to 65535
IOExceptionwhen the connection can not be made (a fixed text: no address, no cause)
Here is the call graph for this function:

◆ discard()

void de.yaacc.snapcast.feed.PcmFeeder.discard ( )

Drops what is queued (a seek or a stop); the writer keeps its clock and writes silence if nothing follows.

Implements de.yaacc.multiroom.feed.PcmSink.

Referenced by de.yaacc.snapcast.feed.PcmFeeder.close(), and de.yaacc.snapcast.feed.PcmFeederOutput.discard().

Here is the caller graph for this function:

◆ write()

boolean de.yaacc.snapcast.feed.PcmFeeder.write ( byte[] data,
int offset,
int length )

Copies PCM into the queue.

Waits up to writeWaitMillis for room; what does not fit by then is dropped and counted.

Returns
true if everything was queued; false if the feeder is not running or bytes were dropped
Exceptions
IllegalArgumentExceptionfor a range outside the array or a length that is not whole frames (an odd number of bytes would shift the channels for the rest of the stream)

Implements de.yaacc.multiroom.feed.PcmSink.

Referenced by de.yaacc.snapcast.feed.PcmFeederOutput.write().

Here is the caller graph for this function:

The documentation for this class was generated from the following file: