4 * Copyright (C) 2008-2013 Adam Williams <broadcast at earthling dot net>
5 * Copyright (C) 2003-2016 Cinelerra CV contributors
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "bccapture.h"
26 #include "bcsignals.h"
28 #include "channeldb.h"
29 #include "chantables.h"
31 #include "edlsession.h"
40 #include "mwindowgui.h"
42 #include "playbackconfig.h"
43 #include "playbackengine.h"
44 #include "preferences.h"
45 #include "recordconfig.h"
46 #include "recordgui.h"
47 #include "recordmonitor.h"
50 #include "vdevice1394.h"
52 #include "vdevicedvb.h"
53 #include "vdevicev4l2.h"
54 #include "vdevicev4l2jpeg.h"
55 #include "vdevicev4l2mpeg.h"
56 #include "vdevicex11.h"
57 #include "videoconfig.h"
58 #include "videodevice.h"
59 #include "videowindow.h"
60 #include "videowindowgui.h"
67 KeepaliveThread::KeepaliveThread(VideoDevice *device)
74 this->device = device;
76 startup_lock = new Mutex("KeepaliveThread::startup_lock");
79 KeepaliveThread::~KeepaliveThread()
84 int KeepaliveThread::start_keepalive()
86 startup_lock->lock("KeepaliveThread::start_keepalive 1");
88 startup_lock->lock("KeepaliveThread::start_keepalive 2");
89 startup_lock->unlock();
93 void KeepaliveThread::run()
95 startup_lock->unlock();
99 // Give the capture a moment
100 // Should fix the delay in case users want slower frame rates.
101 timer.delay((long)(KEEPALIVE_DELAY * 1000));
103 // See if a capture happened
104 if(still_alive == 0 && capturing)
106 // printf("KeepaliveThread::run: device crashed\n");
114 int KeepaliveThread::reset_keepalive()
120 int KeepaliveThread::get_failed()
122 if(failed) return 1; else return 0;
125 int KeepaliveThread::stop()
129 // Force an immediate exit even if capture_frame worked.
141 VideoDevice::VideoDevice(MWindow *mwindow)
143 this->mwindow = mwindow;
144 in_config = new VideoInConfig;
145 out_config = new VideoOutConfig;
146 channel = new Channel;
147 picture = new PictureConfig();
148 sharing_lock = new Mutex("VideoDevice::sharing_lock");
149 channel_lock = new Mutex("VideoDevice::channel_lock");
150 picture_lock = new Mutex("VideoDevice::picture_lock");
155 VideoDevice::~VideoDevice()
157 input_sources.remove_all_objects();
167 int VideoDevice::initialize()
171 sharing_lock->reset();
175 is_playing_back = is_recording = 0;
185 in_config_updated = 0;
201 int VideoDevice::open_input(VideoInConfig *config,
209 *this->in_config = *config;
212 this->input_z = -1; // Force initialization.
213 this->frame_rate = frame_rate;
214 if( input_base ) return 1; // device already open
216 switch(in_config->driver) {
217 #ifdef HAVE_VIDEO4LINUX2
220 case VIDEO4LINUX2JPEG:
221 case VIDEO4LINUX2MPEG:
222 case CAPTURE_JPEG_WEBCAM:
223 case CAPTURE_YUYV_WEBCAM:
229 this->input_x = input_x;
230 this->input_y = input_y;
233 case CAPTURE_FIREWIRE:
234 case CAPTURE_IEC61883:
248 if( !input_base ) return 1;
249 result = input_base->open_input();
250 if(!result) capturing = 1;
251 in_config_updated = 0;
255 VDeviceBase* VideoDevice::new_device_base()
257 switch(in_config->driver) {
258 #ifdef HAVE_VIDEO4LINUX2
260 case CAPTURE_JPEG_WEBCAM:
261 case CAPTURE_YUYV_WEBCAM:
262 return input_base = new VDeviceV4L2(this);
263 case VIDEO4LINUX2JPEG:
264 return input_base = new VDeviceV4L2JPEG(this);
265 case VIDEO4LINUX2MPEG:
266 return input_base = new VDeviceV4L2MPEG(this);
270 return input_base = new VDeviceX11(this, 0);
273 case CAPTURE_FIREWIRE:
274 case CAPTURE_IEC61883:
275 return input_base = new VDevice1394(this);
280 return input_base = new VDeviceDVB(this);
286 static const char* get_channeldb_path(VideoInConfig *vconfig_in)
289 switch(vconfig_in->driver)
292 case CAPTURE_JPEG_WEBCAM:
293 case CAPTURE_YUYV_WEBCAM:
294 path = (char*)"channels_v4l2";
296 case VIDEO4LINUX2JPEG:
297 path = (char*)"channels_v4l2jpeg";
299 case VIDEO4LINUX2MPEG:
300 path = (char*)"channels_v4l2mpeg";
303 path = (char*)"channels_dvb";
309 void VideoDevice::load_channeldb(ChannelDB *channeldb, VideoInConfig *vconfig_in)
311 channeldb->load(get_channeldb_path(vconfig_in));
314 void VideoDevice::save_channeldb(ChannelDB *channeldb, VideoInConfig *vconfig_in)
316 channeldb->save(get_channeldb_path(vconfig_in));
320 VDeviceBase* VideoDevice::get_input_base()
325 VDeviceBase* VideoDevice::get_output_base()
330 DeviceMPEGInput *VideoDevice::mpeg_device()
332 return !input_base ? 0 : input_base->mpeg_device();
335 int VideoDevice::is_compressed(int driver, int use_file, int use_fixed)
337 // FileMOV needs to have write_frames called so the start codes get scanned.
338 return ((driver == VIDEO4LINUX2JPEG && use_fixed) ||
339 (driver == CAPTURE_JPEG_WEBCAM && use_fixed) ||
340 driver == CAPTURE_FIREWIRE ||
341 driver == CAPTURE_IEC61883);
344 int VideoDevice::is_compressed(int use_file, int use_fixed)
346 return is_compressed(in_config->driver, use_file, use_fixed);
350 void VideoDevice::fix_asset(Asset *asset, int driver)
352 // Fix asset using legacy routine
353 const char *vcodec = 0;
356 case CAPTURE_IEC61883:
357 case CAPTURE_FIREWIRE:
358 vcodec = CODEC_TAG_DVSD;
361 case VIDEO4LINUX2JPEG:
362 vcodec = CODEC_TAG_MJPEG;
365 case CAPTURE_JPEG_WEBCAM:
366 vcodec = CODEC_TAG_JPEG;
370 asset->format = FILE_FFMPEG;
371 strcpy(asset->vcodec, vcodec);
375 // Fix asset using inherited routine
378 if(input_base) input_base->fix_asset(asset);
384 const char* VideoDevice::drivertostr(int driver)
387 case PLAYBACK_X11: return PLAYBACK_X11_TITLE;
388 case PLAYBACK_X11_XV: return PLAYBACK_X11_XV_TITLE;
389 case PLAYBACK_X11_GL: return PLAYBACK_X11_GL_TITLE;
390 case VIDEO4LINUX2: return VIDEO4LINUX2_TITLE;
391 case VIDEO4LINUX2JPEG: return VIDEO4LINUX2JPEG_TITLE;
392 case VIDEO4LINUX2MPEG: return VIDEO4LINUX2MPEG_TITLE;
393 case CAPTURE_JPEG_WEBCAM: return CAPTURE_JPEG_WEBCAM_TITLE;
394 case CAPTURE_YUYV_WEBCAM: return CAPTURE_YUYV_WEBCAM_TITLE;
395 case SCREENCAPTURE: return SCREENCAPTURE_TITLE;
396 case CAPTURE_DVB: return CAPTURE_DVB_TITLE;
398 case CAPTURE_FIREWIRE: return CAPTURE_FIREWIRE_TITLE;
399 case CAPTURE_IEC61883: return CAPTURE_IEC61883_TITLE;
405 int VideoDevice::get_best_colormodel(Asset *asset)
407 return input_base ? input_base->get_best_colormodel(asset) : BC_RGB888;
410 int VideoDevice::drop_frames(int frames)
412 return input_base ? input_base->drop_frames(frames) : 1;
415 double VideoDevice::device_timestamp()
417 return input_base ? input_base->device_timestamp() : -1.;
420 double VideoDevice::get_timestamp()
425 int VideoDevice::close_all()
429 output_base->close_all();
438 input_base->close_all();
448 input_sources.remove_all_objects();
454 int VideoDevice::set_adevice(AudioDevice *adev)
461 ArrayList<Channel*>* VideoDevice::get_inputs()
463 return &input_sources;
466 Channel* VideoDevice::new_input_source(char *device_name)
468 for( int i=0; i<input_sources.total; ++i ) {
469 if(!strcmp(input_sources.values[i]->device_name, device_name))
470 return input_sources.values[i];
472 Channel *item = new Channel;
473 strcpy(item->device_name, device_name);
474 input_sources.append(item);
478 int VideoDevice::get_failed()
480 return keepalive ? keepalive->get_failed() : 0;
483 int VideoDevice::interrupt_crash()
485 return input_base ? input_base->interrupt_crash() : 0;
488 int VideoDevice::set_translation(int in_x, int in_y)
495 int VideoDevice::set_field_order(int odd_field_first)
497 this->odd_field_first = odd_field_first;
501 void VideoDevice::set_do_cursor(int do_cursor, int do_big_cursor)
503 int cursor_scale = 0;
513 this->do_cursor = cursor_scale;
516 int VideoDevice::set_channel(Channel *channel)
520 channel_lock->lock("VideoDevice::set_channel");
521 this->channel->copy_settings(channel);
523 channel_lock->unlock();
524 result = input_base ? input_base->set_channel(channel) :
525 output_base ? output_base->set_channel(channel) :
531 int VideoDevice::set_captioning(int mode)
534 input_base->set_captioning(mode);
538 void VideoDevice::set_quality(int quality)
540 this->quality = quality;
543 void VideoDevice::set_cpus(int cpus)
548 int VideoDevice::set_picture(PictureConfig *picture)
551 picture_lock->lock("VideoDevice::set_picture");
552 this->picture->copy_settings(picture);
554 picture_lock->unlock();
556 if(input_base) return input_base->set_picture(picture);
561 int VideoDevice::update_translation()
563 float frame_in_capture_x1f, frame_in_capture_x2f, frame_in_capture_y1f, frame_in_capture_y2f;
564 float capture_in_frame_x1f, capture_in_frame_x2f, capture_in_frame_y1f, capture_in_frame_y2f;
568 input_x = new_input_x;
569 input_y = new_input_y;
570 if( in_config->driver == VIDEO4LINUX2 ) {
571 if(input_z != new_input_z) {
572 input_z = new_input_z;
575 capture_w = (int)((float)in_config->w * input_z + 0.5);
576 capture_h = (int)((float)in_config->h * input_z + 0.5);
578 // Need to align to multiple of 4
583 frame_in_capture_x1f = (float)input_x * input_z + capture_w / 2 - in_config->w / 2;
584 frame_in_capture_x2f = (float)input_x * input_z + capture_w / 2 + in_config->w / 2;
585 frame_in_capture_y1f = (float)input_y * input_z + capture_h / 2 - in_config->h / 2;
586 frame_in_capture_y2f = (float)input_y * input_z + capture_h / 2 + in_config->h / 2;
588 capture_in_frame_x1f = 0;
589 capture_in_frame_y1f = 0;
590 capture_in_frame_x2f = in_config->w;
591 capture_in_frame_y2f = in_config->h;
593 if(frame_in_capture_x1f < 0) { capture_in_frame_x1f -= frame_in_capture_x1f; frame_in_capture_x1f = 0; }
594 if(frame_in_capture_y1f < 0) { capture_in_frame_y1f -= frame_in_capture_y1f; frame_in_capture_y1f = 0; }
595 if(frame_in_capture_x2f > capture_w) { capture_in_frame_x2f -= frame_in_capture_x2f - capture_w; frame_in_capture_x2f = capture_w; }
596 if(frame_in_capture_y2f > capture_h) { capture_in_frame_y2f -= frame_in_capture_y2f - capture_h; frame_in_capture_y2f = capture_h; }
598 frame_in_capture_x1 = (int)frame_in_capture_x1f;
599 frame_in_capture_y1 = (int)frame_in_capture_y1f;
600 frame_in_capture_x2 = (int)frame_in_capture_x2f;
601 frame_in_capture_y2 = (int)frame_in_capture_y2f;
603 capture_in_frame_x1 = (int)capture_in_frame_x1f;
604 capture_in_frame_y1 = (int)capture_in_frame_y1f;
605 capture_in_frame_x2 = (int)capture_in_frame_x2f;
606 capture_in_frame_y2 = (int)capture_in_frame_y2f;
614 int VideoDevice::set_latency_counter(int value)
616 latency_counter = value;
620 int VideoDevice::has_signal()
622 return input_base ? input_base->has_signal() : 0;
625 int VideoDevice::create_channeldb(ArrayList<Channel*> *channeldb)
627 return input_base ? input_base->create_channeldb(channeldb) : 1;
631 int VideoDevice::read_buffer(VFrame *frame)
634 if(!capturing) return 0;
636 //printf("VideoDevice::read_buffer %p %p\n", frame, input_base);
638 // Reset the keepalive thread
639 if(keepalive) keepalive->capturing = 1;
640 result = input_base->read_buffer(frame);
641 timestamp = frame->get_timestamp();
642 if( timestamp < 0 ) {
643 struct timeval tv; gettimeofday(&tv, 0);
644 timestamp = tv.tv_sec + tv.tv_usec / 1000000.0;
647 keepalive->capturing = 0;
648 keepalive->reset_keepalive();
657 // ================================= OUTPUT ==========================================
660 int VideoDevice::open_output(VideoOutConfig *config, float rate,
661 int out_w, int out_h, Canvas *output, int single_frame)
664 //printf("VideoDevice::open_output 1 %d\n", out_config->driver);
665 *this->out_config = *config;
666 //printf("VideoDevice::open_output 1 %d\n", out_config->driver);
670 this->single_frame = single_frame;
672 //printf("VideoDevice::open_output 1 %d\n", out_config->driver);
673 switch(out_config->driver) {
675 case PLAYBACK_X11_XV:
676 case PLAYBACK_X11_GL:
677 output_base = new VDeviceX11(this, output);
681 case PLAYBACK_DV1394:
682 case PLAYBACK_FIREWIRE:
683 case PLAYBACK_IEC61883:
684 output_base = new VDevice1394(this);
688 //printf("VideoDevice::open_output 2 %d\n", out_config->driver);
690 if(output_base->open_output()) {
694 //printf("VideoDevice::open_output 3 %d\n", out_config->driver);
695 return output_base ? 0 : 1;
700 int VideoDevice::start_playback()
702 // arm buffer before doing this
705 return output_base ? output_base->start_playback() : 1;
708 int VideoDevice::stop_playback()
710 if( output_base ) output_base->stop_playback();
716 void VideoDevice::goose_input()
718 if(input_base) input_base->goose_input();
721 void VideoDevice::new_output_buffer(VFrame **output, int colormodel, EDL *edl)
723 if(!output_base) return;
724 output_base->new_output_buffer(output, colormodel, edl);
725 int clr_color = edl->session->cwindow_clear_color;
726 (*output)->set_clear_color(clr_color, 0xff);
730 int VideoDevice::interrupt_playback()
736 int VideoDevice::write_buffer(VFrame *output, EDL *edl)
738 return output_base ? output_base->write_buffer(output, edl) : 1;
741 int VideoDevice::output_visible()
743 return output_base ? output_base->output_visible() : 0;
746 BC_Bitmap* VideoDevice::get_bitmap()
748 return output_base ? output_base->get_bitmap() : 0;
752 int VideoDevice::set_cloexec_flag(int desc, int value)
754 int oldflags = fcntl(desc, F_GETFD, 0);
755 if( oldflags < 0 ) return oldflags;
757 oldflags |= FD_CLOEXEC;
759 oldflags &= ~FD_CLOEXEC;
760 return fcntl(desc, F_SETFD, oldflags);
764 void VideoDevice::auto_update(double rate, int width, int height)
766 if( !in_config || !in_config->follow_video ) return;
770 in_config_updated = 1;
773 int VideoDevice::config_updated()
775 if( !in_config || !in_config->follow_video ) return 0;
776 return in_config_updated;
779 void VideoDevice::config_update()
781 in_config_updated = 0;
782 VideoInConfig *vconfig_in = mwindow->edl->session->vconfig_in;
783 vconfig_in->w = capture_w;
784 vconfig_in->h = capture_h;
785 vconfig_in->in_framerate = frame_rate;
789 int VideoDevice::start_toc(const char *path, const char *toc_path)
791 return input_base ? input_base->start_toc(path, toc_path) : -1;
794 int VideoDevice::start_record(int fd, int bsz)
796 return input_base ? input_base->start_record(fd, bsz) : -1;
799 int VideoDevice::stop_record()
801 return input_base ? input_base->stop_record() : -1;
805 int VideoDevice::total_video_streams()
807 return input_base ? input_base->total_video_streams() : 0;
812 int VideoDevice::get_video_pid(int track)
814 return !input_base ? -1 : input_base->get_video_pid(track);
818 int VideoDevice::get_video_info(int track, int &pid,
819 double &framerate, int &width, int &height, char *title)
821 return !input_base ? 1 : input_base->
822 get_video_info(track, pid, framerate, width, height, title);
825 int VideoDevice::get_thumbnail(int stream, int64_t &position,
826 unsigned char *&thumbnail, int &ww, int &hh)
828 return !input_base ? 1 :
829 input_base->get_thumbnail(stream, position, thumbnail, ww, hh);
832 int VideoDevice::set_skimming(int track, int skim, skim_fn fn, void *vp)
834 return !input_base ? 1 :
835 input_base->set_skimming(track, skim, fn, vp);