00001 #ifndef AUDIOOUTPUTBASE
00002 #define AUDIOOUTPUTBASE
00003
00004
00005 #include <pthread.h>
00006 #include <sys/time.h>
00007
00008
00009 #include <iostream>
00010 using namespace std;
00011
00012
00013 #include <qstring.h>
00014 #include <qmutex.h>
00015
00016
00017 #include "audiooutput.h"
00018 #include "samplerate.h"
00019
00020 namespace soundtouch {
00021 class SoundTouch;
00022 };
00023 class FreeSurround;
00024 class AudioOutputDigitalEncoder;
00025 struct AVCodecContext;
00026
00027 #define AUDIO_SRC_IN_SIZE 16384
00028 #define AUDIO_SRC_OUT_SIZE (16384*6)
00029 #define AUDIO_TMP_BUF_SIZE (16384*6)
00030
00031
00032
00033
00034 #define AUDBUFSIZE 1536000
00035
00036 class AudioOutputBase : public AudioOutput
00037 {
00038 public:
00039 AudioOutputBase(QString laudio_main_device,
00040 QString laudio_passthru_device,
00041 int laudio_bits,
00042 int laudio_channels, int laudio_samplerate,
00043 AudioOutputSource lsource,
00044 bool lset_initial_vol, bool laudio_passthru);
00045 virtual ~AudioOutputBase();
00046
00047
00048 virtual void Reconfigure(int audio_bits,
00049 int audio_channels,
00050 int audio_samplerate,
00051 bool audio_passthru,
00052 void *audio_codec = NULL);
00053
00054
00055 virtual void SetBlocking(bool blocking);
00056
00057
00058 virtual void SetEffDsp(int dsprate);
00059
00060 virtual void SetStretchFactor(float factor);
00061 virtual float GetStretchFactor(void);
00062
00063 virtual void Reset(void);
00064
00065
00066 virtual bool AddSamples(char *buffer, int samples, long long timecode);
00067 virtual bool AddSamples(char *buffers[], int samples, long long timecode);
00068
00069 virtual void SetTimecode(long long timecode);
00070 virtual bool GetPause(void);
00071 virtual void Pause(bool paused);
00072
00073
00074 virtual void Drain(void);
00075
00076 virtual int GetAudiotime(void);
00077
00078
00079 virtual void Status(void);
00080
00081 virtual void SetSourceBitrate(int rate);
00082
00083 virtual void GetBufferStatus(uint &fill, uint &total);
00084
00085
00086
00087 virtual void bufferOutputData(bool y){ buffer_output_data_for_use = y; }
00088 virtual int readOutputData(unsigned char *read_buffer, int max_length);
00089
00090 protected:
00091
00092
00093 virtual bool OpenDevice(void) = 0;
00094 virtual void CloseDevice(void) = 0;
00095 virtual void WriteAudio(unsigned char *aubuf, int size) = 0;
00096 virtual int getSpaceOnSoundcard(void) = 0;
00097 virtual int getBufferedOnSoundcard(void) = 0;
00098 virtual int GetVolumeChannel(int channel) = 0;
00099 virtual void SetVolumeChannel(int channel, int volume) = 0;
00100
00101
00102 virtual bool StartOutputThread(void);
00103 virtual void StopOutputThread(void);
00104
00105 int GetAudioData(unsigned char *buffer, int buf_size, bool fill_buffer);
00106
00107 void _AddSamples(void *buffer, bool interleaved, int samples, long long timecode);
00108
00109 void KillAudio();
00110 void OutputAudioLoop(void);
00111 static void *kickoffOutputAudioLoop(void *player);
00112 void SetAudiotime(void);
00113 int WaitForFreeSpace(int len);
00114
00115 int audiolen(bool use_lock);
00116 int audiofree(bool use_lock);
00117
00118 void UpdateVolume(void);
00119
00120 void SetStretchFactorLocked(float factor);
00121
00122 int GetBaseAudioTime() const { return audiotime; }
00123 int GetBaseAudBufTimeCode() const { return audbuf_timecode; }
00124 soundtouch::SoundTouch *GetSoundStretch() const { return pSoundStretch; }
00125 void SetBaseAudioTime(const int inAudioTime) { audiotime = inAudioTime; }
00126
00127 int effdsp;
00128 int effdspstretched;
00129
00130
00131 int audio_channels;
00132 int audio_bytes_per_sample;
00133 int audio_bits;
00134 int audio_samplerate;
00135 int audio_buffer_unused;
00136 int fragment_size;
00137 long soundcard_buffer_size;
00138 QString audio_main_device;
00139 QString audio_passthru_device;
00140
00141 bool audio_passthru;
00142
00143 float audio_stretchfactor;
00144 AVCodecContext *audio_codec;
00145 AudioOutputSource source;
00146
00147 bool killaudio;
00148
00149 bool pauseaudio, audio_actually_paused, was_paused;
00150 bool set_initial_vol;
00151 bool buffer_output_data_for_use;
00152
00153 int configured_audio_channels;
00154
00155 private:
00156
00157 bool need_resampler;
00158 SRC_STATE *src_ctx;
00159 SRC_DATA src_data;
00160 float src_in[AUDIO_SRC_IN_SIZE];
00161 float src_out[AUDIO_SRC_OUT_SIZE];
00162 short tmp_buff[AUDIO_TMP_BUF_SIZE];
00163
00164
00165 soundtouch::SoundTouch *pSoundStretch;
00166 AudioOutputDigitalEncoder *encoder;
00167 FreeSurround *upmixer;
00168
00169 int source_audio_channels;
00170 int source_audio_bytes_per_sample;
00171 bool needs_upmix;
00172 int surround_mode;
00173
00174 bool blocking;
00175
00176 int lastaudiolen;
00177 long long samples_buffered;
00178
00179 bool audio_thread_exists;
00180 pthread_t audio_thread;
00181
00182 pthread_mutex_t audio_buflock;
00183
00184
00185 pthread_cond_t audio_bufsig;
00186
00187
00188
00189 pthread_mutex_t avsync_lock;
00190
00192 long long audiotime;
00193 struct timeval audiotime_updated;
00194
00195
00196 unsigned char audiobuffer[AUDBUFSIZE];
00197 int raud, waud;
00199 long long audbuf_timecode;
00200
00201 int numlowbuffer;
00202
00203 QMutex killAudioLock;
00204
00205 long current_seconds;
00206 long source_bitrate;
00207
00208
00209 };
00210
00211 #endif
00212