Show
Ignore:
Timestamp:
11/22/07 06:43:39 (13 years ago)
Author:
ppalmers
Message:

backup commit

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/ppalmers-streaming/src/libstreaming/generic/Port.cpp

    r705 r719  
    111111}; 
    112112 
     113bool Port::setName(std::string name) { 
     114    debugOutput( DEBUG_LEVEL_VERBOSE, "Setting name to %s for port %s\n",name.c_str(),m_Name.c_str()); 
     115 
     116    if (m_State != E_Created) { 
     117        debugFatal("Port (%s) not in E_Created state: %d\n",m_Name.c_str(),m_State); 
     118        return false; 
     119    } 
     120    m_Name=name; 
     121    return true; 
     122} 
     123 
     124bool Port::setBufferSize(unsigned int newsize) { 
     125    debugOutput( DEBUG_LEVEL_VERBOSE, "Setting buffersize to %d for port %s\n",newsize,m_Name.c_str()); 
     126    if (m_State != E_Created) { 
     127        debugFatal("Port (%s) not in E_Created state: %d\n",m_Name.c_str(),m_State); 
     128        return false; 
     129    } 
     130    m_buffersize=newsize; 
     131    return true; 
     132} 
     133 
     134unsigned int Port::getEventSize() { 
     135    switch (m_DataType) { 
     136        case E_Float: 
     137            return sizeof(float); 
     138        case E_Int24: // 24 bit 2's complement, packed in a 32bit integer (LSB's) 
     139            return sizeof(uint32_t); 
     140        case E_MidiEvent: 
     141            return sizeof(uint32_t); 
     142        default: 
     143            return 0; 
     144    } 
     145} 
     146 
     147bool Port::setDataType(enum E_DataType d) { 
     148    debugOutput( DEBUG_LEVEL_VERBOSE, "Setting datatype to %d for port %s\n",(int) d,m_Name.c_str()); 
     149    if (m_State != E_Created) { 
     150        debugFatal("Port (%s) not in E_Created state: %d\n",m_Name.c_str(),m_State); 
     151        return false; 
     152    } 
     153 
     154    // do some sanity checks 
     155    bool type_is_ok=false; 
     156    switch (m_PortType) { 
     157        case E_Audio: 
     158            if(d == E_Int24) type_is_ok=true; 
     159            if(d == E_Float) type_is_ok=true; 
     160            break; 
     161        case E_Midi: 
     162            if(d == E_MidiEvent) type_is_ok=true; 
     163            break; 
     164        case E_Control: 
     165            if(d == E_Default) type_is_ok=true; 
     166            break; 
     167        default: 
     168            break; 
     169    } 
     170 
     171    if(!type_is_ok) { 
     172        debugFatal("Datatype not supported by this type of port!\n"); 
     173        return false; 
     174    } 
     175 
     176    m_DataType=d; 
     177    return true; 
     178} 
     179 
     180bool Port::setSignalType(enum E_SignalType s) { 
     181    debugOutput( DEBUG_LEVEL_VERBOSE, "Setting signaltype to %d for port %s\n",(int)s,m_Name.c_str()); 
     182    if (m_State != E_Created) { 
     183        debugFatal("Port (%s) not in E_Created state: %d\n",m_Name.c_str(),m_State); 
     184        return false; 
     185    } 
     186 
     187    // do some sanity checks 
     188    bool type_is_ok=false; 
     189    switch (m_PortType) { 
     190        case E_Audio: 
     191            if(s == E_PeriodSignalled) type_is_ok=true; 
     192            break; 
     193        case E_Midi: 
     194            if(s == E_PacketSignalled) type_is_ok=true; 
     195            break; 
     196        case E_Control: 
     197            if(s == E_PeriodSignalled) type_is_ok=true; 
     198            break; 
     199        default: 
     200            break; 
     201    } 
     202    if(!type_is_ok) { 
     203        debugFatal("Signalling type not supported by this type of port!\n"); 
     204        return false; 
     205    } 
     206    m_SignalType=s; 
     207    return true; 
     208} 
     209 
     210bool Port::setBufferType(enum E_BufferType b) { 
     211    debugOutput( DEBUG_LEVEL_VERBOSE, "Setting buffer type to %d for port %s\n",(int)b,m_Name.c_str()); 
     212    if (m_State != E_Created) { 
     213        debugFatal("Port (%s) not in E_Created state: %d\n",m_Name.c_str(),m_State); 
     214        return false; 
     215    } 
     216    // do some sanity checks 
     217    bool type_is_ok=false; 
     218    switch (m_PortType) { 
     219        case E_Audio: 
     220            if(b == E_PointerBuffer) type_is_ok=true; 
     221            break; 
     222        case E_Midi: 
     223            if(b == E_RingBuffer) type_is_ok=true; 
     224            break; 
     225        case E_Control: 
     226            break; 
     227        default: 
     228            break; 
     229    } 
     230    if(!type_is_ok) { 
     231        debugFatal("Buffer type not supported by this type of port!\n"); 
     232        return false; 
     233    } 
     234    m_BufferType=b; 
     235    return true; 
     236} 
     237 
     238bool Port::useExternalBuffer(bool b) { 
     239    // If called on an initialised stream but the request isn't for a change silently 
     240    // allow it (relied on by C API as used by jack backend driver) 
     241    if (m_State==E_Initialized && m_use_external_buffer==b) 
     242        return true; 
     243 
     244    debugOutput( DEBUG_LEVEL_VERBOSE, "Setting external buffer use to %d for port %s\n",(int)b,m_Name.c_str()); 
     245 
     246    if (m_State != E_Created) { 
     247        debugFatal("Port (%s) not in E_Created state: %d\n",m_Name.c_str(),m_State); 
     248        return false; 
     249    } 
     250    m_use_external_buffer=b; 
     251    return true; 
     252} 
     253 
     254// buffer handling api's for pointer buffers 
     255/** 
     256 * Get the buffer address (being the external or the internal one). 
     257 * 
     258 * @param buff 
     259 */ 
     260void *Port::getBufferAddress() { 
     261    assert(m_BufferType==E_PointerBuffer); 
     262    return m_buffer; 
     263}; 
     264 
     265/** 
     266 * Set the external buffer address. 
     267 * only call this when you have specified that you will use 
     268 * an external buffer before doing the init() 
     269 * 
     270 * @param buff 
     271 */ 
     272void Port::setExternalBufferAddress(void *buff) { 
     273    assert(m_BufferType==E_PointerBuffer); 
     274    assert(m_use_external_buffer); // don't call this with an internal buffer! 
     275    m_buffer=buff; 
     276}; 
     277 
     278// buffer handling api's for ringbuffers 
     279bool Port::writeEvent(void *event) { 
     280 
     281#ifdef DEBUG 
     282    if (m_State != E_Initialized) { 
     283        debugFatal("Port (%s) not in E_Initialized state: %d\n",m_Name.c_str(),m_State); 
     284        return false; 
     285    } 
     286     
     287    if(m_BufferType!=E_RingBuffer) { 
     288        debugError("operation not allowed on non E_RingBuffer ports\n"); 
     289        show(); 
     290        return false; 
     291    } 
     292    assert(m_ringbuffer); 
     293#endif 
     294 
     295    debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "Writing event %08X with size %d to port %s\n",*((quadlet_t *)event),m_eventsize, m_Name.c_str()); 
     296 
     297    return (ffado_ringbuffer_write(m_ringbuffer, (char *)event, m_eventsize)==m_eventsize); 
     298} 
     299 
     300bool Port::readEvent(void *event) { 
     301 
     302#ifdef DEBUG 
     303    if (m_State != E_Initialized) { 
     304        debugFatal("Port (%s) not in E_Initialized state: %d\n",m_Name.c_str(),m_State); 
     305        return false; 
     306    } 
     307     
     308    if(m_BufferType!=E_RingBuffer) { 
     309        debugError("operation not allowed on non E_RingBuffer ports\n"); 
     310        show(); 
     311        return false; 
     312    } 
     313    assert(m_ringbuffer); 
     314#endif 
     315 
     316     
     317    unsigned int read=ffado_ringbuffer_read(m_ringbuffer, (char *)event, m_eventsize); 
     318     
     319    debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "Reading event %X with size %d from port %s\n",*((quadlet_t *)event),m_eventsize,m_Name.c_str()); 
     320 
     321 
     322    return (read==m_eventsize); 
     323} 
     324 
     325int Port::writeEvents(void *event, unsigned int nevents) { 
     326 
     327#ifdef DEBUG 
     328    if (m_State != E_Initialized) { 
     329        debugFatal("Port (%s) not in E_Initialized state: %d\n",m_Name.c_str(),m_State); 
     330        return false; 
     331    } 
     332     
     333    if(m_BufferType!=E_RingBuffer) { 
     334        debugError("operation not allowed on non E_RingBuffer ports\n"); 
     335        show(); 
     336        return false; 
     337    } 
     338    assert(m_ringbuffer); 
     339#endif 
     340 
     341 
     342    unsigned int bytes2write=m_eventsize*nevents; 
     343 
     344    unsigned int written=ffado_ringbuffer_write(m_ringbuffer, (char *)event,bytes2write)/m_eventsize; 
     345 
     346#ifdef DEBUG 
     347    if(written) { 
     348        unsigned int i=0; 
     349        quadlet_t * tmp=(quadlet_t *)event; 
     350        debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "Written %d events (",written); 
     351        for (i=0;i<written;i++) { 
     352            debugOutputShort(DEBUG_LEVEL_VERY_VERBOSE, "%X ", *(tmp+i)); 
     353        } 
     354        debugOutputShort(DEBUG_LEVEL_VERY_VERBOSE, ") to port %s\n",m_Name.c_str()); 
     355    } 
     356#endif 
     357 
     358    return written; 
     359 
     360} 
     361 
     362int Port::readEvents(void *event, unsigned int nevents) { 
     363 
     364#ifdef DEBUG 
     365    if (m_State != E_Initialized) { 
     366        debugFatal("Port (%s) not in E_Initialized state: %d\n",m_Name.c_str(),m_State); 
     367        return false; 
     368    } 
     369    if(m_BufferType!=E_RingBuffer) { 
     370        debugError("operation not allowed on non E_RingBuffer ports\n"); 
     371        show(); 
     372        return false; 
     373    } 
     374    assert(m_ringbuffer); 
     375#endif 
     376 
     377 
     378    unsigned int bytes2read=m_eventsize*nevents; 
     379 
     380    unsigned int read=ffado_ringbuffer_read(m_ringbuffer, (char *)event, bytes2read)/m_eventsize; 
     381 
     382#ifdef DEBUG 
     383    if(read) { 
     384        unsigned int i=0; 
     385        quadlet_t * tmp=(quadlet_t *)event; 
     386        debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "Read %d events (",read); 
     387        for (i=0;i<read;i++) { 
     388            debugOutputShort(DEBUG_LEVEL_VERY_VERBOSE, "%X ", *(tmp+i)); 
     389        } 
     390        debugOutputShort(DEBUG_LEVEL_VERY_VERBOSE, ") from port %s\n",m_Name.c_str()); 
     391    } 
     392#endif 
     393 
     394    return read; 
     395} 
     396 
     397/* rate control */ 
     398bool Port::canRead() { 
     399    bool byte_present_in_buffer; 
     400 
     401    bool retval=false; 
     402 
     403    assert(m_ringbuffer); 
     404 
     405    byte_present_in_buffer=(ffado_ringbuffer_read_space(m_ringbuffer) >= m_eventsize); 
     406 
     407    if(byte_present_in_buffer) { 
     408 
     409        if(!m_do_ratecontrol) { 
     410            return true; 
     411        } 
     412 
     413        if(m_rate_counter <= 0) { 
     414            // update the counter 
     415            if(m_average_ratecontrol) { 
     416                m_rate_counter += m_event_interval; 
     417                assert(m_rate_counter<m_event_interval); 
     418            } else { 
     419                m_rate_counter = m_event_interval; 
     420            } 
     421 
     422            retval=true; 
     423        } else { 
     424            debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "Rate limit (%s)! rate_counter=%d \n",m_Name.c_str(),m_rate_counter); 
     425 
     426        } 
     427    } 
     428 
     429 
     430    m_rate_counter -= m_slot_interval; 
     431 
     432    // we have to limit the decrement of the ratecounter somehow. 
     433    // m_rate_counter_minimum is initialized when enabling ratecontrol 
     434    if(m_rate_counter < m_rate_counter_minimum) { 
     435        m_rate_counter = m_rate_counter_minimum; 
     436    } 
     437 
     438    return retval; 
     439} 
     440 
     441bool Port::useRateControl(bool use, unsigned int slot_interval, 
     442                                unsigned int event_interval, bool average) { 
     443 
     444    if (use) { 
     445        debugOutput(DEBUG_LEVEL_VERBOSE, "Enabling rate control for port %s...\n",m_Name.c_str()); 
     446        if(slot_interval>event_interval) { 
     447            debugWarning("Rate control not needed!\n",m_Name.c_str()); 
     448            m_do_ratecontrol=false; 
     449            return false; 
     450        } 
     451        if(slot_interval==0) { 
     452            debugFatal("Cannot have slot interval == 0!\n"); 
     453            m_do_ratecontrol=false; 
     454            return false; 
     455        } 
     456        if(event_interval==0) { 
     457            debugFatal("Cannot have event interval == 0!\n"); 
     458            m_do_ratecontrol=false; 
     459            return false; 
     460        } 
     461        m_do_ratecontrol=use; 
     462        m_event_interval=event_interval; 
     463        m_slot_interval=slot_interval; 
     464        m_rate_counter=0; 
     465 
     466        // NOTE: pretty arbitrary, but in average mode this limits the peak stream rate 
     467        m_rate_counter_minimum=-(2*event_interval); 
     468 
     469        m_average_ratecontrol=average; 
     470 
     471    } else { 
     472        debugOutput(DEBUG_LEVEL_VERBOSE, "Disabling rate control for port %s...\n",m_Name.c_str()); 
     473        m_do_ratecontrol=use; 
     474    } 
     475    return true; 
     476} 
     477 
     478/// Enable the port. (this can be called anytime) 
     479void 
     480Port::enable()  { 
     481    debugOutput(DEBUG_LEVEL_VERBOSE, "Enabling port %s...\n",m_Name.c_str()); 
     482    m_disabled=false; 
     483}; 
     484 
     485/// Disable the port. (this can be called anytime) 
     486void 
     487Port::disable() { 
     488    debugOutput(DEBUG_LEVEL_VERBOSE, "Disabling port %s...\n",m_Name.c_str()); 
     489    m_disabled=false; 
     490}; 
     491 
     492 
     493/* Private functions */ 
     494 
     495bool Port::allocateInternalBuffer() { 
     496    int event_size=getEventSize(); 
     497 
     498    debugOutput(DEBUG_LEVEL_VERBOSE, 
     499                "Allocating internal buffer of %d events with size %d (%s)\n", 
     500                m_buffersize, event_size, m_Name.c_str()); 
     501 
     502    if(m_buffer) { 
     503        debugWarning("already has an internal buffer attached, re-allocating\n"); 
     504        freeInternalBuffer(); 
     505    } 
     506 
     507    m_buffer=calloc(m_buffersize,event_size); 
     508    if (!m_buffer) { 
     509        debugFatal("could not allocate internal buffer\n"); 
     510        m_buffersize=0; 
     511        return false; 
     512    } 
     513 
     514    return true; 
     515} 
     516 
     517void Port::freeInternalBuffer() { 
     518    debugOutput(DEBUG_LEVEL_VERBOSE, 
     519                "Freeing internal buffer (%s)\n",m_Name.c_str()); 
     520 
     521    if(m_buffer) { 
     522        free(m_buffer); 
     523        m_buffer=0; 
     524    } 
     525} 
     526 
     527bool Port::allocateInternalRingBuffer() { 
     528    int event_size=getEventSize(); 
     529 
     530    debugOutput(DEBUG_LEVEL_VERBOSE, 
     531                "Allocating internal buffer of %d events with size %d (%s)\n", 
     532                m_buffersize, event_size, m_Name.c_str()); 
     533 
     534    if(m_ringbuffer) { 
     535        debugWarning("already has an internal ringbuffer attached, re-allocating\n"); 
     536        freeInternalRingBuffer(); 
     537    } 
     538 
     539    m_ringbuffer=ffado_ringbuffer_create(m_buffersize * event_size); 
     540    if (!m_ringbuffer) { 
     541        debugFatal("could not allocate internal ringbuffer\n"); 
     542        m_buffersize=0; 
     543        return false; 
     544    } 
     545 
     546    return true; 
     547} 
     548 
     549void Port::freeInternalRingBuffer() { 
     550    debugOutput(DEBUG_LEVEL_VERBOSE, 
     551                "Freeing internal ringbuffer (%s)\n",m_Name.c_str()); 
     552 
     553    if(m_ringbuffer) { 
     554        ffado_ringbuffer_free(m_ringbuffer); 
     555        m_ringbuffer=0; 
     556    } 
     557} 
     558 
    113559void Port::show() { 
    114560    debugOutput(DEBUG_LEVEL_VERBOSE,"Name          : %s\n", m_Name.c_str()); 
     
    129575} 
    130576 
    131 bool Port::setName(std::string name) { 
    132     debugOutput( DEBUG_LEVEL_VERBOSE, "Setting name to %s for port %s\n",name.c_str(),m_Name.c_str()); 
    133  
    134     if (m_State != E_Created) { 
    135         debugFatal("Port (%s) not in E_Created state: %d\n",m_Name.c_str(),m_State); 
    136         return false; 
    137     } 
    138  
    139     m_Name=name; 
    140  
    141     return true; 
    142 
    143  
    144 bool Port::setBufferSize(unsigned int newsize) { 
    145     debugOutput( DEBUG_LEVEL_VERBOSE, "Setting buffersize to %d for port %s\n",newsize,m_Name.c_str()); 
    146     if (m_State != E_Created) { 
    147         debugFatal("Port (%s) not in E_Created state: %d\n",m_Name.c_str(),m_State); 
    148         return false; 
    149     } 
    150  
    151     m_buffersize=newsize; 
    152     return true; 
    153  
    154 
    155  
    156 unsigned int Port::getEventSize() { 
    157     switch (m_DataType) { 
    158         case E_Float: 
    159             return sizeof(float); 
    160         case E_Int24: // 24 bit 2's complement, packed in a 32bit integer (LSB's) 
    161             return sizeof(uint32_t); 
    162         case E_MidiEvent: 
    163             return sizeof(uint32_t); 
    164         default: 
    165             return 0; 
    166     } 
    167 
    168  
    169 bool Port::setDataType(enum E_DataType d) { 
    170     debugOutput( DEBUG_LEVEL_VERBOSE, "Setting datatype to %d for port %s\n",(int) d,m_Name.c_str()); 
    171     if (m_State != E_Created) { 
    172         debugFatal("Port (%s) not in E_Created state: %d\n",m_Name.c_str(),m_State); 
    173         return false; 
    174     } 
    175  
    176     // do some sanity checks 
    177     bool type_is_ok=false; 
    178     switch (m_PortType) { 
    179         case E_Audio: 
    180             if(d == E_Int24) type_is_ok=true; 
    181             if(d == E_Float) type_is_ok=true; 
    182             break; 
    183         case E_Midi: 
    184             if(d == E_MidiEvent) type_is_ok=true; 
    185             break; 
    186         case E_Control: 
    187             if(d == E_Default) type_is_ok=true; 
    188             break; 
    189         default: 
    190             break; 
    191     } 
    192  
    193     if(!type_is_ok) { 
    194         debugFatal("Datatype not supported by this type of port!\n"); 
    195         return false; 
    196     } 
    197  
    198     m_DataType=d; 
    199     return true; 
    200 
    201  
    202 bool Port::setSignalType(enum E_SignalType s) { 
    203     debugOutput( DEBUG_LEVEL_VERBOSE, "Setting signaltype to %d for port %s\n",(int)s,m_Name.c_str()); 
    204     if (m_State != E_Created) { 
    205         debugFatal("Port (%s) not in E_Created state: %d\n",m_Name.c_str(),m_State); 
    206         return false; 
    207     } 
    208  
    209     // do some sanity checks 
    210     bool type_is_ok=false; 
    211     switch (m_PortType) { 
    212         case E_Audio: 
    213             if(s == E_PeriodSignalled) type_is_ok=true; 
    214             break; 
    215         case E_Midi: 
    216             if(s == E_PacketSignalled) type_is_ok=true; 
    217             break; 
    218         case E_Control: 
    219             if(s == E_PeriodSignalled) type_is_ok=true; 
    220             break; 
    221         default: 
    222             break; 
    223     } 
    224  
    225     if(!type_is_ok) { 
    226         debugFatal("Signalling type not supported by this type of port!\n"); 
    227         return false; 
    228     } 
    229  
    230     m_SignalType=s; 
    231     return true; 
    232  
    233 
    234  
    235 bool Port::setBufferType(enum E_BufferType b) { 
    236     debugOutput( DEBUG_LEVEL_VERBOSE, "Setting buffer type to %d for port %s\n",(int)b,m_Name.c_str()); 
    237     if (m_State != E_Created) { 
    238         debugFatal("Port (%s) not in E_Created state: %d\n",m_Name.c_str(),m_State); 
    239         return false; 
    240     } 
    241  
    242     // do some sanity checks 
    243     bool type_is_ok=false; 
    244     switch (m_PortType) { 
    245         case E_Audio: 
    246             if(b == E_PointerBuffer) type_is_ok=true; 
    247             break; 
    248         case E_Midi: 
    249             if(b == E_RingBuffer) type_is_ok=true; 
    250             break; 
    251         case E_Control: 
    252             break; 
    253         default: 
    254             break; 
    255     } 
    256  
    257     if(!type_is_ok) { 
    258         debugFatal("Buffer type not supported by this type of port!\n"); 
    259         return false; 
    260     } 
    261  
    262     m_BufferType=b; 
    263     return true; 
    264  
    265 
    266  
    267 bool Port::useExternalBuffer(bool b) { 
    268  
    269     // If called on an initialised stream but the request isn't for a change silently 
    270     // allow it (relied on by C API as used by jack backend driver) 
    271     if (m_State==E_Initialized && m_use_external_buffer==b) 
    272         return true; 
    273  
    274     debugOutput( DEBUG_LEVEL_VERBOSE, "Setting external buffer use to %d for port %s\n",(int)b,m_Name.c_str()); 
    275  
    276     if (m_State != E_Created) { 
    277         debugFatal("Port (%s) not in E_Created state: %d\n",m_Name.c_str(),m_State); 
    278         return false; 
    279     } 
    280  
    281     m_use_external_buffer=b; 
    282     return true; 
    283 
    284  
    285 // buffer handling api's for pointer buffers 
    286 /** 
    287  * Get the buffer address (being the external or the internal one). 
    288  * 
    289  * @param buff 
    290  */ 
    291 void *Port::getBufferAddress() { 
    292     assert(m_BufferType==E_PointerBuffer); 
    293     return m_buffer; 
    294 }; 
    295  
    296 /** 
    297  * Set the external buffer address. 
    298  * only call this when you have specified that you will use 
    299  * an external buffer before doing the init() 
    300  * 
    301  * @param buff 
    302  */ 
    303 void Port::setExternalBufferAddress(void *buff) { 
    304     assert(m_BufferType==E_PointerBuffer); 
    305     assert(m_use_external_buffer); // don't call this with an internal buffer! 
    306     m_buffer=buff; 
    307 }; 
    308  
    309 // buffer handling api's for ringbuffers 
    310 bool Port::writeEvent(void *event) { 
    311  
    312 #ifdef DEBUG 
    313     if (m_State != E_Initialized) { 
    314         debugFatal("Port (%s) not in E_Initialized state: %d\n",m_Name.c_str(),m_State); 
    315         return false; 
    316     } 
    317      
    318     if(m_BufferType!=E_RingBuffer) { 
    319         debugError("operation not allowed on non E_RingBuffer ports\n"); 
    320         show(); 
    321         return false; 
    322     } 
    323     assert(m_ringbuffer); 
    324 #endif 
    325  
    326     debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "Writing event %08X with size %d to port %s\n",*((quadlet_t *)event),m_eventsize, m_Name.c_str()); 
    327  
    328     return (ffado_ringbuffer_write(m_ringbuffer, (char *)event, m_eventsize)==m_eventsize); 
    329 
    330  
    331 bool Port::readEvent(void *event) { 
    332  
    333 #ifdef DEBUG 
    334     if (m_State != E_Initialized) { 
    335         debugFatal("Port (%s) not in E_Initialized state: %d\n",m_Name.c_str(),m_State); 
    336         return false; 
    337     } 
    338      
    339     if(m_BufferType!=E_RingBuffer) { 
    340         debugError("operation not allowed on non E_RingBuffer ports\n"); 
    341         show(); 
    342         return false; 
    343     } 
    344     assert(m_ringbuffer); 
    345 #endif 
    346  
    347      
    348     unsigned int read=ffado_ringbuffer_read(m_ringbuffer, (char *)event, m_eventsize); 
    349      
    350     debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "Reading event %X with size %d from port %s\n",*((quadlet_t *)event),m_eventsize,m_Name.c_str()); 
    351  
    352  
    353     return (read==m_eventsize); 
    354 
    355  
    356 int Port::writeEvents(void *event, unsigned int nevents) { 
    357  
    358 #ifdef DEBUG 
    359     if (m_State != E_Initialized) { 
    360         debugFatal("Port (%s) not in E_Initialized state: %d\n",m_Name.c_str(),m_State); 
    361         return false; 
    362     } 
    363      
    364     if(m_BufferType!=E_RingBuffer) { 
    365         debugError("operation not allowed on non E_RingBuffer ports\n"); 
    366         show(); 
    367         return false; 
    368     } 
    369     assert(m_ringbuffer); 
    370 #endif 
    371  
    372  
    373     unsigned int bytes2write=m_eventsize*nevents; 
    374  
    375     unsigned int written=ffado_ringbuffer_write(m_ringbuffer, (char *)event,bytes2write)/m_eventsize; 
    376  
    377 #ifdef DEBUG 
    378     if(written) { 
    379         unsigned int i=0; 
    380         quadlet_t * tmp=(quadlet_t *)event; 
    381         debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "Written %d events (",written); 
    382         for (i=0;i<written;i++) { 
    383             debugOutputShort(DEBUG_LEVEL_VERY_VERBOSE, "%X ", *(tmp+i)); 
    384         } 
    385         debugOutputShort(DEBUG_LEVEL_VERY_VERBOSE, ") to port %s\n",m_Name.c_str()); 
    386     } 
    387 #endif 
    388  
    389     return written; 
    390  
    391 
    392  
    393 int Port::readEvents(void *event, unsigned int nevents) { 
    394  
    395 #ifdef DEBUG 
    396     if (m_State != E_Initialized) { 
    397         debugFatal("Port (%s) not in E_Initialized state: %d\n",m_Name.c_str(),m_State); 
    398         return false; 
    399     } 
    400     if(m_BufferType!=E_RingBuffer) { 
    401         debugError("operation not allowed on non E_RingBuffer ports\n"); 
    402         show(); 
    403         return false; 
    404     } 
    405     assert(m_ringbuffer); 
    406 #endif 
    407  
    408  
    409     unsigned int bytes2read=m_eventsize*nevents; 
    410  
    411     unsigned int read=ffado_ringbuffer_read(m_ringbuffer, (char *)event, bytes2read)/m_eventsize; 
    412  
    413 #ifdef DEBUG 
    414     if(read) { 
    415         unsigned int i=0; 
    416         quadlet_t * tmp=(quadlet_t *)event; 
    417         debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "Read %d events (",read); 
    418         for (i=0;i<read;i++) { 
    419             debugOutputShort(DEBUG_LEVEL_VERY_VERBOSE, "%X ", *(tmp+i)); 
    420         } 
    421         debugOutputShort(DEBUG_LEVEL_VERY_VERBOSE, ") from port %s\n",m_Name.c_str()); 
    422     } 
    423 #endif 
    424  
    425     return read; 
    426 
    427  
    428 /* rate control */ 
    429 bool Port::canRead() { 
    430     bool byte_present_in_buffer; 
    431  
    432     bool retval=false; 
    433  
    434     assert(m_ringbuffer); 
    435  
    436     byte_present_in_buffer=(ffado_ringbuffer_read_space(m_ringbuffer) >= m_eventsize); 
    437  
    438     if(byte_present_in_buffer) { 
    439  
    440         if(!m_do_ratecontrol) { 
    441             return true; 
    442         } 
    443  
    444         if(m_rate_counter <= 0) { 
    445             // update the counter 
    446             if(m_average_ratecontrol) { 
    447                 m_rate_counter += m_event_interval; 
    448                 assert(m_rate_counter<m_event_interval); 
    449             } else { 
    450                 m_rate_counter = m_event_interval; 
    451             } 
    452  
    453             retval=true; 
    454         } else { 
    455             debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "Rate limit (%s)! rate_counter=%d \n",m_Name.c_str(),m_rate_counter); 
    456  
    457         } 
    458     } 
    459  
    460  
    461     m_rate_counter -= m_slot_interval; 
    462  
    463     // we have to limit the decrement of the ratecounter somehow. 
    464     // m_rate_counter_minimum is initialized when enabling ratecontrol 
    465     if(m_rate_counter < m_rate_counter_minimum) { 
    466         m_rate_counter = m_rate_counter_minimum; 
    467     } 
    468  
    469     return retval; 
    470 
    471  
    472 bool Port::useRateControl(bool use, unsigned int slot_interval, 
    473                                 unsigned int event_interval, bool average) { 
    474  
    475     if (use) { 
    476         debugOutput(DEBUG_LEVEL_VERBOSE, "Enabling rate control for port %s...\n",m_Name.c_str()); 
    477         if(slot_interval>event_interval) { 
    478             debugWarning("Rate control not needed!\n",m_Name.c_str()); 
    479             m_do_ratecontrol=false; 
    480             return false; 
    481         } 
    482         if(slot_interval==0) { 
    483             debugFatal("Cannot have slot interval == 0!\n"); 
    484             m_do_ratecontrol=false; 
    485             return false; 
    486         } 
    487         if(event_interval==0) { 
    488             debugFatal("Cannot have event interval == 0!\n"); 
    489             m_do_ratecontrol=false; 
    490             return false; 
    491         } 
    492         m_do_ratecontrol=use; 
    493         m_event_interval=event_interval; 
    494         m_slot_interval=slot_interval; 
    495         m_rate_counter=0; 
    496  
    497         // NOTE: pretty arbitrary, but in average mode this limits the peak stream rate 
    498         m_rate_counter_minimum=-(2*event_interval); 
    499  
    500         m_average_ratecontrol=average; 
    501  
    502     } else { 
    503         debugOutput(DEBUG_LEVEL_VERBOSE, "Disabling rate control for port %s...\n",m_Name.c_str()); 
    504         m_do_ratecontrol=use; 
    505     } 
    506     return true; 
    507 
    508  
    509 /// Enable the port. (this can be called anytime) 
    510 void 
    511 Port::enable()  { 
    512     debugOutput(DEBUG_LEVEL_VERBOSE, "Enabling port %s...\n",m_Name.c_str()); 
    513     m_disabled=false; 
    514 }; 
    515  
    516 /// Disable the port. (this can be called anytime) 
    517 void 
    518 Port::disable() { 
    519     debugOutput(DEBUG_LEVEL_VERBOSE, "Disabling port %s...\n",m_Name.c_str()); 
    520     m_disabled=false; 
    521 }; 
    522  
    523  
    524 /* Private functions */ 
    525  
    526 bool Port::allocateInternalBuffer() { 
    527     int event_size=getEventSize(); 
    528  
    529     debugOutput(DEBUG_LEVEL_VERBOSE, 
    530                 "Allocating internal buffer of %d events with size %d (%s)\n", 
    531                 m_buffersize, event_size, m_Name.c_str()); 
    532  
    533     if(m_buffer) { 
    534         debugWarning("already has an internal buffer attached, re-allocating\n"); 
    535         freeInternalBuffer(); 
    536     } 
    537  
    538     m_buffer=calloc(m_buffersize,event_size); 
    539     if (!m_buffer) { 
    540         debugFatal("could not allocate internal buffer\n"); 
    541         m_buffersize=0; 
    542         return false; 
    543     } 
    544  
    545     return true; 
    546 
    547  
    548 void Port::freeInternalBuffer() { 
    549     debugOutput(DEBUG_LEVEL_VERBOSE, 
    550                 "Freeing internal buffer (%s)\n",m_Name.c_str()); 
    551  
    552     if(m_buffer) { 
    553         free(m_buffer); 
    554         m_buffer=0; 
    555     } 
    556 
    557  
    558 bool Port::allocateInternalRingBuffer() { 
    559     int event_size=getEventSize(); 
    560  
    561     debugOutput(DEBUG_LEVEL_VERBOSE, 
    562                 "Allocating internal buffer of %d events with size %d (%s)\n", 
    563                 m_buffersize, event_size, m_Name.c_str()); 
    564  
    565     if(m_ringbuffer) { 
    566         debugWarning("already has an internal ringbuffer attached, re-allocating\n"); 
    567         freeInternalRingBuffer(); 
    568     } 
    569  
    570     m_ringbuffer=ffado_ringbuffer_create(m_buffersize * event_size); 
    571     if (!m_ringbuffer) { 
    572         debugFatal("could not allocate internal ringbuffer\n"); 
    573         m_buffersize=0; 
    574         return false; 
    575     } 
    576  
    577     return true; 
    578 
    579  
    580 void Port::freeInternalRingBuffer() { 
    581     debugOutput(DEBUG_LEVEL_VERBOSE, 
    582                 "Freeing internal ringbuffer (%s)\n",m_Name.c_str()); 
    583  
    584     if(m_ringbuffer) { 
    585         ffado_ringbuffer_free(m_ringbuffer); 
    586         m_ringbuffer=0; 
    587     } 
    588 
    589  
    590 
     577