Newer
Older
PrismSoftware / GSTtest / main.cpp
#include <iostream>
//#include <cpprest/http_client.h>
#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <thread>
#include "myOpenCV.h"

using namespace std;

void keep_alive_timer(string keep_alive_message);
void udp_socket_stream();
void VideoStream();

//#define ADDR "192.168.71.50"
#define ADDR "192.168.23.158"
#define V6_ADDR "fe80::76fe:48ff:fe3c:518c"
//const string SERVER = "192.168.71.50";	//ip address of udp server
const string SERVER = ADDR;	//ip address of udp server
const string KA_DATA_MSG = "{\"type\": \"live.data.unicast\", \"key\": \"some_GUID\", \"op\": \"start\"}";
const int BUFLEN = 512;	//Max length of buffer
const int PORT = 49152;	//The port on which to listen for incoming data
struct sockaddr_in si_other;
SOCKET s;

int data_stream_main()
{
	printf("TOBII GLASSES 2 exampel in c++ \n");


	WSAData data;
	int err = WSAStartup(MAKEWORD(2, 2), &data);
	if (err != 0)
	{
		cout << "Failed. Error Code : " << err;
		exit(EXIT_FAILURE);
	}

	int slen = sizeof(si_other);
	char buf[BUFLEN];
	char message[BUFLEN];
	WSADATA wsa;

	//Initialise winsock
	printf(" Initialising Winsock...\n");
	if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
	{
		printf(" Failed.Error Code : %d", WSAGetLastError());
		Sleep(10000);
		exit(EXIT_FAILURE);
	}

	printf(" Create Socket \n");
	if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR)
	{
		printf(" socket() failed with error code : %d", WSAGetLastError());
		Sleep(10000);
		exit(EXIT_FAILURE);
	}

	//setup address structure
	memset((char*)&si_other, 0, sizeof(si_other));
	si_other.sin_family = AF_INET;
	si_other.sin_port = htons(PORT);
	InetPton(si_other.sin_family, SERVER.c_str(), &si_other.sin_addr.S_un.S_addr);
	//si_other.sin_addr.S_un.S_addr = inet_addr(SERVER.c_str());

	printf("Will now connect and show streamed data\n");
	// OutputStream Keep alive thread
	std::thread keep_alive_thread(keep_alive_timer, KA_DATA_MSG); // change or add threads with more messages
	Sleep(1000);

	// InputStream Read udp socket stream
	std::thread udp_socket_stream(udp_socket_stream);
	std::thread video_stream(VideoStream);

	keep_alive_thread.join();
	udp_socket_stream.join();
	video_stream.join();

	return 0;
}

void keep_alive_timer(const string keep_alive_message) {
	/*Send Keep Alive messages every second */

	int slen = sizeof(si_other);
	char message[BUFLEN];
	WSADATA wsa;

	std::string data_message = keep_alive_message;
	strcpy_s(message, BUFLEN, data_message.c_str());


	printf("Start sending keep alive messages \n");
	while (1)
	{
		printf("Keep Alive Data Sent \n");
		if (sendto(s, message, strlen(message), 0, (struct sockaddr*) & si_other, slen) == SOCKET_ERROR)
		{
			printf(" sendto() failed with error code : %d", WSAGetLastError());
			Sleep(10000);
			exit(EXIT_FAILURE);
		}
		Sleep(2000);
	}

	closesocket(s);
	WSACleanup();
}

void udp_socket_stream() {
	/*
	Read and print Stream from udp socket.
	*/

	int slen = sizeof(si_other);
	char buf[BUFLEN];

	while (1)
	{
		//clear the buffer by filling null, it might have previously received data
		memset(buf, '\0', BUFLEN);
		//try to receive some data, this is a blocking call
		if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr*) & si_other, &slen) == SOCKET_ERROR)
		{
			printf(" recvfrom() failed with error code : %d", WSAGetLastError());
			Sleep(1000);
			exit(EXIT_FAILURE);
		}

		puts(buf);
	}

	closesocket(s);
	WSACleanup();

}

void VideoStream() {
	VideoCapture cap("rtsp://" ADDR ":8554/live/scene");
	//VideoCapture cap("rtsp://[" V6_ADDR ":8554]/live/scene");
	if (!cap.isOpened()) {
		printf("cannot open camera " ADDR "\n");
	}
	//Mat img = Mat(240, 320, CV_8UC3, CV_RGB(128, 0, 0));
	namedWindow("gsttest");
	while (1) {
		Mat frame;
		cap >> frame;
		Mat disp;
		resize(frame, disp, Size(), 0.5, 0.5);
		imshow("gsttest", disp);
		if (waitKey(1) == 'q') break;
	}
}

int main() {
	//VideoStream();
	data_stream_main();

	return 0;
}