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        /* (non-Javadoc)
107         * @see sears.tools.player.PlayerInterface#goToOffset(int)
108         */
109        public void goToOffset(int offset) throws PlayerException {
110            //Not implemented for default player
111        }
112    
113        /* (non-Javadoc)
114         * @see sears.tools.player.PlayerInterface#quit()
115         */
116        public void quit() {
117            //simply kill the process.
118            if(currentProcess != null){
119                currentProcess.destroy();
120            }
121        }
122    
123        /* (non-Javadoc)
124         * @see sears.tools.player.PlayerInterface#getPosition()
125         */
126        public int getPosition() throws PlayerException {
127            // TODO Auto-generated method stub
128            return 0;
129        }
130    
131        /* (non-Javadoc)
132         * @see sears.tools.player.PlayerInterface#getPosition()
133         */
134        public int getLength() throws PlayerException {
135            // TODO Auto-generated method stub
136            return 0;
137        }
138    
139        /* (non-Javadoc)
140         * @see sears.tools.player.PlayerInterface#pause()
141         */
142        public void pause() throws PlayerException {
143            // TODO Auto-generated method stub
144            
145        }
146    
147        /* (non-Javadoc)
148         * @see sears.tools.player.PlayerInterface#pause()
149         */
150        public void stop() throws PlayerException {
151            // TODO Auto-generated method stub
152            
153        }
154    
155        /* (non-Javadoc)
156         * @see sears.tools.player.PlayerInterface#setPosition(int)
157         */
158        public void setPosition(int offset) throws PlayerException {
159            // TODO Auto-generated method stub
160            
161        }
162    }