Пример #1
0
//==================================================================================================
int dvbout(options_t *options)
{
    int tempCount = 0;
    int packets = 0;
    int infinite_loop = 0;
    Byte buffer[BSIZE*BBLOCK];
    int i = 0, j = 0;
    FILE *pFile = NULL;

    if (options->ts_filename) 
    {
        if (strcmp("-", options->ts_filename) == 0)
        {
            pFile = stdout;
        }
        else
        {
            pFile = fopen(options->ts_filename, "wb");
        }
    }

    DTV_StartCapture();

    usleep(2000000);

    j = 0;
    
    if (options->packet_limit == 0)
    {
        infinite_loop = 1; // for Burn in test
    }

    while (!gDoExit && (infinite_loop || (i < options->packet_limit))) 
    {
        int pid, ok;
        Dword r;
        r = 188*BBLOCK;

        DTV_GetData(buffer, &r);
        //printf("DTV_GetData - %d\n", r);
        
        if (r <= 0) 
        {
			if (options->verbose)
			{
				fprintf(stderr, "read %d (%lu, %d) \n", j, r, packets);
			}
            usleep(100000); // was 100 ms
            j++;
            
            if (j>10) // was 10 sec
            {
                fprintf(stderr, "Read timeout\n", r);
                
                if (options->relock)
                {
                    if (ChannelLock(options) < 0) 
                    {
                        fprintf(stderr, "Re-lock channel fail!\n");
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
            
            continue;
        }
        
        j = 0;
        
        if (r != 188*BBLOCK) 
        {
            fprintf(stderr, "only read %lu\n", r);
            break;
        }
        
        packets++;

        if (pFile) 
        {
            i = (packets*188*2) / 1024 / 1024; // MB
            fwrite(buffer, 1, sizeof(buffer), pFile);
        }
    }

    DTV_StopCapture();

    if (pFile) 
    {
        fclose(pFile);
        fprintf(stderr, "\n*** Save File to %s (%dM) ***\n", options->ts_filename, i);
    }

    return 0;
}
Пример #2
0
static void ule_rx()
{
    const int nBufSize = 188;
    int nFailCount = 0;
    unsigned char buffer[nBufSize];
    FILE* fileOut = fopen("rx.out", "w+b");
    Dword ret;

    ret = DTV_StartCapture();
    if (ret != ERR_NO_ERROR) {
        debug("start error: %lu", ret);
    } else {
        debug("start success");
    }

    ULEDemuxCtx demuxCtx;
    ule_initDemuxCtx(&demuxCtx);
    demuxCtx.pid = 0x1FAF;

    int nTotalCount = 0;
    int8_t tscc = 0;
    while (true) {
        memset(buffer, 0, nBufSize);
        Dword r = nBufSize;
        DTV_GetData(buffer, &r);
        if (r <= 0) {
            if (++nFailCount > 100) {// 10 sec
                debug("fail.");
                break;
            }
            usleep(100 * 1000);
            continue;
        } else {
            nFailCount = 0; // reset
        }

        if (r != 188) {
            debug("only read %lu", r);
            break; 
        }
        if (buffer[0] != 0x47) {
            //debug("desync (%x, %x, %x, %x) - %lu\n", buffer[0], buffer[1], buffer[2], buffer[3], r);
            while (buffer[0] != 0x47) {
                r = 1;
                DTV_GetData(buffer, &r);
                if (r <= 0) {
                    usleep(100*1000);
                    continue;
                }
                if (r != 1) {
                    debug("not only read 1 but %lu", r);
                    continue;
                }
            }

            // remaining
            r = 187;
            DTV_GetData(buffer+1, &r);
            if (r != 187) {
                debug("sync error read %lu", r);
                continue;
            }
        }

        uint16_t pidFromBuf = ts_getPID(buffer);
        if (pidFromBuf == 0x1FFF) {
            continue;
        }
        if (pidFromBuf != demuxCtx.pid) {
            continue;
        }

        // check tscc
        //
        int8_t tsccNew = ts_getContinuityCounter(buffer);
        if (tsccNew - tscc == 1) {
            tscc++;
        } else if (tsccNew - tscc == -15) {
            tscc = 0;
        } else {
            debug("tscc is discontinuous curr=%d, new=%d", tscc, tsccNew);

            if (tsccNew <= tscc) {
                continue;//skip
            } else {
                tscc = tsccNew;
                ule_resetDemuxCtx(&demuxCtx);
            }
        }

        //debug("start demux");
        ule_demux(&demuxCtx, buffer, nBufSize);
        if (demuxCtx.ule_sndu_outbuf != NULL) {
            nTotalCount++;
            printf("recv: szie=%d, Total=%d\n", demuxCtx.ule_sndu_outbuf_len, nTotalCount);
            //hexdump(demuxCtx.ule_sndu_outbuf, demuxCtx.ule_sndu_outbuf_len);
            fprintf(fileOut, "%s\n", demuxCtx.ule_sndu_outbuf);
            fflush(fileOut);

            // clean & reset outbuf
            free(demuxCtx.ule_sndu_outbuf);
            demuxCtx.ule_sndu_outbuf = NULL;
            demuxCtx.ule_sndu_outbuf_len = 0;
        }

        usleep(100);
    }//end while(true)
    debug("stop ule rx");

    DTV_StopCapture();
}
Пример #3
0
//==================================================================================================
int dvbtraffic(options_t *options)
{
    int tempCount = 0;
    int packets = 0;
    int infinite_loop = 0;
    struct timeval startt, startt2;
    char *search;
    unsigned char buffer[BSIZE];
    int i = 0, j = 0;
    uint8_t seq;
    uint8_t adaptationField;
    uint8_t checkBit;
    FILE *pFile = NULL;

    if (options->ts_filename) 
    {
        if (strcmp("-", options->ts_filename) == 0)
        {
            pFile = stdout;
        }
        else
        {
            pFile = fopen(options->ts_filename, "wb");
        }
    }

    for (j = 0; j < 0x2001; j++) 
    {
        pidt[j].count = 0;
        pidt[j].ulLostCount = 0;
        pidt[j].pid = 0;
        pidt[j].sequence1 = 0xFF;
        pidt[j].sequence2 = 0xFF;
    }

    DTV_StartCapture();

    //usleep(2000000);

    gettimeofday(&startt, 0);
    startt2 = startt;

    //if (argc > 1)
    //	search = argv[1];
    //else
        search = 0;

    j = 0;
    
    if (options->packet_limit == 0)
    {
        infinite_loop = 1; // for Burn in test
    }

    while (!gDoExit && (infinite_loop || (i < options->packet_limit))) 
    {
        int pid, ok;
        Dword r;
        r = 188;
        DTV_GetData(buffer, &r);
        //printf("DTV_GetData - %d\n", r);
        if (r <= 0) 
        {
            //printf("read%d", j);
            usleep(100000);
            j++;
            
            if (j>100) // 10 sec
            {
                break;
            }
            
            continue;
        }
        
        j = 0;
        
        if (r != 188) 
        {
            fprintf(stderr, "only read %lu\n", r);
            break;
        }
        
        if (buffer[0] != 0x47) 
        {
            //continue;
            fprintf(stderr, "desync (%x, %x, %x, %x) - %lu\n", buffer[0], buffer[1], buffer[2], buffer[3], r);
            
            while (buffer[0] != 0x47) 
            {
                r = 1;
                DTV_GetData(buffer, &r);
            }
            
            r = 187;
            DTV_GetData(buffer, &r);
            continue;
            tempCount++;
        }
            //printf("tempCount %d\n", tempCount);
        ok = 1;
        pid = ((((unsigned) buffer[1]) << 8) |
               ((unsigned) buffer[2])) & 0x1FFF;

        adaptationField = (buffer[3]&0x30) >> 4;
        seq = buffer[3] & 0xf;

        if (search) 
        {
            int i, sl = strlen(search);
            ok = 0;
            
            if (pid != 0x1fff) 
            {
                for (i = 0; i < (188 - sl); ++i)
                {
                    if (!memcmp(buffer + i, search, sl))
                    {
                        ok = 1;
                    }
                }
            }
        }

        if (pid == 0x1FFF) 
        {
            //printf("\nSave %d KB...\n", packets*188/1024);
            continue;
        }

        if (ok) 
        {
            pidt[pid].count++;

            if (pidt[pid].sequence1 == 0xFF) 
            {
                //the first packet arrives, nothing to do
                pidt[pid].sequence1 = seq;
            }
            else 
            {
                if (adaptationField==0 || adaptationField==2) 
                {
                    //No increment(lost) check for type 0 & 2 
                }
                else 
                {
                    //Increment(lost) check for type 1 & 3
                    if ((pidt[pid].sequence1==seq) && (pidt[pid].dup_flag==0)) 
                    {
                        // Duplicate packets may be sent as two, and only two, consecutive Transport Stream packets of the same PID
                        pidt[pid].dup_flag = 1; // No more duplicate allowed
                    }
                    else 
                    {
                        if (pidt[pid].sequence1+1 == seq) 
                        {
                            //no packet loss, expected packet arrives 
                            if (pidt[pid].count == 100000)
                            {
                                fprintf(stderr, "%d", pidt[pid].ulLostCount);
                            }
                        }
                        else 
                        {
                            if (pidt[pid].sequence1+1 < seq) 
                            {
                                pidt[pid].ulLostCount += seq - (pidt[pid].sequence1+1);
                            }
                            else 
                            {
                                pidt[pid].ulLostCount += (seq+0x10) - (pidt[pid].sequence1+1);
                            }
                        }
                        
                        pidt[pid].dup_flag = 0; //next duplicate allowed
                        pidt[pid].sequence1 = seq;
                    }
                }
            }
                    
            //Check if TEI error  
            if (checkBit != 0x00)
            {
                pidt[pid].ulErrCount++;
            }
        }

        packets++;

        if (!(packets & 0xFF)) 
        {
            struct timeval now;
            uint32_t diff, diff2;
            gettimeofday(&now, 0);
            diff2 = (now.tv_sec - startt2.tv_sec) * 1000 +
                (now.tv_usec - startt2.tv_usec) / 1000;
                
            if (diff2 > 1000) 
            {
                int n = 0;
                diff = (now.tv_sec - startt.tv_sec) * 1000 +
                    (now.tv_usec - startt.tv_usec) / 1000;
                    
                fprintf(stderr, "\n-PID-------Total----Lost----Error---Mbps-\n");
                
                for (n = 0; n < 0x2001; n++) 
                {
                    if (pidt[n].count) 
                    {
                        fprintf(stderr, "%04x %10d %7d %7d %8d\n",
                                n,
                                pidt[n].count,
                                pidt[n].ulLostCount,
                                pidt[n].ulErrCount,
                                pidt[n].count * 188 / 1000 * 8 * 1000 / diff);
                    }
                }
                
                startt2 = now;
                
                if(i >= 0) 
                {
                    i++;
                }

                if (options->ts_filename) 
                {
                    fprintf(stderr, "\nSave %d KB...\n", packets*188/1024);
                }
            }
        }

        if (pFile) 
        {
            i = (packets*188) / 1024 / 1024; // MB
            fwrite(buffer, 1, sizeof(buffer), pFile);
        }
    }

    DTV_StopCapture();

    if (pFile) 
    {
        fclose(pFile);
        fprintf(stderr, "\n*** Save File to %s (%dM) ***\n", options->ts_filename, i);
    }

    return 0;
}
Пример #4
0
static void echoRX()
{
    FILE* fileOut = fopen("rx_echo.out", "w+");
    const int nBufSize = 188;
    int nFailCount = 0;
    unsigned char buffer[nBufSize];
    memset(buffer, 0, 188);
    Dword ret;
    ret = DTV_StartCapture();
    if (ret != ERR_NO_ERROR) {
        printf("start error: %lu\n", ret);
    }

    while (true) {
        Dword r = nBufSize;
        DTV_GetData(buffer, &r);
        if (r <= 0) {
            if (++nFailCount > 100) // 10 sec
                break;
            usleep(100*1000);
            continue;
        }
        else {
            nFailCount = 0;
        }

        if (r != 188) {
            debug("only read %lu", r);
            break; 
        }
        if (buffer[0] != 0x47) {
            //debug("desync (%x, %x, %x, %x) - %lu\n", buffer[0], buffer[1], buffer[2], buffer[3], r);
            while (buffer[0] != 0x47) {
                r = 1;
                DTV_GetData(buffer, &r);
                if (r <= 0) {
                    usleep(100*1000);
                    continue;
                }
                if (r != 1) {
                    debug("not only 1 but %lu", r);
                    continue;
                }
            }

            // remaining
            r = 187;
            DTV_GetData(buffer+1, &r);
            if (r != 187) {
                debug("sync error read %lu", r);
                continue;
            }
            continue;
        }

        uint16_t pidFromBuf = ts_getPID(buffer);
        if (pidFromBuf == 0x1FFF) {
            continue;
        }
        if (pidFromBuf != 0x1FAF) {
            continue;
        }

        fprintf(fileOut, "%s\n", buffer+4);
        fflush(fileOut);//XXX
        printf("%s\n", buffer+4);
        usleep(100);
    }

    DTV_StopCapture();
}