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
23 #include "bcsignals.h"
26 #include "condition.h"
28 #include "edlsession.h"
31 #include "mainsession.h"
34 #include "mwindowgui.h"
36 #include "packagedispatcher.h"
37 #include "preferences.h"
38 #include "renderfarm.h"
61 BRender::BRender(MWindow *mwindow)
64 this->mwindow = mwindow;
65 map_lock = new Mutex("BRender::map_lock");
66 completion_lock = new Condition(0, "BRender::completion_lock");
71 arguments[0] = arguments[1] = arguments[2] = 0;
81 TRACE("BRender::~BRender 1\n");
84 TRACE("BRender::~BRender 2\n");
86 TRACE("BRender::~BRender 3\n");
88 TRACE("BRender::~BRender 4\n");
92 TRACE("BRender::~BRender 5\n");
95 kill(master_pid, SIGKILL);
96 TRACE("BRender::~BRender 6\n");
98 TRACE("BRender::~BRender 7\n");
101 TRACE("BRender::~BRender 8\n");
103 TRACE("BRender::~BRender 9\n");
104 delete completion_lock;
105 TRACE("BRender::~BRender 10\n");
106 UNSET_TEMP(socket_path);
108 TRACE("BRender::~BRender 11\n");
109 if(arguments[0]) delete [] arguments[0];
110 TRACE("BRender::~BRender 12\n");
111 if(arguments[1]) delete [] arguments[1];
112 TRACE("BRender::~BRender 13\n");
113 if(arguments[2]) delete [] arguments[2];
114 TRACE("BRender::~BRender 14\n");
115 if(map) delete [] map;
116 TRACE("BRender::~BRender 15\n");
118 TRACE("BRender::~BRender 100\n");
121 void BRender::initialize()
124 // Create socket for background process.
126 sprintf(socket_path, "/tmp/cinelerra.");
127 uuid_generate(socket_temp);
128 uuid_unparse(socket_temp, socket_path + strlen(socket_path));
129 SET_TEMP(socket_path);
131 // Start background instance of executable since codecs aren't reentrant
134 // Wait for local node to start
135 thread = new BRenderThread(mwindow, this);
136 thread->initialize();
141 char string[BCTEXTLEN];
143 //printf("BRender::run 1 %d\n", getpid());
146 // Construct executable command with the designated filesystem port
147 fd = fopen("/proc/self/cmdline", "r");
150 (void)fread(string, 1, BCTEXTLEN, fd);
154 perror(_("BRender::fork_background: can't open /proc/self/cmdline.\n"));
156 arguments[0] = new char[strlen(string) + 1];
157 strcpy(arguments[0], string);
159 strcpy(string, "-b");
160 arguments[1] = new char[strlen(string) + 1];
161 strcpy(arguments[1], string);
163 arguments[2] = new char[strlen(socket_path) + 1];
164 strcpy(arguments[2], socket_path);
165 //printf("BRender::fork_background 1 %s\n", socket_path);
172 execvp(arguments[0], arguments);
173 perror("BRender::fork_background");
178 //printf("BRender::fork_background 1 %d\n", master_pid);
183 if(waitpid(master_pid, &return_value, WUNTRACED) < 0)
185 perror("BRender::run waitpid");
189 // Give the last position of the EDL which hasn't changed.
190 // We copy the EDL and restart rendering at the lesser of position and
192 void BRender::restart(EDL *edl)
194 BRenderCommand *new_command = new BRenderCommand;
196 new_command->copy_edl(edl);
197 new_command->command = BRenderCommand::BRENDER_RESTART;
198 thread->send_command(new_command);
199 // Map should be reallocated before this returns.
204 //printf("BRender::stop 1\n");
205 BRenderCommand *new_command = new BRenderCommand;
206 //printf("BRender::stop 1\n");
207 new_command->command = BRenderCommand::BRENDER_STOP;
208 //printf("BRender::stop 1\n");
209 thread->send_command(new_command);
210 //printf("BRender::stop 1\n");
211 completion_lock->lock("BRender::stop");
212 //printf("BRender::stop 2\n");
217 int BRender::get_last_contiguous(int64_t brender_start)
220 map_lock->lock("BRender::get_last_contiguous");
222 result = last_contiguous;
224 result = brender_start;
229 void BRender::allocate_map(int64_t brender_start, int64_t start, int64_t end)
231 map_lock->lock("BRender::allocate_map");
232 unsigned char *old_map = map;
233 map = new unsigned char[end];
236 memcpy(map, old_map, start);
240 // Zero all before brender start
241 bzero(map, brender_start);
242 // Zero all after current start
243 bzero(map + start, end - start);
247 last_contiguous = start;
248 mwindow->session->brender_end = (double)last_contiguous /
249 mwindow->edl->session->frame_rate;
253 int BRender::set_video_map(int64_t position, int value)
256 map_lock->lock("BRender::set_video_map");
259 if(value == BRender::NOT_SCANNED)
261 printf(_("BRender::set_video_map called to set NOT_SCANNED\n"));
271 if(position < map_size)
273 map[position] = value;
278 printf(_("BRender::set_video_map " _LD ": attempt to set beyond end of map " _LD ".\n"),
282 // Maintain last contiguous here to reduce search time
283 if(position == last_contiguous)
286 for(i = position + 1; i < map_size && map[i]; i++)
291 mwindow->session->brender_end = (double)last_contiguous /
292 mwindow->edl->session->frame_rate;
294 if(timer->get_difference() > 1000 || last_contiguous >= map_size)
305 mwindow->gui->lock_window("BRender::set_video_map");
306 mwindow->gui->update_timebar(1);
307 mwindow->gui->unlock_window();
325 BRenderCommand::BRenderCommand()
328 command = BRENDER_NONE;
332 BRenderCommand::~BRenderCommand()
334 // EDL should be zeroed if copied
335 if(edl) edl->Garbage::remove_user();
338 void BRenderCommand::copy_from(BRenderCommand *src)
340 this->edl = src->edl;
342 this->position = src->position;
343 this->command = src->command;
347 void BRenderCommand::copy_edl(EDL *edl)
350 this->edl->create_objects();
351 this->edl->copy_all(edl);
366 BRenderThread::BRenderThread(MWindow *mwindow, BRender *brender)
369 this->mwindow = mwindow;
370 this->brender = brender;
371 input_lock = new Condition(0, "BRenderThread::input_lock");
372 thread_lock = new Mutex("BRenderThread::thread_lock");
373 total_frames_lock = new Mutex("BRenderThread::total_frames_lock");
382 BRenderThread::~BRenderThread()
384 thread_lock->lock("BRenderThread::~BRenderThread");
386 input_lock->unlock();
387 thread_lock->unlock();
391 delete total_frames_lock;
392 if(command) delete command;
393 if(command_queue) delete command_queue;
394 if(preferences) delete preferences;
398 void BRenderThread::initialize()
403 void BRenderThread::send_command(BRenderCommand *command)
405 TRACE("BRenderThread::send_command 1");
406 thread_lock->lock("BRenderThread::send_command");
407 TRACE("BRenderThread::send_command 10");
409 if(this->command_queue)
411 delete this->command_queue;
412 this->command_queue = 0;
414 this->command_queue = command;
415 TRACE("BRenderThread::send_command 20");
418 input_lock->unlock();
419 thread_lock->unlock();
422 int BRenderThread::is_done(int do_lock)
424 if(do_lock) thread_lock->lock("BRenderThread::is_done");
426 if(do_lock) thread_lock->unlock();
430 void BRenderThread::run()
434 BRenderCommand *new_command = 0;
435 thread_lock->lock("BRenderThread::run 1");
444 // Wait for new command
446 thread_lock->unlock();
447 input_lock->lock("BRenderThread::run 2");
448 thread_lock->lock("BRenderThread::run 3");
451 // Pull the command off
454 new_command = command_queue;
458 thread_lock->unlock();
463 // Process the command here to avoid delay.
470 if(new_command->command == BRenderCommand::BRENDER_STOP)
475 // if(command) delete command;
476 // command = new_command;
479 if(new_command->command == BRenderCommand::BRENDER_RESTART)
481 // Compare EDL's and get last equivalent position in new EDL
482 if(command && command->edl)
483 new_command->position =
484 new_command->edl->equivalent_output(command->edl);
486 new_command->position = 0;
490 brender->completion_lock->lock("BRenderThread::run 4");
492 if(new_command->edl->tracks->total_playable_vtracks())
494 if(command) delete command;
495 command = new_command;
507 void BRenderThread::stop()
512 farm_server->wait_clients();
520 brender->completion_lock->unlock();
523 void BRenderThread::start()
525 // Reset return parameters
531 // Allocate render farm.
534 //printf("BRenderThread::start 1\n");
535 preferences = new Preferences;
536 preferences->copy_from(mwindow->preferences);
537 packages = new PackageDispatcher;
539 // Fix preferences to use local node
540 if(!preferences->use_renderfarm)
542 preferences->use_renderfarm = 1;
543 preferences->delete_nodes();
545 preferences->add_node(brender->socket_path,
548 preferences->local_rate);
549 //printf("BRenderThread::start 1 %s\n", brender->socket_path);
550 preferences->brender_asset->use_header = 0;
551 preferences->brender_asset->frame_rate = command->edl->session->frame_rate;
552 preferences->brender_asset->width = command->edl->session->output_w;
553 preferences->brender_asset->height = command->edl->session->output_h;
555 // Get last contiguous and reset map.
556 // If the framerate changes, last good should be 0 from the user.
557 int brender_start = (int)(command->edl->session->brender_start *
558 command->edl->session->frame_rate);
559 int last_contiguous = brender->last_contiguous;
560 int last_good = (int)(command->edl->session->frame_rate *
562 if(last_good < 0) last_good = last_contiguous;
563 int start_frame = MIN(last_contiguous, last_good);
564 start_frame = MAX(start_frame, brender_start);
565 int64_t end_frame = Units::round(command->edl->tracks->total_video_length() *
566 command->edl->session->frame_rate);
567 if(end_frame < start_frame) end_frame = start_frame;
570 printf("BRenderThread::start 1 map=%d equivalent=%d brender_start=%d result=%d end=" _LD "\n",
571 last_contiguous, last_good, brender_start, start_frame, end_frame);
575 brender->allocate_map(brender_start, start_frame, end_frame);
577 //printf("BRenderThread::start 2\n");
579 result = packages->create_packages(mwindow,
583 preferences->brender_asset,
584 (double)start_frame / command->edl->session->frame_rate,
585 (double)end_frame / command->edl->session->frame_rate,
589 //printf("BRenderThread::start 3 %d\n", result);
590 farm_server = new RenderFarmServer(mwindow,
597 preferences->brender_asset,
602 //printf("BRenderThread::start 4\n");
603 result = farm_server->start_clients();
606 // No local rendering because of codec problems.
612 // No-one must be retrieving a package when packages are deleted.
613 //printf("BRenderThread::start 7 %p\n", farm_server);
616 //printf("BRenderThread::start 8 %p\n", preferences);
618 //printf("BRenderThread::start 9\n");
624 //printf("BRenderThread::start 10\n");