View Javadoc

1   /*
2    * Copyright  2004-2005 Stefan Reuter
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package net.sf.asterisk.fastagi.command;
18  
19  /***
20   * Plays the given file, allowing playback to be interrupted by the given
21   * digits, if any.<br>
22   * If offset is provided then the audio will seek to sample offset before play
23   * starts.<br>
24   * Returns 0 if playback completes without a digit being pressed, or the ASCII
25   * numerical value of the digit if one was pressed, or -1 on error or if the
26   * channel was disconnected. <br>
27   * Remember, the file extension must not be included in the filename.
28   * 
29   * @author srt
30   * @version $Id: StreamFileCommand.java,v 1.3 2005/08/01 18:37:49 srt Exp $
31   */
32  public class StreamFileCommand extends AGICommand
33  {
34      /***
35       * Serial version identifier.
36       */
37      private static final long serialVersionUID = 3978141041352128820L;
38  
39      /***
40       * The name of the file to stream.
41       */
42      private String file;
43  
44      /***
45       * When one of these digits is pressed while streaming the command returns.
46       */
47      private String escapeDigits;
48  
49      /***
50       * The offset samples to skip before streaming.
51       */
52      private int offset;
53  
54      /***
55       * Creates a new StreamFileCommand, streaming from the beginning.
56       * 
57       * @param file the name of the file to stream, must not include extension.
58       */
59      public StreamFileCommand(String file)
60      {
61          this.file = file;
62          this.escapeDigits = null;
63          this.offset = -1;
64      }
65  
66      /***
67       * Creates a new StreamFileCommand, streaming from the beginning.
68       * 
69       * @param file the name of the file to stream, must not include extension.
70       * @param escapeDigits contains the digits that allow the user to interrupt
71       *            this command.
72       */
73      public StreamFileCommand(String file, String escapeDigits)
74      {
75          this.file = file;
76          this.escapeDigits = escapeDigits;
77          this.offset = -1;
78      }
79  
80      /***
81       * Creates a new StreamFileCommand, streaming from the given offset.
82       * 
83       * @param file the name of the file to stream, must not include extension.
84       * @param escapeDigits contains the digits that allow the user to interrupt
85       *            this command. Maybe <code>null</code> if you don't want the
86       *            user to interrupt.
87       * @param offset the offset samples to skip before streaming.
88       */
89      public StreamFileCommand(String file, String escapeDigits, int offset)
90      {
91          this.file = file;
92          this.escapeDigits = escapeDigits;
93          this.offset = offset;
94      }
95  
96      /***
97       * Returns the name of the file to stream.
98       * 
99       * @return the name of the file to stream.
100      */
101     public String getFile()
102     {
103         return file;
104     }
105 
106     /***
107      * Sets the name of the file to stream.
108      * 
109      * @param file the name of the file to stream, must not include extension.
110      */
111     public void setFile(String file)
112     {
113         this.file = file;
114     }
115 
116     /***
117      * Returns the digits that allow the user to interrupt this command.
118      * 
119      * @return the digits that allow the user to interrupt this command.
120      */
121     public String getEscapeDigits()
122     {
123         return escapeDigits;
124     }
125 
126     /***
127      * Sets the digits that allow the user to interrupt this command.
128      * 
129      * @param escapeDigits the digits that allow the user to interrupt this
130      *            command or <code>null</code> for none.
131      */
132     public void setEscapeDigits(String escapeDigits)
133     {
134         this.escapeDigits = escapeDigits;
135     }
136 
137     /***
138      * Returns the offset samples to skip before streaming.
139      * 
140      * @return the offset samples to skip before streaming.
141      */
142     public int getOffset()
143     {
144         return offset;
145     }
146 
147     /***
148      * Sets the offset samples to skip before streaming.
149      * 
150      * @param offset the offset samples to skip before streaming.
151      */
152     public void setOffset(int offset)
153     {
154         this.offset = offset;
155     }
156 
157     public String buildCommand()
158     {
159         return "STREAM FILE " + escapeAndQuote(file) + " "
160                 + escapeAndQuote(escapeDigits)
161                 + (offset < 0 ? "" : " " + offset);
162     }
163 }