svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r20849:20855
[blender.git] / source / blender / blenkernel / intern / writeframeserver.c
1 /*
2  * Frameserver
3  * Makes Blender accessible from TMPGenc directly using VFAPI (you can
4  * use firefox too ;-)
5  *
6  * Copyright (c) 2006 Peter Schlaile
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  */
19
20
21 #include <string.h>
22 #include <stdio.h>
23
24 #if defined(_WIN32)
25 #include <winsock2.h>
26 #include <windows.h>
27 #include <winbase.h>
28 #include <direct.h>
29 #else
30 #include <sys/time.h>
31 #include <sys/socket.h>
32 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <net/if.h>
36 #include <netdb.h>
37 #include <sys/ioctl.h>
38 #include <errno.h>
39 #include <unistd.h>
40 #include <sys/un.h>
41 #include <fcntl.h>
42 #endif
43
44 #include <stdlib.h>
45
46 #include "MEM_guardedalloc.h"
47 #include "BLI_blenlib.h"
48 #include "DNA_userdef_types.h"
49
50 #include "BKE_global.h"
51
52 #include "IMB_imbuf_types.h"
53 #include "IMB_imbuf.h"
54
55 #include "DNA_scene_types.h"
56
57 #ifdef HAVE_CONFIG_H
58 #include <config.h>
59 #endif
60
61 static int sock;
62 static int connsock;
63 static int write_ppm;
64 static int render_width;
65 static int render_height;
66
67
68 #if defined(_WIN32)
69 static int startup_socket_system()
70 {
71         WSADATA wsa;
72         return (WSAStartup(MAKEWORD(2,0),&wsa) == 0);
73 }
74
75 static void shutdown_socket_system()
76 {
77         WSACleanup();
78 }
79 static int select_was_interrupted_by_signal()
80 {
81         return (WSAGetLastError() == WSAEINTR);
82 }
83 #else
84 static int startup_socket_system()
85 {
86         return 1;
87 }
88
89 static void shutdown_socket_system()
90 {
91 }
92
93 static int select_was_interrupted_by_signal()
94 {
95         return (errno == EINTR);
96 }
97
98 static int closesocket(int fd) 
99 {
100         return close(fd);
101 }
102 #endif
103
104 void start_frameserver(RenderData *rd, int rectx, int recty)
105 {
106         struct sockaddr_in      addr;
107         int arg = 1;
108
109         if (!startup_socket_system()) {
110                 G.afbreek = 1;
111                 //XXX error("Can't startup socket system");
112                 return;
113         }
114
115         if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
116                 shutdown_socket_system();
117                 G.afbreek = 1; /* Abort render */
118                 //XXX error("Can't open socket");
119                 return;
120         }
121
122         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
123                    (char*) &arg, sizeof(arg));
124
125         addr.sin_family = AF_INET;
126         addr.sin_port = htons(U.frameserverport);
127         addr.sin_addr.s_addr = INADDR_ANY;
128
129         if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
130                 shutdown_socket_system();
131                 G.afbreek = 1; /* Abort render */
132                 //XXX error("Can't bind to socket");
133                 return;
134         }
135
136         if (listen(sock, SOMAXCONN) < 0) {
137                 shutdown_socket_system();
138                 G.afbreek = 1; /* Abort render */
139                 //XXX error("Can't establish listen backlog");
140                 return;
141         }
142         connsock = -1;
143
144         render_width = rectx;
145         render_height = recty;
146 }
147
148 static char index_page[] 
149
150 "HTTP/1.1 200 OK\n"
151 "Content-Type: text/html\n\n"
152 "<html><head><title>Blender Frameserver</title></head>\n"
153 "<body><pre>\n"
154 "<H2>Blender Frameserver</H2>\n"
155 "<A HREF=info.txt>Render Info</A><br>\n"
156 "<A HREF=close.txt>Stop Rendering</A><br>\n"
157 "\n"
158 "Images can be found here\n"
159 "\n"
160 "images/ppm/%d.ppm\n"
161 "\n"
162 "</pre></body></html>\n";
163
164 static char good_bye[]
165 = "HTTP/1.1 200 OK\n"
166 "Content-Type: text/html\n\n"
167 "<html><head><title>Blender Frameserver</title></head>\n"
168 "<body><pre>\n"
169 "Render stopped. Goodbye</pre></body></html>";
170
171 static int safe_write(char * s, int tosend)
172 {
173         int total = tosend;
174         do {
175                 int got = send(connsock, s, tosend, 0);
176                 if (got < 0) {
177                         return got;
178                 }
179                 tosend -= got;
180                 s += got;
181         } while (tosend > 0);
182
183         return total;
184 }
185
186 static int safe_puts(char * s)
187 {
188         return safe_write(s, strlen(s));
189 }
190
191 static int handle_request(RenderData *rd, char * req)
192 {
193         char * p;
194         char * path;
195         int pathlen;
196
197         if (memcmp(req, "GET ", 4) != 0) {
198                 return -1;
199         }
200            
201         p = req + 4;
202         path = p;
203
204         while (*p != ' ' && *p) p++;
205
206         *p = 0;
207
208         if (strcmp(path, "/index.html") == 0
209             || strcmp(path, "/") == 0) {
210                 safe_puts(index_page);
211                 return -1;
212         }
213
214         write_ppm = 0;
215         pathlen = strlen(path);
216
217         if (pathlen > 12 && memcmp(path, "/images/ppm/", 12) == 0) {
218                 write_ppm = 1;
219                 return atoi(path + 12);
220         }
221         if (strcmp(path, "/info.txt") == 0) {
222                 char buf[4096];
223
224                 sprintf(buf, 
225                         "HTTP/1.1 200 OK\n"
226                         "Content-Type: text/html\n\n"
227                         "start %d\n"
228                         "end %d\n"
229                         "width %d\n"
230                         "height %d\n" 
231                         "rate %d\n"
232                         "ratescale %d\n",
233                         rd->sfra,
234                         rd->efra,
235                         render_width,
236                         render_height,
237                         rd->frs_sec,
238                         1
239                         );
240
241                 safe_puts(buf);
242                 return -1;
243         }
244         if (strcmp(path, "/close.txt") == 0) {
245                 safe_puts(good_bye);
246                 G.afbreek = 1; /* Abort render */
247                 return -1;
248         }
249         return -1;
250 }
251
252 int frameserver_loop(RenderData *rd)
253 {
254         fd_set readfds;
255         struct timeval tv;
256         struct sockaddr_in      addr;
257         int len, rval;
258         unsigned int socklen;
259         char buf[4096];
260
261         if (connsock != -1) {
262                 closesocket(connsock);
263                 connsock = -1;
264         }
265
266         tv.tv_sec = 1;
267         tv.tv_usec = 0;
268
269         FD_ZERO(&readfds);
270         FD_SET(sock, &readfds);
271
272         rval = select(sock + 1, &readfds, NULL, NULL, &tv);
273         if (rval < 0) {
274                 return -1;
275         }
276
277         if (rval == 0) { /* nothing to be done */
278                 return -1;
279         }
280
281         socklen = sizeof(addr);
282
283         if ((connsock = accept(sock, (struct sockaddr *)&addr, &socklen)) < 0) {
284                 return -1;
285         }
286
287         FD_ZERO(&readfds);
288         FD_SET(connsock, &readfds);
289
290         for (;;) {
291                 /* give 10 seconds for telnet testing... */
292                 tv.tv_sec = 10;
293                 tv.tv_usec = 0;
294
295                 rval = select(connsock + 1, &readfds, NULL, NULL, &tv);
296                 if (rval > 0) {
297                         break;
298                 } else if (rval == 0) {
299                         return -1;
300                 } else if (rval < 0) {
301                         if (!select_was_interrupted_by_signal()) {
302                                 return -1;
303                         }
304                 }
305         }
306
307         len = recv(connsock, buf, 4095, 0);
308
309         if (len < 0) {
310                 return -1;
311         }
312
313         buf[len] = 0;
314
315         return handle_request(rd, buf);
316 }
317
318 static void serve_ppm(int *pixels, int rectx, int recty)
319 {
320         unsigned char* rendered_frame;
321         unsigned char* row = (unsigned char*) malloc(render_width * 3);
322         int y;
323         char header[1024];
324
325         sprintf(header, 
326                 "HTTP/1.1 200 OK\n"
327                 "Content-Type: image/ppm\n"
328                 "Connection: close\n\n"
329                 "P6\n"
330                 "# Creator: blender frameserver v0.0.1\n"
331                 "%d %d\n"
332                 "255\n",
333                 rectx, recty);
334
335         safe_puts(header);
336
337         rendered_frame = (unsigned char *)pixels;
338
339         for (y = recty - 1; y >= 0; y--) {
340                 unsigned char* target = row;
341                 unsigned char* src = rendered_frame + rectx * 4 * y;
342                 unsigned char* end = src + rectx * 4;
343                 while (src != end) {
344                         target[2] = src[2];
345                         target[1] = src[1];
346                         target[0] = src[0];
347                         
348                         target += 3;
349                         src += 4;
350                 }
351                 safe_write((char*)row, 3 * rectx); 
352         }
353         free(row);
354         closesocket(connsock);
355         connsock = -1;
356 }
357
358 void append_frameserver(RenderData *rd, int frame, int *pixels, int rectx, int recty)
359 {
360         fprintf(stderr, "Serving frame: %d\n", frame);
361         if (write_ppm) {
362                 serve_ppm(pixels, rectx, recty);
363         }
364         if (connsock != -1) {
365                 closesocket(connsock);
366                 connsock = -1;
367         }
368 }
369
370 void end_frameserver()
371 {
372         if (connsock != -1) {
373                 closesocket(connsock);
374                 connsock = -1;
375         }
376         closesocket(sock);
377         shutdown_socket_system();
378 }
379