Skip to content

Commit

Permalink
Add a way to control the camera from the test framework
Browse files Browse the repository at this point in the history
  • Loading branch information
Aic Project committed Nov 23, 2016
1 parent 128e7b6 commit e1a531f
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 32 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
CC?=gcc

all:
$(CC) camera-service.c misc.c camera-format-converters.c camera-capture-ffmpeg.c config_env.c net_pack.c logger.c amqp_rc.c -lavcodec -lavformat -lavutil -lswscale -ggdb -Wall -O3 -o camera-service -lrabbitmq -lpthread `pkg-config --cflags glib-2.0` -lglib-2.0
$(CC) camera-service.c misc.c camera-format-converters.c camera-capture-ffmpeg.c config_env.c net_pack.c logger.c remote_command.c -lavcodec -lavformat -lavutil -lswscale -ggdb -Wall -O3 -o camera-service -lrabbitmq -lpthread `pkg-config --cflags glib-2.0` -lglib-2.0

debug:
$(CC) camera-service.c misc.c camera-format-converters.c camera-capture-ffmpeg.c config_env.c net_pack.c logger.c amqp_rc.c -lavcodec -lavformat -lavutil -lswscale -ggdb -Wall -Wextra -fsanitize=address -fstack-protector -DFORTIFY_SOURCE=2 -Og -o camera-service -lrabbitmq -lpthread `pkg-config --cflags glib-2.0` -lglib-2.0
$(CC) camera-service.c misc.c camera-format-converters.c camera-capture-ffmpeg.c config_env.c net_pack.c logger.c remote_command.c -lavcodec -lavformat -lavutil -lswscale -ggdb -Wall -Wextra -fsanitize=address -fstack-protector -DFORTIFY_SOURCE=2 -Og -o camera-service -lrabbitmq -lpthread `pkg-config --cflags glib-2.0` -lglib-2.0

clean:
rm -f camera-service
Expand Down
28 changes: 3 additions & 25 deletions camera-service.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#include "config_env.h"
#include "misc.h"
#include "logger.h"
#include "amqp_rc.h"
#include "remote_command.h"

#define LOG_TAG "camera-service"
#define T(...) D(__VA_ARGS__)
Expand Down Expand Up @@ -1314,41 +1314,19 @@ android_list_web_cameras(void)
}
// clang-format on

static int camera_connect(const char* ip, short port)
{
struct addrinfo* result;
int sock, error, yes = 1;
;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
return -1;
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(int));
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));

if (getaddrinfo(ip, NULL, NULL, &result))
{
perror("getaddrinfo()");
return -1;
}

((struct sockaddr_in*) result->ai_addr)->sin_port = htons(port);
((struct sockaddr_in*) result->ai_addr)->sin_family = AF_INET;

error = connect(sock, result->ai_addr, sizeof(struct sockaddr));
freeaddrinfo(result);
return (error == -1 ? error : sock);
}

int main(int argc, char** argv)
{
init_logger();
char* vmip = configvar_string("AIC_PLAYER_VM_HOST");

start_amqp_thread();
start_socket_thread();
pthread_mutex_init(&dec_mtx, NULL);

while (1)
{
int sock = camera_connect(vmip, 24800);
int sock = connect_socket(vmip, 24800);
if (sock == -1)
{
C("Could not connect to the VM at %s", vmip);
Expand Down
96 changes: 93 additions & 3 deletions amqp_rc.c → remote_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,53 @@
#include <amqp.h>
#include <amqp_framing.h>
#include <amqp_tcp_socket.h>

#include "amqp_rc.h"
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>

#include "remote_command.h"
#include "camera-capture-ffmpeg.h"
#include "config_env.h"
#include "logger.h"
#include "net_pack.h"

#define LOG_TAG "amqp"
#define LOG_TAG "remote_control"
#define BUFFER_SIZE 256

static CameraDevice* dev = NULL;

static pthread_mutex_t static_device_mtx;

static pthread_t amqp_listen;
static pthread_t socket_listen;

int connect_socket(const char* ip, short port)
{
struct addrinfo* result;
int sock, error, yes = 1;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
return -1;
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(int));
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));

if (getaddrinfo(ip, NULL, NULL, &result))
{
perror("getaddrinfo()");
return -1;
}

((struct sockaddr_in*) result->ai_addr)->sin_port = htons(port);
((struct sockaddr_in*) result->ai_addr)->sin_family = AF_INET;

error = connect(sock, result->ai_addr, sizeof(struct sockaddr));
freeaddrinfo(result);
return (error == -1 ? error : sock);
}

static void unpack_cam_data(char* cam_filename, void* envelope_bytes, size_t envelope_len)
{
Expand Down Expand Up @@ -128,6 +160,64 @@ pthread_t* start_amqp_thread()
return &amqp_listen;
}

/** Remote control from the VM
*
* We connect to the port 32500 on the virtual machine,
* and we then receive video file switch instructions from the VM
*/

static void* socket_dispatch_thread(void* args)
{
const char* const vmip = configvar_string("AIC_PLAYER_VM_HOST");
while (1)
{
int sock = connect_socket(vmip, 32600);
if (sock <= 0)
{
sleep(1);
continue;
}
while (1)
{
struct timeval timeout = {5, 0};
char buf[BUFFER_SIZE];

fd_set fd_read;
FD_ZERO(&fd_read);
FD_SET(sock, &fd_read);

if (select(sock + 1, &fd_read, 0, 0, &timeout) == -1)
break;
if (FD_ISSET(sock, &fd_read))
{
ssize_t len = recv(sock, buf, sizeof(buf), 0);
if (len <= 0)
{
W("Error received from the VM, reconnecting");
break;
}
else
{
char filename[BUFFER_SIZE] = {0};
unpack_cam_data(filename, buf, len);
I("Received file name %s", filename);
pthread_mutex_lock(&static_device_mtx);
switch_video_files(dev, (const char* const) filename);
pthread_mutex_unlock(&static_device_mtx);
}
}
}
close(sock);
}
return NULL;
}

pthread_t* start_socket_thread()
{
pthread_create(&socket_listen, NULL, &socket_dispatch_thread, NULL);
return &socket_listen;
}

/**
* Update the camera device pointer
*/
Expand Down
6 changes: 4 additions & 2 deletions amqp_rc.h → remote_command.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#ifndef _AMQP_RC_H_
#define _AMQP_RC_H_
#ifndef _RC_H_
#define _RC_H_

#include <pthread.h>
#include "camera-common.h"

int connect_socket(const char* ip, short port);
void switch_device(CameraDevice* new_dev);
pthread_t* start_amqp_thread(void);
pthread_t* start_socket_thread(void);
#endif

0 comments on commit e1a531f

Please sign in to comment.