using System; using System.Text; namespace AsterNET.FastAGI.Command { /// /// Plays the given file, allowing playback to be interrupted by the given /// digits, if any, and allows the listner to control the stream.
/// If offset is provided then the audio will seek to sample offset before play /// starts.
/// Returns 0 if playback completes without a digit being pressed, or the ASCII /// numerical value of the digit if one was pressed, or -1 on error or if the /// channel was disconnected.
/// Remember, the file extension must not be included in the filename.
/// Available since Asterisk 1.2 ///
public class ControlStreamFileCommand : AGICommand { /// The name of the file to stream. private string file; /// When one of these digits is pressed while streaming the command returns. private string escapeDigits; /// The offset samples to skip before streaming. private int offset; private string forwardDigit; private string rewindDigit; private string pauseDigit; #region File /// /// Get/Set the name of the file to stream. /// public string File { get { return file; } set { this.file = value; } } #endregion #region EscapeDigits /// /// Get/Set the digits that allow the user to interrupt this command or null for none. /// public string EscapeDigits { get { return escapeDigits; } set { this.escapeDigits = value; } } #endregion #region Offset /// /// Get/Set the offset samples to skip before streaming. /// public int Offset { get { return offset; } set { this.offset = value; } } #endregion #region ForwardDigit /// /// Get the digit for fast forward. /// public string ForwardDigit { get { return forwardDigit; } } #endregion #region RewindDigit /// /// Get the digit for rewind. /// public string RewindDigit { get { return rewindDigit; } } #endregion #region PauseDigit /// /// Get the digit for pause and unpause. /// public string PauseDigit { get { return pauseDigit; } } #endregion #region ControlStreamFileCommand(string file) /// /// Creates a new ControlStreamFileCommand, streaming from the beginning. It /// uses the default digit "#" for forward and "*" for rewind and does not /// support pausing. /// /// the name of the file to stream, must not include extension. public ControlStreamFileCommand(string file) { this.file = file; this.escapeDigits = null; this.offset = - 1; } #endregion #region ControlStreamFileCommand(string file, string escapeDigits) /// /// Creates a new ControlStreamFileCommand, streaming from the beginning. It /// uses the default digit "#" for forward and "*" for rewind and does not /// support pausing. /// /// the name of the file to stream, must not include extension. /// contains the digits that allow the user to interrupt this command. public ControlStreamFileCommand(string file, string escapeDigits) { this.file = file; this.escapeDigits = escapeDigits; this.offset = - 1; } #endregion #region ControlStreamFileCommand(string file, string escapeDigits, int offset) /// /// Creates a new ControlStreamFileCommand, streaming from the given offset. /// It uses the default digit "#" for forward and "*" for rewind and does not /// support pausing. /// /// the name of the file to stream, must not include extension. /// /// contains the digits that allow the user to interrupt this command. /// Maybe null if you don't want the user to interrupt. /// /// the offset samples to skip before streaming. public ControlStreamFileCommand(string file, string escapeDigits, int offset) { this.file = file; this.escapeDigits = escapeDigits; this.offset = offset; } #endregion #region ControlStreamFileCommand(string file, string escapeDigits, int offset, string forwardDigit, string rewindDigit, string pauseDigit) /// /// Creates a new ControlStreamFileCommand, streaming from the given offset. /// It uses the default digit "#" for forward and "*" for rewind and does not /// support pausing. /// /// the name of the file to stream, must not include extension. /// contains the digits that allow the user to interrupt this command. Maybe null if you don't want the user to interrupt. /// the offset samples to skip before streaming. /// the digit for fast forward. /// the digit for rewind. /// the digit for pause and unpause. public ControlStreamFileCommand(string file, string escapeDigits, int offset, string forwardDigit, string rewindDigit, string pauseDigit) { this.file = file; this.escapeDigits = escapeDigits; this.offset = offset; this.forwardDigit = forwardDigit; this.rewindDigit = rewindDigit; this.pauseDigit = pauseDigit; } #endregion #region ControlDigits(string forwardDigit, string rewindDigit) /// /// Sets the control digits for fast forward and rewind. /// /// the digit for fast forward. /// the digit for rewind. public void ControlDigits(string forwardDigit, string rewindDigit) { this.forwardDigit = forwardDigit; this.rewindDigit = rewindDigit; } #endregion #region ControlDigits(string forwardDigit, string rewindDigit, string pauseDigit) /// /// Sets the control digits for fast forward, rewind and pause. /// /// the digit for fast forward. /// the digit for rewind. /// the digit for pause and unpause. public void ControlDigits(string forwardDigit, string rewindDigit, string pauseDigit) { this.forwardDigit = forwardDigit; this.rewindDigit = rewindDigit; this.pauseDigit = pauseDigit; } #endregion #region BuildCommand() public override string BuildCommand() { StringBuilder sb = new StringBuilder("CONTROL STREAM FILE "); sb.Append(EscapeAndQuote(file) + " " + EscapeAndQuote(escapeDigits)); if (offset >= 0) sb.Append(" " + offset.ToString()); else if (forwardDigit != null || rewindDigit != null || pauseDigit != null) sb.Append(" 0"); if (forwardDigit != null || rewindDigit != null || pauseDigit != null) sb.Append(" " + forwardDigit); if (rewindDigit != null || pauseDigit != null) sb.Append(" " + rewindDigit); if (pauseDigit != null) sb.Append(" " + pauseDigit); return sb.ToString(); } #endregion } }