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, and allows the listner to control the stream.<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.<br>
28   * Available since Asterisk 1.2
29   * 
30   * @author srt
31   * @version $Id: ControlStreamFileCommand.java,v 1.2 2005/08/28 13:37:04 srt Exp $
32   * @since 0.2
33   */
34  public class ControlStreamFileCommand extends AGICommand
35  {
36      /***
37       * Serial version identifier.
38       */
39      private static final long serialVersionUID = 3978141041352128820L;
40  
41      /***
42       * The name of the file to stream.
43       */
44      private String file;
45  
46      /***
47       * When one of these digits is pressed while streaming the command returns.
48       */
49      private String escapeDigits;
50  
51      /***
52       * The offset samples to skip before streaming.
53       */
54      private int offset;
55  
56      private String forwardDigit;
57  
58      private String rewindDigit;
59  
60      private String pauseDigit;
61  
62      /***
63       * Creates a new ControlStreamFileCommand, streaming from the beginning. It
64       * uses the default digit "#" for forward and "*" for rewind and does not
65       * support pausing.
66       * 
67       * @param file the name of the file to stream, must not include extension.
68       */
69      public ControlStreamFileCommand(String file)
70      {
71          this.file = file;
72          this.escapeDigits = null;
73          this.offset = -1;
74      }
75  
76      /***
77       * Creates a new ControlStreamFileCommand, streaming from the beginning. It
78       * uses the default digit "#" for forward and "*" for rewind and does not
79       * support pausing.
80       * 
81       * @param file the name of the file to stream, must not include extension.
82       * @param escapeDigits contains the digits that allow the user to interrupt
83       *            this command.
84       */
85      public ControlStreamFileCommand(String file, String escapeDigits)
86      {
87          this.file = file;
88          this.escapeDigits = escapeDigits;
89          this.offset = -1;
90      }
91  
92      /***
93       * Creates a new ControlStreamFileCommand, streaming from the given offset.
94       * It uses the default digit "#" for forward and "*" for rewind and does not
95       * support pausing.
96       * 
97       * @param file the name of the file to stream, must not include extension.
98       * @param escapeDigits contains the digits that allow the user to interrupt
99       *            this command. Maybe <code>null</code> if you don't want the
100      *            user to interrupt.
101      * @param offset the offset samples to skip before streaming.
102      */
103     public ControlStreamFileCommand(String file, String escapeDigits, int offset)
104     {
105         this.file = file;
106         this.escapeDigits = escapeDigits;
107         this.offset = offset;
108     }
109 
110     /***
111      * Creates a new ControlStreamFileCommand, streaming from the given offset.
112      * It uses the default digit "#" for forward and "*" for rewind and does not
113      * support pausing.
114      * 
115      * @param file the name of the file to stream, must not include extension.
116      * @param escapeDigits contains the digits that allow the user to interrupt
117      *            this command. Maybe <code>null</code> if you don't want the
118      *            user to interrupt.
119      * @param offset the offset samples to skip before streaming.
120      * @param forwardDigit the digit for fast forward.
121      * @param rewindDigit the digit for rewind.
122      * @param pauseDigit the digit for pause and unpause.
123      */
124     public ControlStreamFileCommand(String file, String escapeDigits,
125             int offset, String forwardDigit, String rewindDigit,
126             String pauseDigit)
127     {
128         this.file = file;
129         this.escapeDigits = escapeDigits;
130         this.offset = offset;
131         this.forwardDigit = forwardDigit;
132         this.rewindDigit = rewindDigit;
133         this.pauseDigit = pauseDigit;
134     }
135 
136     /***
137      * Returns the name of the file to stream.
138      * 
139      * @return the name of the file to stream.
140      */
141     public String getFile()
142     {
143         return file;
144     }
145 
146     /***
147      * Sets the name of the file to stream.
148      * 
149      * @param file the name of the file to stream, must not include extension.
150      */
151     public void setFile(String file)
152     {
153         this.file = file;
154     }
155 
156     /***
157      * Returns the digits that allow the user to interrupt this command.
158      * 
159      * @return the digits that allow the user to interrupt this command.
160      */
161     public String getEscapeDigits()
162     {
163         return escapeDigits;
164     }
165 
166     /***
167      * Sets the digits that allow the user to interrupt this command.
168      * 
169      * @param escapeDigits the digits that allow the user to interrupt this
170      *            command or <code>null</code> for none.
171      */
172     public void setEscapeDigits(String escapeDigits)
173     {
174         this.escapeDigits = escapeDigits;
175     }
176 
177     /***
178      * Returns the offset samples to skip before streaming.
179      * 
180      * @return the offset samples to skip before streaming.
181      */
182     public int getOffset()
183     {
184         return offset;
185     }
186 
187     /***
188      * Sets the offset samples to skip before streaming.
189      * 
190      * @param offset the offset samples to skip before streaming.
191      */
192     public void setOffset(int offset)
193     {
194         this.offset = offset;
195     }
196 
197     /***
198      * Returns the digit for fast forward.
199      * 
200      * @return the digit for fast forward.
201      */
202     public String getForwardDigit()
203     {
204         return forwardDigit;
205     }
206 
207     /***
208      * Returns the digit for rewind.
209      * 
210      * @return the digit for rewind.
211      */
212     public String getRewindDigit()
213     {
214         return rewindDigit;
215     }
216 
217     /***
218      * Retruns the digit for pause and unpause.
219      * 
220      * @return the digit for pause and unpause.
221      */
222     public String getPauseDigit()
223     {
224         return pauseDigit;
225     }
226 
227     /***
228      * Sets the control digits for fast forward and rewind.
229      * 
230      * @param forwardDigit the digit for fast forward.
231      * @param rewindDigit the digit for rewind.
232      */
233     public void setControlDigits(String forwardDigit, String rewindDigit)
234     {
235         this.forwardDigit = forwardDigit;
236         this.rewindDigit = rewindDigit;
237     }
238 
239     /***
240      * Sets the control digits for fast forward, rewind and pause.
241      * 
242      * @param forwardDigit the digit for fast forward.
243      * @param rewindDigit the digit for rewind.
244      * @param pauseDigit the digit for pause and unpause.
245      */
246     public void setControlDigits(String forwardDigit, String rewindDigit,
247             String pauseDigit)
248     {
249         this.forwardDigit = forwardDigit;
250         this.rewindDigit = rewindDigit;
251         this.pauseDigit = pauseDigit;
252     }
253 
254     public String buildCommand()
255     {
256         StringBuffer sb;
257 
258         sb = new StringBuffer("CONTROL STREAM FILE ");
259         sb.append(escapeAndQuote(file));
260         sb.append(" ");
261         sb.append(escapeAndQuote(escapeDigits));
262         if (offset >= 0)
263         {
264             sb.append(" ");
265             sb.append(offset);
266         }
267         else if (forwardDigit != null || rewindDigit != null
268                 || pauseDigit != null)
269         {
270             sb.append(" 0");
271         }
272 
273         if (forwardDigit != null || rewindDigit != null || pauseDigit != null)
274         {
275             sb.append(" ");
276             sb.append(forwardDigit);
277         }
278         if (rewindDigit != null || pauseDigit != null)
279         {
280             sb.append(" ");
281             sb.append(rewindDigit);
282         }
283         if (pauseDigit != null)
284         {
285             sb.append(" ");
286             sb.append(pauseDigit);
287         }
288 
289         return sb.toString();
290     }
291 }