Exemple #1
0
void RipAudio( HCDROM hCD, DWORD dwStart, DWORD dwLen )
{
  DWORD dwStatus;
  FILE *fp;
  DWORD num2rip = 26;
  int retries;
  LPTRACKBUF t;
  int c = 0;
  DWORD numWritten = 0;

  t = NewTrackBuf( 26 );
  if ( !t )
    return;

  fp = fopen( "track1.wav", "w+b" );

  writeWavHeader( fp, 0 );

  printf( "Beginning to rip %d sectors starting at sector %d\n", dwLen, dwStart );

  while( dwLen )
    {
      if ( !( c++ % 5) )
	printf( "%d: %d\n", dwStart, dwLen );

      if ( dwLen < num2rip )
	num2rip = dwLen;

      retries = 3;
      dwStatus = SS_ERR;
      while ( retries-- && (dwStatus != SS_COMP) )
	{
	  t->numFrames = num2rip;
	  t->startOffset = 0;
	  t->len = 0;
	  t->startFrame = dwStart;
	  dwStatus = ReadCDAudioLBA( hCD, t );
	}
      if ( dwStatus == SS_COMP )
	{
	  fwrite( t->buf + t->startOffset, 1, t->len, fp );
	  numWritten += t->len;
	}
      else
	{
	  printf( "Error at %d (%d:%d)\n", dwStart, GetAspiLibError(), GetAspiLibAspiError() );
	}
      dwStart += num2rip;
      dwLen -= num2rip;
    }

  writeWavHeader( fp, numWritten );
  
  fclose( fp );
  GlobalFree( (HGLOBAL)t );
}
static void *handle_clients(void* args) {
  char s[MM_PROBE_MAX_DATA_SEND_SIZE + sizeof(probeHeader_t)] = {0};
  int num, fd = -1;
  boolean run = true;
  client_terminated = false;
  FILE *binFile = NULL; //Used for combined bin-file
  int server_store_location = 0;  //0=>PC 1=>sdcard
  FILE *wavSpeechFilePaths[MM_PROBE_NUMBER_OF_SPEECH_PROBES] = {NULL}; //Used for Speech probes wav-files
  char wavSpeechFormatWritten[MM_PROBE_NUMBER_OF_SPEECH_PROBES] = {0}; //to store enabled probes for filepath creation
  int probeDataCnt = 0;     //How much data from a read has been used
  int moreProbeData = 1;    //to see if one read contain more than one probe
  char tmpProbe[MM_PROBE_MAX_DATA_SEND_SIZE + sizeof(probeHeader_t)] = {0}; //Temp store for next probe to be written to file
  int tmpProbeHeaderSize = 0;
  int tmpProbePayloadSize = 0;
  threadInputData_t *input = (threadInputData_t*) args;
  char probeIDs[MM_PROBE_NUMBER_OF_SPEECH_PROBES * 3 + 1] = {0}; //to store enabled probes for filepath creation

  char *tmpBinFileBuffer = malloc(MM_PROBE_MAX_DATA_SEND_SIZE);
  int tmpBinFileBufferCounter = 0;

  log_message(L_INFO, "client: waiting for data...");

  fd = open(FIFO_NAME, O_RDONLY);
  if (input->enabledProbes != NULL) {
    strncat(probeIDs, input->enabledProbes, MM_PROBE_NUMBER_OF_SPEECH_PROBES * 3);
  }
  // To make read blocking we must always have a writer connected to the pipe
  int dummy_client = open(FIFO_NAME, O_WRONLY);

  while (run) {
    if ((num = read(fd, s, MM_PROBE_MAX_DATA_SEND_SIZE)) == -1) {
      log_message(L_INFO, "client: failed to read from pipe");
      run = false;
    // Silly temporary solution to kill the client.
    // Works only for 1 client. The rest will still be running since
    // there is a race of which client gets the read.
    // I need to use select with multiple fds instead (I think)...
    } else if (strcmp(s, "die") == 0) {
      log_message(L_INFO, "client: die message received");
      run = false;
    } else if (strcmp(s, "PC") == 0) {
      server_store_location = 0;
      log_message(L_INFO, "client: server_store_location=PC");
    } else if (strcmp(s, "Phone") == 0) {
      server_store_location = 1;
      log_message(L_INFO, "client: server_store_location=Phone");
    } else if (strcmp(s, "Path") == 0) {
      //Check if LogDecoder or commandline interface is used
      //If LogDecoder is used set logDecoderFormat to combined file
      if (probeIDs[0] == '\0') {
        input->logDecoderFormat = 1;
      }
      //Create necessary files for probing to locally on phone
      if (create_files(probeIDs, input->logDecoderFormat, &binFile, wavSpeechFilePaths) != 0) {
        run = false;
      }
    } else if(num > 0) {
      if (server_store_location == 1) {
        if (input->logDecoderFormat != 0) {
          //write all probe data to a single combined file in LogDecoder file format
          if (binFile != NULL) {
            //if the tmp-buffer is full write data to filesystem. Buffer used to reduce no of fwrite which takes long time
            if (num >= (MM_PROBE_MAX_DATA_SEND_SIZE - tmpBinFileBufferCounter)) {
              fwrite(tmpBinFileBuffer, 1, tmpBinFileBufferCounter, binFile);
              tmpBinFileBufferCounter = 0;
            }
            //Copy probe-data to tmp-buffer
            memcpy(tmpBinFileBuffer + tmpBinFileBufferCounter, s, num);
            tmpBinFileBufferCounter += num;
          }
        } else {
          //write probe data to multiple files, one per pid, pcm .wav format
          char* probe_p = s;
          probeHeader_t * header_p = (probeHeader_t *)tmpProbe;
          probeDataCnt = 0;
          moreProbeData = 1;

          while (moreProbeData) {

            //Copy header
            if (tmpProbeHeaderSize < (int)sizeof(probeHeader_t)) {
              int toBeCopied = ( (sizeof(probeHeader_t)-tmpProbeHeaderSize) <= (num -probeDataCnt)  ? (sizeof(probeHeader_t)-tmpProbeHeaderSize) : (num -probeDataCnt) );

              memcpy(tmpProbe, probe_p, toBeCopied);
              probe_p += toBeCopied;
              probeDataCnt += toBeCopied;
              tmpProbeHeaderSize += toBeCopied;
            }
            //Copy payload
            if (tmpProbeHeaderSize == sizeof(probeHeader_t)) {
              if ( tmpProbePayloadSize < header_p->Size ) {
                int toBeCopied = ( ( header_p->Size - tmpProbePayloadSize) <= (num -probeDataCnt) ? header_p->Size - tmpProbePayloadSize : (num -probeDataCnt) );

                memcpy( (tmpProbe + tmpProbeHeaderSize + tmpProbePayloadSize), probe_p, toBeCopied);
                probe_p += toBeCopied;
                probeDataCnt += toBeCopied;
                tmpProbePayloadSize += toBeCopied;
              }
            }

            //If a complete full probe exist write to file
            if ( (tmpProbeHeaderSize == sizeof(probeHeader_t)) && (header_p->Size == tmpProbePayloadSize) ) {
              int probeId = header_p->ProbeId;
              //Check if a file has been open for writing
              //Speech probes
              if ( (probeId < MM_PROBE_NUMBER_OF_SPEECH_PROBES) && (wavSpeechFilePaths[probeId] != NULL) ) {

                if (wavSpeechFormatWritten[probeId] == false) {
                  //Speech probes that uses v1 have to override samplerate
                  if (header_p->DataFormat.SampleRate == MM_PROBE_SAMPLE_RATE_UNKNOWN) {
                    //20 ms probes in CScall
                    if (  (probeId == MM_PROBE_RX_SPD_OUTPUT) || (probeId == MM_PROBE_TX_SPE_INPUT) ||
                          (probeId == MM_PROBE_TX_CS_CALL_INPUT) || (probeId == MM_PROBE_RX_CS_CALL_OUTPUT) ) {
                      if (header_p->Size == 320)
                        header_p->DataFormat.SampleRate = MM_PROBE_SAMPLE_RATE_8KHZ;
                      else
                        header_p->DataFormat.SampleRate = MM_PROBE_SAMPLE_RATE_16KHZ;
                    } else {
                    //10 ms probes in Speech_proc
                      if (header_p->Size == 160)
                        header_p->DataFormat.SampleRate = MM_PROBE_SAMPLE_RATE_8KHZ;
                      else
                        header_p->DataFormat.SampleRate = MM_PROBE_SAMPLE_RATE_16KHZ;
                    }
                  }
                  writeWavHeader((DataFormat_t*) &header_p->DataFormat, wavSpeechFilePaths[probeId]);
                  wavSpeechFormatWritten[probeId] = true;
                }
                fwrite(tmpProbe + sizeof(probeHeader_t), 1, tmpProbePayloadSize, wavSpeechFilePaths[probeId]);
              } else if (probeId > MM_PROBE_NUMBER_OF_SPEECH_PROBES) {
                //ADM/OMX probes
                int i;

                for (i=0; i<MM_PROBE_MAX_NUMBER_ADM_PROBES; i++) {
                  if ( (deviceProbeList[i].pid  == probeId) && (deviceProbeList[i].file  != NULL) ) {

                    if (deviceProbeList[i].formatWritten == false) {
                      writeWavHeader((DataFormat_t*) &header_p->DataFormat, deviceProbeList[i].file);
                      deviceProbeList[i].formatWritten = true;
                    }
                    fwrite(tmpProbe + sizeof(probeHeader_t), 1, tmpProbePayloadSize, deviceProbeList[i].file);
                    break;
                  }
                }
              }
              tmpProbeHeaderSize = 0;
              tmpProbePayloadSize = 0;
            }

            //all read data used
            if ( probeDataCnt == num ) {
              moreProbeData = 0;
            }
          }
        }
      } else {
        send(input->clientfd, s, num, 0);
      }
    } else {
      log_message(L_INFO, "client: this should never happen!");
      run = false;
    }
  }

  if ((server_store_location == 1) && (binFile != NULL)) {
    fclose(binFile);
  } else if ((server_store_location == 1) && (input->logDecoderFormat == 0)) {
    int i;
    //Running without LogDecoder i.e. each active probe file should be closed
    //Speech probes
    for (i=1; i < MM_PROBE_NUMBER_OF_SPEECH_PROBES; i++) {
      if (probe_list[i-1] == 1) {
        if (wavSpeechFilePaths[i] != NULL) {
          if (wavSpeechFormatWritten[i] == true) {
            updateWavHeader(wavSpeechFilePaths[i]);
          }
          fclose(wavSpeechFilePaths[i]);
          wavSpeechFilePaths[i] = NULL;
        }
      }
    }
    //ADM/OMX probes
    for (i=0;  i <MM_PROBE_MAX_NUMBER_ADM_PROBES; i++) {
      if (deviceProbeList[i].file != NULL) {
        if (deviceProbeList[i].formatWritten == true) {
          updateWavHeader(deviceProbeList[i].file);
        }
        fclose(deviceProbeList[i].file);
        deviceProbeList[i].file = NULL;
      }
    }
  }
  close(fd);
  close(dummy_client);
  client_terminated = true;
  disable_all_probes();

  // Client process can now go and die.
  log_message(L_INFO, "client: shutting down.");
  close(input->clientfd);

  return NULL;
}