Esempio n. 1
0
/******************************************************************************
 * non-static function definitions
 ******************************************************************************/
int main(int argc, char **argv)
{
	int retval;
	struct handles handles = {
			.sock = -1,
			.sock2 = -1,
			.server_sock = -1,
			.file = NULL,
			.file2 = NULL,
	};
	struct scope_parameter param;

	if(0 != handle_options(argc,argv, &g_options))
	{
		usage(argv[0]);
		return 1;
	}

	signal_init();

	if (scope_init(&param, &g_options)) {
		retval = 2;
		goto cleanup;
	}

	if (g_options.mode == client || g_options.mode == server) {
		if (connection_init(&g_options, &handles)) {
			retval = 3;
			goto cleanup_scope;
		}
	} else if (g_options.mode == file) {
		if (file_open(&g_options, &handles)) {
			retval = 4;
			goto cleanup_scope;
		}
	}

	retval = 0;
	while (!transfer_interrupted()) {
		if (g_options.mode == client || g_options.mode == server) {
			if (connection_start(&g_options, &handles) < 0) {
				fprintf(stderr, "%s: problem opening connection.\n", __func__);
				continue;
			}
		}

		retval = transfer_data(&param, &g_options, &handles);
		if (retval && !transfer_interrupted())
			fprintf(stderr, "%s: problem transferring data.\n", __func__);

		if (g_options.mode == client || g_options.mode == server)
			connection_stop(&handles);

		if (g_options.mode == file)
			break;
	}

	connection_cleanup(&handles);
	file_close(&handles);

cleanup_scope:
	scope_cleanup(&param, &g_options);
cleanup:
	signal_exit();

	return retval;
}
Esempio n. 2
0
void stream_video(void) {
    char stream_buf[VIDEO_INBUF_SZ + 16]; // padded so libavcodec detects the end
    SOCKET videoSocket = INVALID_SOCKET;
    int keep_waiting = 0;

    if (g_ip != NULL) {
        videoSocket = connectDroidCam(g_ip, g_port);
        if (videoSocket == INVALID_SOCKET) {
            return;
        }
    }
    v_running  =1;
_wait:
    // We are the server
    if (videoSocket == INVALID_SOCKET) {
        videoSocket = accept_inet_connection(g_port);
        if (videoSocket == INVALID_SOCKET) { goto _out; }
        keep_waiting = 1;
    }
    {
        int L = sprintf(stream_buf, VIDEO_REQ, g_webcam_w, g_webcam_h);
        if ( SendRecv(1, stream_buf, L, videoSocket) <= 0 ){
            MSG_ERROR("Connection lost!");
            goto _out;
        }
        dbgprint("Sent request, ");
    }
    memset(stream_buf, 0, sizeof(stream_buf));
    if ( SendRecv(0, stream_buf, 5, videoSocket) <= 0 ){
        MSG_ERROR("Connection reset!\nDroidCam is probably busy with another client");
        goto _out;
    }

    if (decoder_prepare_video(stream_buf) == FALSE) { goto _out; }

    while (1){
        int frameLen;
        char *p = stream_buf;
        if (SendRecv(0, p, 4, videoSocket) == FALSE) goto _out;
        make_int4(frameLen, p[0], p[1], p[2], p[3]);
        SetImageFrameSize(frameLen);

        p = GetImageFrameBuf();
        while (frameLen > 4096) {
            if (SendRecv(0, p, 4096, videoSocket) == FALSE) goto _out;
            frameLen -= 4096;
            p += 4096;
        }
        if (SendRecv(0, p, frameLen, videoSocket) == FALSE) goto _out;
        if (DecodeFrame() == FALSE) break;
    }

_out:
    dbgprint("disconnect\n");
    disconnect(videoSocket);
    decoder_cleanup();

    if (keep_waiting){
        videoSocket = INVALID_SOCKET;
        goto _wait;
    }
    v_running = 0;
    connection_cleanup();
}