📄 transmitter.java
字号:
/****************************************************************************************/
/* 2001-Spring: Java Network-Programming Term-Project */
/* Title: Streaming media generation, capture and store. */
/* Team Member: Yumin Yuan(yuany@rpi.edu), Rui Mu(mur@rpi.edu), Yining Hu(huyn@rpi.edu) */
/* Transmitter.java: RTP session sets up and media transmission */
/* Complile: javac Transmitter.java */
/****************************************************************************************/
import java.awt.Dimension;
import javax.media.*;
import javax.media.protocol.*;
import javax.media.format.*;
import javax.media.control.TrackControl;
import java.io.*;
public abstract class Transmitter {
protected final int TTL = 8;
protected Processor processor = null;
protected DataSink rtptransmitter = null;
protected DataSource dataOutput = null;
protected DataSource dataInput=null;
protected String rtpURL;
public Transmitter(String _destURL,DataSource _dataInput) {
rtpURL=_destURL;
dataInput=_dataInput;
}
/**
* Starts the transmission. Returns null if transmission started ok.
* Otherwise it returns a string with the reason why the setup failed.
*/
public synchronized String start() {
String result;
result = createProcessor();
if (result != null)
return result;
System.out.println("create processor finish");
// Create an RTP session to transmit the output of the
// processor to the specified IP address and port no.
result = createTransmitter();
if (result != null) {
processor.close();
processor = null;
return result;
}
// Start the transmission
processor.start();
return null;
}
/**
* Stops the transmission if already started
*/
public void stop() {
synchronized (this) {
if (processor != null) {
processor.stop();
processor.close();
processor = null;
rtptransmitter.close();
rtptransmitter = null;
}
}
}
abstract String createProcessor();
// Creates an RTP transmit data sink. This is the easiest way to create
// an RTP transmitter. The other way is to use the RTPSessionManager API.
// Using an RTP session manager gives you more control if you wish to
// fine tune your transmission and set other parameters.
protected String createTransmitter() {
MediaLocator outputLocator = new MediaLocator(rtpURL);
try {
rtptransmitter = Manager.createDataSink(dataOutput, outputLocator);
rtptransmitter.open();
rtptransmitter.start();
dataOutput.start();
} catch (MediaException me) {
return "Couldn't create RTP data sink";
} catch (IOException ioe) {
return "Couldn't create RTP data sink";
}
return null;
}
/****************************************************************
* Convenience methods to handle processor's state changes.
****************************************************************/
protected Integer stateLock = new Integer(0);
protected boolean failed = false;
Integer getStateLock() {
return stateLock;
}
void setFailed() {
failed = true;
}
protected synchronized boolean waitForState(Processor p, int state) {
p.addControllerListener(new StateListener());
failed = false;
// Call the required method on the processor
if (state == Processor.Configured) {
p.configure();
} else if (state == Processor.Realized) {
p.realize();
}
// Wait until we get an event that confirms the
// success of the method, or a failure event.
// See StateListener inner class
while (p.getState() < state && !failed) {
synchronized (getStateLock()) {
try {
getStateLock().wait();
} catch (InterruptedException ie) {
return false;
}
}
}
if (failed)
return false;
else
return true;
}
/****************************************************************
* Inner Classes
****************************************************************/
class StateListener implements ControllerListener {
public void controllerUpdate(ControllerEvent ce) {
// If there was an error during configure or
// realize, the processor will be closed
if (ce instanceof ControllerClosedEvent)
setFailed();
// All controller events, send a notification
// to the waiting thread in waitForState method.
if (ce instanceof ControllerEvent) {
synchronized (getStateLock()) {
getStateLock().notifyAll();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -