⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cammanagerimpl.java

📁 Its video trNSMITT FILE USING JMF
💻 JAVA
字号:
/*  CamManagerImpl.java
 * 
 *  Object is created by a CamProcessor and used by a CamController to control 
 *  a web cam remotely.
 *
 *  Copyright (C) 2002 by Frank McCown
 *  University of Arkansas at Little Rock
 *  July 2002
 */

import java.io.*;
import java.util.*;
import rowc.*;

public class CamManagerImpl extends CamManagerPOA 
{
	protected Hashtable callbacks;
	protected CamVideoCallback callback;
	private String camLocation;  // location of cam manager
	private RTPExport export;	
	private String serverHostName = null;
	
	private final String TEMP_VIDEO_FILENAME = "temp.mov";
	
	private boolean transmittingVideo = false;
	private boolean recordingVideo = false;
	
	
	public CamManagerImpl(String location)
	{
		camLocation = location;
		
		export = new RTPExport();
		callbacks = new Hashtable();
	}

	// Need server's host name to connect with socket
	public void setServerName(String hostname)
	{
		serverHostName = hostname;
	}
	
	
	/******************************************************************************
	 *
	 *  Remote method implementations
	 *
	 *****************************************************************************/
	
	// Start transmitting live video stream to CamController at given ipAddr.
	// The callback is used for notifiying CamController when the transmission
	// has begun.  Return false if already bust transmitting, true otherwise. 
	public synchronized boolean startTransmission(String hostName, CamVideoCallback callback)
	{
		System.out.print("startTransmission ");
		
		if (transmittingVideo)
			return false;
			
		transmittingVideo = true;
		
		// The callback gets called when video is finished
		this.callback = callback;
		
		// Start sending video to ipAddr and notify receiver of incoming RTP transmission
		String url = "rtp://" + hostName + ":5050/video";
	
		System.out.println("sending to "+url);
			
		CamProcessor.transmitVideo(hostName);
		
		callback.videoAvailable(url);
		
		return true;
	}
	
	
	// Stop transmitting live video stream to CamController.
	public void stopTransmission()
	{
		System.out.println("stopTransmission called");

		transmittingVideo = false;
		
		CamProcessor.stopTransmitVideo();
	}
	
	// Start recording to file.  Return false if already busy recording to file, 
	// true otherwise.
	public boolean startRec(int maxRecTime, CamVideoCallback callback)
	{
		System.out.println("startRec called");
		
		if (recordingVideo)
			return false;
			
		recordingVideo = true;
		
		// The callback gets called when video is finished
		this.callback = callback;
		
		// Start in new thread so we can return from this function immediately.  This allows
		// stopRec to be called.
		ExportThread et = new ExportThread(maxRecTime);
		et.start();
						
		System.out.println("startRec finished.");
		
		return true;
	}
	
	// Send video file from CamProcessor to MediaServer using sockets.
	private void transmitFile()
	{
		BufferedOutputStream fos = null;
    java.net.Socket socket = null;
  
    try 
    { 
    	System.out.println("Writing file to server socket: " + serverHostName + ":" +
    		FileServer.FILE_SERVER_PORT);
            
      // open a socket connection.  Need server host name and socket
      socket = new java.net.Socket(serverHostName, FileServer.FILE_SERVER_PORT);
      
      // open I/O streams for objects
      fos = new BufferedOutputStream(socket.getOutputStream());
      
      File transferFile = new File(TEMP_VIDEO_FILENAME);
      BufferedInputStream fin = new BufferedInputStream(new FileInputStream(transferFile));
			//byte[] buffer = new byte[4092]; // 4K
			
			// First write out location so Server can name file.
			String fileName = camLocation + "\n";
			byte byteArray[] = new byte[fileName.length()];
			byteArray = fileName.getBytes();
				
			fos.write(byteArray);
				
			int c = fin.read();
      while (c != -1) 
      {  
       	fos.write(c);
       	c = fin.read();
      }
      
      fin.close();
      fos.close();
    } 
    catch(Exception e) 
    {
    	System.out.println("Error transmitting file to Server.");
     	e.printStackTrace();
    }
	}
	
	// Stop recording video stream.
	public void stopRec()
	{
		System.out.println("stopRec called");
		
		export.stopExport();		
		
		recordingVideo = false;
	}
	
  
  // Thread to export video to file, transmit to MediaServer, and notify CamController
	class ExportThread extends Thread
	{
		private int maxRecTime;
		
		ExportThread(int maxRecTime)
		{
			this.maxRecTime = maxRecTime;
		}
		
		public void	run()
		{
			// Generate output media locators
			
			String currentDir = System.getProperty("user.dir");
			String fileName = currentDir + "/" + TEMP_VIDEO_FILENAME;
		
			javax.media.MediaLocator oml = new 
				javax.media.MediaLocator("file:/" + fileName);
		
			System.out.println("Starting RTPExport...");

			CamProcessor.lblInfo.setText("Recording to file... ");

			// Must create clone of data source so video continues to play in CamProcessor.
			if (!export.startExport(
				((javax.media.protocol.SourceCloneable)CamProcessor.dataSource).createClone(),
				//CamProcessor.dataSource,
				oml, maxRecTime)) 
			{
		    System.err.println("RTPExporting failed");
		    VideoClip video = new VideoClip("","",0,0,0);
		    callback.newRecording(video);
			}
			else
			{
				// Notify client of new media
								
				VideoClip video = new VideoClip();
				video.location = camLocation;
				video.timeStamp = System.currentTimeMillis();
				
				// Name is taken from timestamp.  Replace spaces and : with underscores
				video.name = new java.util.Date(video.timeStamp).toString();
				video.name = video.name.replace(' ', '_');
  			video.name = video.name.replace(':', '_');

				File f = new File(TEMP_VIDEO_FILENAME);
				video.size = (int)f.length();
				
				video.length = export.actualRecTime; //maxRecTime;
				
				// Now that video is produced, send it to Server for storing using sockets 
			
				CamProcessor.lblInfo.setText("Transmitting to server... ");	
			  transmitFile();
			  
			  CamProcessor.lblInfo.setText("");
			  	
			  // Notify CamController of new video
				callback.newRecording(video);
				
				recordingVideo = false;
			}
		}
	}  // end ExportThread
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -