aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2022-10-22 21:08:08 +0300
committerkartofen <mladenovnasko0@gmail.com>2022-10-22 21:08:08 +0300
commitf4c66a4c77e5818ca9703f59a80ea92e27ed45c6 (patch)
treee334b3f1c8bdb5c96bd756be4e451836a7762ab4
parent9d88d471ac059c93523c00b96ae1cbbc52e07b69 (diff)
abstract messages for easier use
-rw-r--r--src/display.c16
-rw-r--r--src/listener.c15
-rw-r--r--src/socket.c18
-rw-r--r--src/socket.h6
-rw-r--r--src/talker.c17
-rw-r--r--src/typedef.h24
6 files changed, 57 insertions, 39 deletions
diff --git a/src/display.c b/src/display.c
index ba9bc48..fa3d573 100644
--- a/src/display.c
+++ b/src/display.c
@@ -17,19 +17,19 @@ static void key_callback(GLFWwindow* window, int key, int scancode, int action,
}
extern unsigned char cam_data[];
+extern unsigned int WIDTH;
+extern unsigned int HEIGHT;
-unsigned char WIDTH = 1;
-unsigned char HEIGHT = 1;
GLubyte pixels[BUF_CAP] = {0};
-void pixel(int x, int y, char *buf)
+void pixel(int x, int y)
{
// for rotating the screen
int p = (y*WIDTH + x)*3;
int b = ((HEIGHT-1-y)*WIDTH+x)*3;
- pixels[p+0] = buf[b+0];
- pixels[p+1] = buf[b+1];
- pixels[p+2] = buf[b+2];
+ pixels[p+0] = cam_data[b+0];
+ pixels[p+1] = cam_data[b+1];
+ pixels[p+2] = cam_data[b+2];
}
int display(void)
@@ -56,13 +56,11 @@ int display(void)
while (!glfwWindowShouldClose(window))
{
- WIDTH = MAX(cam_data[0], 1);
- HEIGHT = MAX(cam_data[1], 1);
glfwSetWindowSize(window, WIDTH, HEIGHT);
for(int i = 0; i < HEIGHT; i++)
for(int j = 0; j < WIDTH; j++)
- pixel(j, i, &(cam_data[2]));
+ pixel(j, i);
glClear(GL_COLOR_BUFFER_BIT);
glDrawPixels(WIDTH, HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
diff --git a/src/listener.c b/src/listener.c
index b344b6e..f819383 100644
--- a/src/listener.c
+++ b/src/listener.c
@@ -10,7 +10,9 @@
#include "audio.h"
#include "typedef.h"
-char cam_data[BUF_CAP];
+char cam_data[BUF_CAP] = {0};
+unsigned int WIDTH = 1;
+unsigned int HEIGHT = 1;
void *display_thread(void *arg)
{
@@ -24,18 +26,17 @@ void *display_thread(void *arg)
exit(r);
}
-void on_recv(char *buf, int numbytes)
+void on_recv(message *m)
{
- (void)numbytes;
+ WIDTH = m->WIDTH;
+ HEIGHT = m->HEIGHT;
- memcpy(cam_data, &(buf[REC_CAP]), BUF_CAP-REC_CAP);
- audio_play(buf);
+ memcpy(cam_data, m->video, WIDTH * HEIGHT * 3);
+ audio_play(m->audio);
}
int main(void)
{
- memset(cam_data, 0, BUF_CAP);
-
pthread_t tid;
pthread_create(&tid, NULL, display_thread, NULL);
diff --git a/src/socket.c b/src/socket.c
index 2ce4900..2efe53a 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -21,7 +21,7 @@ void *get_in_addr(struct sockaddr *sa)
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
-int listener(char *port, void (*on_recv)(char *, int))
+int listener(char *port, void (*on_recv)(message *))
{
int sockfd;
int ret;
@@ -73,17 +73,18 @@ int listener(char *port, void (*on_recv)(char *, int))
i = 0;
}
- char buf[BUF_CAP] = {0};
+ message m = {0};
struct sockaddr_storage their_addr;
socklen_t addr_len = sizeof(their_addr);
int numbytes;
- if ((numbytes = recvfrom(sockfd, buf, BUF_CAP-1 , 0,
+ if((numbytes = recvfrom(sockfd, &m, BUF_CAP, 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
return 1;
}
+
#ifdef LOG_INFO_STDOUT
char s[INET6_ADDRSTRLEN];
info("listener: got packet from %s, %d bytes long\n",
@@ -92,7 +93,7 @@ int listener(char *port, void (*on_recv)(char *, int))
s, sizeof(s)), numbytes);
#endif
- on_recv(buf, numbytes);
+ on_recv(&m);
i++;
}
@@ -100,7 +101,7 @@ int listener(char *port, void (*on_recv)(char *, int))
return 0;
}
-int talker(char *port, char *address, void (*on_send)(char *, int *))
+int talker(char *port, char *address, void (*on_send)(message *))
{
int sockfd;
@@ -142,12 +143,11 @@ int talker(char *port, char *address, void (*on_send)(char *, int *))
i = 0;
}
- char buf[BUF_CAP] = {0};
- int bytes;
- on_send(buf, &bytes);
+ message m = {0};
+ on_send(&m);
int numbytes;
- if((numbytes = sendto(sockfd, buf, bytes, 0,
+ if((numbytes = sendto(sockfd, &m, MESSAGE_SZ, 0,
p->ai_addr, p->ai_addrlen)) == -1) {
perror("talker: sendto");
return 1;
diff --git a/src/socket.h b/src/socket.h
index ecb27ab..1868afc 100644
--- a/src/socket.h
+++ b/src/socket.h
@@ -1,8 +1,10 @@
#ifndef SOCKET_H
#define SOCKET_H
-int listener(char *port, void (*on_recv)(char *, int));
+#include "typedef.h"
-int talker(char *port, char *address, void (*on_send)(char *, int *));
+int listener(char *port, void (*on_recv)(message *));
+
+int talker(char *port, char *address, void (*on_send)(message *));
#endif
diff --git a/src/talker.c b/src/talker.c
index bfea616..e7271ad 100644
--- a/src/talker.c
+++ b/src/talker.c
@@ -15,25 +15,22 @@
camera_handle cam_handle;
-void on_send(char *buf, int *bytes)
+void on_send(message *m)
{
- audio_record(buf);
+ audio_record(m->audio);
- int image_sz = X_RES*Y_RES*3;
- buf[REC_CAP+0] = (unsigned char)X_RES;
- buf[REC_CAP+1] = (unsigned char)Y_RES;
+ m->WIDTH = X_RES;
+ m->HEIGHT = Y_RES;
- *bytes = REC_CAP + 2 + image_sz;
-
- memcpy(&(buf[REC_CAP+2]), camera_get_image(&cam_handle), image_sz);
+ memcpy(m->video, camera_get_image(&cam_handle), X_RES * Y_RES * 3);
}
int main(void)
{
camera_params params = {0};
params.device = CAM;
- params.x_res = 160;
- params.y_res = 120;
+ params.x_res = X_RES;
+ params.y_res = Y_RES;
camera_init(&cam_handle, params);
diff --git a/src/typedef.h b/src/typedef.h
index c585f7a..b6b2b29 100644
--- a/src/typedef.h
+++ b/src/typedef.h
@@ -12,7 +12,27 @@
#define info(...) fprintf(stdout, __GREEN__"[INFO]"__RESET__" "__FILE__":"stringize(__LINE__)": "__VA_ARGS__)
#define err(...) fprintf(stderr, __RED__"[ERR]"__RESET__" "__FILE__":"stringize(__LINE__)": "__VA_ARGS__)
-#define BUF_CAP 65536 // max datagram size
-#define REC_CAP 5880 // 30th of a second of audio
+// ----- MESSAGE ----- //
+
+#define BUF_CAP 65500 // max datagram size with some bytes left for the headers and things
+
+#define FRAMES 32
+#define BITRATE 16
+#define SAMPLING_RATE 44100
+#define CHANNELS 2
+
+#define TEMP_BUF_SZ (FRAMES * CHANNELS * 2) // 2 bytes per sample
+#define REC_CAP ((BITRATE * SAMPLING_RATE * CHANNELS)/240) // 30th of a second of audio
+
+#define VID_CAP (BUF_CAP - ((2*(sizeof(int))) + REC_CAP))
+
+typedef struct message {
+ char audio[REC_CAP];
+ unsigned int WIDTH;
+ unsigned int HEIGHT;
+ char video[VID_CAP];
+} message;
+
+#define MESSAGE_SZ sizeof(message)
#endif