1212#include "decoder.h"
1313#include <stdint.h>
1414
15+ extern int a_active ;
16+ extern int v_active ;
1517extern int a_running ;
1618extern int v_running ;
1719extern int thread_cmd ;
@@ -36,15 +38,18 @@ SOCKET GetConnection(void) {
3638// Battry Check thread
3739void * 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
163170early_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
259274STREAM :
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
318334early_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
0 commit comments