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