// caplive.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <winsock2.h>
#include <windows.h>
#include <vfw.h>

SOCKET s;

struct hdr
{
    int type;
    int size;
};
char odata[320*240*3];
unsigned char data[8 + 320*240*3];

LRESULT CALLBACK vclbk(
  HWND hWnd,         
  LPVIDEOHDR lpVHdr  
)
{
    int stride = 320*3;
    for (int j = 0; j < 240; j++)
        for (int i = 0; i < 320; i++)
        {
            data[sizeof(hdr) + stride * (239 - j) + i*3] = lpVHdr->lpData[stride * j + i*3 + 2];
            data[sizeof(hdr) + stride * (239 - j) + i*3 + 1] = lpVHdr->lpData[stride * j + i*3 + 1];
            data[sizeof(hdr) + stride * (239 - j) + i*3 + 2] = lpVHdr->lpData[stride * j + i*3];
        }
 
    hdr *h = (hdr*)data;

    static bool firstframe = true;
    if (firstframe || true)
    {
        firstframe = false;
        h->type = 0xfeedface;
        memcpy(odata, data + 8, sizeof(odata));
    }
    else
    {
        h->type = 0xfeedf00d;
        for (int i = 0; i < 320*240*3; i++)
        {
            char ch = odata[i];
            odata[i] = data[sizeof(hdr) + i];
            data[sizeof(hdr) + i] -= ch;
        }
    }
    h->size = sizeof(data) - 8;
    int total = 0;
    while (total < sizeof(data))
    {
        int r = send(s, (char*)data + total, sizeof(data) - total, 0);
        if (r < 0)
        {
            printf("error sending\n");
            ExitProcess(0);
        }
        total += r;
    }

    static int frame = 0;
    printf("frame %d\n", ++frame);
    return TRUE;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int fps = 1;
    if (argc > 1)
        fps = atoi(argv[1]);

    WORD wVersionRequested = MAKEWORD(2, 2);
    WSADATA wsaData;
    WSAStartup(wVersionRequested, &wsaData);

    s = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in sin;
    memset(&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = inet_addr("10.10.10.2");
    sin.sin_port = htons(7775);
    if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) != 0)
    {
        fprintf(stderr, "cannot connect to host\n");
        return 1;
    }
    HWND hWndC = capCreateCaptureWindow ((LPSTR) "My Capture Window", WS_OVERLAPPED, 
    0, 0, 320, 240,              // window position and dimensions
    (HWND) NULL, (int) 0); 

    if (hWndC == NULL)
    {
        fprintf(stderr, "cannot create capture window\n");
        return 1;
    }

    BOOL fOK = SendMessage (hWndC, WM_CAP_DRIVER_CONNECT, 0, 0L); 
    if (!fOK)
    {
        fprintf(stderr, "cannot connect to capture driver\n");
        return 1;
    }

    capSetCallbackOnVideoStream(hWndC, vclbk);

    CAPTUREPARMS params;
    capCaptureGetSetup(hWndC, &params, sizeof(params));
    params.dwRequestMicroSecPerFrame = 1000000 / fps;
    capCaptureSetSetup(hWndC, &params, sizeof(params));

    capCaptureSequenceNoFile(hWndC);

    capDriverDisconnect (hWndC);         

	return 0;
}


