3 * Copyright (C) 2009-2013 Adam Williams <broadcast at earthling dot net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "audiodevice.h"
25 #include "awindowgui.h"
29 #include "channeldb.h"
30 #include "channelpicker.h"
32 #include "commercials.h"
33 #include "condition.h"
35 #include "devicedvbinput.h"
36 #include "drivesync.h"
38 #include "edlsession.h"
42 #include "filesystem.h"
43 #include "filethread.h"
44 #include "formatcheck.h"
45 #include "indexfile.h"
48 #include "localsession.h"
51 #include "libzmpeg3.h"
55 #include "mwindowgui.h"
58 #include "playbackengine.h"
59 #include "preferences.h"
61 #include "recordaudio.h"
62 #include "recordconfig.h"
63 #include "recordgui.h"
64 #include "recordlabel.h"
65 #include "recordmonitor.h"
66 #include "recordprefs.inc"
67 #include "recordthread.h"
68 #include "recordvideo.h"
69 #include "removefile.h"
70 #include "mainsession.h"
71 #include "sighandler.h"
72 #include "testobject.h"
76 #include "videoconfig.h"
77 #include "videodevice.h"
80 #include <sys/types.h>
84 RecordMenuItem::RecordMenuItem(MWindow *mwindow)
85 : BC_MenuItem(_("Record..."), "r", 'r')
87 this->mwindow = mwindow;
88 record = new Record(mwindow, this);
91 RecordMenuItem::~RecordMenuItem()
96 int RecordMenuItem::handle_event()
103 Record::Record(MWindow *mwindow, RecordMenuItem *menu_item)
105 record_batches(mwindow)
107 this->mwindow = mwindow;
108 this->menu_item = menu_item;
109 mwindow->gui->record = this;
113 picture = new PictureConfig();
114 channeldb = new ChannelDB;
115 master_channel = new Channel;
116 record_channel = new RecordChannel(this);
121 pause_lock = new Mutex("Record::pause_lock");
122 init_lock = new Condition(0,"Record::init_lock");
128 session_sample_offset = 0;
129 device_sample_offset = 0;
134 drop_overrun_frames = 0;
135 fill_underrun_frames = 0;
137 commercial_check = skimming_active = 0;
138 commercial_start_time = -1;
146 current_frame = written_frames = total_frames = 0;
147 current_sample = written_samples = total_samples = 0;
148 audio_time = video_time = -1.;
149 play_gain = mute_gain = 1.;
151 input_threads_pausing = 0;
152 window_lock = new Mutex("Record::window_lock");
153 timer_lock = new Mutex("Record::timer_lock");
154 file_lock = new Mutex("Record::file_lock");
155 adevice_lock = new Mutex("Record::adevice_lock");
156 vdevice_lock = new Mutex("Record::vdevice_lock");
157 batch_lock = new Mutex("Record::batch_lock");
158 #ifdef HAVE_COMMERCIAL
159 skim_thread = new SkimDbThread();
160 cutads_status = new RecordCutAdsStatus(this);
161 blink_status = new RecordBlinkStatus(this);
167 mwindow->gui->record = 0;
169 #ifdef HAVE_COMMERCIAL
171 delete cutads_status;
178 delete record_channel;
179 delete master_channel;
192 int Record::load_defaults()
195 char string[BCTEXTLEN];
196 BC_Hash *defaults = mwindow->defaults;
197 EDLSession *session = SESSION;
198 default_asset->copy_from(session->recording_format, 0);
199 default_asset->channels = session->aconfig_in->channels;
200 default_asset->sample_rate = session->aconfig_in->in_samplerate;
201 default_asset->frame_rate = session->vconfig_in->in_framerate;
202 default_asset->width = session->vconfig_in->w;
203 default_asset->height = session->vconfig_in->h;
204 default_asset->layers = 1;
205 // Fix encoding parameters depending on driver.
206 // These are locked by a specific driver.
207 const char *vcodec = 0;
208 switch( session->vconfig_in->driver ) {
210 case VIDEO4LINUX2MPEG:
212 case VIDEO4LINUX2JPEG:
213 vcodec = CODEC_TAG_MJPEG;
215 case CAPTURE_FIREWIRE:
216 case CAPTURE_IEC61883:
217 vcodec = CODEC_TAG_DVSD;
221 strcpy(default_asset->vcodec, vcodec);
223 record_batches.load_defaults(channeldb, this);
225 int cur_chan_no = defaults->get("RECORD_CURRENT_CHANNEL", 0);
226 current_channel = channeldb->get(cur_chan_no);
227 load_mode = defaults->get("RECORD_LOADMODE", LOADMODE_PASTE);
228 monitor_audio = defaults->get("RECORD_MONITOR_AUDIO", 1);
229 metering_audio = defaults->get("RECORD_METERING_AUDIO", 1);
230 monitor_video = defaults->get("RECORD_MONITOR_VIDEO", 1);
231 video_window_open = defaults->get("RECORD_MONITOR_OPEN", 1);
232 video_x = defaults->get("RECORD_VIDEO_X", 0);
233 video_y = defaults->get("RECORD_VIDEO_Y", 0);
234 video_zoom = defaults->get("RECORD_VIDEO_Z", (float)1);
235 picture->load_defaults();
236 reverse_interlace = defaults->get("REVERSE_INTERLACE", 0);
237 do_cursor = defaults->get("RECORD_CURSOR", 0);
238 do_big_cursor = defaults->get("RECORD_BIG_CURSOR", 0);
239 for( int i=0; i<MAXCHANNELS; ++i ) {
240 sprintf(string, "RECORD_DCOFFSET_%d", i);
241 dc_offset[i] = defaults->get(string, 0);
243 drop_overrun_frames = defaults->get("DROP_OVERRUN_FRAMES", 0);
244 fill_underrun_frames = defaults->get("FILL_UNDERRUN_FRAMES", 0);
245 commercial_check = defaults->get("COMMERCIAL_CHECK", 0);
249 int Record::save_defaults()
251 char string[BCTEXTLEN];
252 BC_Hash *defaults = mwindow->defaults;
253 set_editing_batch(0);
254 // Save default asset path but not the format because that's
255 // overridden by the driver.
256 // The format is saved in preferences.
257 if( record_batches.total() )
258 strcpy(default_asset->path, record_batches[0]->asset->path);
259 default_asset->save_defaults(defaults, "RECORD_", 0, 0, 0, 0, 0);
261 record_batches.save_defaults(channeldb);
263 int cur_chan_no = channeldb->number_of(current_channel);
264 defaults->update("RECORD_CURRENT_CHANNEL", cur_chan_no);
265 defaults->update("RECORD_LOADMODE", load_mode);
266 defaults->update("RECORD_MONITOR_AUDIO", monitor_audio);
267 defaults->update("RECORD_METERING_AUDIO", metering_audio);
268 defaults->update("RECORD_MONITOR_VIDEO", monitor_video);
269 defaults->update("RECORD_MONITOR_OPEN", video_window_open);
270 defaults->update("RECORD_VIDEO_X", video_x);
271 defaults->update("RECORD_VIDEO_Y", video_y);
272 defaults->update("RECORD_VIDEO_Z", video_zoom);
273 picture->save_defaults();
274 defaults->update("REVERSE_INTERLACE", reverse_interlace);
275 defaults->update("RECORD_CURSOR", do_cursor);
276 defaults->update("RECORD_BIG_CURSOR", do_big_cursor);
277 for( int i=0; i<MAXCHANNELS; ++i ) {
278 sprintf(string, "RECORD_DCOFFSET_%d", i);
279 defaults->update(string, dc_offset[i]);
281 defaults->update("DROP_OVERRUN_FRAMES", drop_overrun_frames);
282 defaults->update("FILL_UNDERRUN_FRAMES", fill_underrun_frames);
283 defaults->update("COMMERCIAL_CHECK", commercial_check);
289 void Record::configure_batches()
291 // printf("Record::configure_batches %d\n",__LINE__);
292 // default_assset->dump();
293 strcpy(record_batches[0]->asset->path, default_asset->path);
294 int total_batches = record_batches.total();
295 for( int i=0; i<total_batches; ++i ) {
296 Batch *batch = record_batches[i];
297 batch->asset->copy_format(default_asset);
305 // Default asset forms the first path in the batch capture
306 // and the file format for all operations.
307 default_asset = new Asset;
309 // Determine information about the device.
310 AudioInConfig *aconfig_in = SESSION->aconfig_in;
311 VideoInConfig *vconfig_in = SESSION->vconfig_in;
312 int driver = vconfig_in->driver;
313 VideoDevice::load_channeldb(channeldb, vconfig_in);
314 fixed_compression = VideoDevice::is_compressed(driver, 0, 1);
316 // Apply a major kludge
317 if( fixed_compression ) {
319 device.fix_asset(default_asset, driver);
321 default_asset->channels = aconfig_in->channels;
322 VideoDevice::save_channeldb(channeldb, vconfig_in);
324 mwindow->save_defaults();
326 set_current_batch(0);
327 set_editing_batch(0);
331 edl->create_objects();
332 edl->session->output_w = default_asset->width;
333 edl->session->output_h = default_asset->height;
334 edl->session->aspect_w = SESSION->aspect_w;
335 edl->session->aspect_h = SESSION->aspect_h;
337 window_lock->lock("Record::run 3");
338 record_gui = new RecordGUI(mwindow, this);
339 record_gui->create_objects();
341 record_monitor = new RecordMonitor(mwindow, this);
342 record_monitor->create_objects();
343 record_gui->update_batch_sources();
345 record_gui->show_window();
348 if( mwindow->gui->remote_control->deactivate() )
349 mwindow->gui->record_remote_handler->activate();
351 if( video_window_open ) {
352 record_monitor->window->show_window();
353 record_monitor->window->raise_window();
354 record_monitor->window->flush();
357 window_lock->unlock();
359 start_input_threads();
363 result = record_gui->run_window();
364 // record gui operates here until window is closed
366 if( record_gui->get_window_lock() ) {
367 record_gui->lock_window();
368 record_gui->unlock_window();
371 // Need to stop everything this time
373 int video_stream = -1;
374 #ifdef HAVE_COMMERCIAL
375 stop_commercial_capture(0);
378 if( default_asset->format == FILE_MPEG ) {
379 Channel *channel = get_current_channel();
381 video_stream = channel->video_stream;
385 edl->Garbage::remove_user();
387 if( mwindow->gui->remote_control->deactivate() )
388 mwindow->gui->cwindow_remote_handler->activate();
390 // Save everything again
394 if( !result && load_mode != LOADMODE_NOTHING ) {
395 mwindow->gui->lock_window("Record::run");
396 ArrayList<EDL*> new_edls;
398 int total_batches = record_batches.total();
399 for( int i=0; i<total_batches; ++i ) {
400 Batch *batch = record_batches[i];
401 Asset *asset = batch->asset;
402 if( batch->recorded ) {
403 EDL *new_edl = new EDL;
404 mwindow->remove_asset_from_caches(asset);
405 new_edl->create_objects();
406 new_edl->copy_session(mwindow->edl);
407 mwindow->asset_to_edl(new_edl, asset, batch->labels);
408 new_edls.append(new_edl);
413 mwindow->undo->update_undo_before();
414 // For pasting, clear the active region
415 if(load_mode == LOADMODE_PASTE)
417 int loadmode = load_mode == LOADMODE_RESOURCESONLY ?
418 LOADMODE_ASSETSONLY : load_mode;
419 mwindow->paste_edls(&new_edls, loadmode, 0, -1,
420 SESSION->labels_follow_edits,
421 SESSION->plugins_follow_edits,
422 SESSION->autos_follow_edits,
424 for( int i=0; i<new_edls.total; ++i )
425 new_edls.get(i)->Garbage::remove_user();
426 new_edls.remove_all();
427 if( video_stream >= 0 ) {
428 mwindow->select_asset(video_stream, -1);
429 mwindow->fit_selection();
431 mwindow->save_backup();
432 mwindow->undo->update_undo_after(_("record"), LOAD_ALL);
433 mwindow->restart_brender();
434 mwindow->update_plugin_guis();
435 mwindow->gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
436 mwindow->sync_parameters(CHANGE_ALL);
438 mwindow->gui->unlock_window();
440 AWindowGUI *agui = mwindow->awindow->gui;
441 agui->async_update_assets();
445 record_batches.clear();
446 default_asset->Garbage::remove_user();
449 void Record::stop(int wait)
453 record_gui->set_done(1);
456 window_lock->lock("Record::stop");
457 delete record_thread; record_thread = 0;
458 delete record_monitor; record_monitor = 0;
459 delete record_gui; record_gui = 0;
460 window_lock->unlock();
463 void Record::activate_batch(int number)
465 if( number != current_batch() ) {
467 set_current_batch(number);
468 record_gui->update_batches();
469 record_gui->update_position(current_display_position());
470 record_gui->update_batch_tools();
475 void Record::delete_index_file(Asset *asset)
477 IndexFile::delete_index_files(mwindow->preferences, asset);
480 void Record::delete_batch()
482 // Abort if one batch left
483 int edit_batch = editing_batch();
484 int total_batches = record_batches.total();
485 if( total_batches > 1 && edit_batch < total_batches ) {
486 int cur_batch = current_batch();
487 Batch *batch = record_batches[edit_batch];
488 record_batches.remove(batch); delete batch;
489 if( cur_batch == edit_batch ) stop_writing();
490 record_gui->update_batches();
491 record_gui->update_batch_tools();
495 void Record::change_editing_batch(int number)
497 set_editing_batch(number);
498 record_gui->update_batch_tools();
501 Batch* Record::new_batch()
503 Batch *batch = new Batch(mwindow, this);
504 //printf("Record::new_batch 1\n");
505 batch->create_objects();
506 record_batches.append(batch);
507 batch->asset->copy_format(default_asset);
509 //printf("Record::new_batch 1\n");
510 batch->create_default_path();
511 Batch *edit_batch = get_editing_batch();
513 batch->copy_from(edit_batch);
514 int total_batches = record_batches.total();
515 set_editing_batch(total_batches - 1);
516 //printf("Record::new_batch 1\n");
517 // Update GUI if created yet
519 record_gui->update_batch_tools();
520 //printf("Record::new_batch 2\n");
524 int Record::current_batch()
526 return record_batches.current_item;
529 int Record::set_current_batch(int i)
531 return record_batches.current_item = i;
534 int Record::editing_batch()
536 return record_batches.editing_item;
539 int Record::set_editing_batch(int i)
541 return record_batches.editing_item = i;
544 int Record::delete_output_file()
546 Batch *batch = get_current_batch();
548 if( !file && batch && !access(batch->asset->path, F_OK) ) {
549 batch->set_notice(_("Deleting"));
550 record_gui->update_batches();
551 Asset *asset = batch->asset;
552 remove_file(asset->path);
553 mwindow->remove_asset_from_caches(asset);
554 delete_index_file(asset);
555 batch->clear_notice();
556 record_gui->update_batches();
561 int Record::open_output_file()
565 Batch *batch = get_current_batch();
566 if( !file && batch ) {
567 delete_output_file();
568 Asset *asset = batch->asset;
569 asset->frame_rate = default_asset->frame_rate;
570 asset->sample_rate = default_asset->sample_rate;
571 asset->width = default_asset->width;
572 asset->height = default_asset->height;
574 if( default_asset->format == FILE_MPEG ) {
575 asset->layers = vdevice ? vdevice->total_video_streams() : 0;
576 asset->channels = adevice ? adevice->total_audio_channels() : 0;
580 result = file->open_file(mwindow->preferences, asset, 0, wr);
582 mwindow->sighandler->push_file(file);
584 file->set_processors(mwindow->preferences->real_processors);
585 record_gui->update_batches();
599 window_lock->lock("Record::start");
600 record_gui->lock_window("Record::start");
601 record_gui->raise_window();
602 record_gui->unlock_window();
603 window_lock->unlock();
611 void Record::start_over()
614 Batch *batch = get_current_batch();
615 if( batch ) batch->start_over();
616 record_gui->update_batches();
619 void Record::stop_operation()
622 stop_input_threads();
625 int Record::cron_active()
627 return !record_thread ? 0 : record_thread->cron_active;
630 void Record::close_output_file()
634 mwindow->sighandler->pull_file(file);
640 record_gui->update_batches();
643 void Record::toggle_label()
645 Batch *batch = get_current_batch();
646 if( batch ) batch->toggle_label(current_display_position());
647 record_gui->update_labels(current_display_position());
650 void Record::clear_labels()
652 Batch *batch = get_current_batch();
653 if( batch ) batch->clear_labels();
654 record_gui->update_labels(0);
657 int Record::get_fragment_samples()
659 const int min_samples = 1024, max_samples = 32768;
660 int samples, low_bit;
661 samples = default_asset->sample_rate / SESSION->record_speed;
662 if( samples < min_samples ) samples = min_samples;
663 else if( samples > max_samples ) samples = max_samples;
664 // loop until only one bit left standing
665 while( (low_bit=(samples & ~(samples-1))) != samples )
670 int Record::get_buffer_samples()
672 int fragment_samples = get_fragment_samples();
673 if( !fragment_samples ) return 0;
674 int fragments = (SESSION->record_write_length+fragment_samples-1) / fragment_samples;
675 if( fragments < 1 ) fragments = 1;
676 return fragments * fragment_samples;
679 Batch* Record::get_current_batch()
681 return record_batches.get_current_batch();
684 Batch* Record::get_editing_batch()
686 return record_batches.get_editing_batch();
689 int Record::get_next_batch(int incr)
691 int i = current_batch();
694 int total_batches = record_batches.total();
695 while( i < total_batches ) {
696 if( record_batches[i]->enabled ) return i;
703 const char* Record::current_mode()
705 Batch *batch = get_current_batch();
706 return batch ? Batch::mode_to_text(batch->record_mode) : "";
709 double Record::current_display_position()
711 //printf("Record::current_display_position "%jd %jd\n", total_samples, total_frames);
713 Asset *asset = default_asset;
715 if( asset->video_data && record_video )
716 result = (double) written_frames / asset->frame_rate;
717 else if( asset->audio_data && record_audio )
718 result = (double) written_samples / asset->sample_rate;
721 if( asset->video_data && record_video )
722 result = (double) total_frames / asset->frame_rate;
723 else if( asset->audio_data && record_audio )
724 result = (double) total_samples / asset->sample_rate;
726 result = (double) total_time.get_difference() / 1000.;
731 const char* Record::current_source()
733 Batch *batch = get_current_batch();
734 return batch ? batch->get_source_text() : _("Unknown");
737 Asset* Record::current_asset()
739 Batch *batch = get_current_batch();
740 return batch ? batch->asset : 0;
743 Channel *Record::get_current_channel()
745 return current_channel;
748 Channel *Record::get_editing_channel()
750 Batch *batch = get_editing_batch();
751 return batch ? batch->channel : 0;
754 ArrayList<Channel*>* Record::get_video_inputs()
756 return default_asset->video_data && vdevice ? vdevice->get_inputs() : 0;
760 int64_t Record::timer_position()
762 timer_lock->lock("RecordAudio::timer_positioin");
763 int samplerate = default_asset->sample_rate;
764 int64_t result = timer.get_scaled_difference(samplerate);
765 result += session_sample_offset;
766 timer_lock->unlock();
770 void Record::reset_position(int64_t position)
772 timer_lock->lock("RecordAudio::reset_position");
773 session_sample_offset = position;
774 device_sample_offset = adevice ? adevice->current_position() : 0;
776 timer_lock->unlock();
779 void Record::update_position()
781 int64_t position = sync_position();
782 reset_position(position);
783 current_frame = current_sample = 0;
784 audio_time = video_time = -1.;
787 int64_t Record::adevice_position()
789 timer_lock->lock("RecordAudio::adevice_position");
790 int64_t result = adevice->current_position();
791 result += session_sample_offset - device_sample_offset;
792 timer_lock->unlock();
796 int64_t Record::sync_position()
798 int64_t sync_position = -1;
799 double sync_time = -1.;
800 double next_audio_time = -1.;
801 double next_video_time = -1.;
803 if( current_frame == 0 && current_sample == 0 ) {
804 audio_time = video_time = -1.;
807 switch( SESSION->record_positioning ) {
808 case RECORD_POS_TIMESTAMPS:
809 if( default_asset->video_data && record_video )
810 next_video_time = vdevice->get_timestamp();
811 if( default_asset->audio_data && record_audio ) {
813 !adevice->is_monitoring() || writing_file > 0 ?
814 adevice->get_itimestamp() :
815 adevice->get_otimestamp();
817 if( next_audio_time >= 0. )
818 sync_time = next_audio_time;
819 else if( next_video_time > 0. )
820 sync_time = next_video_time;
821 if( sync_time >= 0. ) {
822 sync_position = sync_time * default_asset->sample_rate;
824 if( sync_position >= 0 ) break;
825 case RECORD_POS_DEVICE:
826 if( default_asset->audio_data && record_audio )
827 sync_position = adevice_position();
828 if( sync_position > 0 ) break;
829 case RECORD_POS_SOFTWARE:
830 sync_position = timer_position();
831 if( sync_position > 0 ) break;
832 case RECORD_POS_SAMPLE:
834 sync_position = current_sample;
838 if( next_video_time < 0. )
839 next_video_time = current_frame / default_asset->frame_rate;
840 if( next_video_time > video_time )
841 video_time = next_video_time;
842 if( next_audio_time < 0. )
843 next_audio_time = (double)sync_position / default_asset->sample_rate;
844 if( next_audio_time > audio_time )
845 audio_time = next_audio_time;
847 return sync_position;
851 void Record::adevice_drain()
853 if( adevice && record_audio ) {
854 adevice->stop_audio(0);
855 adevice->start_recording();
860 #define dbmsg if( debug ) printf
861 void Record::resync()
863 dropped = behind = 0;
864 int64_t audio_position = sync_position();
865 double audiotime = (double) audio_position / default_asset->sample_rate;
866 double diff = video_time - audiotime;
868 dbmsg("resync video %f audio %f/%f diff %f",
869 video_time, audiotime, audio_time, diff);
870 if( fabs(diff) > 5. ) {
871 dbmsg(" drain audio");
872 // jam job dvb file tesing, comment next line
873 record_channel->drain_audio();
875 else if( diff > 0. ) {
876 int64_t delay = (int64_t)(1000.0 * diff);
877 dbmsg(" delay %8jd", delay);
879 video_time = audio_time = -1.;
886 behind = (int)(-diff * default_asset->frame_rate);
887 dbmsg(" behind %d", behind);
888 if( behind > 1 && fill_underrun_frames ) {
889 dropped = behind > 3 ? 3 : behind-1;
890 dbmsg(" dropped %d", dropped);
894 int frames = dropped + 1;
895 current_frame += frames;
896 total_frames += frames;
897 record_gui->update_video(dropped, behind);
898 if( dropped > 0 && drop_overrun_frames )
899 if( vdevice ) vdevice->drop_frames(dropped);
903 void Record::close_audio_input()
905 adevice_lock->lock();
907 adevice->close_all();
911 adevice_lock->unlock();
914 void Record::close_video_input()
916 vdevice_lock->lock();
918 vdevice->close_all();
922 vdevice_lock->unlock();
925 void Record::close_input_devices()
931 void Record::stop_audio_thread()
934 record_audio->stop_recording();
942 void Record::stop_video_thread()
945 record_video->stop_recording();
954 void Record::stop_input_threads()
956 pause_lock->lock("Record::stop_input_threads");
961 pause_lock->unlock();
964 void Record::stop_playback()
967 record_monitor->stop_playback();
970 void Record::open_audio_input()
973 adevice_lock->lock();
975 if( default_asset->audio_data )
976 adevice = new AudioDevice(mwindow);
979 int sw_pos = SESSION->record_positioning == RECORD_POS_SOFTWARE;
980 adevice->set_software_positioning(sw_pos);
981 adevice->open_input(SESSION->aconfig_in, SESSION->vconfig_in,
982 default_asset->sample_rate, get_fragment_samples(),
983 default_asset->channels, SESSION->real_time_record);
984 adevice->start_recording();
985 adevice->open_monitor(SESSION->playback_config->aconfig,monitor_audio);
986 adevice->set_vdevice(vdevice);
987 if( vdevice ) vdevice->set_adevice(adevice);
989 adevice_lock->unlock();
992 void Record::open_video_input()
995 vdevice_lock->lock();
996 if( default_asset->video_data )
997 vdevice = new VideoDevice(mwindow);
1000 vdevice->set_quality(default_asset->jpeg_quality);
1001 vdevice->open_input(SESSION->vconfig_in, video_x, video_y,
1002 video_zoom, default_asset->frame_rate);
1003 // Get configuration parameters from device probe
1004 color_model = vdevice->get_best_colormodel(default_asset);
1005 master_channel->copy_usage(vdevice->channel);
1006 picture->copy_usage(vdevice->picture);
1007 vdevice->set_field_order(reverse_interlace);
1008 vdevice->set_do_cursor(do_cursor, do_big_cursor);
1009 vdevice->set_adevice(adevice);
1010 if( adevice ) adevice->set_vdevice(vdevice);
1011 set_dev_channel(get_current_channel());
1013 vdevice_lock->unlock();
1016 void Record::start_audio_thread()
1019 if( !record_audio ) {
1020 total_samples = current_sample = 0; audio_time = -1.;
1021 record_audio = new RecordAudio(mwindow,this);
1022 record_audio->arm_recording();
1023 record_audio->start_recording();
1028 void Record::start_video_thread()
1031 if( !record_video ) {
1032 total_frames = current_frame = 0; video_time = -1.;
1033 record_video = new RecordVideo(mwindow,this);
1034 record_video->arm_recording();
1035 record_video->start_recording();
1040 void Record::start_input_threads()
1043 input_threads_pausing = 0;
1044 if( default_asset->audio_data )
1045 start_audio_thread();
1046 if( default_asset->video_data )
1047 start_video_thread();
1051 void Record::pause_input_threads()
1053 pause_lock->lock("Record::pause_input_threads");
1054 input_threads_pausing = 1;
1056 adevice_lock->lock();
1057 vdevice_lock->lock();
1059 record_audio->pause_recording();
1061 record_video->pause_recording();
1064 void Record::resume_input_threads()
1067 record_audio->resume_recording();
1069 record_video->resume_recording();
1070 vdevice_lock->unlock();
1071 adevice_lock->unlock();
1072 input_threads_pausing = 0;
1074 pause_lock->unlock();
1078 int Record::start_toc()
1080 if( !mwindow->edl->session->record_realtime_toc ) return 0;
1081 Batch *batch = get_current_batch();
1082 Asset *asset = batch->asset;
1083 char source_filename[BCTEXTLEN], toc_path[BCTEXTLEN];
1084 IndexFile::get_index_filename(source_filename,
1085 mwindow->preferences->index_directory,
1086 toc_path, asset->path,".toc");
1087 if( default_asset->video_data )
1088 return vdevice->start_toc(asset->path, toc_path);
1089 if( default_asset->audio_data )
1090 return adevice->start_toc(asset->path, toc_path);
1094 int Record::start_record(int fd)
1097 if( default_asset->video_data )
1098 return vdevice->start_record(fd);
1099 if( default_asset->audio_data )
1100 return adevice->start_record(fd);
1104 void Record::start_writing_file()
1106 if( !writing_file ) {
1108 written_samples = 0;
1109 do_video = File::renders_video(default_asset);
1110 do_audio = File::renders_audio(default_asset);
1111 if( single_frame ) do_audio = 0;
1112 if( !do_video && single_frame )
1114 else if( !open_output_file() )
1115 writing_file = default_asset->format == FILE_MPEG ? -1 : 1;
1116 if( do_audio && record_audio && writing_file > 0 ) {
1117 int buffer_samples = get_buffer_samples();
1118 record_audio->set_write_buffer_samples(buffer_samples);
1119 file->start_audio_thread(buffer_samples, FILE_RING_BUFFERS);
1121 if( do_video && record_video && writing_file > 0 ) {
1122 int disk_frames = SESSION->video_write_length;
1123 int cmodel = vdevice->get_best_colormodel(default_asset);
1124 int cpress = vdevice->is_compressed(1, 0);
1125 file->start_video_thread(disk_frames, cmodel, FILE_RING_BUFFERS, cpress);
1127 if( writing_file < 0 ) {
1128 int fd = file->record_fd();
1132 if( writing_file ) {
1133 record_gui->start_flash_batch();
1134 if( SESSION->record_sync_drives ) {
1135 drivesync = new DriveSync();
1143 int Record::stop_record()
1145 if( default_asset->video_data )
1146 return vdevice->stop_record();
1147 if( default_asset->audio_data )
1148 return adevice->stop_record();
1152 void Record::flush_buffer()
1155 record_audio->flush_buffer();
1157 record_video->flush_buffer();
1160 void Record::stop_writing_file()
1162 record_gui->stop_flash_batch();
1163 if( !writing_file ) return;
1164 if( writing_file > 0 )
1166 if( writing_file < 0 )
1168 close_output_file();
1170 Asset *asset = current_asset();
1171 asset->audio_length = written_samples;
1172 asset->video_length = written_frames;
1173 record_gui->flash_batch();
1175 drivesync->done = 1;
1181 void Record::stop_writing()
1183 if( writing_file ) {
1184 pause_input_threads();
1185 stop_writing_file();
1186 resume_input_threads();
1191 void Record::start_cron_thread()
1193 if( cron_active() < 0 ) {
1194 delete record_thread;
1197 if( !record_thread ) {
1198 record_thread = new RecordThread(mwindow,this);
1199 record_thread->start();
1200 record_gui->disable_batch_buttons();
1201 record_gui->update_cron_status(_("Running"));
1205 void Record::stop_cron_thread(const char *msg)
1207 if( record_thread ) {
1208 delete record_thread;
1210 record_gui->enable_batch_buttons();
1211 record_gui->update_cron_status(msg);
1215 void Record::set_power_off(int value)
1218 record_gui->power_off->update(value);
1221 void Record::set_video_picture()
1223 if( default_asset->video_data && vdevice )
1224 vdevice->set_picture(picture);
1227 void Record::set_do_cursor()
1229 vdevice->set_do_cursor(do_cursor, do_big_cursor);
1232 void Record::set_translation(int x, int y)
1236 if(default_asset->video_data && vdevice)
1237 vdevice->set_translation(video_x, video_y);
1240 int Record::set_channel(Channel *channel)
1242 if( !channel ) return 1;
1243 printf("set_channel %s\n",channel->title);
1244 if( record_channel->set(channel) ) return 1;
1245 RecordMonitorGUI *window = record_monitor->window;
1246 window->lock_window("Record::set_channel");
1247 window->channel_picker->channel_text->update(channel->title);
1248 window->unlock_window();
1252 int Record::set_channel_no(int chan_no)
1254 if( chan_no < 0 || chan_no >= channeldb->size() ) return 1;
1255 Channel *channel = channeldb->get(chan_no);
1256 return set_channel(channel);
1259 int Record::channel_down()
1261 Channel *channel = get_current_channel();
1262 if( !channel || !channeldb->size() ) return 1;
1263 int n = channeldb->number_of(channel);
1264 if( n < 0 ) return 1;
1265 if( --n < 0 ) n = channeldb->size() - 1;
1266 return set_channel_no(n);
1269 int Record::channel_up()
1271 Channel *channel = get_current_channel();
1272 if( !channel || !channeldb->size() ) return 1;
1273 int n = channeldb->number_of(channel);
1274 if( n < 0 ) return 1;
1275 if( ++n >= channeldb->size() ) n = 0;
1276 return set_channel_no(n);
1279 int Record::set_channel_name(const char *name)
1281 Channel *channel = 0;
1282 int ch = 0, nch = channeldb->size();
1284 channel = channeldb->get(ch);
1285 if( channel && !channel->cstrcmp(name) ) break;
1288 if( ch >= nch || !channel ) return 1;
1289 return set_channel(channel);
1292 void Record::set_batch_channel_no(int chan_no)
1294 Channel *channel = channeldb->get(chan_no);
1296 Batch *batch = get_editing_batch();
1297 if( batch ) batch->channel = channel;
1298 record_gui->batch_source->update(channel->title);
1302 void Record::set_dev_channel(Channel *channel)
1304 // should be tuner device, not vdevice
1305 if( channel && vdevice &&
1306 (!this->channel || *channel != *this->channel) ) {
1307 current_channel = channel;
1308 if( this->channel ) delete this->channel;
1309 this->channel = new Channel(channel);
1310 vdevice->set_channel(channel);
1311 int strk = !SESSION->decode_subtitles ? -1 : SESSION->subtitle_number;
1312 vdevice->set_captioning(strk);
1313 set_video_picture();
1314 printf("new channel %s, has_signal=%d\n",channel->title,vdevice->has_signal());
1317 total_time.update();
1321 /* widget holds xlock and set_channel pauses rendering, deadlock */
1322 /* use a background thread to update channel after widget sets it */
1323 RecordChannel::RecordChannel(Record *record)
1326 this->record = record;
1327 change_channel = new Condition(0,"RecordSetChannel::change_channel");
1328 channel_lock = new Condition(1,"Record::channel_lock",1);
1333 RecordChannel::~RecordChannel()
1336 change_channel->unlock();
1338 delete change_channel;
1339 delete channel_lock;
1342 int RecordChannel::set(Channel *channel)
1344 if( !channel || new_channel ) return 1;
1345 new_channel = channel;
1346 channel_lock->lock(); // block has_signal
1347 change_channel->unlock(); // resume channel changer thread
1351 void RecordChannel::drain_audio()
1353 if( !audio_drain ) {
1355 change_channel->unlock();
1359 void RecordChannel::run()
1363 change_channel->lock();
1365 if( !new_channel && !audio_drain ) continue;
1366 record->pause_input_threads();
1368 record->set_dev_channel(new_channel);
1369 record->record_gui->reset_video();
1370 record->record_gui->reset_audio();
1375 record->adevice_drain();
1377 record->update_position();
1378 record->resume_input_threads();
1381 channel_lock->unlock();
1386 int Record::has_signal()
1388 record_channel->channel_lock->lock("Record::has_signal");
1389 record_channel->channel_lock->unlock();
1390 vdevice_lock->lock();
1391 int result = vdevice ? vdevice->has_signal() : 0;
1392 vdevice_lock->unlock();
1396 int Record::create_channeldb(ArrayList<Channel*> *channeldb)
1398 return vdevice ? vdevice->create_channeldb(channeldb) : 1;
1402 void Record::set_audio_monitoring(int mode)
1405 monitor_audio = mode;
1407 record_audio->set_monitoring(mode);
1408 record_gui->lock_window("Record::set_audio_monitoring");
1409 if( record_gui->monitor_audio )
1410 record_gui->monitor_audio->update(mode);
1411 record_gui->unlock_window();
1414 void Record::set_audio_metering(int mode)
1416 metering_audio = mode;
1417 record_gui->lock_window("Record::set_audio_metering");
1418 if( record_gui->meter_audio )
1419 record_gui->meter_audio->update(mode);
1420 record_gui->unlock_window();
1421 RecordMonitorGUI *window = record_monitor->window;
1422 window->lock_window("Record::set_audio_metering 1");
1423 window->resize_event(window->get_w(),window->get_h());
1424 window->unlock_window();
1428 void Record::set_video_monitoring(int mode)
1430 monitor_video = mode;
1431 record_gui->lock_window("Record::set_video_monitoring");
1432 if( record_gui->monitor_video )
1433 record_gui->monitor_video->update(mode);
1434 record_gui->flush();
1435 record_gui->unlock_window();
1438 int Record::get_time_format()
1440 return SESSION->time_format;
1443 int Record::set_record_mode(int value)
1445 Batch *batch = get_editing_batch();
1446 batch->record_mode = value;
1447 record_gui->update_batches();
1451 int Record::check_batch_complete()
1455 if( writing_file && cron_active() > 0 ) {
1456 Batch *batch = get_current_batch();
1457 if( batch && batch->recorded > 0 &&
1458 batch->record_mode == RECORD_TIMED &&
1459 current_display_position() >= batch->record_duration ) {
1460 batch->recorded = -1;
1462 record_gui->update_batches();
1463 record_thread->batch_timed_lock->unlock();
1467 batch_lock->unlock();
1471 #ifdef HAVE_COMMERCIAL
1472 int Record::skimming(void *vp, int track)
1474 return ((Record*)vp)->skimming(track);
1477 int Record::skimming(int track)
1479 int64_t framenum; uint8_t *tdat; int mw, mh;
1480 if( !vdevice ) return -1;
1481 if( vdevice->get_thumbnail(track, framenum, tdat, mw, mh) ) return 1;
1482 int pid, width, height; double framerate;
1483 if( vdevice->get_video_info(track, pid, framerate, width, height, 0) ) return 1;
1484 if( !framerate ) return 1;
1485 return skim_thread->skim(pid,framenum,framerate, tdat,mw,mh,width,height);
1488 void Record::start_skimming()
1490 if( commercial_check && vdevice && !skimming_active ) {
1491 skimming_active = 1;
1492 skim_thread->start(mwindow->commercials);
1493 vdevice->set_skimming(0, 0, skimming, this);
1497 void Record::stop_skimming()
1499 if( skimming_active && vdevice ) {
1500 skimming_active = 0;
1501 vdevice->set_skimming(0, 0, 0, 0);
1502 skim_thread->stop();
1506 void Record::update_skimming(int v)
1508 if( (commercial_check=v) != 0 )
1515 int Record::skimming(void *vp, int track) { return 1; }
1516 int Record::skimming(int track) { return 1; }
1517 void Record::start_skimming() {}
1518 void Record::stop_skimming() {}
1519 void Record::update_skimming(int v) {}
1522 RecordRemoteHandler::RecordRemoteHandler(RemoteControl *remote_control)
1523 : RemoteHandler(remote_control->gui, GREEN)
1527 RecordRemoteHandler::~RecordRemoteHandler()
1532 display_video_text(int x, int y, const char *text, int font,
1533 int bg_color, int color, int alpha, double secs, double scale)
1535 // gotta have a window for resources
1536 record_monitor->window->
1537 display_video_text(x, y, text, font,
1538 bg_color, color, alpha, secs, scale);
1542 display_cut_icon(int x, int y)
1544 VFrame *cut_icon = *mwindow->theme->get_image_set("commercial");
1546 x += cut_icon->get_w()*scale; y += cut_icon->get_h()*scale;
1547 display_vframe(cut_icon, x, y, 200, 1.0, scale);
1551 DeviceDVBInput *Record::
1554 DeviceDVBInput *dvb_dev = !vdevice ? 0 :
1555 (DeviceDVBInput *)vdevice->mpeg_device();
1556 if( !dvb_dev ) dvb_dev = !adevice ? 0 :
1557 (DeviceDVBInput *)adevice->mpeg_device();
1565 display_vframe(VFrame *in, int x, int y, int alpha, double secs, double scale)
1567 if( !channel ) return;
1568 if( !vdevice || vdevice->in_config->driver != CAPTURE_DVB ) return;
1569 DeviceDVBInput *dvb_input = dvb_device();
1570 if( !dvb_input ) return 1;
1571 zmpeg3_t *fd = dvb_input->get_src();
1573 scale *= fd->video_height(channel->video_stream) / 1080.;
1574 int ww = in->get_w() * scale, hh = in->get_h() * scale;
1575 VFrame out(ww, hh, BC_YUV444P);
1576 BC_WindowBase::get_cmodels()->transfer(out.get_rows(), in->get_rows(),
1577 out.get_y(), out.get_u(), out.get_v(),
1578 in->get_y(), in->get_u(), in->get_v(),
1579 0, 0, in->get_w(), in->get_h(),
1580 0, 0, out.get_w(), out.get_h(),
1581 in->get_color_model(), out.get_color_model(), 0,
1582 in->get_bytes_per_line(), ww);
1583 int sz = ww * hh; uint8_t *yuv = out.get_data(), aimg[sz];
1584 uint8_t *yp = yuv, *up = yuv+sz, *vp = yuv+2*sz, *ap = 0;
1585 if( alpha > 0 ) memset(ap=aimg, alpha, sz);
1586 else if( alpha < 0 ) ap = yp;
1587 fd->display_subtitle(channel->video_stream, 1, 1,
1588 yp,up,vp,ap, x,y,ww,hh, 0, secs*1000);
1589 dvb_input->put_src();
1595 if( !channel ) return;
1596 if( !vdevice || vdevice->in_config->driver != CAPTURE_DVB ) return;
1597 DeviceDVBInput *dvb_input = dvb_device();
1598 if( !dvb_input ) return 1;
1599 zmpeg3_t *fd = dvb_input->get_src();
1601 fd->delete_subtitle(channel->video_stream, 1, 1);
1602 dvb_input->put_src();
1608 display_vframe(VFrame *in, int x, int y, int alpha, double secs, double scale)
1610 record_monitor->display_vframe(in, x, y, alpha, secs, scale);
1616 record_monitor->undisplay_vframe();
1622 display_channel_info()
1625 if( !channel ) return 1;
1626 if( !vdevice || vdevice->in_config->driver != CAPTURE_DVB ) return 1;
1627 DeviceDVBInput *dvb_input = dvb_device();
1628 if( !dvb_input ) return 1;
1629 int elem = channel->element;
1630 time_t tt; time(&tt);
1631 struct tm ttm; localtime_r(&tt,&ttm);
1633 char text[BCTEXTLEN];
1634 zmpeg3_t *fd = dvb_input->get_src();
1636 for( int ord=0, i=0; ord<0x80; ) {
1638 int len = fd->dvb.get_chan_info(elem,ord,i++,cp,BCTEXTLEN-2);
1639 if( len < 0 ) { ++ord; i = 0; continue; }
1640 struct tm stm = ttm, etm = ttm;
1641 char wday[4], *bp = cp; cp += len;
1642 if( sscanf(bp, "%02d:%02d:%02d-%02d:%02d:%02d",
1643 &stm.tm_hour, &stm.tm_min, &stm.tm_sec,
1644 &etm.tm_hour, &etm.tm_min, &etm.tm_sec) != 6 ) continue;
1645 while( bp<cp && *bp++!='\n' );
1646 if( sscanf(bp, "(%3s) %04d/%02d/%02d", &wday[0],
1647 &stm.tm_year, &stm.tm_mon, &stm.tm_mday) != 4 ) continue;
1648 stm.tm_year -= 1900; stm.tm_mon -= 1;
1649 etm.tm_year = stm.tm_year;
1650 etm.tm_mon = stm.tm_mon;
1651 etm.tm_mday = stm.tm_mday;
1652 time_t st = mktime(&stm), et = mktime(&etm);
1653 if( et < st ) et += 24*3600;
1654 if( tt < st || tt >= et ) continue;
1655 int major=0, minor=0;
1656 fd->dvb.get_channel(elem, major, minor);
1657 char chan[64]; sprintf(chan, "%3d.%1d", major, minor);
1658 for( i=0; i<5 && chan[i]!=0; ++i ) *bp++ = chan[i];
1659 while( bp<cp && *bp++!='\n' );
1661 for( char *lp=bp; bp<cp; ++bp ) {
1662 if( *bp == '\n' || ((bp-lp)>=60 && *bp==' ') ) {
1663 if( ++n >= 10 ) break;
1668 while( bp > text && *--bp == '\n' ) *bp = 0;
1672 dvb_input->put_src();
1674 display_video_text(20, 20, text,
1675 BIGFONT, WHITE, BLACK, 0, 3., 1.);
1683 display_channel_schedule()
1686 if( !channel ) return 1;
1687 if( !vdevice || vdevice->in_config->driver != CAPTURE_DVB ) return 1;
1688 DeviceDVBInput *dvb_input = dvb_device();
1689 if( !dvb_input ) return 1;
1690 int elem = channel->element;
1691 time_t tt; time(&tt);
1692 struct tm ttm; localtime_r(&tt,&ttm);
1693 char text[BCTEXTLEN];
1694 zmpeg3_t *fd = dvb_input->get_src();
1696 RecordSchedule channel_schedule;
1697 for( int ord=0, i=0; ord<0x80; ) {
1699 int len = fd->dvb.get_chan_info(elem,ord,i++,cp,BCTEXTLEN-2);
1700 if( len < 0 ) { ++ord; i = 0; continue; }
1701 struct tm stm = ttm, etm = ttm;
1702 char wday[4], *bp = cp; cp += len;
1703 if( sscanf(bp, "%02d:%02d:%02d-%02d:%02d:%02d",
1704 &stm.tm_hour, &stm.tm_min, &stm.tm_sec,
1705 &etm.tm_hour, &etm.tm_min, &etm.tm_sec) != 6 ) continue;
1706 while( bp<cp && *bp++!='\n' );
1707 if( sscanf(bp, "(%3s) %04d/%02d/%02d", &wday[0],
1708 &stm.tm_year, &stm.tm_mon, &stm.tm_mday) != 4 ) continue;
1709 stm.tm_year -= 1900; stm.tm_mon -= 1;
1710 etm.tm_year = stm.tm_year;
1711 etm.tm_mon = stm.tm_mon;
1712 etm.tm_mday = stm.tm_mday;
1713 time_t st = mktime(&stm), et = mktime(&etm);
1714 if( et < st ) et += 24*3600;
1715 if( tt >= et ) continue;
1716 if( bp > text )*--bp = 0;
1717 channel_schedule.append(new RecordScheduleItem(st, text));
1719 dvb_input->put_src();
1721 channel_schedule.sort_times();
1722 char *cp = text, *ep = cp+sizeof(text)-1;
1723 for( int i=0, n=0; i<channel_schedule.size(); ++i ) {
1724 RecordScheduleItem *item = channel_schedule.get(i);
1725 for( char *bp=item->title; cp<ep && *bp!=0; ++bp ) *cp++ = *bp;
1726 if( cp < ep ) *cp++ = '\n';
1727 if( ++n >= 12 ) break;
1730 while( cp > text && *--cp == '\n' ) *cp = 0;
1732 display_video_text(20, 20, text,
1733 BIGFONT, WHITE, BLACK, 0, 3., 1.);
1740 void Record::clear_keybfr()
1746 void Record::add_key(int ch)
1749 while( n<(int)sizeof(keybfr)-2 && keybfr[n] ) ++n;
1750 keybfr[n++] = ch; keybfr[n] = 0;
1751 display_video_text(20, 20, keybfr,
1752 BIGFONT, WHITE, BLACK, 0, 3., 2.);
1755 int RecordRemoteHandler::remote_process_key(RemoteControl *remote_control, int key)
1757 Record *record = remote_control->mwindow_gui->record;
1758 return record->remote_process_key(remote_control, key);
1761 int Record::remote_process_key(RemoteControl *remote_control, int key)
1767 if( last_key == KPENTER ) {
1768 set_channel_name(keybfr);
1772 ch = '.'; // fall through
1773 case '0': if( last_key == '0' && ch == '0' ) {
1777 case '1': case '2': case '3': case '4':
1778 case '5': case '6': case '7': case '8': case '9':
1781 //case UP: case DOWN: case LEFT: case RIGHT:
1782 //case KPPLAY: case KPBACK: case KPFORW:
1783 #ifdef HAVE_COMMERCIAL
1784 case KPRECD: case 'c': // start capture, mark endpoint
1786 start_commercial_capture();
1788 set_play_gain(0.075);
1791 mark_commercial_capture(DEL_MARK);
1792 blink_status->update();
1794 display_cut_icon(10,20);
1796 case KPSTOP: case 'd': // end capture, start cutting
1797 remote_control->set_color(YELLOW);
1798 stop_commercial_capture(1);
1800 case KPAUSE: case 'x': // ignore current commercial
1801 mark_commercial_capture(DEL_SKIP);
1804 case KPBACK: case 'a': // toggle mute audio
1805 if( !monitor_audio ) { set_mute_gain(1); set_play_gain(1); }
1806 set_audio_monitoring(monitor_audio ? 0 : 1);
1808 case 'm': // toggle metering audio
1809 set_audio_metering(metering_audio ? 0 : 1);
1811 #ifdef HAVE_COMMERCIAL
1812 case KPPLAY: case 's': // ignore previous endpoint
1813 mark_commercial_capture(DEL_OOPS);
1816 case KPFWRD: case KPSLASH:
1817 display_channel_info();
1819 case KPMAXW: case KPSTAR:
1820 display_channel_schedule();
1822 case KPCHUP: case KPPLUS:
1825 case KPCHDN: case KPMINUS:
1829 Canvas *canvas = record_monitor->window->canvas;
1830 canvas->use_fullscreen(canvas->get_fullscreen() ? 0 : 1);
1840 #ifdef HAVE_COMMERCIAL
1841 int Record::start_commercial_capture()
1843 if( deletions != 0 ) return 1;
1844 Channel *channel = get_current_channel();
1845 if( !channel ) return 1;
1846 int track = channel->video_stream;
1847 int pid = vdevice->get_video_pid(track);
1848 if( pid < 0 ) return 1;
1849 time_t st; time(&st);
1850 struct tm stm; localtime_r(&st, &stm);
1851 char file_path[BCTEXTLEN];
1852 sprintf(file_path,"/tmp/del%04d%02d%02d-%02d%02d%02d.ts",
1853 stm.tm_year+1900, stm.tm_mon+1, stm.tm_mday,
1854 stm.tm_hour, stm.tm_min, stm.tm_sec);
1855 commercial_fd = open(file_path,O_RDWR+O_CREAT,0666);
1856 if( commercial_fd < 0 ) return 1;
1857 if( vdevice->start_record(commercial_fd, 0x800000) ) {
1858 close(commercial_fd);
1862 commercial_start_time = st;
1863 printf("del to %s\n", file_path);
1864 deletions = new Deletions(pid, file_path);
1865 mark_commercial_capture(DEL_START);
1866 blink_status->start();
1870 int Record::mark_commercial_capture(int action)
1872 if( deletions == 0 ) return 1;
1873 double time = vdevice->get_timestamp();
1874 printf("dele %f\n", time);
1875 deletions->append(new Dele(time, action));
1880 void Record::remote_fill_color(int color)
1882 mwindow->gui->remote_control->fill_color(color);
1885 void Record::set_status_color(int color)
1887 status_color = color;
1888 remote_fill_color(status_color);
1891 void Record::set_mute_gain(double gain)
1893 gain = (mute_gain = gain) * play_gain;
1894 if( adevice ) adevice->set_play_gain(gain);
1897 void Record::set_play_gain(double gain)
1899 gain = mute_gain * (play_gain = gain);
1900 if( adevice ) adevice->set_play_gain(gain);
1903 #ifdef HAVE_COMMERCIAL
1904 int Record::stop_commercial_capture(int run_job)
1906 if( deletions == 0 ) return 1;
1907 if( run_job ) blink_status->stop();
1910 vdevice->stop_record();
1911 commercial_start_time = -1;
1912 if( commercial_fd >= 0 ) {
1913 close(commercial_fd);
1918 char del_filename[BCTEXTLEN];
1919 strcpy(del_filename, deletions->file_path());
1920 strcpy(strrchr(del_filename, '.'),".del");
1921 if( !deletions->write_dels(del_filename) ) {
1922 int pid = spawn("cutads %s",del_filename);
1924 cut_pids.append(pid);
1925 cutads_status->start_waiting();
1931 remove_file(deletions->filepath);
1933 delete deletions; deletions = 0;
1938 spawn(const char *fmt, ...)
1940 const char *exec_path = File::get_cinlib_path();
1941 char cmd[BCTEXTLEN], *cp = cmd, *ep = cp+sizeof(cmd)-1;
1942 va_list ap; va_start(ap, fmt);
1943 cp += snprintf(cp, ep-cp, "exec %s/", exec_path);
1944 cp += vsnprintf(cp, ep-cp, fmt, ap); va_end(ap);
1946 pid_t pid = vfork();
1947 if( pid < 0 ) return -1;
1948 if( pid > 0 ) return pid;
1949 char *const argv[4] = { (char*) "/bin/sh", (char*) "-c", cmd, 0 };
1950 execvp(argv[0], &argv[0]);
1957 for( int i=0; i<cut_pids.size(); ) {
1958 int status, pid = cut_pids.get(i);
1959 if( waitpid(pid, &status, WNOHANG) ) {
1960 cut_pids.remove(pid);
1965 return cut_pids.size();
1969 RecordCutAdsStatus::
1970 RecordCutAdsStatus(Record *record)
1973 this->record = record;
1974 wait_lock = new Condition(0,"RecordCutAdsStatus::wait_lock",0);
1979 RecordCutAdsStatus::
1980 ~RecordCutAdsStatus()
1984 wait_lock->unlock();
1991 void RecordCutAdsStatus::
1994 wait_lock->unlock();
1997 void RecordCutAdsStatus::
2000 int status, pgrp = getpgrp();
2002 wait_lock->lock("RecordCutAdsStatus::run");
2004 if( !record->commercial_jobs() ) continue;
2005 record->set_status_color(YELLOW);
2008 waitpid(-pgrp, &status, 0);
2010 if( !record->commercial_jobs() ) break;
2012 record->set_status_color(GREEN);
2016 void RecordBlinkStatus::
2017 remote_color(int color)
2019 record->remote_fill_color(color);
2022 void RecordBlinkStatus::
2028 void RecordBlinkStatus::
2036 void RecordBlinkStatus::
2047 RecordBlinkStatus(Record *record)
2050 this->record = record;
2054 ~RecordBlinkStatus()
2059 void RecordBlinkStatus::
2064 remote_color(color);
2068 color ^= YELLOW ^ BLUE;
2069 if( timer.get_difference() > 10*60*1000 ) { // 10 minutes
2070 record->stop_commercial_capture(0);
2074 remote_color(record->status_color);