Skip to content

Commit 52a6107

Browse files
committed
microphone initial work
1 parent a50c398 commit 52a6107

File tree

9 files changed

+198
-35
lines changed

9 files changed

+198
-35
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
indent_size = 4
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

lib/gamepad/audio.c

Lines changed: 107 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include <pthread.h>
1010
#include <stdint.h>
11+
#include <string.h>
12+
#include <sys/time.h>
1113

1214
#include "gamepad.h"
1315
#include "vanilla.h"
@@ -35,6 +37,96 @@ typedef struct {
3537
uint32_t video_format;
3638
} AudioPacketVideoFormat;
3739

40+
static unsigned char queued_audio[2048];
41+
static size_t queued_audio_start = 0;
42+
static size_t queued_audio_end = 0;
43+
static pthread_mutex_t queued_audio_mutex;
44+
45+
int send_audio_packet(const void *data, size_t len)
46+
{
47+
pthread_mutex_lock(&queued_audio_mutex);
48+
49+
for (size_t i = 0; i < len; ) {
50+
size_t phys = queued_audio_end % sizeof(queued_audio);
51+
size_t max_write = MIN(sizeof(queued_audio) - phys, len - i);
52+
memcpy(queued_audio + phys, ((const unsigned char *) data) + i, max_write);
53+
i += max_write;
54+
queued_audio_end += max_write;
55+
}
56+
57+
pthread_mutex_unlock(&queued_audio_mutex);
58+
59+
return VANILLA_SUCCESS;
60+
}
61+
62+
static void handle_queued_audio(gamepad_context_t *ctx, int seq_id)
63+
{
64+
AudioPacket ap;
65+
66+
// Initialize payload size to zero
67+
const size_t MIC_PAYLOAD_SIZE = 512;
68+
ap.payload_size = 0;
69+
70+
// Lock mutex
71+
pthread_mutex_lock(&queued_audio_mutex);
72+
73+
// If available, copy over to our packet
74+
if (queued_audio_end >= (queued_audio_start + MIC_PAYLOAD_SIZE)) {
75+
for (size_t i = 0; i < MIC_PAYLOAD_SIZE; ) {
76+
size_t phys = queued_audio_start % sizeof(queued_audio);
77+
size_t max_write = MIN(sizeof(queued_audio) - phys, MIC_PAYLOAD_SIZE - i);
78+
memcpy(ap.payload + i, queued_audio + queued_audio_start, max_write);
79+
i += max_write;
80+
queued_audio_start += max_write;
81+
}
82+
ap.payload_size = MIC_PAYLOAD_SIZE;
83+
}
84+
85+
// Unlock
86+
pthread_mutex_unlock(&queued_audio_mutex);
87+
88+
// If we didn't get audio, return here
89+
if (!ap.payload_size) {
90+
return;
91+
}
92+
93+
// struct timeval tv;
94+
// gettimeofday(&tv, 0);
95+
// if ((tv.tv_sec % 2) == 1) {
96+
// for (int i = 0; i < ap.payload_size; i++) {
97+
// ap.payload[i] = (rand() % 0xFF);
98+
// }
99+
// vanilla_log("NOISE!");
100+
// } else {
101+
// memset(ap.payload, 0x00, ap.payload_size);
102+
// vanilla_log("...quiet...");
103+
// }
104+
105+
// Set up remaining default parameters
106+
ap.format = 6;
107+
ap.mono = 1;
108+
ap.vibrate = 0;
109+
ap.type = TYPE_AUDIO; // Audio data
110+
ap.seq_id = seq_id + 1;
111+
ap.timestamp = 0;
112+
113+
// Reverse bits on params
114+
ap.format = reverse_bits(ap.format, 3);
115+
ap.seq_id = reverse_bits(ap.seq_id, 10);
116+
ap.payload_size = reverse_bits(ap.payload_size, 16);//ntohs(ap.payload_size);
117+
// ap.timestamp = reverse_bits(ap.timestamp, 32); // Not necessary because timestamp is 0
118+
119+
// Further reverse bits
120+
unsigned char *bytes = (unsigned char *) &ap;
121+
const size_t header_sz = sizeof(AudioPacket) - sizeof(ap.payload);
122+
for (int i = 0; i < header_sz; i++) {
123+
bytes[i] = (unsigned char) reverse_bits(bytes[i], 8);
124+
}
125+
126+
// Send packet to console
127+
send_to_console(ctx->socket_aud, &ap, header_sz + ap.payload_size, PORT_AUD);
128+
}
129+
38130
void handle_audio_packet(gamepad_context_t *ctx, unsigned char *data, size_t len)
39131
{
40132
//
@@ -43,16 +135,16 @@ void handle_audio_packet(gamepad_context_t *ctx, unsigned char *data, size_t len
43135
// This for loop skips ap->format, ap->seq_id, and ap->timestamp to save processing.
44136
// If you want those, you'll have to adjust this loop.
45137
//
46-
for (int byte = 0; byte < 2; byte++) {
138+
for (int byte = 0; byte < 8; byte++) {
47139
data[byte] = (unsigned char) reverse_bits(data[byte], 8);
48140
}
49141

50142
AudioPacket *ap = (AudioPacket *) data;
51143

52-
// ap->format = reverse_bits(ap->format, 3);
53-
// ap->seq_id = reverse_bits(ap->seq_id, 10);
54-
ap->payload_size = ntohs(ap->payload_size);
55-
// ap->timestamp = reverse_bits(ap->timestamp, 32);
144+
ap->format = reverse_bits(ap->format, 3);
145+
ap->seq_id = reverse_bits(ap->seq_id, 10);
146+
ap->payload_size = reverse_bits(ap->payload_size, 16);//ntohs(ap->payload_size);
147+
ap->timestamp = reverse_bits(ap->timestamp, 32);
56148

57149
if (ap->type == TYPE_VIDEO) {
58150
AudioPacketVideoFormat *avp = (AudioPacketVideoFormat *) ap->payload;
@@ -69,19 +161,29 @@ void handle_audio_packet(gamepad_context_t *ctx, unsigned char *data, size_t len
69161

70162
uint8_t vibrate_val = ap->vibrate;
71163
push_event(ctx->event_loop, VANILLA_EVENT_VIBRATE, &vibrate_val, sizeof(vibrate_val));
164+
165+
handle_queued_audio(ctx, ap->seq_id);
72166
}
73167

74168
void *listen_audio(void *x)
75169
{
76170
gamepad_context_t *info = (gamepad_context_t *) x;
77171
unsigned char data[2048];
78172
ssize_t size;
173+
174+
queued_audio_start = 0;
175+
queued_audio_end = 0;
176+
177+
pthread_mutex_init(&queued_audio_mutex, 0);
178+
79179
do {
80180
size = recv(info->socket_aud, data, sizeof(data), 0);
81181
if (size > 0) {
82182
handle_audio_packet(info, data, size);
83183
}
84184
} while (!is_interrupted());
185+
186+
pthread_mutex_destroy(&queued_audio_mutex);
85187

86188
pthread_exit(NULL);
87189

lib/gamepad/audio.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#ifndef GAMEPAD_AUDIO_H
22
#define GAMEPAD_AUDIO_H
33

4+
#include <stddef.h>
5+
46
void *listen_audio(void *x);
7+
int send_audio_packet(const void *data, size_t len);
58

69
#endif // GAMEPAD_AUDIO_H

lib/gamepad/command.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,14 @@ void handle_generic_packet(gamepad_context_t *info, int skt, GenericPacket *requ
325325
}
326326
break;
327327
}
328+
case 10:
329+
response.generic_cmd_header.error_code = 0;
330+
break;
331+
case 11:
332+
response.payload[0] = 0;
333+
response.generic_cmd_header.payload_size = htons(1);
334+
response.generic_cmd_header.error_code = 0;
335+
break;
328336
}
329337
break;
330338
case SERVICE_ID_PERIPHERAL:

lib/util.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,12 @@ uint32_t reverse_bits(uint32_t b, int bit_count)
9797

9898
b >>= 32 - bit_count;
9999
return b;
100-
}
100+
}
101+
102+
void print_hex(const void *data, size_t len)
103+
{
104+
const unsigned char *c = (const unsigned char *) data;
105+
for (size_t i = 0; i < len; i++) {
106+
printf("%02X", c[i]);
107+
}
108+
}

lib/util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ unsigned int reverse_bits(unsigned int b, int bit_count);
2424

2525
uint16_t crc16(const void* data, size_t len);
2626

27+
void print_hex(const void *data, size_t len);
28+
2729
#endif // VANILLA_UTIL_H

lib/vanilla.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <string.h>
1313
#include <unistd.h>
1414

15+
#include "gamepad/audio.h"
1516
#include "gamepad/command.h"
1617
#include "gamepad/gamepad.h"
1718
#include "gamepad/input.h"
@@ -231,4 +232,9 @@ size_t vanilla_generate_pps_params(void *data, size_t data_size)
231232
size_t vanilla_generate_h264_header(void *data, size_t size)
232233
{
233234
return generate_h264_header(data, size);
235+
}
236+
237+
void vanilla_send_audio(const void *data, size_t size)
238+
{
239+
send_audio_packet(data, size);
234240
}

lib/vanilla.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ void vanilla_set_battery_status(int battery_status);
193193
*/
194194
size_t vanilla_generate_h264_header(void *data, size_t size);
195195

196+
/**
197+
* Send microphone audio
198+
*/
199+
void vanilla_send_audio(const void *data, size_t size);
200+
196201
#if defined(__cplusplus)
197202
}
198203
#endif

0 commit comments

Comments
 (0)