-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathremote_command.c
229 lines (204 loc) · 6.39 KB
/
remote_command.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <stdio.h>
#include <amqp.h>
#include <amqp_framing.h>
#include <amqp_tcp_socket.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 "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)
{
if (envelope_len > BUFFER_SIZE)
{
C("Got a filename > 256 bytes (%d)", envelope_len);
return;
}
unsigned char buf[BUFFER_SIZE] = {0};
memcpy(buf, envelope_bytes, envelope_len);
unpack(buf, "s", cam_filename);
}
static int check_amqp_error(amqp_rpc_reply_t x, char const* context)
{
switch (x.reply_type)
{
case AMQP_RESPONSE_NORMAL:
return 0;
default:
C("Error: %d %s", x.reply_type, context);
return 1;
}
}
static int amqp_connect(amqp_connection_state_t* conn, const char* const username,
const char* const password, const char* const vhost,
const char* const hostname, const int port)
{
int status;
amqp_socket_t* socket;
amqp_rpc_reply_t reply_status;
*conn = amqp_new_connection();
socket = amqp_tcp_socket_new(*conn);
if (!socket)
{
E("Unable to create socket");
return 1;
}
status = amqp_socket_open(socket, hostname, port);
if (status)
{
E("Unable to open socket");
return 1;
}
reply_status =
amqp_login(*conn, vhost, 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, username, password);
if (check_amqp_error(reply_status, "login"))
return 1;
amqp_channel_open(*conn, 1);
reply_status = amqp_get_rpc_reply(*conn);
if (check_amqp_error(reply_status, "channel open"))
return 1;
return 0;
}
static int amqp_run_consume(amqp_connection_state_t* conn, const char* const queue)
{
amqp_basic_consume(*conn, 1, amqp_cstring_bytes(queue), amqp_empty_bytes, 0, 0, 0,
amqp_empty_table);
if (check_amqp_error(amqp_get_rpc_reply(*conn), "consume"))
return 1;
I("Listening to queue \"%s\"", queue);
return 0;
}
static void* dispatch_amqp(void* ignored)
{
amqp_connection_state_t conn;
int status;
const char* const username = configvar_string("AIC_PLAYER_AMQP_USERNAME");
const char* const password = configvar_string("AIC_PLAYER_AMQP_PASSWORD");
const char* const host = configvar_string("AIC_PLAYER_AMQP_HOST");
const char* const vmid = configvar_string("AIC_PLAYER_VM_ID");
char queue[256];
snprintf(queue, sizeof(queue), "android-events.%s.camera", vmid);
status = amqp_connect(&conn, username, password, "/", host, 5672);
if (status)
ERR("Failed to connect to AMQP");
status = amqp_run_consume(&conn, queue);
if (status)
ERR("Failed to consume AMQP messages");
char filename[BUFFER_SIZE] = {0};
while (1)
{
amqp_envelope_t env;
amqp_rpc_reply_t reply = amqp_consume_message(conn, &env, NULL, 0);
if (reply.reply_type == AMQP_RESPONSE_NORMAL)
{
unpack_cam_data(filename, env.message.body.bytes, env.message.body.len);
I("Received file id %s", filename);
pthread_mutex_lock(&static_device_mtx);
switch_video_files(dev, (const char* const) filename);
pthread_mutex_unlock(&static_device_mtx);
}
amqp_destroy_envelope(&env);
}
return 0;
}
pthread_t* start_amqp_thread()
{
pthread_mutex_init(&static_device_mtx, NULL);
pthread_create(&amqp_listen, NULL, &dispatch_amqp, NULL);
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
*/
void switch_device(CameraDevice* new_dev)
{
pthread_mutex_lock(&static_device_mtx);
dev = new_dev;
pthread_mutex_unlock(&static_device_mtx);
}