r955: Fix the Diffkey icon.
[cinelerra_cv.git] / cinelerra / playbackengine.C
blob6ca11e0e74ec9188de0845b589dec27d0f08f3b9
1 #include "cache.h"
2 #include "condition.h"
3 #include "bchash.h"
4 #include "edl.h"
5 #include "edlsession.h"
6 #include "localsession.h"
7 #include "mbuttons.h"
8 #include "mutex.h"
9 #include "mwindow.h"
10 #include "mwindowgui.h"
11 #include "patchbay.h"
12 #include "tracking.h"
13 #include "playbackengine.h"
14 #include "playtransport.h"
15 #include "preferences.h"
16 #include "renderengine.h"
17 #include "mainsession.h"
18 #include "trackcanvas.h"
19 #include "transportque.h"
20 #include "vrender.h"
23 PlaybackEngine::PlaybackEngine(MWindow *mwindow, Canvas *output)
24  : Thread(1, 0, 0)
26         this->mwindow = mwindow;
27         this->output = output;
28         is_playing_back = 0;
29         tracking_position = 0;
30         tracking_active = 0;
31         audio_cache = 0;
32         video_cache = 0;
33         last_command = STOP;
34         tracking_lock = new Mutex("PlaybackEngine::tracking_lock");
35         tracking_done = new Condition(1, "PlaybackEngine::tracking_done");
36         pause_lock = new Condition(0, "PlaybackEngine::pause_lock");
37         start_lock = new Condition(0, "PlaybackEngine::start_lock");
38         render_engine = 0;
39         debug = 0;
42 PlaybackEngine::~PlaybackEngine()
44         done = 1;
45         que->send_command(STOP,
46                 CHANGE_NONE, 
47                 0,
48                 0);
49         interrupt_playback();
51         Thread::join();
52         delete preferences;
53         delete command;
54         delete que;
55         delete_render_engine();
56         delete audio_cache;
57         delete video_cache;
58         delete tracking_lock;
59         delete tracking_done;
60         delete pause_lock;
61         delete start_lock;
64 int PlaybackEngine::create_objects()
66         int result = 0;
67         preferences = new Preferences;
68         command = new TransportCommand;
69         que = new TransportQue;
70 // Set the first change to maximum
71         que->command.change_type = CHANGE_ALL;
73         preferences->copy_from(mwindow->preferences);
75         done = 0;
76         Thread::start();
77         start_lock->lock("PlaybackEngine::create_objects");
78         return result;
81 ChannelDB* PlaybackEngine::get_channeldb()
83         PlaybackConfig *config = command->get_edl()->session->playback_config;
84         switch(config->vconfig->driver)
85         {
86                 case VIDEO4LINUX2JPEG:
87                         return mwindow->channeldb_v4l2jpeg;
88                         break;
89                 case PLAYBACK_BUZ:
90                         return mwindow->channeldb_buz;
91                         break;
92         }
93         return 0;
96 int PlaybackEngine::create_render_engine()
98 // Fix playback configurations
99         int current_vchannel = 0;
100         int current_achannel = 0;
102         delete_render_engine();
105         render_engine = new RenderEngine(this,
106                 preferences, 
107                 command, 
108                 output,
109                 mwindow->plugindb,
110                 get_channeldb());  
111         return 0;
114 void PlaybackEngine::delete_render_engine()
116         delete render_engine;
117         render_engine = 0;
120 void PlaybackEngine::arm_render_engine()
122         int current_achannel = 0, current_vchannel = 0;
123         if(render_engine)
124                 render_engine->arm_command(command,
125                         current_achannel,
126                         current_vchannel);
129 void PlaybackEngine::start_render_engine()
131         if(render_engine) render_engine->start_command();
134 void PlaybackEngine::wait_render_engine()
136         if(command->realtime && render_engine)
137         {
138                 render_engine->join();
139         }
142 void PlaybackEngine::create_cache()
144         if(audio_cache) delete audio_cache;
145         audio_cache = 0;
146         if(video_cache) delete video_cache;
147         video_cache = 0;
148         if(!audio_cache) 
149                 audio_cache = new CICache(preferences, mwindow->plugindb);
150         if(!video_cache) 
151                 video_cache = new CICache(preferences, mwindow->plugindb);
155 void PlaybackEngine::perform_change()
157         switch(command->change_type)
158         {
159                 case CHANGE_ALL:
160                         create_cache();
161                 case CHANGE_EDL:
162                         create_render_engine();
163                 case CHANGE_PARAMS:
164                         if(command->change_type != CHANGE_EDL &&
165                                 command->change_type != CHANGE_ALL)
166                                 render_engine->edl->synchronize_params(command->get_edl());
167                 case CHANGE_NONE:
168                         break;
169         }
172 void PlaybackEngine::sync_parameters(EDL *edl)
174 // TODO: lock out render engine from keyframe deletions
175         command->get_edl()->synchronize_params(edl);
176         if(render_engine) render_engine->edl->synchronize_params(edl);
180 void PlaybackEngine::interrupt_playback(int wait_tracking)
182         if(render_engine)
183                 render_engine->interrupt_playback();
185 // Stop pausing
186         pause_lock->unlock();
188 // Wait for tracking to finish if it is running
189         if(wait_tracking)
190         {
191                 tracking_done->lock("PlaybackEngine::interrupt_playback");
192                 tracking_done->unlock();
193         }
197 // Return 1 if levels exist
198 int PlaybackEngine::get_output_levels(double *levels, long position)
200         int result = 0;
201         if(render_engine && render_engine->do_audio)
202         {
203                 result = 1;
204                 render_engine->get_output_levels(levels, position);
205         }
206         return result;
210 int PlaybackEngine::get_module_levels(ArrayList<double> *module_levels, long position)
212         int result = 0;
213         if(render_engine && render_engine->do_audio)
214         {
215                 result = 1;
216                 render_engine->get_module_levels(module_levels, position);
217         }
218         return result;
221 int PlaybackEngine::brender_available(long position)
223         return 0;
226 void PlaybackEngine::init_cursor()
230 void PlaybackEngine::stop_cursor()
235 void PlaybackEngine::init_tracking()
237         if(!command->single_frame()) 
238                 tracking_active = 1;
239         else
240                 tracking_active = 0;
242         tracking_position = command->playbackstart;
243         tracking_done->lock("PlaybackEngine::init_tracking");
244         init_cursor();
247 void PlaybackEngine::stop_tracking()
249         tracking_active = 0;
250         stop_cursor();
251         tracking_done->unlock();
254 void PlaybackEngine::update_tracking(double position)
256         tracking_lock->lock("PlaybackEngine::update_tracking");
258         tracking_position = position;
260 // Signal that the timer is accurate.
261         if(tracking_active) tracking_active = 2;
262         tracking_timer.update();
263         tracking_lock->unlock();
266 double PlaybackEngine::get_tracking_position()
268         double result = 0;
270         tracking_lock->lock("PlaybackEngine::get_tracking_position");
273 // Adjust for elapsed time since last update_tracking.
274 // But tracking timer isn't accurate until the first update_tracking
275 // so wait.
276         if(tracking_active == 2)
277         {
278 //printf("PlaybackEngine::get_tracking_position %d %d %d\n", command->get_direction(), tracking_position, tracking_timer.get_scaled_difference(command->get_edl()->session->sample_rate));
281 // Don't interpolate when every frame is played.
282                 if(command->get_edl()->session->video_every_frame &&
283                         render_engine &&
284                         render_engine->do_video)
285                 {
286                         result = tracking_position;
287                 }
288                 else
289 // Interpolate
290                 {
291                         double loop_start = command->get_edl()->local_session->loop_start;
292                         double loop_end = command->get_edl()->local_session->loop_end;
293                         double loop_size = loop_end - loop_start;
295                         if(command->get_direction() == PLAY_FORWARD)
296                         {
297 // Interpolate
298                                 result = tracking_position + 
299                                         command->get_speed() * 
300                                         tracking_timer.get_difference() /
301                                         1000.0;
303 // Compensate for loop
304 //printf("PlaybackEngine::get_tracking_position 1 %d\n", command->get_edl()->local_session->loop_playback);
305                                 if(command->get_edl()->local_session->loop_playback)
306                                 {
307                                         while(result > loop_end) result -= loop_size;
308                                 }
309                         }
310                         else
311                         {
312 // Interpolate
313                                 result = tracking_position - 
314                                         command->get_speed() * 
315                                         tracking_timer.get_difference() /
316                                         1000.0;
318 // Compensate for loop
319                                 if(command->get_edl()->local_session->loop_playback)
320                                 {
321                                         while(result < loop_start) result += loop_size;
322                                 }
323                         }
325                 }
326         }
327         else
328                 result = tracking_position;
330         tracking_lock->unlock();
331 //printf("PlaybackEngine::get_tracking_position %f %f %d\n", result, tracking_position, tracking_active);
333 // Adjust for loop
335         return result;
338 void PlaybackEngine::update_transport(int command, int paused)
340 //      mwindow->gui->lock_window();
341 //      mwindow->gui->mbuttons->transport->update_gui_state(command, paused);
342 //      mwindow->gui->unlock_window();
345 void PlaybackEngine::run()
347         start_lock->unlock();
349         do
350         {
351 // Wait for current command to finish
352                 que->output_lock->lock("PlaybackEngine::run");
354                 wait_render_engine();
357 // Read the new command
358                 que->input_lock->lock("PlaybackEngine::run");
359                 if(done) return;
361                 command->copy_from(&que->command);
362                 que->command.reset();
363                 que->input_lock->unlock();
365 //printf("PlaybackEngine::run 1 %d\n", command->command);
368                 switch(command->command)
369                 {
370 // Parameter change only
371                         case COMMAND_NONE:
372 //                              command->command = last_command;
373                                 perform_change();
374                                 break;
376                         case PAUSE:
377                                 init_cursor();
378                                 pause_lock->lock("PlaybackEngine::run");
379                                 stop_cursor();
380                                 break;
382                         case STOP:
383 // No changing
384                                 break;
386                         case CURRENT_FRAME:
387                                 last_command = command->command;
388                                 perform_change();
389                                 arm_render_engine();
390 // Dispatch the command
391                                 start_render_engine();
392                                 break;
394                         default:
395                                 last_command = command->command;
396                                 is_playing_back = 1;
397                                 if(command->command == SINGLE_FRAME_FWD ||
398                                         command->command == SINGLE_FRAME_REWIND)
399                                 {
400                                         command->playbackstart = get_tracking_position();
401                                 }
403                                 perform_change();
404                                 arm_render_engine();
406 // Start tracking after arming so the tracking position doesn't change.
407 // The tracking for a single frame command occurs during PAUSE
408                                 init_tracking();
410 // Dispatch the command
411                                 start_render_engine();
412                                 break;
413                 }
416 //printf("PlaybackEngine::run 100\n");
417         }while(!done);