Exemplo n.º 1
0
static void AVB_Process(void *pData)
{
   kal_int16 *pDstBuffer;
   kal_int8 *pSrcBuffer;
   kal_uint32 uSrcByte, uDstByte, uConsumeByte;
   kal_trace( TRACE_GROUP_SCO, L1AUDIO_AVB_PROC, AVB.uState, AVB.uSegment_W, AVB.uSegment_R);
   while(AVB.uSegment_W - AVB.uSegment_R < AVB_SEGMENT_NUMBER)
   {
      rbGetReadBuffer(&AVB.PCM, &pSrcBuffer, &uSrcByte);
      if(uSrcByte<4)
      {
         break;
      }
      uDstByte = (AVB_FRAME_SAMPLE-AVB.uSample_W)*2;
      pDstBuffer = AVB.pBuffer + (AVB.uSegment_W & AVB_SEGMENT_MASK) * AVB_FRAME_SAMPLE + AVB.uSample_W;
      uConsumeByte = BLI_Convert(AVB.pBLISRC, (kal_int16*)pSrcBuffer, &uSrcByte, pDstBuffer, &uDstByte);  
      rbReadDataDone(&AVB.PCM, uConsumeByte);
      AVB.uSample_W += uDstByte>>1;
      ASSERT(AVB.uSample_W <= AVB_FRAME_SAMPLE);
      if(AVB.uSample_W == AVB_FRAME_SAMPLE)
      {
         AVB.uSample_W = 0;
         AVB.uSegment_W++;
      }
      else
      {
         uDstByte = (AVB_FRAME_SAMPLE-AVB.uSample_W)*2;
         pDstBuffer = AVB.pBuffer + (AVB.uSegment_W & AVB_SEGMENT_MASK) * AVB_FRAME_SAMPLE + AVB.uSample_W;
         memset(pDstBuffer, 0, uDstByte);
      }
   }
   if(AVB.uState == AVB_STATE_FLUSH_TAIL)
   {
      rbGetReadBuffer(&AVB.PCM, &pSrcBuffer, &uSrcByte);
      if(uSrcByte<4)
      {
         if(AVB.uSample_W)
         {
            AVB.uSample_W = 0;
            AVB.uSegment_W++;
         }
         AVB.uState = AVB_STATE_FLUSH_PING;
      }
   }
}
int32 LADPlayBuffer::LADPlayWrite(int8* iBuf, int32 num)
{
   int32 tryCount = 0;
   uint32 inCount, outCount, consumed;
   int8* buf;
   int dataCount = 0;

   LOG_LADPlay("+LADPlayWrite num=%d",num);

   //if VM case call VM write function
   if ( mFormat == android_audio_legacy::AudioSystem::VM_FMT ){
      return LADPlayWriteVM(iBuf, num);
   }

   //num not consumed
   int32 leftCount = num;

   //do 8bit to 16bit conversion if necessary
   if( mFormat == android_audio_legacy::AudioSystem::PCM_8_BIT ) {
      buf = new int8[num * 2];
      ConvertPcm8ToPcm16(iBuf, buf, num);
   }
   else{
      buf = iBuf;
   }
   //keep on trying until leftCount = 0
   do {

      // lock
      BuffLock();

      // if LadPlayer is closed, return this function to revent block
      if(pLadPlayer->runFlag == 0) {
         LOG_LADPlay("pLadPlayer=%x, runFlag=%d",pLadPlayer,pLadPlayer->runFlag );
      	BuffUnLock();
      	return num;
      }
/*
      //get data count in buffer
      dataCount = rb_getDataCount(&mBuf);
      LOG_LADPlay("!!!LADPlayWrite dataCount:%d\n",dataCount);
*/
      if( mBuf.pRead <= mBuf.pWrite ) {
         //printf("glbcnt: %d\n", glbcnt);
         //if ( glbcnt == 3 )
         //   glbcnt = glbcnt;
         //glbcnt++;

         //If write pointer is larger than read, we output from write til end
         inCount = leftCount;
         if ( mBuf.pRead == mBuf.pBufBase )
            outCount = mBuf.pBufBase + mBuf.bufLen - mBuf.pWrite - 2;
         else
            outCount = mBuf.pBufBase + mBuf.bufLen - mBuf.pWrite;

//         LOG_LADPlay("1LADPlayWrite~ inCount=%d, outCount=%d",inCount,outCount);

#ifdef DUMP_BGS_BLI_BUF
         fwrite(buf, sizeof(char), inCount, p_OutFile1);
#endif
         //Do Conversion
         consumed = BLI_Convert(pSrcHdl, (int16*)buf, &inCount, (int16*)mBuf.pWrite, &outCount);
         buf += consumed;


         LOG_LADPlay("1LADPlayWrite consumed=%d, inCount=%d, outCount=%d",consumed,inCount,outCount);

         //Judge if we need to try consume again
         if ( inCount == 0 ) {         // all input buffer is consumed, means no data to copy to rb
            mBuf.pWrite += outCount;   // update write pointer with outcount
            BuffUnLock();
            break;
         }
         else {                           // input buffer is not completely consumed, but ring buffer hits its end
            mBuf.pWrite += outCount;
            if ( mBuf.pWrite == mBuf.pBufBase + mBuf.bufLen )
               mBuf.pWrite = mBuf.pBufBase;  // ring buffer write pointer start from base
            leftCount -= consumed;        // update leftCount try to consume again
         }

         //get data count in buffer
         dataCount = rb_getDataCount(&mBuf);

         LOG_LADPlay("1LADPlayWrite pRead:%d, pWrite:%d, dataCount:%d\n",
            mBuf.pRead - mBuf.pBufBase, mBuf.pWrite - mBuf.pBufBase, dataCount);
      }
      else {
         //If read pointer is larger than write, we output from write til read pointer
         inCount = leftCount;
         outCount = mBuf.pRead - mBuf.pWrite - 2;

//         LOG_LADPlay("2LADPlayWrite~ inCount=%d, outCount=%d",inCount,outCount);

#ifdef DUMP_BGS_BLI_BUF
         fwrite(buf, sizeof(char), inCount, p_OutFile1);
#endif

         //Do Conversion
         consumed = BLI_Convert(pSrcHdl, (int16*)buf, &inCount, (int16*)mBuf.pWrite, &outCount);
         buf += consumed;

         LOG_LADPlay("2LADPlayWrite consumed=%d, inCount=%d, outCount=%d",consumed,inCount,outCount);

         //Judge if we need to try consume again
         if ( inCount == 0 ) {         // all input buffer is consumed, means no data to copy to rb
            mBuf.pWrite += outCount;   // update write pointer with outcount
            BuffUnLock();
            break;
         }
         else {                           // input buffer is not completely consumed, but ring buffer hits read pointer
            mBuf.pWrite += outCount;      // update write pointer
            leftCount -= consumed;        // update leftCount try to consume again
         }

         //get data count in buffer
         dataCount = rb_getDataCount(&mBuf);

         LOG_LADPlay("2LADPlayWrite pRead:%d, pWrite:%d, dataCount:%d\n",
            mBuf.pRead - mBuf.pBufBase, mBuf.pWrite - mBuf.pBufBase, dataCount);
      }

      //unlock
      BuffUnLock();

      AAD_Sleep_1ms(3);      //wait some time and try again

   } while ( tryCount++ < 200 );

      //   printf("exit:-- pRead:%d, pWrite:%d, pBase:0x%x, pEnd:0x%x\n",
      //      mBuf.pRead - mBuf.pBufBase, mBuf.pWrite - mBuf.pBufBase, mBuf.pBufBase, mBuf.pBufBase+ mBuf.bufLen);

   if(tryCount >= 20) ASSERT(0);

   //LOG_LADPlay("-LADPlayWrite tryCount:%d",tryCount);

   // always return num
   return num;
}