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 skim_thread = new SkimDbThread();
159 cutads_status = new RecordCutAdsStatus(this);
160 blink_status = new RecordBlinkStatus(this);
165 mwindow->gui->record = 0;
168 delete cutads_status;
174 delete record_channel;
175 delete master_channel;
188 int Record::load_defaults()
191 char string[BCTEXTLEN];
192 BC_Hash *defaults = mwindow->defaults;
193 EDLSession *session = SESSION;
194 default_asset->copy_from(session->recording_format, 0);
195 default_asset->channels = session->aconfig_in->channels;
196 default_asset->sample_rate = session->aconfig_in->in_samplerate;
197 default_asset->frame_rate = session->vconfig_in->in_framerate;
198 default_asset->width = session->vconfig_in->w;
199 default_asset->height = session->vconfig_in->h;
200 default_asset->layers = 1;
201 // Fix encoding parameters depending on driver.
202 // These are locked by a specific driver.
203 const char *vcodec = 0;
204 switch( session->vconfig_in->driver ) {
206 case VIDEO4LINUX2MPEG:
208 case VIDEO4LINUX2JPEG:
209 vcodec = CODEC_TAG_MJPEG;
211 case CAPTURE_FIREWIRE:
212 case CAPTURE_IEC61883:
213 vcodec = CODEC_TAG_DVSD;
217 strcpy(default_asset->vcodec, vcodec);
219 record_batches.load_defaults(channeldb, this);
221 int cur_chan_no = defaults->get("RECORD_CURRENT_CHANNEL", 0);
222 current_channel = channeldb->get(cur_chan_no);
223 load_mode = defaults->get("RECORD_LOADMODE", LOADMODE_PASTE);
224 monitor_audio = defaults->get("RECORD_MONITOR_AUDIO", 1);
225 metering_audio = defaults->get("RECORD_METERING_AUDIO", 1);
226 monitor_video = defaults->get("RECORD_MONITOR_VIDEO", 1);
227 video_window_open = defaults->get("RECORD_MONITOR_OPEN", 1);
228 video_x = defaults->get("RECORD_VIDEO_X", 0);
229 video_y = defaults->get("RECORD_VIDEO_Y", 0);
230 video_zoom = defaults->get("RECORD_VIDEO_Z", (float)1);
231 picture->load_defaults();
232 reverse_interlace = defaults->get("REVERSE_INTERLACE", 0);
233 do_cursor = defaults->get("RECORD_CURSOR", 0);
234 do_big_cursor = defaults->get("RECORD_BIG_CURSOR", 0);
235 for( int i=0; i<MAXCHANNELS; ++i ) {
236 sprintf(string, "RECORD_DCOFFSET_%d", i);
237 dc_offset[i] = defaults->get(string, 0);
239 drop_overrun_frames = defaults->get("DROP_OVERRUN_FRAMES", 0);
240 fill_underrun_frames = defaults->get("FILL_UNDERRUN_FRAMES", 0);
241 commercial_check = defaults->get("COMMERCIAL_CHECK", 0);
245 int Record::save_defaults()
247 char string[BCTEXTLEN];
248 BC_Hash *defaults = mwindow->defaults;
249 set_editing_batch(0);
250 // Save default asset path but not the format because that's
251 // overridden by the driver.
252 // The format is saved in preferences.
253 if( record_batches.total() )
254 strcpy(default_asset->path, record_batches[0]->asset->path);
255 default_asset->save_defaults(defaults, "RECORD_", 0, 0, 0, 0, 0);
257 record_batches.save_defaults(channeldb);
259 int cur_chan_no = channeldb->number_of(current_channel);
260 defaults->update("RECORD_CURRENT_CHANNEL", cur_chan_no);
261 defaults->update("RECORD_LOADMODE", load_mode);
262 defaults->update("RECORD_MONITOR_AUDIO", monitor_audio);
263 defaults->update("RECORD_METERING_AUDIO", metering_audio);
264 defaults->update("RECORD_MONITOR_VIDEO", monitor_video);
265 defaults->update("RECORD_MONITOR_OPEN", video_window_open);
266 defaults->update("RECORD_VIDEO_X", video_x);
267 defaults->update("RECORD_VIDEO_Y", video_y);
268 defaults->update("RECORD_VIDEO_Z", video_zoom);
269 picture->save_defaults();
270 defaults->update("REVERSE_INTERLACE", reverse_interlace);
271 defaults->update("RECORD_CURSOR", do_cursor);
272 defaults->update("RECORD_BIG_CURSOR", do_big_cursor);
273 for( int i=0; i<MAXCHANNELS; ++i ) {
274 sprintf(string, "RECORD_DCOFFSET_%d", i);
275 defaults->update(string, dc_offset[i]);
277 defaults->update("DROP_OVERRUN_FRAMES", drop_overrun_frames);
278 defaults->update("FILL_UNDERRUN_FRAMES", fill_underrun_frames);
279 defaults->update("COMMERCIAL_CHECK", commercial_check);
285 void Record::configure_batches()
287 // printf("Record::configure_batches %d\n",__LINE__);
288 // default_assset->dump();
289 strcpy(record_batches[0]->asset->path, default_asset->path);
290 int total_batches = record_batches.total();
291 for( int i=0; i<total_batches; ++i ) {
292 Batch *batch = record_batches[i];
293 batch->asset->copy_format(default_asset);
301 // Default asset forms the first path in the batch capture
302 // and the file format for all operations.
303 default_asset = new Asset;
305 // Determine information about the device.
306 AudioInConfig *aconfig_in = SESSION->aconfig_in;
307 VideoInConfig *vconfig_in = SESSION->vconfig_in;
308 int driver = vconfig_in->driver;
309 VideoDevice::load_channeldb(channeldb, vconfig_in);
310 fixed_compression = VideoDevice::is_compressed(driver, 0, 1);
312 // Apply a major kludge
313 if( fixed_compression ) {
315 device.fix_asset(default_asset, driver);
317 default_asset->channels = aconfig_in->channels;
318 VideoDevice::save_channeldb(channeldb, vconfig_in);
320 mwindow->save_defaults();
322 set_current_batch(0);
323 set_editing_batch(0);
327 edl->create_objects();
328 edl->session->output_w = default_asset->width;
329 edl->session->output_h = default_asset->height;
330 edl->session->aspect_w = SESSION->aspect_w;
331 edl->session->aspect_h = SESSION->aspect_h;
333 window_lock->lock("Record::run 3");
334 record_gui = new RecordGUI(mwindow, this);
335 record_gui->create_objects();
337 record_monitor = new RecordMonitor(mwindow, this);
338 record_monitor->create_objects();
339 record_gui->update_batch_sources();
341 record_gui->show_window();
344 if( mwindow->gui->remote_control->deactivate() )
345 mwindow->gui->record_remote_handler->activate();
347 if( video_window_open ) {
348 record_monitor->window->show_window();
349 record_monitor->window->raise_window();
350 record_monitor->window->flush();
353 window_lock->unlock();
355 start_input_threads();
359 result = record_gui->run_window();
360 // record gui operates here until window is closed
362 if( record_gui->get_window_lock() ) {
363 record_gui->lock_window();
364 record_gui->unlock_window();
367 // Need to stop everything this time
369 stop_commercial_capture(0);
370 int video_stream = -1;
371 if( default_asset->format == FILE_MPEG ) {
372 Channel *channel = get_current_channel();
374 video_stream = channel->video_stream;
377 edl->Garbage::remove_user();
379 if( mwindow->gui->remote_control->deactivate() )
380 mwindow->gui->cwindow_remote_handler->activate();
382 // Save everything again
386 if( !result && load_mode != LOADMODE_NOTHING ) {
387 mwindow->gui->lock_window("Record::run");
388 ArrayList<EDL*> new_edls;
390 int total_batches = record_batches.total();
391 for( int i=0; i<total_batches; ++i ) {
392 Batch *batch = record_batches[i];
393 Asset *asset = batch->asset;
394 if( batch->recorded ) {
395 EDL *new_edl = new EDL;
396 mwindow->remove_asset_from_caches(asset);
397 new_edl->create_objects();
398 new_edl->copy_session(mwindow->edl);
399 mwindow->asset_to_edl(new_edl, asset, batch->labels);
400 new_edls.append(new_edl);
405 mwindow->undo->update_undo_before();
406 // For pasting, clear the active region
407 if(load_mode == LOADMODE_PASTE)
409 int loadmode = load_mode == LOADMODE_RESOURCESONLY ?
410 LOADMODE_ASSETSONLY : load_mode;
411 mwindow->paste_edls(&new_edls, loadmode, 0, -1,
412 SESSION->labels_follow_edits,
413 SESSION->plugins_follow_edits,
414 SESSION->autos_follow_edits,
416 for( int i=0; i<new_edls.total; ++i )
417 new_edls.get(i)->Garbage::remove_user();
418 new_edls.remove_all();
419 if( video_stream >= 0 ) {
420 mwindow->select_asset(video_stream, -1);
421 mwindow->fit_selection();
423 mwindow->save_backup();
424 mwindow->undo->update_undo_after(_("record"), LOAD_ALL);
425 mwindow->restart_brender();
426 mwindow->update_plugin_guis();
427 mwindow->gui->update(1, 2, 1, 1, 1, 1, 0);
428 mwindow->sync_parameters(CHANGE_ALL);
430 mwindow->gui->unlock_window();
432 AWindowGUI *agui = mwindow->awindow->gui;
433 agui->async_update_assets();
437 record_batches.clear();
438 default_asset->Garbage::remove_user();
441 void Record::stop(int wait)
445 record_gui->set_done(1);
448 window_lock->lock("Record::stop");
449 delete record_thread; record_thread = 0;
450 delete record_monitor; record_monitor = 0;
451 delete record_gui; record_gui = 0;
452 window_lock->unlock();
455 void Record::activate_batch(int number)
457 if( number != current_batch() ) {
459 set_current_batch(number);
460 record_gui->update_batches();
461 record_gui->update_position(current_display_position());
462 record_gui->update_batch_tools();
467 void Record::delete_index_file(Asset *asset)
469 IndexFile::delete_index(mwindow->preferences, asset, ".toc");
470 IndexFile::delete_index(mwindow->preferences, asset, ".idx");
471 IndexFile::delete_index(mwindow->preferences, asset, ".mkr");
474 void Record::delete_batch()
476 // Abort if one batch left
477 int edit_batch = editing_batch();
478 int total_batches = record_batches.total();
479 if( total_batches > 1 && edit_batch < total_batches ) {
480 int cur_batch = current_batch();
481 Batch *batch = record_batches[edit_batch];
482 record_batches.remove(batch); delete batch;
483 if( cur_batch == edit_batch ) stop_writing();
484 record_gui->update_batches();
485 record_gui->update_batch_tools();
489 void Record::change_editing_batch(int number)
491 set_editing_batch(number);
492 record_gui->update_batch_tools();
495 Batch* Record::new_batch()
497 Batch *batch = new Batch(mwindow, this);
498 //printf("Record::new_batch 1\n");
499 batch->create_objects();
500 record_batches.append(batch);
501 batch->asset->copy_format(default_asset);
503 //printf("Record::new_batch 1\n");
504 batch->create_default_path();
505 Batch *edit_batch = get_editing_batch();
507 batch->copy_from(edit_batch);
508 int total_batches = record_batches.total();
509 set_editing_batch(total_batches - 1);
510 //printf("Record::new_batch 1\n");
511 // Update GUI if created yet
513 record_gui->update_batch_tools();
514 //printf("Record::new_batch 2\n");
518 int Record::current_batch()
520 return record_batches.current_item;
523 int Record::set_current_batch(int i)
525 return record_batches.current_item = i;
528 int Record::editing_batch()
530 return record_batches.editing_item;
533 int Record::set_editing_batch(int i)
535 return record_batches.editing_item = i;
538 int Record::delete_output_file()
540 Batch *batch = get_current_batch();
542 if( !file && batch && !access(batch->asset->path, F_OK) ) {
543 batch->set_notice(_("Deleting"));
544 record_gui->update_batches();
545 Asset *asset = batch->asset;
546 remove_file(asset->path);
547 mwindow->remove_asset_from_caches(asset);
548 delete_index_file(asset);
549 batch->clear_notice();
550 record_gui->update_batches();
555 int Record::open_output_file()
559 Batch *batch = get_current_batch();
560 if( !file && batch ) {
561 delete_output_file();
562 Asset *asset = batch->asset;
563 asset->frame_rate = default_asset->frame_rate;
564 asset->sample_rate = default_asset->sample_rate;
565 asset->width = default_asset->width;
566 asset->height = default_asset->height;
568 if( default_asset->format == FILE_MPEG ) {
569 asset->layers = vdevice ? vdevice->total_video_streams() : 0;
570 asset->channels = adevice ? adevice->total_audio_channels() : 0;
574 result = file->open_file(mwindow->preferences, asset, 0, wr);
576 mwindow->sighandler->push_file(file);
578 file->set_processors(mwindow->preferences->real_processors);
579 record_gui->update_batches();
593 window_lock->lock("Record::start");
594 record_gui->lock_window("Record::start");
595 record_gui->raise_window();
596 record_gui->unlock_window();
597 window_lock->unlock();
605 void Record::start_over()
608 Batch *batch = get_current_batch();
609 if( batch ) batch->start_over();
610 record_gui->update_batches();
613 void Record::stop_operation()
616 stop_input_threads();
619 int Record::cron_active()
621 return !record_thread ? 0 : record_thread->cron_active;
624 void Record::close_output_file()
628 mwindow->sighandler->pull_file(file);
634 record_gui->update_batches();
637 void Record::toggle_label()
639 Batch *batch = get_current_batch();
640 if( batch ) batch->toggle_label(current_display_position());
641 record_gui->update_labels(current_display_position());
644 void Record::clear_labels()
646 Batch *batch = get_current_batch();
647 if( batch ) batch->clear_labels();
648 record_gui->update_labels(0);
651 int Record::get_fragment_samples()
653 const int min_samples = 1024, max_samples = 32768;
654 int samples, low_bit;
655 samples = default_asset->sample_rate / SESSION->record_speed;
656 if( samples < min_samples ) samples = min_samples;
657 else if( samples > max_samples ) samples = max_samples;
658 // loop until only one bit left standing
659 while( (low_bit=(samples & ~(samples-1))) != samples )
664 int Record::get_buffer_samples()
666 int fragment_samples = get_fragment_samples();
667 if( !fragment_samples ) return 0;
668 int fragments = (SESSION->record_write_length+fragment_samples-1) / fragment_samples;
669 if( fragments < 1 ) fragments = 1;
670 return fragments * fragment_samples;
673 Batch* Record::get_current_batch()
675 return record_batches.get_current_batch();
678 Batch* Record::get_editing_batch()
680 return record_batches.get_editing_batch();
683 int Record::get_next_batch(int incr)
685 int i = current_batch();
688 int total_batches = record_batches.total();
689 while( i < total_batches ) {
690 if( record_batches[i]->enabled ) return i;
697 const char* Record::current_mode()
699 Batch *batch = get_current_batch();
700 return batch ? Batch::mode_to_text(batch->record_mode) : "";
703 double Record::current_display_position()
705 //printf("Record::current_display_position "
706 // _LD " " _LD "\n", total_samples, total_frames)
708 Asset *asset = default_asset;
710 if( asset->video_data && record_video )
711 result = (double) written_frames / asset->frame_rate;
712 else if( asset->audio_data && record_audio )
713 result = (double) written_samples / asset->sample_rate;
716 if( asset->video_data && record_video )
717 result = (double) total_frames / asset->frame_rate;
718 else if( asset->audio_data && record_audio )
719 result = (double) total_samples / asset->sample_rate;
721 result = (double) total_time.get_difference() / 1000.;
726 const char* Record::current_source()
728 Batch *batch = get_current_batch();
729 return batch ? batch->get_source_text() : _("Unknown");
732 Asset* Record::current_asset()
734 Batch *batch = get_current_batch();
735 return batch ? batch->asset : 0;
738 Channel *Record::get_current_channel()
740 return current_channel;
743 Channel *Record::get_editing_channel()
745 Batch *batch = get_editing_batch();
746 return batch ? batch->channel : 0;
749 ArrayList<Channel*>* Record::get_video_inputs()
751 return default_asset->video_data && vdevice ? vdevice->get_inputs() : 0;
755 int64_t Record::timer_position()
757 timer_lock->lock("RecordAudio::timer_positioin");
758 int samplerate = default_asset->sample_rate;
759 int64_t result = timer.get_scaled_difference(samplerate);
760 result += session_sample_offset;
761 timer_lock->unlock();
765 void Record::reset_position(int64_t position)
767 timer_lock->lock("RecordAudio::reset_position");
768 session_sample_offset = position;
769 device_sample_offset = adevice ? adevice->current_position() : 0;
771 timer_lock->unlock();
774 void Record::update_position()
776 int64_t position = sync_position();
777 reset_position(position);
778 current_frame = current_sample = 0;
779 audio_time = video_time = -1.;
782 int64_t Record::adevice_position()
784 timer_lock->lock("RecordAudio::adevice_position");
785 int64_t result = adevice->current_position();
786 result += session_sample_offset - device_sample_offset;
787 timer_lock->unlock();
791 int64_t Record::sync_position()
793 int64_t sync_position = -1;
794 double sync_time = -1.;
795 double next_audio_time = -1.;
796 double next_video_time = -1.;
798 if( current_frame == 0 && current_sample == 0 ) {
799 audio_time = video_time = -1.;
802 switch( SESSION->record_positioning ) {
803 case RECORD_POS_TIMESTAMPS:
804 if( default_asset->video_data && record_video )
805 next_video_time = vdevice->get_timestamp();
806 if( default_asset->audio_data && record_audio ) {
808 !adevice->is_monitoring() || writing_file > 0 ?
809 adevice->get_itimestamp() :
810 adevice->get_otimestamp();
812 if( next_audio_time >= 0. )
813 sync_time = next_audio_time;
814 else if( next_video_time > 0. )
815 sync_time = next_video_time;
816 if( sync_time >= 0. ) {
817 sync_position = sync_time * default_asset->sample_rate;
819 if( sync_position >= 0 ) break;
820 case RECORD_POS_DEVICE:
821 if( default_asset->audio_data && record_audio )
822 sync_position = adevice_position();
823 if( sync_position > 0 ) break;
824 case RECORD_POS_SOFTWARE:
825 sync_position = timer_position();
826 if( sync_position > 0 ) break;
827 case RECORD_POS_SAMPLE:
829 sync_position = current_sample;
833 if( next_video_time < 0. )
834 next_video_time = current_frame / default_asset->frame_rate;
835 if( next_video_time > video_time )
836 video_time = next_video_time;
837 if( next_audio_time < 0. )
838 next_audio_time = (double)sync_position / default_asset->sample_rate;
839 if( next_audio_time > audio_time )
840 audio_time = next_audio_time;
842 return sync_position;
846 void Record::adevice_drain()
848 if( adevice && record_audio ) {
849 adevice->stop_audio(0);
850 adevice->start_recording();
855 #define dbmsg if( debug ) printf
856 void Record::resync()
858 dropped = behind = 0;
859 int64_t audio_position = sync_position();
860 double audiotime = (double) audio_position / default_asset->sample_rate;
861 double diff = video_time - audiotime;
863 dbmsg("resync video %f audio %f/%f diff %f",
864 video_time, audiotime, audio_time, diff);
865 if( fabs(diff) > 5. ) {
866 dbmsg(" drain audio");
867 // jam job dvb file tesing, comment next line
868 record_channel->drain_audio();
870 else if( diff > 0. ) {
871 int64_t delay = (int64_t)(1000.0 * diff);
872 dbmsg(" delay %8jd", delay);
874 video_time = audio_time = -1.;
881 behind = (int)(-diff * default_asset->frame_rate);
882 dbmsg(" behind %d", behind);
883 if( behind > 1 && fill_underrun_frames ) {
884 dropped = behind > 3 ? 3 : behind-1;
885 dbmsg(" dropped %d", dropped);
889 int frames = dropped + 1;
890 current_frame += frames;
891 total_frames += frames;
892 record_gui->update_video(dropped, behind);
893 if( dropped > 0 && drop_overrun_frames )
894 if( vdevice ) vdevice->drop_frames(dropped);
898 void Record::close_audio_input()
900 adevice_lock->lock();
902 adevice->close_all();
906 adevice_lock->unlock();
909 void Record::close_video_input()
911 vdevice_lock->lock();
913 vdevice->close_all();
917 vdevice_lock->unlock();
920 void Record::close_input_devices()
926 void Record::stop_audio_thread()
929 record_audio->stop_recording();
937 void Record::stop_video_thread()
940 record_video->stop_recording();
949 void Record::stop_input_threads()
951 pause_lock->lock("Record::stop_input_threads");
956 pause_lock->unlock();
959 void Record::stop_playback()
962 record_monitor->stop_playback();
965 void Record::open_audio_input()
968 adevice_lock->lock();
970 if( default_asset->audio_data )
971 adevice = new AudioDevice(mwindow);
974 int sw_pos = SESSION->record_positioning == RECORD_POS_SOFTWARE;
975 adevice->set_software_positioning(sw_pos);
976 adevice->open_input(SESSION->aconfig_in, SESSION->vconfig_in,
977 default_asset->sample_rate, get_fragment_samples(),
978 default_asset->channels, SESSION->real_time_record);
979 adevice->start_recording();
980 adevice->open_monitor(SESSION->playback_config->aconfig,monitor_audio);
981 adevice->set_vdevice(vdevice);
982 if( vdevice ) vdevice->set_adevice(adevice);
984 adevice_lock->unlock();
987 void Record::open_video_input()
990 vdevice_lock->lock();
991 if( default_asset->video_data )
992 vdevice = new VideoDevice(mwindow);
995 vdevice->set_quality(default_asset->jpeg_quality);
996 vdevice->open_input(SESSION->vconfig_in, video_x, video_y,
997 video_zoom, default_asset->frame_rate);
998 // Get configuration parameters from device probe
999 color_model = vdevice->get_best_colormodel(default_asset);
1000 master_channel->copy_usage(vdevice->channel);
1001 picture->copy_usage(vdevice->picture);
1002 vdevice->set_field_order(reverse_interlace);
1003 vdevice->set_do_cursor(do_cursor, do_big_cursor);
1004 vdevice->set_adevice(adevice);
1005 if( adevice ) adevice->set_vdevice(vdevice);
1006 set_dev_channel(get_current_channel());
1008 vdevice_lock->unlock();
1011 void Record::start_audio_thread()
1014 if( !record_audio ) {
1015 total_samples = current_sample = 0; audio_time = -1.;
1016 record_audio = new RecordAudio(mwindow,this);
1017 record_audio->arm_recording();
1018 record_audio->start_recording();
1023 void Record::start_video_thread()
1026 if( !record_video ) {
1027 total_frames = current_frame = 0; video_time = -1.;
1028 record_video = new RecordVideo(mwindow,this);
1029 record_video->arm_recording();
1030 record_video->start_recording();
1035 void Record::start_input_threads()
1038 input_threads_pausing = 0;
1039 if( default_asset->audio_data )
1040 start_audio_thread();
1041 if( default_asset->video_data )
1042 start_video_thread();
1046 void Record::pause_input_threads()
1048 pause_lock->lock("Record::pause_input_threads");
1049 input_threads_pausing = 1;
1051 adevice_lock->lock();
1052 vdevice_lock->lock();
1054 record_audio->pause_recording();
1056 record_video->pause_recording();
1059 void Record::resume_input_threads()
1062 record_audio->resume_recording();
1064 record_video->resume_recording();
1065 vdevice_lock->unlock();
1066 adevice_lock->unlock();
1067 input_threads_pausing = 0;
1069 pause_lock->unlock();
1073 int Record::start_toc()
1075 if( !mwindow->edl->session->record_realtime_toc ) return 0;
1076 Batch *batch = get_current_batch();
1077 Asset *asset = batch->asset;
1078 char source_filename[BCTEXTLEN], toc_path[BCTEXTLEN];
1079 IndexFile::get_index_filename(source_filename,
1080 mwindow->preferences->index_directory,
1081 toc_path, asset->path,".toc");
1082 if( default_asset->video_data )
1083 return vdevice->start_toc(asset->path, toc_path);
1084 if( default_asset->audio_data )
1085 return adevice->start_toc(asset->path, toc_path);
1089 int Record::start_record(int fd)
1092 if( default_asset->video_data )
1093 return vdevice->start_record(fd);
1094 if( default_asset->audio_data )
1095 return adevice->start_record(fd);
1099 void Record::start_writing_file()
1101 if( !writing_file ) {
1103 written_samples = 0;
1104 do_video = File::renders_video(default_asset);
1105 do_audio = File::renders_audio(default_asset);
1106 if( single_frame ) do_audio = 0;
1107 if( !do_video && single_frame )
1109 else if( !open_output_file() )
1110 writing_file = default_asset->format == FILE_MPEG ? -1 : 1;
1111 if( do_audio && record_audio && writing_file > 0 ) {
1112 int buffer_samples = get_buffer_samples();
1113 record_audio->set_write_buffer_samples(buffer_samples);
1114 file->start_audio_thread(buffer_samples, FILE_RING_BUFFERS);
1116 if( do_video && record_video && writing_file > 0 ) {
1117 int disk_frames = SESSION->video_write_length;
1118 int cmodel = vdevice->get_best_colormodel(default_asset);
1119 int cpress = vdevice->is_compressed(1, 0);
1120 file->start_video_thread(disk_frames, cmodel, FILE_RING_BUFFERS, cpress);
1122 if( writing_file < 0 ) {
1123 int fd = file->record_fd();
1127 if( writing_file ) {
1128 record_gui->start_flash_batch();
1129 if( SESSION->record_sync_drives ) {
1130 drivesync = new DriveSync();
1138 int Record::stop_record()
1140 if( default_asset->video_data )
1141 return vdevice->stop_record();
1142 if( default_asset->audio_data )
1143 return adevice->stop_record();
1147 void Record::flush_buffer()
1150 record_audio->flush_buffer();
1152 record_video->flush_buffer();
1155 void Record::stop_writing_file()
1157 record_gui->stop_flash_batch();
1158 if( !writing_file ) return;
1159 if( writing_file > 0 )
1161 if( writing_file < 0 )
1163 close_output_file();
1165 Asset *asset = current_asset();
1166 asset->audio_length = written_samples;
1167 asset->video_length = written_frames;
1168 record_gui->flash_batch();
1170 drivesync->done = 1;
1176 void Record::stop_writing()
1178 if( writing_file ) {
1179 pause_input_threads();
1180 stop_writing_file();
1181 resume_input_threads();
1186 void Record::start_cron_thread()
1188 if( cron_active() < 0 ) {
1189 delete record_thread;
1192 if( !record_thread ) {
1193 record_thread = new RecordThread(mwindow,this);
1194 record_thread->start();
1195 record_gui->disable_batch_buttons();
1196 record_gui->update_cron_status(_("Running"));
1200 void Record::stop_cron_thread(const char *msg)
1202 if( record_thread ) {
1203 delete record_thread;
1205 record_gui->enable_batch_buttons();
1206 record_gui->update_cron_status(msg);
1210 void Record::set_power_off(int value)
1213 record_gui->power_off->update(value);
1216 void Record::set_video_picture()
1218 if( default_asset->video_data && vdevice )
1219 vdevice->set_picture(picture);
1222 void Record::set_do_cursor()
1224 vdevice->set_do_cursor(do_cursor, do_big_cursor);
1227 void Record::set_translation(int x, int y)
1231 if(default_asset->video_data && vdevice)
1232 vdevice->set_translation(video_x, video_y);
1235 int Record::set_channel(Channel *channel)
1237 if( !channel ) return 1;
1238 printf("set_channel %s\n",channel->title);
1239 if( record_channel->set(channel) ) return 1;
1240 RecordMonitorGUI *window = record_monitor->window;
1241 window->lock_window("Record::set_channel");
1242 window->channel_picker->channel_text->update(channel->title);
1243 window->unlock_window();
1247 int Record::set_channel_no(int chan_no)
1249 if( chan_no < 0 || chan_no >= channeldb->size() ) return 1;
1250 Channel *channel = channeldb->get(chan_no);
1251 return set_channel(channel);
1254 int Record::channel_down()
1256 Channel *channel = get_current_channel();
1257 if( !channel || !channeldb->size() ) return 1;
1258 int n = channeldb->number_of(channel);
1259 if( n < 0 ) return 1;
1260 if( --n < 0 ) n = channeldb->size() - 1;
1261 return set_channel_no(n);
1264 int Record::channel_up()
1266 Channel *channel = get_current_channel();
1267 if( !channel || !channeldb->size() ) return 1;
1268 int n = channeldb->number_of(channel);
1269 if( n < 0 ) return 1;
1270 if( ++n >= channeldb->size() ) n = 0;
1271 return set_channel_no(n);
1274 int Record::set_channel_name(const char *name)
1276 Channel *channel = 0;
1277 int ch = 0, nch = channeldb->size();
1279 channel = channeldb->get(ch);
1280 if( channel && !channel->cstrcmp(name) ) break;
1283 if( ch >= nch || !channel ) return 1;
1284 return set_channel(channel);
1287 void Record::set_batch_channel_no(int chan_no)
1289 Channel *channel = channeldb->get(chan_no);
1291 Batch *batch = get_editing_batch();
1292 if( batch ) batch->channel = channel;
1293 record_gui->batch_source->update(channel->title);
1297 void Record::set_dev_channel(Channel *channel)
1299 // should be tuner device, not vdevice
1300 if( channel && vdevice &&
1301 (!this->channel || *channel != *this->channel) ) {
1302 current_channel = channel;
1303 if( this->channel ) delete this->channel;
1304 this->channel = new Channel(channel);
1305 vdevice->set_channel(channel);
1306 int strk = !SESSION->decode_subtitles ? -1 : SESSION->subtitle_number;
1307 vdevice->set_captioning(strk);
1308 set_video_picture();
1309 printf("new channel %s, has_signal=%d\n",channel->title,vdevice->has_signal());
1312 total_time.update();
1316 /* widget holds xlock and set_channel pauses rendering, deadlock */
1317 /* use a background thread to update channel after widget sets it */
1318 RecordChannel::RecordChannel(Record *record)
1321 this->record = record;
1322 change_channel = new Condition(0,"RecordSetChannel::change_channel");
1323 channel_lock = new Condition(1,"Record::channel_lock",1);
1328 RecordChannel::~RecordChannel()
1331 change_channel->unlock();
1333 delete change_channel;
1334 delete channel_lock;
1337 int RecordChannel::set(Channel *channel)
1339 if( !channel || new_channel ) return 1;
1340 new_channel = channel;
1341 channel_lock->lock(); // block has_signal
1342 change_channel->unlock(); // resume channel changer thread
1346 void RecordChannel::drain_audio()
1348 if( !audio_drain ) {
1350 change_channel->unlock();
1354 void RecordChannel::run()
1358 change_channel->lock();
1360 if( !new_channel && !audio_drain ) continue;
1361 record->pause_input_threads();
1363 record->set_dev_channel(new_channel);
1364 record->record_gui->reset_video();
1365 record->record_gui->reset_audio();
1370 record->adevice_drain();
1372 record->update_position();
1373 record->resume_input_threads();
1376 channel_lock->unlock();
1381 int Record::has_signal()
1383 record_channel->channel_lock->lock("Record::has_signal");
1384 record_channel->channel_lock->unlock();
1385 vdevice_lock->lock();
1386 int result = vdevice ? vdevice->has_signal() : 0;
1387 vdevice_lock->unlock();
1391 int Record::create_channeldb(ArrayList<Channel*> *channeldb)
1393 return vdevice ? vdevice->create_channeldb(channeldb) : 1;
1397 void Record::set_audio_monitoring(int mode)
1400 monitor_audio = mode;
1402 record_audio->set_monitoring(mode);
1403 record_gui->lock_window("Record::set_audio_monitoring");
1404 if( record_gui->monitor_audio )
1405 record_gui->monitor_audio->update(mode);
1406 record_gui->unlock_window();
1409 void Record::set_audio_metering(int mode)
1411 metering_audio = mode;
1412 record_gui->lock_window("Record::set_audio_metering");
1413 if( record_gui->meter_audio )
1414 record_gui->meter_audio->update(mode);
1415 record_gui->unlock_window();
1416 RecordMonitorGUI *window = record_monitor->window;
1417 window->lock_window("Record::set_audio_metering 1");
1418 window->resize_event(window->get_w(),window->get_h());
1419 window->unlock_window();
1423 void Record::set_video_monitoring(int mode)
1425 monitor_video = mode;
1426 record_gui->lock_window("Record::set_video_monitoring");
1427 if( record_gui->monitor_video )
1428 record_gui->monitor_video->update(mode);
1429 record_gui->flush();
1430 record_gui->unlock_window();
1433 int Record::get_time_format()
1435 return SESSION->time_format;
1438 int Record::set_record_mode(int value)
1440 Batch *batch = get_editing_batch();
1441 batch->record_mode = value;
1442 record_gui->update_batches();
1446 int Record::check_batch_complete()
1450 if( writing_file && cron_active() > 0 ) {
1451 Batch *batch = get_current_batch();
1452 if( batch && batch->recorded > 0 &&
1453 batch->record_mode == RECORD_TIMED &&
1454 current_display_position() >= batch->record_duration ) {
1455 batch->recorded = -1;
1457 record_gui->update_batches();
1458 record_thread->batch_timed_lock->unlock();
1462 batch_lock->unlock();
1467 int Record::skimming(void *vp, int track)
1469 return ((Record*)vp)->skimming(track);
1472 int Record::skimming(int track)
1474 int64_t framenum; uint8_t *tdat; int mw, mh;
1475 if( !vdevice ) return -1;
1476 if( vdevice->get_thumbnail(track, framenum, tdat, mw, mh) ) return 1;
1477 int pid, width, height; double framerate;
1478 if( vdevice->get_video_info(track, pid, framerate, width, height, 0) ) return 1;
1479 if( !framerate ) return 1;
1480 return skim_thread->skim(pid,framenum,framerate, tdat,mw,mh,width,height);
1483 void Record::start_skimming()
1485 if( commercial_check && vdevice && !skimming_active ) {
1486 skimming_active = 1;
1487 skim_thread->start(mwindow->commercials);
1488 vdevice->set_skimming(0, 0, skimming, this);
1492 void Record::stop_skimming()
1494 if( skimming_active && vdevice ) {
1495 skimming_active = 0;
1496 vdevice->set_skimming(0, 0, 0, 0);
1497 skim_thread->stop();
1501 void Record::update_skimming(int v)
1503 if( (commercial_check=v) != 0 )
1509 RecordRemoteHandler::RecordRemoteHandler(RemoteControl *remote_control)
1510 : RemoteHandler(remote_control->gui, GREEN)
1514 RecordRemoteHandler::~RecordRemoteHandler()
1519 display_video_text(int x, int y, const char *text, int font,
1520 int bg_color, int color, int alpha, double secs, double scale)
1522 // gotta have a window for resources
1523 record_monitor->window->
1524 display_video_text(x, y, text, font,
1525 bg_color, color, alpha, secs, scale);
1529 display_cut_icon(int x, int y)
1531 VFrame *cut_icon = *mwindow->theme->get_image_set("commercial");
1533 x += cut_icon->get_w()*scale; y += cut_icon->get_h()*scale;
1534 display_vframe(cut_icon, x, y, 200, 1.0, scale);
1538 DeviceDVBInput *Record::
1541 DeviceDVBInput *dvb_dev = !vdevice ? 0 :
1542 (DeviceDVBInput *)vdevice->mpeg_device();
1543 if( !dvb_dev ) dvb_dev = !adevice ? 0 :
1544 (DeviceDVBInput *)adevice->mpeg_device();
1552 display_vframe(VFrame *in, int x, int y, int alpha, double secs, double scale)
1554 if( !channel ) return;
1555 if( !vdevice || vdevice->in_config->driver != CAPTURE_DVB ) return;
1556 DeviceDVBInput *dvb_input = dvb_device();
1557 if( !dvb_input ) return 1;
1558 zmpeg3_t *fd = dvb_input->get_src();
1560 scale *= fd->video_height(channel->video_stream) / 1080.;
1561 int ww = in->get_w() * scale, hh = in->get_h() * scale;
1562 VFrame out(ww, hh, BC_YUV444P);
1563 BC_WindowBase::get_cmodels()->transfer(out.get_rows(), in->get_rows(),
1564 out.get_y(), out.get_u(), out.get_v(),
1565 in->get_y(), in->get_u(), in->get_v(),
1566 0, 0, in->get_w(), in->get_h(),
1567 0, 0, out.get_w(), out.get_h(),
1568 in->get_color_model(), out.get_color_model(), 0,
1569 in->get_bytes_per_line(), ww);
1570 int sz = ww * hh; uint8_t *yuv = out.get_data(), aimg[sz];
1571 uint8_t *yp = yuv, *up = yuv+sz, *vp = yuv+2*sz, *ap = 0;
1572 if( alpha > 0 ) memset(ap=aimg, alpha, sz);
1573 else if( alpha < 0 ) ap = yp;
1574 fd->display_subtitle(channel->video_stream, 1, 1,
1575 yp,up,vp,ap, x,y,ww,hh, 0, secs*1000);
1576 dvb_input->put_src();
1582 if( !channel ) return;
1583 if( !vdevice || vdevice->in_config->driver != CAPTURE_DVB ) return;
1584 DeviceDVBInput *dvb_input = dvb_device();
1585 if( !dvb_input ) return 1;
1586 zmpeg3_t *fd = dvb_input->get_src();
1588 fd->delete_subtitle(channel->video_stream, 1, 1);
1589 dvb_input->put_src();
1595 display_vframe(VFrame *in, int x, int y, int alpha, double secs, double scale)
1597 record_monitor->display_vframe(in, x, y, alpha, secs, scale);
1603 record_monitor->undisplay_vframe();
1609 display_channel_info()
1612 if( !channel ) return 1;
1613 if( !vdevice || vdevice->in_config->driver != CAPTURE_DVB ) return 1;
1614 DeviceDVBInput *dvb_input = dvb_device();
1615 if( !dvb_input ) return 1;
1616 int elem = channel->element;
1617 time_t tt; time(&tt);
1618 struct tm ttm; localtime_r(&tt,&ttm);
1620 char text[BCTEXTLEN];
1621 zmpeg3_t *fd = dvb_input->get_src();
1623 for( int ord=0, i=0; ord<0x80; ) {
1625 int len = fd->dvb.get_chan_info(elem,ord,i++,cp,BCTEXTLEN-2);
1626 if( len < 0 ) { ++ord; i = 0; continue; }
1627 struct tm stm = ttm, etm = ttm;
1628 char wday[4], *bp = cp; cp += len;
1629 if( sscanf(bp, "%02d:%02d:%02d-%02d:%02d:%02d",
1630 &stm.tm_hour, &stm.tm_min, &stm.tm_sec,
1631 &etm.tm_hour, &etm.tm_min, &etm.tm_sec) != 6 ) continue;
1632 while( bp<cp && *bp++!='\n' );
1633 if( sscanf(bp, "(%3s) %04d/%02d/%02d", &wday[0],
1634 &stm.tm_year, &stm.tm_mon, &stm.tm_mday) != 4 ) continue;
1635 stm.tm_year -= 1900; stm.tm_mon -= 1;
1636 etm.tm_year = stm.tm_year;
1637 etm.tm_mon = stm.tm_mon;
1638 etm.tm_mday = stm.tm_mday;
1639 time_t st = mktime(&stm), et = mktime(&etm);
1640 if( et < st ) et += 24*3600;
1641 if( tt < st || tt >= et ) continue;
1642 int major=0, minor=0;
1643 fd->dvb.get_channel(elem, major, minor);
1644 char chan[64]; sprintf(chan, "%3d.%1d", major, minor);
1645 for( i=0; i<5 && chan[i]!=0; ++i ) *bp++ = chan[i];
1646 while( bp<cp && *bp++!='\n' );
1648 for( char *lp=bp; bp<cp; ++bp ) {
1649 if( *bp == '\n' || ((bp-lp)>=60 && *bp==' ') ) {
1650 if( ++n >= 10 ) break;
1655 while( bp > text && *--bp == '\n' ) *bp = 0;
1659 dvb_input->put_src();
1661 display_video_text(20, 20, text,
1662 BIGFONT, WHITE, BLACK, 0, 3., 1.);
1670 display_channel_schedule()
1673 if( !channel ) return 1;
1674 if( !vdevice || vdevice->in_config->driver != CAPTURE_DVB ) return 1;
1675 DeviceDVBInput *dvb_input = dvb_device();
1676 if( !dvb_input ) return 1;
1677 int elem = channel->element;
1678 time_t tt; time(&tt);
1679 struct tm ttm; localtime_r(&tt,&ttm);
1680 char text[BCTEXTLEN];
1681 zmpeg3_t *fd = dvb_input->get_src();
1683 RecordSchedule channel_schedule;
1684 for( int ord=0, i=0; ord<0x80; ) {
1686 int len = fd->dvb.get_chan_info(elem,ord,i++,cp,BCTEXTLEN-2);
1687 if( len < 0 ) { ++ord; i = 0; continue; }
1688 struct tm stm = ttm, etm = ttm;
1689 char wday[4], *bp = cp; cp += len;
1690 if( sscanf(bp, "%02d:%02d:%02d-%02d:%02d:%02d",
1691 &stm.tm_hour, &stm.tm_min, &stm.tm_sec,
1692 &etm.tm_hour, &etm.tm_min, &etm.tm_sec) != 6 ) continue;
1693 while( bp<cp && *bp++!='\n' );
1694 if( sscanf(bp, "(%3s) %04d/%02d/%02d", &wday[0],
1695 &stm.tm_year, &stm.tm_mon, &stm.tm_mday) != 4 ) continue;
1696 stm.tm_year -= 1900; stm.tm_mon -= 1;
1697 etm.tm_year = stm.tm_year;
1698 etm.tm_mon = stm.tm_mon;
1699 etm.tm_mday = stm.tm_mday;
1700 time_t st = mktime(&stm), et = mktime(&etm);
1701 if( et < st ) et += 24*3600;
1702 if( tt >= et ) continue;
1703 if( bp > text )*--bp = 0;
1704 channel_schedule.append(new RecordScheduleItem(st, text));
1706 dvb_input->put_src();
1708 channel_schedule.sort_times();
1709 char *cp = text, *ep = cp+sizeof(text)-1;
1710 for( int i=0, n=0; i<channel_schedule.size(); ++i ) {
1711 RecordScheduleItem *item = channel_schedule.get(i);
1712 for( char *bp=item->title; cp<ep && *bp!=0; ++bp ) *cp++ = *bp;
1713 if( cp < ep ) *cp++ = '\n';
1714 if( ++n >= 12 ) break;
1717 while( cp > text && *--cp == '\n' ) *cp = 0;
1719 display_video_text(20, 20, text,
1720 BIGFONT, WHITE, BLACK, 0, 3., 1.);
1727 void Record::clear_keybfr()
1733 void Record::add_key(int ch)
1736 while( n<(int)sizeof(keybfr)-2 && keybfr[n] ) ++n;
1737 keybfr[n++] = ch; keybfr[n] = 0;
1738 display_video_text(20, 20, keybfr,
1739 BIGFONT, WHITE, BLACK, 0, 3., 2.);
1742 int RecordRemoteHandler::remote_process_key(RemoteControl *remote_control, int key)
1744 Record *record = remote_control->mwindow_gui->record;
1745 return record->remote_process_key(remote_control, key);
1748 int Record::remote_process_key(RemoteControl *remote_control, int key)
1754 if( last_key == KPENTER ) {
1755 set_channel_name(keybfr);
1759 ch = '.'; // fall through
1760 case '0': if( last_key == '0' && ch == '0' ) {
1764 case '1': case '2': case '3': case '4':
1765 case '5': case '6': case '7': case '8': case '9':
1768 //case UP: case DOWN: case LEFT: case RIGHT:
1769 //case KPPLAY: case KPBACK: case KPFORW:
1770 case KPRECD: case 'c': // start capture, mark endpoint
1772 start_commercial_capture();
1774 set_play_gain(0.075);
1777 mark_commercial_capture(DEL_MARK);
1778 blink_status->update();
1780 display_cut_icon(10,20);
1782 case KPSTOP: case 'd': // end capture, start cutting
1783 remote_control->set_color(YELLOW);
1784 stop_commercial_capture(1);
1786 case KPAUSE: case 'x': // ignore current commercial
1787 mark_commercial_capture(DEL_SKIP);
1789 case KPBACK: case 'a': // toggle mute audio
1790 if( !monitor_audio ) { set_mute_gain(1); set_play_gain(1); }
1791 set_audio_monitoring(monitor_audio ? 0 : 1);
1793 case 'm': // toggle metering audio
1794 set_audio_metering(metering_audio ? 0 : 1);
1796 case KPPLAY: case 's': // ignore previous endpoint
1797 mark_commercial_capture(DEL_OOPS);
1799 case KPFWRD: case KPSLASH:
1800 display_channel_info();
1802 case KPMAXW: case KPSTAR:
1803 display_channel_schedule();
1805 case KPCHUP: case KPPLUS:
1808 case KPCHDN: case KPMINUS:
1812 Canvas *canvas = record_monitor->window->canvas;
1813 if( !canvas->get_fullscreen() )
1814 canvas->start_fullscreen();
1816 canvas->stop_fullscreen();
1827 int Record::start_commercial_capture()
1829 if( deletions != 0 ) return 1;
1830 Channel *channel = get_current_channel();
1831 if( !channel ) return 1;
1832 int track = channel->video_stream;
1833 int pid = vdevice->get_video_pid(track);
1834 if( pid < 0 ) return 1;
1835 time_t st; time(&st);
1836 struct tm stm; localtime_r(&st, &stm);
1837 char file_path[BCTEXTLEN];
1838 sprintf(file_path,"/tmp/del%04d%02d%02d-%02d%02d%02d.ts",
1839 stm.tm_year+1900, stm.tm_mon+1, stm.tm_mday,
1840 stm.tm_hour, stm.tm_min, stm.tm_sec);
1841 commercial_fd = open(file_path,O_RDWR+O_CREAT,0666);
1842 if( commercial_fd < 0 ) return 1;
1843 if( vdevice->start_record(commercial_fd, 0x800000) ) {
1844 close(commercial_fd);
1848 commercial_start_time = st;
1849 printf("del to %s\n", file_path);
1850 deletions = new Deletions(pid, file_path);
1851 mark_commercial_capture(DEL_START);
1852 blink_status->start();
1856 int Record::mark_commercial_capture(int action)
1858 if( deletions == 0 ) return 1;
1859 double time = vdevice->get_timestamp();
1860 printf("dele %f\n", time);
1861 deletions->append(new Dele(time, action));
1865 void Record::remote_fill_color(int color)
1867 mwindow->gui->remote_control->fill_color(color);
1870 void Record::set_status_color(int color)
1872 status_color = color;
1873 remote_fill_color(status_color);
1876 void Record::set_mute_gain(double gain)
1878 gain = (mute_gain = gain) * play_gain;
1879 if( adevice ) adevice->set_play_gain(gain);
1882 void Record::set_play_gain(double gain)
1884 gain = mute_gain * (play_gain = gain);
1885 if( adevice ) adevice->set_play_gain(gain);
1888 int Record::stop_commercial_capture(int run_job)
1890 if( deletions == 0 ) return 1;
1891 if( run_job ) blink_status->stop();
1894 vdevice->stop_record();
1895 commercial_start_time = -1;
1896 if( commercial_fd >= 0 ) {
1897 close(commercial_fd);
1902 char del_filename[BCTEXTLEN];
1903 strcpy(del_filename, deletions->file_path());
1904 strcpy(strrchr(del_filename, '.'),".del");
1905 if( !deletions->write_dels(del_filename) ) {
1906 int pid = spawn("cutads %s",del_filename);
1908 cut_pids.append(pid);
1909 cutads_status->start_waiting();
1915 remove_file(deletions->filepath);
1917 delete deletions; deletions = 0;
1922 spawn(const char *fmt, ...)
1924 const char *exec_path = File::get_cinlib_path();
1925 char cmd[BCTEXTLEN], *cp = cmd, *ep = cp+sizeof(cmd)-1;
1926 va_list ap; va_start(ap, fmt);
1927 cp += snprintf(cp, ep-cp, "exec %s/", exec_path);
1928 cp += vsnprintf(cp, ep-cp, fmt, ap); va_end(ap);
1930 pid_t pid = vfork();
1931 if( pid < 0 ) return -1;
1932 if( pid > 0 ) return pid;
1933 char *const argv[4] = { (char*) "/bin/sh", (char*) "-c", cmd, 0 };
1934 execvp(argv[0], &argv[0]);
1941 for( int i=0; i<cut_pids.size(); ) {
1942 int status, pid = cut_pids.get(i);
1943 if( waitpid(pid, &status, WNOHANG) ) {
1944 cut_pids.remove(pid);
1949 return cut_pids.size();
1953 RecordCutAdsStatus::
1954 RecordCutAdsStatus(Record *record)
1957 this->record = record;
1958 wait_lock = new Condition(0,"RecordCutAdsStatus::wait_lock",0);
1963 RecordCutAdsStatus::
1964 ~RecordCutAdsStatus()
1968 wait_lock->unlock();
1975 void RecordCutAdsStatus::
1978 wait_lock->unlock();
1981 void RecordCutAdsStatus::
1984 int status, pgrp = getpgrp();
1986 wait_lock->lock("RecordCutAdsStatus::run");
1988 if( !record->commercial_jobs() ) continue;
1989 record->set_status_color(YELLOW);
1992 waitpid(-pgrp, &status, 0);
1994 if( !record->commercial_jobs() ) break;
1996 record->set_status_color(GREEN);
2000 void RecordBlinkStatus::
2001 remote_color(int color)
2003 record->remote_fill_color(color);
2006 void RecordBlinkStatus::
2012 void RecordBlinkStatus::
2020 void RecordBlinkStatus::
2031 RecordBlinkStatus(Record *record)
2034 this->record = record;
2038 ~RecordBlinkStatus()
2043 void RecordBlinkStatus::
2048 remote_color(color);
2052 color ^= YELLOW ^ BLUE;
2053 if( timer.get_difference() > 10*60*1000 ) { // 10 minites
2054 record->stop_commercial_capture(0);
2058 remote_color(record->status_color);