OSDN Git Service

[TAG]2.4.1
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.4.1 / 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 /**\r
40  * 簡易JMFキャプチャクラス\r
41  * @deprecated このクラスは近い将来削除します。\r
42  * JmfCaptureDeviceList/JmfCaptureDeviceを使用してください。\r
43  * {@link #JmfCaptureDeviceList()}\r
44  *\r
45  */\r
46 @Deprecated public class JmfCameraCapture\r
47 {\r
48         private Dimension image_size;\r
49 \r
50         private JmfCaptureListener capture_listener;\r
51 \r
52         // private DataSource jmf_data_source;\r
53         private MonitorStream jmf_monitor_stream;\r
54 \r
55         private Processor jmf_processor;\r
56 \r
57         private VideoFormat jmf_video_format;\r
58 \r
59         private Buffer read_buf = new Buffer();\r
60 \r
61         public static final String PIXEL_FORMAT_RGB = "RGB";\r
62 \r
63         public JmfCameraCapture(int i_width, int i_height, float i_rate, String i_pixcel_format)\r
64         {\r
65                 String encoding = i_pixcel_format;// comboEncoding.getSelectedItem();\r
66                 image_size = new Dimension(i_width, i_height);\r
67                 jmf_video_format = new VideoFormat(encoding, image_size, Format.NOT_SPECIFIED, null, i_rate);\r
68         }\r
69 \r
70         public Dimension getSize()\r
71         {\r
72                 return image_size;\r
73         }\r
74 \r
75         public javax.media.Buffer readBuffer() throws NyARException\r
76         {\r
77                 if (jmf_monitor_stream == null) {\r
78                         throw new NyARException();\r
79                 }\r
80                 try {\r
81                         jmf_monitor_stream.read(read_buf);\r
82                 } catch (Exception e) {\r
83                         throw new NyARException(e);\r
84                 }\r
85                 return read_buf;\r
86         }\r
87 \r
88         public void setCaptureListener(JmfCaptureListener i_listener) throws NyARException\r
89         {\r
90                 if (jmf_processor != null)\r
91                 {\r
92                         throw new NyARException();\r
93                 }\r
94                 capture_listener = i_listener;\r
95 \r
96         }\r
97 \r
98         public void start() throws NyARException\r
99         {\r
100 \r
101                 DataSource ds = getCaptureDS(jmf_video_format);\r
102                 VideoFormat[] formats = new VideoFormat[] { new VideoFormat(null) };\r
103                 ProcessorModel pm = new ProcessorModel(ds, formats, null);// ,\r
104                 // formats,\r
105                 // ftd);\r
106                 Processor processor;\r
107                 try {\r
108                         processor = Manager.createRealizedProcessor(pm);\r
109                 } catch (Exception e) {\r
110                         // Make sure the capture devices are released\r
111                         ds.disconnect();\r
112                         throw new NyARException(e);\r
113                 }\r
114                 // Get the monitor control:\r
115                 // Since there are more than one MonitorControl objects\r
116                 // exported by the DataSource, we get the specific one\r
117                 // that is also the MonitorStream object.\r
118                 jmf_monitor_stream = (MonitorStream) ds.getControl("jmfsample.MonitorStream");\r
119                 jmf_monitor_stream.setCaptureListener(capture_listener);\r
120                 // jmf_data_source=ds;\r
121                 jmf_processor = processor;\r
122                 jmf_processor.start();\r
123         }\r
124 \r
125         public void stop()\r
126         {\r
127                 jmf_processor.stop();\r
128                 jmf_processor.close();\r
129                 jmf_processor = null;\r
130 \r
131         }\r
132 \r
133         protected void finalize()\r
134         {\r
135                 if (jmf_processor != null) {\r
136                         jmf_processor.stop();\r
137                         jmf_processor.close();\r
138                         jmf_processor = null;\r
139                 }\r
140         }\r
141 \r
142         private static DataSource getCaptureDS(VideoFormat vf)\r
143         {\r
144                 DataSource dsVideo = null;\r
145                 DataSource ds = null;\r
146 \r
147                 // Create a capture DataSource for the video\r
148                 // If there is no video capture device, then exit with null\r
149                 if (vf != null) {\r
150                         dsVideo = createDataSource(vf);\r
151                         if (dsVideo == null)\r
152                                 return null;\r
153                 }\r
154 \r
155                 // Create the monitoring datasource wrapper\r
156                 if (dsVideo != null) {\r
157                         dsVideo = new MonitorCDS(dsVideo);\r
158                         return dsVideo;\r
159                 }\r
160 \r
161                 // Merge the data sources, if both audio and video are available\r
162                 try {\r
163                         ds = Manager.createMergingDataSource(new DataSource[] { dsVideo });\r
164                 } catch (IncompatibleSourceException ise) {\r
165                         return null;\r
166                 }\r
167 \r
168                 return ds;\r
169         }\r
170 \r
171         private static DataSource createDataSource(Format format)\r
172         {\r
173                 DataSource ds;\r
174                 Vector devices;\r
175                 CaptureDeviceInfo cdi;\r
176                 MediaLocator ml;\r
177 \r
178                 // Find devices for format\r
179                 devices = CaptureDeviceManager.getDeviceList(format);\r
180                 if (devices.size() < 1) {\r
181                         System.err.println("! No Devices for " + format);\r
182                         return null;\r
183                 }\r
184                 // Pick the first device\r
185                 cdi = (CaptureDeviceInfo) devices.elementAt(0);\r
186 \r
187                 ml = cdi.getLocator();\r
188 \r
189                 try {\r
190                         ds = Manager.createDataSource(ml);\r
191                         ds.connect();\r
192                         if (ds instanceof CaptureDevice) {\r
193                                 setCaptureFormat((CaptureDevice) ds, format);\r
194                         }\r
195                 } catch (Exception e) {\r
196                         System.err.println(e);\r
197                         return null;\r
198                 }\r
199                 return ds;\r
200         }\r
201 \r
202         private static void setCaptureFormat(CaptureDevice cdev, Format format)\r
203         {\r
204                 FormatControl[] fcs = cdev.getFormatControls();\r
205                 if (fcs.length < 1) {\r
206                         return;\r
207                 }\r
208                 FormatControl fc = fcs[0];\r
209                 Format[] formats = fc.getSupportedFormats();\r
210                 for (int i = 0; i < formats.length; i++) {\r
211                         if (formats[i].matches(format)) {\r
212                                 format = formats[i].intersects(format);\r
213                                 fc.setFormat(format);\r
214                                 break;\r
215                         }\r
216                 }\r
217         }\r
218 }