SDLaudioIO.hpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: SDLaudioIO.hpp * 00006 * * 00007 * * 00008 * * 00009 * ITEC institute of the University of Klagenfurt (Austria) * 00010 * http://www.itec.uni-klu.ac.at * 00011 * * 00012 * * 00013 * For more information visit the ViTooKi homepage: * 00014 * http://ViTooKi.sourceforge.net * 00015 * vitooki-user@lists.sourceforge.net * 00016 * vitooki-devel@lists.sourceforge.net * 00017 * * 00018 * This file is part of ViTooKi, a free video toolkit. * 00019 * ViTooKi is free software; you can redistribute it and/or * 00020 * modify it under the terms of the GNU General Public License * 00021 * as published by the Free Software Foundation; either version 2 * 00022 * of the License, or (at your option) any later version. * 00023 * * 00024 * This program is distributed in the hope that it will be useful, * 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00027 * GNU General Public License for more details. * 00028 * * 00029 * You should have received a copy of the GNU General Public License * 00030 * along with this program; if not, write to the Free Software * 00031 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * 00032 * MA 02111-1307, USA. * 00033 * * 00034 ***********************************************************************/ 00035 00036 /*********************************************************************** 00037 * * 00038 * REVISION HISTORY: * 00039 * * 00040 * * 00041 * * 00042 ***********************************************************************/ 00043 00044 // SDLaudioIO.hpp: Class for Audio output. 00045 // 00047 00048 #ifdef ISOMP4 00049 #include "ISOMovies.h" //XXX for data types definition 00050 #endif 00051 00052 #ifndef _SDLAUDIOIO_HPP_ 00053 #define _SDLAUDIOIO_HPP_ 00054 00055 #if _MSC_VER > 1000 00056 #pragma once 00057 #endif // _MSC_VER > 1000 00058 00059 #include "AudioIO.hpp" 00060 #include "adaptors/GlobalTimer.hpp" 00061 #include "SDL.h" 00062 #include <SDL_thread.h> 00063 #include "VMutex.hpp" 00064 #include "VCondition.hpp" 00065 00066 #include "AudioESInfo.hpp" 00067 #include "Frame.hpp" 00068 #include "io/Rtp.hpp" 00069 00070 #define SDL_MAX_AUDIOQ_SIZE (2 * 4 * 48000) // 2 media seconds @48 kHz, 16-bit stereo 00071 // this may be only 2 jumbo packets! 00072 /* SDL audio buffer size, in samples. Should be small to have precise 00073 * A/V sync as SDL does not have hardware buffer fullness info. */ 00074 #define SDL_AUDIO_BUFFER_SAMPLES 1024 00075 00076 class Frame; 00077 class AudioESInfo; 00078 00079 typedef struct APacket { 00080 u64 pts; /* presentation time stamp in stream units */ 00081 u8 *data; 00082 int size; 00083 } APacket; 00084 00085 typedef struct APacketList { 00086 APacket pkt; 00087 struct APacketList *next; 00088 } APacketList; 00089 00090 00091 typedef struct PacketQueue { 00092 APacketList *first_pkt, *last_pkt; 00093 int nb_packets; 00094 int size; 00095 int abort_request; 00096 #ifndef WINCE 00097 SDL_mutex *mutex; 00098 SDL_cond *cond; 00099 #else 00100 VMutex *mutex; 00101 VCondition *cond; 00102 #endif 00103 } PacketQueue; 00104 00105 class SDLaudioIO; //forward declaration 00106 00107 typedef struct AudioState { 00108 SDLaudioIO *selfClass; 00109 SDL_AudioSpec spec; 00110 int abort_request; 00111 PacketQueue audioq; 00112 APacket audio_pkt; // current packet to be processed by sdl_audio_callback() 00113 int audio_pkt_index; // index into audio_pkt.data 00114 u32 audio_pkt_time_increment; // in ms 00115 u32 ticks_per_second; 00116 u32 queue_full_delay; // in ms 00117 } AudioState; 00118 00119 00120 00132 class SDLaudioIO : public AudioIO { 00133 public: 00138 SDLaudioIO(AudioESInfo *es, ESSynchronizer *ess = NULL); 00139 00140 virtual ~SDLaudioIO(); 00141 00142 int initialize(); 00143 00144 // IO Methods 00145 Frame * getFrame() ; 00146 00148 int writeFrame(Frame * frm, ESInfo *out_es=NULL); 00149 00152 bool open(); 00153 00154 bool close(bool immediate=false); 00155 00156 IO::State play(double prefetchTime=0.0); 00157 00158 IO::State pause(); 00159 00160 IO::State mute(); 00161 00162 int packet_queue_get(PacketQueue *q, APacket *pkt, int block); 00163 00164 void resume(); 00165 //static void sdl_audio_callback(void *opaque, Uint8 *stream, int len); 00166 00168 void clearBuffer(); 00169 00170 int getPacketsInQueue(); 00171 00172 friend void sdl_audio_callback(void *opaque, Uint8 *stream, int length); 00173 00174 private: 00175 bool initialized; 00176 bool requestUnpauseESS; 00177 AudioState *is; 00178 void packet_queue_init(); 00179 void packet_queue_end(); 00180 int packet_queue_put(APacket *pkt); 00181 void packet_queue_abort(); 00182 void packet_queue_clear(); 00183 00187 const char *get_sdl_audio_state() const; 00188 00192 int restartSDLThread(); 00193 }; 00194 00195 #endif