예제 #1
0
파일: autoconf.c 프로젝트: Maescool/MuMuDVB
/** @brief This function is called when a new packet is there and the autoconf is not finished*/
int autoconf_new_packet(int pid, unsigned char *ts_packet, autoconf_parameters_t *autoconf_vars, fds_t *fds, mumudvb_chan_and_pids_t *chan_and_pids, tuning_parameters_t *tuneparams, multicast_parameters_t *multicast_vars,  unicast_parameters_t *unicast_vars, int server_id)
{
  int iRet=0;
  if(autoconf_vars->autoconfiguration==AUTOCONF_MODE_FULL) //Full autoconfiguration, we search the channels and their names
  {
    if(pid==0) //PAT : contains the services identifiers and the PMT PID for each service
    {
      if(get_ts_packet(ts_packet,autoconf_vars->autoconf_temp_pat))
      {
        if(autoconf_read_pat(autoconf_vars))
        {
          log_message( log_module, MSG_DEBUG,"It seems that we have finished to get the services list\n");
          //we finish full autoconfiguration
          iRet = autoconf_finish_full(chan_and_pids, autoconf_vars, multicast_vars, tuneparams, fds, unicast_vars, server_id);
        }
      }
    }
    else if(pid==17) //SDT : contains the names of the services
    {
      if(get_ts_packet(ts_packet,autoconf_vars->autoconf_temp_sdt))
      {
        autoconf_read_sdt(autoconf_vars->autoconf_temp_sdt->data_full,autoconf_vars->autoconf_temp_sdt->len_full,autoconf_vars->services);
      }
    }
    else if(pid==PSIP_PID && tuneparams->fe_type==FE_ATSC) //PSIP : contains the names of the services
    {
      if(get_ts_packet(ts_packet,autoconf_vars->autoconf_temp_psip))
      {
        autoconf_read_psip(autoconf_vars);
      }
    }
  }
  else if(autoconf_vars->autoconfiguration==AUTOCONF_MODE_PIDS) //We have the channels and their PMT, we search the other pids
  {
    int curr_channel;
    for(curr_channel=0;curr_channel<MAX_CHANNELS;curr_channel++)
    {
      if((!chan_and_pids->channels[curr_channel].autoconfigurated) &&(chan_and_pids->channels[curr_channel].pmt_pid==pid)&& pid)
      {
        if(get_ts_packet(ts_packet,chan_and_pids->channels[curr_channel].pmt_packet))
	{
	  //Now we have the PMT, we parse it
          if(autoconf_read_pmt(chan_and_pids->channels[curr_channel].pmt_packet, &chan_and_pids->channels[curr_channel], tuneparams->card_dev_path, tuneparams->tuner, chan_and_pids->asked_pid, chan_and_pids->number_chan_asked_pid, fds)==0)
          {
            log_pids(log_module,&chan_and_pids->channels[curr_channel],curr_channel);

            chan_and_pids->channels[curr_channel].autoconfigurated=1;

            //We parse the NIT before finishing autoconfiguration
	      autoconf_vars->autoconfiguration=AUTOCONF_MODE_NIT;
            for (curr_channel = 0; curr_channel < chan_and_pids->number_of_channels; curr_channel++)
              if(!chan_and_pids->channels[curr_channel].autoconfigurated)
                autoconf_vars->autoconfiguration=AUTOCONF_MODE_PIDS;  //not finished we continue

            //if it's finished, we open the new descriptors and add the new filters
            if(autoconf_vars->autoconfiguration!=AUTOCONF_MODE_PIDS)
            {
              autoconf_set_channel_filt(tuneparams->card_dev_path, tuneparams->tuner, chan_and_pids, fds);
              //We free autoconf memory
              autoconf_freeing(autoconf_vars);
	      if(autoconf_vars->autoconfiguration==AUTOCONF_MODE_NIT)
		log_message( log_module, MSG_DETAIL,"We search for the NIT\n");
              else
                autoconf_definite_end(tuneparams->card, tuneparams->tuner, chan_and_pids, multicast_vars, unicast_vars);
            }
          }
        }
      }
    }
  }
  else if(autoconf_vars->autoconfiguration==AUTOCONF_MODE_NIT) //We search the NIT
  {
    if(pid==16) //NIT : Network Information Table
    {
      if(get_ts_packet(ts_packet,autoconf_vars->autoconf_temp_nit))
      {
	log_message( log_module, MSG_FLOOD,"New NIT\n");
        if(autoconf_read_nit(autoconf_vars, chan_and_pids->channels, chan_and_pids->number_of_channels)==0)
	{
	  autoconf_vars->autoconfiguration=0;
          int curr_channel;
          char lcn[4];
          int len=MAX_NAME_LEN;
          for(curr_channel=0;curr_channel<MAX_CHANNELS;curr_channel++)
          {
            if(chan_and_pids->channels[curr_channel].logical_channel_number)
            {
              sprintf(lcn,"%03d",chan_and_pids->channels[curr_channel].logical_channel_number);
              mumu_string_replace(chan_and_pids->channels[curr_channel].name,&len,0,"%lcn",lcn);
              sprintf(lcn,"%02d",chan_and_pids->channels[curr_channel].logical_channel_number);
              mumu_string_replace(chan_and_pids->channels[curr_channel].name,&len,0,"%2lcn",lcn);
            }
            else
            {
              mumu_string_replace(chan_and_pids->channels[curr_channel].name,&len,0,"%lcn","");
              mumu_string_replace(chan_and_pids->channels[curr_channel].name,&len,0,"%2lcn","");
            }
          }
	  free(autoconf_vars->autoconf_temp_nit);
	  autoconf_vars->autoconf_temp_nit=NULL;
          autoconf_definite_end(tuneparams->card, tuneparams->tuner, chan_and_pids, multicast_vars, unicast_vars);
	}
      }
    }
  }
  return Interrupted;
}
예제 #2
0
int main(void)
{
  int press_enter = PRESS_ENTER;

  //We initalise the logging parameters
  log_params.verbosity = 999;
  log_params.log_type=LOGGING_CONSOLE;


  log_message( log_module, MSG_INFO,"===================================================================\n");
  log_message( log_module, MSG_INFO,"Testing program for MuMuDVB\n");
  log_message( log_module, MSG_INFO,"===================================================================\n");

  /****************************  Very basic test ****************************************************/
  log_message( log_module, MSG_INFO,"===================================================================\n");
  log_message( log_module, MSG_INFO,"Display Ca system id 1\n" );
  log_message( log_module, MSG_INFO,"===================================================================\n");

  log_message( log_module, MSG_INFO,"%s\n\n" ,ca_sys_id_to_str(1));

  /****************************  Testing string compute *********************************************/
  log_message( log_module, MSG_INFO,"===================================================================\n");
  log_message( log_module, MSG_INFO,"Testing string compute %s\n",TEST_STRING_COMPUT );
  log_message( log_module, MSG_INFO,"===================================================================\n");

  int resultat;
  resultat=string_comput(TEST_STRING_COMPUT);
  if(resultat==TEST_STRING_COMPUT_RES)
    log_message( log_module, MSG_INFO,"%d  --  PASS\n\n" ,resultat);
  else
    log_message( log_module, MSG_INFO,"%d  --  FAIL\n\n" ,resultat);

  /************************************* Testing the SDT parser *************************************/
  char *files_sdt[NUM_FILES_TEST_READ_SDT]={FILES_TEST_READ_SDT_TS};
  for(int i_file=0;i_file<NUM_FILES_TEST_READ_SDT;i_file++)
  {
    log_message( log_module, MSG_INFO,"===================================================================\n");
    log_message( log_module, MSG_INFO,"Testing TS read for SDT - test %d on %d file %s\n", i_file+1, NUM_FILES_TEST_READ_SDT , files_sdt[i_file] );
    press_enter_func(press_enter);
    FILE *testfile;
    testfile=fopen (files_sdt[i_file], "r");
    if(testfile!=NULL)
      {
        //We read all the packets contained in the file
        unsigned char ts_packet_raw[TS_PACKET_SIZE];
        int num_sdt_read=0;
        mumudvb_ts_packet_t ts_packet_mumu;
        memset(&ts_packet_mumu, 0, sizeof(mumudvb_ts_packet_t));

        mumudvb_service_t services;
        memset(&services, 0, sizeof(mumudvb_service_t));
        //Just to make pthread happy
        pthread_mutex_init(&ts_packet_mumu.packetmutex,NULL);
        int iRet,pid;
        log_message( log_module, MSG_INFO,"File opened, reading packets\n" );
        while(fread(ts_packet_raw,TS_PACKET_SIZE,1, testfile) && num_sdt_read<NUM_READ_SDT)
          {
            /************ SYNC *************/
            if(ts_packet_raw[0] != 0x47)
              {
                log_message( log_module, MSG_INFO," !!!!!!!!!!!!! Sync error, we search for a new sync byte !!!!!!!!!!!!!!\n");
                unsigned char sync;
                //We search the next sync byte
                while(fread(&sync,1,1, testfile) && sync!=0x47);
                ts_packet_raw[0]=sync;
                //We found a "sync byte" we get the rest of the packet
                if(!fread(ts_packet_raw-1,TS_PACKET_SIZE-1,1, testfile))
                  continue;
                else
                  log_message( log_module, MSG_INFO," sync byte found :) \n");
              }
            // Get the PID of the received TS packet
            pid = HILO(((ts_header_t *)ts_packet_raw)->pid);
            if(pid != 17) continue;
            log_message( log_module, MSG_INFO,"New elementary (188bytes TS packet) pid %d continuity_counter %d",
                         pid,
                         ((ts_header_t *)ts_packet_raw)->continuity_counter );
            iRet=get_ts_packet(ts_packet_raw, &ts_packet_mumu);
            //If it's the beginning of a new packet we display some information
            if(((ts_header_t *)ts_packet_raw)->payload_unit_start_indicator)
              log_message(log_module, MSG_INFO, "First two bytes of the packet 0x%02x %02x",
                          ts_packet_mumu.data_partial[0],
                          ts_packet_mumu.data_partial[1]);

            if(iRet==1)//packet is parsed
              {
                log_message( log_module, MSG_INFO,"New packet -- parsing\n" );
                num_sdt_read++;
                autoconf_read_sdt(ts_packet_mumu.data_full,ts_packet_mumu.len_full,&services);
              }
          }
        log_message( log_module, MSG_INFO,"Final services list .... \n");
        autoconf_print_services(&services);
        log_message( log_module, MSG_INFO,"===================================================================\n");
        log_message( log_module, MSG_INFO,"Testing service sorting on this list\n" );
        press_enter_func(press_enter);
        autoconf_sort_services(&services);
        autoconf_print_services(&services);
        //We free starting at the next since the first is not malloc'ed
        autoconf_free_services(services.next);
        fclose(testfile);

      }
    else
      log_message( log_module, MSG_INFO,"Test file %s cannot be open : %s\n", files_sdt[i_file],strerror(errno) );

  }


  /************************************* Testing the NIT parser *************************************/
  char *files_nit[NUM_FILES_TEST_READ_NIT]={FILES_TEST_READ_NIT_TS};
  for(int i_file=0;i_file<NUM_FILES_TEST_READ_NIT;i_file++)
  {
    log_message( log_module, MSG_INFO,"===================================================================\n");
    log_message( log_module, MSG_INFO,"Testing TS read for NIT - test %d on %d file %s\n", i_file+1, NUM_FILES_TEST_READ_NIT , files_sdt[i_file] );
    press_enter_func(press_enter);
    FILE *testfile;
    testfile=fopen (files_nit[i_file], "r");
    if(testfile!=NULL)
      {
        //We read all the packets contained in the file
        unsigned char ts_packet_raw[TS_PACKET_SIZE];
        int num_nit_read=0;
        mumudvb_ts_packet_t ts_packet_mumu;
        memset(&ts_packet_mumu, 0, sizeof(mumudvb_ts_packet_t));

        mumudvb_service_t services;
        memset(&services, 0, sizeof(mumudvb_service_t));
        //Just to make pthread happy
        pthread_mutex_init(&ts_packet_mumu.packetmutex,NULL);
        int iRet,pid;
        log_message( log_module, MSG_INFO,"File opened, reading packets\n" );
        while(fread(ts_packet_raw,TS_PACKET_SIZE,1, testfile) && num_nit_read<NUM_READ_NIT)
          {
            /************ SYNC *************/
            if(ts_packet_raw[0] != 0x47)
              {
                log_message( log_module, MSG_INFO," !!!!!!!!!!!!! Sync error, we search for a new sync byte !!!!!!!!!!!!!!\n");
                unsigned char sync;
                //We search the next sync byte
                while(fread(&sync,1,1, testfile) && sync!=0x47);
                ts_packet_raw[0]=sync;
                //We found a "sync byte" we get the rest of the packet
                if(!fread(ts_packet_raw-1,TS_PACKET_SIZE-1,1, testfile))
                  continue;
                else
                  log_message( log_module, MSG_INFO," sync byte found :) \n");
              }
            // Get the PID of the received TS packet
            pid = HILO(((ts_header_t *)ts_packet_raw)->pid);
            if(pid != 16) continue;
            log_message( log_module, MSG_INFO,"New elementary (188bytes TS packet) pid %d continuity_counter %d",
                         pid,
                         ((ts_header_t *)ts_packet_raw)->continuity_counter );
            iRet=get_ts_packet(ts_packet_raw, &ts_packet_mumu);
            //If it's the beginning of a new packet we display some information
            if(((ts_header_t *)ts_packet_raw)->payload_unit_start_indicator)
              log_message(log_module, MSG_INFO, "First two bytes of the packet 0x%02x %02x",
                          ts_packet_mumu.data_partial[0],
                          ts_packet_mumu.data_partial[1]);

            if(iRet==1)//packet is parsed
              {
                log_message( log_module, MSG_INFO,"New packet -- parsing\n" );
                num_nit_read++;
                //autoconf_read_sdt(ts_packet_mumu.data_full,ts_packet_mumu.len_full,&services);
				parse_nit_descriptors(ts_packet_mumu.data_full,ts_packet_mumu.len_full);
              }
          }
        log_message( log_module, MSG_INFO,"===================================================================\n");
        fclose(testfile);
      }
    else
      log_message( log_module, MSG_INFO,"Test file %s cannot be open : %s\n", files_sdt[i_file],strerror(errno) );

  }


  /************************************* Testing the resistance to strange data *********************/
  log_message( log_module, MSG_INFO,"===================================================================\n");
  char *files_rand[NUM_FILES_TEST_READ_RAND]={FILES_TEST_READ_RAND};
  for(int i_file=0;i_file<NUM_FILES_TEST_READ_RAND;i_file++)
    {
      log_message( log_module, MSG_INFO,"Testing Resistance to bad packets file %d on %d : %s\n", i_file+1, NUM_FILES_TEST_READ_RAND, files_rand[i_file] );
      press_enter_func(press_enter);
      FILE *testfile;
      testfile=fopen (files_rand[i_file] , "r");
      if(testfile!=NULL)
	{
	  //We read all the packets contained in the file
	  unsigned char ts_packet_raw[TS_PACKET_SIZE];
	  int num_rand_read=0;
	  mumudvb_ts_packet_t ts_packet_mumu;
	  //Just to make pthread happy
	  pthread_mutex_init(&ts_packet_mumu.packetmutex,NULL);
	  int iRet;
	  log_message( log_module, MSG_INFO,"File opened, reading packets\n" );
	  while(fread(ts_packet_raw,TS_PACKET_SIZE,1, testfile))
	    {
	      num_rand_read++;
	      log_message( log_module, MSG_INFO,"Position %d packets\n", num_rand_read);
	      iRet=get_ts_packet(ts_packet_raw, &ts_packet_mumu);
	      if(iRet==1)//packet is parsed
		{
		  log_message( log_module, MSG_INFO,"New VALID packet\n" );
		}
	    }
	  fclose(testfile);
	}
      else
	log_message( log_module, MSG_INFO,"Test file %s cannot be open : %s\n", files_rand[i_file], strerror(errno) );
    }
  /************************ Testing autoconfiguration with a dump **********************************/
  char *files_autoconf[NUM_FILES_TEST_AUTOCONF]={FILES_TEST_AUTOCONF};
  for(int i_file=0;i_file<NUM_FILES_TEST_AUTOCONF;i_file++)
  {

    log_message( log_module, MSG_INFO,"===================================================================\n");
    log_message( log_module, MSG_INFO,"Testing autoconfiguration file %d on %d %s\n",i_file+1, NUM_FILES_TEST_AUTOCONF, files_autoconf[i_file]);
    press_enter_func(press_enter);

    FILE *testfile;
    testfile=fopen (files_autoconf[i_file] , "r");
    if(testfile!=NULL)
    {
      int iret;
      mumudvb_chan_and_pids_t chan_and_pids;
      memset(&chan_and_pids,0,sizeof(mumudvb_chan_and_pids_t));
      chan_and_pids.number_of_channels=0;

      //autoconfiguration
      autoconf_parameters_t autoconf_vars;
      memset(&autoconf_vars,0,sizeof(autoconf_parameters_t));
      autoconf_vars.autoconfiguration=AUTOCONF_MODE_FULL;
      autoconf_vars.autoconf_radios=1;
      autoconf_vars.autoconf_scrambled=1;
      strcpy (autoconf_vars.autoconf_ip4,"239.100.%card.%number");
      autoconf_vars.transport_stream_id=-1;


      unicast_parameters_t unicast_vars;
      memset(&unicast_vars,0,sizeof(unicast_parameters_t));
      unicast_vars.unicast=0;

      //multicast parameters
      multicast_parameters_t multicast_vars;
      memset(&multicast_vars,0,sizeof(multicast_parameters_t));
      multicast_vars.multicast=0;

      fds_t fds;
      memset(&fds,0,sizeof(fds_t));
      tuning_parameters_t tuneparams;
      memset(&tuneparams,0,sizeof(tuning_parameters_t));

      iret=autoconf_init(&autoconf_vars, chan_and_pids.channels,chan_and_pids.number_of_channels);
      if(iret)
      {
        log_message( log_module, MSG_INFO,"error with autoconfiguration init\n");
      }

      unsigned char actual_ts_packet[TS_PACKET_SIZE];
      int pid;
      int packet_count=0;
      while(fread(actual_ts_packet,TS_PACKET_SIZE,1, testfile))
      {
        // get the pid of the received ts packet
        pid = ((actual_ts_packet[1] & 0x1f) << 8) | (actual_ts_packet[2]);
        packet_count++;
        if(!(packet_count %100))
          log_message( log_module, MSG_INFO,"Packet count %d", packet_count);

        iret = autoconf_new_packet(pid, actual_ts_packet, &autoconf_vars,  &fds, &chan_and_pids, &tuneparams, &multicast_vars, &unicast_vars, 0);

      }
      log_message( log_module, MSG_INFO,"===================================================================\n");
      log_message( log_module, MSG_INFO,"Forcing to finish and re read");
      press_enter_func(press_enter);
      //if there is a partial service list, we force autoconf to go to the next step
      autoconf_vars.time_start_autoconfiguration=1;
      autoconf_poll(100000, &autoconf_vars, &chan_and_pids, &tuneparams, &multicast_vars, &fds, &unicast_vars, 0);
      press_enter_func(press_enter);
      rewind(testfile);
      while(fread(actual_ts_packet,TS_PACKET_SIZE,1, testfile))
      {
        // get the pid of the received ts packet
        pid = ((actual_ts_packet[1] & 0x1f) << 8) | (actual_ts_packet[2]);
        log_message( log_module, MSG_INFO,"PID  %d", pid);
        packet_count++;
        if(!(packet_count %100))
          log_message( log_module, MSG_INFO,"Packet count %d", packet_count);

        iret = autoconf_new_packet(pid, actual_ts_packet, &autoconf_vars,  &fds, &chan_and_pids, &tuneparams, &multicast_vars, &unicast_vars, 0);

      }
      fclose(testfile);

      log_message( log_module, MSG_INFO,"============ DONE we display the services ========\n");
      if(autoconf_vars.services)
      {
        autoconf_sort_services(autoconf_vars.services);
        autoconf_print_services(autoconf_vars.services);
      }
      else
        log_message( log_module, MSG_INFO,"No services or services freed\n");

      log_message( log_module, MSG_INFO,"============ DONE we display the channels ========\n");
      if(chan_and_pids.number_of_channels)
      {
        log_streamed_channels(log_module,chan_and_pids.number_of_channels, chan_and_pids.channels, 1, 0, 0, 0,"");
        //We can tests other things here like REWRITE etc ....
        log_message( log_module, MSG_INFO,"===================================================================\n");
        log_message( log_module, MSG_INFO,"Testing SDT rewrite on this file (%s)\n", files_autoconf[i_file]);
        press_enter_func(press_enter);
        FILE *testfile;
        testfile=fopen (files_autoconf[i_file] , "r");
        if(testfile!=NULL)
        {
          //Parameters for rewriting
          rewrite_parameters_t rewrite_vars={
            .rewrite_pat = OPTION_OFF,
            .pat_version=-1,
            .full_pat=NULL,
            .pat_needs_update=1,
            .full_pat_ok=0,
            .pat_continuity_counter=0,
            .rewrite_sdt = OPTION_ON,
            .sdt_version=-1,
            .full_sdt=NULL,
            .sdt_needs_update=1,
            .full_sdt_ok=0,
            .sdt_continuity_counter=0,
            .eit_sort=OPTION_OFF,
          };
          for (int curr_channel = 0; curr_channel < MAX_CHANNELS; curr_channel++)
            chan_and_pids.channels[curr_channel].generated_sdt_version=-1;
          rewrite_vars.full_sdt=malloc(sizeof(mumudvb_ts_packet_t));
          if(rewrite_vars.full_sdt==NULL)
          {
            log_message( log_module, MSG_ERROR,"Problem with malloc : %s file : %s line %d\n",strerror(errno),__FILE__,__LINE__);
          }
          memset (rewrite_vars.full_sdt, 0, sizeof( mumudvb_ts_packet_t));//we clear it
          pthread_mutex_init(&rewrite_vars.full_sdt->packetmutex,NULL);

          while(fread(actual_ts_packet,TS_PACKET_SIZE,1, testfile))
          {
            // get the pid of the received ts packet
            pid = ((actual_ts_packet[1] & 0x1f) << 8) | (actual_ts_packet[2]);
            if( (pid == 17) ) //This is a SDT PID
              {
                //we check the new packet and if it's fully updated we set the skip to 0
                if(sdt_rewrite_new_global_packet(actual_ts_packet, &rewrite_vars)==1)
                {
                  log_message( log_module, MSG_DETAIL,"The SDT version changed, we force the update of all the channels.\n");
                  for (int curr_channel = 0; curr_channel < chan_and_pids.number_of_channels; curr_channel++)
                    chan_and_pids.channels[curr_channel].sdt_rewrite_skip=0;
                }
                for (int curr_channel = 0; curr_channel < chan_and_pids.number_of_channels; curr_channel++)
                {
                  if(!chan_and_pids.channels[curr_channel].sdt_rewrite_skip ) //AND the generation was successful
                    sdt_rewrite_new_channel_packet(actual_ts_packet, &rewrite_vars, &chan_and_pids.channels[curr_channel], curr_channel);
                }
              }

          }
          fclose(testfile);
          log_message( log_module, MSG_INFO,"===================================================================\n");
          log_message( log_module, MSG_INFO,"End of testing SDT rewritef\n");
          log_message( log_module, MSG_INFO,"===================================================================\n");
        }

      }
      else