1 /*
2  * Copyright (C) 2007 iptego GmbH
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.
10  *
11  * For a license to use the sems software under conditions
12  * other than those described here, or to purchase support for this
13  * software, please contact iptel.org by e-mail at the following addresses:
14  *    info@iptel.org
15  *
16  * SEMS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25 /** @file AmCachedAudioFile.h */
26 #ifndef _AMFILECACHE_H
27 #define _AMFILECACHE_H
28 
29 #include "AmAudioFile.h"
30 
31 #include <string>
32 
33 /**
34  * \brief memory cache for AmAudioFile
35  *
36  * The AmFileCache class loads a file once into memory
37  * to be used e.g. by AmCachedAudioFile.
38  */
39 class AmFileCache
40 {
41   void* data;
42   size_t data_size;
43   std::string name;
44 
45  public:
46   AmFileCache();
47   ~AmFileCache();
48 
49   /** load filename into memory
50    * @return 0 if everything's OK
51    */
52   int load(const std::string& filename);
53   /** get the size of the file */
54   size_t getSize();
55   /** read size bytes from pos into buf */
56   int read(void* buf, size_t* pos, size_t size);
57   /** get the filename */
58   const string& getFilename();
59   /** get a pointer to the file's data - use with caution! */
getData()60   void* getData() { return data; }
61 };
62 
63 /**
64  * \brief AmAudio implementation for cached file
65  *
66  *  This uses an AmFileCache instance to read the data
67  *  rather than a file.
68  */
69 class AmCachedAudioFile
70 : public AmAudio
71 {
72   AmFileCache* cache;
73   /** current position */
74   size_t fpos;
75   /** beginning of data in file */
76   size_t begin;
77   bool good;
78 
79   /** @see AmAudio::read */
80   int read(unsigned int user_ts, unsigned int size);
81 
82   /** @see AmAudio::write */
83   int write(unsigned int user_ts, unsigned int size);
84 
85   /** get the file format from the file name */
86   AmAudioFileFormat* fileName2Fmt(const string& name);
87 
88   /** Format of that file. @see fp, open(). */
89   amci_inoutfmt_t* iofmt;
90 
91  public:
92   AmCachedAudioFile(AmFileCache* cache);
93   ~AmCachedAudioFile();
94 
95   /** loop the file? */
96   AmSharedVar<bool> loop;
97 
98   /**
99    * Rewind the file.
100    */
101   void rewind();
102 
103   /** Closes the file. */
104   void close();
105 
106   /** everything ok? */
is_good()107   bool is_good() { return good; }
108 };
109 #endif //_AMFILECACHE_H
110