Android中抓取手机视频流数据。

http://blog.csdn.net/peijiangping1989/article/details/6942585

目前实时抓取手机视频数据有2种方法,一种是通过camera的回调获取源数据,这里获取的源数据是没有编码的数据。

有的人发送yuv数据然后在那绘制图片,也说视频聊天,真是可笑。这种方式是可是实现视频聊天的,但是需要移植编码库

目前可移植的有,android的opencore编码,参考http://www.shouyanwang.org/thread-184-1-1.html

ffmpeg编码,这个网上很难找,但是也有人实现哦。(编码效率好,但是支持不了高清视频)

编码完成可以通过rtp协议发送。就可以视频啦。rtp协议什么的就不说了哦。

以上方法的代码,在这个链接最好回复我贴了代码,不是完整的。链接:http://www.eoeandroid.com/thread-51460-1-1.html

第2种方法,也就是我用的方法。

通过MediaRecorder录制。然后绑定一个localsocket,可以获取编码后的视频数据传输;

代码如下:

[java] view plain copy print ?
  1. package com.pei;  
  2.   
  3. import java.io.DataInputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.graphics.PixelFormat;  
  9. import android.media.MediaRecorder;  
  10. import android.net.LocalServerSocket;  
  11. import android.net.LocalSocket;  
  12. import android.net.LocalSocketAddress;  
  13. import android.view.SurfaceHolder;  
  14. import android.view.SurfaceView;  
  15. import android.view.View;  
  16. import android.view.Window;  
  17. import android.view.WindowManager;  
  18.   
  19. /** 
  20.  * class name:VideoCameraActivity<BR> 
  21.  * class description:CATCH THE VIDEODATA SEND TO RED5<BR> 
  22.  * PS: <BR> 
  23.  *  
  24.  * @version 1.00 2011/11/05 
  25.  * @author CODYY)peijiangping 
  26.  */  
  27. public class VideoCameraActivity extends Activity implements  
  28.         SurfaceHolder.Callback, MediaRecorder.OnErrorListener,  
  29.         MediaRecorder.OnInfoListener {  
  30.     private static final int mVideoEncoder = MediaRecorder.VideoEncoder.H264;  
  31.     private LocalSocket receiver, sender;  
  32.     private LocalServerSocket lss;  
  33.     private MediaRecorder mMediaRecorder = null;  
  34.     private boolean mMediaRecorderRecording = false;  
  35.     private SurfaceView mSurfaceView = null;  
  36.     private SurfaceHolder mSurfaceHolder = null;  
  37.     private Thread t;  
  38.   
  39.     @Override  
  40.     public void onCreate(Bundle savedInstanceState) {  
  41.         super.onCreate(savedInstanceState);  
  42.         getWindow().setFormat(PixelFormat.TRANSLUCENT);  
  43.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  44.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  45.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  46.         setContentView(R.layout.main);  
  47.         mSurfaceView = (SurfaceView) this.findViewById(R.id.surface_camera);  
  48.         SurfaceHolder holder = mSurfaceView.getHolder();  
  49.         holder.addCallback(this);  
  50.         holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
  51.         mSurfaceView.setVisibility(View.VISIBLE);  
  52.         receiver = new LocalSocket();  
  53.         try {  
  54.             lss = new LocalServerSocket("VideoCamera");  
  55.             receiver.connect(new LocalSocketAddress("VideoCamera"));  
  56.             receiver.setReceiveBufferSize(500000);  
  57.             receiver.setSendBufferSize(500000);  
  58.             sender = lss.accept();  
  59.             sender.setReceiveBufferSize(500000);  
  60.             sender.setSendBufferSize(500000);  
  61.         } catch (IOException e) {  
  62.             finish();  
  63.             return;  
  64.         }  
  65.     }  
  66.   
  67.     @Override  
  68.     public void onPause() {  
  69.         super.onPause();  
  70.         if (mMediaRecorderRecording) {  
  71.             stopVideoRecording();  
  72.             try {  
  73.                 lss.close();  
  74.                 receiver.close();  
  75.                 sender.close();  
  76.             } catch (IOException e) {  
  77.                 e.printStackTrace();  
  78.             }  
  79.         }  
  80.         finish();  
  81.     }  
  82.   
  83.     private void stopVideoRecording() {  
  84.         System.out.println("stopVideoRecording");  
  85.         if (mMediaRecorderRecording || mMediaRecorder != null) {  
  86.             if (t != null)  
  87.                 t.interrupt();  
  88.             releaseMediaRecorder();  
  89.         }  
  90.     }  
  91.   
  92.     private void startVideoRecording() {  
  93.         (t = new Thread() {  
  94.             public void run() {  
  95.                 int frame_size = 20000;  
  96.                 byte[] buffer = new byte[1024 * 64];  
  97.                 int num, number = 0;  
  98.                 InputStream fis = null;  
  99.                 try {  
  100.                     fis = receiver.getInputStream();  
  101.                 } catch (IOException e1) {  
  102.                     return;  
  103.                 }  
  104.                 number = 0;  
  105.                 releaseMediaRecorder();  
  106.                 while (true) {  
  107.                     System.out.println("ok");  
  108.                     try {  
  109.                         num = fis.read(buffer, number, frame_size);  
  110.                         number += num;  
  111.                         if (num < frame_size) {  
  112.                             System.out.println("recoend break");  
  113.                             break;  
  114.                         }  
  115.                     } catch (IOException e) {  
  116.                         System.out.println("exception break");  
  117.                         break;  
  118.                     }  
  119.                 }  
  120.                 initializeVideo();  
  121.                 number = 0;  
  122.                 Consumer consumer = new Publisher();// Publisher继承了Consumer   
  123.                 Thread consumerThread = new Thread((Runnable) consumer);  
  124.                 consumer.setRecording(true);// 设置线程状态;   
  125.                 consumerThread.start();// 开始发布数据流   
  126.                 DataInputStream dis = new DataInputStream(fis);  
  127.                 try {  
  128.                     dis.read(buffer, 032);  
  129.                 } catch (IOException e1) {  
  130.                     e1.printStackTrace();  
  131.                 }  
  132.                 byte[] aa = { 0x010x42, (byte0x800x0A, (byte0xFF,  
  133.                         (byte0xE10x000x120x670x42, (byte0x800x0A,  
  134.                         (byte0xE90x02, (byte0xC10x290x080x000x00,  
  135.                         0x1F0x400x000x04, (byte0xE20x000x200x01,  
  136.                         0x000x040x68, (byte0xCE0x3C, (byte0x80 };  
  137.                 consumer.putData(System.currentTimeMillis(), aa, 33);  
  138.                 while (true) {  
  139.                     try {  
  140.                         int h264length = dis.readInt();  
  141.                         number = 0;  
  142.                         while (number < h264length) {  
  143.                             int lost = h264length - number;  
  144.                             num = fis.read(buffer, 0,  
  145.                                     frame_size < lost ? frame_size : lost);  
  146.                             number += num;  
  147.                             consumer.putData(System.currentTimeMillis(),  
  148.                                     buffer, num);  
  149.                         }  
  150.                     } catch (IOException e) {  
  151.                         break;  
  152.                     }  
  153.                 }  
  154.                 consumer.setRecording(false);// 设置线程状态;   
  155.             }  
  156.         }).start();  
  157.     }  
  158.   
  159.     private boolean initializeVideo() {  
  160.         System.out.println("initializeVideo");  
  161.         if (mSurfaceHolder == null)  
  162.             return false;  
  163.         mMediaRecorderRecording = true;  
  164.         if (mMediaRecorder == null)  
  165.             mMediaRecorder = new MediaRecorder();  
  166.         else  
  167.             mMediaRecorder.reset();  
  168.         mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);  
  169.         mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);  
  170.         mMediaRecorder.setVideoFrameRate(20);  
  171.         mMediaRecorder.setVideoSize(352288);  
  172.         mMediaRecorder.setVideoEncoder(mVideoEncoder);  
  173.         mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());  
  174.         mMediaRecorder.setMaxDuration(0);  
  175.         mMediaRecorder.setMaxFileSize(0);  
  176.         mMediaRecorder.setOutputFile(sender.getFileDescriptor());  
  177.         try {  
  178.             mMediaRecorder.setOnInfoListener(this);  
  179.             mMediaRecorder.setOnErrorListener(this);  
  180.             mMediaRecorder.prepare();  
  181.             mMediaRecorder.start();  
  182.         } catch (IOException exception) {  
  183.             releaseMediaRecorder();  
  184.             finish();  
  185.             return false;  
  186.         }  
  187.         return true;  
  188.     }  
  189.   
  190.     private void releaseMediaRecorder() {  
  191.         System.out.println("Releasing media recorder.");  
  192.         if (mMediaRecorder != null) {  
  193.             if (mMediaRecorderRecording) {  
  194.                 try {  
  195.                     mMediaRecorder.setOnErrorListener(null);  
  196.                     mMediaRecorder.setOnInfoListener(null);  
  197.                     mMediaRecorder.stop();  
  198.                 } catch (RuntimeException e) {  
  199.                     System.out.println("stop fail: " + e.getMessage());  
  200.                 }  
  201.                 mMediaRecorderRecording = false;  
  202.             }  
  203.             mMediaRecorder.reset();  
  204.             mMediaRecorder.release();  
  205.             mMediaRecorder = null;  
  206.         }  
  207.     }  
  208.   
  209.     @Override  
  210.     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {  
  211.         System.out.println("surfaceChanged");  
  212.         mSurfaceHolder = holder;  
  213.         if (!mMediaRecorderRecording) {  
  214.             initializeVideo();  
  215.             startVideoRecording();  
  216.         }  
  217.     }  
  218.   
  219.     @Override  
  220.     public void surfaceCreated(SurfaceHolder holder) {  
  221.         System.out.println("surfaceCreated");  
  222.         mSurfaceHolder = holder;  
  223.     }  
  224.   
  225.     @Override  
  226.     public void surfaceDestroyed(SurfaceHolder holder) {  
  227.         System.out.println("surfaceDestroyed");  
  228.         mSurfaceView = null;  
  229.         mSurfaceHolder = null;  
  230.         mMediaRecorder = null;  
  231.         if (t != null) {  
  232.             t.interrupt();  
  233.         }  
  234.     }  
  235.   
  236.     @Override  
  237.     public void onInfo(MediaRecorder mr, int what, int extra) {  
  238.         switch (what) {  
  239.         case MediaRecorder.MEDIA_RECORDER_INFO_UNKNOWN:  
  240.             System.out.println("MEDIA_RECORDER_INFO_UNKNOWN");  
  241.             break;  
package com.pei; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import android.app.Activity; import android.os.Bundle; import android.graphics.PixelFormat; import android.media.MediaRecorder; import android.net.LocalServerSocket; import android.net.LocalSocket; import android.net.LocalSocketAddress; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.Window; import android.view.WindowManager; /** * class name:VideoCameraActivity<BR> * class description:CATCH THE VIDEODATA SEND TO RED5<BR> * PS: <BR> * * @version 1.00 2011/11/05 * @author CODYY)peijiangping */ public class VideoCameraActivity extends Activity implements SurfaceHolder.Callback, MediaRecorder.OnErrorListener, MediaRecorder.OnInfoListener { private static final int mVideoEncoder = MediaRecorder.VideoEncoder.H264; private LocalSocket receiver, sender; private LocalServerSocket lss; private MediaRecorder mMediaRecorder = null; private boolean mMediaRecorderRecording = false; private SurfaceView mSurfaceView = null; private SurfaceHolder mSurfaceHolder = null; private Thread t; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFormat(PixelFormat.TRANSLUCENT); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); mSurfaceView = (SurfaceView) this.findViewById(R.id.surface_camera); SurfaceHolder holder = mSurfaceView.getHolder(); holder.addCallback(this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mSurfaceView.setVisibility(View.VISIBLE); receiver = new LocalSocket(); try { lss = new LocalServerSocket("VideoCamera"); receiver.connect(new LocalSocketAddress("VideoCamera")); receiver.setReceiveBufferSize(500000); receiver.setSendBufferSize(500000); sender = lss.accept(); sender.setReceiveBufferSize(500000); sender.setSendBufferSize(500000); } catch (IOException e) { finish(); return; } } @Override public void onPause() { super.onPause(); if (mMediaRecorderRecording) { stopVideoRecording(); try { lss.close(); receiver.close(); sender.close(); } catch (IOException e) { e.printStackTrace(); } } finish(); } private void stopVideoRecording() { System.out.println("stopVideoRecording"); if (mMediaRecorderRecording || mMediaRecorder != null) { if (t != null) t.interrupt(); releaseMediaRecorder(); } } private void startVideoRecording() { (t = new Thread() { public void run() { int frame_size = 20000; byte[] buffer = new byte[1024 * 64]; int num, number = 0; InputStream fis = null; try { fis = receiver.getInputStream(); } catch (IOException e1) { return; } number = 0; releaseMediaRecorder(); while (true) { System.out.println("ok"); try { num = fis.read(buffer, number, frame_size); number += num; if (num < frame_size) { System.out.println("recoend break"); break; } } catch (IOException e) { System.out.println("exception break"); break; } } initializeVideo(); number = 0; Consumer consumer = new Publisher();// Publisher继承了Consumer Thread consumerThread = new Thread((Runnable) consumer); consumer.setRecording(true);// 设置线程状态; consumerThread.start();// 开始发布数据流 DataInputStream dis = new DataInputStream(fis); try { dis.read(buffer, 0, 32); } catch (IOException e1) { e1.printStackTrace(); } byte[] aa = { 0x01, 0x42, (byte) 0x80, 0x0A, (byte) 0xFF, (byte) 0xE1, 0x00, 0x12, 0x67, 0x42, (byte) 0x80, 0x0A, (byte) 0xE9, 0x02, (byte) 0xC1, 0x29, 0x08, 0x00, 0x00, 0x1F, 0x40, 0x00, 0x04, (byte) 0xE2, 0x00, 0x20, 0x01, 0x00, 0x04, 0x68, (byte) 0xCE, 0x3C, (byte) 0x80 }; consumer.putData(System.currentTimeMillis(), aa, 33); while (true) { try { int h264length = dis.readInt(); number = 0; while (number < h264length) { int lost = h264length - number; num = fis.read(buffer, 0, frame_size < lost ? frame_size : lost); number += num; consumer.putData(System.currentTimeMillis(), buffer, num); } } catch (IOException e) { break; } } consumer.setRecording(false);// 设置线程状态; } }).start(); } private boolean initializeVideo() { System.out.println("initializeVideo"); if (mSurfaceHolder == null) return false; mMediaRecorderRecording = true; if (mMediaRecorder == null) mMediaRecorder = new MediaRecorder(); else mMediaRecorder.reset(); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mMediaRecorder.setVideoFrameRate(20); mMediaRecorder.setVideoSize(352, 288); mMediaRecorder.setVideoEncoder(mVideoEncoder); mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface()); mMediaRecorder.setMaxDuration(0); mMediaRecorder.setMaxFileSize(0); mMediaRecorder.setOutputFile(sender.getFileDescriptor()); try { mMediaRecorder.setOnInfoListener(this); mMediaRecorder.setOnErrorListener(this); mMediaRecorder.prepare(); mMediaRecorder.start(); } catch (IOException exception) { releaseMediaRecorder(); finish(); return false; } return true; } private void releaseMediaRecorder() { System.out.println("Releasing media recorder."); if (mMediaRecorder != null) { if (mMediaRecorderRecording) { try { mMediaRecorder.setOnErrorListener(null); mMediaRecorder.setOnInfoListener(null); mMediaRecorder.stop(); } catch (RuntimeException e) { System.out.println("stop fail: " + e.getMessage()); } mMediaRecorderRecording = false; } mMediaRecorder.reset(); mMediaRecorder.release(); mMediaRecorder = null; } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { System.out.println("surfaceChanged"); mSurfaceHolder = holder; if (!mMediaRecorderRecording) { initializeVideo(); startVideoRecording(); } } @Override public void surfaceCreated(SurfaceHolder holder) { System.out.println("surfaceCreated"); mSurfaceHolder = holder; } @Override public void surfaceDestroyed(SurfaceHolder holder) { System.out.println("surfaceDestroyed"); mSurfaceView = null; mSurfaceHolder = null; mMediaRecorder = null; if (t != null) { t.interrupt(); } } @Override public void onInfo(MediaRecorder mr, int what, int extra) { switch (what) { case MediaRecorder.MEDIA_RECORDER_INFO_UNKNOWN: System.out.println("MEDIA_RECORDER_INFO_UNKNOWN"); break;
[java] view plain copy print ?
  1.     case MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED:  
  2.         System.out.println("MEDIA_RECORDER_INFO_MAX_DURATION_REACHED");  
  3.         break;  
  4.     case MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED:  
  5.         System.out.println("MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED");  
  6.         break;  
  7.     }  
  8. }  
  9.   
  10. @Override  
  11. public void onError(MediaRecorder mr, int what, int extra) {  
  12.     if (what == MediaRecorder.MEDIA_RECORDER_ERROR_UNKNOWN) {  
  13.         System.out.println("MEDIA_RECORDER_ERROR_UNKNOWN");  
  14.         finish();  
  15.     }  
  16. }  
case MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED: System.out.println("MEDIA_RECORDER_INFO_MAX_DURATION_REACHED"); break; case MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED: System.out.println("MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED"); break; } } @Override public void onError(MediaRecorder mr, int what, int extra) { if (what == MediaRecorder.MEDIA_RECORDER_ERROR_UNKNOWN) { System.out.println("MEDIA_RECORDER_ERROR_UNKNOWN"); finish(); } } }

你可能感兴趣的:(android,exception,null,buffer,手机,byte)