package forumposter; import java.io.IOException; import java.io.*; import java.util.Timer; import java.util.TimerTask; import javax.microedition.io.Connector; import javax.microedition.io.HttpConnection; import javax.microedition.io.SocketConnection; import javax.microedition.lcdui.*; import javax.microedition.media.*; import javax.microedition.media.control.*; import javax.microedition.midlet.MIDlet; public class VStreamerMIDlet extends MIDlet implements CommandListener { private Display mDisplay; private Form mMainForm; private Command mExitCommand, mVideoCommand; private Command mVideoStopCommand, mVideoCaptureCommand; private Command mVideoPlayCommand, mVideoSendCommand; private Command mAddText; private Player mPlayer; private VideoControl mVideoControl; private ByteArrayOutputStream outStream = new ByteArrayOutputStream(); private ByteArrayInputStream inStream; private RecordControl rc; private ByteArrayOutputStream frameOutStream; private ByteArrayInputStream frameInStream; private boolean videoCaptured = false; private boolean captureSetup = false; boolean captureRunning = false; boolean videoPlaying = false; private TextField titleTextfield; private TextField descriptionTextfield; private String title = ""; private String description = ""; private boolean addingText = false; public VStreamerMIDlet() { mExitCommand = new Command("Exit", Command.EXIT, 0); mVideoCommand = new Command("Show Video", Command.SCREEN, 0); mVideoCaptureCommand = new Command("Start Capture", Command.SCREEN, 0); mVideoPlayCommand = new Command("Play Video", Command.SCREEN, 0); mVideoStopCommand = new Command("Stop", Command.SCREEN, 0); mVideoSendCommand = new Command("Send Video", Command.SCREEN, 0); mAddText = new Command("Add Text", Command.SCREEN, 0); mMainForm = new Form("Video Streamer"); mMainForm.addCommand(mExitCommand); String video_support = System.getProperty("video.encodings"); if (video_support != null && video_support.length() > 0) { mMainForm.append("Ready to shoot video."); //mMainForm.append(video_support); //mMainForm.append("video/3gpp"); mMainForm.addCommand(mVideoCommand); mMainForm.addCommand(mVideoCaptureCommand); mMainForm.addCommand(mVideoPlayCommand); mMainForm.addCommand(mVideoStopCommand); mMainForm.addCommand(mVideoSendCommand); mMainForm.addCommand(mAddText); } else { mMainForm.append("Cannot use this device to shoot video."); } mMainForm.setCommandListener(this); } public void startApp() { mDisplay = Display.getDisplay(this); mDisplay.setCurrent(mMainForm); } public void pauseApp() {} public void destroyApp(boolean unconditional) {} public void commandAction(Command c, Displayable s) { if (c.getCommandType() == Command.EXIT) { destroyApp(true); notifyDestroyed(); } else if (c == mVideoCommand) { showVideoCamera(); } else if (c == mVideoCaptureCommand) { videoCapture(); } else if (c == mVideoStopCommand) { if (captureRunning) { stopCapture(); } else if (videoPlaying) { stopVideo(); } } else if (c == mVideoPlayCommand) { playVideo(); } else if (c == mVideoSendCommand) { sendCapturedVideo(); } else if (c == mAddText) { addText(); } } private void addText() { if (!addingText) { Form mTextForm = new Form("Adding Text"); titleTextfield = new TextField("Title", title, 128, TextField.ANY); descriptionTextfield = new TextField("Description", description, 256, TextField.ANY); mTextForm.append(titleTextfield); mTextForm.append(descriptionTextfield); mTextForm.addCommand(mAddText); mTextForm.setCommandListener(this); mDisplay.setCurrent(mTextForm); addingText = true; } else { title = titleTextfield.getString(); description = descriptionTextfield.getString(); mDisplay.setCurrent(mMainForm); addingText = false; } } private void showVideoCamera() { try { //mPlayer = Manager.createPlayer("capture://video?width=80&height=60&fps=5"); //mPlayer = Manager.createPlayer("capture://video?fps=5"); // 128 x 160 mPlayer = Manager.createPlayer("capture://video"); mPlayer.realize(); mVideoControl = (VideoControl) mPlayer.getControl("VideoControl"); Canvas canvas = new CameraCanvas(this, mVideoControl, CameraCanvas.VIDEO_CAPTURE); canvas.addCommand(mVideoCaptureCommand); canvas.addCommand(mVideoStopCommand); canvas.setCommandListener(this); mDisplay.setCurrent(canvas); mPlayer.start(); } catch (IOException ioe) { handleException(ioe); } catch (MediaException me) { handleException(me); } } public void setupCapture() { rc = (RecordControl) mPlayer.getControl("RecordControl"); rc.setRecordStream(outStream); captureSetup = true; } public void videoCapture() { if (!captureRunning) { if (!captureSetup) { setupCapture(); } captureRunning = true; try { rc.startRecord(); mPlayer.start(); } catch (MediaException me) { handleException(me); } } } public void stopCapture() { if (captureRunning) { try { rc.stopRecord(); rc.commit(); mPlayer.stop(); mPlayer.close(); inStream = new ByteArrayInputStream(outStream.toByteArray()); videoCaptured = true; mDisplay.setCurrent(mMainForm); captureRunning = false; } catch (MediaException me) { handleException(me); } catch (IOException e) { handleException(e); } } } public void stopVideo() { try { mPlayer.stop(); mPlayer.close(); mDisplay.setCurrent(mMainForm); videoPlaying = false; } catch (MediaException me) { handleException(me); } } public void playVideo() { if (videoCaptured) { try { mPlayer = Manager.createPlayer(inStream,"video/3gpp"); mPlayer.realize(); // get video control mVideoControl = (VideoControl) mPlayer.getControl("VideoControl"); Canvas canvas = new CameraCanvas(this, mVideoControl, CameraCanvas.VIDEO_PLAYBACK); canvas.addCommand(mVideoStopCommand); canvas.setCommandListener(this); mDisplay.setCurrent(canvas); mPlayer.start(); } catch (MediaException me) { handleException(me); } catch (IOException e) { handleException(e); } } } private void handleException(Exception e) { Alert a = new Alert("Exception", e.toString(), null, null); a.setTimeout(Alert.FOREVER); mDisplay.setCurrent(a, mMainForm); } private void showAlert(String sToPrint) { Alert a = new Alert("Exception", sToPrint, null, null); a.setTimeout(Alert.FOREVER); mDisplay.setCurrent(a, mMainForm); }