blib/lib/Video/DVDRip/Cluster/JobPlanner.pm untested

filter: Statementstmt 0.0% 0 / 177 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 248 CC 23 SCAR 32.6
CC
23
Cov
0%
build_cluster_transcode_job
42.0
build_cluster_psu_jobs
42.0
build_cluster_merge_audio_tracks_job
42.0
CRAP 26.1
1 # $Id: JobPlanner.pm 2368 2009-02-22 18:26:44Z 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 program 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::Cluster::JobPlanner;
12  
13 use base qw(Video::DVDRip::JobPlanner);
14  
15 use Carp;
16 use strict;
17  
18 use Locale::TextDomain qw (video.dvdrip);
19 use Event::ExecFlow qw (video.dvdrip);
20  
21  
22 sub build_cluster_transcode_job {
23 my $self = shift;
24  
25 my $project = $self->get_project;
26 my $title = $project->title,
27  
28 my @title_jobs;
29 my ($job, $last_job);
30  
31 #-- work has to be done per PSU. Jobs for all PSU's
32 #-- are collected here
33 my @all_psu_jobs;
34 foreach my $psu ( @{ $title->program_stream_units } ) {
35 next if not $psu->selected;
36 push @all_psu_jobs, $self->build_cluster_psu_jobs($title, $psu);
37 }
38  
39 #-- build a group for all PSU jobs
40 push @title_jobs, $job = Event::ExecFlow::Job::Group->new (
41 title => __x("Process all PSU's - title #{nr}", nr => $title->nr),
42 jobs => \@all_psu_jobs,
43 parallel => 1,
44 );
45 $last_job = $job;
46  
47 if ( @all_psu_jobs > 1 ) {
48 #-- merge all PSU's
49 push @title_jobs, $job = $self->build_cluster_merge_psu_files_job($title);
50 $job->set_depends_on([$last_job]);
51 $last_job = $job;
52 }
53 else {
54 #-- move PSU 0 file to final destination
55 push @title_jobs, $job = Event::ExecFlow::Job::Command->new (
56 title => __"Move PSU #0 file to final destination",
57 command => sub {
58 "mv ".
59 $title->audio_video_psu_file." ".
60 $title->target_avi_file,
61 },
62 stash => { psu => 0 },
63 depends_on => [ $last_job ],
64 );
65 $last_job = $job;
66 }
67  
68 #-- split video file
69 if ( $title->tc_split ) {
70 push @title_jobs, $job = $self->build_split_job($title);
71 $job->set_depends_on([$last_job]);
72 $job->set_stash({ prefer_local_access => 1 });
73 $last_job = $job;
74 }
75  
76 #-- vobsub generation?
77 if ( $title->has_vobsub_subtitles ) {
78 push @title_jobs, $job = $self->build_vobsub_job($title);
79 $job->set_depends_on([$last_job]);
80 my $stash_method = $job->get_type eq 'group' ? "add_stash_to_all_jobs" : "add_stash";
81 $job->$stash_method({ prefer_local_access => 1 });
82 $last_job = $job;
83 }
84  
85 #-- build a group for all jobs of this title
86 return Event::ExecFlow::Job::Group->new (
87 title => __x("Project '{project}' - title #{nr}",
88 project => $project->name, nr => $title->nr ),
89 jobs => \@title_jobs,
90 parallel => 1,
91 );
92 }
93  
94 sub build_cluster_psu_jobs {
95 my $self = shift;
96 my ($title, $psu) = @_;
97
98 my ($job, $last_job);
99
100 #-- calculate number of chunks for this PSU
101 my $frames_per_chunk = $title->frames_per_chunk || 10000;
102 my $psu_frames = $psu->frames;
103 my $psu_nr = $psu->nr;
104 my $chunk_cnt = int( $psu_frames / $frames_per_chunk );
105  
106 my $nodes_cnt =
107 1 + Video::DVDRip::Cluster::Master->get_master->get_online_nodes_cnt;
108  
109 $chunk_cnt = $nodes_cnt if $chunk_cnt < $nodes_cnt;
110 $chunk_cnt = 2 if $chunk_cnt < 2;
111 $psu->set_chunk_cnt($chunk_cnt);
112  
113 #-- jobs for this PSU are collected here
114 my @psu_jobs;
115  
116 #-- first all audio transcoding jobs
117 my $tc_audio_job = $self->build_cluster_audio_jobs($title, $psu);
118 push @psu_jobs, $tc_audio_job if $tc_audio_job;
119  
120 #-- then all video chunk jobs
121 my $tc_video_job;
122 push @psu_jobs, $tc_video_job = $self->build_cluster_video_chunk_jobs($title, $psu);
123  
124 #-- add jobs for video chunk merging
125 my $tc_merge_video_job = $self->build_cluster_merge_video_chunks_job ($title, $psu);
126 $tc_merge_video_job->set_depends_on([$tc_video_job]);
127 push @psu_jobs, $tc_merge_video_job;
128  
129 #-- add jobs for audio tracks merging
130 if ( $tc_audio_job ) {
131 my $tc_merge_audio_job = $self->build_cluster_merge_audio_tracks_job ($title, $psu);
132 if ( $tc_merge_audio_job ) {
133 $tc_merge_audio_job->set_depends_on([$tc_merge_video_job, $tc_audio_job]);
134 push @psu_jobs, $tc_merge_audio_job;
135 }
136 else {
137 #-- with one audio track video need to depend on the audio job
138 push @{$tc_merge_video_job->{depends_on}}, $tc_audio_job->get_name;
139 }
140 }
141  
142 #-- build a group for all jobs of this PSU
143 return Event::ExecFlow::Job::Group->new (
144 title => __x("Process PSU #{nr}", nr => $psu->nr),
145 jobs => \@psu_jobs,
146 parallel => 1,
147 );
148 }
149  
150 sub build_cluster_audio_jobs {
151 my $self = shift;
152 my ($title, $psu) = @_;
153
154 my @audio_jobs;
155 foreach my $audio ( @{$title->audio_tracks} ) {
156 #-- skip deactivated tracks
157 next if $audio->tc_target_track == -1;
158  
159 my $vob_nr = $audio->tc_nr;
160 my $avi_nr = $audio->tc_target_track;
161  
162 my $job = $self->build_transcode_audio_job($title, $vob_nr, $avi_nr);
163  
164 $job->set_stash ({
165 prefer_local_access => 1,
166 psu => $psu->nr,
167 chunk_cnt => $psu->chunk_cnt,
168 vob_nr => $vob_nr,
169 avi_nr => $avi_nr,
170 });
171  
172 push @audio_jobs, $job;
173 }
174
175 return unless @audio_jobs;
176  
177 return Event::ExecFlow::Job::Group->new (
178 title => __x("Process all audio tracks - title #{nr}, PSU #{psu}",
179 nr => $title->nr, psu => $psu->nr ),
180 jobs => \@audio_jobs,
181 parallel => 1,
182 );
183 }
184  
185 sub build_cluster_merge_audio_tracks_job {
186 my $self = shift;
187 my ($title, $psu) = @_;
188
189 my $mode = $title->is_ogg ? "all" : "skip1";
190
191 my @merge_audio_jobs;
192 my $first = 1;
193 my $last_job;
194 foreach my $audio ( sort { $a->tc_target_track <=> $b->tc_target_track }
195 @{$title->audio_tracks} ) {
196 #-- skip deactivated tracks
197 next if $audio->tc_target_track == -1;
198  
199 #-- all or skip first?
200 if ( $mode eq 'skip1' && $first ) {
201 $first = 0;
202 next;
203 }
204 $first = 0;
205  
206 my $vob_nr = $audio->tc_nr;
207 my $avi_nr = $audio->tc_target_track;
208  
209 my $job = $self->build_merge_audio_job($title, $vob_nr, $avi_nr);
210 $job->set_depends_on([$last_job]) if $last_job;
211  
212 $job->set_stash ({
213 prefer_local_access => 1,
214 psu => $psu->nr,
215 chunk_cnt => $psu->chunk_cnt,
216 vob_nr => $vob_nr,
217 avi_nr => $avi_nr,
218 });
219  
220 push @merge_audio_jobs, $job;
221 $last_job = $job;
222 }
223  
224 return unless @merge_audio_jobs;
225  
226 return Event::ExecFlow::Job::Group->new (
227 title => __x("Merge all audio tracks - title #{nr}, PSU #{psu}",
228 nr => $title->nr, psu => $psu->nr ),
229 jobs => \@merge_audio_jobs,
230 parallel => 1,
231 );
232 }
233  
234 sub build_cluster_video_chunk_jobs {
235 my $self = shift;
236 my ($title, $psu) = @_;
237
238 my @all_video_chunk_jobs;
239  
240 my $chunk_cnt = $psu->chunk_cnt;
241 my $multipass = $title->tc_multipass;
242
243 my ($job, $last_job);
244
245 for ( my $i = 0; $i < $chunk_cnt; ++$i ) {
246 if ( $multipass ) {
247 #-- First pass
248 my $pass1_job = $self->build_transcode_video_pass_job($title, 1, undef, $i+1, $psu->nr);
249 $pass1_job->set_progress_max($psu->frames);
250 $pass1_job->set_stash({
251 chunk => $i,
252 chunk_cnt => $psu->chunk_cnt,
253 psu => $psu->nr,
254 });
255
256 #-- Second pass
257 my $pass2_job = $self->build_transcode_video_pass_job($title, 2, undef, $i+1, $psu->nr);
258 $pass2_job->set_progress_max($psu->frames);
259 $pass2_job->set_stash({
260 chunk => $i,
261 chunk_cnt => $psu->chunk_cnt,
262 psu => $psu->nr,
263 });
264 $pass2_job->set_depends_on([$pass1_job]);
265
266 #-- Build group for both passes
267 push @all_video_chunk_jobs, Event::ExecFlow::Job::Group->new (
268 title => __x("Multipass video transcoding - title #{nr}, PSU #{psu}, ".
269 "chunk #{chunk}",
270 nr => $title->nr, psu => $psu->nr, chunk => $i+1),
271 jobs => [ $pass1_job, $pass2_job ],
272 parallel => 1,
273 );
274 }
275 else {
276 #-- Single pass transcoding
277 my $tc_video_job = $self->build_transcode_video_pass_job($title, 0, undef, $i+1, $psu->nr);
278 $tc_video_job->set_progress_max($psu->frames);
279 $tc_video_job->set_stash({
280 chunk => $i,
281 chunk_cnt => $psu->chunk_cnt,
282 psu => $psu->nr,
283 });
284 push @all_video_chunk_jobs, $tc_video_job;
285 }
286 }
287
288 return Event::ExecFlow::Job::Group->new (
289 title => __x("Process all video chunks - title #{nr}, PSU #{psu}",
290 nr => $title->nr, psu => $psu->nr ),
291 jobs => \@all_video_chunk_jobs,
292 parallel => 1,
293 );
294 }
295  
296 sub build_cluster_merge_video_chunks_job {
297 my $self = shift;
298 my ($title, $psu) = @_;
299
300 my $info = __x("Merge video chunks - title #{nr}, PSU #{psu}",
301 nr => $title->nr, psu => $psu->nr);
302  
303 my $command = sub {
304 $title->get_merge_video_audio_command;
305 };
306  
307 my $progress_parser = sub {
308 my ($job, $buffer) = @_;
309 if ( $buffer =~ /\(\d+-(\d+)\)/ ) {
310 # avimerge
311 $job->set_progress_cnt ($1);
312 } elsif ( $buffer =~ /(\d+)/ ) {
313 # ogmmerge
314 $job->set_progress_cnt ($1);
315 }
316 };
317  
318 return Event::ExecFlow::Job::Command->new (
319 title => $info,
320 command => $command,
321 diskspace_consumed => 0, # $diskspace_consumed,
322 progress_ips => __"fps",
323 progress_max => $psu->frames,
324 progress_parser => $progress_parser,
325 stash => {
326 prefer_local_access => 1,
327 psu => $psu->nr,
328 },
329 );
330 }
331  
332 sub build_cluster_merge_psu_files_job {
333 my $self = shift;
334 my ($title) = @_;
335  
336 my $info = __x("Merge all PSU files - title #{nr}",
337 nr => $title->nr);
338  
339 my $command = sub {
340 $title->get_merge_psu_command;
341 };
342  
343 my $progress_parser = sub {
344 my ($job, $buffer) = @_;
345 if ( $buffer =~ /\(\d+-(\d+)\)/ ) {
346 # avimerge
347 $job->set_progress_cnt ($1);
348 } elsif ( $buffer =~ /(\d+)/ ) {
349 # ogmmerge
350 $job->set_progress_cnt ($1);
351 }
352 };
353  
354 return Event::ExecFlow::Job::Command->new (
355 title => $info,
356 command => $command,
357 diskspace_consumed => 0, # $diskspace_consumed,
358 progress_ips => __"fps",
359 progress_max => $title->frames,
360 progress_parser => $progress_parser,
361 stash => {
362 prefer_local_access => 1,
363 },
364 );
365 }
366  
367 1;