/** * Blocks and reads into a <tt>Buffer</tt> from this <tt>PullBufferStream</tt>. * * @param buffer the <tt>Buffer</tt> this <tt>PullBufferStream</tt> is to read into * @throws IOException if an I/O error occurs while this <tt>PullBufferStream</tt> reads into the * specified <tt>Buffer</tt> * @see AbstractVideoPullBufferStream#doRead(Buffer) */ @Override protected void doRead(Buffer buffer) throws IOException { /* * Determine the Format in which we're expected to output. We cannot * rely on the Format always being specified in the Buffer because it is * not its responsibility, the DataSource of this ImageStream knows the * output Format. */ Format format = buffer.getFormat(); if (format == null) { format = getFormat(); if (format != null) buffer.setFormat(format); } if (format instanceof AVFrameFormat) { Object o = buffer.getData(); AVFrame frame; if (o instanceof AVFrame) frame = (AVFrame) o; else { frame = new AVFrame(); buffer.setData(frame); } AVFrameFormat avFrameFormat = (AVFrameFormat) format; Dimension size = avFrameFormat.getSize(); ByteBuffer data = readScreenNative(size); if (data != null) { if (frame.avpicture_fill(data, avFrameFormat) < 0) { data.free(); throw new IOException("avpicture_fill"); } } else { /* * This can happen when we disconnect a monitor from computer * before or during grabbing. */ throw new IOException("Failed to grab screen."); } } else { byte[] bytes = (byte[]) buffer.getData(); Dimension size = ((VideoFormat) format).getSize(); bytes = readScreen(bytes, size); buffer.setData(bytes); buffer.setOffset(0); buffer.setLength(bytes.length); } buffer.setHeader(null); buffer.setTimeStamp(System.nanoTime()); buffer.setSequenceNumber(seqNo); buffer.setFlags(Buffer.FLAG_SYSTEM_TIME | Buffer.FLAG_LIVE_DATA); seqNo++; }
private void newImage(Buffer buffer) { Object data = buffer.getData(); if (!(data instanceof int[])) return; RGBFormat format = (RGBFormat) buffer.getFormat(); DirectColorModel dcm = new DirectColorModel( format.getBitsPerPixel(), format.getRedMask(), format.getGreenMask(), format.getBlueMask()); sourceImage = new MemoryImageSource( format.getLineStride(), format.getSize().height, dcm, (int[]) data, 0, format.getLineStride()); sourceImage.setAnimated(true); sourceImage.setFullBufferUpdates(true); if (component != null) { destImage = component.createImage(sourceImage); component.prepareImage(destImage, component); } }
@Override public int process(Buffer input, Buffer output) { if (!checkInputBuffer(input)) { return BUFFER_PROCESSED_FAILED; } if (isEOM(input)) { propagateEOM(output); // TODO: what about data? can there be any? return BUFFER_PROCESSED_OK; } try { // TODO: this is very inefficient - it allocates a new byte array // (or more) every time final ByteArrayInputStream is = new ByteArrayInputStream((byte[]) input.getData(), input.getOffset(), input.getLength()); final BufferedImage image = ImageIO.read(is); is.close(); final Buffer b = ImageToBuffer.createBuffer(image, ((VideoFormat) outputFormat).getFrameRate()); output.setData(b.getData()); output.setOffset(b.getOffset()); output.setLength(b.getLength()); output.setFormat(b.getFormat()); // TODO: this is a bit hacky, this // format will be more specific // than the actual set output // format, because now we know what // ImageIO gave us for a // BufferedImage as far as pixel // masks, etc. return BUFFER_PROCESSED_OK; } catch (IOException e) { output.setDiscard(true); output.setLength(0); return BUFFER_PROCESSED_FAILED; } }
/** Processes the data and renders it to a component */ public synchronized int process(Buffer buffer) { if (component == null) return BUFFER_PROCESSED_FAILED; Format inf = buffer.getFormat(); if (inf == null) return BUFFER_PROCESSED_FAILED; if (inf != inputFormat || !buffer.getFormat().equals(inputFormat)) { if (setInputFormat(inf) != null) return BUFFER_PROCESSED_FAILED; } Object data = buffer.getData(); if (!(data instanceof int[])) return BUFFER_PROCESSED_FAILED; if (lastBuffer != buffer) { lastBuffer = buffer; newImage(buffer); } sourceImage.newPixels(0, 0, inWidth, inHeight); Graphics g = component.getGraphics(); if (g != null) { if (reqBounds == null) { bounds = component.getBounds(); bounds.x = 0; bounds.y = 0; } else bounds = reqBounds; g.drawImage( destImage, bounds.x, bounds.y, bounds.width, bounds.height, 0, 0, inWidth, inHeight, component); } return BUFFER_PROCESSED_OK; }