OSDN Git Service

Copied remotely
[nyartoolkit-and/nyartoolkit-and.git] / trunk / 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
31
32 import javax.media.*;
33 import javax.media.protocol.*;
34
35 import javax.media.util.BufferToImage;
36 import java.io.IOException;
37 import java.awt.*;
38
39 public class MonitorStream implements PushBufferStream, BufferTransferHandler {
40
41     JmfCaptureListener img_listener;
42     PushBufferStream actual = null;
43     boolean dataAvailable = false;
44     boolean terminate = false;
45     boolean enabled = false;
46     Object bufferLock = new Object();
47     Buffer cbuffer = new Buffer();
48     BufferTransferHandler transferHandler = null;
49     Component component = null;
50     MonitorCDS cds;
51     BufferToImage bti = null;
52     
53     MonitorStream(PushBufferStream actual, MonitorCDS cds) {
54         this.actual = actual;
55         actual.setTransferHandler(this);
56         this.cds = cds;
57     }
58
59     public javax.media.Format getFormat()
60     {
61         return actual.getFormat();
62     }
63     /**
64      * 非同期READ
65      */
66     public void read(Buffer buffer) throws IOException
67     {
68         // Wait for data to be available
69         // Doesn't get used much because the transferData
70         // call is made when data IS available. And most
71         // Processors/Players read the data in the same
72         // thread that called transferData, although that's
73         // not a safe assumption to make
74         if (!dataAvailable) {
75             synchronized (bufferLock) {
76                 while (!dataAvailable && !terminate) {
77                     try {
78                         bufferLock.wait(100);
79                     } catch (InterruptedException ie) {
80                     }
81                 }
82             }
83         }
84
85         if (dataAvailable) {
86             synchronized (bufferLock) {
87                 // Copy the buffer attributes, but swap the data
88                 // attributes so that no extra copy is made.
89                 buffer.copy(cbuffer, true);
90                 //dataAvailable = false;
91             }
92         }
93 //      return;
94     }
95     public void setCaptureListener(JmfCaptureListener i_listener)
96     {
97         img_listener=i_listener;
98     }
99
100     public void transferData(PushBufferStream pbs)
101     {
102         // Get the data from the original source stream
103         synchronized (bufferLock) {
104             try {
105                 pbs.read(cbuffer);
106             } catch (IOException ioe) {
107                 return;
108             }
109             dataAvailable = true;
110             bufferLock.notifyAll();
111         }
112         if(img_listener!=null){
113             img_listener.onUpdateBuffer(cbuffer);
114         }
115         
116 /*
117         // Display data if monitor is active
118         if (isEnabled()) {
119             if (bti == null) {
120                 VideoFormat vf = (VideoFormat) cbuffer.getFormat();
121                 bti = new BufferToImage(vf);
122             }
123             if (bti != null && component != null) {
124                 Image im = bti.createImage(cbuffer);
125                 Graphics g = component.getGraphics();
126                 Dimension size = component.getSize();
127                 if (g != null)
128                     g.drawImage(im, 0, 0, component);
129             }
130         }
131 */
132         // Maybe synchronize this with setTransferHandler() ?
133         if (transferHandler != null && cds.delStarted)
134             transferHandler.transferData(this);
135     }
136
137     public void setTransferHandler(BufferTransferHandler transferHandler) {
138         this.transferHandler = transferHandler;
139     }
140
141     public boolean setEnabled(boolean value) {
142         enabled = value;
143         if (value == false) {
144             if (!cds.delStarted) {
145                 try {
146                     cds.stopDelegate();
147                 } catch (IOException ioe) {
148                 }
149             }
150         } else {
151             // Start the capture datasource if the monitor is enabled
152             try {
153                 cds.startDelegate();
154             }catch (IOException ioe) {
155             }
156         }
157         return enabled;
158     }
159
160     public boolean isEnabled()
161     {
162         return enabled;
163     }
164
165
166
167     public float setPreviewFrameRate(float rate)
168     {
169         System.err.println("TODO");
170         return rate;
171     }
172         
173     public ContentDescriptor getContentDescriptor()
174     {
175         return actual.getContentDescriptor();
176     }
177
178     public long getContentLength()
179     {
180         return actual.getContentLength();
181     }
182
183     public boolean endOfStream() {
184         return actual.endOfStream();
185     }
186
187     public Object [] getControls() {
188         return new Object[0];
189     }
190
191     public Object getControl(String str) {
192         return null;
193     }
194
195
196 }