1 /***************************************************************************
2                           ADM_audioStream.h  -  description
3                              -------------------
4     copyright            : (C) 2008 by mean
5     email                : fixounet@free.fr
6  ***************************************************************************/
7 
8 /***************************************************************************
9  *                                                                         *
10  *   This program is free software; you can redistribute it and/or modify  *
11  *   it under the terms of the GNU General Public License as published by  *
12  *   the Free Software Foundation; either version 2 of the License, or     *
13  *   (at your option) any later version.                                   *
14  *                                                                         *
15  ***************************************************************************/
16 #ifndef ADM_audioStream_H
17 #define ADM_audioStream_H
18 
19 #include "ADM_coreAudio6_export.h"
20 #include "ADM_assert.h"
21 #include "ADM_baseAudioStream.h"
22 #include "string"
23 
24 
25 #define ADM_UNKNOWN_LANGUAGE std::string("und")
26 #define AAC_DEFAULT_FRAME_LENGTH 1024
27 
28 /**
29         \fn      ADM_audioAccess
30         \brief   Access layer to the file. That one is re-instancied by each demuxer.
31                  Some methods are also present in audioStream to allow override or
32                         computation when access does not provide it.
33 */
34 #define ADM_AUDIO_NO_DTS ((uint64_t)-1)
35 class ADM_audioAccess
36 {
37 protected:
38                         /// must be allocated/freed if needed by derived class
39                         uint8_t *extraData;
40                         uint32_t extraDataLen;
41 
42 public:
ADM_audioAccess()43                                   ADM_audioAccess() {extraData=NULL;extraDataLen=0;}
~ADM_audioAccess()44                 virtual           ~ADM_audioAccess() {}
45                                     /// Hint, the stream is pure CBR (AC3,MP2,MP3)
isCBR(void)46                 virtual bool      isCBR(void) { return true;}
47                                     /// Return true if we can expect constant nb of samples per packet
constantSamplesPerPacket(void)48                 virtual bool      constantSamplesPerPacket(void) {return true;};
49                                     /// Return true if the demuxer can seek in time
canSeekTime(void)50                 virtual bool      canSeekTime(void) {return false;};
51                                     /// Return true if the demuxer can seek by offser
canSeekOffset(void)52                 virtual bool      canSeekOffset(void) {return false;};
53                                     /// Return true if we can have the audio duration
canGetDuration(void)54                 virtual bool      canGetDuration(void) {return false;};
55                                     /// Returns audio duration in us
getDurationInUs(void)56                 virtual uint64_t  getDurationInUs(void) {return 0;}
57                                     /// Returns length in bytes of the audio stream
getLength(void)58                 virtual uint32_t  getLength(void){return 0;}
59                                     /// Set position in bytes
setPos(uint64_t pos)60                 virtual bool      setPos(uint64_t pos){ADM_assert(0); return 0;};
61                                     /// Get position in bytes
getPos()62                 virtual uint64_t  getPos(){ADM_assert(0); return 0;};
63                                     /// Go to a given time
goToTime(uint64_t timeUs)64                 virtual bool      goToTime(uint64_t timeUs){ADM_assert(0); return false;}
65                                     /// Grab extra data
getExtraData(uint32_t * l,uint8_t ** d)66                 virtual bool      getExtraData(uint32_t *l, uint8_t **d)
67                                     {
68                                             *l=extraDataLen;
69                                             *d=extraData;
70                                             return true;
71                                     };
72 
73 
74                 virtual bool    getPacket(uint8_t *buffer, uint32_t *size, uint32_t maxSize,uint64_t *dts)=0;
75 };
76 /**
77         \fn ADM_audioStream
78         \brief Base class for audio stream
79 
80 */
81 class ADM_COREAUDIO6_EXPORT ADM_audioStream
82 {
83         protected:
84                        WAVHeader                wavHeader;
85 /// Access will be allocated externally, but will be destroy by ADM_audioStream when it is destroyed
86                        ADM_audioAccess          *access;
87                        uint32_t                 lengthInBytes;
88                        uint32_t                 samplesPerPacket;
89                        uint64_t                 position;
90                        uint64_t                 lastDts;
91                        uint64_t                 durationInUs;
92                        uint64_t                 lastDtsBase;
93                        uint64_t                 sampleElapsed;
94                        std::string              language;
95 
96     ///
97                         void                    setDts(uint64_t newDts);
98     /// increment DTS by samples
99                        bool                     advanceDtsBySample(uint32_t samples);
100     /// Same with provided frequency (SBR)
101                        bool                     advanceDtsByCustomSample(uint32_t samples,uint32_t fq);
102         public:
103 /// Default constructor
104                        ADM_audioStream(WAVHeader *header,ADM_audioAccess *access);
105               virtual  ~ADM_audioStream() ;
106 /// Returns wavheader
getInfo(void)107 virtual                 WAVHeader                *getInfo(void) {return &wavHeader;}
108 ///  Get a packet
109 virtual uint8_t         getPacket(uint8_t *buffer,uint32_t *size, uint32_t sizeMax,uint32_t *nbSample,uint64_t *dts);
110 /// Go to a given time, in microseconds
111 virtual bool            goToTime(uint64_t nbUs);
112 /// Returns current time in us. Not used.
113 //virtual uint8_t         getTime(uint64_t *nbUs);
114 /// Returns extra configuration data
115 virtual bool            getExtraData(uint32_t *l, uint8_t **d);
116 /// Returns or compute duration. If the access cannot provide it, it will be computed here
getDurationInUs(void)117         uint64_t        getDurationInUs(void) {return durationInUs;}
isCBR()118 virtual bool            isCBR()
119                             {
120                                 if(!access) return false;
121                                 return access->isCBR();
122                             }
constantSamplesPerPacket(void)123 virtual bool            constantSamplesPerPacket(void)
124                             {
125                                 if(!access) return true;
126                                 return access->constantSamplesPerPacket();
127                             }
setSamplesPerPacket(uint32_t nbSamples)128 virtual bool            setSamplesPerPacket(uint32_t nbSamples) {samplesPerPacket=nbSamples; return true;}
getSamplesPerPacket(void)129 virtual uint32_t        getSamplesPerPacket(void) {return samplesPerPacket;} // use only for AAC
getLanguage()130 virtual        const std::string &getLanguage() {return language;}
setLanguage(const std::string & lan)131 virtual        void              setLanguage(const std::string &lan) {language=lan;}
132 virtual        bool              isLanguageSet(void);
133 };
134 /**
135    \fn ADM_audioCreateStream
136     \brief Create the appropriate audio stream. It will be a derivated class of audioStream if possible (MP3/AC3)
137 */
138 ADM_COREAUDIO6_EXPORT ADM_audioStream  *ADM_audioCreateStream(WAVHeader *wavheader, ADM_audioAccess *access,bool createTimeMap=true);
139 #endif
140 // EOF
141 
142