8 #include <sys/socket.h>
10 #include <netinet/in.h>
13 #include <arpa/inet.h>
24 #include <sys/ioctl.h>
31 #define VIDEO_PORT 5555
32 #define RAW_CAPTURE_PORT 5557
34 #define H_ACQ_WIDTH VIDEO_WIDTH
35 #define H_ACQ_HEIGHT VIDEO_HEIGHT
36 #define VIDEO_BUFFER_SIZE (2*(VIDEO_WIDTH)*(VIDEO_HEIGHT))
38 #define DATA_RECEIVED_MAX_SIZE 8192
40 static video_controller_t controller;
41 static vp_api_picture_t picture;
42 static int pictureBpp;
44 // note: allocate twice the size of an image
45 uint16_t picture_buf[VIDEO_BUFFER_SIZE];
46 int num_picture_decoded = 0;
48 void stream_init(void)
52 memset(&controller, 0, sizeof(controller));
53 memset(&picture, 0, sizeof(picture));
54 memset(picture_buf, 0, sizeof(picture_buf));
57 /// Picture configuration
58 picture.format = PIX_FMT_RGB565;
59 picture.width = H_ACQ_WIDTH;
60 picture.height = H_ACQ_HEIGHT;
61 picture.framerate = 15;
62 picture.y_line_size = picture.width * pictureBpp;
63 picture.cb_line_size = 0;
64 picture.cr_line_size = 0;
65 picture.y_buf = (uint8_t *)picture_buf;
66 picture.cb_buf = NULL;
67 picture.cr_buf = NULL;
69 status = video_codec_open( &controller, UVLC_CODEC );
71 INFO("video_codec_open() failed\n");
73 video_controller_set_motion_estimation(&controller, FALSE);
74 video_controller_set_format( &controller, H_ACQ_WIDTH, H_ACQ_HEIGHT );
77 void stream_deinit(void)
79 video_codec_close(&controller);
82 static int video_udp_socket = -1;
84 void video_write (int8_t *buffer, int32_t len)
86 struct sockaddr_in to;
89 if( video_udp_socket < 0 )
91 struct sockaddr_in video_udp_addr;
93 memset( (char*)&video_udp_addr, 0, sizeof(video_udp_addr) );
95 video_udp_addr.sin_family = AF_INET;
96 video_udp_addr.sin_addr.s_addr = INADDR_ANY;
97 video_udp_addr.sin_port = htons( VIDEO_PORT + 100 );
99 video_udp_socket = socket( AF_INET, SOCK_DGRAM, 0 );
101 if( video_udp_socket > 0 )
103 flags = fcntl(video_udp_socket, F_GETFL, 0);
108 flags = fcntl(video_udp_socket, F_SETFL, flags );
112 INFO("Get Socket Options failed\n");
115 bind(video_udp_socket, (struct sockaddr*)&video_udp_addr, sizeof(struct sockaddr) );
119 if( video_udp_socket > 0 )
123 memset( (char*)&to, 0, sizeof(to) );
124 to.sin_family = AF_INET;
125 to.sin_addr.s_addr = inet_addr(WIFI_MYKONOS_IP); // BROADCAST address for subnet 192.168.1.xxx
126 to.sin_port = htons (VIDEO_PORT);
128 res = sendto( video_udp_socket, (char*)buffer, len, 0, (struct sockaddr*)&to, sizeof(to) );
132 DEFINE_THREAD_ROUTINE( stream_loop, data )
135 int sockfd, addr_in_size;
136 struct sockaddr_in *my_addr, *from;
138 INFO("VIDEO stream thread starting (thread=%d)...\n", (int)pthread_self());
142 addr_in_size = sizeof(struct sockaddr_in);
144 from = (struct sockaddr_in *)malloc(addr_in_size);
145 my_addr = (struct sockaddr_in *)malloc(addr_in_size);
149 memset((char *)my_addr,(char)0,addr_in_size);
150 my_addr->sin_family = AF_INET;
151 my_addr->sin_addr.s_addr = htonl(INADDR_ANY);
152 my_addr->sin_port = htons( VIDEO_PORT );
154 if((sockfd = socket (AF_INET, SOCK_DGRAM, 0)) < 0){
155 INFO ("socket: %s\n", strerror(errno));
159 if (bind(sockfd, (struct sockaddr *)my_addr, addr_in_size) < 0){
160 INFO ("bind: %s\n", strerror(errno));
169 setsockopt( sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
172 INFO("Ready to receive video data...\n");
175 video_write ((int8_t*)&one, sizeof( one ));
181 size = recvfrom (sockfd,
182 video_controller_get_stream_ptr(&controller),
183 DATA_RECEIVED_MAX_SIZE, 0, (struct sockaddr *)from,
184 (socklen_t*)&addr_in_size);
188 INFO ("Lost connection \n");
190 video_write ((int8_t*)&one, sizeof(one));
193 int decodeOK = FALSE;
195 controller.in_stream.used = size;
196 controller.in_stream.size = size;
197 controller.in_stream.index = 0;
198 controller.in_stream.length = 32;
199 controller.in_stream.code = 0;
201 //INFO("decode_blockline(size=%d)\n", size);
202 status = video_decode_blockline(&controller, &picture, &decodeOK);
204 INFO("video_decode_blockline() failed\n");
207 // this is ugly: update counter and notify video renderer
208 num_picture_decoded = controller.num_frames;
209 //INFO("num_picture_decoded = %d\n", num_picture_decoded);
221 INFO("VIDEO stream thread stopping\n");