BASS_Encode_ServerInit


Initializes a server to send an encoder's output to connecting clients.

DWORD BASS_Encode_ServerInit(
    HENCODE handle,
    char *port,
    DWORD buffer,
    DWORD burst,
    DWORD flags,
    ENCODECLIENTPROC *proc,
    void *user
);

Parameters

handleThe encoder handle.
portThe IP address and port number to accept client connections on... "xxx.xxx.xxx.xxx:port", NULL = an available port on all local addresses. The IP address should be local and the port number should be lower than 65536. If the address is "0.0.0.0" or omitted, then the server will accept connections on all local addresses. If the port is "0" or omitted, then an available port will be assigned.
bufferThe server's buffer length in bytes.
burstThe amount of buffered data to send to new clients. This will be capped at the size of the buffer.
flagsA combination of these flags.
BASS_ENCODE_SERVER_METASend Shoutcast metadata to clients that request it. The default metadata interval is 16KB (ie. the metadata is refreshed every 16KB), but a different interval (in KB) can be specified in the HIWORD - use MAKEWORD(flags,interval). The Shoutcast metadata can be set with BASS_Encode_CastSetTitle.
BASS_ENCODE_SERVER_NOHTTPDo not read or send HTTP headers.
procCallback function to receive notification of clients connecting and disconnecting... NULL = no callback.
userUser instance data to pass to the callback function.

Return value

If successful, the new server's port number is returned, else 0 is returned. Use BASS_ErrorGetCode to get the error code.

Error codes

BASS_ERROR_HANDLEhandle is not valid.
BASS_ERROR_ALREADYThere is already a server set on the encoder.
BASS_ERROR_ILLPARAMport or flags is not valid. Shoutcast metadata requires HTTP headers.
BASS_ERROR_BUSYThe port is in use.
BASS_ERROR_MEMThere is insufficient memory.
BASS_ERROR_UNKNOWNSome other mystery problem!

Remarks

This function allows remote (or local) clients to receive the encoder's output by setting up a TCP server for them to connect to, using BASS_StreamCreateURL for example. Connections can be refused by the ENCODECLIENTPROC callback function, and already connected clients can be kicked with the BASS_Encode_ServerKick function.

The server buffers the data that it receives from the encoder, and the data is then sent from the buffer to the connected clients. The buffer should be at least big enough to account for the time that it takes for the clients to receive the data. If a client falls too far behind (beyond the buffer length), it will miss some data. When a client connects, buffered data can be "burst" to the client, allowing it to prebuffer and begin playback more quickly.

An encoder needs to be started, but with no data yet sent to it, before using this function to setup the server. If BASS_Encode_Start is used, the encoder should be setup to write its output to STDOUT. Due to the length restrictions of WAVE headers/files, the encoder should also be started with the BASS_ENCODE_NOHEAD flag, and the sample format details sent via the command-line.

Normally, BASSenc will produce the encoded data (with the help of an encoder) that is sent to a clients, but it is also possible to send already encoded data (without first decoding and re-encoding it) via the PCM encoding option. The encoder can be set on any BASS channel, as rather than feeding on sample data from the channel, BASS_Encode_Write would be used to feed in the already encoded data. BASSenc does not know what the data's bitrate is in that case, so it is up to the user to process the data at the correct rate (real-time speed).

The server supports Shoutcast metadata with any audio format, but Shoutcast traditionally only supports MP3 or AAC encoding, so some players might not read the metadata with other formats. BASS (eg. BASS_StreamCreateURL) does support Shoutcast metadata with any audio format.

Platform-specific

This function is not available on Windows CE.

Example

Start encoding a stereo 44100hz channel to 128kb/s MP3, and start a server on port 8000 with a fully burstable 4 second (64KB) buffer.
HENCODE encoder=BASS_Encode_Start(channel, "lame -r -s 44100 -b 128 -", BASS_ENCODE_NOHEAD, NULL, 0); // setup the encoder
BASS_Encode_ServerInit(encoder, "8000", 64000, 64000, 0, NULL, NULL); // start the server

Start encoding a stereo 44100hz channel to 128kb/s MP3, and start a server on port 8000 with a fully burstable 4 second (64KB) buffer and Shoutcast metadata enabled with an 8KB interval.

HENCODE encoder=BASS_Encode_Start(channel, "lame -r -s 44100 -b 128 -", BASS_ENCODE_NOHEAD, NULL, 0); // setup the encoder
BASS_Encode_ServerInit(encoder, "8000", 64000, 64000, MAKELONG(BASS_ENCODE_SERVER_META, 8), NULL, NULL); // start the server

Start encoding a stereo 44100hz channel to 160kb/s OGG, and start a server on any available port on the loopback address (127.0.0.1) with a fully burstable 2 second (40KB) buffer.

HENCODE encoder=BASS_Encode_Start(channel, "oggenc -r -R 44100 -M 160 -m 160 -", BASS_ENCODE_NOHEAD, NULL, 0); // setup the encoder
DWORD port=BASS_Encode_ServerInit(encoder, "127.0.0.1", 40000, 40000, 0, NULL, NULL); // start the server

Setup PCM encoding on a dummy stream to feed pre-encoded data to a server on port 8000 with a 64KB buffer.

dummy=BASS_StreamCreate(44100, 1, BASS_STREAM_DECODE, STREAMPROC_DUMMY, NULL); // create a dummy stream to host the encoder
encoder=BASS_Encode_Start(dummy, NULL, BASS_ENCODE_PCM|BASS_ENCODE_NOHEAD, NULL, NULL); // setup the PCM encoder
DWORD port=BASS_Encode_ServerInit(encoder, "8000", 64000, 64000, 0, NULL, NULL); // start the server
...
BASS_Encode_Write(encoder, data, length); // feed encoded data to the encoder/server (repeat periodically)

See also

BASS_Encode_CastInit, BASS_Encode_ServerKick, BASS_Encode_SetNotify, BASS_Encode_Start, BASS_Encode_StartACM, BASS_Encode_StartCA, BASS_CONFIG_ENCODE_CAST_TIMEOUT