001: /*
002: *
003: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025: package com.sun.mmedia;
026:
027: import com.sun.j2me.app.AppIsolate;
028:
029: /**
030: * Description of the Class
031: *
032: */
033: public class AudioTunnel {
034:
035: private TunnelThread tunnelThread = null;
036: private int num_open_players = 0;
037: private boolean pcmdev_acquired = false;
038: private static AudioTunnel tunnel = null;
039:
040: native static int nInit(int isolateId);
041:
042: native static int nPlayBack(int isolateId);
043:
044: native static int nStop(int isolateId);
045:
046: native static int nStartMixer(int isolateId);
047:
048: native static int nStopMixer(int isolateId);
049:
050: private AudioTunnel() {
051: if (nInit(AppIsolate.getIsolateId()) == 0) {
052: tunnelThread = new TunnelThread();
053: nStartMixer(AppIsolate.getIsolateId());
054: pcmdev_acquired = true;
055: }
056: }
057:
058: synchronized static AudioTunnel getInstance() {
059: if (tunnel == null) {
060: tunnel = new AudioTunnel();
061: } else if (!tunnel.pcmdev_acquired) {
062: try {
063: tunnel.tunnelThread.join();
064: } catch (InterruptedException e) {
065: }
066: tunnel = null;
067: tunnel = new AudioTunnel();
068: }
069: return tunnel;
070: }
071:
072: void start() {
073: if (tunnelThread != null) {
074: synchronized (tunnelThread) {
075: num_open_players++;
076: tunnelThread.notify();
077: }
078: }
079: }
080:
081: void stop() {
082: if (tunnelThread != null) {
083: synchronized (tunnelThread) {
084: if (num_open_players > 0) {
085: num_open_players--;
086: }
087: }
088: }
089: }
090:
091: protected void finalize() {
092: num_open_players = 0;
093: }
094:
095: private class TunnelThread extends Thread {
096: private boolean terminate = false;
097:
098: public TunnelThread() {
099: setPriority(Thread.MAX_PRIORITY);
100: start();
101: }
102:
103: public void run() {
104: int s;
105: long stopTime = 0;
106: while (!terminate) {
107: while (pcmdev_acquired) {
108: s = nPlayBack(AppIsolate.getIsolateId());
109: if (s > 0) {
110: try {
111: Thread.sleep(s);
112: } catch (Exception e) {
113: }
114: stopTime = 0;
115: } else {
116: synchronized (this ) {
117: if (num_open_players == 0
118: && pcmdev_acquired == true) {
119: if (stopTime == 0) {
120: stopTime = System
121: .currentTimeMillis() + 5000; /* 5 sec */
122: } else if (stopTime < System
123: .currentTimeMillis()) {
124: nStopMixer(AppIsolate
125: .getIsolateId());
126: pcmdev_acquired = false;
127: stopTime = 0;
128: }
129: }
130: }
131: }
132: }
133: if (num_open_players == 0) {
134: terminate = true;
135: nStop(AppIsolate.getIsolateId());
136: } else {
137: synchronized (this ) {
138: try {
139: wait(1000);
140: } catch (Exception ex) {
141: }
142: ;
143: }
144: }
145: }
146: ;
147: }
148:
149: protected void finalize() {
150: num_open_players = 0;
151: }
152:
153: }
154:
155: }
|