OSDN Git Service

[更新]NyARToolkit
[nyartoolkit-and/nyartoolkit-and.git] / src.utils / jmf / jp / nyatla / nyartoolkit / jmf / utils / MonitorStream.java
1 /*
2  * Copyright (c) 1996-2001 Sun Microsystems, Inc. All Rights Reserved.
3  *
4  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
5  * modify and redistribute this software in source and binary code form,
6  * provided that i) this copyright notice and license appear on all copies of
7  * the software; and ii) Licensee does not utilize the software in a manner
8  * which is disparaging to Sun.
9  *
10  * This software is provided "AS IS," without a warranty of any kind. ALL
11  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
12  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
13  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
14  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
15  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
16  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
17  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
18  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
19  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
20  * POSSIBILITY OF SUCH DAMAGES.
21  *
22  * This software is not designed or intended for use in on-line control of
23  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
24  * the design, construction, operation or maintenance of any nuclear
25  * facility. Licensee represents and warrants that it will not use or
26  * redistribute the Software for such purposes.
27  */
28 package jp.nyatla.nyartoolkit.jmf.utils;
29
30 import javax.media.*;
31 import javax.media.protocol.*;
32
33 import javax.media.util.BufferToImage;
34 import java.io.IOException;
35 import java.awt.*;
36
37 public class MonitorStream implements PushBufferStream, BufferTransferHandler
38 {
39
40         JmfCaptureListener img_listener;
41
42         PushBufferStream actual = null;
43
44         boolean dataAvailable = false;
45
46         boolean terminate = false;
47
48         boolean enabled = false;
49
50         Object bufferLock = new Object();
51
52         Buffer cbuffer = new Buffer();
53
54         BufferTransferHandler transferHandler = null;
55
56         Component component = null;
57
58         MonitorCDS cds;
59
60         BufferToImage bti = null;
61
62         MonitorStream(PushBufferStream actual, MonitorCDS cds)
63         {
64                 this.actual = actual;
65                 actual.setTransferHandler(this);
66                 this.cds = cds;
67         }
68
69         public javax.media.Format getFormat()
70         {
71                 return actual.getFormat();
72         }
73
74         /**
75          * 非同期READ
76          */
77         public void read(Buffer buffer) throws IOException
78         {
79                 // Wait for data to be available
80                 // Doesn't get used much because the transferData
81                 // call is made when data IS available. And most
82                 // Processors/Players read the data in the same
83                 // thread that called transferData, although that's
84                 // not a safe assumption to make
85                 if (!dataAvailable) {
86                         synchronized (bufferLock) {
87                                 while (!dataAvailable && !terminate) {
88                                         try {
89                                                 bufferLock.wait(100);
90                                         } catch (InterruptedException ie) {
91                                         }
92                                 }
93                         }
94                 }
95
96                 if (dataAvailable) {
97                         synchronized (bufferLock) {
98                                 // Copy the buffer attributes, but swap the data
99                                 // attributes so that no extra copy is made.
100                                 buffer.copy(cbuffer, true);
101                                 //dataAvailable = false;
102                         }
103                 }
104                 //      return;
105         }
106
107         public void setCaptureListener(JmfCaptureListener i_listener)
108         {
109                 img_listener = i_listener;
110         }
111
112         public void transferData(PushBufferStream pbs)
113         {
114                 // Get the data from the original source stream
115                 synchronized (bufferLock) {
116                         try {
117                                 pbs.read(cbuffer);
118                         } catch (IOException ioe) {
119                                 return;
120                         }
121                         dataAvailable = true;
122                         bufferLock.notifyAll();
123                 }
124                 if (img_listener != null) {
125                         img_listener.onUpdateBuffer(cbuffer);
126                 }
127
128                 /*
129                  // Display data if monitor is active
130                  if (isEnabled()) {
131                  if (bti == null) {
132                  VideoFormat vf = (VideoFormat) cbuffer.getFormat();
133                  bti = new BufferToImage(vf);
134                  }
135                  if (bti != null && component != null) {
136                  Image im = bti.createImage(cbuffer);
137                  Graphics g = component.getGraphics();
138                  Dimension size = component.getSize();
139                  if (g != null)
140                  g.drawImage(im, 0, 0, component);
141                  }
142                  }
143                  */
144                 // Maybe synchronize this with setTransferHandler() ?
145                 if (transferHandler != null && cds.delStarted)
146                         transferHandler.transferData(this);
147         }
148
149         public void setTransferHandler(BufferTransferHandler transferHandler)
150         {
151                 this.transferHandler = transferHandler;
152         }
153
154         public boolean setEnabled(boolean value)
155         {
156                 enabled = value;
157                 if (value == false) {
158                         if (!cds.delStarted) {
159                                 try {
160                                         cds.stopDelegate();
161                                 } catch (IOException ioe) {
162                                 }
163                         }
164                 } else {
165                         // Start the capture datasource if the monitor is enabled
166                         try {
167                                 cds.startDelegate();
168                         } catch (IOException ioe) {
169                         }
170                 }
171                 return enabled;
172         }
173
174         public boolean isEnabled()
175         {
176                 return enabled;
177         }
178
179         public float setPreviewFrameRate(float rate)
180         {
181                 System.err.println("TODO");
182                 return rate;
183         }
184
185         public ContentDescriptor getContentDescriptor()
186         {
187                 return actual.getContentDescriptor();
188         }
189
190         public long getContentLength()
191         {
192                 return actual.getContentLength();
193         }
194
195         public boolean endOfStream()
196         {
197                 return actual.endOfStream();
198         }
199
200         public Object[] getControls()
201         {
202                 return new Object[0];
203         }
204
205         public Object getControl(String str)
206         {
207                 return null;
208         }
209
210 }