我正在开发一个项目,该项目需要实现从服务器上的网络摄像头到远程客户端上的小程序的实时视频流。
我有一个代码,用于通过服务器上的 JMF 从网络摄像头捕获实时流,并在 rtp 上部署流和一个从 rtp 地址接收流然后将其播放到其中的小程序。
但问题是它只能在我自己的电脑(服务器)中工作并且不能在本地网络上工作(它加载小程序但不运行视频播放器)。
服务器代码:
public class Streaming_Server {
/**
* @param args the command line arguments
*/
private static String PORT = "10000";
private static InetAddress addr;
static final Format[] FORMATS = { new VideoFormat("rgb") };
static final ContentDescriptor CONTENT_DESCRIPTOR = new ContentDescriptor("raw.rtp");
public static void main(String[] args) throws IOException, NoDataSourceException, NoProcessorException, CannotRealizeException, NoDataSinkException {
// TODO code application logic here
try
{
addr = InetAddress.getLocalHost();
String ipAddr = addr.getHostAddress();
String hostname = addr.getHostName();
System.out.println("Your IP is " + ipAddr);
System.out.println("Your Host Name is " + hostname);
}
catch (UnknownHostException e){}
CaptureDeviceInfo webcamInfooo = new CaptureDeviceInfo("Camera", new MediaLocator("vfw://0"), null);
MediaLocator camDeviceMediaLocator = webcamInfooo.getLocator();
DataSource source = Manager.createDataSource(camDeviceMediaLocator);
Processor mediaProcessor = Manager.createRealizedProcessor(new ProcessorModel(source, FORMATS, CONTENT_DESCRIPTOR));
//MediaLocator outputMediaLocator = new MediaLocator("rtp://" + addr.getHostAddress() + ":" + PORT + "/video");
MediaLocator outputMediaLocator = new MediaLocator("rtp://192.168.1.4:20000/video");
DataSink dataSink = Manager.createDataSink(mediaProcessor.getDataOutput(), outputMediaLocator);
mediaProcessor.start();
dataSink.open();
dataSink.start();
}
}
客户代码:
public class Streaming_Client extends Applet implements ControllerListener {
/**
* Initialization method that will be called after the applet is loaded into
* the browser.
*/
Player player = null;
private MediaLocator mediaLocator;
private static String PORT = "10000";
private static InetAddress addr;
public void init() {
// TODO start asynchronous download of heavy resources
//mediaLocator = new MediaLocator("rtp:/192.168.1.4:10000/video");
mediaLocator = new MediaLocator("rtp:/192.168.1.4:20000/video");
setLayout(new BorderLayout());
try {
player = Manager.createPlayer(mediaLocator);
player.addControllerListener(this);
} catch (IOException | NoPlayerException ex) {
Logger.getLogger(Streaming_Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void start(){
player.start();
}
public void stop(){
player.stop();
player.deallocate();
}
public void destroy(){
player.close();
}
// TODO overwrite start(), stop() and destroy() methods
@Override
public void controllerUpdate(ControllerEvent ce) {
if ((ce instanceof RealizeCompleteEvent))
{
Component comp;
if ((comp = this.player.getVisualComponent()) != null)
add("Center", comp);
if ((comp = this.player.getControlPanelComponent()) != null)
add("South", comp);
validate();
}
}
}