⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mclient.c

📁 TI evm642的源码,给那些没有买TI的评估板,却想要评估板程序的人.
💻 C
字号:
//---------------------------------------------------------------------------
// TEST
//---------------------------------------------------------------------------
// MCLIENT.C
//---------------------------------------------------------------------------
#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <winsock.h>

char recbuf[256000];
char playbuf[256000];

// Commands we can send to the DSP
#define CMD_SETCLOCK    0
#define CMD_SHOWCLOCK   1
#define CMD_SHOWDOTS    2

static int peerCmd( SOCKET s, int cmd, int arg )
{
    if( send( s, (char *)&cmd, 4, 0 ) < 0 ||
        send( s, (char *)&arg, 4, 0 ) < 0 )
    {
        printf("failed command send (%d)\n",WSAGetLastError());
        return(0);
    }
    return(1);
}


int main( int argc, char *argv[] )
{
    WORD         wVersionRequested;
    WSADATA      wsaData;
    SOCKET       srecv,ssend;
    struct sockaddr_in sin;
    int          size,readsize,playsize,tmp;
    int          tmp1,tmp2,tmp3,tmp4;
    time_t       ts,tn;
    int          frames,size_lo,size_hi,size_tot;
    int          recording=0,playing=0,pause=0;
    int          recframes=0,motframes;
    int          showClock=1,showGrid=0;
    FILE         *vidfile;
    unsigned char c;

    printf("\nMCLIENT - Windows Client App for JPEG_Motion Demo\n");

    // IP address to connect
    if( argc !=2 && argc != 3 ||
        sscanf(argv[1],"%03d.%03d.%03d.%03d",&tmp1,&tmp2,&tmp3,&tmp4)!=4 ||
        (tmp1 < 0 || tmp1 > 255 || tmp2 < 0 || tmp2 > 255 ||
         tmp3 < 0 || tmp3 > 255 || tmp4 < 0 || tmp4 > 255) )
    {
        printf("\nUsage: MCLIENT <x.x.x.x> <hours behind GMT>\n");
        exit(0);
    }

    tmp1 |= tmp2 << 8;
    tmp1 |= tmp3 << 16;
    tmp1 |= tmp4 << 24;

    // Get the time zone info
    if( argc != 3 ||  sscanf(argv[2],"%d",&tmp2) != 1 )
        tmp2 = 0;
    else
        tmp2 *= 3600;

    wVersionRequested = MAKEWORD(1, 1);
    tmp = WSAStartup(wVersionRequested, &wsaData);
    if (tmp != 0)
    {
        printf("\r\nUnable to initialize WinSock for host info");
        exit(0);
    }

    // Create the recv socket. This socket is always connected
    srecv = socket( AF_INET, SOCK_STREAM, 0 );
    if( srecv == INVALID_SOCKET )
    {
        printf("failed socket (%d)\n",WSAGetLastError());
        goto leave;
    }

    // Connect recv socket to port 3002
    sin.sin_family      = AF_INET;
    sin.sin_addr.s_addr = tmp1;
    sin.sin_port        = htons(3002);

    if( connect( srecv, &sin, sizeof(sin) ) < 0 )
    {
        printf("failed connect (%d)\n",WSAGetLastError());
        goto leave;
    }

    printf("\nMCLIENT Connected\nPress CTRL-C to Exit\n");

    ts = time(0);

    // Send command to set the DSP clock
    if( !peerCmd( srecv, CMD_SETCLOCK, ts-tmp2 ) )
        goto leave;

    // Send command to show the clock
    if( !peerCmd( srecv, CMD_SHOWCLOCK, showClock ) )
        goto leave;

    // Send command to turn off the grid
    if( !peerCmd( srecv, CMD_SHOWDOTS, showGrid ) )
        goto leave;

    frames = 0;
    motframes = 0;
    size_lo = 999999;
    size_hi = 0;
    size_tot = 0;

client_loop:
    while(!kbhit())
    {
        // Read the file size if the next JPG image
        readsize = 0;
        while( readsize < 4 )
        {
            tmp = recv( srecv, ((char *)&size)+readsize, 4-readsize, 0 );
            if( tmp < 0 )
            {
                printf("Socket Read Error (%d)\n",WSAGetLastError());
                goto leave;
            }
            if( !tmp )
            {
                printf("Socket Read Peer Disconnect!\n");
                goto leave;
            }
            readsize += tmp;
        }

        // When the size is non-zero, read the JPG file data next
        readsize = 0;
        while( readsize < size )
        {
            tmp = recv( srecv, recbuf+readsize, size-readsize, 0 );
            if( tmp < 0 )
            {
                printf("Socket Read Error (%d)\n",WSAGetLastError());
                goto leave;
            }
            if( !tmp )
            {
                printf("Socket Read Peer Disconnect!\n");
                goto leave;
            }
            readsize += tmp;
        }

        // If we're recording, recode this video image
        if( recording && size )
        {
            fwrite(&size,4,1,vidfile);
            fwrite(recbuf,size,1,vidfile);
            if( ++recframes > 5000 )
            {
                printf("STOPPED [5000 frame limit]\n");
                recording = 0;
                recframes = 0;
                fclose(vidfile);
            }
        }

        // If we got a motion frame, add its size to the stats
        frames++;
        if( size )
        {
            motframes++;
            size_tot += size;
            if( size < size_lo )
                size_lo = size;
            if( size > size_hi )
                size_hi = size;
        }

        // See if its time to print the stats
        tn = time(0);
        if( tn > ts )
        {
            if( !motframes )
                printf("%d frames (none sent) [%d]\n",frames,recframes);
            else
            {
                printf("%d frames (%d) [%d] Lo: %d  Hi: %d  Avg: %d\n",
                        frames,motframes,recframes,size_lo,size_hi,size_tot/motframes);
            }

            ts = tn;
            frames = 0;
            motframes = 0;
            size_lo = 999999;
            size_hi = 0;
            size_tot = 0;
        }

        // If we're playing, replace the video with a file read
        if( playing )
        {
            // Read next frame if not paused
            if( pause != 1 )
            {
                tmp = fread(&readsize,1,4,vidfile);
                if( tmp != 4 )
                {
                    printf("STOPPED [EOF]\n");
                    fclose(vidfile);
                    closesocket( ssend );
                    playing = 0;
                    pause = 0;
                    playsize = 0;
                }
                else
                {
                    fread(playbuf,1,readsize,vidfile);
                    playsize = readsize;
                }
            }

            // Do this for frame advance
            if( pause > 1 )
                pause--;

            // Send the data
            if( playsize )
            {
                // Send the encoded file

                // Send the file size
                if( send( ssend, (char *)&playsize, 4, 0 ) < 0 )
                {
                    c = 's';
                    goto proc_char;
                }

                // Send the file data
                if( send( ssend, playbuf, playsize, 0 ) < 0 )
                {
                    c = 's';
                    goto proc_char;
                }
            }
        }
    }

    // If we fell out of the loop, a key was pressed
    c = getch();

    // Make the key lower case
    if( c>='A' && c<='Z' )
        c += 'a'-'A';

proc_char:
    // Pause/Frame Advance
    if( c == 'f' )
    {
        if( playing )
        {
            printf("PAUSED\n");
            pause++;
        }
        else
            printf("Not Playing!\n");

    }
    // Get out of pause mode with 'p'
    else if( c == 'p' && pause > 0 && playing )
    {
        pause = 0;
        goto client_loop;
    }
    else if( c == 'g' )
    {
        showGrid ^= 1;

        // Send command to turn off the grid
        if( !peerCmd( srecv, CMD_SHOWDOTS, showGrid ) )
            goto leave;
    }
    else if( c == 'c' )
    {
        showClock ^= 1;

        // Send command to show the clock
        if( !peerCmd( srecv, CMD_SHOWCLOCK, showClock ) )
            goto leave;
    }
    else if( c == 's' || c=='p' || c=='r' || c== 'b' || c=='q' )
    {
        // If playing, disconnect the play socket. This tells
        // the DSP that there is no "play" data to display.
        if( playing )
            closesocket( ssend );

        // If recording or playing, set the internal status "stopped"
        if( recording || playing )
        {
            fclose(vidfile);
            playing = 0;
            pause = 0;
            recording = 0;
            recframes = 0;
        }

        if( c == 's' )
        {
            printf("STOPPED\n");
        }
        else if( c == 'r' )
        {
            if(!(vidfile = fopen("video.dat","wb")))
                printf("Can't open output file\n");
            else
            {
                printf("RECORDING\n");
                recframes = 0;
                recording = 1;
            }
        }
        else if( c == 'p' || c == 'b'  )
        {
            if( c == 'p' )
                vidfile = fopen("video.dat","rb");
            else
                vidfile = fopen("battle.dat","rb");

            if( !vidfile )
                printf("Can't open input file\n");
            else
            {
                printf("PLAYING\n");
                playing = 1;
                pause = 0;

                // Create to send socket to send out JPG images
                ssend = socket( AF_INET, SOCK_STREAM, 0 );
                if( ssend == INVALID_SOCKET )
                {
                    printf("failed socket (%d)\n",WSAGetLastError());
                    goto leave;
                }

                // Connect to the same IP address, but on port 3001
                sin.sin_family      = AF_INET;
                sin.sin_addr.s_addr = tmp1;
                sin.sin_port        = htons(3001);

                if( connect( ssend, &sin, sizeof(sin) ) < 0 )
                {
                    printf("failed connect (%d)\n",WSAGetLastError());
                    c = 's';
                    goto proc_char;
                }
            }
        }
        else if( c == 'q' )
        {
            printf("QUIT\n");
            closesocket( ssend );
            closesocket( srecv );
            goto leave;
        }
    }
    else
        printf("Keys: [Q]uit, [R]ecord, [P]lay, [S]top, [F]rameByFrame, [G]rid, [C]lock\n");

    goto client_loop;

leave:
    printf("\nABORT\n");

    WSACleanup();
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -