RTP Audio System  2.0.0
audiodevice.h
Go to the documentation of this file.
00001 // ##########################################################################
00002 // ####                                                                  ####
00003 // ####                      RTP Audio Server Project                    ####
00004 // ####                    ============================                  ####
00005 // ####                                                                  ####
00006 // #### Audio Device                                                     ####
00007 // ####                                                                  ####
00008 // ####           Copyright (C) 1999-2012 by Thomas Dreibholz            ####
00009 // ####                                                                  ####
00010 // #### Contact:                                                         ####
00011 // ####    EMail: dreibh@iem.uni-due.de                                  ####
00012 // ####    WWW:   https://www.nntb.no/~dreibh/rtpaudio                ####
00013 // ####                                                                  ####
00014 // #### ---------------------------------------------------------------- ####
00015 // ####                                                                  ####
00016 // #### This program is free software: you can redistribute it and/or    ####
00017 // #### modify it under the terms of the GNU General Public License as   ####
00018 // #### published by the Free Software Foundation, either version 3 of   ####
00019 // #### the License, or (at your option) any later version.              ####
00020 // ####                                                                  ####
00021 // #### This program is distributed in the hope that it will be useful,  ####
00022 // #### but WITHOUT ANY WARRANTY; without even the implied warranty of   ####
00023 // #### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    ####
00024 // #### GNU General Public License for more details.                     ####
00025 // ####                                                                  ####
00026 // #### You should have received a copy of the GNU General Public        ####
00027 // #### License along with this program.  If not, see                    ####
00028 // #### <http://www.gnu.org/licenses/>.                                  ####
00029 // ####                                                                  ####
00030 // ##########################################################################
00031 // $Id: audiodevice.h 1329 2011-12-23 22:06:51Z dreibh $
00032 
00033 
00034 #ifndef AUDIODEVICE_H
00035 #define AUDIODEVICE_H
00036 
00037 
00038 #include "tdsystem.h"
00039 #include "audiowriterinterface.h"
00040 
00041 #ifdef HAVE_PULSEAUDIO
00042 #include <pulse/thread-mainloop.h>
00043 #include <pulse/context.h>
00044 #include <pulse/stream.h>
00045 #else
00046 #include "thread.h"
00047 #include "ringbuffer.h"
00048 #endif
00049 
00050 
00058 class AudioDevice : virtual public AudioWriterInterface
00059 #ifndef HAVE_PULSEAUDIO
00060                     , public Thread
00061 #endif
00062 {
00063    // ====== Constructor/Destructor =========================================
00064    public:
00070    AudioDevice(const char* name = "/dev/dsp");
00071 
00075    ~AudioDevice();
00076 
00077 
00078    // ====== Status functions ===============================================
00086    inline cardinal getSyncCount() const;
00087 
00093    inline void resetSyncCount();
00094 
00095 
00096    // ====== AudioQualityInterface implementation ===========================
00102    card16 getSamplingRate() const;
00103 
00109    card8 getBits() const;
00110 
00116    card8 getChannels() const;
00117 
00118 
00124    card16 getByteOrder() const;
00125 
00126 
00132    cardinal getBytesPerSecond() const;
00133 
00139    cardinal getBitsPerSample() const;
00140 
00141 
00147    card16 setSamplingRate(const card16 samplingRate);
00148 
00154    card8 setBits(const card8 bits);
00155 
00161    card8 setChannels(const card8 channels);
00162 
00163 
00169    card16 setByteOrder(const card16 byteOrder);
00170 
00171 
00172    // ====== AudioInterface implementation ==================================
00178    bool ready() const;
00179 
00185    void sync();
00186 
00192    bool write(const void* data, const size_t length);
00193 
00199    cardinal getCurrentCapacity();
00200 
00201 
00202    // ====== Constants ======================================================
00206    static const cardinal RingBufferSize = 128 * 1024;
00207 
00211    static const cardinal ResizeThresholdPercent = 75;
00212 
00218    static const cardinal ResizeModulo = 4;
00219 
00220 
00221    // ====== Internal data ==================================================
00222    private:
00223    void run();
00224 #ifdef HAVE_PULSEAUDIO
00225    bool openStream();
00226    void closeStream();
00227    static void context_state_callback(pa_context* context, void* userData);
00228 #endif
00229 
00230    bool                  IsReady;
00231    cardinal              SyncCount;
00232    cardinal              JitterCompensationLatency;
00233 
00234    card16                AudioSamplingRate;  // Format of data written to AudioDevice.
00235    card8                 AudioBits;
00236    card8                 AudioChannels;
00237    card16                AudioByteOrder;
00238 
00239    card16                DeviceSamplingRate; // Format of data written to real device.
00240    card8                 DeviceBits;
00241    card8                 DeviceChannels;
00242    card16                DeviceByteOrder;
00243 
00244 #ifdef HAVE_PULSEAUDIO
00245    friend class          AudioMixer;
00246    pa_threaded_mainloop* MainLoop;
00247    pa_mainloop_api*      MainLoopAPI;
00248    pa_context*           Context;
00249    pa_stream*            Stream;
00250 #else
00251    int                   DeviceFD;           // Device and its properties.
00252    int                   DeviceCapabilities;
00253    int                   DeviceFormats;
00254    integer               DeviceBlockSize;
00255    integer               DeviceFragmentSize;
00256    integer               DeviceOSpace;
00257 
00258    RingBuffer            Buffer;             // Jitter buffer and jitter compensation
00259    cardinal              ResizeThreshold;
00260    card64                LastWriteTimeStamp;
00261    integer               Balance;
00262    bool                  IsFillingBuffer;
00263 #endif
00264 };
00265 
00266 
00267 #include "audiodevice.icc"
00268 
00269 
00270 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines