示例#1
0
文件: wplay.cpp 项目: jeppeter/game
int main(int argc, char* argv[])
{
    FILE* fp=NULL;
    CViWavePlay *pPlay=NULL;
	WAVEFORMATEX *pwfx=(WAVEFORMATEX*)st_FormatEx;
    int ret;
    BOOL bret;
    char* pBuffer=NULL;
    int rlen=0;
    int retlen=0;
    int totalread=0;
    int filesize=0;
    char* pCurPtr=NULL;

    if(argc < 2)
    {
        fprintf(stderr,"wplay filetoplay\n");
        ret = -3;
        goto out;
    }

    fopen_s(&fp,argv[1],"r+b");
    if(fp == NULL)
    {
        ret = -(LAST_ERROR_CODE());
        fprintf(stderr,"can not open %s error(%d)\n",argv[1],ret);
        goto out;
    }

    fseek(fp,0,2);
    filesize = ftell(fp);
    rewind(fp);
    pBuffer = (char*)malloc(filesize);
    if(pBuffer == NULL)
    {
        ret = -LAST_ERROR_CODE();
        goto out;
    }

    retlen = fread(pBuffer,filesize,1,fp);
    if(retlen != 1)
    {
        ret = -LAST_ERROR_CODE();
        fprintf(stderr,"can not read %s filesize %d error(%d)\n",
                argv[1],filesize,ret);
        goto out;
    }

    pPlay = new CViWavePlay();

#if 0
    wfx.nSamplesPerSec = 48000;
    wfx.wBitsPerSample = 32;
    wfx.nChannels  = 2;
    wfx.cbSize = 0;
    wfx.wFormatTag      = WAVE_FORMAT_IEEE_FLOAT;
    wfx.nBlockAlign     = (wfx.wBitsPerSample * wfx.nChannels) >> 3;
    wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;

#endif
    rlen = pwfx->nSamplesPerSec * pwfx->wBitsPerSample * pwfx->nChannels / 8 / 10;
	DEBUG_INFO("pwfx->nSamplesPerSec %d\n",pwfx->nSamplesPerSec);
	DEBUG_INFO("pwfx->wBitsPerSample %d\n",pwfx->wBitsPerSample);
	DEBUG_INFO("pwfx->nChannels %d\n",pwfx->nChannels);
	DEBUG_INFO("pwfx->cbSize %d\n",pwfx->cbSize);
	DEBUG_INFO("pwfx->wFormatTag %d\n",pwfx->wFormatTag);
	DEBUG_INFO("pwfx->nBlockAlign %d\n",pwfx->nBlockAlign);
	DEBUG_INFO("pwfx->nAvgBytesPerSec %d\n",pwfx->nAvgBytesPerSec);
	DEBUG_BUFFER_FMT(pwfx,sizeof(st_FormatEx),"wfx sizeof(%d)",sizeof(st_FormatEx));
	SetLastError(0);
    bret = pPlay->Start(pwfx);
    if(!bret)
    {
        ret = -LAST_ERROR_CODE();
        fprintf(stderr,"start error %d\n",ret);
        goto out;
    }

    totalread = 0;
    pCurPtr = pBuffer;
    while(totalread < filesize)
    {
        retlen = rlen;
        if((totalread + retlen)> filesize)
        {
            retlen = filesize - totalread;
        }
        //DEBUG_BUFFER_FMT(pCurPtr,(retlen > 16 ? 16 : retlen),"buffer at %d retlen %d",totalread,retlen);
        bret = pPlay->PlayAudio(pCurPtr,retlen);
        if(!bret)
        {
            ret = -LAST_ERROR_CODE();
            fprintf(stderr,"play audio %d error %d\n",totalread,ret);
            goto out;
        }
        totalread += retlen;
		pCurPtr += retlen;
        Sleep(100);
    }


    ret = 0;

out:
    if(pBuffer)
    {
        free(pBuffer);
    }
    pBuffer = NULL;
    if(pPlay)
    {
        pPlay->Stop();
        delete pPlay;
    }
    pPlay = NULL;
    if(fp)
    {
        fclose(fp);
    }
    fp = NULL;
    return ret;
}
示例#2
0
        /*success ,so we do this*/
        pBuffer = NULL;
        usleep(20000);

        seekoff += bufret;
        framenum += 1;
        pts += 1800;
    }

out:
    if(pBuffer)
    {
        free(pBuffer);
    }
    pBuffer = NULL;
    if(fp)
    {
        fclose(fp);
    }
    fp = NULL;

    DEBUG_INFO("\n");
    this->m_ThreadExited = 1;
    return (void*)ret;
}

#else /*AUDIO_DUAL_FILE_EMULATE*/
void* SdkAudioStream::__ThreadImpl()
{
    int ret;
    void* pBuffer = NULL;
    /*for 16 k*/
    uint32_t buflen,bufret;
    uint32_t infosize=1024,inputsize;
    std::auto_ptr<unsigned char> pExInfo2(new unsigned char[infosize]);
    unsigned char* pExInfo =pExInfo2.get();
    struct timeval tmval;
    uint32_t lastidx=0;
    GMI_RESULT gmiret;
    unsigned long long pts=0,lasttick=0,curtick=0;
    struct timespec tmspec;

    buflen = 0x1000;
    if(this->m_Format == audio_format_g711 ||
            this->m_Format == audio_format_default)
    {
        /*for 16k bytes*/
        buflen = 0x4000;
    }
    DEBUG_INFO("start thread impl\n");
    while(this->m_ThreadRunning)
    {

        if(pBuffer== NULL)
        {
            /*we reuse the buffer ,when last time not get data and insert ,so we can limit the time*/
            pBuffer = malloc(buflen);
        }
        if(pBuffer == NULL)
        {
            ret = -errno ? -errno : -1;
            goto out;
        }

        bufret = buflen;
        inputsize= infosize;
        gmiret = this->m_IPCClient.Read(pBuffer,&bufret,&tmval,pExInfo,&inputsize);
        if(gmiret ==GMI_SUCCESS)
        {
            ExtMediaEncInfo* pInfo=(ExtMediaEncInfo*)pExInfo;
            /*now to get the */
            pts = VFREQ_PER_SECOND(tmval.tv_sec) + VFREQ_PER_USECOND(tmval.tv_usec);
            if(pts < 100000)
            {
                /*make sure it is not overflow*/
                pts *= 9;
                pts /= 100;
            }
            else
            {
                pts /= 100;
                pts *= 9;
            }

            ret = clock_gettime(CLOCK_MONOTONIC,&tmspec);
            if(ret < 0)
            {
                ERROR_INFO("could not get time error(%d)\n",errno);
            }
            else
            {
                curtick = VFREQ_PER_SECOND(tmspec.tv_sec) + VFREQ_PER_NANOSECOND(tmspec.tv_nsec);
                if(lasttick && (curtick - lasttick) >200000)
                {
                    ERROR_INFO("audio[%d] curtick(%lld) lasttick(%lld) (%lld)\n",pInfo->s_FrameNum,curtick,lasttick,(curtick-lasttick));
                }
				lasttick = curtick;
            }



            if(pInfo->s_FrameNum != (lastidx + 1))
            {
                DEBUG_INFO("framenum (%d) != (%d + 1)\n",pInfo->s_FrameNum,lastidx);
            }
            lastidx = pInfo->s_FrameNum;
            if((lastidx % 200)==0)
            {
                DEBUG_BUFFER_FMT(pBuffer,bufret,"framenum %d",lastidx);
            }

            ret = this->__PushStreamData(pBuffer,bufret,pInfo->s_FrameNum,pts);
            if(ret < 0)
            {
                goto out;
            }
            /*insert success ,so we should not free this buffer*/
            pBuffer = NULL;

        }
        else
        {
            /*we are not success ,so we sleep for a while*/
            usleep(20000);
        }

    }
    ret = 0;

out:
    if(pBuffer)
    {
        free(pBuffer);
    }
    pBuffer = NULL;
    DEBUG_INFO("stop thread impl\n");
    this->m_ThreadExited = 1;
    return (void*) ret;
}