001    /////////////////////////////////////////////////
002    // This file is part of Sears project.
003    // Subtitle Editor And Re-Synch
004    // A tool to easily modify and resynch movies subtitles.
005    /////////////////////////////////////////////////
006    //This program is free software; 
007    //you can redistribute it and/or modify it under the terms 
008    //of the GNU General Public License 
009    //as published by the Free Software Foundation; 
010    //either version 2 of the License, or (at your option) any later version.
011    /////////////////////////////////////////////////
012    //Sears project is available under sourceforge
013    // at adress: http://sourceforge.net/projects/sears/
014    //Copyright (C) 2005 Booba Skaya
015    //Mail: booba.skaya@gmail.com
016    ////////////////////////////////////////////////
017    package sears.tools.player;
018    
019    import java.io.IOException;
020    import java.io.InputStreamReader;
021    import java.util.ArrayList;
022    import java.util.StringTokenizer;
023    import sears.tools.SearsProperties;
024    import sears.tools.Trace;
025    
026    /**
027     * Class DefaultPlayer.
028     * <br><b>Summary:</b><br>
029     * That is the default player, which permit to launch a player on a video file and a subtitle file.
030     */
031    public class DefaultPlayer implements PlayerInterface {
032        /**The pattern to found in the defines command line, and to be replaced by the file path.*/
033        private static final String FILE_PATTERN = "%f%";
034        /**The pattern to found in the defines command line, and to be replaced by the subtitle path.*/
035        private static final String SUB_PATTERN = "%s%";
036        /**The current process that is playing the video.*/
037        private Process currentProcess;
038        /* (non-Javadoc)
039         * @see sears.tools.player.PlayerInterface#play(java.lang.String, java.lang.String)
040         */
041        public void play(String videoFile, String subtitleFile) throws PlayerException {
042            if(currentProcess != null){
043                currentProcess.destroy();
044            }
045            //beware of null subtitle file.
046            if(subtitleFile == null){
047                subtitleFile = "";
048            }
049            //First is to construct the command that will be executed.
050            String playerCommand = SearsProperties.getProperty(SearsProperties.PLAYER_FULL_PATH);
051            //To do that, we will parse the command line given, spliting in tokens.
052            StringTokenizer stk = new StringTokenizer(playerCommand);
053            //The array of commands.
054            ArrayList<String> commands = new ArrayList<String>();
055            while(stk.hasMoreTokens()){
056                //get the next token in command line.
057                String token = stk.nextToken();
058                //If found the file pattern, replace with videoFile.
059                if(token.equals(FILE_PATTERN)){
060                    token = videoFile;
061                }else if (token.equals(SUB_PATTERN)){
062                    //else if found the subtitle patter, replace with the subtitle file.
063                    token = subtitleFile;
064                }
065                //Add the commands token to array.
066                commands.add(token);
067            }
068            try {
069                //Then just have to launch the process with commands array.
070                currentProcess = Runtime.getRuntime().exec((String[]) commands.toArray(new String[commands.size()]));
071                
072                //Flust the outputs not block the process.
073                final InputStreamReader input = new InputStreamReader(currentProcess.getInputStream());
074                Thread inputFlusher = new Thread(){
075                  public void run(){
076                      char c;
077                      try{
078                      while((c = (char) input.read()) != -1){
079                          System.out.print(c);
080                      }
081                      }catch(IOException e){
082                          Trace.trace(e.getMessage(), Trace.ERROR_PRIORITY);
083                      }
084                  }
085                };
086                inputFlusher.start();
087                final InputStreamReader err = new InputStreamReader(currentProcess.getErrorStream());
088                Thread errFlusher = new Thread(){
089                    public void run(){
090                        char c;
091                        try{
092                        while((c = (char) err.read()) != -1){
093                            System.err.print(c);
094                        }
095                        }catch(IOException e){
096                            Trace.trace(e.getMessage(), Trace.ERROR_PRIORITY);
097                        }
098                    }
099                  };
100                  errFlusher.start();
101            } catch (IOException e) {
102                throw new PlayerException("IOException ("+e.getMessage()+") occurs when launching :"+playerCommand);
103            }
104        }
105    
106        /**
107         * Empty method, not implemented for default player
108         * @param offset                    the offset
109         * @throws PlayerException  if method failed
110         */
111        public void goToOffset(int offset) throws PlayerException {
112            //Not implemented for default player
113        }
114    
115        /* (non-Javadoc)
116         * @see sears.tools.player.PlayerInterface#quit()
117         */
118        public void quit() {
119            //simply kill the process.
120            if(currentProcess != null){
121                currentProcess.destroy();
122            }
123        }
124    
125        /* (non-Javadoc)
126         * @see sears.tools.player.PlayerInterface#getPosition()
127         */
128        public int getPosition() throws PlayerException {
129            // TODO Auto-generated method stub
130            return 0;
131        }
132    
133        /* (non-Javadoc)
134         * @see sears.tools.player.PlayerInterface#getPosition()
135         */
136        public int getLength() throws PlayerException {
137            // TODO Auto-generated method stub
138            return 0;
139        }
140    
141        /* (non-Javadoc)
142         * @see sears.tools.player.PlayerInterface#pause()
143         */
144        public void pause() throws PlayerException {
145            // TODO Auto-generated method stub
146            
147        }
148    
149        /* (non-Javadoc)
150         * @see sears.tools.player.PlayerInterface#pause()
151         */
152        public void stop() throws PlayerException {
153            // TODO Auto-generated method stub
154            
155        }
156    
157        /* (non-Javadoc)
158         * @see sears.tools.player.PlayerInterface#setPosition(int)
159         */
160        public void setPosition(int offset) throws PlayerException {
161            // TODO Auto-generated method stub
162            
163        }
164    }