1 /*
2  * Copyright (C) 2002-2003 Fhg Fokus
3  *
4  * This file is part of SEMS, a free SIP media server.
5  *
6  * SEMS is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version. This program is released under
10  * the GPL with the additional exemption that compiling, linking,
11  * and/or using OpenSSL is allowed.
12  *
13  * For a license to use the SEMS software under conditions
14  * other than those described here, or to purchase support for this
15  * software, please contact iptel.org by e-mail at the following addresses:
16  *    info@iptel.org
17  *
18  * SEMS is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  */
27 /** @file AmAudioFile.h */
28 #ifndef _AmAudioFile_h_
29 #define _AmAudioFile_h_
30 
31 #include "AmAudio.h"
32 #include "AmBufferedAudio.h"
33 
34 /** \brief \ref AmAudioFormat for file */
35 class AmAudioFileFormat: public AmAudioFormat
36 {
37   /** == "" if not yet initialized. */
38   string          name;
39 
40   /** == -1 if not yet initialized. */
41   int             subtype;
42 
43   /** ==  0 if not yet initialized. */
44   amci_subtype_t* p_subtype;
45 
46 protected:
47   int getCodecId();
48 
49 public:
50   /**
51    * Constructor for file based formats.
52    * All information are taken from the plug-in description.
53    * @param name The file format name (ex. "Wav").
54    * @param subtype Subtype for the file format (see amci.h).
55    */
56   AmAudioFileFormat(const string& name, int subtype = -1);
57   /** format with rate & channels, not taken from subtype */
58 
59   AmAudioFileFormat(const string& name, int subtype, amci_subtype_t* p_subtype);
60 
~AmAudioFileFormat()61   virtual ~AmAudioFileFormat()  { }
62 
63   virtual amci_codec_t* getCodec();
64 
65   /** @return Format name. */
getName()66   string        getName() { return name; }
67   /** @return Format subtype. */
getSubtypeId()68   int           getSubtypeId() { return subtype; }
69   /** @return Subtype pointer. */
70   virtual amci_subtype_t*  getSubtype();
71 
72   void setSubtypeId(int subtype_id);
73 };
74 
75 /**
76  * \brief AmAudio implementation for file access
77  */
78 class AmAudioFile: public AmBufferedAudio
79 {
80 public:
81   /** Open mode. */
82   enum OpenMode { Read=1, Write=2 };
83 
84 protected:
85   /** Pointer to the file opened as last. */
86   FILE* fp;
87   long begin;
88 
89   /** Format of that file. @see fp, open(). */
90   amci_inoutfmt_t* iofmt;
91   /** Open mode. */
92   int open_mode;
93 
94   /** Size of datas having been read/written until now. */
95   int data_size;
96 
97   bool on_close_done;
98   bool close_on_exit;
99 
100   /** @see AmAudio::read */
101   int read(unsigned int user_ts, unsigned int size);
102 
103   /** @see AmAudio::write */
104   int write(unsigned int user_ts, unsigned int size);
105 
106   /** @return a file format from file name. (ex: '1234.wav') */
107   virtual AmAudioFileFormat* fileName2Fmt(const string& name, const string& subtype);
108 
109   /** @return subtype ID and trim filename if subtype embedded */
110   string getSubtype(string& filename);
111 
112   /** internal function for opening the file */
113   int fpopen_int(const string& filename, OpenMode mode, FILE* n_fp, const string& subtype);
114 
115 public:
116   AmSharedVar<bool> loop;
117   AmSharedVar<bool> autorewind;
118 
119   AmAudioFile();
120   ~AmAudioFile();
121 
122   /**
123    * Opens a file.
124    * <ul>
125    * <li>In read mode: sets input format.
126    * <li>In write mode: <ol>
127    *                    <li>needs output format set.
128    *                    <li>If file name already exists,
129    *                        the file will be overwritten.
130    *                    </ol>
131    * </ul>
132    * @param filename Name of the file.
133    * @param mode Open mode.
134    * @return 0 if everything's OK
135    * @see OpenMode
136    */
137   int open(const string& filename, OpenMode mode,
138 	   bool is_tmp=false);
139 
140   int fpopen(const string& filename, OpenMode mode, FILE* n_fp);
141 
142   /** Rewind the file to beginning. */
143   void rewind();
144 
145   /** Rewind the file some milliseconds. */
146   void rewind(unsigned int msec);
147 
148   /** skip forward some milliseconds. */
149   void forward(unsigned int msec);
150 
151   /** Closes the file. */
152   void close();
153 
154   /** Executes the handler's on_close. */
155   void on_close();
156 
157   /** be carefull with this one ;-) */
getfp()158   FILE* getfp() { return fp; }
159 
getMode()160   OpenMode getMode() { return (OpenMode)open_mode; }
161 
162   /** Gets data size in the current file */
getDataSize()163   int getDataSize() { return data_size; }
164 
165   /** Gets length of the current file in ms */
166   int getLength();
167 
168   /**
169    * @return MIME type corresponding to the audio file.
170    */
171   string getMimeType();
172 
setCloseOnDestroy(bool cod)173   void setCloseOnDestroy(bool cod){
174     close_on_exit = cod;
175   }
176 };
177 
178 #endif
179 
180 // Local Variables:
181 // mode:C++
182 // End:
183