Skip to content

Commit 234e7fe

Browse files
committed
Fixup a/v/batt syncing
1 parent 57b7ff4 commit 234e7fe

4 files changed

Lines changed: 38 additions & 12 deletions

File tree

src/av.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "decoder.h"
1313
#include <stdint.h>
1414

15+
extern int a_active;
16+
extern int v_active;
1517
extern int a_running;
1618
extern int v_running;
1719
extern int thread_cmd;
@@ -36,15 +38,18 @@ SOCKET GetConnection(void) {
3638
// Battry Check thread
3739
void *BatteryThreadProc(__attribute__((__unused__)) void *args) {
3840
SOCKET socket = INVALID_SOCKET;
39-
char buf[128];
40-
char battery_value[32];
41+
char buf[128] = {0};
42+
char battery_value[32] = {0};
4143
int i, j;
4244

43-
memset(battery_value, 0, sizeof(battery_value));
4445
dbgprint("Battery Thread Start\n");
4546

46-
usleep(500000);
4747
while (v_running || a_running) {
48+
if (v_active == 0 && a_active == 0) {
49+
usleep(50000);
50+
continue;
51+
}
52+
4853
socket = GetConnection();
4954
if (socket == INVALID_SOCKET) {
5055
goto LOOP;
@@ -126,6 +131,7 @@ void *VideoThreadProc(void *args) {
126131

127132
memset(buf, 0, sizeof(buf));
128133
if (RecvAll(buf, 9, videoSocket) <= 0) {
134+
errprint("recv error (%d) '%s'\n", errno, strerror(errno));
129135
MSG_ERROR("Connection reset!\nIs the app running?");
130136
goto early_out;
131137
}
@@ -134,6 +140,7 @@ void *VideoThreadProc(void *args) {
134140
goto early_out;
135141
}
136142

143+
v_active = 1;
137144
while (v_running != 0){
138145
if (thread_cmd != 0) {
139146
len = 0;
@@ -161,6 +168,7 @@ void *VideoThreadProc(void *args) {
161168
}
162169

163170
early_out:
171+
v_active = 0;
164172
dbgprint("disconnect\n");
165173
disconnect(videoSocket);
166174
decoder_cleanup();
@@ -195,6 +203,13 @@ void *AudioThreadProc(void *arg) {
195203
return 0;
196204
}
197205

206+
// wait for video
207+
while (v_running) {
208+
usleep(200000);
209+
if (v_active) break;
210+
if (!a_running) return 0;
211+
}
212+
198213
if (g_settings.connection == CB_RADIO_IOS)
199214
goto TCP_ONLY;
200215
if (strncmp(g_settings.ip, ADB_LOCALHOST_IP, CSTR_LEN(ADB_LOCALHOST_IP)) == 0)
@@ -257,6 +272,7 @@ void *AudioThreadProc(void *arg) {
257272
bytes_per_packet = CHUNKS_PER_PACKET * DROIDCAM_SPX_CHUNK_BYTES_2;
258273

259274
STREAM:
275+
a_active = 1;
260276
while (a_running) {
261277
int len = (mode == UDP_STREAM)
262278
? RecvNonBlockUDP(stream_buf, STREAM_BUF_SIZE, socket)
@@ -316,6 +332,7 @@ void *AudioThreadProc(void *arg) {
316332
}
317333

318334
early_out:
335+
a_active = 0;
319336
if (mode == UDP_STREAM)
320337
SendUDPMessage(socket, STOP_REQ, CSTR_LEN(STOP_REQ), g_settings.ip, g_settings.port + 1);
321338

src/connection.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@ char* DROIDCAM_CONNECT_ERROR = \
3030
SOCKET Connect(const char* ip, int port, char **errormsg) {
3131
int flags;
3232
struct sockaddr_in sin;
33-
SOCKET sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
33+
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
3434

35-
printf("connecting to %s:%d\n", ip, port);
35+
dbgprint("connect to %s:%d\n", ip, port);
3636
if(sock == INVALID_SOCKET) {
3737
errprint("socket() error %d '%s'\n", errno, strerror(errno));
3838
*errormsg = strerror(errno);
3939
goto _error_out;
4040
}
41+
4142
sin.sin_family = AF_INET;
4243
sin.sin_addr.s_addr = inet_addr(ip);
4344
sin.sin_port = htons(port);

src/droidcam-cli.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ Thread athread = {0, -1}, vthread = {0, -1}, dthread = {0, -1};
2323

2424
char *v4l2_dev = 0;
2525
unsigned v4l2_width = 0, v4l2_height = 0;
26-
int v_running = 0;
27-
int a_running = 0;
28-
int thread_cmd = 0;
26+
volatile int a_active = 0;
27+
volatile int v_active = 0;
28+
volatile int v_running = 0;
29+
volatile int a_running = 0;
30+
volatile int thread_cmd = 0;
2931
int no_controls = 0;
3032
struct settings g_settings = {0};
3133

src/droidcam.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ GThread* hDecodeThread;
4040
GThread* hBatteryThread;
4141

4242
char *v4l2_dev = 0;
43-
int a_running = 0;
44-
int v_running = 0;
45-
int thread_cmd = 0;
43+
volatile int a_active = 0;
44+
volatile int v_active = 0;
45+
volatile int a_running = 0;
46+
volatile int v_running = 0;
47+
volatile int thread_cmd = 0;
4648
struct settings g_settings = {0};
4749

4850
extern const char *thread_cmd_val_str;
@@ -124,6 +126,8 @@ static void Stop(void) {
124126
hBatteryThread = NULL;
125127
}
126128

129+
a_active = 0;
130+
v_active = 0;
127131
gtk_widget_set_sensitive(GTK_WIDGET(elButton), FALSE);
128132
gtk_widget_set_sensitive(GTK_WIDGET(wbButton), FALSE);
129133
gtk_widget_set_sensitive(GTK_WIDGET(menuButton), FALSE);
@@ -194,6 +198,7 @@ static void Start(void) {
194198
}
195199

196200
if (g_settings.video) {
201+
v_active = 0;
197202
v_running = 1;
198203
hVideoThread = g_thread_new(NULL, VideoThreadProc, (void*) (SOCKET_PTR) s);
199204
hDecodeThread = g_thread_new(NULL, DecodeThreadProc, NULL);
@@ -202,6 +207,7 @@ static void Start(void) {
202207
}
203208

204209
if (g_settings.audio) {
210+
a_active = 0;
205211
a_running = 1;
206212
hAudioThread = g_thread_new(NULL, AudioThreadProc, NULL);
207213
}

0 commit comments

Comments
 (0)