| 1 | # $Id: Preview.pm 2305 2007-04-13 11:25:03Z joern $ | ||
| 2 | |||
| 3 | #----------------------------------------------------------------------- | ||
| 4 | # Copyright (C) 2001-2006 Jörn Reder <joern AT zyn.de>. | ||
| 5 | # All Rights Reserved. See file COPYRIGHT for details. | ||
| 6 | # | ||
| 7 | # This module is part of Video::DVDRip, which is free software; you can | ||
| 8 | # redistribute it and/or modify it under the same terms as Perl itself. | ||
| 9 | #----------------------------------------------------------------------- | ||
| 10 | |||
| 11 | package Video::DVDRip::GUI::Preview; | ||
| 12 | use Locale::TextDomain qw (video.dvdrip); | ||
| 13 | |||
| 14 | use Carp; | ||
| 15 | use strict; | ||
| 16 | |||
| 17 | @Video::DVDRip::GUI::Preview::ISA = qw ( Video::DVDRip::GUI::Base ); | ||
| 18 | |||
| 19 | use Video::DVDRip::TranscodeRC; | ||
| 20 | use Gtk2::Helper; | ||
| 21 | |||
| 22 | sub transcode_remote { shift->{transcode_remote} } | ||
| 23 | sub transcode_pipe { shift->{transcode_pipe} } | ||
| 24 | |||
| 25 | sub set_transcode_remote { shift->{transcode_remote} = $_[1] } | ||
| 26 | sub set_transcode_pipe { shift->{transcode_pipe} = $_[1] } | ||
| 27 | |||
| 28 | sub stop_in_progress { shift->{stop_in_progress} } | ||
| 29 | sub set_stop_in_progress { shift->{stop_in_progress} = $_[1] } | ||
| 30 | |||
| 31 | sub eof_cb { shift->{eof_cb} } | ||
| 32 | sub closed_cb { shift->{closed_cb} } | ||
| 33 | sub selection_cb { shift->{selection_cb} } | ||
| 34 | |||
| 35 | sub set_eof_cb { shift->{eof_cb} = $_[1] } | ||
| 36 | sub set_closed_cb { shift->{closed_cb} = $_[1] } | ||
| 37 | sub set_selection_cb { shift->{selection_cb} = $_[1] } | ||
| 38 | |||
| 39 | sub gdk_input { shift->{gdk_input} } | ||
| 40 | sub set_gdk_input { shift->{gdk_input} = $_[1] } | ||
| 41 | |||
| 42 | sub settings_applied { shift->{settings_applied} } | ||
| 43 | sub set_settings_applied { shift->{settings_applied} = $_[1] } | ||
| 44 | |||
| 45 | sub new { | ||
| 46 | my $class = shift; | ||
| 47 | my %par = @_; | ||
| 48 | my ( $closed_cb, $selection_cb, $eof_cb ) | ||
| 49 | = @par{ 'closed_cb', 'selection_cb', 'eof_cb' }; | ||
| 50 | |||
| 51 | my $self = $class->SUPER::new(@_); | ||
| 52 | |||
| 53 | $self->set_closed_cb($closed_cb); | ||
| 54 | $self->set_selection_cb($selection_cb); | ||
| 55 | $self->set_eof_cb($eof_cb); | ||
| 56 | |||
| 57 | return $self; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub closed { not defined shift->transcode_pipe } | ||
| 61 | |||
| 62 | sub open { | ||
| 63 | my $self = shift; | ||
| 64 | |||
| 65 | return if $self->transcode_pipe; | ||
| 66 | |||
| 67 | my $socket_file | ||
| 68 | = "/tmp/tc.$$." . time . ( int( rand(100000) ) ) . ".sock"; | ||
| 69 | |||
| 70 | # start transcode | ||
| 71 | |||
| 72 | my $title = $self->selected_title; | ||
| 73 | |||
| 74 | my ( $orig_start, $orig_end ) | ||
| 75 | = ( $title->tc_start_frame, $title->tc_end_frame, ); | ||
| 76 | |||
| 77 | $title->set_tc_start_frame( $title->tc_preview_start_frame ); | ||
| 78 | $title->set_tc_end_frame( $title->tc_preview_end_frame ); | ||
| 79 | |||
| 80 | my $command = $title->get_transcode_command( pass => 1 ); | ||
| 81 | |||
| 82 | $title->set_tc_start_frame($orig_start); | ||
| 83 | $title->set_tc_end_frame($orig_end); | ||
| 84 | |||
| 85 | $command =~ s/&& echo EXECFLOW_OK//; | ||
| 86 | $command =~ s/-y\s+[^\s]+//; # remove target codecs | ||
| 87 | $command =~ s/-o\s+[^\s]+//; # remove output file | ||
| 88 | |||
| 89 | $command =~ s/(-J\s+extsub[^\s]+)//g; | ||
| 90 | my $subtitle = $1; | ||
| 91 | |||
| 92 | $command =~ s/-J\s+[^\s]+//g; # remove all filters | ||
| 93 | $command =~ s/--psu_mode//g; # disable PSU core | ||
| 94 | $command =~ s/--no_split//g; # disable PSU core | ||
| 95 | |||
| 96 | $command .= " -J pv=cache=" . $title->tc_preview_buffer_frames; | ||
| 97 | $command .= " --socket $socket_file -u 1"; | ||
| 98 | |||
| 99 | $command .= " $subtitle" if $subtitle; | ||
| 100 | |||
| 101 | require Video::DVDRip::GUI::Pipe; | ||
| 102 | |||
| 103 | my $transcode_pipe; | ||
| 104 | $transcode_pipe = Video::DVDRip::GUI::Pipe->new( | ||
| 105 | command => $command, | ||
| 106 | need_output => 0, | ||
| 107 | cb_finished => sub { | ||
| 108 | my $eof_cb = $self->eof_cb; | ||
| 109 | &$eof_cb($transcode_pipe->output =~ /EXECFLOW_OK/) if $eof_cb; | ||
| 110 | $self->stop if not $self->stop_in_progress; | ||
| 111 | $transcode_pipe = undef; | ||
| 112 | 1; | ||
| 113 | }, | ||
| 114 | ); | ||
| 115 | |||
| 116 | $transcode_pipe->open; | ||
| 117 | |||
| 118 | $self->set_transcode_pipe($transcode_pipe); | ||
| 119 | |||
| 120 | my $start_time = time(); | ||
| 121 | |||
| 122 | my $timer; | ||
| 123 | $timer = Glib::Timeout->add( | ||
| 124 | 200, | ||
| 125 | sub { | ||
| 126 | if ( not -e $socket_file ) { | ||
| 127 | if ( time - $start_time >= 2 ) { | ||
| 128 | $self->stop; | ||
| 129 | Glib::Source->remove($timer) if $timer; | ||
| 130 | croak "msg:Couldn't start transcode."; | ||
| 131 | } | ||
| 132 | return 1; | ||
| 133 | } | ||
| 134 | |||
| 135 | Glib::Source->remove($timer); | ||
| 136 | |||
| 137 | # start remote control | ||
| 138 | my $transcode_remote = Video::DVDRip::TranscodeRC->new( | ||
| 139 | socket_file => $socket_file, | ||
| 140 | error_cb => sub { | ||
| 141 | my ($line) = @_; | ||
| 142 | $self->long_message_window( message => __ | ||
| 143 | "Error executing a transcode socket command:\n\n" | ||
| 144 | . $line ); | ||
| 145 | 1; | ||
| 146 | }, | ||
| 147 | selection_cb => $self->selection_cb, | ||
| 148 | ); | ||
| 149 | |||
| 150 | $transcode_remote->connect; | ||
| 151 | |||
| 152 | my $socket_fileno = $transcode_remote->socket->fileno; | ||
| 153 | |||
| 154 | my $gdk_input = Gtk2::Helper->add_watch( | ||
| 155 | $socket_fileno, | ||
| 156 | 'in', | ||
| 157 | sub { | ||
| 158 | my $rc = $transcode_remote->receive; | ||
| 159 | if ( defined $rc ) { | ||
| 160 | $self->process_transcode_remote_output( line => $rc, ) | ||
| 161 | if $rc ne ''; | ||
| 162 | } | ||
| 163 | 1; | ||
| 164 | } | ||
| 165 | ); | ||
| 166 | |||
| 167 | $self->set_gdk_input($gdk_input); | ||
| 168 | |||
| 169 | $self->set_transcode_remote($transcode_remote); | ||
| 170 | |||
| 171 | $self->apply_filter_settings; | ||
| 172 | |||
| 173 | return 0; | ||
| 174 | } | ||
| 175 | ); | ||
| 176 | |||
| 177 | 1; | ||
| 178 | } | ||
| 179 | |||
| 180 | sub apply_filter_settings { | ||
| 181 | my $self = shift; | ||
| 182 | |||
| 183 | my $title = $self->selected_title; | ||
| 184 | |||
| 185 | my $config_strings | ||
| 186 | = $title->tc_filter_settings->get_filter_config_strings( | ||
| 187 | with_suffixes => 1 ); | ||
| 188 | |||
| 189 | my $max_frames_needed = $title->tc_filter_settings->get_max_frames_needed, | ||
| 190 | |||
| 191 | my $transcode_remote = $self->transcode_remote; | ||
| 192 | |||
| 193 | foreach my $config ( @{$config_strings} ) { | ||
| 194 | $transcode_remote->config_filter( | ||
| 195 | filter => $config->{filter}, | ||
| 196 | options => $config->{options}, | ||
| 197 | ); | ||
| 198 | if ( $config->{enabled} ) { | ||
| 199 | $transcode_remote->enable_filter( filter => $config->{filter} ); | ||
| 200 | } | ||
| 201 | else { | ||
| 202 | $transcode_remote->disable_filter( filter => $config->{filter} ); | ||
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | $transcode_remote->preview( | ||
| 207 | command => "draw", | ||
| 208 | options => $max_frames_needed, | ||
| 209 | ) if $transcode_remote->paused; | ||
| 210 | |||
| 211 | $self->set_settings_applied(1); | ||
| 212 | |||
| 213 | 1; | ||
| 214 | } | ||
| 215 | |||
| 216 | sub stop { | ||
| 217 | my $self = shift; | ||
| 218 | |||
| 219 | $self->set_stop_in_progress(1); | ||
| 220 | |||
| 221 | if ( $self->transcode_remote and $self->transcode_remote->paused ) { | ||
| 222 | $self->pause; | ||
| 223 | $self->transcode_remote->quit; | ||
| 224 | Glib::Timeout->add( | ||
| 225 | 500, | ||
| 226 | sub { | ||
| 227 | $self->transcode_remote->disconnect; | ||
| 228 | $self->transcode_pipe->cancel; | ||
| 229 | Gtk2::Helper->remove_watch( $self->gdk_input ); | ||
| 230 | $self->close; | ||
| 231 | return 0; | ||
| 232 | |||
| 233 | } | ||
| 234 | ) | ||
| 235 | if $self->transcode_pipe; | ||
| 236 | } | ||
| 237 | else { | ||
| 238 | $self->transcode_remote->disconnect | ||
| 239 | if $self->transcode_remote; | ||
| 240 | $self->transcode_pipe->cancel | ||
| 241 | if $self->transcode_pipe; | ||
| 242 | Gtk2::Helper->remove_watch( $self->gdk_input ) | ||
| 243 | if $self->gdk_input; | ||
| 244 | $self->close; | ||
| 245 | } | ||
| 246 | |||
| 247 | 1; | ||
| 248 | } | ||
| 249 | |||
| 250 | sub close { | ||
| 251 | my $self = shift; | ||
| 252 | |||
| 253 | $self->set_transcode_pipe(undef); | ||
| 254 | $self->set_transcode_remote(undef); | ||
| 255 | |||
| 256 | $self->set_stop_in_progress(0); | ||
| 257 | |||
| 258 | 1; | ||
| 259 | } | ||
| 260 | |||
| 261 | sub pause { | ||
| 262 | my $self = shift; | ||
| 263 | |||
| 264 | $self->set_settings_applied(0); | ||
| 265 | |||
| 266 | $self->transcode_remote->preview( command => "pause", ); | ||
| 267 | |||
| 268 | return $self->transcode_remote->paused; | ||
| 269 | } | ||
| 270 | |||
| 271 | sub paused { | ||
| 272 | my $self = shift; | ||
| 273 | return 0 if ! $self->transcode_remote; | ||
| 274 | return $self->transcode_remote->paused; | ||
| 275 | } | ||
| 276 | |||
| 277 | sub process_transcode_remote_output { | ||
| 278 | my $self = shift; | ||
| 279 | my %par = @_; | ||
| 280 | my ($line) = @par{'line'}; | ||
| 281 | |||
| 282 | if ( $line =~ /preview window close/ ) { | ||
| 283 | my $closed_cb = $self->closed_cb; | ||
| 284 | &$closed_cb() if $closed_cb; | ||
| 285 | if ( $self->transcode_pipe ) { | ||
| 286 | $self->transcode_pipe->cancel; | ||
| 287 | Gtk2::Helper->remove_watch( $self->gdk_input ); | ||
| 288 | $self->close; | ||
| 289 | } | ||
| 290 | } | ||
| 291 | |||
| 292 | 1; | ||
| 293 | } | ||
| 294 | |||
| 295 | 1; | ||
| 296 |