001: /*
002: * $RCSfile: MediaContainerRetained.java,v $
003: *
004: * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
006: *
007: * This code is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License version 2 only, as
009: * published by the Free Software Foundation. Sun designates this
010: * particular file as subject to the "Classpath" exception as provided
011: * by Sun in the LICENSE file that accompanied this code.
012: *
013: * This code is distributed in the hope that it will be useful, but WITHOUT
014: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: * version 2 for more details (a copy is included in the LICENSE file that
017: * accompanied this code).
018: *
019: * You should have received a copy of the GNU General Public License version
020: * 2 along with this work; if not, write to the Free Software Foundation,
021: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
022: *
023: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
024: * CA 95054 USA or visit www.sun.com if you need additional information or
025: * have any questions.
026: *
027: * $Revision: 1.7 $
028: * $Date: 2008/02/28 20:17:26 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import java.net.URL;
035: import java.io.InputStream;
036:
037: /**
038: * The MediaContainerRetained object defines all rendering state that can
039: * be set as a component object of a retained Soundscape node.
040: */
041: class MediaContainerRetained extends NodeComponentRetained {
042: /**
043: * Gain Scale Factor applied to source with this attribute
044: */
045: boolean cached = true;
046:
047: /**
048: * URL string that references the sound data
049: */
050: URL url = null;
051: String urlString = null;
052: InputStream inputStream = null;
053:
054: /**
055: * Set Cached flag
056: * @param state flag denoting sound data is cached by app within node
057: */
058: void setCacheEnable(boolean state) {
059: this .cached = state;
060: // changing this AFTER sound data attached to node is ignored
061: // notifyUsers();
062: }
063:
064: /**
065: * Retrieve Attrribute Gain (amplitude)
066: * @return gain amplitude scale factor
067: */
068: boolean getCacheEnable() {
069: return this .cached;
070: }
071:
072: /**
073: * Set URL object that references the sound data
074: * @param url URL object that references the sound data
075: */
076: void setURLObject(URL url) {
077: setURLObject(url, true);
078: }
079:
080: /**
081: * Set URL object that references the sound data
082: * @param url URL object that references the sound data
083: * @param forceLoad ensures that message about change is sent to scheduler
084: */
085: void setURLObject(URL url, boolean forceLoad) {
086: // can NOT set URL object field unless the other related fields are null
087: if (url != null) {
088: if (urlString != null || inputStream != null)
089: throw new IllegalArgumentException(J3dI18N
090: .getString("MediaContainer5"));
091: // Test if url object is valid by openning it
092: try {
093: InputStream stream;
094: stream = url.openStream();
095: stream.close();
096: } catch (Exception e) {
097: throw new SoundException(javax.media.j3d.J3dI18N
098: .getString("MediaContainer0"));
099: }
100: }
101: this .url = url;
102: // notifyUsers();
103: // avoid re-loading SAME MediaContainer when duplicateAttrib calls
104: if (forceLoad)
105: dispatchMessage();
106: }
107:
108: /**
109: * Set URL path that references the sound data
110: * @param path string of URL that references the sound data
111: */
112: void setURLString(String path) {
113: setURLString(path, true);
114: }
115:
116: /**
117: * Set URL path that references the sound data
118: * @param path string of URL that references the sound data
119: * @param forceLoad ensures that message about change is sent to scheduler
120: */
121: void setURLString(String path, boolean forceLoad) {
122: // can NOT set string field unless the other related fields are null
123: if (path != null) {
124: if (this .url != null || inputStream != null)
125: throw new IllegalArgumentException(J3dI18N
126: .getString("MediaContainer5"));
127: // Test if path string is valid URL by trying to generate a URL
128: // and then openning it
129: try {
130: URL url = new URL(path);
131: InputStream stream;
132: stream = url.openStream();
133: stream.close();
134: } catch (Exception e) {
135: throw new SoundException(javax.media.j3d.J3dI18N
136: .getString("MediaContainer0"));
137: }
138: }
139: this .urlString = path;
140: // notifyUsers();
141: // avoid re-loading SAME MediaContainer when duplicateAttrib calls
142: if (forceLoad)
143: dispatchMessage();
144: }
145:
146: /**
147: * Set input stream reference to sound data
148: * @param stream InputStream that references the sound data
149: * @param forceLoad ensures that message about change is sent to scheduler
150: */
151: void setInputStream(InputStream stream) {
152: setInputStream(stream, true);
153: }
154:
155: /**
156: * Set input stream reference to sound data
157: * @param stream InputStream that references the sound data
158: */
159: void setInputStream(InputStream stream, boolean forceLoad) {
160: // XXXX: AudioDevice not intellegent enough to process InputStreams yet
161: // can NOT set stream field unless the other related fields are null
162: if (stream != null) {
163: if (url != null || urlString != null)
164: throw new IllegalArgumentException(J3dI18N
165: .getString("MediaContainer5"));
166: }
167: this .inputStream = stream;
168: // notifyUsers();
169: // avoid re-loading SAME MediaContainer when duplicateAttrib calls
170: if (forceLoad)
171: dispatchMessage();
172: }
173:
174: /**
175: * Retrieve URL String
176: * @return URL string that references the sound data
177: */
178: String getURLString() {
179: return this .urlString;
180: }
181:
182: /**
183: * Retrieve URL objects
184: * @return URL object that references the sound data
185: */
186: URL getURLObject() {
187: return this .url;
188: }
189:
190: /**
191: * Retrieve InputData
192: * @return InputString that references the sound data
193: */
194: InputStream getInputStream() {
195: return this .inputStream;
196: }
197:
198: /**
199: * Dispatch a message about a media container change
200: */
201: void dispatchMessage() {
202: // Send message including a integer argumentD
203: J3dMessage createMessage = new J3dMessage();
204: createMessage.threads = J3dThread.SOUND_SCHEDULER;
205: createMessage.type = J3dMessage.MEDIA_CONTAINER_CHANGED;
206: createMessage.universe = null;
207: createMessage.args[0] = this ;
208: createMessage.args[1] = new Integer(
209: SoundRetained.SOUND_DATA_DIRTY_BIT);
210: createMessage.args[2] = new Integer(users.size());
211: createMessage.args[3] = users;
212: VirtualUniverse.mc.processMessage(createMessage);
213: }
214: }
|