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"
40 #include "pluginvclient.h"
41 #include "pluginserver.h"
42 #include "recordconfig.h"
43 #include "transportque.inc"
45 #include "videodevice.h"
46 #include "videodevice.inc"
51 #define HISTORY_FRAMES 30
53 class LiveVideoWindow;
60 void copy_from(LiveVideoConfig &src);
61 int equivalent(LiveVideoConfig &src);
62 void interpolate(LiveVideoConfig &prev,
63 LiveVideoConfig &next,
66 int64_t current_frame);
71 // Without access to the video device, the ChannelPicker can't
72 // do any of the things it was designed to. Instead, just provide
73 // a list of channels.
74 class LiveChannelList : public BC_ListBox
77 LiveChannelList(LiveVideo *plugin,
88 class LiveChannelSelect : public BC_Button
91 LiveChannelSelect(LiveVideo *plugin,
101 class LiveVideoWindow : public PluginClientWindow
104 LiveVideoWindow(LiveVideo *plugin);
107 void create_objects();
109 int resize_event(int w, int h);
111 ArrayList<BC_ListBoxItem*> channel_list;
113 LiveChannelList *list;
114 LiveChannelSelect *select;
123 class LiveVideo : public PluginVClient
126 LiveVideo(PluginServer *server);
130 PLUGIN_CLASS_MEMBERS(LiveVideoConfig);
132 int process_buffer(VFrame *frame,
133 int64_t start_position,
136 int is_multichannel();
138 void save_data(KeyFrame *keyframe);
139 void read_data(KeyFrame *keyframe);
143 ChannelDB *channeldb;
144 VideoDevice *vdevice;
145 // Colormodel the device generates
147 // Temporary for colormodel conversion
149 // What configuration parameters the device supports
150 Channel master_channel;
151 PictureConfig *picture;
154 // Decompressors for different video drivers
172 LiveVideoConfig::LiveVideoConfig()
177 void LiveVideoConfig::copy_from(LiveVideoConfig &src)
179 this->channel = src.channel;
182 int LiveVideoConfig::equivalent(LiveVideoConfig &src)
184 return (this->channel == src.channel);
187 void LiveVideoConfig::interpolate(LiveVideoConfig &prev,
188 LiveVideoConfig &next,
191 int64_t current_frame)
193 this->channel = prev.channel;
200 LiveVideoWindow::LiveVideoWindow(LiveVideo *plugin)
201 : PluginClientWindow(plugin,
208 this->plugin = plugin;
211 LiveVideoWindow::~LiveVideoWindow()
213 channel_list.remove_all_objects();
216 void LiveVideoWindow::create_objects()
219 int ys5 = yS(5), ys10 = yS(10);
220 int x = xs10, y = ys10;
222 EDLSession *session = plugin->get_edl()->session;
224 VideoDevice::load_channeldb(plugin->channeldb, session->vconfig_in);
225 for(int i = 0; i < plugin->channeldb->size(); i++)
227 BC_ListBoxItem *current;
228 channel_list.append(current =
229 new BC_ListBoxItem(plugin->channeldb->get(i)->title));
230 if(i == plugin->config.channel) current->set_selected(1);
233 add_subwindow(title = new BC_Title(x, y, _("Channels:")));
234 y += title->get_h() + ys5;
235 add_subwindow(list = new LiveChannelList(plugin,
240 get_h() - y - BC_OKButton::calculate_h() - ys10 - ys10));
241 y += list->get_h() + ys10;
242 add_subwindow(select = new LiveChannelSelect(plugin,
251 int LiveVideoWindow::resize_event(int w, int h)
253 int list_bottom = get_h() - list->get_y() - list->get_h();
254 int list_side = get_w() - list->get_x() - list->get_w();
255 int select_top = get_h() - select->get_y();
257 title->reposition_window(title->get_x(), title->get_y());
259 list->reposition_window(list->get_x(),
261 w - list->get_x() - list_side,
262 h - list->get_y() - list_bottom);
263 select->reposition_window(select->get_x(),
273 LiveChannelList::LiveChannelList(LiveVideo *plugin,
274 LiveVideoWindow *gui,
283 LISTBOX_TEXT, // Display text list or icons
284 &gui->channel_list) // Each column has an ArrayList of BC_ListBoxItems.
286 this->plugin = plugin;
290 int LiveChannelList::handle_event()
292 plugin->config.channel = get_selection_number(0, 0);
293 plugin->send_configure_change();
298 LiveChannelSelect::LiveChannelSelect(LiveVideo *plugin,
299 LiveVideoWindow *gui,
303 BC_WindowBase::get_resources()->ok_images)
305 this->plugin = plugin;
309 int LiveChannelSelect::handle_event()
311 plugin->config.channel = gui->list->get_selection_number(0, 0);
312 plugin->send_configure_change();
341 REGISTER_PLUGIN(LiveVideo)
348 LiveVideo::LiveVideo(PluginServer *server)
349 : PluginVClient(server)
353 channeldb = new ChannelDB;
362 this->server = server;
367 LiveVideo::~LiveVideo()
372 vdevice->interrupt_crash();
373 vdevice->close_all();
380 if(dv) dv_delete(dv);
382 if(mjpeg) mjpeg_delete(mjpeg);
388 int LiveVideo::process_buffer(VFrame *frame,
389 int64_t start_position,
392 load_configuration();
393 //printf("LiveVideo::process_buffer 10 start_position=%lld buffer_size=%d size=%d\n",
394 //start_position, get_buffer_size(), size);
396 EDLSession *session = get_edl()->session;
401 vdevice = new VideoDevice(server ? server->mwindow : 0);
402 vdevice->open_input(session->vconfig_in,
408 // The color model depends on the asset configured by the user for recording.
409 // Unfortunately, get_best_colormodel returns the best colormodel for displaying
410 // on the record monitor, not the colormodel supported by the device.
411 // Some devices can read directly to the best colormodel and some can't.
412 switch(session->vconfig_in->driver)
414 case CAPTURE_FIREWIRE:
415 case CAPTURE_IEC61883:
416 case VIDEO4LINUX2JPEG:
417 case CAPTURE_JPEG_WEBCAM:
418 input_cmodel = BC_COMPRESSED;
421 input_cmodel = vdevice->get_best_colormodel(session->recording_format);
426 // Load the picture config from the main defaults file.
428 // Load channel table
429 VideoDevice::load_channeldb(channeldb, session->vconfig_in);
433 picture = new PictureConfig;
434 picture->load_defaults();
437 // Picture must have usage from driver before it can load defaults.
438 master_channel.copy_usage(vdevice->channel);
439 picture->copy_usage(vdevice->picture);
440 picture->load_defaults();
442 // Need to load picture defaults but this requires MWindow.
443 vdevice->set_picture(picture);
444 vdevice->set_channel(channeldb->get(config.channel));
446 prev_channel = config.channel;
449 if(session && vdevice)
452 if(prev_channel != config.channel)
454 prev_channel = config.channel;
455 vdevice->set_picture(picture);
456 vdevice->set_channel(channeldb->get(config.channel));
460 (void)vdevice->config_updated();
461 VFrame *input = frame;
462 if(input_cmodel != frame->get_color_model() ||
463 session->vconfig_in->w != frame->get_w() ||
464 session->vconfig_in->h != frame->get_h())
468 temp = new VFrame(session->vconfig_in->w, session->vconfig_in->h,
474 vdevice->read_buffer(input);
478 if(input->get_color_model() != BC_COMPRESSED)
480 int w = MIN(session->vconfig_in->w, frame->get_w());
481 int h = MIN(session->vconfig_in->h, frame->get_h());
482 BC_CModels::transfer(frame->get_rows(), /* Leave NULL if non existent */
484 frame->get_y(), /* Leave NULL if non existent */
487 input->get_y(), /* Leave NULL if non existent */
490 0, /* Dimensions to capture from input frame */
494 0, /* Dimensions to project on output frame */
498 input->get_color_model(),
499 frame->get_color_model(),
500 0, /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
501 input->get_bytes_per_line(), /* For planar use the luma rowspan */
502 frame->get_bytes_per_line()); /* For planar use the luma rowspan */
503 frame->set_opengl_state(VFrame::RAM);
506 if(input->get_compressed_size())
508 switch(session->vconfig_in->driver)
511 case CAPTURE_FIREWIRE:
512 case CAPTURE_IEC61883:
513 // Decompress a DV frame from the driver
516 dv_read_video(((dv_t*)dv),
519 input->get_compressed_size(),
520 frame->get_color_model());
521 frame->set_opengl_state(VFrame::RAM);
524 case VIDEO4LINUX2JPEG:
526 mjpeg = mjpeg_new(frame->get_w(),
529 mjpeg_decompress(mjpeg,
531 input->get_compressed_size(),
532 input->get_field2_offset(),
537 frame->get_color_model(),
538 get_project_smp() + 1);
541 case CAPTURE_JPEG_WEBCAM:
543 mjpeg = mjpeg_new(frame->get_w(),
546 // printf("LiveVideo::process_buffer %d %p %d\n",
548 // input->get_data(),
549 // input->get_compressed_size());
550 mjpeg_decompress(mjpeg,
552 input->get_compressed_size(),
558 frame->get_color_model(),
559 get_project_smp() + 1);
565 printf("LiveVideo::process_buffer %d zero size image\n", __LINE__);
573 void LiveVideo::render_stop()
577 vdevice->interrupt_crash();
578 vdevice->close_all();
587 const char* LiveVideo::plugin_title() { return N_("Live Video"); }
588 int LiveVideo::is_realtime() { return 1; }
589 int LiveVideo::is_multichannel() { return 0; }
590 int LiveVideo::is_synthesis() { return 1; }
594 NEW_WINDOW_MACRO(LiveVideo, LiveVideoWindow)
596 LOAD_CONFIGURATION_MACRO(LiveVideo, LiveVideoConfig)
600 void LiveVideo::save_data(KeyFrame *keyframe)
603 output.set_shared_output(keyframe->xbuf);
604 output.tag.set_title("LIVEVIDEO");
605 output.tag.set_property("CHANNEL", config.channel);
607 output.tag.set_title("/LIVEVIDEO");
609 output.append_newline();
610 output.terminate_string();
613 void LiveVideo::read_data(KeyFrame *keyframe)
617 input.set_shared_input(keyframe->xbuf);
623 result = input.read_tag();
627 if(input.tag.title_is("LIVEVIDEO"))
629 config.channel = input.tag.get_property("CHANNEL", config.channel);
635 void LiveVideo::update_gui()
639 if(load_configuration())
641 thread->window->lock_window("LiveVideo::update_gui");
642 ((LiveVideoWindow*)thread->window)->list->set_selected(
643 &((LiveVideoWindow*)thread->window)->channel_list,
646 ((LiveVideoWindow*)thread->window)->list->draw_items(1);
647 thread->window->unlock_window();