OSDN Git Service

[TAG]NyARToolkit-2.0.0
[nyartoolkit-and/nyartoolkit-and.git] / branches / nyatla / src.utils / jmf / jp / nyatla / nyartoolkit / jmf / utils / JmfCameraCapture.java
1 /* \r
2  * PROJECT: NyARToolkit JMF utilities.\r
3  * --------------------------------------------------------------------------------\r
4  * The MIT License\r
5  * Copyright (c) 2008 nyatla\r
6  * airmail(at)ebony.plala.or.jp\r
7  * http://nyatla.jp/nyartoolkit/\r
8  * \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
17  * \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
24  * THE SOFTWARE.\r
25  * \r
26  */\r
27 package jp.nyatla.nyartoolkit.jmf.utils;\r
28 \r
29 import javax.media.*;\r
30 import javax.media.protocol.*;\r
31 import javax.media.control.*;\r
32 import javax.media.format.*;\r
33 import java.awt.*;\r
34 import java.util.*;\r
35 import javax.media.protocol.DataSource;\r
36 \r
37 import jp.nyatla.nyartoolkit.NyARException;\r
38 /**\r
39  * 簡易JMFキャプチャクラス\r
40  * @author atla\r
41  *\r
42  */\r
43 public class JmfCameraCapture\r
44 {\r
45         private Dimension image_size;\r
46 \r
47         private JmfCaptureListener capture_listener;\r
48 \r
49         // private DataSource jmf_data_source;\r
50         private MonitorStream jmf_monitor_stream;\r
51 \r
52         private Processor jmf_processor;\r
53 \r
54         private VideoFormat jmf_video_format;\r
55 \r
56         private Buffer read_buf = new Buffer();\r
57 \r
58         public static final String PIXEL_FORMAT_RGB = "RGB";\r
59 \r
60         public JmfCameraCapture(int i_width, int i_height, float i_rate, String i_pixcel_format)\r
61         {\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
65         }\r
66 \r
67         public Dimension getSize()\r
68         {\r
69                 return image_size;\r
70         }\r
71 \r
72         public javax.media.Buffer readBuffer() throws NyARException\r
73         {\r
74                 if (jmf_monitor_stream == null) {\r
75                         throw new NyARException();\r
76                 }\r
77                 try {\r
78                         jmf_monitor_stream.read(read_buf);\r
79                 } catch (Exception e) {\r
80                         throw new NyARException(e);\r
81                 }\r
82                 return read_buf;\r
83         }\r
84 \r
85         public void setCaptureListener(JmfCaptureListener i_listener) throws NyARException\r
86         {\r
87                 if (jmf_processor != null) {\r
88                         throw new NyARException();\r
89                 }\r
90                 capture_listener = i_listener;\r
91 \r
92         }\r
93 \r
94         public void start() throws NyARException\r
95         {\r
96 \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
100                 // formats,\r
101                 // ftd);\r
102                 Processor processor;\r
103                 try {\r
104                         processor = Manager.createRealizedProcessor(pm);\r
105                 } catch (Exception e) {\r
106                         // Make sure the capture devices are released\r
107                         ds.disconnect();\r
108                         throw new NyARException(e);\r
109                 }\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
119         }\r
120 \r
121         public void stop()\r
122         {\r
123                 jmf_processor.stop();\r
124                 jmf_processor.close();\r
125                 jmf_processor = null;\r
126 \r
127         }\r
128 \r
129         protected void finalize()\r
130         {\r
131                 if (jmf_processor != null) {\r
132                         jmf_processor.stop();\r
133                         jmf_processor.close();\r
134                         jmf_processor = null;\r
135                 }\r
136         }\r
137 \r
138         private static DataSource getCaptureDS(VideoFormat vf)\r
139         {\r
140                 DataSource dsVideo = null;\r
141                 DataSource ds = null;\r
142 \r
143                 // Create a capture DataSource for the video\r
144                 // If there is no video capture device, then exit with null\r
145                 if (vf != null) {\r
146                         dsVideo = createDataSource(vf);\r
147                         if (dsVideo == null)\r
148                                 return null;\r
149                 }\r
150 \r
151                 // Create the monitoring datasource wrapper\r
152                 if (dsVideo != null) {\r
153                         dsVideo = new MonitorCDS(dsVideo);\r
154                         return dsVideo;\r
155                 }\r
156 \r
157                 // Merge the data sources, if both audio and video are available\r
158                 try {\r
159                         ds = Manager.createMergingDataSource(new DataSource[] { dsVideo });\r
160                 } catch (IncompatibleSourceException ise) {\r
161                         return null;\r
162                 }\r
163 \r
164                 return ds;\r
165         }\r
166 \r
167         private static DataSource createDataSource(Format format)\r
168         {\r
169                 DataSource ds;\r
170                 Vector devices;\r
171                 CaptureDeviceInfo cdi;\r
172                 MediaLocator ml;\r
173 \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
178                         return null;\r
179                 }\r
180                 // Pick the first device\r
181                 cdi = (CaptureDeviceInfo) devices.elementAt(0);\r
182 \r
183                 ml = cdi.getLocator();\r
184 \r
185                 try {\r
186                         ds = Manager.createDataSource(ml);\r
187                         ds.connect();\r
188                         if (ds instanceof CaptureDevice) {\r
189                                 setCaptureFormat((CaptureDevice) ds, format);\r
190                         }\r
191                 } catch (Exception e) {\r
192                         System.err.println(e);\r
193                         return null;\r
194                 }\r
195                 return ds;\r
196         }\r
197 \r
198         private static void setCaptureFormat(CaptureDevice cdev, Format format)\r
199         {\r
200                 FormatControl[] fcs = cdev.getFormatControls();\r
201                 if (fcs.length < 1) {\r
202                         return;\r
203                 }\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
210                                 break;\r
211                         }\r
212                 }\r
213         }\r
214 }