public void prepare() throws IllegalStateException, IOException { if (mode == MODE_STREAMING) { createSockets(); // We write the ouput of the camera in a local socket instead of a file ! // This one little trick makes streaming feasible quiet simply: data from the camera // can then be manipulated at the other end of the socket setOutputFile(sender.getFileDescriptor()); } super.prepare(); }
/** * Constructs instance from connected socket. * * @param socket non-null; connected socket * @throws IOException */ ZygoteConnection(LocalSocket socket) throws IOException { mSocket = socket; mSocketOutStream = new DataOutputStream(socket.getOutputStream()); mSocketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()), 256); mSocket.setSoTimeout(CONNECTION_TIMEOUT_MILLIS); try { peer = mSocket.getPeerCredentials(); } catch (IOException ex) { Log.e(TAG, "Cannot read peer credentials", ex); throw ex; } peerSecurityContext = SELinux.getPeerContext(mSocket.getFileDescriptor()); }
private boolean prepareVideoRecorder() { mMediaRecorder = new MediaRecorder(); // Step 1: Unlock and set camera to MediaRecorder camera.unlock(); mMediaRecorder.setCamera(camera); // Step 2: Set sources mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); // Step 3: Set a CamcorderProfile (requires API Level 8 or higher) // mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH)); // Step 3: Set output format and encoding (for versions prior to API Level 8) mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); // Step 4: Set output file mMediaRecorder.setOutputFile(ls.getFileDescriptor()); // Step 5: Set the preview output // mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface()); mMediaRecorder.setPreviewDisplay(holder.getSurface()); Log.i(TAG, "Surface valid: " + holder.getSurface().isValid()); /*mMediaRecorder.setVideoSize(176, 144); mMediaRecorder.setVideoFrameRate(20); mMediaRecorder.setAudioChannels(1); */ // Step 6: Prepare configured MediaRecorder try { mMediaRecorder.prepare(); } catch (Exception e) { Log.d(TAG, "Exception preparing MediaRecorder: " + e.getMessage()); releaseMediaRecorder(); return false; } return true; }
/** * Handles post-fork setup of child proc, closing sockets as appropriate, reopen stdio as * appropriate, and ultimately throwing MethodAndArgsCaller if successful or returning if failed. * * @param parsedArgs non-null; zygote args * @param descriptors null-ok; new file descriptors for stdio if available. * @param pipeFd null-ok; pipe for communication back to Zygote. * @param newStderr null-ok; stream to use for stderr until stdio is reopened. * @throws ZygoteInit.MethodAndArgsCaller on success to trampoline to code that invokes static * main. */ private void handleChildProc( Arguments parsedArgs, FileDescriptor[] descriptors, FileDescriptor pipeFd, PrintStream newStderr) throws ZygoteInit.MethodAndArgsCaller { /* * Close the socket, unless we're in "peer wait" mode, in which * case it's used to track the liveness of this process. */ if (parsedArgs.peerWait) { try { ZygoteInit.setCloseOnExec(mSocket.getFileDescriptor(), true); sPeerWaitSocket = mSocket; } catch (IOException ex) { Log.e(TAG, "Zygote Child: error setting peer wait " + "socket to be close-on-exec", ex); } } else { closeSocket(); ZygoteInit.closeServerSocket(); } if (descriptors != null) { try { ZygoteInit.reopenStdio(descriptors[0], descriptors[1], descriptors[2]); for (FileDescriptor fd : descriptors) { IoUtils.closeQuietly(fd); } newStderr = System.err; } catch (IOException ex) { Log.e(TAG, "Error reopening stdio", ex); } } if (parsedArgs.niceName != null) { Process.setArgV0(parsedArgs.niceName); } if (parsedArgs.runtimeInit) { if (parsedArgs.invokeWith != null) { WrapperInit.execApplication( parsedArgs.invokeWith, parsedArgs.niceName, parsedArgs.targetSdkVersion, pipeFd, parsedArgs.remainingArgs); } else { RuntimeInit.zygoteInit(parsedArgs.targetSdkVersion, parsedArgs.remainingArgs); } } else { String className; try { className = parsedArgs.remainingArgs[0]; } catch (ArrayIndexOutOfBoundsException ex) { logAndPrintError(newStderr, "Missing required class name argument", null); return; } String[] mainArgs = new String[parsedArgs.remainingArgs.length - 1]; System.arraycopy(parsedArgs.remainingArgs, 1, mainArgs, 0, mainArgs.length); if (parsedArgs.invokeWith != null) { WrapperInit.execStandalone( parsedArgs.invokeWith, parsedArgs.classpath, className, mainArgs); } else { ClassLoader cloader; if (parsedArgs.classpath != null) { cloader = new PathClassLoader(parsedArgs.classpath, ClassLoader.getSystemClassLoader()); } else { cloader = ClassLoader.getSystemClassLoader(); } try { ZygoteInit.invokeStaticMain(cloader, className, mainArgs); } catch (RuntimeException ex) { logAndPrintError(newStderr, "Error starting.", ex); } } } }
/** * Returns the file descriptor of the associated socket. * * @return null-ok; file descriptor */ FileDescriptor getFileDesciptor() { return mSocket.getFileDescriptor(); }
public FileDescriptor getInputFileDescriptor() { return localSender.getFileDescriptor(); }
public FileDescriptor getOutputFileDescriptor() { return localReceiver.getFileDescriptor(); }
public void testLocalConnections() throws IOException { // create client and server socket LocalServerSocket localServerSocket = new LocalServerSocket(mSockAddr); LocalSocket clientSocket = new LocalSocket(); // establish connection between client and server LocalSocketAddress locSockAddr = new LocalSocketAddress(mSockAddr); assertFalse(clientSocket.isConnected()); clientSocket.connect(locSockAddr); assertTrue(clientSocket.isConnected()); LocalSocket serverSocket = localServerSocket.accept(); Credentials credent = clientSocket.getPeerCredentials(); assertTrue(0 != credent.getPid()); // send data from client to server OutputStream clientOutStream = clientSocket.getOutputStream(); clientOutStream.write(12); InputStream serverInStream = serverSocket.getInputStream(); assertEquals(12, serverInStream.read()); // send data from server to client OutputStream serverOutStream = serverSocket.getOutputStream(); serverOutStream.write(3); InputStream clientInStream = clientSocket.getInputStream(); assertEquals(3, clientInStream.read()); // Test sending and receiving file descriptors clientSocket.setFileDescriptorsForSend(new FileDescriptor[] {FileDescriptor.in}); clientOutStream.write(32); assertEquals(32, serverInStream.read()); FileDescriptor[] out = serverSocket.getAncillaryFileDescriptors(); assertEquals(1, out.length); FileDescriptor fd = clientSocket.getFileDescriptor(); assertTrue(fd.valid()); // shutdown input stream of client clientSocket.shutdownInput(); assertEquals(-1, clientInStream.read()); // shutdown output stream of client clientSocket.shutdownOutput(); try { clientOutStream.write(10); fail("testLocalSocket shouldn't come to here"); } catch (IOException e) { // expected } // shutdown input stream of server serverSocket.shutdownInput(); assertEquals(-1, serverInStream.read()); // shutdown output stream of server serverSocket.shutdownOutput(); try { serverOutStream.write(10); fail("testLocalSocket shouldn't come to here"); } catch (IOException e) { // expected } // close client socket clientSocket.close(); try { clientInStream.read(); fail("testLocalSocket shouldn't come to here"); } catch (IOException e) { // expected } // close server socket serverSocket.close(); try { serverInStream.read(); fail("testLocalSocket shouldn't come to here"); } catch (IOException e) { // expected } }