BASS.NET API for the Un4seen BASS Audio Library

BassEncBASS_Encode_ServerInit Method

BASS.NET API for the Un4seen BASS Audio Library
Initializes a server to send an encoder's output to connecting clients.

Namespace:  Un4seen.Bass.AddOn.Enc
Assembly:  Bass.Net (in Bass.Net.dll) Version: 2.4.17.5
Syntax

[DllImportAttribute("bassenc", CharSet = CharSet.Auto)]
public static int BASS_Encode_ServerInit(
	int handle,
	string port,
	int buffer,
	int burst,
	BASSEncodeServer flags,
	ENCODECLIENTPROC proc,
	IntPtr user
)

Parameters

handle
Type: SystemInt32
The encoder handle.
port
Type: SystemString
The IP address and port number to accept client connections on... "xxx.xxx.xxx.xxx:port", = 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.
buffer
Type: SystemInt32
The server's buffer length in bytes.
burst
Type: SystemInt32
The amount of buffered data to send to new clients. This will be capped at the size of the buffer.
flags
Type: Un4seen.Bass.AddOn.EncBASSEncodeServer
A combination of these flags:
BASS_ENCODE_SERVER_NOHTTPDo not read or send HTTP headers.
BASS_ENCODE_SERVER_SSLSupport SSL/TLS encryption but also allow unencrypted connections. This requires the OpenSSL library.
BASS_ENCODE_SERVER_SSLONLYRequire SSL/TLS encryption. This requires the OpenSSL library.
proc
Type: Un4seen.Bass.AddOn.EncENCODECLIENTPROC
Callback function to receive notification of clients connecting and disconnecting... = no callback.
user
Type: SystemIntPtr
User instance data to pass to the callback function.

Return Value

Type: Int32
If successful, the new server's port number is returned, else 0 is returned. Use BASS_ErrorGetCode to get the error code.
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(String, Int32, BASSFlag, DOWNLOADPROC, IntPtr) for example. Connections can be refused by the ENCODECLIENTPROC callback function, and already connected clients can be kicked with the BASS_Encode_ServerKick(Int32, String) 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. If a speed cap is set (in the buffer parameter), that will limit the burst speed.

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(Int32, String, BASSEncode, ENCODEPROC, IntPtr) 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(Int32, IntPtr, Int32) 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).

SSL/TLS encryption requires the OpenSSL library (libssl) or compatible. A filename for that can be specified via the BASS_CONFIG_LIBSSL config option. If that is unset or fails to load then BASSenc will check if an OpenSSL library has already been loaded into the process with global scope and use that, and if that fails too then it will try some standard filenames for the library (see platform-specific notes below). A certificate and private key are also required, and can be set via the BASS_CONFIG_ENCODE_SERVER_CERT and BASS_CONFIG_ENCODE_SERVER_KEY config options, respectively. If there already is a server on the same port then its certificate and key will be used by the new server too, regardless of those config settings.

Chained OGG streams are supported, which can be used for metadata updates. The server also 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(String, Int32, BASSFlag, DOWNLOADPROC, IntPtr)) does support Shoutcast metadata with any audio format.

ERROR CODEDescription
BASS_ERROR_HANDLEhandle is not valid.
BASS_ERROR_ALREADYThere is already a server set on the encoder.
BASS_ERROR_ILLPARAMport is not valid.
BASS_ERROR_BUSYThe port is in use.
BASS_ERROR_SSLThe OpenSSL library could not be loaded or initialized.
BASS_ERROR_SERVER_CERTThe SSL certificate and/or key files are missing or invalid.
BASS_ERROR_MEMThere is insufficient memory.
BASS_ERROR_UNKNOWNSome other mystery problem!

Platform-specific

The default OpenSSL library filenames checked (in order of preference) on Linux are libssl.so, libssl.so.1.1, libssl.so.10, libssl.so.1.0.0. On Windows, it is bass_ssl.dll (BASS_SSL add-on), libssl-1_1.dll(or libssl-1_1-x64.dll if 64-bit), ssleay32.dll. On macOS, it is libssl.0.9.8.dylib. On Android, it is libbass_ssl.so(BASS_SSL add-on), libssl.so, libboringssl.so. The BASS_SSL add-on is available for Windows and Android from the BASS website. The BASS_CONFIG_LIBSSL config option is normally only available on Linux and Android but BASSenc adds support for it on other platforms too.

This function is not available on Windows CE.

Examples

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:
C#
// setup the encoder
int encoder = BassEnc.BASS_Encode_Start(channel, "lame -r -s 44100 -b 128 -", 
                                        BASSEncode.BASS_ENCODE_NOHEAD, null, IntPtr.Zero);
// start the server
BassEnc.BASS_Encode_ServerInit(encoder, "8000", 64000, 64000, 
                               BASSEncodeServer.BASS_ENCODE_SERVER_DEFAULT, null, IntPtr.Zero);
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:
C#
// setup the encoder
int encoder = BassEnc.BASS_Encode_Start(channel, "oggenc -r -R 44100 -M 160 -m 160 -", 
                                        BASSEncode.BASS_ENCODE_NOHEAD, null, IntPtr.Zero);
// start the server
BassEnc.BASS_Encode_ServerInit(encoder, "127.0.0.1", 40000, 40000, 
                               BASSEncodeServer.BASS_ENCODE_SERVER_DEFAULT, null, IntPtr.Zero);
Setup PCM encoding on a dummy stream to feed pre-encoded data to a server on port 8000 with a 64KB buffer:
C#
// create a dummy stream to host the encoder
int dummy = Bass.BASS_StreamCreateDummy(44100, 1, BASSFlag.BASS_STREAM_DECODE, IntPtr.Zero);
// setup the encoder
int encoder = BassEnc.BASS_Encode_Start(dummy, null, 
                                        BASSEncode.BASS_ENCODE_PCM | BASSEncode.BASS_ENCODE_NOHEAD, 
                                        null, IntPtr.Zero);
// start the server
int port = BassEnc.BASS_Encode_ServerInit(encoder, "8000", 64000, 64000, 
                                          BASSEncodeServer.BASS_ENCODE_SERVER_DEFAULT, null, IntPtr.Zero);
...
// feed encoded data to the encoder/server (repeat periodically)
BassEnc.BASS_Encode_Write(encoder, data, length);
See Also

Reference