4 * Copyright (C) 2008 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
23 #include "bcdisplayinfo.h"
24 #include "bcsignals.h"
26 #include "channeldb.h"
30 #include "edlsession.h"
38 #include "pluginvclient.h"
39 #include "pluginserver.h"
40 #include "recordconfig.h"
41 #include "transportque.inc"
43 #include "videodevice.h"
44 #include "videodevice.inc"
49 #define HISTORY_FRAMES 30
51 class LiveVideoWindow;
58 void copy_from(LiveVideoConfig &src);
59 int equivalent(LiveVideoConfig &src);
60 void interpolate(LiveVideoConfig &prev,
61 LiveVideoConfig &next,
64 int64_t current_frame);
69 // Without access to the video device, the ChannelPicker can't
70 // do any of the things it was designed to. Instead, just provide
71 // a list of channels.
72 class LiveChannelList : public BC_ListBox
75 LiveChannelList(LiveVideo *plugin,
86 class LiveChannelSelect : public BC_Button
89 LiveChannelSelect(LiveVideo *plugin,
99 class LiveVideoWindow : public PluginClientWindow
102 LiveVideoWindow(LiveVideo *plugin);
105 void create_objects();
107 int resize_event(int w, int h);
109 ArrayList<BC_ListBoxItem*> channel_list;
111 LiveChannelList *list;
112 LiveChannelSelect *select;
121 class LiveVideo : public PluginVClient
124 LiveVideo(PluginServer *server);
128 PLUGIN_CLASS_MEMBERS(LiveVideoConfig);
130 int process_buffer(VFrame *frame,
131 int64_t start_position,
134 int is_multichannel();
136 void save_data(KeyFrame *keyframe);
137 void read_data(KeyFrame *keyframe);
141 ChannelDB *channeldb;
142 VideoDevice *vdevice;
143 // Colormodel the device generates
145 // Temporary for colormodel conversion
147 // What configuration parameters the device supports
148 Channel master_channel;
149 PictureConfig *picture;
152 // Decompressors for different video drivers
168 LiveVideoConfig::LiveVideoConfig()
173 void LiveVideoConfig::copy_from(LiveVideoConfig &src)
175 this->channel = src.channel;
178 int LiveVideoConfig::equivalent(LiveVideoConfig &src)
180 return (this->channel == src.channel);
183 void LiveVideoConfig::interpolate(LiveVideoConfig &prev,
184 LiveVideoConfig &next,
187 int64_t current_frame)
189 this->channel = prev.channel;
196 LiveVideoWindow::LiveVideoWindow(LiveVideo *plugin)
197 : PluginClientWindow(plugin,
204 this->plugin = plugin;
207 LiveVideoWindow::~LiveVideoWindow()
209 channel_list.remove_all_objects();
212 void LiveVideoWindow::create_objects()
216 EDLSession *session = plugin->get_edl()->session;
218 VideoDevice::load_channeldb(plugin->channeldb, session->vconfig_in);
219 for(int i = 0; i < plugin->channeldb->size(); i++)
221 BC_ListBoxItem *current;
222 channel_list.append(current =
223 new BC_ListBoxItem(plugin->channeldb->get(i)->title));
224 if(i == plugin->config.channel) current->set_selected(1);
227 add_subwindow(title = new BC_Title(x, y, _("Channels:")));
228 y += title->get_h() + 5;
229 add_subwindow(list = new LiveChannelList(plugin,
234 get_h() - y - BC_OKButton::calculate_h() - 10 - 10));
235 y += list->get_h() + 10;
236 add_subwindow(select = new LiveChannelSelect(plugin,
245 int LiveVideoWindow::resize_event(int w, int h)
247 int list_bottom = get_h() - list->get_y() - list->get_h();
248 int list_side = get_w() - list->get_x() - list->get_w();
249 int select_top = get_h() - select->get_y();
251 title->reposition_window(title->get_x(), title->get_y());
253 list->reposition_window(list->get_x(),
255 w - list->get_x() - list_side,
256 h - list->get_y() - list_bottom);
257 select->reposition_window(select->get_x(),
267 LiveChannelList::LiveChannelList(LiveVideo *plugin,
268 LiveVideoWindow *gui,
277 LISTBOX_TEXT, // Display text list or icons
278 &gui->channel_list) // Each column has an ArrayList of BC_ListBoxItems.
280 this->plugin = plugin;
284 int LiveChannelList::handle_event()
286 plugin->config.channel = get_selection_number(0, 0);
287 plugin->send_configure_change();
292 LiveChannelSelect::LiveChannelSelect(LiveVideo *plugin,
293 LiveVideoWindow *gui,
297 BC_WindowBase::get_resources()->ok_images)
299 this->plugin = plugin;
303 int LiveChannelSelect::handle_event()
305 plugin->config.channel = gui->list->get_selection_number(0, 0);
306 plugin->send_configure_change();
335 REGISTER_PLUGIN(LiveVideo)
342 LiveVideo::LiveVideo(PluginServer *server)
343 : PluginVClient(server)
347 channeldb = new ChannelDB;
354 this->server = server;
359 LiveVideo::~LiveVideo()
364 vdevice->interrupt_crash();
365 vdevice->close_all();
371 if(dv) dv_delete(dv);
372 if(mjpeg) mjpeg_delete(mjpeg);
378 int LiveVideo::process_buffer(VFrame *frame,
379 int64_t start_position,
382 load_configuration();
383 //printf("LiveVideo::process_buffer 10 start_position=%lld buffer_size=%d size=%d\n",
384 //start_position, get_buffer_size(), size);
386 EDLSession *session = get_edl()->session;
391 vdevice = new VideoDevice(server ? server->mwindow : 0);
392 vdevice->open_input(session->vconfig_in,
398 // The color model depends on the asset configured by the user for recording.
399 // Unfortunately, get_best_colormodel returns the best colormodel for displaying
400 // on the record monitor, not the colormodel supported by the device.
401 // Some devices can read directly to the best colormodel and some can't.
402 switch(session->vconfig_in->driver)
404 case CAPTURE_FIREWIRE:
405 case CAPTURE_IEC61883:
406 case VIDEO4LINUX2JPEG:
407 case CAPTURE_JPEG_WEBCAM:
408 input_cmodel = BC_COMPRESSED;
411 input_cmodel = vdevice->get_best_colormodel(session->recording_format);
416 // Load the picture config from the main defaults file.
418 // Load channel table
419 VideoDevice::load_channeldb(channeldb, session->vconfig_in);
423 picture = new PictureConfig;
424 picture->load_defaults();
427 // Picture must have usage from driver before it can load defaults.
428 master_channel.copy_usage(vdevice->channel);
429 picture->copy_usage(vdevice->picture);
430 picture->load_defaults();
432 // Need to load picture defaults but this requires MWindow.
433 vdevice->set_picture(picture);
434 vdevice->set_channel(channeldb->get(config.channel));
436 prev_channel = config.channel;
439 if(session && vdevice)
442 if(prev_channel != config.channel)
444 prev_channel = config.channel;
445 vdevice->set_picture(picture);
446 vdevice->set_channel(channeldb->get(config.channel));
450 (void)vdevice->config_updated();
451 VFrame *input = frame;
452 if(input_cmodel != frame->get_color_model() ||
453 session->vconfig_in->w != frame->get_w() ||
454 session->vconfig_in->h != frame->get_h())
458 temp = new VFrame(session->vconfig_in->w, session->vconfig_in->h,
464 vdevice->read_buffer(input);
468 if(input->get_color_model() != BC_COMPRESSED)
470 int w = MIN(session->vconfig_in->w, frame->get_w());
471 int h = MIN(session->vconfig_in->h, frame->get_h());
472 BC_CModels::transfer(frame->get_rows(), /* Leave NULL if non existent */
474 frame->get_y(), /* Leave NULL if non existent */
477 input->get_y(), /* Leave NULL if non existent */
480 0, /* Dimensions to capture from input frame */
484 0, /* Dimensions to project on output frame */
488 input->get_color_model(),
489 frame->get_color_model(),
490 0, /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
491 input->get_bytes_per_line(), /* For planar use the luma rowspan */
492 frame->get_bytes_per_line()); /* For planar use the luma rowspan */
493 frame->set_opengl_state(VFrame::RAM);
496 if(input->get_compressed_size())
498 switch(session->vconfig_in->driver)
500 case CAPTURE_FIREWIRE:
501 case CAPTURE_IEC61883:
502 // Decompress a DV frame from the driver
505 dv_read_video(((dv_t*)dv),
508 input->get_compressed_size(),
509 frame->get_color_model());
510 frame->set_opengl_state(VFrame::RAM);
513 case VIDEO4LINUX2JPEG:
515 mjpeg = mjpeg_new(frame->get_w(),
518 mjpeg_decompress(mjpeg,
520 input->get_compressed_size(),
521 input->get_field2_offset(),
526 frame->get_color_model(),
527 get_project_smp() + 1);
530 case CAPTURE_JPEG_WEBCAM:
532 mjpeg = mjpeg_new(frame->get_w(),
535 // printf("LiveVideo::process_buffer %d %p %d\n",
537 // input->get_data(),
538 // input->get_compressed_size());
539 mjpeg_decompress(mjpeg,
541 input->get_compressed_size(),
547 frame->get_color_model(),
548 get_project_smp() + 1);
554 printf("LiveVideo::process_buffer %d zero size image\n", __LINE__);
562 void LiveVideo::render_stop()
566 vdevice->interrupt_crash();
567 vdevice->close_all();
576 const char* LiveVideo::plugin_title() { return N_("Live Video"); }
577 int LiveVideo::is_realtime() { return 1; }
578 int LiveVideo::is_multichannel() { return 0; }
579 int LiveVideo::is_synthesis() { return 1; }
583 NEW_WINDOW_MACRO(LiveVideo, LiveVideoWindow)
585 LOAD_CONFIGURATION_MACRO(LiveVideo, LiveVideoConfig)
589 void LiveVideo::save_data(KeyFrame *keyframe)
592 output.set_shared_output(keyframe->xbuf);
593 output.tag.set_title("LIVEVIDEO");
594 output.tag.set_property("CHANNEL", config.channel);
596 output.tag.set_title("/LIVEVIDEO");
598 output.append_newline();
599 output.terminate_string();
602 void LiveVideo::read_data(KeyFrame *keyframe)
606 input.set_shared_input(keyframe->xbuf);
612 result = input.read_tag();
616 if(input.tag.title_is("LIVEVIDEO"))
618 config.channel = input.tag.get_property("CHANNEL", config.channel);
624 void LiveVideo::update_gui()
628 if(load_configuration())
630 thread->window->lock_window("LiveVideo::update_gui");
631 ((LiveVideoWindow*)thread->window)->list->set_selected(
632 &((LiveVideoWindow*)thread->window)->channel_list,
635 ((LiveVideoWindow*)thread->window)->list->draw_items(1);
636 thread->window->unlock_window();