BASS.NET API for the Un4seen BASS Audio Library

StreamingServer Class

BASS.NET API for the Un4seen BASS Audio Library
Base class for all streaming server classes (e.g. SHOUTcast or ICEcast).
Inheritance Hierarchy

SystemObject
  Un4seen.Bass.MiscStreamingServer
    Un4seen.Bass.MiscICEcast
    Un4seen.Bass.MiscSHOUTcast
    Un4seen.Bass.MiscWMAcast

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

[SerializableAttribute]
public abstract class StreamingServer : IStreamingServer

The StreamingServer type exposes the following members.

Constructors

  NameDescription
Public methodStreamingServer(IBaseEncoder)
Default constructor, creating an instance for a streaming server implementation (using BASSenc as the default processor).
Public methodStreamingServer(IBaseEncoder, Boolean)
Default constructor, creating an instance for a streaming server implementation.
Top
Properties

  NameDescription
Public propertyEncoder
Returns the encoder interface which is used with this instance.
Public propertyForceUTF8TitleUpdates
Gets or Sets if title updates (via UpdateTitle(String, String)) should in any case use UTF-8 (default is ).
Public propertyIsConnected
Returns , if connected to a boadcast server - else .
Public propertyLastError
Gets or Sets the last streaming error.
Public propertyLastErrorMessage
Gets or Sets the last streaming error message.
Public propertyLocalNetworkCardIPAddress
Gets or Sets a local IP address to use for cast server connections (=any, default):
Public propertySongTitle
The current/last song title.
Public propertySongUrl
The current/last song url.
Public propertyUseBASS
Gets if native BASSenc should be used to connect and stream to the server (as specified in the constructor).
Top
Methods

  NameDescription
Public methodConnect
Connects to a streaming server.
Public methodDisconnect
Disconnects from a streaming server.
Public methodDispose
Implement IDisposable.
Protected methodFinalize
Finalization code.
(Overrides ObjectFinalize.)
Protected methodStatic memberGetIPfromHost
Resolved a given host name (DNS or IP) to its possible IP addesses.
Public methodGetListeners
Returns the number of listeners currently connected.
Public methodGetStats
Returns the XML stats of the server.
Public methodLogin
Performs a login to and/or initializes the streaming server.
Public methodSendData
Sends encoded sample data (manually) to the streaming server (e.g. the data as received in an own ENCODEPROC).
Public methodUpdateTitle(String, String)
Updates the song title of the streaming server.
Public methodUpdateTitle(TAG_INFO, String)
Updates the song title of the streaming server.
Top
Remarks

This base class is not intended for direct use, but defines all abstract properties and methods which needs to be implemented by an actual streaming server class. A derived class must implement: IsConnected, Connect, Disconnect, Login, SendData(IntPtr, Int32) and UpdateTitle(String, String).

The properties Encoder, LastError and LastErrorMessage have been already implemented.

You might use this base class to derive your own streaming server implementations.

If an instance of a derived class is being disposed, an already created connection to a streaming server will automatically be closed (using Disconnect) and an Encoder will be stopped, if active.

Examples

This example shows a basic implementation of an own streaming server class:
public class MyOwnServer : StreamingServer
{
    // Constructor
    public MyOwnServer(IBaseEncoder encoder, bool useBASS) : base(encoder, useBASS)
    {
      // validity check
      if (encoder.EncoderType != BASSChannelType.BASS_CTYPE_STREAM_MP3 &&
          encoder.EncoderType != BASSChannelType.BASS_CTYPE_STREAM_AAC)
        throw new Exception( "Invalid EncoderType (only MP3 and AAC is supported)!" );
    }

    private Socket _socket = null;
    private bool _loggedIn = false;
    private byte[] _data = null;
    private object _lock = false;
    public string ServerAddress = "localhost";
    public int ServerPort = 8000;

    public override bool IsConnected
    {
      get
      {
        if (_socket != null)
          return _socket.Connected && _loggedIn;
        else
          return false;
      }
    }

    public override bool Connect()
    {
      // check the encoder
      if (!Encoder.IsActive)
      {
        LastError = STREAMINGERROR.Error_EncoderError;
        LastErrorMessage = "Encoder not active!";
        return false;
      }
      // close any connections, if still open
      if (_socket != null && _socket.Connected)
      {
        _socket.Close();
        _socket = null;
      }
      // create a connection at port+1
      _socket = CreateSocket( ServerAddress, ServerPort+1 );
      return (_socket != null && _socket.Connected);
    }

    public override bool Disconnect()
    {
      bool ok = false;
      try
      {
        _socket.Close();
      }
      catch { }
      finally
      {
        if (_socket != null && _socket.Connected) 
        {
          LastError = STREAMINGERROR.Error_Disconnect;
          LastErrorMessage = "Winsock error: " + 
            Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
        }
        else
        {
          ok = true;
          _socket = null;
          _loggedIn = false;
        }
      }
      return ok;
    }

    public override bool Login()
    {
      if (_socket == null)
      {
        LastError = STREAMINGERROR.Error_NotConnected;
        LastErrorMessage = "Not connected to server.";
        return false;
      }
      bool ok = false;
      if ( MyLogin() )
      {
        if ( MyInit() )
        {
          ok = true;
          _loggedIn = true;
          LastError = STREAMINGERROR.Ok;
          LastErrorMessage = String.Empty;
        }
        else
        {
          LastError = STREAMINGERROR.Error_Login;
          LastErrorMessage = "Server could not be initialized.";
        }
      }
      else
      {
        LastError = STREAMINGERROR.Error_Login;
        LastErrorMessage = "Invalid username or password.";
      }
      return ok;
    }

    public override int SendData(IntPtr buffer, int length)
    {
      if (buffer == IntPtr.Zero || length == 0)
        return 0;
      int sendData = -1;
      try
      {
        lock (_lock)
        {
          // dynamic buffer allocation
          if (_data == null || _data.Length < length)
            _data = new byte[length];
          Marshal.Copy(buffer, _data, 0, length);
          sendData = _socket.Send( _data, 0, length, SocketFlags.None );
          if (sendData < 0)
          {
            LastError = STREAMINGERROR.Error_SendingData;
            LastErrorMessage = String.Format( "{0} bytes not send.", length);
            Disconnect();
          }
          else if (sendData != length)
          {
            LastError = STREAMINGERROR.Warning_LessDataSend;
            LastErrorMessage = String.Format( "{0} of {1} bytes send.", sendData, length);
          }
        }
      }
      catch (Exception e)
      { 
        LastError = STREAMINGERROR.Error_SendingData;
        LastErrorMessage = e.Message;
        sendData = -1;
        Disconnect();
      }
      return sendData;
    }

    public override bool UpdateTitle(string song, string url)
    {
      SongTitle = song;
      return MyUpdateTitle(song);
    }

    private Socket CreateSocket(string serveraddress, int port)
    {
      Socket socket = null;
      IPHostEntry hostEntry = null;
      try
      {
        // Get host related information
        hostEntry = Dns.GetHostEntry(serveraddress);
        // Loop through the AddressList to obtain the supported AddressFamily.
        foreach(IPAddress address in hostEntry.AddressList)
        {
          try
          {
            IPEndPoint ipe = new IPEndPoint(address, port);
            Socket tempSocket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            tempSocket.Connect(ipe);
            if (tempSocket.Connected)
            {
              socket = tempSocket;
              break;
            }
            else
            {
              continue;
            }
          }
          catch (Exception e)
          { 
            LastError = STREAMINGERROR.Error_CreatingConnection;
            LastErrorMessage = e.Message;
            socket = null; 
          }
        }
      }
      catch (Exception e)
      { 
        LastError = STREAMINGERROR.Error_ResolvingServerAddress;
        LastErrorMessage = e.Message;
        socket = null;
      }
      return socket;
    }

    private bool MyLogin() 
    {
      if (_socket == null)
        return false;
      // e.g. send password here using the "_socket"
      ...
    }

    private bool MyInit() 
    {
      if (_socket == null)
        return false;
      // e.g. send some init data to the server here using the "_socket"
      ...
    }

    private bool MyUpdateTitle(string song)
    {
      bool ok = false;
      Socket socket = null;
      try
      {
        socket = CreateSocket( ServerAddress, ServerPort );
        if (socket != null)
        {
          // send the song title using the created "socket"
          ...
          ok = true;
        }
      }
      catch { }
      finally
      {
        // disconnect
        if (socket != null)
        {
          socket.Close();
          socket = null;
        }
      }
      return ok;
    }
}
See Also

Reference