JMF Example
  1 
  2 package jmfexample;
  3 
  4 import java.io.*;
  5 import java.net.URL;
  6 import java.net.MalformedURLException;
  7 import java.awt.*;
  8 import javax.swing.JFileChooser;
  9 import javax.media.*;
 10 import javax.media.control.*;
 11 import javax.media.protocol.*;
 12 import javax.media.format.VideoFormat;
 13 
 14 import javax.swing.JOptionPane;
 15 
 16 
 17 public class mainFrame extends javax.swing.JFrame {
 18     
 19     private camDataSource dataSource;
 20     
 21     private DataSource camSource;
 22     private DataSource recordCamSource;
 23     private DataSink dataSink;
 24     private Processor processor;
 25     private Processor recordProcessor;
 26     private camStateHelper playhelper;
 27     
 28     private JFileChooser movieChooser;
 29     
 30     public mainFrame(camDataSource dataSource) {
 31         this.dataSource = dataSource;
 32         this.dataSource.setParent(this);
 33         camSource = dataSource.cloneCamSource();
 34         initComponents();
 35         try{
 36             processor = Manager.createProcessor(camSource);
 37         }catch (IOException e) {
 38             JOptionPane.showMessageDialog(this, "Exception creating processor: " + e.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
 39             return;
 40         }catch (NoProcessorException e) {
 41             JOptionPane.showMessageDialog(this, "Exception creating processor: " + e.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
 42             return;
 43         }
 44         playhelper = new camStateHelper(processor);
 45         if(!playhelper.configure(10000)){
 46             JOptionPane.showMessageDialog(this, "cannot configure processor", "Error", JOptionPane.WARNING_MESSAGE);
 47             return;
 48         }
 49         
 50         processor.setContentDescriptor(null);
 51         if(!playhelper.realize(10000)){
 52             JOptionPane.showMessageDialog(this, "cannot realize processor", "Error", JOptionPane.WARNING_MESSAGE);
 53             return;
 54         }
 55         checkIncoding(processor.getTrackControls());
 56         setJPEGQuality(processor, 1.0f);
 57         //Control control = processor.getControl("javax.media.control.FrameRateControl");
 58        // if ( control != null && control instanceof javax.media.control.FrameRateControl ) ((javax.media.control.FrameRateControl)control).setFrameRate(5f);
 59         processor.start();
 60         try{
 61             Thread.sleep(10000);
 62         }catch(java.lang.InterruptedException e){}
 63         
 64          Control control = processor.getControl("javax.media.control.FrameRateControl");
 65         if ( control != null && control instanceof javax.media.control.FrameRateControl ){
 66             ((javax.media.control.FrameRateControl)control).setFrameRate(15.0f);
 67         } else{
 68             System.out.println("no frame control");
 69         }
 70         
 71         processor.getVisualComponent().setBackground(Color.gray);
 72         centerPanel.add(processor.getVisualComponent(), BorderLayout.CENTER);
 73         centerPanel.add(processor.getControlPanelComponent(), BorderLayout.SOUTH);
 74     }
 75     
 76     
 77     private void initComponents() {                          
 78         northPanel = new javax.swing.JPanel();
 79         messageLabel = new javax.swing.JLabel();
 80         southPanel = new javax.swing.JPanel();
 81         mainToolBar = new javax.swing.JToolBar();
 82         recordButton = new javax.swing.JButton();
 83         fileLabel = new javax.swing.JLabel();
 84         centerPanel = new javax.swing.JPanel();
 85 
 86         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 87         setTitle("My Webcam");
 88         addWindowListener(new java.awt.event.WindowAdapter() {
 89             public void windowClosing(java.awt.event.WindowEvent evt) {
 90                 formWindowClosing(evt);
 91             }
 92         });
 93 
 94         northPanel.setLayout(new java.awt.BorderLayout());
 95 
 96         messageLabel.setText("Status");
 97         northPanel.add(messageLabel, java.awt.BorderLayout.CENTER);
 98 
 99         getContentPane().add(northPanel, java.awt.BorderLayout.NORTH);
100 
101         southPanel.setLayout(new java.awt.BorderLayout());
102 
103         recordButton.setText("Record");
104         recordButton.addActionListener(new java.awt.event.ActionListener() {
105             public void actionPerformed(java.awt.event.ActionEvent evt) {
106                 recordButtonActionPerformed(evt);
107             }
108         });
109 
110         mainToolBar.add(recordButton);
111 
112         fileLabel.setText("File:");
113         mainToolBar.add(fileLabel);
114 
115         southPanel.add(mainToolBar, java.awt.BorderLayout.CENTER);
116 
117         getContentPane().add(southPanel, java.awt.BorderLayout.SOUTH);
118 
119         centerPanel.setLayout(new java.awt.BorderLayout());
120 
121         getContentPane().add(centerPanel, java.awt.BorderLayout.CENTER);
122 
123         pack();
124     }                        
125     
126     private void formWindowClosing(java.awt.event.WindowEvent evt) {                                   
127         processor.close();
128     }                                  
129     
130     private void recordButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
131         if(recordButton.getText().equals("Record")){
132             fileLabel.setText("File:");
133             if (movieChooser == null) movieChooser = new JFileChooser();
134             movieChooser.setDialogType(JFileChooser.SAVE_DIALOG);
135             //Add a custom file filter and disable the default
136             //(Accept All) file filter.
137             movieChooser.addChoosableFileFilter(new MOVFilter());
138             movieChooser.setAcceptAllFileFilterUsed(false);
139             movieChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
140             int returnVal = movieChooser.showDialog(this, "Record");
141             if (returnVal == JFileChooser.APPROVE_OPTION) {
142                 File file = movieChooser.getSelectedFile();
143                 if(!file.getName().endsWith(".mov")&&!file.getName().endsWith(".MOV")) file = new File(file.toString() + ".mov");
144                 recordToFile(file);
145                 fileLabel.setText("File:" + file.toString());
146                 recordButton.setText("Stop");
147             }
148         }else{
149             stopRecording();
150             recordButton.setText("Record");
151         }
152     }                                            
153     
154     void setJPEGQuality(Player p, float val) {
155         Control cs[] = p.getControls();
156         QualityControl qc = null;
157         VideoFormat jpegFmt = new VideoFormat(VideoFormat.MPEG);
158         
159         // Loop through the controls to find the Quality control for
160         // the JPEG encoder.
161         for (int i = 0; i < cs.length; i++) {
162             if (cs[i] instanceof QualityControl && cs[i] instanceof Owned) {
163                 Object owner = ((Owned)cs[i]).getOwner();
164                 // Check to see if the owner is a Codec.
165                 // Then check for the output format.
166                 if (owner instanceof Codec) {
167                     Format fmts[] = ((Codec)owner).getSupportedOutputFormats(null);
168                     for (int j = 0; j < fmts.length; j++) {
169                         if (fmts[j].matches(jpegFmt)) {
170                             qc = (QualityControl)cs[i];
171                             qc.setQuality(val);
172                             break;
173                         }
174                     }
175                 }
176                 if (qc != null) break;
177             }
178         }
179     }
180     
181     public void checkIncoding(TrackControl track[]){
182         for (int i = 0; i < track.length; i++) {
183             Format format = track[i].getFormat();
184             if (track[i].isEnabled() && format instanceof VideoFormat) {
185                 Dimension size = ((VideoFormat)format).getSize();
186                 float frameRate = ((VideoFormat)format).getFrameRate();
187                 int w = (size.width % 8 == 0 ? size.width :(int)(size.width / 8) * 8);
188                 int h = (size.height % 8 == 0 ? size.height :(int)(size.height / 8) * 8);
189                 VideoFormat jpegFormat = new VideoFormat(VideoFormat.JPEG_RTP, new Dimension(w, h), Format.NOT_SPECIFIED, Format.byteArray, frameRate);
190                 
191                 //VideoFormat jpegFormat = new VideoFormat(((VideoFormat)format).getEncoding(), new Dimension(w, h), ((VideoFormat)format).getMaxDataLength(), ((VideoFormat)format).getDataType(), 15.0f);
192                 //track[i].setFormat(jpegFormat);
193                 
194                 messageLabel.setText("Status: Video transmitted as: " + jpegFormat.toString());
195             }
196         }
197     }
198     
199     public void recordToFile(File file){
200         URL movieUrl = null;
201         MediaLocator dest = null;
202         try{
203             movieUrl = file.toURL();
204             dest = new MediaLocator(movieUrl);
205         }catch(MalformedURLException e){
206             
207         }
208         
209         recordCamSource = dataSource.cloneCamSource();
210         try{
211             recordProcessor = Manager.createProcessor(recordCamSource);
212         }catch (IOException e) {
213             JOptionPane.showMessageDialog(this, "Exception creating record processor: " + e.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
214             return;
215         }catch (NoProcessorException e) {
216             JOptionPane.showMessageDialog(this, "Exception creating record processor: " + e.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
217             return;
218         }
219         playhelper = new camStateHelper(recordProcessor);
220         if(!playhelper.configure(10000)){
221             JOptionPane.showMessageDialog(this, "cannot configure record processor", "Error", JOptionPane.WARNING_MESSAGE);
222             return;
223         }
224         
225         VideoFormat vfmt = new VideoFormat(VideoFormat.CINEPAK);
226         (recordProcessor.getTrackControls())[0].setFormat(vfmt);
227         (recordProcessor.getTrackControls())[0].setEnabled(true);
228         recordProcessor.setContentDescriptor(new FileTypeDescriptor(FileTypeDescriptor.QUICKTIME));
229         
230         if(!playhelper.realize(10000)){
231             JOptionPane.showMessageDialog(this, "cannot realize processor", "Error", JOptionPane.WARNING_MESSAGE);
232             return;
233         }
234        // Control control = recordProcessor.getControl("javax.media.control.FrameRateControl");
235         //if ( control != null && control instanceof javax.media.control.FrameRateControl ) ((javax.media.control.FrameRateControl)control).setFrameRate(0.7f);
236         try {
237             if(recordProcessor.getDataOutput()==null){
238                 JOptionPane.showMessageDialog(this, "No Data Output", "Error", JOptionPane.WARNING_MESSAGE);
239                 return;
240             }
241             dataSink = Manager.createDataSink(recordProcessor.getDataOutput(), dest);
242             recordProcessor.start();
243             dataSink.open();
244             dataSink.start();
245         } catch (NoDataSinkException ex) {
246             JOptionPane.showMessageDialog(this, "No DataSink " + ex.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
247         } catch (IOException ex) {
248             JOptionPane.showMessageDialog(this, "IOException " + ex.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
249         }
250     }
251     
252     public void stopRecording(){
253         try {
254             recordProcessor.close();
255             dataSink.stop();
256             dataSink.close();
257         } catch (IOException e) {
258             JOptionPane.showMessageDialog(this, "cannot stop recording " + e.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
259         }
260     }
261     
262     // Variables declaration - do not modify                     
263     private javax.swing.JPanel centerPanel;
264     private javax.swing.JLabel fileLabel;
265     private javax.swing.JToolBar mainToolBar;
266     private javax.swing.JLabel messageLabel;
267     private javax.swing.JPanel northPanel;
268     private javax.swing.JButton recordButton;
269     private javax.swing.JPanel southPanel;
270     // End of variables declaration                   
271     
272 }
273