EMIPLIB

mipossinputoutput.h

Go to the documentation of this file.
00001 /*
00002     
00003   This file is a part of EMIPLIB, the EDM Media over IP Library.
00004   
00005   Copyright (C) 2006-2011  Hasselt University - Expertise Centre for
00006                       Digital Media (EDM) (http://www.edm.uhasselt.be)
00007 
00008   This library is free software; you can redistribute it and/or
00009   modify it under the terms of the GNU Lesser General Public
00010   License as published by the Free Software Foundation; either
00011   version 2.1 of the License, or (at your option) any later version.
00012 
00013   This library is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016   Lesser General Public License for more details.
00017 
00018   You should have received a copy of the GNU Lesser General Public
00019   License along with this library; if not, write to the Free Software
00020   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  
00021   USA
00022 
00023 */
00024 
00029 #ifndef MIPOSSINPUTOUTPUT_H
00030 
00031 #define MIPOSSINPUTOUTPUT_H
00032 
00033 #include "mipconfig.h"
00034 
00035 #ifdef MIPCONFIG_SUPPORT_OSS
00036 
00037 #include "mipcomponent.h"
00038 #include "miptime.h"
00039 #include "mipsignalwaiter.h"
00040 #include <jthread/jthread.h>
00041 #include <string>
00042 
00043 class MIPRaw16bitAudioMessage;
00044 
00046 class EMIPLIB_IMPORTEXPORT MIPOSSInputOutputParams
00047 {
00048 public:
00049         MIPOSSInputOutputParams() : m_bufferTime(10.0), m_ossBufferTime(0.007), m_devName("/dev/dsp")
00050                                                                                         { m_exactRate = true; m_ossFragments = 6; }
00051         ~MIPOSSInputOutputParams()                                                      { }
00052 
00054         void setUseExactRate(bool f)                                                    { m_exactRate = f; }
00055 
00061         void setBufferTime(MIPTime t)                                                   { m_bufferTime = t; }
00062 
00064         void setDeviceName(const std::string &name)                                     { m_devName = name; }
00065 
00070         void setOSSBufferSize(MIPTime t)                                                { m_ossBufferTime = t; }
00071 
00076         void setOSSFragments(uint16_t n)                                                { m_ossFragments = n; }
00077 
00079         bool useExactRate() const                                                       { return m_exactRate; }
00080 
00082         MIPTime getBufferTime() const                                                   { return m_bufferTime; }
00083 
00085         std::string getDeviceName() const                                               { return m_devName; }
00086 
00088         MIPTime getOSSBufferSize() const                                                { return m_ossBufferTime; }
00089 
00091         uint16_t getOSSFragments() const                                                { return m_ossFragments; }
00092 private:
00093         bool m_exactRate;
00094         uint16_t m_ossFragments;
00095         MIPTime m_bufferTime, m_ossBufferTime;
00096         std::string m_devName;
00097 };
00098 
00109 class EMIPLIB_IMPORTEXPORT MIPOSSInputOutput : public MIPComponent
00110 {
00111 public:
00118         enum AccessMode { ReadOnly, WriteOnly, ReadWrite };
00119         
00120         MIPOSSInputOutput();
00121         ~MIPOSSInputOutput();
00122         
00131         bool open(int sampRate, int channels, MIPTime interval, AccessMode accessMode = WriteOnly, 
00132                   const MIPOSSInputOutputParams *pParams = 0);
00133         
00135         int getSamplingRate() const                                                     { return m_sampRate; }
00136 
00138         uint32_t getRawAudioSubtype() const                                             { return m_audioSubtype; }
00139         
00141         bool close();
00142         
00143         bool push(const MIPComponentChain &chain, int64_t iteration, MIPMessage *pMsg);
00144         bool pull(const MIPComponentChain &chain, int64_t iteration, MIPMessage **pMsg);
00145 private:
00146         class IOThread : public jthread::JThread
00147         {
00148         public:
00149                 IOThread(MIPOSSInputOutput &ossIO);
00150                 ~IOThread();
00151                 void stop();
00152         protected:
00153                 MIPOSSInputOutput &m_ossIO;
00154                 jthread::JMutex m_stopMutex;
00155                 bool m_stopLoop;
00156         };
00157 
00158         class InputThread : public IOThread
00159         {
00160         public:
00161                 InputThread(MIPOSSInputOutput &ossIO) : IOThread(ossIO)         { }
00162                 ~InputThread()                                                  { stop(); }
00163                 void *Thread();
00164         };
00165         
00166         class OutputThread : public IOThread
00167         {
00168         public:
00169                 OutputThread(MIPOSSInputOutput &ossIO) : IOThread(ossIO)        { }
00170                 ~OutputThread()                                                 { stop(); }
00171                 void *Thread();
00172         };
00173 
00174         bool inputThreadStep();
00175         bool outputThreadStep();
00176 
00177         InputThread *m_pInputThread;
00178         OutputThread *m_pOutputThread;
00179 
00180         friend class InputThread;
00181         friend class OutputThread;
00182 
00183         int m_device;
00184         int m_sampRate;
00185         int m_channels;
00186         AccessMode m_accessMode;
00187         uint32_t m_audioSubtype;
00188         bool m_isSigned;
00189 
00190         uint16_t *m_pInputBuffer;
00191         uint16_t *m_pLastInputCopy;
00192         uint16_t *m_pMsgBuffer;
00193         MIPRaw16bitAudioMessage *m_pMsg;
00194         bool m_gotLastInput;
00195         bool m_gotMsg;
00196         MIPSignalWaiter m_sigWait;
00197         MIPTime m_sampleInstant;
00198 
00199         uint16_t *m_pOutputBuffer;
00200         size_t m_bufferLength, m_blockLength, m_blockFrames;
00201         size_t m_curOutputPos;
00202         size_t m_nextOutputPos;
00203         MIPTime m_blockTime,m_outputDistTime;
00204 
00205         jthread::JMutex m_inputFrameMutex, m_outputFrameMutex;
00206 };
00207 
00208 #endif // MIPCONFIG_SUPPORT_OSS
00209 
00210 #endif // MIPOSSINPUTOUTPUT_H
00211