KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inzyme > jmds > DSDataSource


1 package com.inzyme.jmds;
2
3 import java.awt.Component JavaDoc;
4 import java.io.IOException JavaDoc;
5
6 import javax.media.Format;
7 import javax.media.Owned;
8 import javax.media.Time;
9 import javax.media.control.FormatControl;
10 import javax.media.protocol.PushBufferDataSource;
11 import javax.media.protocol.PushBufferStream;
12
13 /**
14  * Provides a DataSource onto a particular pin of a capture device.
15  *
16  * @author Mike Schrag
17  */

18 public class DSDataSource extends PushBufferDataSource {
19     private DSSourceStream myStream;
20     private DSCapturePin myCapturePin;
21
22     private boolean myCrossBarLoaded;
23     private DSCrossBar myCrossBar;
24
25     private FormatControl myFormatControl;
26     
27     /**
28      * Constructs a new DSDataSource.
29      *
30      * @param _capturePin the capture pin to attach to
31      */

32     public DSDataSource(DSCapturePin _capturePin) {
33         myCapturePin = _capturePin;
34         myStream = new DSSourceStream(_capturePin);
35         myFormatControl = new DSDataSourceFormatControl();
36     }
37     
38     /**
39      * Returns a list of available streams for this data source.
40      *
41      * @return a list of available streams for this data source
42      */

43     public PushBufferStream[] getStreams() {
44         return new PushBufferStream[] { myStream };
45     }
46     
47     /**
48      * Returns the content type of the underlying stream.
49      *
50      * @reutrn the content type of the underlying stream
51      */

52     public String JavaDoc getContentType() {
53         return myStream.getContentDescriptor().getContentType();
54     }
55     
56     /**
57      * Connects to this DataSource.
58      */

59     public void connect() throws IOException JavaDoc {
60         myStream.connect();
61     }
62
63     /**
64      * Disconnects from this DataSource.
65      */

66     public void disconnect() {
67         myStream.disconnect();
68     }
69     
70     /**
71      * Starts the capture device stream for this pin.
72      */

73     public void start() throws IOException JavaDoc {
74         myStream.start();
75     }
76
77     /**
78      * Stops the capture device stream for this pin.
79      */

80     public void stop() throws IOException JavaDoc {
81         myStream.stop();
82     }
83     
84     /**
85      * Returns the set of available controls for this
86      * DataSource. Currently the only available control
87      * is the FormatControl.
88      *
89      * @return the set of available controls
90      */

91     public Object JavaDoc[] getControls() {
92         return new Object JavaDoc[] { myFormatControl };
93     }
94
95     /**
96      * Returns the control for the particular class name. Currently
97      * only FormatControl is supported.
98      *
99      * @param _controlClass the class name of the control to retrieve
100      * @return the control for the particular class name (or null if not available)
101      */

102     public Object JavaDoc getControl(String JavaDoc _controlClass) {
103         Object JavaDoc control = null;
104         if (FormatControl.class.getName().equals(_controlClass)) {
105             control = myFormatControl;
106         }
107         return control;
108     }
109     
110     /**
111      * Returns the duration of this stream (which is TIME_UNKNOWN)
112      */

113     public Time getDuration() {
114         return Time.TIME_UNKNOWN;
115     }
116     
117     /**
118      * Returns whether or not this DataSource has a crossbar. This can
119      * only be called after connecting to the DataSource.
120      *
121      * @return whether or not this DataSource has a crossbar
122      */

123     public boolean hasCrossBar() {
124         ensureCrossBarLoaded();
125         return myCrossBar != null;
126     }
127
128     /**
129      * Returns the crossbar for this device (or null if there isn't one).
130      * This can only be called after connecting to the DataSource.
131      *
132      * @return the crossbar for this device (or null if there isn't one)
133      */

134     public DSCrossBar getCrossBar() {
135         ensureCrossBarLoaded();
136         return myCrossBar;
137     }
138
139     private void ensureCrossBarLoaded() {
140         if (!myCrossBarLoaded) {
141             myCrossBar = getCrossBar0();
142             myCrossBarLoaded = true;
143         }
144     }
145
146     private native DSCrossBar getCrossBar0();
147     
148     public class DSDataSourceFormatControl implements FormatControl, Owned {
149         public Component JavaDoc getControlComponent() {
150             return null;
151         }
152
153         public Format[] getSupportedFormats() {
154             return myCapturePin.getFormats();
155         }
156
157         public boolean isEnabled() {
158             return true;
159         }
160
161         public void setEnabled(boolean _enabled) {
162         }
163
164         public Format getFormat() {
165             return myStream.getFormat();
166         }
167         
168         public Object JavaDoc getOwner() {
169             return DSDataSource.this;
170         }
171         
172         /**
173          * Currently you can't request a format that is not explicitly
174          * supported by the device.
175          */

176         public Format setFormat(Format _format) {
177             int matchingIndex = -1;
178             Format[] formats = getSupportedFormats();
179             for (int i = 0; matchingIndex == -1 && i < formats.length; i ++) {
180                 if (formats[i].matches(_format)) {
181                     matchingIndex = i;
182                 }
183             }
184             if (matchingIndex == -1) {
185                 return myStream.getFormat();
186             }
187             else {
188                 return myStream.setFormat(matchingIndex);
189             }
190         }
191     }
192 }
193
Popular Tags