1 /* 2 * @(#)JobCollection.java - all about a collection 3 * 4 * Copyright (c) 2005-2008 by dvb.matt, All Rights Reserved. 5 * 6 * This file is part of ProjectX, a free Java based demux utility. 7 * By the authors, ProjectX is intended for educational purposes only, 8 * as a non-commercial test project. 9 * 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation; either version 2 of the License, or 14 * (at your option) any later version. 15 * 16 * This program is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 * 21 * You should have received a copy of the GNU General Public License 22 * along with this program; if not, write to the Free Software 23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 24 * 25 */ 26 27 package net.sourceforge.dvb.projectx.common; 28 29 import java.util.ArrayList; 30 import java.util.List; 31 import java.util.Hashtable; 32 33 import java.io.File; 34 import java.io.RandomAccessFile; 35 import java.io.PrintStream; 36 import java.io.PrintWriter; 37 import java.io.IOException; 38 import java.io.FileOutputStream; 39 import java.io.ByteArrayInputStream; 40 41 import net.sourceforge.dvb.projectx.xinput.XInputFile; 42 43 import net.sourceforge.dvb.projectx.parser.Gop; 44 import net.sourceforge.dvb.projectx.parser.GopArray; 45 import net.sourceforge.dvb.projectx.parser.CommonParsing; 46 47 48 /** 49 * saves all stuff of a collection 50 */ 51 public class JobCollection extends Object { 52 53 private List cut_points = null; 54 private List chapter_points = null; 55 private List input_files = null; 56 private List predefined_IDs = null; 57 58 private String output_directory = null; 59 private String output_name = null; 60 private String normalLog = null; 61 62 private String file_separator = System.getProperty("file.separator"); 63 private String line_separator = System.getProperty("line.separator"); 64 65 private boolean debug = false; 66 private boolean progress_status = false; 67 68 private int primaryInputFileSegments = 0; 69 70 private int action_type = -1; 71 72 private PrintStream logging; 73 74 private JobProcessing job_processing; 75 76 private Hashtable cut_images; 77 78 private Settings settings; 79 80 /** 81 * 82 */ JobCollection()83 private JobCollection() 84 {} 85 86 /** 87 * 88 */ JobCollection(String _output_directory)89 public JobCollection(String _output_directory) 90 { 91 cut_points = new ArrayList(); 92 chapter_points = new ArrayList(); 93 input_files = new ArrayList(); 94 predefined_IDs = new ArrayList(); 95 96 init(_output_directory, "", action_type); 97 } 98 99 /** 100 * 101 */ JobCollection(String _output_directory, String _output_name, int _action_type, List _cut_points, List _chapter_points, List _input_files, List _predefined_IDs)102 public JobCollection(String _output_directory, String _output_name, int _action_type, List _cut_points, List _chapter_points, List _input_files, List _predefined_IDs) 103 { 104 cut_points = copyListElements(_cut_points); 105 chapter_points = copyListElements(_chapter_points); 106 input_files = copyListElements(_input_files); 107 predefined_IDs = copyListElements(_predefined_IDs); 108 109 init(_output_directory, _output_name, _action_type); 110 } 111 112 /** 113 * 114 */ init(String _output_directory, String _output_name, int _action_type)115 private void init(String _output_directory, String _output_name, int _action_type) 116 { 117 setOutputDirectory(_output_directory); 118 setOutputName(_output_name); 119 normalLog = ""; 120 action_type = _action_type; 121 122 cut_images = new Hashtable(); 123 } 124 125 /** 126 * 127 */ copyListElements(List sourceList)128 private List copyListElements(List sourceList) 129 { 130 List list = new ArrayList(); 131 132 for (int i = 0; i < sourceList.size(); i++) 133 list.add(sourceList.get(i)); 134 135 return list; 136 } 137 138 /** 139 * 140 */ getNewInstance()141 public JobCollection getNewInstance() 142 { 143 return (new JobCollection(output_directory, output_name, action_type, cut_points, chapter_points, input_files, predefined_IDs)); 144 } 145 146 /** 147 * init the process and all variables 148 */ startProcessing(boolean b)149 public void startProcessing(boolean b) 150 { 151 progress_status = true; 152 153 job_processing = new JobProcessing(this, b, getOutputDirectory()); 154 } 155 156 /** 157 * finish the process and all objects 158 */ finishProcessing()159 public void finishProcessing() 160 { 161 progress_status = false; 162 163 if (job_processing != null) 164 { 165 job_processing.finishProcessing(); 166 setOutputDirectory(job_processing.getSavedOutputDirectory()); 167 } 168 169 job_processing = null; 170 } 171 172 /** 173 * check if a process from this coll is running 174 * hinder some modifications of files and so on 175 */ isActive()176 public boolean isActive() 177 { 178 return progress_status; 179 } 180 181 /** 182 * 183 */ getFileSeparator()184 public String getFileSeparator() 185 { 186 return file_separator; 187 } 188 189 /** 190 * 191 */ getJobProcessing()192 public JobProcessing getJobProcessing() 193 { 194 return job_processing; 195 } 196 197 /** 198 * 199 */ addInputFile(Object input)200 public void addInputFile(Object input) 201 { 202 addInputFile(-1, input); 203 } 204 205 /** 206 * 207 */ addInputFile(Object[] input)208 public void addInputFile(Object[] input) 209 { 210 for (int i = 0; i < input.length; i++) 211 addInputFile(input[i]); 212 } 213 214 /** 215 * 216 */ addInputFile(int index, Object input)217 public void addInputFile(int index, Object input) 218 { 219 if (isActive()) 220 return; 221 222 if (index < 0) 223 index = input_files.size(); 224 225 XInputFile xInputFile = ((XInputFile) input).getNewInstance(); 226 227 xInputFile.setFileID(Common.getNewFileID()); 228 229 input_files.add(index, xInputFile); 230 // input_files.add(index, input); 231 232 determinePrimaryFileSegments(); 233 } 234 235 /** 236 * 237 */ removeInputFile(int index)238 public Object removeInputFile(int index) 239 { 240 if (isActive()) 241 return null; 242 243 if (index < 0 || index >= getInputFilesCount()) 244 return null; 245 246 Object obj = input_files.remove(index); 247 248 determinePrimaryFileSegments(); 249 250 return obj; 251 } 252 253 /** 254 * remove file index, start with last 255 */ removeInputFile(int[] index)256 public Object[] removeInputFile(int[] index) 257 { 258 if (isActive()) 259 return null; 260 261 Object[] objects = new Object[index.length]; 262 263 for (int i = index.length - 1; i >= 0; i--) 264 objects[i] = removeInputFile(index[i]); 265 266 return objects; 267 } 268 269 /** 270 * 271 */ determinePrimaryFileSegments()272 public void determinePrimaryFileSegments() 273 { 274 int primaryFilesCount = 0; // at least one file is primary 275 boolean completed = false; 276 XInputFile xInputFile; 277 278 filesearch: 279 for (int i = 0, j = -1, stream_type = -1, k = getInputFilesCount(); i < k; i++) 280 { 281 xInputFile = (XInputFile) getInputFile(i); 282 283 if (xInputFile.getStreamInfo() == null) 284 Common.getScanClass().getStreamInfo(xInputFile); 285 286 // continue loop to scan all files for later use 287 if (completed) 288 continue; 289 290 stream_type = xInputFile.getStreamInfo().getStreamType(); 291 292 switch (stream_type) 293 { 294 case CommonParsing.TS_TYPE: 295 case CommonParsing.PES_AV_TYPE: 296 case CommonParsing.PES_MPA_TYPE: 297 case CommonParsing.PES_PS1_TYPE: 298 case CommonParsing.MPEG1PS_TYPE: 299 case CommonParsing.MPEG2PS_TYPE: 300 case CommonParsing.PVA_TYPE: 301 if (j != -1 && j != stream_type) 302 { 303 completed = true; 304 continue; 305 } 306 break; 307 308 default: 309 continue; 310 } 311 312 j = stream_type; 313 314 primaryFilesCount++; 315 } 316 317 setPrimaryInputFileSegments(primaryFilesCount); 318 } 319 320 /** 321 * 322 */ getInputFile(int index)323 public Object getInputFile(int index) 324 { 325 return input_files.get(index); 326 } 327 328 /** 329 * 330 */ getInputFiles()331 public Object[] getInputFiles() 332 { 333 return input_files.toArray(); 334 } 335 336 /** 337 * 338 */ getInputFilesAsList()339 public List getInputFilesAsList() 340 { 341 return input_files; 342 } 343 344 /** 345 * 346 */ getInputFilesCount()347 public int getInputFilesCount() 348 { 349 return input_files.size(); 350 } 351 352 /** 353 * 354 */ getCutpointCount()355 public int getCutpointCount() 356 { 357 return cut_points.size(); 358 } 359 360 /** 361 * 362 */ getChapterpointCount()363 public int getChapterpointCount() 364 { 365 return chapter_points.size(); 366 } 367 368 /** 369 * 370 */ getPIDCount()371 public int getPIDCount() 372 { 373 return predefined_IDs.size(); 374 } 375 376 /** 377 * 378 */ getCutpointList()379 public List getCutpointList() 380 { 381 return cut_points; 382 } 383 384 /** 385 * 386 */ getChapterpointList()387 public List getChapterpointList() 388 { 389 return chapter_points; 390 } 391 392 /** 393 * 394 */ getOutputNameParent(String str)395 public String getOutputNameParent(String str) 396 { 397 int index; 398 399 if ( (index = str.lastIndexOf(".")) < 0) 400 return (getOutputDirectory() + getFileSeparator() + str); 401 402 return (getOutputDirectory() + getFileSeparator() + str.substring(0, index)); 403 } 404 405 /** 406 * 407 */ getOutputDirectory()408 public String getOutputDirectory() 409 { 410 return output_directory; 411 } 412 413 /** 414 * 415 */ setOutputDirectory(String value)416 public void setOutputDirectory(String value) 417 { 418 if (value.endsWith(getFileSeparator())) 419 output_directory = value.substring(0, value.length() - 1); 420 421 else 422 output_directory = value; 423 } 424 425 /** 426 * 427 */ checkOutputDirectory()428 public String checkOutputDirectory() 429 { 430 String str = output_directory; 431 432 if (str == null || str.length() == 0 || str.startsWith("[res]")) 433 { 434 if (input_files.size() == 0) 435 return ""; 436 437 str = new File(input_files.get(0).toString()).getParent(); 438 } 439 440 if (checkWriteAccess(str)) 441 { 442 output_directory = str; 443 return null; 444 } 445 446 return str; 447 } 448 449 /** 450 * check write access 451 */ checkWriteAccess(String path)452 private boolean checkWriteAccess(String path) 453 { 454 try { 455 File _path = new File(path); 456 String _file = path + getFileSeparator() + "~$pjx$.tmp"; 457 458 if (path == null || !_path.exists()) 459 return false; 460 461 RandomAccessFile raf = new RandomAccessFile(_file, "rw"); 462 raf.close(); 463 464 new File(_file).delete(); 465 466 } catch (Exception ex2) { 467 468 return false; 469 } 470 471 return true; 472 } 473 474 /** 475 * 476 */ getOutputName()477 public String getOutputName() 478 { 479 return output_name; 480 } 481 482 /** 483 * 484 */ getOutputName(String str)485 public String getOutputName(String str) 486 { 487 if (output_name.length() > 0) 488 return getOutputName(); 489 490 return str; 491 } 492 493 /** 494 * 495 */ setOutputName(String str)496 public void setOutputName(String str) 497 { 498 output_name = str; 499 } 500 501 /** 502 * 503 */ addPID(Object value)504 public void addPID(Object value) 505 { 506 if (!predefined_IDs.contains(value)) 507 predefined_IDs.add(value); 508 } 509 510 /** 511 * 512 */ addPID(Object[] values)513 public void addPID(Object[] values) 514 { 515 for (int i = 0; i < values.length; i++) 516 addPID(values[i]); 517 } 518 519 /** 520 * 521 */ clearPIDs()522 public void clearPIDs() 523 { 524 predefined_IDs.clear(); 525 } 526 527 /** 528 * 529 */ removePID(Object value)530 public void removePID(Object value) 531 { 532 int index; 533 534 if ((index = predefined_IDs.indexOf(value)) < 0) 535 return; 536 537 predefined_IDs.remove(index); 538 } 539 540 /** 541 * 542 */ removePID(Object[] values)543 public void removePID(Object[] values) 544 { 545 for (int i = 0; i < values.length; i++) 546 removePID(values[i]); 547 } 548 549 /** 550 * 551 */ getPIDs()552 public Object[] getPIDs() 553 { 554 return predefined_IDs.toArray(); 555 } 556 557 /** 558 * 559 */ getPIDsAsInteger()560 public int[] getPIDsAsInteger() 561 { 562 int len = predefined_IDs == null ? 0 : predefined_IDs.size(); 563 564 int[] array = new int[len]; 565 566 for (int i = 0; i < len; i++) 567 array[i] = Integer.parseInt(predefined_IDs.get(i).toString().substring(2), 16); 568 569 return array; 570 } 571 572 /** 573 * 574 */ addCutpoint(Object value)575 public void addCutpoint(Object value) 576 { 577 if (!cut_points.contains(value)) 578 cut_points.add(value); 579 } 580 581 /** 582 * 583 */ addCutpoint(int index, Object value)584 public void addCutpoint(int index, Object value) 585 { 586 cut_points.add(index, value); 587 } 588 589 /** 590 * 591 */ addCutpoint(Object[] values)592 public void addCutpoint(Object[] values) 593 { 594 for (int i = 0; i < values.length; i++) 595 addCutpoint(values[i]); 596 } 597 598 /** 599 * 600 */ clearCutpoints()601 public void clearCutpoints() 602 { 603 cut_points.clear(); 604 } 605 606 /** 607 * 608 */ removeCutpoint(int index)609 public Object removeCutpoint(int index) 610 { 611 if (index < 0 || index >= cut_points.size()) 612 return null; 613 614 Object obj = cut_points.remove(index); 615 616 removeCutImage(obj); 617 618 return obj; 619 } 620 621 /** 622 * 623 */ getCutpoints()624 public Object[] getCutpoints() 625 { 626 return cut_points.toArray(); 627 } 628 629 /** 630 * 631 */ addChapterpoint(Object value)632 public void addChapterpoint(Object value) 633 { 634 if (!chapter_points.contains(value)) 635 chapter_points.add(value); 636 } 637 638 /** 639 * 640 */ addChapterpoint(int index, Object value)641 public void addChapterpoint(int index, Object value) 642 { 643 chapter_points.add(index, value); 644 } 645 646 /** 647 * 648 */ addChapterpoint(Object[] values)649 public void addChapterpoint(Object[] values) 650 { 651 for (int i = 0; i < values.length; i++) 652 addChapterpoint(values[i]); 653 } 654 655 /** 656 * 657 */ clearChapterpoints()658 public void clearChapterpoints() 659 { 660 chapter_points.clear(); 661 } 662 663 /** 664 * 665 */ removeChapterpoint(int index)666 public Object removeChapterpoint(int index) 667 { 668 if (index < 0 || index >= chapter_points.size()) 669 return null; 670 671 Object obj = chapter_points.remove(index); 672 673 return obj; 674 } 675 676 /** 677 * 678 */ getChapterpoints()679 public Object[] getChapterpoints() 680 { 681 return chapter_points.toArray(); 682 } 683 684 /** 685 * 686 */ getFirstFileBase()687 public String getFirstFileBase() 688 { 689 return (getOutputDirectory() + getFirstFileName()); 690 } 691 692 /** 693 * 694 */ getFirstFileName()695 public String getFirstFileName() 696 { 697 String firstFileChild = new File(getInputFiles()[0].toString()).getName(); 698 int index; 699 700 if ( (index = firstFileChild.lastIndexOf(".")) != -1 ) 701 firstFileChild = firstFileChild.substring(0, index); 702 703 return firstFileChild; 704 } 705 706 /** 707 * 708 */ getFirstFileDate()709 public long getFirstFileDate() 710 { 711 return ((XInputFile) getInputFiles()[0]).lastModified(); 712 } 713 714 /** 715 * set log files 716 */ setLogFiles()717 public void setLogFiles() 718 { 719 String str = getOutputName(getFirstFileName()); 720 721 if (Common.getSettings().getBooleanProperty(Keys.KEY_DebugLog)) 722 { 723 debug = true; 724 725 setDebugLogStream(getOutputDirectory() + getFileSeparator() + str + "_biglog.txt"); 726 } 727 728 //settings �bergeben!! 729 if (Common.getSettings().getBooleanProperty(Keys.KEY_NormalLog)) 730 { 731 if (Common.getSettings().getBooleanProperty(Keys.KEY_ExternPanel_createVdrIndex) && Common.getSettings().getIntProperty(Keys.KEY_ConversionMode) == 1) 732 normalLog = getOutputDirectory() + getFileSeparator() + "summary.vdr"; 733 734 else 735 normalLog = getOutputDirectory() + getFileSeparator() + str + "_log.txt"; 736 } 737 } 738 739 /** 740 * set systems log output for 'big log' 741 */ setDebugLogStream(String str)742 private void setDebugLogStream(String str) 743 { 744 try { 745 746 logging = new PrintStream(new FileOutputStream(str)); 747 System.setOut(logging); 748 749 } catch (IOException e) { 750 751 Common.setExceptionMessage(e); 752 } 753 } 754 755 /** 756 * 757 */ closeDebugLogStream()758 public void closeDebugLogStream() 759 { 760 if ( !Common.getSettings().getBooleanProperty(Keys.KEY_DebugLog)) 761 return; 762 763 if (logging != null) 764 { 765 logging.flush(); 766 logging.close(); 767 } 768 769 debug = false; 770 } 771 772 773 /** 774 * 775 */ closeNormalLogStream(String str)776 public void closeNormalLogStream(String str) 777 { 778 if ( !Common.getSettings().getBooleanProperty(Keys.KEY_NormalLog)) 779 return; 780 781 try { 782 PrintWriter nlf = new PrintWriter(new FileOutputStream(normalLog)); 783 784 nlf.print(str); 785 nlf.close(); 786 787 } catch (IOException e) { 788 789 Common.setExceptionMessage(e); 790 } 791 } 792 793 /** 794 * 795 */ DebugMode()796 public boolean DebugMode() 797 { 798 return debug; 799 } 800 801 802 /** 803 * 804 */ setPrimaryInputFileSegments(int val)805 public void setPrimaryInputFileSegments(int val) 806 { 807 primaryInputFileSegments = val; 808 } 809 810 /** 811 * 812 */ getPrimaryInputFileSegments()813 public int getPrimaryInputFileSegments() 814 { 815 return primaryInputFileSegments; 816 } 817 818 /** 819 * 820 */ getSecondaryInputFileSegments()821 public int getSecondaryInputFileSegments() 822 { 823 return (getInputFilesCount() - getPrimaryInputFileSegments()); 824 } 825 826 /** 827 * 828 */ getCutImage(Object obj)829 public int[] getCutImage(Object obj) 830 { 831 if (obj != null && cut_images.containsKey(obj)) 832 return ((int[]) cut_images.get(obj)); 833 834 return null; 835 } 836 837 /** 838 * 839 */ setCutImage(String str, int[] data)840 public void setCutImage(String str, int[] data) 841 { 842 cut_images.put(str, data); 843 } 844 845 /** 846 * 847 */ removeCutImage(Object obj)848 public int[] removeCutImage(Object obj) 849 { 850 if (cut_images.containsKey(obj)) 851 return ((int[]) cut_images.remove(obj)); 852 853 return null; 854 } 855 856 /** 857 * the coll table (gui only) 858 */ getCollectionAsTable()859 public Object[][] getCollectionAsTable() 860 { 861 int size = getInputFilesCount(); 862 863 Object[][] table = new Object[size > 10 ? size : 10][12]; 864 865 for (int i = 0; i < size; i++) 866 { 867 XInputFile xInputFile = (XInputFile) getInputFile(i); 868 869 table[i][0] = xInputFile.getFileID(); 870 // table[i][0] = xInputFile.getStreamInfo().getFileID(); 871 table[i][1] = xInputFile.getStreamInfo().getFileSourceBase(); 872 table[i][2] = i < getPrimaryInputFileSegments() ? new Integer(i) : new Integer(-i); 873 table[i][3] = xInputFile.getName(); 874 table[i][4] = (xInputFile.getParent().length() > 0 ? xInputFile.getParent() : xInputFile.toString().substring(0, xInputFile.toString().indexOf(xInputFile.getName()))); 875 table[i][5] = String.valueOf(xInputFile.length() / 1048576L) + " MB"; 876 table[i][6] = Common.formatTime_3(xInputFile.lastModified()); 877 table[i][7] = new Integer(xInputFile.getStreamInfo().getVideoStreams().length); 878 table[i][8] = new Integer(xInputFile.getStreamInfo().getAudioStreams().length); 879 table[i][9] = new Integer(xInputFile.getStreamInfo().getTeletextStreams().length); 880 table[i][10] = new Integer(xInputFile.getStreamInfo().getSubpictureStreams().length); 881 table[i][11] = xInputFile.getStreamInfo().getFileType(); 882 } 883 884 return table; 885 } 886 887 /** 888 * 889 */ setActionType(int value)890 public void setActionType(int value) 891 { 892 action_type = value; 893 } 894 895 /** 896 * 897 */ getActionType()898 public int getActionType() 899 { 900 return action_type; 901 } 902 903 /** 904 * 905 */ getAllSizes()906 public long getAllSizes() 907 { 908 long value = 0; 909 910 for (int i = 0, j = getInputFilesCount(); i < j; i++) 911 value += ((XInputFile) getInputFile(i)).length(); 912 913 return (value / 1048576L); 914 } 915 916 /** 917 * 918 */ getShortSummary()919 public String getShortSummary() 920 { 921 String str = isActive() ? Resource.getString("JobCollection.InProgress") : Resource.getString("JobCollection.Idle"); 922 str += line_separator; 923 str += Resource.getString("JobCollection.Action") + " " + (getActionType() < 0 ? Resource.getString("JobCollection.unspecified") : Keys.ITEMS_ConversionMode[getActionType()].toString()); 924 str += line_separator; 925 str += Resource.getString("JobCollection.PrimaryFileSegments") + " " + getPrimaryInputFileSegments(); 926 str += line_separator; 927 str += Resource.getString("JobCollection.SecondaryFiles") + " " + getSecondaryInputFileSegments(); 928 str += line_separator; 929 str += Resource.getString("JobCollection.Cutpoints") + " " + getCutpointCount(); 930 str += line_separator; 931 str += Resource.getString("JobCollection.Chapters") + " " + getChapterpointCount(); 932 str += line_separator; 933 str += Resource.getString("JobCollection.PidSelection") + " " + getPIDCount(); 934 str += line_separator; 935 str += Resource.getString("JobCollection.OwnSettings") + " " + (settings != null ? Resource.getString("General.Yes") : Resource.getString("General.No")); 936 str += line_separator; 937 str += Resource.getString("JobCollection.AllSize")+ " " + getAllSizes() + "MB"; 938 939 return str; 940 } 941 942 /** 943 * 944 */ hasSettings()945 public boolean hasSettings() 946 { 947 return (settings != null); 948 } 949 950 /** 951 * routing returning settings 952 */ getSettings()953 public Settings getSettings() 954 { 955 if (settings == null) 956 return Common.getSettings(); 957 958 return settings; 959 } 960 961 /** 962 * collection specific settings 963 */ setSettings(Settings _settings)964 public void setSettings(Settings _settings) 965 { 966 if (isActive()) 967 return; 968 969 else if (_settings == null) 970 { 971 settings = null; 972 return; 973 } 974 975 settings = new Settings(); 976 977 try { 978 settings.loadProperties(new ByteArrayInputStream(_settings.storeProperties())); 979 980 } catch (IOException e) { 981 982 settings = null; 983 Common.setExceptionMessage(e); 984 } 985 } 986 987 } 988