this patch features several cleanups and bugfixes for the sequencer:
[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 <windows.h>
26 #include <winbase.h>
27 #include <direct.h>
28 #else
29 #include <sys/time.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <net/if.h>
34 #include <netdb.h>
35 #include <sys/ioctl.h>
36 #include <errno.h>
37 #include <unistd.h>
38 #include <sys/un.h>
39 #include <fcntl.h>
40 #endif
41
42 #include <stdlib.h>
43
44 #include "MEM_guardedalloc.h"
45 #include "BLI_blenlib.h"
46 #include "DNA_userdef_types.h"
47
48 #include "BKE_bad_level_calls.h"
49 #include "BKE_global.h"
50
51 #include "IMB_imbuf_types.h"
52 #include "IMB_imbuf.h"
53
54 #include "DNA_scene_types.h"
55 #include "blendef.h"
56
57 #ifdef HAVE_CONFIG_H
58 #include <config.h>
59 #endif
60
61 /*
62   Big red FIXME:
63
64   You can't simply press escape to stop the frameserver, since somehow
65   the escape signal handling does not work, when you wait for a connection.
66
67   You have to point your favorite webbrowser to
68
69   blenderserver:port
70
71   and click on "Stop Rendering"
72
73   It does help, if you start blender using "-p 0 0 800 600" e.g...
74
75 */
76
77 static int sock;
78 static int connsock;
79 static int write_ppm;
80 static int render_width;
81 static int render_height;
82
83
84 #if !defined(_WIN32)
85 static int closesocket(int fd) {
86         return close(fd);
87 }
88 #endif
89
90 void start_frameserver(RenderData *rd, int rectx, int recty)
91 {
92         struct sockaddr_in      addr;
93
94         if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
95                 G.afbreek = 1; /* Abort render */
96                 error("Can't open socket");
97                 return;
98         }
99
100         addr.sin_family = AF_INET;
101         addr.sin_port = htons(U.frameserverport);
102         addr.sin_addr.s_addr = INADDR_ANY;
103
104         if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
105                 G.afbreek = 1; /* Abort render */
106                 error("Can't bind to socket");
107                 return;
108         }
109
110         if (listen(sock, SOMAXCONN) < 0) {
111                 G.afbreek = 1; /* Abort render */
112                 error("Can't establish listen backlog");
113                 return;
114         }
115         connsock = -1;
116
117         render_width = rectx;
118         render_height = recty;
119 }
120
121 static char index_page[] 
122
123 "HTTP/1.1 200 OK\n"
124 "Content-Type: text/html\n\n"
125 "<html><head><title>Blender Frameserver</title></head>\n"
126 "<body><pre>\n"
127 "<H2>Blender Frameserver</H2>\n"
128 "<A HREF=info.txt>Render Info</A><br>\n"
129 "<A HREF=close.txt>Stop Rendering</A><br>\n"
130 "\n"
131 "Images can be found here\n"
132 "\n"
133 "images/ppm/%d.ppm\n"
134 "\n"
135 "</pre></body></html>\n";
136
137 static char good_bye[]
138 = "HTTP/1.1 200 OK\n"
139 "Content-Type: text/html\n\n"
140 "<html><head><title>Blender Frameserver</title></head>\n"
141 "<body><pre>\n"
142 "Render stopped. Goodbye</pre></body></html>";
143
144 static int safe_write(char * s, int tosend)
145 {
146         int total = tosend;
147         do {
148                 int got = send(connsock, s, tosend, 0);
149                 if (got < 0) {
150                         return got;
151                 }
152                 tosend -= got;
153                 s += got;
154         } while (tosend > 0);
155
156         return total;
157 }
158
159 static int safe_puts(char * s)
160 {
161         return safe_write(s, strlen(s));
162 }
163
164 static int handle_request(char * req)
165 {
166         char * p;
167         char * path;
168         int pathlen;
169
170         if (memcmp(req, "GET ", 4) != 0) {
171                 return -1;
172         }
173            
174         p = req + 4;
175         path = p;
176
177         while (*p != ' ' && *p) p++;
178
179         *p = 0;
180
181         if (strcmp(path, "/index.html") == 0
182             || strcmp(path, "/") == 0) {
183                 safe_puts(index_page);
184                 return -1;
185         }
186
187         write_ppm = 0;
188         pathlen = strlen(path);
189
190         if (pathlen > 12 && memcmp(path, "/images/ppm/", 12) == 0) {
191                 write_ppm = 1;
192                 return atoi(path + 12);
193         }
194         if (strcmp(path, "/info.txt") == 0) {
195                 char buf[4096];
196
197                 sprintf(buf, 
198                         "HTTP/1.1 200 OK\n"
199                         "Content-Type: text/html\n\n"
200                         "start %d\n"
201                         "end %d\n"
202                         "width %d\n"
203                         "height %d\n" 
204                         "rate %d\n"
205                         "ratescale %d\n",
206                         G.scene->r.sfra,
207                         G.scene->r.efra,
208                         render_width,
209                         render_height,
210                         G.scene->r.frs_sec,
211                         1
212                         );
213
214                 safe_puts(buf);
215                 return -1;
216         }
217         if (strcmp(path, "/close.txt") == 0) {
218                 safe_puts(good_bye);
219                 G.afbreek = 1; /* Abort render */
220                 return -1;
221         }
222         return -1;
223 }
224
225 int frameserver_loop()
226 {
227         fd_set readfds;
228 #if !defined(_WIN32)
229         struct timeval tv;
230 #endif
231         struct sockaddr_in      addr;
232         int len;
233         char buf[4096];
234         int rval;
235
236         if (connsock != -1) {
237                 closesocket(connsock);
238                 connsock = -1;
239         }
240
241 #if !defined(_WIN32)
242         /* FIXME: Don't know, how to wait for socket on Windows ... */
243
244         tv.tv_sec = 1;
245         tv.tv_usec = 0;
246
247         FD_ZERO(&readfds);
248         FD_SET(sock, &readfds);
249
250         rval = select(sock + 1, &readfds, NULL, NULL, &tv);
251         if (rval < 0) {
252                 return -1;
253         }
254
255         if (rval == 0) { /* nothing to be done */
256                 return -1;
257         }
258 #endif
259
260         len = sizeof(addr);
261
262         if ((connsock = accept(sock, (struct sockaddr *)&addr, &len)) < 0) {
263                 return -1;
264         }
265
266 #if !defined(_WIN32)
267         /* FIXME: Don't know, how to wait for socket on Windows ... */
268
269         FD_ZERO(&readfds);
270         FD_SET(connsock, &readfds);
271
272         for (;;) {
273                 /* give 10 seconds for telnet testing... */
274                 tv.tv_sec = 10;
275                 tv.tv_usec = 0;
276
277                 rval = select(connsock + 1, &readfds, NULL, NULL, &tv);
278                 if (rval > 0) {
279                         break;
280                 } else if (rval == 0) {
281                         return -1;
282                 } else if (rval < 0) {
283                         if (errno != EINTR) {
284                                 return -1;
285                         }
286                 }
287         }
288 #endif
289
290         len = recv(connsock, buf, 4095, 0);
291
292         if (len < 0) {
293                 return -1;
294         }
295
296         buf[len] = 0;
297
298         return handle_request(buf);
299 }
300
301 static void serve_ppm(int *pixels, int rectx, int recty)
302 {
303         unsigned char* rendered_frame;
304         unsigned char* row = (unsigned char*) malloc(render_width * 3);
305         int y;
306         char header[1024];
307
308         sprintf(header, 
309                 "HTTP/1.1 200 OK\n"
310                 "Content-Type: image/ppm\n"
311                 "Connection: close\n\n"
312                 "P6\n"
313                 "# Creator: blender frameserver v0.0.1\n"
314                 "%d %d\n"
315                 "255\n",
316                 rectx, recty);
317
318         safe_puts(header);
319
320         rendered_frame = (unsigned char *)pixels;
321
322         for (y = recty - 1; y >= 0; y--) {
323                 unsigned char* target = row;
324                 unsigned char* src = rendered_frame + rectx * 4 * y;
325                 unsigned char* end = src + rectx * 4;
326                 while (src != end) {
327                         target[2] = src[2];
328                         target[1] = src[1];
329                         target[0] = src[0];
330                         
331                         target += 3;
332                         src += 4;
333                 }
334                 safe_write(row, 3 * rectx); 
335         }
336         free(row);
337         closesocket(connsock);
338         connsock = -1;
339 }
340
341 void append_frameserver(int frame, int *pixels, int rectx, int recty)
342 {
343         fprintf(stderr, "Serving frame: %d\n", frame);
344         if (write_ppm) {
345                 serve_ppm(pixels, rectx, recty);
346         }
347         if (connsock != -1) {
348                 closesocket(connsock);
349                 connsock = -1;
350         }
351 }
352
353 void end_frameserver()
354 {
355         if (connsock != -1) {
356                 closesocket(connsock);
357                 connsock = -1;
358         }
359         closesocket(sock);
360 }
361