Android中抓取手机视频流数据

目前实时抓取手机视频数据有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;  
[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. }  

你可能感兴趣的:(Android中抓取手机视频流数据)