r885: Don't delete a borrowed frame.
[cinelerra_cv/ct.git] / cinelerra / record.C
blobab917ffdb2db5cfdb353af73f0a85a93636466ef
1 #include "asset.h"
2 #include "assets.h"
3 #include "audiodevice.h"
4 #include "batch.h"
5 #include "channel.h"
6 #include "channeldb.h"
7 #include "channelpicker.h"
8 #include "clip.h"
9 #include "bchash.h"
10 #include "edl.h"
11 #include "edlsession.h"
12 #include "errorbox.h"
13 #include "file.h"
14 #include "filexml.h"
15 #include "filemov.h"
16 #include "filesystem.h"
17 #include "filethread.h"
18 #include "formatcheck.h"
19 #include "indexfile.h"
20 #include "language.h"
21 #include "localsession.h"
22 #include "mainundo.h"
23 #include "mutex.h"
24 #include "mwindow.h"
25 #include "mwindowgui.h"
26 #include "picture.h"
27 #include "playbackengine.h"
28 #include "preferences.h"
29 #include "quicktime.h"
30 #include "record.h"
31 #include "recordaudio.h"
32 #include "recordconfig.h"
33 #include "recordgui.h"
34 #include "recordlabel.h"
35 #include "recordmonitor.h"
36 #include "recordthread.h"
37 #include "recordvideo.h"
38 #include "recordwindow.h"
39 #include "removethread.h"
40 #include "mainsession.h"
41 #include "sighandler.h"
42 #include "testobject.h"
43 #include "theme.h"
44 #include "timebar.h"
45 #include "tracks.h"
46 #include "videoconfig.h"
47 #include "videodevice.h"
49 #include <string.h>
53 RecordMenuItem::RecordMenuItem(MWindow *mwindow)
54  : BC_MenuItem(_("Record..."), "r", 'r')
56         this->mwindow = mwindow;
57         thread = new Record(mwindow, this);
58         current_state = RECORD_NOTHING;
61 RecordMenuItem::~RecordMenuItem()
63         delete thread;
66 int RecordMenuItem::handle_event()
68         if(thread->running()) 
69         {
70                 switch(current_state)
71                 {
72                         case RECORD_INTRO:
73                                 thread->window_lock->lock("RecordMenuItem::handle_event 1");
74                                 if(thread->record_window)
75                                 {
76                                         thread->record_window->lock_window("RecordMenuItem::handle_event 1");
77                                         thread->record_window->raise_window();
78                                         thread->record_window->unlock_window();
79                                 }
80                                 thread->window_lock->unlock();
81                                 break;
82                         
83                         case RECORD_CAPTURING:
84                                 thread->window_lock->lock("RecordMenuItem::handle_event 2");
85                                 if(thread->record_gui)
86                                 {
87                                         thread->record_gui->lock_window("RecordMenuItem::handle_event 2");
88                                         thread->record_gui->raise_window();
89                                         thread->record_gui->unlock_window();
90                                 }
91                                 thread->window_lock->unlock();
92                                 break;
93                 }
94                 return 0;
95         }
97         thread->start();
98         return 1;
110 Record::Record(MWindow *mwindow, RecordMenuItem *menu_item)
111  : Thread()
113         this->mwindow = mwindow;
114         this->menu_item = menu_item;
115         script = 0;
116         capture_state = IS_DONE;
117         adevice = 0;
118         vdevice = 0;
119         file = 0;
120         editing_batch = 0;
121         current_batch = 0;
122 SET_TRACE
123         picture = new PictureConfig(mwindow->defaults);
124 SET_TRACE
125         channeldb = new ChannelDB;
126         master_channel = new Channel;
127         window_lock = new Mutex("Record::window_lock");
130 Record::~Record()
132         delete picture;
133         delete channeldb;
134         delete master_channel;
135         delete window_lock;
139 int Record::load_defaults()
141         char string[BCTEXTLEN];
142         BC_Hash *defaults = mwindow->defaults;
144 // Load file format
145 //      default_asset->load_defaults(defaults, 
146 //              "RECORD_", 
147 //              1,
148 //              1,
149 //              1,
150 //              1,
151 //              1);
152 // This reads back everything that was saved in save_defaults.
153         default_asset->copy_from(mwindow->edl->session->recording_format, 0);
154         default_asset->channels = mwindow->edl->session->aconfig_in->channels;
155         default_asset->sample_rate = mwindow->edl->session->aconfig_in->in_samplerate;
156         default_asset->frame_rate = mwindow->edl->session->vconfig_in->in_framerate;
157         default_asset->width = mwindow->edl->session->vconfig_in->w;
158         default_asset->height = mwindow->edl->session->vconfig_in->h;
159         default_asset->layers = 1;
163 // Fix encoding parameters depending on driver.
164 // These are locked by a specific driver.
165         if(mwindow->edl->session->vconfig_in->driver == CAPTURE_LML ||
166                 mwindow->edl->session->vconfig_in->driver == CAPTURE_BUZ ||
167                 mwindow->edl->session->vconfig_in->driver == VIDEO4LINUX2JPEG)
168                 strncpy(default_asset->vcodec, QUICKTIME_MJPA, 4);
169         else
170         if(mwindow->edl->session->vconfig_in->driver == CAPTURE_FIREWIRE ||
171                 mwindow->edl->session->vconfig_in->driver == CAPTURE_IEC61883)
172         {
173                 strncpy(default_asset->vcodec, QUICKTIME_DVSD, 4);
174         }
179 // Load batches
180         int total_batches = defaults->get("TOTAL_BATCHES", 1);
181         if(total_batches < 1) total_batches = 1;
182         for(int i = 0; i < total_batches; i++)
183         {
184                 Batch *batch = new_batch();
185                 Asset *asset = batch->assets.values[0];
187                 sprintf(string, "RECORD_PATH_%d", i);
188                 defaults->get(string, asset->path);
189                 sprintf(string, "RECORD_CHANNEL_%d", i);
190                 batch->channel = defaults->get(string, batch->channel);
191                 sprintf(string, "RECORD_STARTTYPE_%d", i);
192                 batch->start_type = defaults->get(string, batch->start_type);
193                 sprintf(string, "RECORD_STARTDAY_%d", i);
194                 batch->start_day = defaults->get(string, batch->start_day);
195                 sprintf(string, "RECORD_STARTTIME_%d", i);
196                 batch->start_time = defaults->get(string, batch->start_time);
197                 sprintf(string, "RECORD_DURATION_%d", i);
198                 batch->duration = defaults->get(string, batch->duration);
199                 sprintf(string, "RECORD_MODE_%d", i);
200                 batch->record_mode = defaults->get(string, batch->record_mode);
201                 sprintf(string, "BATCH_ENABLED_%d", i);
202                 batch->enabled = defaults->get(string, batch->enabled);
203         }
206         load_mode = defaults->get("RECORD_LOADMODE", LOAD_PASTE);
208         monitor_audio = defaults->get("RECORD_MONITOR_AUDIO", 1);
209         monitor_video = defaults->get("RECORD_MONITOR_VIDEO", 1);
210         video_window_open = defaults->get("RECORD_MONITOR_OPEN", 1);
211         video_x = defaults->get("RECORD_VIDEO_X", 0);
212         video_y = defaults->get("RECORD_VIDEO_Y", 0);
213         video_zoom = defaults->get("RECORD_VIDEO_Z", (float)1);
215 SET_TRACE
216         picture->load_defaults();
217 SET_TRACE
219         reverse_interlace = defaults->get("REVERSE_INTERLACE", 0);
220         for(int i = 0; i < MAXCHANNELS; i++) 
221         {
222                 sprintf(string, "RECORD_DCOFFSET_%d", i);
223                 dc_offset[i] = defaults->get(string, 0);
224         }
225         fill_frames = defaults->get("FILL_DROPPED_FRAMES", 0);
226         return 0;
229 int Record::save_defaults()
231         char string[BCTEXTLEN];
232         BC_Hash *defaults = mwindow->defaults;
233         editing_batch = 0;
235 // Save default asset path but not the format because that's
236 // overridden by the driver.
237 // The format is saved in preferences.
238         if(batches.total)
239                 strcpy(default_asset->path, batches.values[0]->assets.values[0]->path);
240         default_asset->save_defaults(defaults,
241                 "RECORD_",
242                 0,
243                 0,
244                 0,
245                 0,
246                 0);
248 //      default_asset->save_defaults(defaults,
249 //              "RECORD_",
250 //              1,
251 //              !fixed_compression,
252 //              1,
253 //              1,
254 //              1);
256 //      defaults->update("RECORD_CHANNELS", default_asset->channels);
263         defaults->update("TOTAL_BATCHES", batches.total);
264         for(int i = 0; i < batches.total; i++)
265         {
266                 Batch *batch = batches.values[i];
267                 Asset *asset = batch->assets.values[0];
269                 sprintf(string, "RECORD_PATH_%d", i);
270                 defaults->update(string, asset->path);
271                 sprintf(string, "RECORD_CHANNEL_%d", i);
272                 defaults->update(string, batch->channel);
273                 sprintf(string, "RECORD_STARTTYPE_%d", i);
274                 defaults->update(string, batch->start_type);
275                 sprintf(string, "RECORD_STARTDAY_%d", i);
276                 defaults->update(string, batch->start_day);
277                 sprintf(string, "RECORD_STARTTIME_%d", i);
278                 defaults->update(string, batch->start_time);
279                 sprintf(string, "RECORD_DURATION_%d", i);
280                 defaults->update(string, batch->duration);
281                 sprintf(string, "RECORD_MODE_%d", i);
282                 defaults->update(string, batch->record_mode);
283                 sprintf(string, "BATCH_ENABLED_%d", i);
284                 defaults->update(string, batch->enabled);
285         }
288         defaults->update("RECORD_LOADMODE", load_mode);
289         defaults->update("RECORD_MONITOR_AUDIO", monitor_audio);
290         defaults->update("RECORD_MONITOR_VIDEO", monitor_video);
291         defaults->update("RECORD_MONITOR_OPEN", video_window_open);
292         defaults->update("RECORD_VIDEO_X", video_x);
293         defaults->update("RECORD_VIDEO_Y", video_y);
294         defaults->update("RECORD_VIDEO_Z", video_zoom);
295         
296 SET_TRACE
297         picture->save_defaults();
298 SET_TRACE
299         defaults->update("REVERSE_INTERLACE", reverse_interlace);
300         for(int i = 0; i < MAXCHANNELS; i++)
301         {
302                 sprintf(string, "RECORD_DCOFFSET_%d", i);
303                 defaults->update(string, dc_offset[i]);
304         }
305         defaults->update("FILL_DROPPED_FRAMES", fill_frames);
307         return 0;
310 void Record::configure_batches()
312         strcpy(batches.values[0]->assets.values[0]->path, default_asset->path);
313         for(int i = 0; i < batches.total; i++)
314         {
315                 Batch *batch = batches.values[i];
316 // Update format
317                 batch->get_current_asset()->copy_format(default_asset);
319 // Update news
320                 batch->calculate_news();
321         }
324 void Record::source_to_text(char *string, Batch *batch)
326 // Update source
327         strcpy(string, "Record::source_to_text: not implemented");
328         switch(mwindow->edl->session->vconfig_in->driver)
329         {
330                 case VIDEO4LINUX:
331                 case VIDEO4LINUX2:
332                 case CAPTURE_BUZ:
333                 case VIDEO4LINUX2JPEG:
334                         if(batch->channel < 0 || batch->channel >= channeldb->size())
335                                 sprintf(string, _("None"));
336                         else
337                                 sprintf(string, channeldb->get(batch->channel)->title);
338                         break;
339         }
343 void Record::run()
345         int result = 0, format_error = 0;
346         int64_t start, end;
347         record_gui = 0;
349 // Default asset forms the first path in the batch capture
350 // and the file format for all operations.
351         default_asset = new Asset;
352         prompt_cancel = 0;
354 // Determine information about the device.
355         VideoDevice::load_channeldb(channeldb, mwindow->edl->session->vconfig_in);
356         fixed_compression = VideoDevice::is_compressed(
357                 mwindow->edl->session->vconfig_in->driver,
358                 0,
359                 1);
360         load_defaults();
362         if(fixed_compression)
363         {
364                 VideoDevice device;
365                 device.fix_asset(default_asset, 
366                         mwindow->edl->session->vconfig_in->driver);
367         }
370         menu_item->current_state = RECORD_INTRO;
372 // // Get information about the file format
373 //      do
374 //      {
375 //              int x = mwindow->gui->get_root_w(0, 1) / 2 - RECORD_WINDOW_WIDTH / 2;
376 //              int y = mwindow->gui->get_root_h(1) / 2 - RECORD_WINDOW_HEIGHT / 2;
378 //              window_lock->lock("Record::run 1");
379 //              record_window = new RecordWindow(mwindow, this, x, y);
380 //              record_window->create_objects();
381 //              window_lock->unlock();
382 // 
383 // 
384 //              result = record_window->run_window();
385 //              window_lock->lock("Record::run 2");
386 //              delete record_window;
387 //              record_window = 0;
388 //              window_lock->unlock();
389 // 
390 // 
391 // 
392 //              if(!result)
393 //              {
394 //                      FormatCheck check_format(default_asset);
395 //                      format_error = check_format.check_format();
396 //              }
397 //      }while(format_error && !result);
399         default_asset->channels = mwindow->edl->session->aconfig_in->channels;
400         VideoDevice::save_channeldb(channeldb, mwindow->edl->session->vconfig_in);
401         save_defaults();
402         mwindow->save_defaults();
404         configure_batches();
405         current_batch = 0;
406         editing_batch = 0;
408 // Run recordgui
409         if(!result)
410         {
411                 edl = new EDL;
412                 edl->create_objects();
413                 edl->session->output_w = default_asset->width;
414                 edl->session->output_h = default_asset->height;
415                 edl->session->aspect_w = mwindow->edl->session->aspect_w;
416                 edl->session->aspect_h = mwindow->edl->session->aspect_h;
418 SET_TRACE
419                 window_lock->lock("Record::run 3");
420 SET_TRACE
421                 record_gui = new RecordGUI(mwindow, this);
422                 record_gui->load_defaults();
423                 record_gui->create_objects();
425 SET_TRACE
426                 record_monitor = new RecordMonitor(mwindow, this);
427 SET_TRACE
428                 record_monitor->create_objects();
429 SET_TRACE
430                 record_gui->update_batch_sources();
432 SET_TRACE
433                 menu_item->current_state = RECORD_CAPTURING;
434                 record_engine = new RecordThread(mwindow, this);
435                 record_engine->create_objects();
436                 monitor_engine = new RecordThread(mwindow, this);
437                 monitor_engine->create_objects();
439 SET_TRACE
441                 record_gui->show_window();
442                 record_gui->flush();
443                 if(video_window_open)
444                 {
445                         record_monitor->window->show_window();
446                         record_monitor->window->raise_window();
447                         record_monitor->window->flush();
448                 }
450 SET_TRACE
451                 start_monitor();
453 SET_TRACE
454                 window_lock->unlock();
456                 result = record_gui->run_window();
457 // Must unlock to stop operation
458                 record_gui->unlock_window();
460 // Force monitor to quit without resuming
461                 if(monitor_engine->record_video) 
462                         monitor_engine->record_video->batch_done = 1;
463                 else if (monitor_engine->record_audio)
464                         monitor_engine->record_audio->batch_done = 1;
466 SET_TRACE
467 //              stop_operation(0);
468 // Need to stop everything this time
469                 monitor_engine->stop_recording(0);
470 SET_TRACE
471                 record_engine->stop_recording(0);
472 SET_TRACE
474                 close_output_file();
475 SET_TRACE
477                 window_lock->lock("Record::run 4");
479 SET_TRACE
480                 delete record_monitor;
481                 record_monitor = 0;
482 SET_TRACE
485                 delete record_engine;
486                 record_engine = 0;
487 SET_TRACE
489                 delete monitor_engine;
490                 monitor_engine = 0;
492 SET_TRACE
493                 record_gui->save_defaults();
495 SET_TRACE
496                 delete record_gui;
497                 record_gui = 0;
498                 window_lock->unlock();
500 SET_TRACE
501                 delete edl;
503 SET_TRACE
504         }
506         menu_item->current_state = RECORD_NOTHING;
508 // Save everything again
509         save_defaults();
515 // Paste into EDL
516         if(!result && load_mode != LOAD_NOTHING)
517         {
518                 mwindow->gui->lock_window("Record::run");
519                 ArrayList<EDL*> new_edls;
522 // Paste assets
523                 for(int i = 0; i < batches.total; i++)
524                 {
525                         Batch *batch = batches.values[i];
526                         Asset *asset = batch->get_current_asset();
528                         if(batch->recorded)
529                         {
530                                 for(int j = 0; j < batch->assets.total; j++)
531                                 {
532                                         Asset *new_asset = batch->assets.values[j];
533                                         EDL *new_edl = new EDL;
534                                         mwindow->remove_asset_from_caches(new_asset);
535                                         new_edl->create_objects();
536                                         new_edl->copy_session(mwindow->edl);
537                                         mwindow->asset_to_edl(new_edl, 
538                                                 new_asset, 
539                                                 batch->labels);
540                                         new_edls.append(new_edl);
541                                 }
542                         }
543                 }
545                 if(new_edls.total)
546                 {
548 // For pasting, clear the active region
549                         if(load_mode == LOAD_PASTE)
550                         {
551                                 mwindow->clear(0);
552                         }
554                         mwindow->paste_edls(&new_edls, 
555                                 load_mode,
556                                 0,
557                                 -1,
558                                 mwindow->edl->session->labels_follow_edits,
559                                 mwindow->edl->session->plugins_follow_edits);
560 //printf("Record::run 7\n");
562                         new_edls.remove_all_objects();
563 //printf("Record::run 8\n");
565                         mwindow->save_backup();
566                         mwindow->undo->update_undo(_("record"), LOAD_ALL);
567                         mwindow->restart_brender();
568                         mwindow->update_plugin_guis();
569                         mwindow->gui->update(1, 
570                                 2,
571                                 1,
572                                 1,
573                                 1,
574                                 1,
575                                 0);
576                         mwindow->sync_parameters(CHANGE_ALL);
577                 }
578                 mwindow->gui->unlock_window();
579         }
581 // Delete everything
582         script = 0;
583         batches.remove_all_objects();
584         Garbage::delete_object(default_asset);
587 void Record::activate_batch(int number, int stop_operation)
589         if(number != current_batch)
590         {
591                 if(stop_operation) this->stop_operation(1);
592                 close_output_file();
593                 get_current_batch()->calculate_news();
595                 current_batch = number;
596                 record_gui->update_batches();
597                 record_gui->update_position(current_display_position());
598                 record_gui->update_batch_tools();
599         }
602 void Record::delete_batch()
604 // Abort if one batch left
605         if(batches.total > 1)
606         {
607 // Stop operation if active batch
608                 if(current_batch == editing_batch)
609                 {
610                         if(current_batch < batches.total - 1)
611                                 activate_batch(current_batch + 1, 1);
612                         else
613                                 activate_batch(current_batch - 1, 1);
615                         delete batches.values[editing_batch];
616                         batches.remove_number(editing_batch);
617                         editing_batch = current_batch;
618                 }
619                 else
620                 {
621                         if(current_batch > editing_batch) current_batch--;
622                         delete batches.values[editing_batch];
623                         batches.remove_number(editing_batch);
624                         if(editing_batch >= batches.total) editing_batch--;
625                 }
626                 record_gui->update_batch_tools();
627         }
630 void Record::change_editing_batch(int number)
632         this->editing_batch = number;
633         record_gui->update_batch_tools();
636 Batch* Record::new_batch()
638         Batch *result = new Batch(mwindow, this);
639 //printf("Record::new_batch 1\n");
640         result->create_objects();
641         batches.append(result);
642         result->get_current_asset()->copy_format(default_asset);
643         
644 //printf("Record::new_batch 1\n");
646         result->create_default_path();
647         result->calculate_news();
648         if(get_editing_batch()) result->copy_from(get_editing_batch());
649         editing_batch = batches.total - 1;
650 //printf("Record::new_batch 1\n");
651 // Update GUI if created yet
652         if(record_gui) record_gui->update_batch_tools();
653 //printf("Record::new_batch 2\n");
654         return result;
657 int Record::delete_output_file()
659         FILE *test;
663 // Delete old file
664         if(!file)
665         {
666                 Batch *batch = get_current_batch();
667                 if(batch && (test = fopen(batch->get_current_asset()->path, "r")))
668                 {
669                         fclose(test);
671                         record_gui->lock_window("Record::delete_output_file");
673 // Update GUI
674                         sprintf(batch->news, _("Deleting"));
675                         record_gui->update_batches();
677 // Remove it from disk
678                         mwindow->remove_asset_from_caches(batch->get_current_asset());
679                         mwindow->remove_thread->remove_file(batch->get_current_asset()->path);
680 //                      remove(batch->get_current_asset()->path);
682 // Update GUI
683                         sprintf(batch->news, _("OK"));
684                         record_gui->update_batches();
686                         record_gui->unlock_window();
687                 }
688         }
689         return 0;
692 int Record::open_output_file()
694         int result = 0;
695 // Create initial file for the batch
696         if(!file)
697         {
698                 Batch *batch = get_current_batch();
699                 delete_output_file();
701                 file = new File;
702                 result = file->open_file(mwindow->preferences, 
703                         batch->get_current_asset(), 
704                         0, 
705                         1, 
706                         default_asset->sample_rate, 
707                         default_asset->frame_rate);
709                 if(result)
710                 {
711                         delete file;
712                         file = 0;
713                 }
714                 else
715                 {
716                         mwindow->sighandler->push_file(file);
717                         IndexFile::delete_index(mwindow->preferences, 
718                                 batch->get_current_asset());
719                         file->set_processors(mwindow->preferences->real_processors);
720                         batch->calculate_news();
721                         record_gui->lock_window("Record::open_output_file");
722                         record_gui->update_batches();
723                         record_gui->unlock_window();
724                 }
725         }
726         return result;
729 int Record::init_next_file()
731         Batch *batch = get_current_batch();
732         Asset *asset;
734         if(file)
735         {
736                 mwindow->sighandler->pull_file(file);
737                 file->close_file();
738                 delete file;
739                 file = 0;
740         }
742         batch->current_asset++;
743         batch->assets.append(asset = new Asset);
744         *asset = *default_asset;
745         sprintf(asset->path, "%s%03d", asset->path, batch->current_asset);
746         int result = open_output_file();
747         return result;
750 // Rewind file at the end of a loop.
751 // This is called by RecordThread.
752 void Record::rewind_file()
754         if(file)
755         {
756                 if(default_asset->audio_data) 
757                         file->set_audio_position(0, default_asset->frame_rate);
758                 if(default_asset->video_data)
759                         file->set_video_position(0, default_asset->frame_rate);
760         }
762         get_current_batch()->current_sample = 0;
763         get_current_batch()->current_frame = 0;
764         record_gui->lock_window("Record::rewind_file");
765         record_gui->update_position(0);
766         record_gui->unlock_window();
769 void Record::start_over()
771         stop_operation(1);
773         Batch *batch = get_current_batch();
774         if(file)
775         {
776                 mwindow->sighandler->pull_file(file);
777                 file->close_file();
778                 delete file;
779                 file = 0;
780         }
782         get_current_batch()->start_over();
784         record_gui->lock_window("Record::start_over");
785         record_gui->update_position(0);
786         record_gui->update_batches();
787         record_gui->unlock_window();
790 void Record::close_output_file()
792 // Can't close until recordmonitor is done
793 //printf("Record::close_output_file 1\n");
794         if(file)
795         {
796                 mwindow->sighandler->pull_file(file);
797                 file->close_file();
798                 delete file;
799                 file = 0;
800         }
801 //printf("Record::close_output_file 2\n");
804 void Record::toggle_label()
806         get_current_batch()->toggle_label(current_display_position());
807         record_gui->update_labels(current_display_position());
810 void Record::get_audio_write_length(int &buffer_size, 
811         int &fragment_size)
813         fragment_size = 1;
814         while(fragment_size < default_asset->sample_rate / mwindow->edl->session->record_speed) 
815                 fragment_size *= 2;
816         fragment_size /= 2;
817         CLAMP(fragment_size, 1024, 32768);
819         for(buffer_size = fragment_size; 
820                 buffer_size < mwindow->edl->session->record_write_length; 
821                 buffer_size += fragment_size)
822                 ;
825 Batch* Record::get_current_batch()
827         if(batches.total)
828                 return batches.values[current_batch];
829         else
830                 return 0;
833 int Record::get_next_batch()
835         int i = current_batch;
836         while(i < batches.total - 1)
837         {
838                 i++;
839                 if(batches.values[i]->enabled) return i;
840         }
841         return -1;
845 Batch* Record::get_editing_batch()
847 //printf("Record::get_editing_batch %d %d\n", batches.total, editing_batch);
849         if(batches.total)
850                 return batches.values[editing_batch];
851         else
852                 return 0;
855 char* Record::current_mode()
857         return Batch::mode_to_text(get_current_batch()->record_mode);
860 int64_t Record::batch_video_offset()
862         return (int64_t)((double)get_current_batch()->file_offset * 
863                 default_asset->frame_rate + 0.5);
866 int64_t Record::current_audio_position()
868         if(file)
869         {
870                 return (int64_t)(file->get_audio_position(default_asset->sample_rate) + 
871                         get_current_batch()->file_offset + 0.5);
872         }
873         return 0;
876 int64_t Record::current_video_position()
878         if(file)
879         {
880                 return file->get_video_position(default_asset->frame_rate) + 
881                         (int64_t)((double)get_current_batch()->file_offset / 
882                                 default_asset->sample_rate * 
883                                 default_asset->frame_rate + 
884                                 0.5);
885         }
886         return 0;
889 double Record::current_display_position()
891 //printf("Record::current_display_position %d %d\n", get_current_batch()->current_sample, get_current_batch()->file_offset);
894         if(default_asset->video_data)
895                 return (double)get_current_batch()->current_frame / 
896                         default_asset->frame_rate + 
897                         get_current_batch()->file_offset;
898         else
899                 return (double)get_current_batch()->current_sample / 
900                         default_asset->sample_rate + 
901                         get_current_batch()->file_offset;
902         return 0;
905 char* Record::current_source()
907         return get_current_batch()->get_source_text();
910 char* Record::current_news()
912         return batches.values[current_batch]->news;
915 Asset* Record::current_asset()
917         return batches.values[current_batch]->get_current_asset();
920 double* Record::current_start()
922         return &batches.values[current_batch]->start_time;
925 int Record::get_current_channel()
927         return get_current_batch()->channel;
930 int Record::get_editing_channel()
932         return get_editing_batch()->channel;
935 Channel* Record::get_current_channel_struct()
937         int channel = get_current_channel();
938         if(channel >= 0 && channel < channeldb->size())
939         {
940                 return channeldb->get(channel);
941         }
942         return 0;
945 double* Record::current_duration()
947         return &batches.values[current_batch]->duration;
950 int64_t Record::current_duration_samples()
952         return (int64_t)((float)batches.values[current_batch]->duration * default_asset->sample_rate + 0.5);
955 int64_t Record::current_duration_frames()
957         return (int64_t)((float)batches.values[current_batch]->duration * default_asset->frame_rate + 0.5);
960 int* Record::current_offset_type()
962         return &batches.values[current_batch]->start_type;
965 ArrayList<Channel*>* Record::get_video_inputs() 
967         if(default_asset->video_data && vdevice) 
968                 return vdevice->get_inputs();
969         else
970                 return 0;
973 int64_t Record::sync_position()
975         switch(capture_state)
976         {
977                 case IS_DONE:
978                         return -1;
979                         break;    
980                 case IS_MONITORING:
981                         return monitor_engine->sync_position();
982                         break;    
983                 case IS_DUPLEXING: 
984                 case IS_RECORDING: 
985                         return record_engine->sync_position();
986                         break;    
987         }
988         return 0;
992 int Record::open_input_devices(int duplex, int context)
994         int audio_opened = 0;
995         int video_opened = 0;
996         AudioInConfig *aconfig_in = mwindow->edl->session->aconfig_in;
999 // Create devices
1000         if(default_asset->audio_data && context != CONTEXT_SINGLEFRAME)
1001                 adevice = new AudioDevice(mwindow);
1002         else
1003                 adevice = 0;
1005         if(default_asset->video_data)
1006                 vdevice = new VideoDevice(mwindow);
1007         else
1008                 vdevice = 0;
1010 // Initialize sharing
1011         if(adevice && vdevice)
1012         {
1013                 vdevice->set_adevice(adevice);
1014                 adevice->set_vdevice(vdevice);
1015         }
1017 // Configure audio
1018         if(adevice)
1019         {
1020                 adevice->set_software_positioning(mwindow->edl->session->record_software_position);
1022 // Initialize full duplex
1023 // Duplex is only needed if the timeline and the recording have audio
1024                 if(duplex && mwindow->edl->tracks->playable_audio_tracks())
1025                 {
1026 // Case 1: duplex device is identical to input device
1027 //                      if(AudioInConfig::is_duplex(aconfig_in, mwindow->edl->session->aconfig_duplex))
1028 //                      {
1029 //                              adevice->open_duplex(mwindow->edl->session->aconfig_duplex,
1030 //                                                      default_asset->sample_rate,
1031 //                                                      get_in_length(),
1032 //                                                      mwindow->edl->session->real_time_playback);
1033 //                              audio_opened = 1;
1034 //                      }
1035 //                      else
1036 // Case 2: two separate devices
1037                         {
1038                                 adevice->open_output(mwindow->edl->session->aconfig_duplex,
1039                                                 default_asset->sample_rate,
1040                                                 mwindow->edl->session->playback_buffer,
1041                                                 mwindow->edl->session->audio_channels,
1042                                                 mwindow->edl->session->real_time_playback);
1043                         }
1044                 }
1046                 if(!audio_opened)
1047                 {
1048                         adevice->open_input(mwindow->edl->session->aconfig_in, 
1049                                 mwindow->edl->session->vconfig_in, 
1050                                 default_asset->sample_rate, 
1051                                 get_in_length(),
1052                                 default_asset->channels,
1053                                 mwindow->edl->session->real_time_record);
1054                         adevice->start_recording();
1055                 }
1056         }
1059 // Initialize video
1060         if(vdevice)
1061         {
1062                 vdevice->set_quality(default_asset->jpeg_quality);
1063                 vdevice->open_input(mwindow->edl->session->vconfig_in, 
1064                         video_x, 
1065                         video_y, 
1066                         video_zoom,
1067                         default_asset->frame_rate);
1069 // Get configuration parameters from device probe
1070                 color_model = vdevice->get_best_colormodel(default_asset);
1071                 master_channel->copy_usage(vdevice->channel);
1072                 picture->copy_usage(vdevice->picture);
1073                 vdevice->set_field_order(reverse_interlace);
1075 // Set the device configuration
1076                 set_channel(get_current_channel());
1077         }
1079         return 0;
1082 int Record::close_input_devices(int is_monitor)
1084         if(is_monitor && capture_state != IS_MONITORING) return 0;
1086         if(vdevice)
1087         {
1088                 vdevice->close_all();
1089                 delete vdevice;
1090                 vdevice = 0;
1091         }
1093         if(adevice)
1094         {
1095                 adevice->close_all();
1096                 delete adevice;
1097                 adevice = 0;
1098         }
1100         return 0;
1103 int Record::start_recording(int duplex, int context)
1105         if(capture_state != IS_RECORDING)
1106         {
1107                 pause_monitor();
1109 // Want the devices closed during file deletion to avoid buffer overflow
1110                 delete_output_file();
1112 // These two contexts need to open the device here to allow full duplex.  
1113 // Batch context opens them in RecordThread::run
1114                 if(context == CONTEXT_INTERACTIVE ||
1115                         context == CONTEXT_SINGLEFRAME)
1116                         open_input_devices(duplex, context);
1118                 prompt_cancel = 1;
1120 // start the duplex engine if necessary
1121 // OSS < 3.9 crashes if recording starts before playback
1122 // OSS >= 3.9 crashes if playback starts before recording
1123                 if(duplex)
1124                 {
1125                         capture_state = IS_DUPLEXING;
1126                 }
1127                 else
1128                         capture_state = IS_RECORDING;
1130 // Toggle once to cue the user that we're not dead.
1131                 if(context == CONTEXT_BATCH)
1132                 {
1133                         record_gui->lock_window("Record::start_recording");
1134                         record_gui->flash_batch();
1135                         record_gui->unlock_window();
1136                 }
1137                 record_engine->start_recording(0, context);
1138         }
1139         return 0;
1142 int Record::start_monitor()
1144         monitor_timer.update();
1145         open_input_devices(0, CONTEXT_INTERACTIVE);
1146         monitor_engine->start_recording(1, CONTEXT_INTERACTIVE);
1147         capture_state = IS_MONITORING;
1148         return 0;
1151 int Record::stop_monitor()
1153         monitor_engine->stop_recording(0);
1154         return 0;
1157 int Record::pause_monitor()
1159         if(capture_state == IS_MONITORING)
1160         {
1161                 monitor_engine->pause_recording();
1162         }
1163         return 0;
1166 int Record::resume_monitor()
1168         if(capture_state != IS_MONITORING)
1169         {
1170                 capture_state = IS_MONITORING;
1171                 monitor_timer.update();
1172                 open_input_devices(0, CONTEXT_INTERACTIVE);
1173                 monitor_engine->resume_recording();
1174         }
1175         return 0;
1178 int Record::stop_duplex()
1180         return 0;
1183 int Record::stop_operation(int resume_monitor)
1185         switch(capture_state)
1186         {
1187                 case IS_MONITORING:
1188                         if(!resume_monitor) monitor_engine->stop_recording(0);
1189                         break;
1190                 case IS_RECORDING:
1191                         record_engine->stop_recording(resume_monitor);
1192                         break;
1193                 case IS_DUPLEXING:
1194                         break;
1195                 case IS_PREVIEWING:
1196                         break;
1197         }
1198         return 0;
1202 // Remember to change meters if you change this.
1203 // Return the size of the fragments to read from the audio device.
1204 int Record::get_in_length()
1206         int64_t fragment_size = 1;
1207         while(fragment_size < default_asset->sample_rate / 
1208                 mwindow->edl->session->record_speed)
1209                 fragment_size *= 2;
1210         fragment_size /= 2;
1211         fragment_size = MAX(fragment_size, 512);
1212         return fragment_size;
1215 int Record::set_video_picture()
1217         if(default_asset->video_data && vdevice)
1218                 vdevice->set_picture(picture);
1219         return 0;
1222 void Record::set_translation(int x, int y)
1224         video_x = x;
1225         video_y = y;
1226         if(default_asset->video_data && vdevice)
1227                 vdevice->set_translation(video_x, video_y);
1231 int Record::set_channel(int channel)
1233         if(channel >= 0 && channel < channeldb->size())
1234         {
1235                 char string[BCTEXTLEN];
1236                 get_editing_batch()->channel = channel;
1237                 source_to_text(string, get_editing_batch());
1240                 record_gui->lock_window("Record::set_channel");
1241                 record_gui->batch_source->update(string);
1242                 record_monitor->window->channel_picker->channel_text->update(string);
1243                 record_gui->update_batches();
1244                 record_gui->unlock_window();
1247                 if(vdevice)
1248                 {
1249                         vdevice->set_channel(channeldb->get(channel));
1250                         set_video_picture();
1251                 }
1252         }
1253         return 0;
1256 // Change to a channel not in the db for editing
1257 void Record::set_channel(Channel *channel)
1259         if(vdevice) vdevice->set_channel(channel);
1262 int Record::has_signal()
1264         if(vdevice) return vdevice->has_signal();
1265         return 0;
1268 void Record::get_current_time(double &seconds, int &day)
1270         time_t result = time(0) + 1;
1271         struct tm *struct_tm = localtime(&result);
1272         day = struct_tm->tm_wday;
1273         seconds = struct_tm->tm_hour * 3600 + struct_tm->tm_min * 60 + struct_tm->tm_sec;
1287 int Record::get_time_format()
1289         return mwindow->edl->session->time_format;
1292 float Record::get_frame_rate()
1294         return 0.0;
1295 //      return mwindow->session->frame_rate;
1298 int Record::set_loop_duration(int64_t value)
1300         loop_duration = value; 
1301         return 0;
1304 int Record::get_vu_format() { return mwindow->edl->session->meter_format; }
1305 float Record::get_min_db() { return mwindow->edl->session->min_meter_db; }
1307 int Record::get_rec_mode() { return record_mode; }
1308 int Record::set_rec_mode(int value) { record_mode = value; }
1310 int Record::get_video_buffersize() { return mwindow->edl->session->video_write_length; }
1311 int Record::get_everyframe() { return mwindow->edl->session->video_every_frame; }
1313 int Record::get_out_length() { return mwindow->edl->session->playback_buffer; }
1314 int Record::get_software_positioning() { return mwindow->edl->session->record_software_position; }
1315 int64_t Record::get_out_buffersize() { return mwindow->edl->session->playback_buffer; }
1316 int64_t Record::get_in_buffersize() { return mwindow->edl->session->record_write_length; }
1317 int Record::get_realtime() { return realtime; }
1318 int Record::get_meter_speed() { return mwindow->edl->session->record_speed; }
1320 int Record::enable_duplex() { return mwindow->edl->session->enable_duplex; }
1321 int64_t Record::get_playback_buffer() { return mwindow->edl->session->playback_buffer; }