/
spcaview.c
executable file
·2520 lines (2303 loc) · 66.7 KB
/
spcaview.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
# spcaview: Sdl video recorder and viewer with sound. #
#This package work with the spca5xx based webcam with the raw jpeg feature. #
#All the decoding is in user space with the help of libjpeg. #
#. #
# Copyright (C) 2003 2004 2005 Michel Xhaard #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
# #
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <string.h>
#include <pthread.h>
#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
#include <SDL/SDL_audio.h>
#include <SDL/SDL_timer.h>
#include <linux/videodev.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <sys/time.h>
#include <signal.h>
#include "SDL_audioin.h"
#include "jconfig.h"
#include "avilib.h"
#include "dpsh.h"
#include "utils.h"
#include "tcputils.h"
#include "spcaframe.h"
#include "version.h"
#define WAVE_AUDIO_PCM 1
#define CHANNELS 1
#define AUDIO_SAMPLERATE 22050 // 44100 // 11025 // 8000 // 22050
#define AUDIO_SAMPLES 512 // 1024
#define AUDIO_FORMAT AUDIO_S16
#define AUDIOSIZE 2
#define NUM_CHUNCK 64 // 256 a large ring buffer
#define MAXBUFFERSIZE (AUDIO_SAMPLES*AUDIOSIZE*NUM_CHUNCK)
#define HELPBUFFERSIZE (AUDIO_SAMPLES*AUDIOSIZE)
// INIT_BRIGHT is the initial brightness level
// BRIGHTMEAN is the goal mean brightness for the
// auto adjustment.
// SPRING_CONSTANT determines the restoring force
// for the auto adjust
// BRIGHT_WINDOW is the window around the goal mean
// in which no adjustment is to occur
#define V4L_BRIGHT_MIN 0
#define V4L_BRIGHT_MAX 50000 //65000
#define BRIGHTMEAN 128 //145
#define SPRING_CONSTANT 25
#define BRIGHTWINDOW 10
/* pictFlag should be set by the hardware or the timer */
/* call_back function need to reset the flag */
static int totmean;
int interval = 0;
volatile int pictFlag = 0;
static int Oneshoot = 1;
static int numshoot = 0;
static int modshoot = 0;
static videoOk = 0;
/* picture structure */
typedef struct pictstruct{
unsigned char *data;
int sizeIn;
int width;
int height;
int formatIn;
int mode;
avi_t *out_fd;
pthread_mutex_t mutex;
pthread_cond_t cond;
} pictstruct;
void *waitandshoot(void* ptr);
/* sound buffer */
struct Rbuffer {
Uint8 buffer[MAXBUFFERSIZE];
Uint8 helpbuffer[HELPBUFFERSIZE];
int ptread ;
int ptwrite;
};
Uint32 GetVideoBpp (void);
static Uint32 callback_timer (Uint32 interval)
{
pictFlag = 1;
//printf("TIMER ON !!!!\n");
return interval;
}
void callback_record(void *userdata, Uint8 *stream, int len)
{
int part1 =0;
int part2 =0;
struct Rbuffer *mybuf=(struct Rbuffer*) userdata;
if ((mybuf->ptwrite + len) > MAXBUFFERSIZE){
part1=MAXBUFFERSIZE-mybuf->ptwrite;
part2 = len-part1;
memcpy (mybuf->buffer+mybuf->ptwrite,stream,part1);
memcpy (mybuf->buffer,stream+part1,part2);
} else {
memcpy(mybuf->buffer+mybuf->ptwrite,stream,len);
}
mybuf->ptwrite = (mybuf->ptwrite +len) % MAXBUFFERSIZE;
//fprintf(stderr,"callback_record(%p,%d,%d %d)\n",userdata,mybuf->ptwrite,part1,part2);
}
void callback_play(void *userdata, Uint8 *stream, int len)
{
struct Rbuffer *mybuf=(struct Rbuffer*) userdata;
int ptwrite=mybuf->ptwrite ;
int ptread =mybuf->ptread;
int part1 =0;
int part2 =0;
int delta =0;
delta = ptwrite-ptread;
if(delta < 0){
if (((MAXBUFFERSIZE + delta) >= len)){
part1=MAXBUFFERSIZE-ptread;
if (part1 >=len){
part1 = len;
part2 = 0 ;
} else {
part2 = len-part1;
}
//fprintf(stderr,"callback_playT part1 %d part2 %d \n",part1,part2);
memcpy (mybuf->helpbuffer,mybuf->buffer+ptread,part1);
if (part2)
memcpy (mybuf->helpbuffer+part1,mybuf->buffer,part2);
SDL_MixAudio(stream,mybuf->helpbuffer, len, SDL_MIX_MAXVOLUME);
mybuf->ptread = (mybuf->ptread +len) % MAXBUFFERSIZE;
//fprintf(stderr,"callback_playT ptread %d ptwrite %d \n",ptread,ptwrite);
} else {
//fprintf(stderr,"Waiting for dataT to Play Ptread %dPtwrite %d !! \n",ptread,ptwrite);
}
} else {
if (delta >= len){
memcpy (mybuf->helpbuffer,mybuf->buffer+ptread,len);
SDL_MixAudio(stream,mybuf->helpbuffer, len, SDL_MIX_MAXVOLUME);
mybuf->ptread = (mybuf->ptread +len) % MAXBUFFERSIZE;
//fprintf(stderr,"callback_playF ptread %d ptwrite %d \n",ptread,ptwrite);
} else {
//fprintf(stderr,"Waiting for dataF to Play Ptread %d Ptwrite %d !! \n",ptread,ptwrite);
}
}
}
static int clip_to(int x, int low, int high);
//void equalize (unsigned char *src, int width, int height, int format);
static int adjust_bright( struct video_picture *videopict, int fd);
static int get_pic_mean ( int width, int height, const unsigned char *buffer,
int is_rgb,int startx, int starty, int endx,
int endy );
static int setVideoPict (struct video_picture *videopict, int fd);
static int isSpcaChip (const char *BridgeName);
/* return Bridge otherwhise -1 */
static int getStreamId (const char * BridgeName);
/* return Stream_id otherwhise -1 */
static int probeSize (const char *BridgeName, int *width, int *height);
/* return 1->ok otherwhise -1 */
void
resize (unsigned char *dst,unsigned char *src, int Wd,int Hd,int Ws,int Hs) ;
static int
readFrame (avi_t *out_fd,long i,unsigned char **jpegData, int * jpegSize, struct Rbuffer *RingBuffer, int maxsound, int *audiolen,
int *soundbytes, int isaudio, int updown, int compress);
static void
refresh_screen (unsigned char *src, unsigned char *pict, int format, int width,
int height,int owidth ,int oheight, int size, int autobright);
int spcaClient (char *Ip, short port,int owidth, int oheight ,int statOn);
int spcaPlay (char* inputfile, int width, int height);
int spcaGrab (char *outputfile,char fourcc[4] , const char *videodevice, int image_width,int image_height, int format, int owidth, int oheight,
int grabMethod,int videoOn,int audioout,int videocomp,int autobright,int statOn,int decodeOn);
void *waitandshoot(void* ptr)
{ pictstruct *mypict = (pictstruct *) ptr;
int width = mypict->width;
int height = mypict->height;
int format = mypict->formatIn;
int size = mypict->sizeIn;
int mode = mypict->mode;
avi_t *fd = mypict->out_fd;
int status;
while(1){
status = pthread_mutex_lock (&mypict->mutex);
if (status != 0) {fprintf(stderr,"Lock error!\n"); exit(-1);}
while (pictFlag == 0) {
/* Set predicate */
status = pthread_cond_wait(&mypict->cond, &mypict->mutex);
if (status != 0) {fprintf(stderr,"Wait error!\n"); exit(-1);}
}
pictFlag = 0;
getJpegPicture(mypict->data,width,height,format,size,mode,fd);
if (modshoot)
if (!(--numshoot)) Oneshoot = 0;
status = pthread_mutex_unlock (&mypict->mutex);
if (status != 0) {fprintf(stderr,"Unlock error!\n"); exit(-1);}
/******* EXIT the MONITOR ************/
//printf("Condition was accept, data.value=%d\n",pictFlag);
}
return NULL;
}
void init_callbackmessage(struct client_t* callback)
{ char key[4] ={'O','K','\0','\0'} ;
int x = 128;
int y = 128;
unsigned char sleepon=0;
unsigned char bright=0;
unsigned char contrast =0;
unsigned char exposure = 0;
unsigned char colors = 0;
unsigned char size = 0;
unsigned char fps = 0;
memcpy(callback->message,key,4);
callback->x = x;
callback->y = y;
callback->updobright=bright;
callback->updocontrast=contrast;
callback->updoexposure = exposure;
callback->updocolors = colors;
callback->sleepon=sleepon;
callback->updosize = size;
callback->fps = fps;
}
/* callback spec
updobright = 1 increase bright
updobright = 0 nothing todo
updobright = 2 decrease bright
same for all updocontrast updosize
sleepon =1 ask the server for no frame
on wakeup sleepon is cleared server will send frame
and client should display
sleepon =2 ask the server to send a 100ms pulse on pin 14
pin 14 is inverted always 1 pulse 0
*/
void reset_callbackmessage(struct client_t* callback)
{
callback->updobright= 0;
callback->updocontrast= 0;
callback->updoexposure = 0;
callback->updocolors = 0;
callback->sleepon= 0;
callback->updosize = 0;
callback->fps = 0;
}
int
main (int argc, char *argv[])
{
/* Timing value used for statistic */
const char *videodevice = NULL;
/* default mmap */
int grabMethod = 1;
int format = VIDEO_PALETTE_YUV420P;
/******** output screen pointer ***/
int image_width = IMAGE_WIDTH;
int image_height = IMAGE_HEIGHT;
int owidth = 0;
int oheight = 0;
/**********************************/
/* avi parametres */
char *inputfile = NULL;
char *outputfile = NULL;
char fourcc[4] = "MJPG";
char *sizestring = NULL;
int use_libjpeg = 1;
char *separateur;
char *mode = NULL;
/*********************************/
/* Starting Flags */
int i;
int videoOn = 1;
int decodeOn =1 ;
int statOn = 0;
int audioout = 0;
int videocomp = 0;
int channel = 0;
int norme = 0;
int autobright = 0;
/*********************************/
char *AdIpPort;
char AdIp[]= "000.000.000.000";
unsigned short ports = 0;
/*********************************/
SPCASTATE funct;
/* init default bytes per pixels for VIDEO_PALETTE_RAW_JPEG */
/* FIXME bpp is used as byte per pixel for the ouput screen */
/* we need also a bpp_in for the input stream as spcaview */
/* can convert stream That will be a good idea to have 2 struct */
/* with all global data one for input the other for output */
bpp = 3;
funct = GRABBER;
printf(" %s \n",version);
/* check arguments */
for (i = 1; i < argc; i++) {
/* skip bad arguments */
if (argv[i] == NULL || *argv[i] == 0 || *argv[i] != '-') {
continue;
}
if (strcmp (argv[i], "-d") == 0) {
if (i + 1 >= argc) {
printf ("No parameter specified with -d, aborting.\n");
exit (1);
}
videodevice = strdup (argv[i + 1]);
}
if (strcmp (argv[i], "-n") == 0) {
if (i + 1 >= argc) {
printf ("No parameter specified with -n, aborting.\n");
exit (1);
}
norme = atoi (argv[i + 1]);
if (norme < 0 || norme > 4)
printf ("Norme should be between 0..4 Read the readme !.\n");
}
if (strcmp (argv[i], "-c") == 0) {
if (i + 1 >= argc) {
printf ("No parameter specified with -c, aborting.\n");
exit (1);
}
channel = atoi (argv[i + 1]);
if (channel < 0 || channel > 9)
printf ("Channel should be between [0..3] || [6..9] Read the readme !.\n");
}
if (strcmp (argv[i], "-v") == 0) {
videoOn = 0;
}
if (strcmp (argv[i], "-j") == 0) {
decodeOn = 0;
}
if (strcmp (argv[i], "-t") == 0) {
statOn = 1;
}
if (strcmp (argv[i], "-z") == 0) {
videocomp = 1;
}
if (strcmp (argv[i], "-b") == 0) {
autobright = 1;
}
if (strcmp (argv[i], "-f") == 0) {
if (i + 1 >= argc) {
printf ("No parameter specified with -f, aborting.\n");
exit (1);
}
mode = strdup (argv[i + 1]);
if (strncmp (mode, "r32", 3) == 0) {
format = VIDEO_PALETTE_RGB32;
bpp = 4;
snprintf (fourcc, 5, "RGB4");
} else if (strncmp (mode, "r24", 3) == 0) {
format = VIDEO_PALETTE_RGB24;
bpp = 3;
snprintf (fourcc, 5, "RGB3");
} else if (strncmp (mode, "r16", 3) == 0) {
format = VIDEO_PALETTE_RGB565;
bpp = 2;
snprintf (fourcc, 5, "RGB2");
} else if (strncmp (mode, "yuv", 3) == 0) {
format = VIDEO_PALETTE_YUV420P;
bpp = 3;
snprintf (fourcc, 5, "I420");
} else if (strncmp (mode, "jpg", 3) == 0) {
format = VIDEO_PALETTE_JPEG;
bpp = 3;
snprintf (fourcc, 5, "MJPG");
}else {
format = VIDEO_PALETTE_YUV420P;
bpp = 3;
snprintf (fourcc, 5, "I420");
}
}
if (strcmp (argv[i], "-i") == 0) {
if (i + 1 >= argc) {
printf ("No parameter specified with -i, aborting.\n");
exit (1);
}
inputfile = strdup (argv[i + 1]);
funct = PLAYER ;
}
if (strcmp (argv[i], "-g") == 0) {
/* Ask for read instead default mmap */
grabMethod = 0;
}
if (strcmp (argv[i], "-a") == 0) {
if (i + 1 >= argc) {
printf ("No parameter specified with -a, aborting.\n");
exit (1);
}
audioout = atoi (argv[i + 1]);
if ((audioout < 0) || (audioout > 2)) {
audioout = 0;
}
printf ("audio channel %d\n", audioout);
}
if (strcmp (argv[i], "-o") == 0) {
if (i + 1 >= argc) {
printf ("No parameter specified with -o, aborting.\n");
exit (1);
}
outputfile = strdup (argv[i + 1]);
}
/* custom ? */
if (strcmp (argv[i], "-s") == 0) {
if (i + 1 >= argc) {
printf ("No parameter specified with -s, aborting.\n");
exit (1);
}
sizestring = strdup (argv[i + 1]);
image_width = strtoul (sizestring, &separateur, 10);
if (*separateur != 'x') {
printf ("Error in size use -s widthxheight \n");
exit (1);
} else {
++separateur;
image_height =
strtoul (separateur, &separateur, 10);
if (*separateur != 0)
printf ("hmm.. dont like that!! trying this height \n");
printf (" size width: %d height: %d \n",
image_width, image_height);
}
}
if (strcmp (argv[i], "-m") == 0) {
if (i + 1 >= argc) {
printf ("No parameter specified with -m, aborting.\n");
exit (1);
}
sizestring = strdup (argv[i + 1]);
owidth = strtoul (sizestring, &separateur, 10);
if (*separateur != 'x') {
printf ("Error in size use -m widthxheight \n");
exit (1);
} else {
++separateur;
oheight =
strtoul (separateur, &separateur, 10);
if (*separateur != 0)
printf ("hmm.. dont like that!! trying this height \n");
printf (" output size width: %d height: %d \n",
owidth, oheight);
}
}
if (strcmp (argv[i], "-p") == 0) {
if (argv[i + 1]) interval = atoi(argv[i + 1]);
else {
printf ("No parameter specified with -p, aborting.\n");
exit (1);
}
}
if (strcmp (argv[i], "-N") == 0) {
if (argv[i + 1]){
numshoot = (atoi(argv[i + 1])); // timer works on ms
if (numshoot <= 0) numshoot = 1; // in case :)
if(!interval) interval = 1000;
modshoot = 1;
}
else {
printf ("No parameter specified with -p, aborting.\n");
exit (1);
}
}
if (strcmp (argv[i], "-w") == 0) {
if (i + 1 >= argc) {
printf ("No parameter specified with -w, aborting.\n");
exit (1);
}
AdIpPort = strdup (argv[i + 1]);
if(reportip(AdIpPort,AdIp,&ports) < 0)
printf("error in port convertion \n");
printf ("using Server %s Port %d \n",AdIp,ports);
funct = CLIENT ;
}
/* large ? */
if (strcmp (argv[i], "-l") == 0) {
image_width = 640;
image_height = 480;
}
if (strcmp (argv[i], "-h") == 0) {
printf ("usage: spcaview [-h -d -n -c -v -j -z -g -l -s -m -a -f -b -t -p] [-i inputfile | -o outputfile]\n");
printf ("-h print this message \n");
printf ("-d /dev/videoX use videoX device\n");
printf ("-n norme 0->pal 2->secam 1->ntsc \n");
printf ("-c input channels 0..9 exclude 4 and 5 \n");
printf ("-v disable video output for raw recording \n");
printf ("-j disable video output and userspace decoding \n");
printf ("-z compress yuv420p video output with DPSH encoder\n");
printf ("-g use read method for grab instead mmap \n");
printf ("-l default 320x480 use input size 640x480 \n");
printf ("-s widthxheight use specified input size \n");
printf ("-m widthxheight use specified ouput size \n");
printf ("-a default 0 -> nosound 1-> microphone 2-> mixer output\n");
printf ("-f video format default yuv others options are r16 r24 r32 yuv jpg \n");
printf ("-b enable automatic brightness adjustment \n");
printf ("-t print statistics \n");
printf ("-p x getPicture every x msec \n");
printf ("-p x && -o getPicture every x msec and record in outfile\n");
printf ("-w Address:Port read from Address xxx.xxx.xxx.xxx:Port\n");
printf ("-N x take a x pictures and exit if p is not set p = 1 second \n");
exit (0);
}
}
switch (funct) {
case PLAYER:
{
/* that is spcaview player */
spcaPlay (inputfile, owidth, oheight);
}
break;
case GRABBER:
{
/* spcaview grabber */
spcaGrab (outputfile,fourcc , videodevice, image_width,image_height, format, owidth, oheight,
grabMethod, videoOn, audioout, videocomp, autobright, statOn, decodeOn);
}
break;
case CLIENT:
{
spcaClient(AdIp,ports,owidth,oheight, statOn);
}
}
exit (0);
}
int readjpeg(int sock, unsigned char **buf,struct frame_t *headerframe,struct client_t *message,int statOn)
{
int byteread,bytewrite;
bytewrite = write_sock(sock,(unsigned char*)message,sizeof(struct client_t));
// is sleeping ?
if ((byteread= read_sock(sock,(unsigned char*)headerframe,sizeof(struct frame_t))) < 0){
printf("Seem server is gone !! try later \n");
goto error;
}
if(statOn)
printf (" key %s nb %d width %d height %d times %dms size %d \n",headerframe->header,
headerframe->nbframe,headerframe->w,headerframe->h,headerframe->deltatimes,headerframe->size);
if(headerframe->size && !headerframe->wakeup){
//if(headerframe->size){
*buf=(unsigned char*) realloc(*buf,headerframe->size);
if((byteread = read_sock(sock,*buf,headerframe->size)) < 0){
printf("Seem server is gone !! try later \n");
goto error;}
}
//printf("buf read %d \n",byteread);
if(headerframe->acknowledge)
reset_callbackmessage(message);
usleep(5000);
return ((headerframe->wakeup)?0:(headerframe->size));
//return (headerframe->size);
error:
return -1;
}
void init_sdlall(void)
{ /* Initialize defaults, Video and Audio */
if ((SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_EVENTTHREAD) == -1)) {
printf ("Could not initialize SDL: %s.\n", SDL_GetError ());
exit (-1);
}
videoOk =1;
atexit (SDL_Quit);
}
void init_sdlvideo(void)
{ /* Initialize defaults, Video and Audio */
if ((SDL_InitSubSystem (SDL_INIT_VIDEO ) == -1)) {
printf ("Could not initialize SDL: %s.\n", SDL_GetError ());
exit (-1);
}
videoOk = 1;
}
void close_sdlvideo(void)
{ SDL_QuitSubSystem (SDL_INIT_VIDEO ) ;
videoOk = 0;
}
int spcaClient (char *Ip, short port,int owidth, int oheight, int statOn)
{
struct frame_t *headerframe;
struct client_t *messcallback;
unsigned char *buf = NULL;
int width,height;
int jpegsize;
int sock_client;
int run = 1;
int quit =1;
int keypressed =0;
int bpp = 3;
SDL_Surface *pscreen;
SDL_Event sdlevent;
unsigned char* p= NULL;
unsigned char *picture = NULL;
struct tm *tdate;
time_t curdate;
char titre[21];
init_sdlall();
sock_client = open_clientsock(Ip,port);
headerframe=(struct frame_t*)malloc(sizeof(struct frame_t));
messcallback=(struct client_t*)malloc(sizeof(struct client_t));
init_callbackmessage(messcallback);
if ((jpegsize = readjpeg(sock_client,&buf,headerframe,messcallback,statOn)) < 0){
printf("got size = 0 \n");
goto error;
}
width = headerframe->w;
height = headerframe->h;
if(!owidth || !oheight){
owidth = width;
oheight = height;
}
if(videoOk) {
pscreen = SDL_SetVideoMode (owidth, oheight, bpp * 8,
SDL_DOUBLEBUF | SDL_SWSURFACE);
p=pscreen->pixels;
}
do{
if((jpegsize = readjpeg(sock_client,&buf,headerframe,messcallback,statOn)) < 0){
// printf(" No size !!! exit fatal \n");
goto error;
}
/* mode sleep off */
if(!jpegsize && videoOk)
close_sdlvideo();
if( !videoOk && jpegsize){
init_sdlvideo();
pscreen = SDL_SetVideoMode (owidth, oheight, bpp * 8,
SDL_DOUBLEBUF | SDL_SWSURFACE);
p=pscreen->pixels;
}
curdate = (time_t) (headerframe->seqtimes / 1000);
tdate = localtime(&curdate);
snprintf (titre,21,"%02d/%02d/%04d-%02d:%02d:%02d\0",
tdate->tm_mday, tdate->tm_mon + 1, tdate->tm_year + 1900,
tdate->tm_hour, tdate->tm_min, tdate->tm_sec);
if(jpegsize && videoOk){
jpeg_decode(&picture,buf,&width,&height);
resize (p,picture,owidth,oheight,width,height) ;
SDL_WM_SetCaption (titre, NULL);
SDL_Flip (pscreen);
}
printf("bright %05d contrast %05d \r",headerframe->bright,headerframe->contrast);
fflush (stdout);
switch (run){
case 1:
if (SDL_PollEvent (&sdlevent) == 1) {
switch (sdlevent.type) {
case SDL_KEYDOWN:
switch (sdlevent.key.keysym.sym) {
case SDLK_s:
//getPicture(buf,jpegsize);
getJpegPicture(buf,width,height,
VIDEO_PALETTE_JPEG,jpegsize,PICTURE,NULL);
break;
case SDLK_w:
messcallback->updocontrast =2;
break;
case SDLK_x:
messcallback->updocontrast =1;
break;
case SDLK_b:
messcallback->updobright=1;
break;
case SDLK_n:
messcallback->updobright=2;
break;
case SDLK_l:
messcallback->sleepon=1;
break;
case SDLK_c:
messcallback->sleepon=2;
break;
case SDLK_j:
messcallback->updoexposure=1;
break;
case SDLK_d:
messcallback->updosize=1; //workaround change quality index
break;
case SDLK_f:
messcallback->updosize=2; //workaround change quality index
break;
case SDLK_g:
messcallback->fps=1; // decrease time_interval
break;
case SDLK_h:
messcallback->fps=2; // increase time interval
break;
case SDLK_UP:
if(messcallback->y -1 > 0)
messcallback->y--;
keypressed = SDLK_UP ; run = 2;
break;
case SDLK_DOWN:
if(messcallback->y +1 < 256)
messcallback->y++;
keypressed = SDLK_DOWN; run = 2;
break;
case SDLK_RIGHT:
if(messcallback->x -1 > 0)
messcallback->x--;
keypressed = SDLK_RIGHT; run = 2;
break;
case SDLK_LEFT:
if(messcallback->x +1 < 256)
messcallback->x++;
keypressed = SDLK_LEFT; run = 2;
break;
case SDLK_SPACE:
run = 0;
break;
case SDLK_q:
quit =0;
break;
}
break;
case SDL_QUIT:
quit = 0;
break;
}
} //end Poll event
break;
case 0:
if (SDL_WaitEvent (&sdlevent) == 1) {
switch (sdlevent.type) {
case SDL_KEYDOWN:
switch (sdlevent.key.keysym.sym) {
case SDLK_s:
//getPicture(buf,jpegsize);
break;
case SDLK_SPACE:
run = 1;
break;
case SDLK_q:
quit =0;
break;
}
break;
case SDL_QUIT:
quit = 0;
break;
}
} //end wait event
break;
case 2:{
if (SDL_PollEvent (&sdlevent) == 1) {
switch (sdlevent.type) {
case SDL_KEYDOWN:
keypressed = 0;
run = 1;
break;
} //end event type poll 2
}
switch (keypressed){
case SDLK_UP:
if(messcallback->y -1 > 0)
messcallback->y--;
break;
case SDLK_DOWN:
if(messcallback->y +1 < 256)
messcallback->y++;
break;
case SDLK_RIGHT:
if(messcallback->x -1 > 0)
messcallback->x--;
break;
case SDLK_LEFT:
if(messcallback->x +1 < 256)
messcallback->x++;
break;
default:
break;
}
} // end case 2
break;
}
}while(quit);
error:
if(picture){
free(picture);
picture = NULL;
}
close_sock(sock_client);
free(buf);
free(messcallback);
free(headerframe);
SDL_Quit ();
return 0;
}
int spcaPlay (char* inputfile, int width, int height){
/* Timing value used for statistic */
Uint32 synctime = 0;
Uint32 intime = 0;
Uint32 pictime = 0;
int format = VIDEO_PALETTE_YUV420P;
/******** output screen pointer ***/
SDL_Surface *pscreen;
SDL_Event sdlevent;
unsigned char *p = NULL;
unsigned char *pp = NULL;
int image_width = IMAGE_WIDTH;
int image_height = IMAGE_HEIGHT;
int owidth = width;
int oheight = height;
/**********************************/
/* avi parametres */
avi_t *out_fd;
char *compressor;
double framerate;
long audiorate;
int audiobits;
int audioformat;
unsigned char *tmp = NULL; // don't forget the Till lesson
unsigned char *dpshDest = NULL;
u_int8_t *jpegData = NULL; // Till lesson a good pointer is a pointer NULL
int jpegSize;
int framecount = 0;
/*********************************/
/* file input output */
char *outputfile = NULL;
static char Picture[80];
char fourcc[4] = "MJPG";
FILE *foutpict;
/*********************************/
/* Starting Flags */
int run = 1;
int quit = 1;
int initAudio = 0; //flag start audio
int videocomp = 0; //is compression dsph
int frame_size = 0;
int len = 0;
int fullsize = 0;
/*********************************/
/* data for SDL_audioin && SDL_audio */
SDL_AudioSpec spec, result;
SDL_AudioSpec expect;
struct Rbuffer RingBuffer;
int retry = 100;
int err = 0;
int bytes_per_read =0;
int audioout = 0;
int audiolength = 0;
int maxsound = 0;
/*********************************/
/* misc index */
int i,j,k;
int testbpp = 16;
RingBuffer.ptread =0;
RingBuffer.ptwrite =0;
/* that a is spcaview player */
printf ("Initializing SDL.\n");
/* Initialize defaults, Video and Audio */
if ((SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1)) {
printf ("Could not initialize SDL: %s.\n", SDL_GetError ());
exit (-1);
}
//bitperpix = GetVideoBpp();
/* Clean up on exit */
atexit (SDL_Quit);
printf ("SDL initialized.\n");
printf ("Using %s avifile \n", inputfile);
if ((out_fd = AVI_open_input_file (inputfile, 1)) == NULL) {
printf ("cannot open input file ? \n");
exit (1);
}
/* get the parameters from the file */
framecount = (int) AVI_video_frames (out_fd);
image_width = (int) AVI_video_width (out_fd);
image_height = (int) AVI_video_height (out_fd);
if(!owidth || !oheight){
owidth = image_width;
oheight = image_height;
}
printf ("frame: %d width: %d height: %d \n",
framecount, image_width, image_height);
framerate = AVI_frame_rate (out_fd);
printf (" framerate : %f \n", framerate);
audiobits = AVI_audio_bits (out_fd);
audiorate = AVI_audio_rate (out_fd);
audioformat = AVI_audio_format (out_fd);
printf ("audio rate: %d audio bits: %d audio format: %d\n",
audiorate, audiobits, audioformat);
compressor = AVI_video_compressor (out_fd);
printf (" codec: %s \n", compressor);
if (strncmp (compressor, "RGB4", 4) == 0) {
format = VIDEO_PALETTE_RGB32;
bpp = 4;
} else if (strncmp (compressor, "RGB3", 4) == 0) {
format = VIDEO_PALETTE_RGB24;
bpp = 3;
} else if (strncmp (compressor, "RGB2", 4) == 0) {
format = VIDEO_PALETTE_RGB565;
bpp = 2;
} else if (strncmp (compressor, "I420", 4) == 0) {
format = VIDEO_PALETTE_YUV420P;
bpp = 3;
} else if (strncmp (compressor, "MJPG", 4) == 0) {
format = VIDEO_PALETTE_JPEG;
bpp = 3;
} else if (strncmp (compressor, "DPSH", 4) == 0) {
format = VIDEO_PALETTE_YUV420P;
videocomp = 1;
frame_size = (image_width*image_height*3) >> 1;
bpp = 3;
} else {
printf ("Cannot play this file try Mplayer or Xine \n");
AVI_close (out_fd);
exit (1);
}
/* Init the player */
printf ("bpp %d format %d\n", bpp, format);
//if (format == VIDEO_PALETTE_RAW_JPEG)