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;
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) {
212 #ifdef HAVE_VIDEO4LINUX2
215 case VIDEO4LINUX2JPEG:
216 case VIDEO4LINUX2MPEG:
217 case CAPTURE_JPEG_WEBCAM:
218 case CAPTURE_YUYV_WEBCAM:
224 this->input_x = input_x;
225 this->input_y = input_y;
228 case CAPTURE_FIREWIRE:
229 case CAPTURE_IEC61883:
243 if( !input_base ) return 1;
244 result = input_base->open_input();
245 if(!result) capturing = 1;
246 in_config_updated = 0;
250 VDeviceBase* VideoDevice::new_device_base()
252 switch(in_config->driver) {
253 #ifdef HAVE_VIDEO4LINUX2
255 case CAPTURE_JPEG_WEBCAM:
256 case CAPTURE_YUYV_WEBCAM:
257 return input_base = new VDeviceV4L2(this);
258 case VIDEO4LINUX2JPEG:
259 return input_base = new VDeviceV4L2JPEG(this);
260 case VIDEO4LINUX2MPEG:
261 return input_base = new VDeviceV4L2MPEG(this);
265 return input_base = new VDeviceX11(this, 0);
268 case CAPTURE_FIREWIRE:
269 case CAPTURE_IEC61883:
270 return input_base = new VDevice1394(this);
275 return input_base = new VDeviceDVB(this);
281 static const char* get_channeldb_path(VideoInConfig *vconfig_in)
284 switch(vconfig_in->driver)
287 case CAPTURE_JPEG_WEBCAM:
288 case CAPTURE_YUYV_WEBCAM:
289 path = (char*)"channels_v4l2";
291 case VIDEO4LINUX2JPEG:
292 path = (char*)"channels_v4l2jpeg";
294 case VIDEO4LINUX2MPEG:
295 path = (char*)"channels_v4l2mpeg";
298 path = (char*)"channels_dvb";
304 void VideoDevice::load_channeldb(ChannelDB *channeldb, VideoInConfig *vconfig_in)
306 channeldb->load(get_channeldb_path(vconfig_in));
309 void VideoDevice::save_channeldb(ChannelDB *channeldb, VideoInConfig *vconfig_in)
311 channeldb->save(get_channeldb_path(vconfig_in));
315 VDeviceBase* VideoDevice::get_input_base()
320 VDeviceBase* VideoDevice::get_output_base()
325 DeviceMPEGInput *VideoDevice::mpeg_device()
327 return !input_base ? 0 : input_base->mpeg_device();
330 int VideoDevice::is_compressed(int driver, int use_file, int use_fixed)
332 // FileMOV needs to have write_frames called so the start codes get scanned.
333 return ((driver == VIDEO4LINUX2JPEG && use_fixed) ||
334 (driver == CAPTURE_JPEG_WEBCAM && use_fixed) ||
335 driver == CAPTURE_FIREWIRE ||
336 driver == CAPTURE_IEC61883);
339 int VideoDevice::is_compressed(int use_file, int use_fixed)
341 return is_compressed(in_config->driver, use_file, use_fixed);
345 void VideoDevice::fix_asset(Asset *asset, int driver)
347 // Fix asset using legacy routine
348 const char *vcodec = 0;
350 case CAPTURE_IEC61883:
351 case CAPTURE_FIREWIRE:
352 vcodec = CODEC_TAG_DVSD;
355 case VIDEO4LINUX2JPEG:
356 vcodec = CODEC_TAG_MJPEG;
359 case CAPTURE_JPEG_WEBCAM:
360 vcodec = CODEC_TAG_JPEG;
364 asset->format = FILE_FFMPEG;
365 strcpy(asset->vcodec, vcodec);
369 // Fix asset using inherited routine
372 if(input_base) input_base->fix_asset(asset);
378 const char* VideoDevice::drivertostr(int driver)
381 case PLAYBACK_X11: return PLAYBACK_X11_TITLE;
382 case PLAYBACK_X11_XV: return PLAYBACK_X11_XV_TITLE;
383 case PLAYBACK_X11_GL: return PLAYBACK_X11_GL_TITLE;
384 case VIDEO4LINUX2: return VIDEO4LINUX2_TITLE;
385 case VIDEO4LINUX2JPEG: return VIDEO4LINUX2JPEG_TITLE;
386 case VIDEO4LINUX2MPEG: return VIDEO4LINUX2MPEG_TITLE;
387 case CAPTURE_JPEG_WEBCAM: return CAPTURE_JPEG_WEBCAM_TITLE;
388 case CAPTURE_YUYV_WEBCAM: return CAPTURE_YUYV_WEBCAM_TITLE;
389 case SCREENCAPTURE: return SCREENCAPTURE_TITLE;
390 case CAPTURE_DVB: return CAPTURE_DVB_TITLE;
392 case CAPTURE_FIREWIRE: return CAPTURE_FIREWIRE_TITLE;
393 case CAPTURE_IEC61883: return CAPTURE_IEC61883_TITLE;
399 int VideoDevice::get_best_colormodel(Asset *asset)
401 return input_base ? input_base->get_best_colormodel(asset) : BC_RGB888;
404 int VideoDevice::drop_frames(int frames)
406 return input_base ? input_base->drop_frames(frames) : 1;
409 double VideoDevice::device_timestamp()
411 return input_base ? input_base->device_timestamp() : -1.;
414 double VideoDevice::get_timestamp()
419 int VideoDevice::close_all()
423 output_base->close_all();
432 input_base->close_all();
442 input_sources.remove_all_objects();
448 int VideoDevice::set_adevice(AudioDevice *adev)
455 ArrayList<Channel*>* VideoDevice::get_inputs()
457 return &input_sources;
460 Channel* VideoDevice::new_input_source(char *device_name)
462 for( int i=0; i<input_sources.total; ++i ) {
463 if(!strcmp(input_sources.values[i]->device_name, device_name))
464 return input_sources.values[i];
466 Channel *item = new Channel;
467 strcpy(item->device_name, device_name);
468 input_sources.append(item);
472 int VideoDevice::get_failed()
474 return keepalive ? keepalive->get_failed() : 0;
477 int VideoDevice::interrupt_crash()
479 return input_base ? input_base->interrupt_crash() : 0;
482 int VideoDevice::set_translation(int in_x, int in_y)
489 int VideoDevice::set_field_order(int odd_field_first)
491 this->odd_field_first = odd_field_first;
495 int VideoDevice::set_channel(Channel *channel)
499 channel_lock->lock("VideoDevice::set_channel");
500 this->channel->copy_settings(channel);
502 channel_lock->unlock();
503 result = input_base ? input_base->set_channel(channel) :
504 output_base ? output_base->set_channel(channel) :
510 int VideoDevice::set_captioning(int mode)
513 input_base->set_captioning(mode);
517 void VideoDevice::set_quality(int quality)
519 this->quality = quality;
522 void VideoDevice::set_cpus(int cpus)
527 int VideoDevice::set_picture(PictureConfig *picture)
530 picture_lock->lock("VideoDevice::set_picture");
531 this->picture->copy_settings(picture);
533 picture_lock->unlock();
535 if(input_base) return input_base->set_picture(picture);
540 int VideoDevice::update_translation()
542 float frame_in_capture_x1f, frame_in_capture_x2f, frame_in_capture_y1f, frame_in_capture_y2f;
543 float capture_in_frame_x1f, capture_in_frame_x2f, capture_in_frame_y1f, capture_in_frame_y2f;
547 input_x = new_input_x;
548 input_y = new_input_y;
549 if( in_config->driver == VIDEO4LINUX2 ) {
550 if(input_z != new_input_z) {
551 input_z = new_input_z;
554 capture_w = (int)((float)in_config->w * input_z + 0.5);
555 capture_h = (int)((float)in_config->h * input_z + 0.5);
557 // Need to align to multiple of 4
562 frame_in_capture_x1f = (float)input_x * input_z + capture_w / 2 - in_config->w / 2;
563 frame_in_capture_x2f = (float)input_x * input_z + capture_w / 2 + in_config->w / 2;
564 frame_in_capture_y1f = (float)input_y * input_z + capture_h / 2 - in_config->h / 2;
565 frame_in_capture_y2f = (float)input_y * input_z + capture_h / 2 + in_config->h / 2;
567 capture_in_frame_x1f = 0;
568 capture_in_frame_y1f = 0;
569 capture_in_frame_x2f = in_config->w;
570 capture_in_frame_y2f = in_config->h;
572 if(frame_in_capture_x1f < 0) { capture_in_frame_x1f -= frame_in_capture_x1f; frame_in_capture_x1f = 0; }
573 if(frame_in_capture_y1f < 0) { capture_in_frame_y1f -= frame_in_capture_y1f; frame_in_capture_y1f = 0; }
574 if(frame_in_capture_x2f > capture_w) { capture_in_frame_x2f -= frame_in_capture_x2f - capture_w; frame_in_capture_x2f = capture_w; }
575 if(frame_in_capture_y2f > capture_h) { capture_in_frame_y2f -= frame_in_capture_y2f - capture_h; frame_in_capture_y2f = capture_h; }
577 frame_in_capture_x1 = (int)frame_in_capture_x1f;
578 frame_in_capture_y1 = (int)frame_in_capture_y1f;
579 frame_in_capture_x2 = (int)frame_in_capture_x2f;
580 frame_in_capture_y2 = (int)frame_in_capture_y2f;
582 capture_in_frame_x1 = (int)capture_in_frame_x1f;
583 capture_in_frame_y1 = (int)capture_in_frame_y1f;
584 capture_in_frame_x2 = (int)capture_in_frame_x2f;
585 capture_in_frame_y2 = (int)capture_in_frame_y2f;
593 int VideoDevice::set_latency_counter(int value)
595 latency_counter = value;
599 int VideoDevice::has_signal()
601 return input_base ? input_base->has_signal() : 0;
604 int VideoDevice::create_channeldb(ArrayList<Channel*> *channeldb)
606 return input_base ? input_base->create_channeldb(channeldb) : 1;
610 int VideoDevice::read_buffer(VFrame *frame)
613 if(!capturing) return 0;
615 //printf("VideoDevice::read_buffer %p %p\n", frame, input_base);
617 // Reset the keepalive thread
618 if(keepalive) keepalive->capturing = 1;
619 result = input_base->read_buffer(frame);
620 timestamp = frame->get_timestamp();
621 if( timestamp < 0 ) {
622 struct timeval tv; gettimeofday(&tv, 0);
623 timestamp = tv.tv_sec + tv.tv_usec / 1000000.0;
626 keepalive->capturing = 0;
627 keepalive->reset_keepalive();
636 // ================================= OUTPUT ==========================================
639 int VideoDevice::open_output(VideoOutConfig *config, float rate,
640 int out_w, int out_h, Canvas *output, int single_frame)
643 //printf("VideoDevice::open_output 1 %d\n", out_config->driver);
644 *this->out_config = *config;
645 //printf("VideoDevice::open_output 1 %d\n", out_config->driver);
649 this->single_frame = single_frame;
651 //printf("VideoDevice::open_output 1 %d\n", out_config->driver);
652 switch(out_config->driver) {
654 case PLAYBACK_X11_XV:
655 case PLAYBACK_X11_GL:
656 output_base = new VDeviceX11(this, output);
660 case PLAYBACK_DV1394:
661 case PLAYBACK_FIREWIRE:
662 case PLAYBACK_IEC61883:
663 output_base = new VDevice1394(this);
667 //printf("VideoDevice::open_output 2 %d\n", out_config->driver);
669 if(output_base->open_output()) {
673 //printf("VideoDevice::open_output 3 %d\n", out_config->driver);
674 return output_base ? 0 : 1;
679 int VideoDevice::start_playback()
681 // arm buffer before doing this
684 return output_base ? output_base->start_playback() : 1;
687 int VideoDevice::stop_playback()
689 if( output_base ) output_base->stop_playback();
695 void VideoDevice::goose_input()
697 if(input_base) input_base->goose_input();
700 void VideoDevice::new_output_buffer(VFrame **output, int colormodel)
702 if(!output_base) return;
703 output_base->new_output_buffer(output, colormodel);
707 int VideoDevice::interrupt_playback()
713 int VideoDevice::write_buffer(VFrame *output, EDL *edl)
715 return output_base ? output_base->write_buffer(output, edl) : 1;
718 int VideoDevice::output_visible()
720 return output_base ? output_base->output_visible() : 0;
723 BC_Bitmap* VideoDevice::get_bitmap()
725 return output_base ? output_base->get_bitmap() : 0;
729 int VideoDevice::set_cloexec_flag(int desc, int value)
731 int oldflags = fcntl(desc, F_GETFD, 0);
732 if( oldflags < 0 ) return oldflags;
734 oldflags |= FD_CLOEXEC;
736 oldflags &= ~FD_CLOEXEC;
737 return fcntl(desc, F_SETFD, oldflags);
741 void VideoDevice::auto_update(double rate, int width, int height)
743 if( !in_config || !in_config->follow_video ) return;
747 in_config_updated = 1;
750 int VideoDevice::config_updated()
752 if( !in_config || !in_config->follow_video ) return 0;
753 return in_config_updated;
756 void VideoDevice::config_update()
758 in_config_updated = 0;
759 VideoInConfig *vconfig_in = mwindow->edl->session->vconfig_in;
760 vconfig_in->w = capture_w;
761 vconfig_in->h = capture_h;
762 vconfig_in->in_framerate = frame_rate;
766 int VideoDevice::start_toc(const char *path, const char *toc_path)
768 return input_base ? input_base->start_toc(path, toc_path) : -1;
771 int VideoDevice::start_record(int fd, int bsz)
773 return input_base ? input_base->start_record(fd, bsz) : -1;
776 int VideoDevice::stop_record()
778 return input_base ? input_base->stop_record() : -1;
782 int VideoDevice::total_video_streams()
784 return input_base ? input_base->total_video_streams() : 0;
789 int VideoDevice::get_video_pid(int track)
791 return !input_base ? -1 : input_base->get_video_pid(track);
795 int VideoDevice::get_video_info(int track, int &pid,
796 double &framerate, int &width, int &height, char *title)
798 return !input_base ? 1 : input_base->
799 get_video_info(track, pid, framerate, width, height, title);
802 int VideoDevice::get_thumbnail(int stream, int64_t &position,
803 unsigned char *&thumbnail, int &ww, int &hh)
805 return !input_base ? 1 :
806 input_base->get_thumbnail(stream, position, thumbnail, ww, hh);
809 int VideoDevice::set_skimming(int track, int skim, skim_fn fn, void *vp)
811 return !input_base ? 1 :
812 input_base->set_skimming(track, skim, fn, vp);