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"
37 #include "mwindowgui.h"
39 #include "playbackconfig.h"
40 #include "playbackengine.h"
41 #include "preferences.h"
42 #include "recordconfig.h"
43 #include "recordgui.h"
44 #include "recordmonitor.h"
47 #include "vdevice1394.h"
49 #include "vdevicedvb.h"
50 #include "vdevicev4l2.h"
51 #include "vdevicev4l2jpeg.h"
52 #include "vdevicev4l2mpeg.h"
53 #include "vdevicex11.h"
54 #include "videoconfig.h"
55 #include "videodevice.h"
56 #include "videowindow.h"
57 #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;
198 int VideoDevice::open_input(VideoInConfig *config,
206 *this->in_config = *config;
209 this->input_z = -1; // Force initialization.
210 this->frame_rate = frame_rate;
211 if( input_base ) return 1; // device already open
213 switch(in_config->driver) {
214 #ifdef HAVE_VIDEO4LINUX2
217 case VIDEO4LINUX2JPEG:
218 case VIDEO4LINUX2MPEG:
219 case CAPTURE_JPEG_WEBCAM:
220 case CAPTURE_YUYV_WEBCAM:
226 this->input_x = input_x;
227 this->input_y = input_y;
230 case CAPTURE_FIREWIRE:
231 case CAPTURE_IEC61883:
245 if( !input_base ) return 1;
246 result = input_base->open_input();
247 if(!result) capturing = 1;
248 in_config_updated = 0;
252 VDeviceBase* VideoDevice::new_device_base()
254 switch(in_config->driver) {
255 #ifdef HAVE_VIDEO4LINUX2
257 case CAPTURE_JPEG_WEBCAM:
258 case CAPTURE_YUYV_WEBCAM:
259 return input_base = new VDeviceV4L2(this);
260 case VIDEO4LINUX2JPEG:
261 return input_base = new VDeviceV4L2JPEG(this);
262 case VIDEO4LINUX2MPEG:
263 return input_base = new VDeviceV4L2MPEG(this);
267 return input_base = new VDeviceX11(this, 0);
270 case CAPTURE_FIREWIRE:
271 case CAPTURE_IEC61883:
272 return input_base = new VDevice1394(this);
277 return input_base = new VDeviceDVB(this);
283 static const char* get_channeldb_path(VideoInConfig *vconfig_in)
286 switch(vconfig_in->driver)
289 case CAPTURE_JPEG_WEBCAM:
290 case CAPTURE_YUYV_WEBCAM:
291 path = (char*)"channels_v4l2";
293 case VIDEO4LINUX2JPEG:
294 path = (char*)"channels_v4l2jpeg";
296 case VIDEO4LINUX2MPEG:
297 path = (char*)"channels_v4l2mpeg";
300 path = (char*)"channels_dvb";
306 void VideoDevice::load_channeldb(ChannelDB *channeldb, VideoInConfig *vconfig_in)
308 channeldb->load(get_channeldb_path(vconfig_in));
311 void VideoDevice::save_channeldb(ChannelDB *channeldb, VideoInConfig *vconfig_in)
313 channeldb->save(get_channeldb_path(vconfig_in));
317 VDeviceBase* VideoDevice::get_input_base()
322 VDeviceBase* VideoDevice::get_output_base()
327 DeviceMPEGInput *VideoDevice::mpeg_device()
329 return !input_base ? 0 : input_base->mpeg_device();
332 int VideoDevice::is_compressed(int driver, int use_file, int use_fixed)
334 // FileMOV needs to have write_frames called so the start codes get scanned.
335 return ((driver == VIDEO4LINUX2JPEG && use_fixed) ||
336 (driver == CAPTURE_JPEG_WEBCAM && use_fixed) ||
337 driver == CAPTURE_FIREWIRE ||
338 driver == CAPTURE_IEC61883);
341 int VideoDevice::is_compressed(int use_file, int use_fixed)
343 return is_compressed(in_config->driver, use_file, use_fixed);
347 void VideoDevice::fix_asset(Asset *asset, int driver)
349 // Fix asset using legacy routine
350 const char *vcodec = 0;
352 case CAPTURE_IEC61883:
353 case CAPTURE_FIREWIRE:
354 vcodec = CODEC_TAG_DVSD;
357 case VIDEO4LINUX2JPEG:
358 vcodec = CODEC_TAG_MJPEG;
361 case CAPTURE_JPEG_WEBCAM:
362 vcodec = CODEC_TAG_JPEG;
366 asset->format = FILE_FFMPEG;
367 strcpy(asset->vcodec, vcodec);
371 // Fix asset using inherited routine
374 if(input_base) input_base->fix_asset(asset);
380 const char* VideoDevice::drivertostr(int driver)
383 case PLAYBACK_X11: return PLAYBACK_X11_TITLE;
384 case PLAYBACK_X11_XV: return PLAYBACK_X11_XV_TITLE;
385 case PLAYBACK_X11_GL: return PLAYBACK_X11_GL_TITLE;
386 case VIDEO4LINUX2: return VIDEO4LINUX2_TITLE;
387 case VIDEO4LINUX2JPEG: return VIDEO4LINUX2JPEG_TITLE;
388 case VIDEO4LINUX2MPEG: return VIDEO4LINUX2MPEG_TITLE;
389 case CAPTURE_JPEG_WEBCAM: return CAPTURE_JPEG_WEBCAM_TITLE;
390 case CAPTURE_YUYV_WEBCAM: return CAPTURE_YUYV_WEBCAM_TITLE;
391 case SCREENCAPTURE: return SCREENCAPTURE_TITLE;
392 case CAPTURE_DVB: return CAPTURE_DVB_TITLE;
394 case CAPTURE_FIREWIRE: return CAPTURE_FIREWIRE_TITLE;
395 case CAPTURE_IEC61883: return CAPTURE_IEC61883_TITLE;
401 int VideoDevice::get_best_colormodel(Asset *asset)
403 return input_base ? input_base->get_best_colormodel(asset) : BC_RGB888;
406 int VideoDevice::drop_frames(int frames)
408 return input_base ? input_base->drop_frames(frames) : 1;
411 double VideoDevice::device_timestamp()
413 return input_base ? input_base->device_timestamp() : -1.;
416 double VideoDevice::get_timestamp()
421 int VideoDevice::close_all()
425 output_base->close_all();
434 input_base->close_all();
444 input_sources.remove_all_objects();
450 int VideoDevice::set_adevice(AudioDevice *adev)
457 ArrayList<Channel*>* VideoDevice::get_inputs()
459 return &input_sources;
462 Channel* VideoDevice::new_input_source(char *device_name)
464 for( int i=0; i<input_sources.total; ++i ) {
465 if(!strcmp(input_sources.values[i]->device_name, device_name))
466 return input_sources.values[i];
468 Channel *item = new Channel;
469 strcpy(item->device_name, device_name);
470 input_sources.append(item);
474 int VideoDevice::get_failed()
476 return keepalive ? keepalive->get_failed() : 0;
479 int VideoDevice::interrupt_crash()
481 return input_base ? input_base->interrupt_crash() : 0;
484 int VideoDevice::set_translation(int in_x, int in_y)
491 int VideoDevice::set_field_order(int odd_field_first)
493 this->odd_field_first = odd_field_first;
497 void VideoDevice::set_do_cursor(int do_cursor, int do_big_cursor)
499 int cursor_scale = 0;
509 this->do_cursor = cursor_scale;
512 int VideoDevice::set_channel(Channel *channel)
516 channel_lock->lock("VideoDevice::set_channel");
517 this->channel->copy_settings(channel);
519 channel_lock->unlock();
520 result = input_base ? input_base->set_channel(channel) :
521 output_base ? output_base->set_channel(channel) :
527 int VideoDevice::set_captioning(int mode)
530 input_base->set_captioning(mode);
534 void VideoDevice::set_quality(int quality)
536 this->quality = quality;
539 void VideoDevice::set_cpus(int cpus)
544 int VideoDevice::set_picture(PictureConfig *picture)
547 picture_lock->lock("VideoDevice::set_picture");
548 this->picture->copy_settings(picture);
550 picture_lock->unlock();
552 if(input_base) return input_base->set_picture(picture);
557 int VideoDevice::update_translation()
559 float frame_in_capture_x1f, frame_in_capture_x2f, frame_in_capture_y1f, frame_in_capture_y2f;
560 float capture_in_frame_x1f, capture_in_frame_x2f, capture_in_frame_y1f, capture_in_frame_y2f;
564 input_x = new_input_x;
565 input_y = new_input_y;
566 if( in_config->driver == VIDEO4LINUX2 ) {
567 if(input_z != new_input_z) {
568 input_z = new_input_z;
571 capture_w = (int)((float)in_config->w * input_z + 0.5);
572 capture_h = (int)((float)in_config->h * input_z + 0.5);
574 // Need to align to multiple of 4
579 frame_in_capture_x1f = (float)input_x * input_z + capture_w / 2 - in_config->w / 2;
580 frame_in_capture_x2f = (float)input_x * input_z + capture_w / 2 + in_config->w / 2;
581 frame_in_capture_y1f = (float)input_y * input_z + capture_h / 2 - in_config->h / 2;
582 frame_in_capture_y2f = (float)input_y * input_z + capture_h / 2 + in_config->h / 2;
584 capture_in_frame_x1f = 0;
585 capture_in_frame_y1f = 0;
586 capture_in_frame_x2f = in_config->w;
587 capture_in_frame_y2f = in_config->h;
589 if(frame_in_capture_x1f < 0) { capture_in_frame_x1f -= frame_in_capture_x1f; frame_in_capture_x1f = 0; }
590 if(frame_in_capture_y1f < 0) { capture_in_frame_y1f -= frame_in_capture_y1f; frame_in_capture_y1f = 0; }
591 if(frame_in_capture_x2f > capture_w) { capture_in_frame_x2f -= frame_in_capture_x2f - capture_w; frame_in_capture_x2f = capture_w; }
592 if(frame_in_capture_y2f > capture_h) { capture_in_frame_y2f -= frame_in_capture_y2f - capture_h; frame_in_capture_y2f = capture_h; }
594 frame_in_capture_x1 = (int)frame_in_capture_x1f;
595 frame_in_capture_y1 = (int)frame_in_capture_y1f;
596 frame_in_capture_x2 = (int)frame_in_capture_x2f;
597 frame_in_capture_y2 = (int)frame_in_capture_y2f;
599 capture_in_frame_x1 = (int)capture_in_frame_x1f;
600 capture_in_frame_y1 = (int)capture_in_frame_y1f;
601 capture_in_frame_x2 = (int)capture_in_frame_x2f;
602 capture_in_frame_y2 = (int)capture_in_frame_y2f;
610 int VideoDevice::set_latency_counter(int value)
612 latency_counter = value;
616 int VideoDevice::has_signal()
618 return input_base ? input_base->has_signal() : 0;
621 int VideoDevice::create_channeldb(ArrayList<Channel*> *channeldb)
623 return input_base ? input_base->create_channeldb(channeldb) : 1;
627 int VideoDevice::read_buffer(VFrame *frame)
630 if(!capturing) return 0;
632 //printf("VideoDevice::read_buffer %p %p\n", frame, input_base);
634 // Reset the keepalive thread
635 if(keepalive) keepalive->capturing = 1;
636 result = input_base->read_buffer(frame);
637 timestamp = frame->get_timestamp();
638 if( timestamp < 0 ) {
639 struct timeval tv; gettimeofday(&tv, 0);
640 timestamp = tv.tv_sec + tv.tv_usec / 1000000.0;
643 keepalive->capturing = 0;
644 keepalive->reset_keepalive();
653 // ================================= OUTPUT ==========================================
656 int VideoDevice::open_output(VideoOutConfig *config, float rate,
657 int out_w, int out_h, Canvas *output, int single_frame)
660 //printf("VideoDevice::open_output 1 %d\n", out_config->driver);
661 *this->out_config = *config;
662 //printf("VideoDevice::open_output 1 %d\n", out_config->driver);
666 this->single_frame = single_frame;
668 //printf("VideoDevice::open_output 1 %d\n", out_config->driver);
669 switch(out_config->driver) {
671 case PLAYBACK_X11_XV:
672 case PLAYBACK_X11_GL:
673 output_base = new VDeviceX11(this, output);
677 case PLAYBACK_DV1394:
678 case PLAYBACK_FIREWIRE:
679 case PLAYBACK_IEC61883:
680 output_base = new VDevice1394(this);
684 //printf("VideoDevice::open_output 2 %d\n", out_config->driver);
686 if(output_base->open_output()) {
690 //printf("VideoDevice::open_output 3 %d\n", out_config->driver);
691 return output_base ? 0 : 1;
696 int VideoDevice::start_playback()
698 // arm buffer before doing this
701 return output_base ? output_base->start_playback() : 1;
704 int VideoDevice::stop_playback()
706 if( output_base ) output_base->stop_playback();
712 void VideoDevice::goose_input()
714 if(input_base) input_base->goose_input();
717 void VideoDevice::new_output_buffer(VFrame **output, int colormodel, EDL *edl)
719 if(!output_base) return;
720 output_base->new_output_buffer(output, colormodel, edl);
724 int VideoDevice::interrupt_playback()
730 int VideoDevice::write_buffer(VFrame *output, EDL *edl)
732 return output_base ? output_base->write_buffer(output, edl) : 1;
735 int VideoDevice::output_visible()
737 return output_base ? output_base->output_visible() : 0;
740 BC_Bitmap* VideoDevice::get_bitmap()
742 return output_base ? output_base->get_bitmap() : 0;
746 int VideoDevice::set_cloexec_flag(int desc, int value)
748 int oldflags = fcntl(desc, F_GETFD, 0);
749 if( oldflags < 0 ) return oldflags;
751 oldflags |= FD_CLOEXEC;
753 oldflags &= ~FD_CLOEXEC;
754 return fcntl(desc, F_SETFD, oldflags);
758 void VideoDevice::auto_update(double rate, int width, int height)
760 if( !in_config || !in_config->follow_video ) return;
764 in_config_updated = 1;
767 int VideoDevice::config_updated()
769 if( !in_config || !in_config->follow_video ) return 0;
770 return in_config_updated;
773 void VideoDevice::config_update()
775 in_config_updated = 0;
776 VideoInConfig *vconfig_in = mwindow->edl->session->vconfig_in;
777 vconfig_in->w = capture_w;
778 vconfig_in->h = capture_h;
779 vconfig_in->in_framerate = frame_rate;
783 int VideoDevice::start_toc(const char *path, const char *toc_path)
785 return input_base ? input_base->start_toc(path, toc_path) : -1;
788 int VideoDevice::start_record(int fd, int bsz)
790 return input_base ? input_base->start_record(fd, bsz) : -1;
793 int VideoDevice::stop_record()
795 return input_base ? input_base->stop_record() : -1;
799 int VideoDevice::total_video_streams()
801 return input_base ? input_base->total_video_streams() : 0;
806 int VideoDevice::get_video_pid(int track)
808 return !input_base ? -1 : input_base->get_video_pid(track);
812 int VideoDevice::get_video_info(int track, int &pid,
813 double &framerate, int &width, int &height, char *title)
815 return !input_base ? 1 : input_base->
816 get_video_info(track, pid, framerate, width, height, title);
819 int VideoDevice::get_thumbnail(int stream, int64_t &position,
820 unsigned char *&thumbnail, int &ww, int &hh)
822 return !input_base ? 1 :
823 input_base->get_thumbnail(stream, position, thumbnail, ww, hh);
826 int VideoDevice::set_skimming(int track, int skim, skim_fn fn, void *vp)
828 return !input_base ? 1 :
829 input_base->set_skimming(track, skim, fn, vp);