Commit f2c870e8 authored by belen's avatar belen
Browse files

B

parent 5c22261b
......@@ -11,7 +11,7 @@
#include <arpa/inet.h>
#include <netdb.h>
#include <iostream>
#include <sys/types.h>
using namespace cv;
using namespace std;
......@@ -19,6 +19,15 @@ using namespace std;
#define PORT "3490"
#define HOST "localhost"
#define MAXLEN 1024
#define MAX_MSG_SIZE 65536
struct parametros{
int client_socket;
int socket_de_clienteUDP;
};
void stream(parametros);
void menu(parametros);
int main(void){
......@@ -44,7 +53,10 @@ int main(void){
int send(client_socket, aux, 1, 0);
pthread_t hilo_menu;
if (int pthread_create(&hilo_menu, NULL, menu(), NULL) != 0) perror("Error al abrir el menu");
struct parametros *p;
p->client_socket= client_socket;
p->socket_de_clienteUDP=0;
int pthread_create(&hilo_menu, NULL, (void *) menu, p);
// se desplega menu
......@@ -52,19 +64,19 @@ int main(void){
}
void menu(){
void menu(parametros *p){
//abro socket UDP para stream
int socket_de_clienteUDP = socket(AF_INET, SOCK_DGRAM, 0);
//obtenemos la direccion con getaddrinfo
struct addrinfo hints, *res;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
getaddrinfo(HOST, PORT, &hints, &res);
struct addrinfo hints2, *res2;
hints2.ai_family = AF_INET;
hints2.ai_socktype = SOCK_DGRAM;
getaddrinfo(HOST, PORT, &hints2, &res2);
p->socket_de_clienteUDP=socket_de_clienteUDP;
//ejecuto stream
pthread_t hilo_stream;
if (int pthread_create(&hilo_stream, NULL, stream(), NULL) != 0) perror("No se pudo reproducir el video");//separo thread (idcliente, udp)
int pthread_create(&hilo_stream, NULL, (void*)stream, p);
bool reproduciendo = true;
bool pausado = false;
......@@ -81,26 +93,38 @@ void menu(){
cin << opcion;
//primitiva SEND, retorna sent_msg..=-1 error o el numero bytes enviados
switch (opcion){
case 1:
//enviar al servidor
char *msg = "1";
int msg_size = strlen(msg);
int sent_msg_size = send(p->client_socket, msg, msg_size, 0);
reproduciendo = true;
pausado = false;
break;
case 2:
//enviar al servidor
char *msg = "2";
int msg_size = strlen(msg);
int sent_msg_size = send(p->client_socket, msg, msg_size, 0);
pausado = false;
break;
case 3:
//enviar al servidor
char *msg = "3";
int msg_size = strlen(msg);
int sent_msg_size = send(p->client_socket, msg, msg_size, 0);
pausado = true;
break;
case 4:
//enviar al servidor
char *msg = "4";
int msg_size = strlen(msg);
int sent_msg_size = send(p->client_socket, msg, msg_size, 0);
reproduciendo = false;
break;
case 5:
//enviar al servidor
char *msg = "5";
int msg_size = strlen(msg);
int sent_msg_size = send(p->client_socket, msg, msg_size, 0);
pthread_cancel(hilo_stream);
close(socket_de_clienteUDP);
}
......@@ -111,17 +135,26 @@ void menu(){
}
void stream(){
void stream(parametros *p){
do {
//primitiva RECEIVE
char* dataV = malloc(MAX_MSG_SIZE);
struct sockaddr_in server_addr;
int server_addr_size = sizeof(server_addr);
int dataV_size = MAX_MSG_SIZE;
struct sockaddr_in server_addr2;
socklen_t *server_addr_size2 = sizeof(server_addr2);
int received_data_size = recvfrom(
socket_de_clienteUDP,
dataV, 65535, 0,
(struct sockaddr*)&server_addr, &server_addr_size
p->socket_de_clienteUDP,
dataV, dataV_size, 0,
(struct sockaddr*)&server_addr2, server_addr_size2
);
namedWindow("cliente", CV_WINDOW_AUTOSIZE);
Mat rawData = Mat(1, received_data_size, CV_8UC1, dataV); //matriz con la imagen recibida.
Mat frame = imdecode(rawData, CV_LOAD_IMAGE_COLOR); //decodifica la imagen
imshow("cliente", frame); //muestra la imagen en la ventana cliente
waitKey(1);
} while (true);
}
......
......@@ -47,7 +47,7 @@
<option id="gnu.cpp.compiler.exe.release.option.debugging.level.837509089" name="Debug Level" superClass="gnu.cpp.compiler.exe.release.option.debugging.level" useByScannerDiscovery="false" value="gnu.cpp.compiler.debugging.level.none" valueType="enumerated"/>
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="gnu.cpp.compiler.option.include.paths.1388316086" name="Include paths (-I)" superClass="gnu.cpp.compiler.option.include.paths" useByScannerDiscovery="false" valueType="includePath">
<option id="gnu.cpp.compiler.option.include.paths.1388316086" name="Include paths (-I)" superClass="gnu.cpp.compiler.option.include.paths" useByScannerDiscovery="false" valueType="includePath">
<listOptionValue builtIn="false" value="-I/usr/include/opencv"/>
......@@ -71,13 +71,13 @@
<tool id="cdt.managedbuild.tool.gnu.cpp.linker.exe.release.1849450641" name="GCC C++ Linker" superClass="cdt.managedbuild.tool.gnu.cpp.linker.exe.release">
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="gnu.cpp.link.option.paths.1468562094" name="Library search path (-L)" superClass="gnu.cpp.link.option.paths" useByScannerDiscovery="false" valueType="libPaths">
<option id="gnu.cpp.link.option.paths.1468562094" name="Library search path (-L)" superClass="gnu.cpp.link.option.paths" useByScannerDiscovery="false" valueType="libPaths">
<listOptionValue builtIn="false" value="/usr/include/opencv"/>
</option>
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="gnu.cpp.link.option.libs.242340579" name="Libraries (-l)" superClass="gnu.cpp.link.option.libs" useByScannerDiscovery="false" valueType="libs">
<option id="gnu.cpp.link.option.libs.242340579" name="Libraries (-l)" superClass="gnu.cpp.link.option.libs" useByScannerDiscovery="false" valueType="libs">
<listOptionValue builtIn="false" value="opencv_core"/>
......
......@@ -25,9 +25,22 @@ using namespace std;
#define PAUSA 3
#define FINALIZAR 4
#define SALIR 5
struct parametros{
int id_cliente;
Mat frame;
VideoCapture cap;
vector<uchar> encoded;
int &comunicacion[];
sockaddr_in client_addr;
socklen_t client_addr_size;
bool inicializado;
int socket_to_clientTCP;
};
pthread_mutex_t finalizar;
pthread_mutex_t pausa;
pthread_mutex_t mutex;
void streamVideo(parametros);
void controlVideo(parametros);
int main(void)
......@@ -92,8 +105,18 @@ int main(void)
pthread_mutex_init(&mutex, NULL);
pthread_t hilo_control;
struct parametros *p;
p->id_cliente= id_cliente;
p->frame= frame;
p->cap= cap;
p->encoded= encoded;
p->comunicacion=comunicacion;
p->client_addr= client_addr;
p->client_addr_size= client_addr_size;
p->inicializado=false;
p->socket_to_clientTCP=socket_to_clientTCP;
if (int pthread_create(&hilo_control, NULL, void * controlVideo(socket_to_clientTCP,id_cliente, comunicacion, frame, cap, encoded, client_addr, client_addr_size), NULL) != 0) perror("No se pudo ejecutar el programa de control"); //separo thread (idcliente, tcp)
int pthread_create(&hilo_control, NULL, (void *) controlVideo,p); //separo thread (idcliente, tcp)
......@@ -109,20 +132,18 @@ int main(void)
}//fin servidor
void controlVideo(int socket_to_clientTCP,int id_cliente, int &comunicacion[], Mat frame, VideoCapture cap, vector<uchar> encoded, sockaddr_in client_addr, socklen_t client_addr_size){
pthread_mutex_t pausa;
pthread_mutex_t finalizar;
void controlVideo(parametros *p){
pthread_mutex_init(&finalizar, NULL);
pthread_mutex_init(&pausa, NULL);
pthread_t hilo_stream;
bool inicializado = false;
if (int pthread_create(&hilo_stream, NULL, streamVideo(id_cliente, frame, cap, encoded, comunicacion, pausa, finalizar, client_addr, client_addr_size, inicializado), NULL) != 0) perror("No se pudo enviar el video");//separo thread (idcliente, udp)
int pthread_create(&hilo_stream, NULL, (void *)streamVideo, p);
int aux = 1;
char data;
while (aux != 5){
//primitiva RECEIVE
int recv(socket_to_clientTCP, data, 1, MSG_WAITALL);
int recv(p->socket_to_clientTCP, data, 1, MSG_WAITALL);
switch (data){
case 1:
pthread_mutex_unlock(&finalizar);
......@@ -142,26 +163,27 @@ void controlVideo(int socket_to_clientTCP,int id_cliente, int &comunicacion[], M
}
pthread_mutex_lock(&mutex);
aux = comunicacion[id_cliente];
aux = p->comunicacion[p->id_cliente];
pthread_mutex_unlock(&mutex);
}
pthread_join(hilo_stream, NULL);
}
void streamVideo(int id_cliente, Mat frame, VideoCapture cap, vector<uchar> encoded, int &comunicacion[], pthread_mutex_t &pausa, pthread_mutex_t &finalizar, sockaddr_in client_addr, socklen_t client_addr_size, bool inicializado){
if (!inicializado){
void streamVideo(parametros *p){
int socket_to_clientUDP;
if (!p->inicializado){
//primitiva SOCKET
int socket_to_clientUDP = socket(PF_INET, SOCK_DGRAM, 0);
socket_to_clientUDP = socket(PF_INET, SOCK_DGRAM, 0);
//primitiva BIND
bind(socket_to_clientUDP,(struct sockaddr*)&client_addr, client_addr_size);
inicializado = true;
bind(socket_to_clientUDP,(struct sockaddr*)&p->client_addr, p->client_addr_size);
p->inicializado = true;
}
pthread_mutex_lock(&mutex);
int aux = comunicacion[id_cliente];
int aux = p->comunicacion[p->id_cliente];
pthread_mutex_unlock(&mutex);
int tamaño_frame;
int tamaño_video = sizeof(cap);
int tamaño_video = sizeof(p->cap);
while ( aux != SALIR) { //MIRAR ESTO PARA CORREGIR
......@@ -169,41 +191,43 @@ void streamVideo(int id_cliente, Mat frame, VideoCapture cap, vector<uchar> enco
int pthread_mutex_unlock(&pausa);
//pasar las frames
cap >> frame; //obtener nuevo frame
imshow("servidor", frame); //muestra la imagen en la ventana servidor
p->cap >> p->frame; //obtener nuevo frame
imshow("servidor", p->frame); //muestra la imagen en la ventana servidor
waitKey(1000/30); //espera para ver las frames y que se reproduzca el video en el servidor
//convertir la imagen a texto para enviar
vector <int> compression_params;
compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
compression_params.push_back(80);
imencode(".jpg", frame, encoded, compression_params);
imencode(".jpg", p->frame, p->encoded, compression_params);
const char* data= p->encoded ;
//SEND
int sent_data_size = sendto(
socket_to_clientUDP,
encoded, encoded.size(), 0,
(struct sockaddr*)& client_addr, client_addr_size
data, sizeof(p->encoded), 0,
(struct sockaddr*) (p->client_addr),sizeof( p->client_addr)
);
tamaño_frame += sizeof(frame);
tamaño_frame += sizeof(p->frame);
if (tamaño_frame == tamaño_video){
pthread_mutex_lock(&mutex);
int aux = comunicacion[id_cliente] = 4;
int aux = p->comunicacion[p->id_cliente] = 4;
pthread_mutex_unlock(&mutex);
}
pthread_mutex_lock(&mutex);
int aux = comunicacion[id_cliente];
int aux = p->comunicacion[p->id_cliente];
pthread_mutex_unlock(&mutex);
if(aux == FINALIZAR){
pthread_mutex_lock(&finalizar); //tcp le da unlock cuando comunicacion[id] = INICIO
streamVideo(id_cliente, frame, cap, encoded, &comunicacion[], &pausa, &finalizar, client_addr, client_addr_size, inicializado);
streamVideo(p);
}
pthread_mutex_lock(&mutex);
aux = comunicacion[id_cliente];
aux = p->comunicacion[p->id_cliente];
pthread_mutex_unlock(&mutex);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment