rework android-rmt display, add a few buttons
[goodguy/history.git] / cinelerra-5.0 / cinelerra / playbackengine.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  * 
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.
10  * 
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.
15  * 
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
19  * 
20  */
21
22 #include "bchash.h"
23 #include "bcsignals.h"
24 #include "cache.h"
25 #include "condition.h"
26 #include "edl.h"
27 #include "edlsession.h"
28 #include "localsession.h"
29 #include "mbuttons.h"
30 #include "mutex.h"
31 #include "mwindow.h"
32 #include "mwindowgui.h"
33 #include "patchbay.h"
34 #include "tracking.h"
35 #include "playbackengine.h"
36 #include "playtransport.h"
37 #include "preferences.h"
38 #include "renderengine.h"
39 #include "mainsession.h"
40 #include "trackcanvas.h"
41 #include "transportque.h"
42 #include "vrender.h"
43
44
45 PlaybackEngine::PlaybackEngine(MWindow *mwindow, Canvas *output)
46  : Thread(1, 0, 0)
47 {
48         this->mwindow = mwindow;
49         this->output = output;
50         is_playing_back = 0;
51         tracking_position = 0;
52         tracking_active = 0;
53         audio_cache = 0;
54         video_cache = 0;
55         last_command = STOP;
56         tracking_lock = new Mutex("PlaybackEngine::tracking_lock");
57         renderengine_lock = new Mutex("PlaybackEngine::renderengine_lock");
58         tracking_done = new Condition(1, "PlaybackEngine::tracking_done");
59         pause_lock = new Condition(0, "PlaybackEngine::pause_lock");
60         start_lock = new Condition(0, "PlaybackEngine::start_lock");
61         render_engine = 0;
62         debug = 0;
63 }
64
65 PlaybackEngine::~PlaybackEngine()
66 {
67         done = 1;
68         que->send_command(STOP,
69                 CHANGE_NONE, 
70                 0,
71                 0);
72         interrupt_playback();
73
74         Thread::join();
75         delete preferences;
76         delete command;
77         delete que;
78         delete_render_engine();
79         delete audio_cache;
80         delete video_cache;
81         delete tracking_lock;
82         delete tracking_done;
83         delete pause_lock;
84         delete start_lock;
85         delete renderengine_lock;
86 }
87
88 void PlaybackEngine::create_objects()
89 {
90         preferences = new Preferences;
91         command = new TransportCommand;
92         que = new TransportQue;
93 // Set the first change to maximum
94         que->command.change_type = CHANGE_ALL;
95
96         preferences->copy_from(mwindow->preferences);
97
98         done = 0;
99         Thread::start();
100         start_lock->lock("PlaybackEngine::create_objects");
101 }
102
103 ChannelDB* PlaybackEngine::get_channeldb()
104 {
105         PlaybackConfig *config = command->get_edl()->session->playback_config;
106         switch(config->vconfig->driver)
107         {
108                 case VIDEO4LINUX2JPEG:
109                         return mwindow->channeldb_v4l2jpeg;
110                 case PLAYBACK_BUZ:
111                         return mwindow->channeldb_buz;
112         }
113         return 0;
114 }
115
116 int PlaybackEngine::create_render_engine()
117 {
118 // Fix playback configurations
119         delete_render_engine();
120
121
122         render_engine = new RenderEngine(this,
123                 preferences, 
124                 output,
125                 get_channeldb(),
126                 0);
127 //printf("PlaybackEngine::create_render_engine %d\n", __LINE__);
128         return 0;
129 }
130
131 void PlaybackEngine::delete_render_engine()
132 {
133         renderengine_lock->lock("PlaybackEngine::delete_render_engine");
134         delete render_engine;
135         render_engine = 0;
136         renderengine_lock->unlock();
137 }
138
139 void PlaybackEngine::arm_render_engine()
140 {
141         if(render_engine)
142                 render_engine->arm_command(command);
143 }
144
145 void PlaybackEngine::start_render_engine()
146 {
147         if(render_engine) render_engine->start_command();
148 }
149
150 void PlaybackEngine::wait_render_engine()
151 {
152         if(command->realtime && render_engine)
153         {
154                 render_engine->join();
155         }
156 }
157
158 void PlaybackEngine::create_cache()
159 {
160         if(audio_cache) { delete audio_cache;  audio_cache = 0; }
161         if(video_cache) { delete video_cache;  video_cache = 0; }
162         if(!audio_cache) audio_cache = new CICache(preferences);
163         if(!video_cache) video_cache = new CICache(preferences);
164 }
165
166
167 void PlaybackEngine::perform_change()
168 {
169         switch(command->change_type)
170         {
171                 case CHANGE_ALL:
172                         create_cache();
173                 case CHANGE_EDL:
174                         create_render_engine();
175                 case CHANGE_PARAMS:
176                         if(command->change_type != CHANGE_EDL &&
177                                 (uint32_t)command->change_type != CHANGE_ALL)
178                                 render_engine->get_edl()->synchronize_params(command->get_edl());
179                 case CHANGE_NONE:
180                         break;
181         }
182 }
183
184 void PlaybackEngine::sync_parameters(EDL *edl)
185 {
186 // TODO: lock out render engine from keyframe deletions
187         command->get_edl()->synchronize_params(edl);
188         if(render_engine) render_engine->get_edl()->synchronize_params(edl);
189 }
190
191
192 void PlaybackEngine::interrupt_playback(int wait_tracking)
193 {
194         renderengine_lock->lock("PlaybackEngine::interrupt_playback");
195         if(render_engine)
196                 render_engine->interrupt_playback();
197         renderengine_lock->unlock();
198
199 // Stop pausing
200         pause_lock->unlock();
201
202 // Wait for tracking to finish if it is running
203         if(wait_tracking)
204         {
205                 tracking_done->lock("PlaybackEngine::interrupt_playback");
206                 tracking_done->unlock();
207         }
208 }
209
210
211 // Return 1 if levels exist
212 int PlaybackEngine::get_output_levels(double *levels, long position)
213 {
214         int result = 0;
215         if(render_engine && render_engine->do_audio)
216         {
217                 result = 1;
218                 render_engine->get_output_levels(levels, position);
219         }
220         return result;
221 }
222
223
224 int PlaybackEngine::get_module_levels(ArrayList<double> *module_levels, long position)
225 {
226         int result = 0;
227         if(render_engine && render_engine->do_audio)
228         {
229                 result = 1;
230                 render_engine->get_module_levels(module_levels, position);
231         }
232         return result;
233 }
234
235 int PlaybackEngine::brender_available(long position)
236 {
237         return 0;
238 }
239
240 void PlaybackEngine::init_cursor()
241 {
242 }
243
244 void PlaybackEngine::init_meters()
245 {
246 }
247
248 void PlaybackEngine::stop_cursor()
249 {
250 }
251
252
253 void PlaybackEngine::init_tracking()
254 {
255         if(!command->single_frame()) 
256                 tracking_active = 1;
257         else
258                 tracking_active = 0;
259
260         tracking_position = command->playbackstart;
261         tracking_done->lock("PlaybackEngine::init_tracking");
262         init_cursor();
263         init_meters();
264 }
265
266 void PlaybackEngine::stop_tracking()
267 {
268         tracking_active = 0;
269         stop_cursor();
270         tracking_done->unlock();
271 }
272
273 void PlaybackEngine::update_tracking(double position)
274 {
275         tracking_lock->lock("PlaybackEngine::update_tracking");
276
277         tracking_position = position;
278
279 // Signal that the timer is accurate.
280         if(tracking_active) tracking_active = 2;
281         tracking_timer.update();
282         tracking_lock->unlock();
283 }
284
285 double PlaybackEngine::get_tracking_position()
286 {
287         double result = 0;
288
289         tracking_lock->lock("PlaybackEngine::get_tracking_position");
290
291
292 // Adjust for elapsed time since last update_tracking.
293 // But tracking timer isn't accurate until the first update_tracking
294 // so wait.
295         if(tracking_active == 2)
296         {
297 //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));
298
299
300 // Don't interpolate when every frame is played.
301                 if(command->get_edl()->session->video_every_frame &&
302                         render_engine &&
303                         render_engine->do_video)
304                 {
305                         result = tracking_position;
306                 }
307                 else
308 // Interpolate
309                 {
310                         double loop_start = command->get_edl()->local_session->loop_start;
311                         double loop_end = command->get_edl()->local_session->loop_end;
312                         double loop_size = loop_end - loop_start;
313
314                         if(command->get_direction() == PLAY_FORWARD)
315                         {
316 // Interpolate
317                                 result = tracking_position + 
318                                         command->get_speed() * 
319                                         tracking_timer.get_difference() /
320                                         1000.0;
321
322 // Compensate for loop
323 //printf("PlaybackEngine::get_tracking_position 1 %d\n", command->get_edl()->local_session->loop_playback);
324                                 if(command->get_edl()->local_session->loop_playback)
325                                 {
326                                         while(result > loop_end) result -= loop_size;
327                                 }
328                         }
329                         else
330                         {
331 // Interpolate
332                                 result = tracking_position - 
333                                         command->get_speed() * 
334                                         tracking_timer.get_difference() /
335                                         1000.0;
336
337 // Compensate for loop
338                                 if(command->get_edl()->local_session->loop_playback)
339                                 {
340                                         while(result < loop_start) result += loop_size;
341                                 }
342                         }
343
344                 }
345         }
346         else
347                 result = tracking_position;
348
349         tracking_lock->unlock();
350 //printf("PlaybackEngine::get_tracking_position %f %f %d\n", result, tracking_position, tracking_active);
351
352 // Adjust for loop
353
354         return result;
355 }
356
357 void PlaybackEngine::update_transport(int command, int paused)
358 {
359 //      mwindow->gui->lock_window();
360 //      mwindow->gui->mbuttons->transport->update_gui_state(command, paused);
361 //      mwindow->gui->unlock_window();
362 }
363
364 void PlaybackEngine::run()
365 {
366         start_lock->unlock();
367
368         do
369         {
370 // Wait for current command to finish
371                 que->output_lock->lock("PlaybackEngine::run");
372
373                 wait_render_engine();
374
375
376 // Read the new command
377                 que->input_lock->lock("PlaybackEngine::run");
378                 if(done) return;
379
380                 command->copy_from(&que->command);
381                 que->command.reset();
382                 que->input_lock->unlock();
383
384 //printf("PlaybackEngine::run 1 %d\n", command->command);
385
386
387                 switch(command->command)
388                 {
389 // Parameter change only
390                         case COMMAND_NONE:
391 //                              command->command = last_command;
392                                 perform_change();
393                                 break;
394
395                         case PAUSE:
396                                 init_cursor();
397                                 pause_lock->lock("PlaybackEngine::run");
398                                 stop_cursor();
399                                 break;
400
401                         case STOP:
402 // No changing
403                                 break;
404
405                         case CURRENT_FRAME:
406                                 last_command = command->command;
407                                 perform_change();
408                                 arm_render_engine();
409 // Dispatch the command
410                                 start_render_engine();
411                                 break;
412
413                         default:
414                                 last_command = command->command;
415                                 is_playing_back = 1;
416                                 if(command->command == SINGLE_FRAME_FWD ||
417                                         command->command == SINGLE_FRAME_REWIND)
418                                 {
419                                         command->playbackstart = get_tracking_position();
420                                 }
421
422                                 perform_change();
423                                 arm_render_engine();
424
425 // Start tracking after arming so the tracking position doesn't change.
426 // The tracking for a single frame command occurs during PAUSE
427                                 init_tracking();
428
429 // Dispatch the command
430                                 start_render_engine();
431                                 break;
432                 }
433
434
435 //printf("PlaybackEngine::run 100\n");
436         }while(!done);
437 }
438
439
440