Android Red5视频通讯第三篇:视频数据采集

Android Red5视频通讯第三篇:视频数据采集

1.视频编码RemoteUtil.java

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
/**
 *  RTMP decord
 * @author Administrator
 *
 */
public class RemoteUtil {
    private static Deflater deflater = new Deflater();
    public static byte[] decodeYUV420SP2RGB(byte[] yuv420sp, int width, int height) {
        final int frameSize = width * height;
        byte[] rgbBuf = new byte[frameSize * 3];
        // if (rgbBuf == null) throw new NullPointerException("buffer 'rgbBuf' is null");
        if (rgbBuf.length < frameSize * 3) throw new IllegalArgumentException("buffer 'rgbBuf' size "  + rgbBuf.length + " < minimum " + frameSize * 3);
        if (yuv420sp == null) throw new NullPointerException("buffer 'yuv420sp' is null");
        if (yuv420sp.length < frameSize * 3 / 2) throw new IllegalArgumentException("buffer 'yuv420sp' size " + yuv420sp.length + " < minimum " + frameSize * 3 / 2);
        int i = 0, y = 0;
        int uvp = 0, u = 0, v = 0;
        int y1192 = 0, r = 0, g = 0, b = 0;
        for (int j = 0, yp = 0; j < height; j++) {
            uvp = frameSize + (j >> 1) * width;
            u = 0;
            v = 0;
            for (i = 0; i < width; i++, yp++) {
                y = (0xff & ((int) yuv420sp[yp])) - 16;
                if (y < 0) y = 0;
                if ((i & 1) == 0) {
                    v = (0xff & yuv420sp[uvp++]) - 128;
                    u = (0xff & yuv420sp[uvp++]) - 128;
                }
                y1192 = 1192 * y;
                r = (y1192 + 1634 * v);
                g = (y1192 - 833 * v - 400 * u);
                b = (y1192 + 2066 * u);
                if (r < 0) r = 0; else if (r > 262143) r = 262143;
                if (g < 0) g = 0; else if (g > 262143) g = 262143;
                if (b < 0) b = 0; else if (b > 262143) b = 262143;
                rgbBuf[yp * 3] = (byte)(r >> 10);
                rgbBuf[yp * 3 + 1] = (byte)(g >> 10);
                rgbBuf[yp * 3 + 2] = (byte)(b >> 10);
                /**
                 现在的编码出来的颜色是反的 如 蓝色成了橙色,黄的是红的
                 *     尝试 b g r 顺序
                 */
            }
        }//for
        return rgbBuf;
    }// decodeYUV420Sp2RGB


    public static byte[] decodeYUV420SP2YUV420(byte[]data,int length) {
        int width = 176;
        int height = 144;
        byte[] str = new byte[length];
        System.arraycopy(data, 0, str, 0,width*height);
        int strIndex = width*height;
        for(int i = width*height+1; i < length ;i+=2) {
            str[strIndex++] = data[i];
        }
        for(int i = width*height;i<length;i+=2) {
            str[strIndex++] = data[i];
        }
        return str;
    } //YUV420SP2YUV420


    public static byte[] encode(final byte[] current, final byte[] previous, final int blockWidth, final int blockHeight, final int width, final int height) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(16 * 1024);
        if (previous == null) {
            baos.write(getTag(0x01 /* key-frame */, 0x03 /* ScreenVideo codec */));
        } else {
            baos.write(getTag(0x02 /* inter-frame */, 0x03 /* ScreenVideo codec */));
        }
        // write header
        final int wh = width + ((blockWidth / 16 - 1) << 12);
        final int hh = height + ((blockHeight / 16 - 1) << 12);
        writeShort(baos, wh);
        writeShort(baos, hh);
        // write content
        int y0 = height;
        int x0 = 0;
        int bwidth = blockWidth;
        int bheight = blockHeight;
        while (y0 > 0) {
            bheight = Math.min(y0, blockHeight);
            y0 -= bheight;
            bwidth = blockWidth;
            x0 = 0;
            while (x0 < width) {
                bwidth = (x0 + blockWidth > width) ? width - x0 : blockWidth;
                final boolean changed = isChanged(current, previous, x0, y0, bwidth, bheight, width, height);
                if (changed) {
                    ByteArrayOutputStream blaos = new ByteArrayOutputStream(4 * 1024);
                    DeflaterOutputStream dos = new DeflaterOutputStream(blaos, deflater);
                   for (int y = 0; y < bheight; y++) {
                        //Log.i("DEBUG", "current的长度:"+current.length+" 起始点:"+3 * ((y0 + bheight - y - 1) * width + x0)+" 终点:"+3 * bwidth);
                        dos.write(current, 3 * ((y0 + bheight - y - 1) * width + x0), 3 * bwidth);
                    }
//                    dos.write(current, 0, current.length);
                    dos.finish();
                    deflater.reset();
                    final byte[] bbuf = blaos.toByteArray();
                    final int written = bbuf.length;
                    // write DataSize
                    writeShort(baos, written);
                    // write Data
                    baos.write(bbuf, 0, written);
                } else {
                    // write DataSize
                    writeShort(baos, 0);
                }
                x0 += bwidth;
            }
        }
        return baos.toByteArray();
    }

    /**
     * Writes short value to the {@link OutputStream <tt>os</tt>}.
     *
     * @param os
     * @param n
     * @throws Exception if an exception occurred
     */
    private static void writeShort(OutputStream os, final int n) throws Exception {
        os.write((n >> 8) & 0xFF);
        os.write((n >> 0) & 0xFF);
    }
    /**
     * Checks if image block is changed.
     * @param current
     * @param previous
     * @param x0
     * @param y0
     * @param blockWidth
     * @param blockHeight
     * @param width
     * @param height
     * @return <code>true</code> if changed, otherwise <code>false</code>
     */
    public static boolean isChanged(final byte[] current, final byte[] previous, final int x0, final int y0, final int blockWidth, final int blockHeight, final int width, final int height) {
        if (previous == null)
            return true;
        for (int y = y0, ny = y0 + blockHeight; y < ny; y++) {
            final int foff = 3 * (x0 + width * y);
            final int poff = 3 * (x0 + width * y);
            for (int i = 0, ni = 3 * blockWidth; i < ni; i++) {
                if (current[foff + i] != previous[poff + i])
                    return true;
            }
        }
        return false;
    }

    /**
     * @param frame
     * @param codec
     * @return tag
     */
    public static int getTag(final int frame, final int codec) {
        return ((frame & 0x0F) << 4) + ((codec & 0x0F) << 0);
    }
}


 

2.视频数据采集WechatCamera .java

import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.smaxe.io.ByteArray;
import com.smaxe.uv.client.camera.AbstractCamera;
import com.smaxe.uv.stream.support.MediaDataByteArray;
import java.io.IOException;

public class WechatCamera extends AbstractCamera implements SurfaceHolder.Callback, Camera.PreviewCallback {
    private SurfaceHolder surfaceHolder;
    private Camera camera;
    private int width;
    private int height;
    private boolean init;
    int blockWidth;
    int blockHeight;
    int timeBetweenFrames; // 1000 / frameRate
    int frameCounter;
    byte[] previous;
    public WechatCamera(SurfaceView surfaceView) {
        surfaceView.getHolder().addCallback(WechatCamera.this);
        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        width = 320;
        height = 240;
        init = false;
    }


    public void start() {
        initCamera();
        if (camera != null)
            camera.startPreview();
    }

    public void stop() {
        if (camera != null) {
            camera.stopPreview();
            camera.setPreviewCallback(null);
            camera.release();
            camera = null;
        }
    }

    @Override
    public void onPreviewFrame(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub
        //if (!active) return;
        if (!init) {
            blockWidth = 32;
            blockHeight = 32;
            timeBetweenFrames = 100; // 1000 / frameRate
            frameCounter = 0;
            previous = null;
            init = true;
        }
        final long ctime = System.currentTimeMillis();
        byte[] current = RemoteUtil.decodeYUV420SP2RGB(arg0, width, height);
        try {
            final byte[] packet = RemoteUtil.encode(current, previous, blockWidth, blockHeight, width, height);
            // Log.d("DEBUG", packet.toString());
            fireOnVideoData(new MediaDataByteArray(timeBetweenFrames, new ByteArray(packet)));
            previous = current;
           if (++frameCounter % 10 == 0) previous = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        final int spent = (int) (System.currentTimeMillis() - ctime);
        try {
            Thread.sleep(Math.max(0, timeBetweenFrames - spent));
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        Log.i("SWService", "surfaceCreated");
        surfaceHolder = holder;    
    }

    /**
     *初始化攝像頭
     * */
    private void initCamera() {
        try {
            if (surfaceHolder == null) {
                return;
            }
            camera = Camera.open(0);
            if (camera == null) {
                camera = Camera.open();
            }
            if (camera == null) {
                return;
            }
            camera.setPreviewDisplay(surfaceHolder);
            camera.setPreviewCallback(this);
            Camera.Parameters params = camera.getParameters();
            params.setPreviewSize(width, height);
            camera.setParameters(params);
            camera.setDisplayOrientation(90);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.i("SWService", "get camera error:" + e.getMessage());
            stop();
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        stop();
    }
}


3.视频数据预览WechatCameraView.java
 


import android.content.Context;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class WechatCameraView extends SurfaceView {
    private WechatCamera wechatCamera;
    public WechatCameraView(Context context) {
        this(context, null);
    }

    public WechatCameraView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WechatCameraView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
    private void init(Context context) {
        wechatCamera = new WechatCamera( this);
    }
    public WechatCamera getWechatCamera() {
        return wechatCamera;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值