Filling in branch from trunk
[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_bad_level_calls.h"
51 #include "BKE_global.h"
52
53 #include "IMB_imbuf_types.h"
54 #include "IMB_imbuf.h"
55
56 #include "DNA_scene_types.h"
57 #include "blendef.h"
58
59 #ifdef HAVE_CONFIG_H
60 #include <config.h>
61 #endif
62
63 static int sock;
64 static int connsock;
65 static int write_ppm;
66 static int render_width;
67 static int render_height;
68
69
70 #if defined(_WIN32)
71 static int startup_socket_system()
72 {
73         WSADATA wsa;
74         return (WSAStartup(MAKEWORD(2,0),&wsa) == 0);
75 }
76
77 static void shutdown_socket_system()
78 {
79         WSACleanup();
80 }
81 static int select_was_interrupted_by_signal()
82 {
83         return (WSAGetLastError() == WSAEINTR);
84 }
85 #else
86 static int startup_socket_system()
87 {
88         return 1;
89 }
90
91 static void shutdown_socket_system()
92 {
93 }
94
95 static int select_was_interrupted_by_signal()
96 {
97         return (errno == EINTR);
98 }
99
100 static int closesocket(int fd) 
101 {
102         return close(fd);
103 }
104 #endif
105
106 void start_frameserver(RenderData *rd, int rectx, int recty)
107 {
108         struct sockaddr_in      addr;
109         int arg = 1;
110
111         if (!startup_socket_system()) {
112                 G.afbreek = 1;
113                 error("Can't startup socket system");
114                 return;
115         }
116
117         if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
118                 shutdown_socket_system();
119                 G.afbreek = 1; /* Abort render */
120                 error("Can't open socket");
121                 return;
122         }
123
124         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
125                    (char*) &arg, sizeof(arg));
126
127         addr.sin_family = AF_INET;
128         addr.sin_port = htons(U.frameserverport);
129         addr.sin_addr.s_addr = INADDR_ANY;
130
131         if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
132                 shutdown_socket_system();
133                 G.afbreek = 1; /* Abort render */
134                 error("Can't bind to socket");
135                 return;
136         }
137
138         if (listen(sock, SOMAXCONN) < 0) {
139                 shutdown_socket_system();
140                 G.afbreek = 1; /* Abort render */
141                 error("Can't establish listen backlog");
142                 return;
143         }
144         connsock = -1;
145
146         render_width = rectx;
147         render_height = recty;
148 }
149
150 static char index_page[] 
151
152 "HTTP/1.1 200 OK\n"
153 "Content-Type: text/html\n\n"
154 "<html><head><title>Blender Frameserver</title></head>\n"
155 "<body><pre>\n"
156 "<H2>Blender Frameserver</H2>\n"
157 "<A HREF=info.txt>Render Info</A><br>\n"
158 "<A HREF=close.txt>Stop Rendering</A><br>\n"
159 "\n"
160 "Images can be found here\n"
161 "\n"
162 "images/ppm/%d.ppm\n"
163 "\n"
164 "</pre></body></html>\n";
165
166 static char good_bye[]
167 = "HTTP/1.1 200 OK\n"
168 "Content-Type: text/html\n\n"
169 "<html><head><title>Blender Frameserver</title></head>\n"
170 "<body><pre>\n"
171 "Render stopped. Goodbye</pre></body></html>";
172
173 static int safe_write(char * s, int tosend)
174 {
175         int total = tosend;
176         do {
177                 int got = send(connsock, s, tosend, 0);
178                 if (got < 0) {
179                         return got;
180                 }
181                 tosend -= got;
182                 s += got;
183         } while (tosend > 0);
184
185         return total;
186 }
187
188 static int safe_puts(char * s)
189 {
190         return safe_write(s, strlen(s));
191 }
192
193 static int handle_request(char * req)
194 {
195         char * p;
196         char * path;
197         int pathlen;
198
199         if (memcmp(req, "GET ", 4) != 0) {
200                 return -1;
201         }
202            
203         p = req + 4;
204         path = p;
205
206         while (*p != ' ' && *p) p++;
207
208         *p = 0;
209
210         if (strcmp(path, "/index.html") == 0
211             || strcmp(path, "/") == 0) {
212                 safe_puts(index_page);
213                 return -1;
214         }
215
216         write_ppm = 0;
217         pathlen = strlen(path);
218
219         if (pathlen > 12 && memcmp(path, "/images/ppm/", 12) == 0) {
220                 write_ppm = 1;
221                 return atoi(path + 12);
222         }
223         if (strcmp(path, "/info.txt") == 0) {
224                 char buf[4096];
225
226                 sprintf(buf, 
227                         "HTTP/1.1 200 OK\n"
228                         "Content-Type: text/html\n\n"
229                         "start %d\n"
230                         "end %d\n"
231                         "width %d\n"
232                         "height %d\n" 
233                         "rate %d\n"
234                         "ratescale %d\n",
235                         G.scene->r.sfra,
236                         G.scene->r.efra,
237                         render_width,
238                         render_height,
239                         G.scene->r.frs_sec,
240                         1
241                         );
242
243                 safe_puts(buf);
244                 return -1;
245         }
246         if (strcmp(path, "/close.txt") == 0) {
247                 safe_puts(good_bye);
248                 G.afbreek = 1; /* Abort render */
249                 return -1;
250         }
251         return -1;
252 }
253
254 int frameserver_loop()
255 {
256         fd_set readfds;
257         struct timeval tv;
258         struct sockaddr_in      addr;
259         int len, rval;
260         unsigned int socklen;
261         char buf[4096];
262
263         if (connsock != -1) {
264                 closesocket(connsock);
265                 connsock = -1;
266         }
267
268         tv.tv_sec = 1;
269         tv.tv_usec = 0;
270
271         FD_ZERO(&readfds);
272         FD_SET(sock, &readfds);
273
274         rval = select(sock + 1, &readfds, NULL, NULL, &tv);
275         if (rval < 0) {
276                 return -1;
277         }
278
279         if (rval == 0) { /* nothing to be done */
280                 return -1;
281         }
282
283         socklen = sizeof(addr);
284
285         if ((connsock = accept(sock, (struct sockaddr *)&addr, &socklen)) < 0) {
286                 return -1;
287         }
288
289         FD_ZERO(&readfds);
290         FD_SET(connsock, &readfds);
291
292         for (;;) {
293                 /* give 10 seconds for telnet testing... */
294                 tv.tv_sec = 10;
295                 tv.tv_usec = 0;
296
297                 rval = select(connsock + 1, &readfds, NULL, NULL, &tv);
298                 if (rval > 0) {
299                         break;
300                 } else if (rval == 0) {
301                         return -1;
302                 } else if (rval < 0) {
303                         if (!select_was_interrupted_by_signal()) {
304                                 return -1;
305                         }
306                 }
307         }
308
309         len = recv(connsock, buf, 4095, 0);
310
311         if (len < 0) {
312                 return -1;
313         }
314
315         buf[len] = 0;
316
317         return handle_request(buf);
318 }
319
320 static void serve_ppm(int *pixels, int rectx, int recty)
321 {
322         unsigned char* rendered_frame;
323         unsigned char* row = (unsigned char*) malloc(render_width * 3);
324         int y;
325         char header[1024];
326
327         sprintf(header, 
328                 "HTTP/1.1 200 OK\n"
329                 "Content-Type: image/ppm\n"
330                 "Connection: close\n\n"
331                 "P6\n"
332                 "# Creator: blender frameserver v0.0.1\n"
333                 "%d %d\n"
334                 "255\n",
335                 rectx, recty);
336
337         safe_puts(header);
338
339         rendered_frame = (unsigned char *)pixels;
340
341         for (y = recty - 1; y >= 0; y--) {
342                 unsigned char* target = row;
343                 unsigned char* src = rendered_frame + rectx * 4 * y;
344                 unsigned char* end = src + rectx * 4;
345                 while (src != end) {
346                         target[2] = src[2];
347                         target[1] = src[1];
348                         target[0] = src[0];
349                         
350                         target += 3;
351                         src += 4;
352                 }
353                 safe_write((char*)row, 3 * rectx); 
354         }
355         free(row);
356         closesocket(connsock);
357         connsock = -1;
358 }
359
360 void append_frameserver(int frame, int *pixels, int rectx, int recty)
361 {
362         fprintf(stderr, "Serving frame: %d\n", frame);
363         if (write_ppm) {
364                 serve_ppm(pixels, rectx, recty);
365         }
366         if (connsock != -1) {
367                 closesocket(connsock);
368                 connsock = -1;
369         }
370 }
371
372 void end_frameserver()
373 {
374         if (connsock != -1) {
375                 closesocket(connsock);
376                 connsock = -1;
377         }
378         closesocket(sock);
379         shutdown_socket_system();
380 }
381