public static int getSize(FileDescriptor fd) throws IOException { return native_get_size(fd); }
public void deactivate() { if (!isDeactivated()) { try { if (Build.VERSION.SDK_INT < 21) { native_munmap_int(mAddressInt, mLength); mAddressInt = 0; } else { native_munmap_long(mAddressLong, mLength); mAddressLong = 0; } } catch (IOException ex) { Log.e(LOG_TAG, "Failed to deactivate, error: " + ex.toString()); } } }
public void writeBytes(byte[] buffer, int srcOffset, int destOffset, int count) throws IOException { if (isDeactivated()) { throw new IOException("Can't write to deactivated memory file."); } if (srcOffset < 0 || srcOffset > buffer.length || count < 0 || count > buffer.length - srcOffset || destOffset < 0 || destOffset > mLength || count > mLength - destOffset) { throw new IndexOutOfBoundsException(); } if(Build.VERSION.SDK_INT < 21) { native_write_int(mFD, mAddressInt, buffer, srcOffset, destOffset, count, false); } else { native_write_long(mFD, mAddressLong, buffer, srcOffset, destOffset, count, false); } }
public int readBytes(byte[] buffer, int srcOffset, int destOffset, int count) throws IOException { if (isDeactivated()) { throw new IOException("Can't read from deactivated memory file."); } if (destOffset < 0 || destOffset > buffer.length || count < 0 || count > buffer.length - destOffset || srcOffset < 0 || srcOffset > mLength || count > mLength - srcOffset) { throw new IndexOutOfBoundsException(); } if(Build.VERSION.SDK_INT < 21) { return native_read_int(mFD, mAddressInt, buffer, srcOffset, destOffset, count, false); } else { return native_read_long(mFD, mAddressLong, buffer, srcOffset, destOffset, count, false); } }
public AshmemFile(ParcelFileDescriptor pfd, int length, int mode) throws IOException, NullPointerException, IllegalArgumentException { FileDescriptor fd = pfd.getFileDescriptor(); if (fd == null) { throw new NullPointerException("File descriptor is null."); } if (!isMemoryFile(fd)) { throw new IllegalArgumentException("Not a memory file."); } mLength = length; mFD = fd; mPFD = pfd; if (Build.VERSION.SDK_INT < 21) { mAddressInt = native_mmap_int(mFD, length, mode); } else { mAddressLong = native_mmap_1ong(mFD, length, mode); } }
public void close() { deactivate(); if (!isClosed()) { //native_close(mFD); try { mPFD.close(); } catch (IOException e) { Log.e(LOG_TAG, "Failed to close file descriptor"); } mFD = null; } }
public boolean fillFrame(byte[] data, int offset, int size, long pts) { try { inputMemoryFile.writeBytes(data, offset, 0, size); return decoder.fillFrame(0, size, pts); } catch (IOException e) { Log.e(LOG_TAG, "Failed to write bytes"); } catch (RemoteException e) { Log.e(LOG_TAG, "Failed to fill frame"); } return false; } }
public boolean config(int width, int height, Surface surface) { try { Bundle params = new Bundle(); params.putInt(IVideoDecoder.KEY_WIDTH, width); params.putInt(IVideoDecoder.KEY_HEIGHT, height); params.putParcelable(IVideoDecoder.KEY_SURFACE, surface); if (!decoder.config(params)) { Log.e(LOG_TAG, "Failed to config decoder"); return false; } ParcelFileDescriptor pfd = decoder.getInputMemoryFile(); inputMemoryFileSize = decoder.getInputMemoryFileSize(); try { inputMemoryFile = new AshmemFile(pfd, inputMemoryFileSize, AshmemFile.PROT_WRITE); } catch (IOException e) { Log.e(LOG_TAG, "Failed to create decoder's input memory file"); } return true; } catch (RemoteException e) { Log.e(LOG_TAG, "Failed to create x264 encoder"); } return false; }
public static boolean isMemoryFile(FileDescriptor fd) throws IOException { return (native_get_size(fd) >= 0); }