2 * PROJECT: NyARToolkit JMF utilities.
\r
3 * --------------------------------------------------------------------------------
\r
5 * Copyright (c) 2008 nyatla
\r
6 * airmail(at)ebony.plala.or.jp
\r
7 * http://nyatla.jp/nyartoolkit/
\r
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
10 * of this software and associated documentation files (the "Software"), to deal
\r
11 * in the Software without restriction, including without limitation the rights
\r
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
13 * copies of the Software, and to permit persons to whom the Software is
\r
14 * furnished to do so, subject to the following conditions:
\r
15 * The above copyright notice and this permission notice shall be included in
\r
16 * all copies or substantial portions of the Software.
\r
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
27 package jp.nyatla.nyartoolkit.jmf.utils;
\r
29 import javax.media.*;
\r
30 import javax.media.protocol.*;
\r
31 import javax.media.control.*;
\r
32 import javax.media.format.*;
\r
35 import javax.media.protocol.DataSource;
\r
37 import jp.nyatla.nyartoolkit.NyARException;
\r
43 public class JmfCameraCapture
\r
45 private Dimension image_size;
\r
47 private JmfCaptureListener capture_listener;
\r
49 // private DataSource jmf_data_source;
\r
50 private MonitorStream jmf_monitor_stream;
\r
52 private Processor jmf_processor;
\r
54 private VideoFormat jmf_video_format;
\r
56 private Buffer read_buf = new Buffer();
\r
58 public static final String PIXEL_FORMAT_RGB = "RGB";
\r
60 public JmfCameraCapture(int i_width, int i_height, float i_rate, String i_pixcel_format)
\r
62 String encoding = i_pixcel_format;// comboEncoding.getSelectedItem();
\r
63 image_size = new Dimension(i_width, i_height);
\r
64 jmf_video_format = new VideoFormat(encoding, image_size, Format.NOT_SPECIFIED, null, i_rate);
\r
67 public Dimension getSize()
\r
72 public javax.media.Buffer readBuffer() throws NyARException
\r
74 if (jmf_monitor_stream == null) {
\r
75 throw new NyARException();
\r
78 jmf_monitor_stream.read(read_buf);
\r
79 } catch (Exception e) {
\r
80 throw new NyARException(e);
\r
85 public void setCaptureListener(JmfCaptureListener i_listener) throws NyARException
\r
87 if (jmf_processor != null) {
\r
88 throw new NyARException();
\r
90 capture_listener = i_listener;
\r
94 public void start() throws NyARException
\r
97 DataSource ds = getCaptureDS(jmf_video_format);
\r
98 VideoFormat[] formats = new VideoFormat[] { new VideoFormat(null) };
\r
99 ProcessorModel pm = new ProcessorModel(ds, formats, null);// ,
\r
102 Processor processor;
\r
104 processor = Manager.createRealizedProcessor(pm);
\r
105 } catch (Exception e) {
\r
106 // Make sure the capture devices are released
\r
108 throw new NyARException(e);
\r
110 // Get the monitor control:
\r
111 // Since there are more than one MonitorControl objects
\r
112 // exported by the DataSource, we get the specific one
\r
113 // that is also the MonitorStream object.
\r
114 jmf_monitor_stream = (MonitorStream) ds.getControl("jmfsample.MonitorStream");
\r
115 jmf_monitor_stream.setCaptureListener(capture_listener);
\r
116 // jmf_data_source=ds;
\r
117 jmf_processor = processor;
\r
118 jmf_processor.start();
\r
123 jmf_processor.stop();
\r
124 jmf_processor.close();
\r
125 jmf_processor = null;
\r
129 protected void finalize()
\r
131 if (jmf_processor != null) {
\r
132 jmf_processor.stop();
\r
133 jmf_processor.close();
\r
134 jmf_processor = null;
\r
138 private static DataSource getCaptureDS(VideoFormat vf)
\r
140 DataSource dsVideo = null;
\r
141 DataSource ds = null;
\r
143 // Create a capture DataSource for the video
\r
144 // If there is no video capture device, then exit with null
\r
146 dsVideo = createDataSource(vf);
\r
147 if (dsVideo == null)
\r
151 // Create the monitoring datasource wrapper
\r
152 if (dsVideo != null) {
\r
153 dsVideo = new MonitorCDS(dsVideo);
\r
157 // Merge the data sources, if both audio and video are available
\r
159 ds = Manager.createMergingDataSource(new DataSource[] { dsVideo });
\r
160 } catch (IncompatibleSourceException ise) {
\r
167 private static DataSource createDataSource(Format format)
\r
171 CaptureDeviceInfo cdi;
\r
174 // Find devices for format
\r
175 devices = CaptureDeviceManager.getDeviceList(format);
\r
176 if (devices.size() < 1) {
\r
177 System.err.println("! No Devices for " + format);
\r
180 // Pick the first device
\r
181 cdi = (CaptureDeviceInfo) devices.elementAt(0);
\r
183 ml = cdi.getLocator();
\r
186 ds = Manager.createDataSource(ml);
\r
188 if (ds instanceof CaptureDevice) {
\r
189 setCaptureFormat((CaptureDevice) ds, format);
\r
191 } catch (Exception e) {
\r
192 System.err.println(e);
\r
198 private static void setCaptureFormat(CaptureDevice cdev, Format format)
\r
200 FormatControl[] fcs = cdev.getFormatControls();
\r
201 if (fcs.length < 1) {
\r
204 FormatControl fc = fcs[0];
\r
205 Format[] formats = fc.getSupportedFormats();
\r
206 for (int i = 0; i < formats.length; i++) {
\r
207 if (formats[i].matches(format)) {
\r
208 format = formats[i].intersects(format);
\r
209 fc.setFormat(format);
\r