001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.applications.audioconf;
020:
021: import org.lucane.applications.audioconf.audio.*;
022: import org.lucane.applications.audioconf.gui.*;
023: import org.lucane.client.*;
024: import org.lucane.client.widgets.DialogBox;
025: import org.lucane.common.*;
026: import org.lucane.common.net.ObjectConnection;
027:
028: public class AudioConf extends Plugin {
029: private ConnectInfo friend;
030: private ObjectConnection connection;
031:
032: private AudioRecorder recorder;
033: private AudioPlayer player;
034:
035: private Controller controller;
036:
037: private boolean stopped = false;
038:
039: public AudioConf() {
040: //empty constructor for pluginloader
041: }
042:
043: private AudioConf(ConnectInfo friend) {
044: this .friend = friend;
045: }
046:
047: public Plugin newInstance(ConnectInfo[] friends) {
048: return new AudioConf(friends.length > 0 ? friends[0] : null);
049: }
050:
051: public void load(ObjectConnection oc, ConnectInfo who, String data) {
052: this .connection = oc;
053: this .friend = who;
054: }
055:
056: public void start() {
057: if (friend == null) {
058: DialogBox.info(tr("err.noUserSelected"));
059: exit();
060: return;
061: }
062:
063: ConfigDialog cd = new ConfigDialog(this );
064: cd.show();
065: }
066:
067: public void follow() {
068: Logging.getLogger().info(
069: "receiving audio stream from " + friend.getName());
070:
071: try {
072: AudioConfig config = (AudioConfig) this .connection.read();
073:
074: String msg = tr("msg.acceptFrom");
075: msg = msg.replaceAll("%1", getFriendName());
076:
077: boolean accept = DialogBox.question(getTitle(), msg);
078: this .connection.write(Boolean.valueOf(accept));
079:
080: if (accept)
081: startAll(config);
082: } catch (Exception e) {
083: e.printStackTrace();
084: }
085: }
086:
087: public void waitForAccept(AudioConfig config) {
088: this .connection = Communicator.getInstance().sendMessageTo(
089: this .friend, this .getName(), "");
090: try {
091: this .connection.write(config);
092: Boolean accepted = (Boolean) this .connection.read();
093: if (accepted.booleanValue())
094: startAll(config);
095: else {
096: String msg = tr("msg.rejectedBy");
097: msg = msg.replaceAll("%1", getFriendName());
098: DialogBox.info(msg);
099: }
100: } catch (Exception e) {
101: DialogBox.info(tr("err.accept"));
102: e.printStackTrace();
103: }
104: }
105:
106: public void startRecorder(AudioConfig config) {
107: recorder = new AudioRecorder(config);
108: recorder.addAudioListener(new Streamer(this , connection));
109: Thread thread = new Thread(recorder);
110: thread.start();
111: }
112:
113: public void startPlayer(AudioConfig config) {
114: player = new AudioPlayer(this , config,
115: new AudioConfInputStream(this , connection));
116: Thread thread = new Thread(player);
117: thread.start();
118: }
119:
120: //-- error handling
121:
122: public void reportRecorderError(Exception e) {
123: stopAndExit();
124: }
125:
126: public void reportPlayerError(Exception e) {
127: stopAndExit();
128: }
129:
130: //-- for controller
131:
132: public void startAll(AudioConfig config) {
133: startPlayer(config);
134: startRecorder(config);
135: controller = new Controller(this );
136: controller.showController();
137: }
138:
139: public String getFriendName() {
140: return friend.getName();
141: }
142:
143: public void stopAndExit() {
144: //avoid infinite loop
145: if (this .stopped)
146: return;
147: this .stopped = true;
148:
149: Logging.getLogger().info("Stopping AudioConf");
150:
151: recorder.stop();
152: player.stop();
153: controller.hideController();
154: exit();
155: }
156: }
|