我正在从事一个项目,该项目需要实现从服务器上的网络摄像头到远程客户端上的小程序的实时视频流。
我有一个代码,用于通过服务器上的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();
}
}
}
经过一番研究,我发现服务器中的媒体定位器采用目标IP而不是源IP。
这是一件烦人的事情,因为您必须知道在哪里流式传输。
所以,在这次编辑之后..
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.6:20000/video");
DataSink dataSink = Manager.createDataSink(mediaProcessor.getDataOutput(), outputMediaLocator);
我只能通过内联网将实时视频捕获从 192.168.1.4 流式传输到 192.168.1.6。
但是直到现在我都找不到如何通过互联网流式传输,因为我必须分配远程PC IP。