Logo Search packages:      
Sourcecode: kaffeine version File versions

dvbsection.cpp

/***************************************************************************
                          dvbsection.cpp  -  description
                             -------------------
    begin                : Tue Dec 16 2003
    copyright            : (C) 2003-2005 by Christophe Thommeret
    email                : hftom@free.fr
    last modified        : $Date: 2005/02/15 08:18:35 $ by $Author: hftom $
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#include <klocale.h>

#include "dvbsection.h"
#include "gdvb.h"



DVBsection::DVBsection( bool *ok, int card )
{
      demuxer = "";
      *ok = true;
      QString s = QString("/dev/dvb/adapter%1/demux0").arg( card );

      if ((fdDemux = open( s, O_RDWR /*| O_NONBLOCK*/)) < 0) {
            perror ("open failed");
            *ok = false;
      }

      isRunning = false;
}



DVBsection::DVBsection( bool *ok, int card, bool /*openDemux*/ )
{
      *ok = true;
      demuxer = QString("/dev/dvb/adapter%1/demux0").arg( card );
      isRunning = false;
}




DVBsection::~DVBsection()
{
      if ( demuxer=="" ) {
            close( fdDemux );
      }
}



bool DVBsection::openFilter( int pid, int tid, int timeout, bool checkcrc )
{
      if ((fdDemux = open( demuxer, O_RDWR /*| O_NONBLOCK*/)) < 0) {
            perror ("open failed");
            return false;
      }
      if ( !setFilter( pid, tid, timeout, checkcrc ) ) return false;
      return true;
}



bool DVBsection::setFilter( int pid, int tid, int timeout, bool checkcrc )
{
      struct dmx_sct_filter_params sctfilter;

      memset( &sctfilter, 0, sizeof( sctfilter ) );

      sctfilter.pid = pid;
      if ( tid<256 && tid>0 ) {
            sctfilter.filter.filter[0] = tid;
            sctfilter.filter.mask[0] = 0xff;
      }
      sctfilter.flags = DMX_IMMEDIATE_START;
      if ( checkcrc ) sctfilter.flags|= DMX_CHECK_CRC;
      sctfilter.timeout = timeout;

      if ( ioctl( fdDemux, DMX_SET_FILTER, &sctfilter ) < 0 ) {
            perror ( "ioctl DMX_SET_FILTER failed" );
            return false;
      }
      return true;
}



void DVBsection::closeFilter()
{
      ioctl( fdDemux, DMX_STOP );
      close( fdDemux );
}



void DVBsection::stopFilter()
{
      ioctl( fdDemux, DMX_STOP );
}



unsigned int DVBsection::getBits( unsigned char *b, int offbits, int nbits )
{
      int i, nbytes;
      unsigned int ret = 0;
      unsigned char *buf;

      buf = b+(offbits/8);
      offbits %=8;
      nbytes = (offbits+nbits)/8;
      if ( ((offbits+nbits)%8)>0 ) nbytes++;
      for ( i=0; i<nbytes; i++ ) ret += buf[i]<<((nbytes-i-1)*8);
      i = (4-nbytes)*8+offbits;
      ret = ((ret<<i)>>i)>>((nbytes*8)-nbits-offbits);

      return ret;
}



QString DVBsection::getText( unsigned char *buf, int length )
{
      unsigned int index=0;
      QString s;
      int i;

      if ( length==0 ) return "";

      if ( buf[0]<0x10 ) index=1;

      for ( i=index; i<length; i++ ) {  // removes control codes
            if ( buf[i]>=0x20 && (buf[i]<0x80 || buf[i]>0x9f) ) s+= buf[i];
      }

      return s.stripWhiteSpace();
}



QString DVBsection::langDesc( unsigned char* buf )
{
      char c[4];
      QString s;

      memset( mempcpy( c, buf+2, 3 ), 0, 1 );
      s = c;
      return s;
}



QTime DVBsection::getTime( unsigned char *buf )
{
      int h, m, s;

      h = ((getBits(buf,0,4)*10)+getBits(buf,4,4))%24;
      m = ((getBits(buf,8,4)*10)+getBits(buf,12,4))%60;
      s = ((getBits(buf,16,4)*10)+getBits(buf,20,4))%60;
      return QTime( h, m, s );
}



QDate DVBsection::getDate( unsigned char *buf )
{
      int i, j, m, D, Y, M, k, mjd;

      mjd = getBits(buf,0,16);
      i = (int)((mjd-15078.2)/365.25);
      j = (int)(i*365.25);
      m = (int)((mjd-14956.1-j)/30.6001);
      D = mjd-14956-j-(int)(m*30.6001);
      if ( m==14 || m==15 ) k = 1;
      else k = 0;
      Y = i+k+1900;
      M = m-1-k*12;

      return QDate( (Y>=1970)?Y:1970, (M>0 && M<13)?M:1, (D>0 && D<32)?D:1 );
}



QDateTime DVBsection::getDateTime( unsigned char *buf )
{
      int hh, mm, ss;
      int i, j, m, D, Y, M, k, mjd;
      int sec;

      mjd = getBits(buf,0,16);
      i = (int)((mjd-15078.2)/365.25);
      j = (int)(i*365.25);
      m = (int)((mjd-14956.1-j)/30.6001);
      D = mjd-14956-j-(int)(m*30.6001);
      if ( m==14 || m==15 ) k = 1;
      else k = 0;
      Y = i+k+1900;
      M = m-1-k*12;

      hh = ((getBits(buf+2,0,4)*10)+getBits(buf+2,4,4))%24;
      mm = ((getBits(buf+2,8,4)*10)+getBits(buf+2,12,4))%60;
      ss = ((getBits(buf+2,16,4)*10)+getBits(buf+2,20,4))%60;

      QDateTime dt( QDate( (Y>=1970)?Y:1970, (M>0 && M<13)?M:1, (D>0 && D<32)?D:1 ), QTime( hh, mm, ss ) );
      QDateTime u( QDate( 1970, 1, 1 ), QTime( 0, 0, 0 ) );
      sec = u.secsTo( dt );
      u.setTime_t( sec ); // UTC to local
      return u;
}

Generated by  Doxygen 1.6.0   Back to index