4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "filesystem.h"
33 #include "pluginserver.h"
34 #include "preferences.h"
35 #include "renderfarm.h"
36 #include "renderfarmclient.h"
37 //#include "renderfarmfsclient.h"
38 #include "sighandler.h"
40 #include <arpa/inet.h>
44 #include <netinet/in.h>
47 #include <sys/socket.h>
48 #include <sys/types.h>
55 #define AF_FILE AF_LOCAL
58 // The render client waits for connections from the server.
59 // Then it starts a thread for each connection.
60 RenderFarmClient::RenderFarmClient(int port,
65 char string[BCTEXTLEN];
68 this->deamon_path = deamon_path;
69 SigHandler *signals = new SigHandler;
70 signals->initialize("/tmp/cinelerra_farm%d.dmp");
73 (void)nice(nice_value);
76 MWindow::init_defaults(boot_defaults, config_path);
77 boot_preferences = new Preferences;
78 boot_preferences->load_defaults(boot_defaults);
79 MWindow::init_plugins(0, boot_preferences);
80 MWindow::init_ladspa_plugins(0, boot_preferences);
81 BC_Signals::set_catch_segv(boot_preferences->trap_sigsegv);
82 BC_Signals::set_catch_intr(0);
83 if( boot_preferences->trap_sigsegv ) {
84 BC_Trace::enable_locks();
87 BC_Trace::disable_locks();
90 strcpy(string, boot_preferences->plugin_dir);
91 strcat(string, "/" FONT_SEARCHPATH);
92 BC_Resources::init_fontconfig(string);
98 RenderFarmClient::~RenderFarmClient()
100 delete boot_defaults;
101 delete boot_preferences;
105 void RenderFarmClient::main_loop()
108 BC_WindowBase::get_resources()->vframe_shm = 1;
111 // Open listening port
115 struct sockaddr_in addr;
117 addr.sin_family = AF_INET;
118 addr.sin_port = htons((unsigned short)port);
119 addr.sin_addr.s_addr = htonl(INADDR_ANY);
121 if((socket_fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
123 perror(_("RenderFarmClient::main_loop: socket"));
129 if( setsockopt(socket_fd, SOL_SOCKET, SO_LINGER, &lgr, sizeof(lgr)) < 0 )
130 perror("RenderFarmClient::setsockopt:setlinger 0");
133 (struct sockaddr*)&addr,
137 _("RenderFarmClient::main_loop: bind port %d: %s"),
145 struct sockaddr_un addr;
146 addr.sun_family = AF_FILE;
147 strcpy(addr.sun_path, deamon_path);
148 int size = (offsetof(struct sockaddr_un, sun_path) +
149 strlen(deamon_path) + 1);
151 if((socket_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
153 perror(_("RenderFarmClient::main_loop: socket"));
159 if( setsockopt(socket_fd, SOL_SOCKET, SO_LINGER, &lgr, sizeof(lgr)) < 0 )
160 perror("RenderFarmClient::setsockopt:setlinger 1");
163 (struct sockaddr*)&addr,
167 _("RenderFarmClient::main_loop: bind path %s: %s\n"),
174 // Wait for connections
175 printf("RenderFarmClient::main_loop: client started\n");
178 if(listen(socket_fd, 256) < 0) {
179 perror(_("RenderFarmClient::main_loop: listen"));
187 struct sockaddr_in clientname;
188 socklen_t size = sizeof(clientname);
189 if( (new_socket_fd = accept(socket_fd,
190 (struct sockaddr*)&clientname, &size) ) < 0 ) {
191 perror(_("RenderFarmClient::main_loop: accept"));
194 printf("RenderFarmClient::main_loop: Session started from %s\n", inet_ntoa(clientname.sin_addr));
195 RenderFarmClientThread *thread =
196 new RenderFarmClientThread(this);
197 thread->main_loop(new_socket_fd);
201 struct sockaddr_un clientname;
202 socklen_t size = sizeof(clientname);
203 if( (new_socket_fd = accept(socket_fd,
204 (struct sockaddr*)&clientname, &size)) < 0 ) {
205 perror(_("RenderFarmClient::main_loop: accept"));
208 printf("RenderFarmClient::main_loop: Session started from %s\n", clientname.sun_path);
209 RenderFarmClientThread *thread =
210 new RenderFarmClientThread(this);
211 thread->main_loop(new_socket_fd);
216 void RenderFarmClient::kill_client()
218 printf("RenderFarmClient::kill_client 1\n");
221 printf("RenderFarmClient::kill_client 2\n");
223 kill(this_pid, SIGKILL);
228 // The thread requests jobs from the server until the job table is empty
229 // or the server reports an error. This thread must poll the server
230 // after every frame for the error status.
231 // Detaches when finished.
232 RenderFarmClientThread::RenderFarmClientThread(RenderFarmClient *client)
235 this->client = client;
237 frames_per_second = 0;
238 Thread::set_synchronous(0);
240 mutex_lock = new Mutex("RenderFarmClientThread::mutex_lock");
245 RenderFarmClientThread::~RenderFarmClientThread()
247 // if(fs_client) delete fs_client;
254 int RenderFarmClientThread::send_request_header(int request,
257 unsigned char datagram[5];
258 datagram[0] = request;
262 // printf("RenderFarmClientThread::send_request_header %d %02x%02x%02x%02x%02x\n",
263 // request, datagram[0], datagram[1], datagram[2], datagram[3], datagram[4]);
265 return (write_socket((char*)datagram, 5) != 5);
268 int RenderFarmClientThread::write_socket(char *data, int len)
270 //printf("RenderFarmClientThread::write_socket 1\n");
271 int result = write(socket_fd, data, len);
272 //printf("RenderFarmClientThread::write_socket 10\n");
276 int RenderFarmClientThread::read_socket(char *data, int len)
280 //printf("RenderFarmClientThread::read_socket 1\n");
282 watchdog->begin_request();
283 while(len > 0 && bytes_read >= 0)
285 bytes_read = read(socket_fd, data + offset, len);
289 offset += bytes_read;
298 watchdog->end_request();
299 //printf("RenderFarmClientThread::read_socket 10\n");
304 int RenderFarmClientThread::write_int64(int64_t value)
306 unsigned char data[sizeof(int64_t)];
307 data[0] = (value >> 56) & 0xff;
308 data[1] = (value >> 48) & 0xff;
309 data[2] = (value >> 40) & 0xff;
310 data[3] = (value >> 32) & 0xff;
311 data[4] = (value >> 24) & 0xff;
312 data[5] = (value >> 16) & 0xff;
313 data[6] = (value >> 8) & 0xff;
314 data[7] = value & 0xff;
315 return (write_socket((char*)data, sizeof(int64_t)) != sizeof(int64_t));
318 int64_t RenderFarmClientThread::read_int64(int *error)
321 if(!error) error = &temp;
323 unsigned char data[sizeof(int64_t)];
324 *error = (read_socket((char*)data, sizeof(int64_t)) != sizeof(int64_t));
326 // Make it return 1 if error so it can be used to read a result code from the
331 result = (((int64_t)data[0]) << 56) |
332 (((uint64_t)data[1]) << 48) |
333 (((uint64_t)data[2]) << 40) |
334 (((uint64_t)data[3]) << 32) |
335 (((uint64_t)data[4]) << 24) |
336 (((uint64_t)data[5]) << 16) |
337 (((uint64_t)data[6]) << 8) |
343 void RenderFarmClientThread::read_string(char* &string)
345 unsigned char header[4];
346 if(read_socket((char*)header, 4) != 4)
352 int64_t len = (((u_int32_t)header[0]) << 24) |
353 (((u_int32_t)header[1]) << 16) |
354 (((u_int32_t)header[2]) << 8) |
355 ((u_int32_t)header[3]);
359 string = new char[len];
360 if(read_socket(string, len) != len)
371 void RenderFarmClientThread::abort()
373 send_completion(socket_fd);
378 void RenderFarmClientThread::lock(const char *location)
380 mutex_lock->lock(location);
383 void RenderFarmClientThread::unlock()
385 mutex_lock->unlock();
388 void RenderFarmClientThread::get_command(int socket_fd, int *command)
391 *command = read_int64(&error);
400 void RenderFarmClientThread::read_preferences(int socket_fd,
401 Preferences *preferences)
403 lock("RenderFarmClientThread::read_preferences");
404 send_request_header(RENDERFARM_PREFERENCES,
411 defaults.load_string((char*)string);
412 preferences->load_defaults(&defaults);
420 void RenderFarmClientThread::read_asset(int socket_fd, Asset *asset)
422 lock("RenderFarmClientThread::read_asset");
423 send_request_header(RENDERFARM_ASSET,
428 read_string(string1);
429 read_string(string2);
434 file.read_from_string((char*)string2);
440 defaults.load_string((char*)string1);
441 asset->load_defaults(&defaults, 0, 1, 1, 1, 1, 1);
443 //printf("RenderFarmClientThread::read_asset %d\n", __LINE__);
451 void RenderFarmClientThread::read_edl(int socket_fd,
452 EDL *edl, Preferences *preferences)
454 lock("RenderFarmClientThread::read_edl");
455 send_request_header(RENDERFARM_EDL, 0);
461 file.read_from_string((char*)string);
464 edl->load_xml(&file, LOAD_ALL);
468 int RenderFarmClientThread::read_package(int socket_fd, RenderPackage *package)
470 lock("RenderFarmClientThread::read_package");
471 send_request_header(RENDERFARM_PACKAGE,
474 unsigned char datagram[5];
478 // Fails if -ieee isn't set.
479 int64_t fixed = !EQUIV(frames_per_second, 0.0) ?
480 (int64_t)(frames_per_second * 65536.0) : 0;
482 write_socket((char*)datagram, 4);
485 //printf("RenderFarmClientThread::read_package 1 %f %ld\n", frames_per_second, fixed);
487 unsigned char *data_ptr;
489 //printf("RenderFarmClientThread::read_package 2 %p\n", data);
490 // Signifies end of session.
493 // printf(_("RenderFarmClientThread::read_package no output path received.\n"));
498 //printf("RenderFarmClientThread::read_package 2\n");
501 data_ptr = (unsigned char*)data;
502 strcpy(package->path, data);
503 data_ptr += strlen(package->path);
505 package->audio_start = READ_INT32(data_ptr);
507 package->audio_end = READ_INT32(data_ptr);
509 package->video_start = READ_INT32(data_ptr);
511 package->video_end = READ_INT32(data_ptr);
513 package->use_brender = READ_INT32(data_ptr);
515 package->audio_do = READ_INT32(data_ptr);
517 package->video_do = READ_INT32(data_ptr);
525 int RenderFarmClientThread::send_completion(int socket_fd)
527 lock("RenderFarmClientThread::send_completion");
528 int result = send_request_header(RENDERFARM_DONE, 0);
534 void RenderFarmClientThread::ping_server()
536 lock("RenderFarmClientThread::ping_server");
537 send_request_header(RENDERFARM_KEEPALIVE, 0);
543 void RenderFarmClientThread::main_loop(int socket_fd)
545 this->socket_fd = socket_fd;
550 void RenderFarmClientThread::run()
552 // Create new memory space
557 waitpid(pid, &return_value, 0);
561 // Get the pid of the fork if inside the fork
563 BC_Signals::set_trap_hook(trap_hook, this);
566 int socket_fd = this->socket_fd;
567 int watchdog_timeout = client->boot_preferences->renderfarm_watchdog_timeout;
568 if( watchdog_timeout > 0 )
569 init_client_keepalive(watchdog_timeout);
571 // Get command to run
573 lock("RenderFarmClientThread::run");
574 get_command(socket_fd, &command);
577 //printf("RenderFarmClientThread::run command=%d\n", command);
581 case RENDERFARM_TUNER:
584 case RENDERFARM_PACKAGES:
585 do_packages(socket_fd);
593 void RenderFarmClientThread::init_client_keepalive(int timeout_secs)
595 keep_alive = new RenderFarmKeepalive(this);
597 watchdog = new RenderFarmWatchdog(timeout_secs, 0, this);
603 void RenderFarmClientThread::do_tuner(int socket_fd)
605 // Currently only 1 tuner driver. Maybe more someday.
606 // DVBTune server(this);
607 // server.main_loop();
608 // ::close(socket_fd);
612 void RenderFarmClientThread::do_packages(int socket_fd)
614 RenderPackage *package;
615 Asset *default_asset;
616 Preferences *preferences;
620 FarmPackageRenderer package_renderer(this, socket_fd);
625 //printf("RenderFarmClientThread::run 2\n");
627 preferences = new Preferences;
628 default_asset = new Asset;
629 package = new RenderPackage;
631 edl->create_objects();
633 //printf("RenderFarmClientThread::run 3\n");
634 read_preferences(socket_fd, preferences);
635 //printf("RenderFarmClientThread::run 4\n");
636 read_asset(socket_fd, default_asset);
637 //printf("RenderFarmClientThread::run 5\n");
638 read_edl(socket_fd, edl, preferences);
640 //printf("RenderFarmClientThread::run 6\n");
642 package_renderer.initialize(0, edl, preferences, default_asset);
647 //printf("RenderFarmClientThread::run 5\n");
648 result = read_package(socket_fd, package);
649 //printf("RenderFarmClientThread::run 6 %d\n", result);
655 //printf("RenderFarmClientThread::run 7\n");
657 result = send_completion(socket_fd);
665 if(package_renderer.render_package(package))
667 //printf("RenderFarmClientThread::run 8\n");
668 result = send_completion(socket_fd);
673 frames_per_second = package_renderer.frames_per_second;
674 // frames_per_second = (double)(package->video_end - package->video_start) /
675 // ((double)timer.get_difference() / 1000);
677 //printf("RenderFarmClientThread::run 9\n");
684 //printf("RenderFarmClientThread::run 9\n");
685 default_asset->Garbage::remove_user();
686 //printf("RenderFarmClientThread::run 10\n");
687 edl->Garbage::remove_user();
689 //printf("RenderFarmClientThread::run 11\n");
691 printf(_("RenderFarmClientThread::run: Session finished.\n"));
694 void RenderFarmClientThread::trap_hook(FILE *fp, void *vp)
696 RenderFarmClientThread *thread = (RenderFarmClientThread*)vp;
697 fprintf(fp, "\nEDL:\n");
698 if( thread->edl ) thread->edl->dump(fp);
703 RenderFarmKeepalive::RenderFarmKeepalive(
704 RenderFarmClientThread *client_thread)
707 this->client_thread = client_thread;
711 RenderFarmKeepalive::~RenderFarmKeepalive()
718 void RenderFarmKeepalive::run()
726 //printf("RenderFarmKeepalive::run 1\n");
727 // watchdog thread kills this if it gets stuck
728 client_thread->ping_server();
729 //printf("RenderFarmKeepalive::run 10\n");
735 FarmPackageRenderer::FarmPackageRenderer(RenderFarmClientThread *thread,
739 this->thread = thread;
740 this->socket_fd = socket_fd;
744 FarmPackageRenderer::~FarmPackageRenderer()
749 int FarmPackageRenderer::get_result()
751 thread->lock("FarmPackageRenderer::get_result");
752 thread->send_request_header(RENDERFARM_GET_RESULT,
754 unsigned char data[1];
756 if(thread->read_socket((char*)data, 1) != 1)
765 void FarmPackageRenderer::set_result(int value)
767 thread->lock("FarmPackageRenderer::set_result");
768 thread->send_request_header(RENDERFARM_SET_RESULT,
770 unsigned char data[1];
772 thread->write_socket((char*)data, 1);
776 void FarmPackageRenderer::set_progress(int64_t total_samples)
778 thread->lock("FarmPackageRenderer::set_progress");
779 thread->send_request_header(RENDERFARM_PROGRESS,
781 unsigned char datagram[8];
783 STORE_INT32(total_samples);
786 int64_t fixed = (!EQUIV(frames_per_second, 0.0)) ?
787 (int64_t)(frames_per_second * 65536.0) : 0;
791 thread->write_socket((char*)datagram, 8);
795 int FarmPackageRenderer::set_video_map(int64_t position, int value)
798 unsigned char datagram[8];
799 char return_value[1];
802 thread->lock("FarmPackageRenderer::set_video_map");
803 thread->send_request_header(RENDERFARM_SET_VMAP,
805 STORE_INT32(position);
807 thread->write_socket((char*)datagram, 8);
809 // Get completion since the GUI may be locked for a long time.
810 if(!thread->read_socket(return_value, 1))