4 * Copyright (C) 2008-2013 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "bccapture.h"
25 #include "bcsignals.h"
27 #include "channeldb.h"
28 #include "chantables.h"
30 #include "edlsession.h"
32 #include "../hvirtual_config.h"
38 #include "mwindowgui.h"
40 #include "playbackconfig.h"
41 #include "playbackengine.h"
42 #include "preferences.h"
43 #include "recordconfig.h"
44 #include "recordgui.h"
45 #include "recordmonitor.h"
47 #include "vdevice1394.h"
48 #include "vdevicebuz.h"
49 #include "vdevicedvb.h"
50 #include "vdevicev4l.h"
51 #include "vdevicev4l2.h"
52 #include "vdevicev4l2jpeg.h"
53 #include "vdevicev4l2mpeg.h"
54 #include "vdevicex11.h"
55 #include "videoconfig.h"
56 #include "videodevice.h"
57 #include "videowindow.h"
58 #include "videowindowgui.h"
64 KeepaliveThread::KeepaliveThread(VideoDevice *device)
71 this->device = device;
73 startup_lock = new Mutex("KeepaliveThread::startup_lock");
76 KeepaliveThread::~KeepaliveThread()
81 int KeepaliveThread::start_keepalive()
83 startup_lock->lock("KeepaliveThread::start_keepalive 1");
85 startup_lock->lock("KeepaliveThread::start_keepalive 2");
86 startup_lock->unlock();
90 void KeepaliveThread::run()
92 startup_lock->unlock();
96 // Give the capture a moment
97 // Should fix the delay in case users want slower frame rates.
98 timer.delay((long)(KEEPALIVE_DELAY * 1000));
100 // See if a capture happened
101 if(still_alive == 0 && capturing)
103 // printf("KeepaliveThread::run: device crashed\n");
111 int KeepaliveThread::reset_keepalive()
117 int KeepaliveThread::get_failed()
119 if(failed) return 1; else return 0;
122 int KeepaliveThread::stop()
126 // Force an immediate exit even if capture_frame worked.
138 VideoDevice::VideoDevice(MWindow *mwindow)
140 this->mwindow = mwindow;
141 in_config = new VideoInConfig;
142 out_config = new VideoOutConfig;
143 channel = new Channel;
144 picture = new PictureConfig();
145 sharing_lock = new Mutex("VideoDevice::sharing_lock");
146 channel_lock = new Mutex("VideoDevice::channel_lock");
147 picture_lock = new Mutex("VideoDevice::picture_lock");
152 VideoDevice::~VideoDevice()
154 input_sources.remove_all_objects();
164 int VideoDevice::initialize()
168 sharing_lock->reset();
172 is_playing_back = is_recording = 0;
182 in_config_updated = 0;
196 int VideoDevice::open_input(VideoInConfig *config,
204 *this->in_config = *config;
207 this->input_z = -1; // Force initialization.
208 this->frame_rate = frame_rate;
209 if( input_base ) return 1; // device already open
211 switch(in_config->driver) {
213 keepalive = new KeepaliveThread(this);
214 keepalive->start_keepalive();
218 #ifdef HAVE_VIDEO4LINUX2
221 case VIDEO4LINUX2JPEG:
222 case VIDEO4LINUX2MPEG:
223 case CAPTURE_JPEG_WEBCAM:
224 case CAPTURE_YUYV_WEBCAM:
230 this->input_x = input_x;
231 this->input_y = input_y;
235 //printf("VideoDevice 1\n");
236 keepalive = new KeepaliveThread(this);
237 keepalive->start_keepalive();
240 case CAPTURE_FIREWIRE:
241 case CAPTURE_IEC61883:
255 if( !input_base ) return 1;
256 result = input_base->open_input();
257 if(!result) capturing = 1;
258 in_config_updated = 0;
262 VDeviceBase* VideoDevice::new_device_base()
264 switch(in_config->driver) {
265 #ifdef HAVE_VIDEO4LINUX
267 return input_base = new VDeviceV4L(this);
270 #ifdef HAVE_VIDEO4LINUX2
272 case CAPTURE_JPEG_WEBCAM:
273 case CAPTURE_YUYV_WEBCAM:
274 return input_base = new VDeviceV4L2(this);
275 case VIDEO4LINUX2JPEG:
276 return input_base = new VDeviceV4L2JPEG(this);
277 case VIDEO4LINUX2MPEG:
278 return input_base = new VDeviceV4L2MPEG(this);
282 return input_base = new VDeviceX11(this, 0);
284 #ifdef HAVE_VIDEO4LINUX
286 return input_base = new VDeviceBUZ(this);
288 return input_base = new VDeviceLML(this);
292 case CAPTURE_FIREWIRE:
293 case CAPTURE_IEC61883:
294 return input_base = new VDevice1394(this);
299 return input_base = new VDeviceDVB(this);
305 static char* get_channeldb_path(VideoInConfig *vconfig_in)
308 switch(vconfig_in->driver)
311 path = (char*)"channels_v4l";
314 case CAPTURE_JPEG_WEBCAM:
315 case CAPTURE_YUYV_WEBCAM:
316 path = (char*)"channels_v4l2";
318 case VIDEO4LINUX2JPEG:
319 path = (char*)"channels_v4l2jpeg";
321 case VIDEO4LINUX2MPEG:
322 path = (char*)"channels_v4l2mpeg";
325 path = (char*)"channels_buz";
328 path = (char*)"channels_dvb";
334 void VideoDevice::load_channeldb(ChannelDB *channeldb, VideoInConfig *vconfig_in)
336 channeldb->load(get_channeldb_path(vconfig_in));
339 void VideoDevice::save_channeldb(ChannelDB *channeldb, VideoInConfig *vconfig_in)
341 channeldb->save(get_channeldb_path(vconfig_in));
345 VDeviceBase* VideoDevice::get_input_base()
350 VDeviceBase* VideoDevice::get_output_base()
355 DeviceMPEGInput *VideoDevice::mpeg_device()
357 return !input_base ? 0 : input_base->mpeg_device();
360 int VideoDevice::is_compressed(int driver, int use_file, int use_fixed)
362 // FileMOV needs to have write_frames called so the start codes get scanned.
363 return ((driver == CAPTURE_BUZ && use_fixed) ||
364 (driver == VIDEO4LINUX2JPEG && use_fixed) ||
365 (driver == CAPTURE_JPEG_WEBCAM && use_fixed) ||
366 driver == CAPTURE_LML ||
367 driver == CAPTURE_FIREWIRE ||
368 driver == CAPTURE_IEC61883);
371 int VideoDevice::is_compressed(int use_file, int use_fixed)
373 return is_compressed(in_config->driver, use_file, use_fixed);
377 void VideoDevice::fix_asset(Asset *asset, int driver)
379 // Fix asset using legacy routine
380 const char *vcodec = 0;
382 case CAPTURE_IEC61883:
383 case CAPTURE_FIREWIRE:
384 vcodec = CODEC_TAG_DVSD;
389 case VIDEO4LINUX2JPEG:
390 vcodec = CODEC_TAG_MJPEG;
393 case CAPTURE_JPEG_WEBCAM:
394 vcodec = CODEC_TAG_JPEG;
398 asset->format = FILE_FFMPEG;
399 strcpy(asset->vcodec, vcodec);
403 // Fix asset using inherited routine
406 if(input_base) input_base->fix_asset(asset);
412 const char* VideoDevice::drivertostr(int driver)
415 case PLAYBACK_X11: return PLAYBACK_X11_TITLE;
416 case PLAYBACK_X11_XV: return PLAYBACK_X11_XV_TITLE;
417 case PLAYBACK_X11_GL: return PLAYBACK_X11_GL_TITLE;
418 case PLAYBACK_BUZ: return PLAYBACK_BUZ_TITLE;
419 case VIDEO4LINUX: return VIDEO4LINUX_TITLE;
420 case VIDEO4LINUX2: return VIDEO4LINUX2_TITLE;
421 case VIDEO4LINUX2JPEG: return VIDEO4LINUX2JPEG_TITLE;
422 case VIDEO4LINUX2MPEG: return VIDEO4LINUX2MPEG_TITLE;
423 case CAPTURE_JPEG_WEBCAM: return CAPTURE_JPEG_WEBCAM_TITLE;
424 case CAPTURE_YUYV_WEBCAM: return CAPTURE_YUYV_WEBCAM_TITLE;
425 case SCREENCAPTURE: return SCREENCAPTURE_TITLE;
426 case CAPTURE_BUZ: return CAPTURE_BUZ_TITLE;
427 case CAPTURE_LML: return CAPTURE_LML_TITLE;
428 case CAPTURE_DVB: return CAPTURE_DVB_TITLE;
429 case CAPTURE_FIREWIRE: return CAPTURE_FIREWIRE_TITLE;
430 case CAPTURE_IEC61883: return CAPTURE_IEC61883_TITLE;
435 int VideoDevice::get_best_colormodel(Asset *asset)
437 return input_base ? input_base->get_best_colormodel(asset) : BC_RGB888;
440 int VideoDevice::drop_frames(int frames)
442 return input_base ? input_base->drop_frames(frames) : 1;
445 double VideoDevice::device_timestamp()
447 return input_base ? input_base->device_timestamp() : -1.;
450 double VideoDevice::get_timestamp()
455 int VideoDevice::close_all()
459 output_base->close_all();
468 input_base->close_all();
478 input_sources.remove_all_objects();
484 int VideoDevice::set_adevice(AudioDevice *adev)
491 ArrayList<Channel*>* VideoDevice::get_inputs()
493 return &input_sources;
496 Channel* VideoDevice::new_input_source(char *device_name)
498 for( int i=0; i<input_sources.total; ++i ) {
499 if(!strcmp(input_sources.values[i]->device_name, device_name))
500 return input_sources.values[i];
502 Channel *item = new Channel;
503 strcpy(item->device_name, device_name);
504 input_sources.append(item);
508 int VideoDevice::get_failed()
510 return keepalive ? keepalive->get_failed() : 0;
513 int VideoDevice::interrupt_crash()
515 return input_base ? input_base->interrupt_crash() : 0;
518 int VideoDevice::set_translation(int in_x, int in_y)
525 int VideoDevice::set_field_order(int odd_field_first)
527 this->odd_field_first = odd_field_first;
531 int VideoDevice::set_channel(Channel *channel)
535 channel_lock->lock("VideoDevice::set_channel");
536 this->channel->copy_settings(channel);
538 channel_lock->unlock();
539 result = input_base ? input_base->set_channel(channel) :
540 output_base ? output_base->set_channel(channel) :
546 int VideoDevice::set_captioning(int mode)
549 input_base->set_captioning(mode);
553 void VideoDevice::set_quality(int quality)
555 this->quality = quality;
558 void VideoDevice::set_cpus(int cpus)
563 int VideoDevice::set_picture(PictureConfig *picture)
566 picture_lock->lock("VideoDevice::set_picture");
567 this->picture->copy_settings(picture);
569 picture_lock->unlock();
571 if(input_base) return input_base->set_picture(picture);
576 int VideoDevice::update_translation()
578 float frame_in_capture_x1f, frame_in_capture_x2f, frame_in_capture_y1f, frame_in_capture_y2f;
579 float capture_in_frame_x1f, capture_in_frame_x2f, capture_in_frame_y1f, capture_in_frame_y2f;
583 input_x = new_input_x;
584 input_y = new_input_y;
585 if( in_config->driver == VIDEO4LINUX ||
586 in_config->driver == VIDEO4LINUX2 ) {
587 if(input_z != new_input_z) {
588 input_z = new_input_z;
591 capture_w = (int)((float)in_config->w * input_z + 0.5);
592 capture_h = (int)((float)in_config->h * input_z + 0.5);
594 // Need to align to multiple of 4
599 frame_in_capture_x1f = (float)input_x * input_z + capture_w / 2 - in_config->w / 2;
600 frame_in_capture_x2f = (float)input_x * input_z + capture_w / 2 + in_config->w / 2;
601 frame_in_capture_y1f = (float)input_y * input_z + capture_h / 2 - in_config->h / 2;
602 frame_in_capture_y2f = (float)input_y * input_z + capture_h / 2 + in_config->h / 2;
604 capture_in_frame_x1f = 0;
605 capture_in_frame_y1f = 0;
606 capture_in_frame_x2f = in_config->w;
607 capture_in_frame_y2f = in_config->h;
609 if(frame_in_capture_x1f < 0) { capture_in_frame_x1f -= frame_in_capture_x1f; frame_in_capture_x1f = 0; }
610 if(frame_in_capture_y1f < 0) { capture_in_frame_y1f -= frame_in_capture_y1f; frame_in_capture_y1f = 0; }
611 if(frame_in_capture_x2f > capture_w) { capture_in_frame_x2f -= frame_in_capture_x2f - capture_w; frame_in_capture_x2f = capture_w; }
612 if(frame_in_capture_y2f > capture_h) { capture_in_frame_y2f -= frame_in_capture_y2f - capture_h; frame_in_capture_y2f = capture_h; }
614 frame_in_capture_x1 = (int)frame_in_capture_x1f;
615 frame_in_capture_y1 = (int)frame_in_capture_y1f;
616 frame_in_capture_x2 = (int)frame_in_capture_x2f;
617 frame_in_capture_y2 = (int)frame_in_capture_y2f;
619 capture_in_frame_x1 = (int)capture_in_frame_x1f;
620 capture_in_frame_y1 = (int)capture_in_frame_y1f;
621 capture_in_frame_x2 = (int)capture_in_frame_x2f;
622 capture_in_frame_y2 = (int)capture_in_frame_y2f;
630 int VideoDevice::set_latency_counter(int value)
632 latency_counter = value;
636 int VideoDevice::has_signal()
638 return input_base ? input_base->has_signal() : 0;
641 int VideoDevice::create_channeldb(ArrayList<Channel*> *channeldb)
643 return input_base ? input_base->create_channeldb(channeldb) : 1;
647 int VideoDevice::read_buffer(VFrame *frame)
650 if(!capturing) return 0;
652 //printf("VideoDevice::read_buffer %p %p\n", frame, input_base);
654 // Reset the keepalive thread
655 if(keepalive) keepalive->capturing = 1;
656 result = input_base->read_buffer(frame);
657 timestamp = frame->get_timestamp();
658 if( timestamp < 0 ) {
659 struct timeval tv; gettimeofday(&tv, 0);
660 timestamp = tv.tv_sec + tv.tv_usec / 1000000.0;
663 keepalive->capturing = 0;
664 keepalive->reset_keepalive();
673 // ================================= OUTPUT ==========================================
676 int VideoDevice::open_output(VideoOutConfig *config, float rate,
677 int out_w, int out_h, Canvas *output, int single_frame)
680 //printf("VideoDevice::open_output 1 %d\n", out_config->driver);
681 *this->out_config = *config;
682 //printf("VideoDevice::open_output 1 %d\n", out_config->driver);
686 this->single_frame = single_frame;
688 //printf("VideoDevice::open_output 1 %d\n", out_config->driver);
689 switch(out_config->driver) {
690 #ifdef HAVE_VIDEO4LINUX
692 output_base = new VDeviceBUZ(this);
695 output_base = new VDeviceLML(this);
699 case PLAYBACK_X11_XV:
700 case PLAYBACK_X11_GL:
701 output_base = new VDeviceX11(this, output);
704 case PLAYBACK_DV1394:
705 case PLAYBACK_FIREWIRE:
706 case PLAYBACK_IEC61883:
707 output_base = new VDevice1394(this);
710 //printf("VideoDevice::open_output 2 %d\n", out_config->driver);
712 if(output_base->open_output()) {
716 //printf("VideoDevice::open_output 3 %d\n", out_config->driver);
717 return output_base ? 0 : 1;
722 int VideoDevice::start_playback()
724 // arm buffer before doing this
727 return output_base ? output_base->start_playback() : 1;
730 int VideoDevice::stop_playback()
732 if( output_base ) output_base->stop_playback();
738 void VideoDevice::goose_input()
740 if(input_base) input_base->goose_input();
743 void VideoDevice::new_output_buffer(VFrame **output, int colormodel)
745 if(!output_base) return;
746 output_base->new_output_buffer(output, colormodel);
750 int VideoDevice::interrupt_playback()
756 int VideoDevice::write_buffer(VFrame *output, EDL *edl)
758 return output_base ? output_base->write_buffer(output, edl) : 1;
761 int VideoDevice::output_visible()
763 return output_base ? output_base->output_visible() : 0;
766 BC_Bitmap* VideoDevice::get_bitmap()
768 return output_base ? output_base->get_bitmap() : 0;
772 int VideoDevice::set_cloexec_flag(int desc, int value)
774 int oldflags = fcntl(desc, F_GETFD, 0);
775 if( oldflags < 0 ) return oldflags;
777 oldflags |= FD_CLOEXEC;
779 oldflags &= ~FD_CLOEXEC;
780 return fcntl(desc, F_SETFD, oldflags);
784 void VideoDevice::auto_update(double rate, int width, int height)
786 if( !in_config || !in_config->follow_video ) return;
790 in_config_updated = 1;
793 int VideoDevice::config_updated()
795 if( !in_config || !in_config->follow_video ) return 0;
796 return in_config_updated;
799 void VideoDevice::config_update()
801 in_config_updated = 0;
802 VideoInConfig *vconfig_in = mwindow->edl->session->vconfig_in;
803 vconfig_in->w = capture_w;
804 vconfig_in->h = capture_h;
805 vconfig_in->in_framerate = frame_rate;
809 int VideoDevice::start_toc(const char *path, const char *toc_path)
811 return input_base ? input_base->start_toc(path, toc_path) : -1;
814 int VideoDevice::start_record(int fd, int bsz)
816 return input_base ? input_base->start_record(fd, bsz) : -1;
819 int VideoDevice::stop_record()
821 return input_base ? input_base->stop_record() : -1;
825 int VideoDevice::total_video_streams()
827 return input_base ? input_base->total_video_streams() : 0;
832 int VideoDevice::get_video_pid(int track)
834 return !input_base ? -1 : input_base->get_video_pid(track);
838 int VideoDevice::get_video_info(int track, int &pid,
839 double &framerate, int &width, int &height, char *title)
841 return !input_base ? 1 : input_base->
842 get_video_info(track, pid, framerate, width, height, title);
845 int VideoDevice::get_thumbnail(int stream, int64_t &position,
846 unsigned char *&thumbnail, int &ww, int &hh)
848 return !input_base ? 1 :
849 input_base->get_thumbnail(stream, position, thumbnail, ww, hh);
852 int VideoDevice::set_skimming(int track, int skim, skim_fn fn, void *vp)
854 return !input_base ? 1 :
855 input_base->set_skimming(track, skim, fn, vp);