Android Open Source - sony-camera-remote-java Live View Fetcher






From Project

Back to project page sony-camera-remote-java.

License

The source code is released under:

Apache License

If you think the Android project sony-camera-remote-java listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.praetoriandroid.cameraremote;
//from   w  ww  .j  a  va2  s  .  co m
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;

@SuppressWarnings("UnusedDeclaration")
public class LiveViewFetcher {

    public static class Frame {

        private byte[] buffer;
        private int size;

        void update(byte[] buffer, int size) {
            this.buffer = buffer;
            this.size = size;
        }

        public byte[] getBuffer() {
            return buffer;
        }

        public int getSize() {
            return size;
        }
    }

    private static class DataBlock {

        protected ByteBuffer buffer;
        protected int dataAmount;

        public DataBlock(int size) {
            buffer = ByteBuffer.allocate(size);
        }

        /**
         * Read exactly as much bytes as <code>'amount'</code> parameter.
         * @param inputStream the stream for reading data.
         * @throws IOException if any I/O error occurred while reading data or in case of premature
         * end of stream.
         * @param amount number of bytes to read.
         */
        public void read(InputStream inputStream, int amount) throws IOException {
            int offset = 0;
            int left = amount;
            byte[] array = buffer.array();
            while (left > 0) {
                int length = inputStream.read(array, offset, left);
                if (length == -1) {
                    throw new EOFException();
                }
                left -= length;
                offset += length;
            }
            dataAmount = amount;
        }

        public void write(OutputStream outputStream) throws IOException {
            outputStream.write(buffer.array(), 0, dataAmount);
        }

        public void ensureBufferSize(int size) {
            if (buffer.array().length < size) {
                buffer = ByteBuffer.allocate(size);
            }
        }

    }

    private static class CommonHeader extends DataBlock {

        private static final int HEADER_SIZE = 8;

        private static final byte PAYLOAD_TYPE_LIVE_VIEW = 0x01;

        private short seqNumber;
        private long timestamp;

        public CommonHeader() {
            super(HEADER_SIZE);
        }

        public void read(InputStream inputStream) throws IOException, ParseException {
            super.read(inputStream, HEADER_SIZE);

            byte startByte = buffer.get(0);
            if (startByte != (byte) 0xff) {
                throw new ParseException("Illegal start byte: 0x" + Integer.toString(startByte, 16));
            }

            byte payloadType = buffer.get(1);
            if (payloadType != PAYLOAD_TYPE_LIVE_VIEW) {
                throw new ParseException("Illegal payload type: 0x" + Integer.toString(payloadType, 16));
            }

            seqNumber = buffer.getShort(2);
            timestamp = buffer.getInt(4);
        }

        public short getSeqNumber() {
            return seqNumber;
        }

        public long getTimestamp() {
            return timestamp;
        }
    }

    private static class PayloadHeader extends DataBlock {

        private static final int HEADER_SIZE = 128;

        private static final int START_CODE = 0x24356879;

        private int dataSize;
        private int paddingSize;

        public PayloadHeader() {
            super(HEADER_SIZE);
        }

        public void read(InputStream inputStream) throws IOException, ParseException {
            super.read(inputStream, HEADER_SIZE);

            int startCode = buffer.getInt(0);
            if (startCode != START_CODE) {
                throw new ParseException("Illegal start code: 0x" + Integer.toString(startCode, 16));
            }

            byte[] data = buffer.array();
            dataSize = (((data[4] & 0xff) << 16) |
                    ((data[5] & 0xff) << 8) |
                    ((data[6] & 0xff)));

            paddingSize = buffer.get(7);

            byte flag = buffer.get(12);
            if (flag != 0x00) {
                throw new ParseException("Illegal flag: 0x" + Integer.toString(flag, 16));
            }
        }

        public int getDataSize() {
            return dataSize;
        }

        public int getPaddingSize() {
            return paddingSize;
        }
    }

    private HttpClient httpClient = new HttpClient();
    private InputStream inputStream;
    private CommonHeader commonHeader = new CommonHeader();
    private PayloadHeader payloadHeader = new PayloadHeader();
    private DataBlock payloadData = new DataBlock(8192);
    private DataBlock padding = new DataBlock(0);
    private Frame reusableFrame = new Frame();
    private boolean closed;

    public void setConnectionTimeout(int timeout) {
        httpClient.setConnectionTimeout(timeout);
    }

    public void connect(String url) throws IOException {
        closed = false;
        inputStream = httpClient.get(url);
    }

    public void disconnect() throws IOException {
        closed = true;
        if (inputStream != null) {
            inputStream.close();
        }
    }

    public Frame getNextFrame() throws IOException, ParseException, LiveViewDisconnectedException {
        try {
            commonHeader.read(inputStream);
            payloadHeader.read(inputStream);

            payloadData.ensureBufferSize(payloadHeader.getDataSize());
            payloadData.read(inputStream, payloadHeader.getDataSize());

            padding.ensureBufferSize(payloadHeader.getPaddingSize());
            padding.read(inputStream, payloadHeader.getPaddingSize());

            reusableFrame.update(payloadData.buffer.array(), payloadData.dataAmount);
            return reusableFrame;
        } catch (IOException e) {
            if (closed) {
                throw new LiveViewDisconnectedException();
            } else {
                throw e;
            }
        }
    }

    public void writeNextFrame(OutputStream outputStream) throws IOException {
        payloadData.write(outputStream);
    }

}




Java Source Code List

com.praetoriandroid.cameraremote.BadHttpStatusException.java
com.praetoriandroid.cameraremote.DeviceDescription.java
com.praetoriandroid.cameraremote.HttpClient.java
com.praetoriandroid.cameraremote.InvalidDataFormatException.java
com.praetoriandroid.cameraremote.LiveViewDisconnectedException.java
com.praetoriandroid.cameraremote.LiveViewFetcher.java
com.praetoriandroid.cameraremote.Logger.java
com.praetoriandroid.cameraremote.ParseException.java
com.praetoriandroid.cameraremote.RpcClient.java
com.praetoriandroid.cameraremote.RpcException.java
com.praetoriandroid.cameraremote.ServiceNotSupportedException.java
com.praetoriandroid.cameraremote.SsdpClient.java
com.praetoriandroid.cameraremote.SsdpException.java
com.praetoriandroid.cameraremote.app.LiveView.java
com.praetoriandroid.cameraremote.app.MainActivity.java
com.praetoriandroid.cameraremote.app.Rpc.java
com.praetoriandroid.cameraremote.app.SelfTimerSelector.java
com.praetoriandroid.cameraremote.app.ThisApp.java
com.praetoriandroid.cameraremote.rpc.ActTakePictureRequest.java
com.praetoriandroid.cameraremote.rpc.ActTakePictureResponse.java
com.praetoriandroid.cameraremote.rpc.AwaitTakePictureRequest.java
com.praetoriandroid.cameraremote.rpc.BaseRequest.java
com.praetoriandroid.cameraremote.rpc.BaseResponse.java
com.praetoriandroid.cameraremote.rpc.CustomizableEntity.java
com.praetoriandroid.cameraremote.rpc.EventEntity.java
com.praetoriandroid.cameraremote.rpc.GetApplicationInfoRequest.java
com.praetoriandroid.cameraremote.rpc.GetApplicationInfoResponse.java
com.praetoriandroid.cameraremote.rpc.GetAvailableApiListRequest.java
com.praetoriandroid.cameraremote.rpc.GetAvailableApiListResponse.java
com.praetoriandroid.cameraremote.rpc.GetAvailableSelfTimerRequest.java
com.praetoriandroid.cameraremote.rpc.GetAvailableSelfTimerResponse.java
com.praetoriandroid.cameraremote.rpc.GetEventRequest.java
com.praetoriandroid.cameraremote.rpc.GetEventResponse.java
com.praetoriandroid.cameraremote.rpc.GetExposureModeRequest.java
com.praetoriandroid.cameraremote.rpc.GetMethodTypesRequest.java
com.praetoriandroid.cameraremote.rpc.GetMethodTypesResponse.java
com.praetoriandroid.cameraremote.rpc.GetSelfTimerRequest.java
com.praetoriandroid.cameraremote.rpc.GetSupportedSelfTimerRequest.java
com.praetoriandroid.cameraremote.rpc.GetSupportedSelfTimerResponse.java
com.praetoriandroid.cameraremote.rpc.GetVersionsRequest.java
com.praetoriandroid.cameraremote.rpc.GetVersionsResponse.java
com.praetoriandroid.cameraremote.rpc.IllegalResultSizeException.java
com.praetoriandroid.cameraremote.rpc.MethodTypesEntity.java
com.praetoriandroid.cameraremote.rpc.RpcMethod.java
com.praetoriandroid.cameraremote.rpc.SetSelfTimerRequest.java
com.praetoriandroid.cameraremote.rpc.SimpleResponse.java
com.praetoriandroid.cameraremote.rpc.StartLiveViewRequest.java
com.praetoriandroid.cameraremote.rpc.StartLiveViewResponse.java
com.praetoriandroid.cameraremote.rpc.StartMovieRecRequest.java
com.praetoriandroid.cameraremote.rpc.StartRecModeRequest.java
com.praetoriandroid.cameraremote.rpc.StopLiveviewRequest.java
com.praetoriandroid.cameraremote.rpc.StopMovieRecRequest.java
com.praetoriandroid.cameraremote.rpc.StopMovieRecResponse.java
com.praetoriandroid.cameraremote.rpc.StopRecModeRequest.java
com.praetoriandroid.cameraremote.rpc.ValidationException.java
com.praetoriandroid.cameraremote.rpc._DummyResponse.java
com.praetoriandroid.cameraremote.tool.Cache.java
com.praetoriandroid.cameraremote.tool.RemoteControl.java
com.praetoriandroid.widget.ItemViewCreator.java
com.praetoriandroid.widget.RadialFreeSpace.java
com.praetoriandroid.widget.RadialSelector.java