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"
39 #include "mwindowgui.h"
41 #include "playbackconfig.h"
42 #include "playbackengine.h"
43 #include "preferences.h"
44 #include "recordconfig.h"
45 #include "recordgui.h"
46 #include "recordmonitor.h"
49 #include "vdevice1394.h"
51 #include "vdevicedvb.h"
52 #include "vdevicev4l2.h"
53 #include "vdevicev4l2jpeg.h"
54 #include "vdevicev4l2mpeg.h"
55 #include "vdevicex11.h"
56 #include "videoconfig.h"
57 #include "videodevice.h"
58 #include "videowindow.h"
59 #include "videowindowgui.h"
66 KeepaliveThread::KeepaliveThread(VideoDevice *device)
73 this->device = device;
75 startup_lock = new Mutex("KeepaliveThread::startup_lock");
78 KeepaliveThread::~KeepaliveThread()
83 int KeepaliveThread::start_keepalive()
85 startup_lock->lock("KeepaliveThread::start_keepalive 1");
87 startup_lock->lock("KeepaliveThread::start_keepalive 2");
88 startup_lock->unlock();
92 void KeepaliveThread::run()
94 startup_lock->unlock();
98 // Give the capture a moment
99 // Should fix the delay in case users want slower frame rates.
100 timer.delay((long)(KEEPALIVE_DELAY * 1000));
102 // See if a capture happened
103 if(still_alive == 0 && capturing)
105 // printf("KeepaliveThread::run: device crashed\n");
113 int KeepaliveThread::reset_keepalive()
119 int KeepaliveThread::get_failed()
121 if(failed) return 1; else return 0;
124 int KeepaliveThread::stop()
128 // Force an immediate exit even if capture_frame worked.
140 VideoDevice::VideoDevice(MWindow *mwindow)
142 this->mwindow = mwindow;
143 in_config = new VideoInConfig;
144 out_config = new VideoOutConfig;
145 channel = new Channel;
146 picture = new PictureConfig();
147 sharing_lock = new Mutex("VideoDevice::sharing_lock");
148 channel_lock = new Mutex("VideoDevice::channel_lock");
149 picture_lock = new Mutex("VideoDevice::picture_lock");
154 VideoDevice::~VideoDevice()
156 input_sources.remove_all_objects();
166 int VideoDevice::initialize()
170 sharing_lock->reset();
174 is_playing_back = is_recording = 0;
184 in_config_updated = 0;
200 int VideoDevice::open_input(VideoInConfig *config,
208 *this->in_config = *config;
211 this->input_z = -1; // Force initialization.
212 this->frame_rate = frame_rate;
213 if( input_base ) return 1; // device already open
215 switch(in_config->driver) {
216 #ifdef HAVE_VIDEO4LINUX2
219 case VIDEO4LINUX2JPEG:
220 case VIDEO4LINUX2MPEG:
221 case CAPTURE_JPEG_WEBCAM:
222 case CAPTURE_YUYV_WEBCAM:
228 this->input_x = input_x;
229 this->input_y = input_y;
232 case CAPTURE_FIREWIRE:
233 case CAPTURE_IEC61883:
247 if( !input_base ) return 1;
248 result = input_base->open_input();
249 if(!result) capturing = 1;
250 in_config_updated = 0;
254 VDeviceBase* VideoDevice::new_device_base()
256 switch(in_config->driver) {
257 #ifdef HAVE_VIDEO4LINUX2
259 case CAPTURE_JPEG_WEBCAM:
260 case CAPTURE_YUYV_WEBCAM:
261 return input_base = new VDeviceV4L2(this);
262 case VIDEO4LINUX2JPEG:
263 return input_base = new VDeviceV4L2JPEG(this);
264 case VIDEO4LINUX2MPEG:
265 return input_base = new VDeviceV4L2MPEG(this);
269 return input_base = new VDeviceX11(this, 0);
272 case CAPTURE_FIREWIRE:
273 case CAPTURE_IEC61883:
274 return input_base = new VDevice1394(this);
279 return input_base = new VDeviceDVB(this);
285 static const char* get_channeldb_path(VideoInConfig *vconfig_in)
288 switch(vconfig_in->driver)
291 case CAPTURE_JPEG_WEBCAM:
292 case CAPTURE_YUYV_WEBCAM:
293 path = (char*)"channels_v4l2";
295 case VIDEO4LINUX2JPEG:
296 path = (char*)"channels_v4l2jpeg";
298 case VIDEO4LINUX2MPEG:
299 path = (char*)"channels_v4l2mpeg";
302 path = (char*)"channels_dvb";
308 void VideoDevice::load_channeldb(ChannelDB *channeldb, VideoInConfig *vconfig_in)
310 channeldb->load(get_channeldb_path(vconfig_in));
313 void VideoDevice::save_channeldb(ChannelDB *channeldb, VideoInConfig *vconfig_in)
315 channeldb->save(get_channeldb_path(vconfig_in));
319 VDeviceBase* VideoDevice::get_input_base()
324 VDeviceBase* VideoDevice::get_output_base()
329 DeviceMPEGInput *VideoDevice::mpeg_device()
331 return !input_base ? 0 : input_base->mpeg_device();
334 int VideoDevice::is_compressed(int driver, int use_file, int use_fixed)
336 // FileMOV needs to have write_frames called so the start codes get scanned.
337 return ((driver == VIDEO4LINUX2JPEG && use_fixed) ||
338 (driver == CAPTURE_JPEG_WEBCAM && use_fixed) ||
339 driver == CAPTURE_FIREWIRE ||
340 driver == CAPTURE_IEC61883);
343 int VideoDevice::is_compressed(int use_file, int use_fixed)
345 return is_compressed(in_config->driver, use_file, use_fixed);
349 void VideoDevice::fix_asset(Asset *asset, int driver)
351 // Fix asset using legacy routine
352 const char *vcodec = 0;
355 case CAPTURE_IEC61883:
356 case CAPTURE_FIREWIRE:
357 vcodec = CODEC_TAG_DVSD;
360 case VIDEO4LINUX2JPEG:
361 vcodec = CODEC_TAG_MJPEG;
364 case CAPTURE_JPEG_WEBCAM:
365 vcodec = CODEC_TAG_JPEG;
369 asset->format = FILE_FFMPEG;
370 strcpy(asset->vcodec, vcodec);
374 // Fix asset using inherited routine
377 if(input_base) input_base->fix_asset(asset);
383 const char* VideoDevice::drivertostr(int driver)
386 case PLAYBACK_X11: return PLAYBACK_X11_TITLE;
387 case PLAYBACK_X11_XV: return PLAYBACK_X11_XV_TITLE;
388 case PLAYBACK_X11_GL: return PLAYBACK_X11_GL_TITLE;
389 case VIDEO4LINUX2: return VIDEO4LINUX2_TITLE;
390 case VIDEO4LINUX2JPEG: return VIDEO4LINUX2JPEG_TITLE;
391 case VIDEO4LINUX2MPEG: return VIDEO4LINUX2MPEG_TITLE;
392 case CAPTURE_JPEG_WEBCAM: return CAPTURE_JPEG_WEBCAM_TITLE;
393 case CAPTURE_YUYV_WEBCAM: return CAPTURE_YUYV_WEBCAM_TITLE;
394 case SCREENCAPTURE: return SCREENCAPTURE_TITLE;
395 case CAPTURE_DVB: return CAPTURE_DVB_TITLE;
397 case CAPTURE_FIREWIRE: return CAPTURE_FIREWIRE_TITLE;
398 case CAPTURE_IEC61883: return CAPTURE_IEC61883_TITLE;
404 int VideoDevice::get_best_colormodel(Asset *asset)
406 return input_base ? input_base->get_best_colormodel(asset) : BC_RGB888;
409 int VideoDevice::drop_frames(int frames)
411 return input_base ? input_base->drop_frames(frames) : 1;
414 double VideoDevice::device_timestamp()
416 return input_base ? input_base->device_timestamp() : -1.;
419 double VideoDevice::get_timestamp()
424 int VideoDevice::close_all()
428 output_base->close_all();
437 input_base->close_all();
447 input_sources.remove_all_objects();
453 int VideoDevice::set_adevice(AudioDevice *adev)
460 ArrayList<Channel*>* VideoDevice::get_inputs()
462 return &input_sources;
465 Channel* VideoDevice::new_input_source(char *device_name)
467 for( int i=0; i<input_sources.total; ++i ) {
468 if(!strcmp(input_sources.values[i]->device_name, device_name))
469 return input_sources.values[i];
471 Channel *item = new Channel;
472 strcpy(item->device_name, device_name);
473 input_sources.append(item);
477 int VideoDevice::get_failed()
479 return keepalive ? keepalive->get_failed() : 0;
482 int VideoDevice::interrupt_crash()
484 return input_base ? input_base->interrupt_crash() : 0;
487 int VideoDevice::set_translation(int in_x, int in_y)
494 int VideoDevice::set_field_order(int odd_field_first)
496 this->odd_field_first = odd_field_first;
500 void VideoDevice::set_do_cursor(int do_cursor, int do_big_cursor)
502 int cursor_scale = 0;
512 this->do_cursor = cursor_scale;
515 int VideoDevice::set_channel(Channel *channel)
519 channel_lock->lock("VideoDevice::set_channel");
520 this->channel->copy_settings(channel);
522 channel_lock->unlock();
523 result = input_base ? input_base->set_channel(channel) :
524 output_base ? output_base->set_channel(channel) :
530 int VideoDevice::set_captioning(int mode)
533 input_base->set_captioning(mode);
537 void VideoDevice::set_quality(int quality)
539 this->quality = quality;
542 void VideoDevice::set_cpus(int cpus)
547 int VideoDevice::set_picture(PictureConfig *picture)
550 picture_lock->lock("VideoDevice::set_picture");
551 this->picture->copy_settings(picture);
553 picture_lock->unlock();
555 if(input_base) return input_base->set_picture(picture);
560 int VideoDevice::update_translation()
562 float frame_in_capture_x1f, frame_in_capture_x2f, frame_in_capture_y1f, frame_in_capture_y2f;
563 float capture_in_frame_x1f, capture_in_frame_x2f, capture_in_frame_y1f, capture_in_frame_y2f;
567 input_x = new_input_x;
568 input_y = new_input_y;
569 if( in_config->driver == VIDEO4LINUX2 ) {
570 if(input_z != new_input_z) {
571 input_z = new_input_z;
574 capture_w = (int)((float)in_config->w * input_z + 0.5);
575 capture_h = (int)((float)in_config->h * input_z + 0.5);
577 // Need to align to multiple of 4
582 frame_in_capture_x1f = (float)input_x * input_z + capture_w / 2 - in_config->w / 2;
583 frame_in_capture_x2f = (float)input_x * input_z + capture_w / 2 + in_config->w / 2;
584 frame_in_capture_y1f = (float)input_y * input_z + capture_h / 2 - in_config->h / 2;
585 frame_in_capture_y2f = (float)input_y * input_z + capture_h / 2 + in_config->h / 2;
587 capture_in_frame_x1f = 0;
588 capture_in_frame_y1f = 0;
589 capture_in_frame_x2f = in_config->w;
590 capture_in_frame_y2f = in_config->h;
592 if(frame_in_capture_x1f < 0) { capture_in_frame_x1f -= frame_in_capture_x1f; frame_in_capture_x1f = 0; }
593 if(frame_in_capture_y1f < 0) { capture_in_frame_y1f -= frame_in_capture_y1f; frame_in_capture_y1f = 0; }
594 if(frame_in_capture_x2f > capture_w) { capture_in_frame_x2f -= frame_in_capture_x2f - capture_w; frame_in_capture_x2f = capture_w; }
595 if(frame_in_capture_y2f > capture_h) { capture_in_frame_y2f -= frame_in_capture_y2f - capture_h; frame_in_capture_y2f = capture_h; }
597 frame_in_capture_x1 = (int)frame_in_capture_x1f;
598 frame_in_capture_y1 = (int)frame_in_capture_y1f;
599 frame_in_capture_x2 = (int)frame_in_capture_x2f;
600 frame_in_capture_y2 = (int)frame_in_capture_y2f;
602 capture_in_frame_x1 = (int)capture_in_frame_x1f;
603 capture_in_frame_y1 = (int)capture_in_frame_y1f;
604 capture_in_frame_x2 = (int)capture_in_frame_x2f;
605 capture_in_frame_y2 = (int)capture_in_frame_y2f;
613 int VideoDevice::set_latency_counter(int value)
615 latency_counter = value;
619 int VideoDevice::has_signal()
621 return input_base ? input_base->has_signal() : 0;
624 int VideoDevice::create_channeldb(ArrayList<Channel*> *channeldb)
626 return input_base ? input_base->create_channeldb(channeldb) : 1;
630 int VideoDevice::read_buffer(VFrame *frame)
633 if(!capturing) return 0;
635 //printf("VideoDevice::read_buffer %p %p\n", frame, input_base);
637 // Reset the keepalive thread
638 if(keepalive) keepalive->capturing = 1;
639 result = input_base->read_buffer(frame);
640 timestamp = frame->get_timestamp();
641 if( timestamp < 0 ) {
642 struct timeval tv; gettimeofday(&tv, 0);
643 timestamp = tv.tv_sec + tv.tv_usec / 1000000.0;
646 keepalive->capturing = 0;
647 keepalive->reset_keepalive();
656 // ================================= OUTPUT ==========================================
659 int VideoDevice::open_output(VideoOutConfig *config, float rate,
660 int out_w, int out_h, Canvas *output, int single_frame)
663 //printf("VideoDevice::open_output 1 %d\n", out_config->driver);
664 *this->out_config = *config;
665 //printf("VideoDevice::open_output 1 %d\n", out_config->driver);
669 this->single_frame = single_frame;
671 //printf("VideoDevice::open_output 1 %d\n", out_config->driver);
672 switch(out_config->driver) {
674 case PLAYBACK_X11_XV:
675 case PLAYBACK_X11_GL:
676 output_base = new VDeviceX11(this, output);
680 case PLAYBACK_DV1394:
681 case PLAYBACK_FIREWIRE:
682 case PLAYBACK_IEC61883:
683 output_base = new VDevice1394(this);
687 //printf("VideoDevice::open_output 2 %d\n", out_config->driver);
689 if(output_base->open_output()) {
693 //printf("VideoDevice::open_output 3 %d\n", out_config->driver);
694 return output_base ? 0 : 1;
699 int VideoDevice::start_playback()
701 // arm buffer before doing this
704 return output_base ? output_base->start_playback() : 1;
707 int VideoDevice::stop_playback()
709 if( output_base ) output_base->stop_playback();
715 void VideoDevice::goose_input()
717 if(input_base) input_base->goose_input();
720 void VideoDevice::new_output_buffer(VFrame **output, int colormodel, EDL *edl)
722 if(!output_base) return;
723 output_base->new_output_buffer(output, colormodel, edl);
727 int VideoDevice::interrupt_playback()
733 int VideoDevice::write_buffer(VFrame *output, EDL *edl)
735 return output_base ? output_base->write_buffer(output, edl) : 1;
738 int VideoDevice::output_visible()
740 return output_base ? output_base->output_visible() : 0;
743 BC_Bitmap* VideoDevice::get_bitmap()
745 return output_base ? output_base->get_bitmap() : 0;
749 int VideoDevice::set_cloexec_flag(int desc, int value)
751 int oldflags = fcntl(desc, F_GETFD, 0);
752 if( oldflags < 0 ) return oldflags;
754 oldflags |= FD_CLOEXEC;
756 oldflags &= ~FD_CLOEXEC;
757 return fcntl(desc, F_SETFD, oldflags);
761 void VideoDevice::auto_update(double rate, int width, int height)
763 if( !in_config || !in_config->follow_video ) return;
767 in_config_updated = 1;
770 int VideoDevice::config_updated()
772 if( !in_config || !in_config->follow_video ) return 0;
773 return in_config_updated;
776 void VideoDevice::config_update()
778 in_config_updated = 0;
779 VideoInConfig *vconfig_in = mwindow->edl->session->vconfig_in;
780 vconfig_in->w = capture_w;
781 vconfig_in->h = capture_h;
782 vconfig_in->in_framerate = frame_rate;
786 int VideoDevice::start_toc(const char *path, const char *toc_path)
788 return input_base ? input_base->start_toc(path, toc_path) : -1;
791 int VideoDevice::start_record(int fd, int bsz)
793 return input_base ? input_base->start_record(fd, bsz) : -1;
796 int VideoDevice::stop_record()
798 return input_base ? input_base->stop_record() : -1;
802 int VideoDevice::total_video_streams()
804 return input_base ? input_base->total_video_streams() : 0;
809 int VideoDevice::get_video_pid(int track)
811 return !input_base ? -1 : input_base->get_video_pid(track);
815 int VideoDevice::get_video_info(int track, int &pid,
816 double &framerate, int &width, int &height, char *title)
818 return !input_base ? 1 : input_base->
819 get_video_info(track, pid, framerate, width, height, title);
822 int VideoDevice::get_thumbnail(int stream, int64_t &position,
823 unsigned char *&thumbnail, int &ww, int &hh)
825 return !input_base ? 1 :
826 input_base->get_thumbnail(stream, position, thumbnail, ww, hh);
829 int VideoDevice::set_skimming(int track, int skim, skim_fn fn, void *vp)
831 return !input_base ? 1 :
832 input_base->set_skimming(track, skim, fn, vp);