[Streaming media] Android real-time video encoding-H.264 hard Encoding

Source: Internet
Author: User

Http://www.cnblogs.com/skyseraph/archive/2012/04/04/2431771.html

1 hard Coding &
Soft Encoding

Hard Coding: By calling the camera recording video that comes with the Android system, it actually calls the underlying HD encoding hardware module, that is, the video card, which does not use the CPU and is fast.

Soft Encoding: Use the CPU for encoding, such as common C/C ++ code. Generally, the binary files generated by compilation are all slow. For example, Android
Ndk compiles h264 to generate the so library, write the JNI interface, and then use Java to call the so library.

2 Principle

① Process: Use mediarecoder to collect videos and map the video streams to localsocket for sending and receiving.

② PrincipleFor more information, see [streaming media] H264-MP4 format and extraction of h264 SPS, PPs and code stream and
"[Streaming media] Real-Time Android video collection-mediarecoder recording"

3 core code ① mediarecoder video collection

Video Capture uses the mediarecorde recording method. For details, see
"[Streaming media] Real-Time Android video collection-mediarecoder recording"

② In initiativevideo, setoutputfile is set to output in streaming mode.

// Set the output file mode: local storage or localsocket remote output if (bifnativeorremote = true) // native {lvideofilefullpath = strrecvideofilepath + String. valueof (system. currenttimemillis () + lvideofilefullpath; mrecvideofile = new file (lvideofilefullpath); // mmediarecorder. setoutputfile (mrecvideofile. getabsolutepath (); mmediarecorder. setoutputfile (mrecvideofile. getpath (); // called after set ** source before prepare L Og. I (TAG, "Start write into file ~ ");} Else // remote {mmediarecorder. setoutputfile (sender. getfiledescriptor (); // set to output log. I (TAG," Start send into sender ~ ");}
③ Startrecording

Function:Use this function to start the thread to map the video stream to localsocket and implement encoding.

    private void startRecording()    {        Log.i(TAG, "##startRecording....");        new Thread(this).start();                }
Run:
@ Override public void run () {log. I (TAG, "# Run .... "); // defines datainputstream = NULL; int offset = 0, TMP = 0, beleft = 0; try {datainputstream = new datainputstream (receiver. getinputstream ();} catch (ioexception E2) {// todo auto-generated Catch Block e2.printstacktrace ();} Try {thread. currentthread (). sleep (500);} catch (interruptedexception E1) {e1.printstacktrace ();} // method 1: To view the pre-recorded video, you need to skip the first 32 bytes of mdat (different hardware platforms !) Try {datainputstream. read (hsf-frame, 0, 32); log. I (TAG, "Read mdat from");} catch (ioexception E1) {// todo auto-generated Catch Block e1.printstacktrace ();} // Method 2: automated Search (to be supplemented) Try {hsf-fileoutputstream = new fileoutputstream ("/sdcard/AVSS/h264.264"); // 264 code stream log. I (TAG, "hsf-fileoutputstream");} catch (filenotfoundexception e) {// todo auto-generated Catch Block E. printstacktrace ();} Try {Hsf-fileoutputstream. write (hsf-head); hsf-fileoutputstream. write (hsf-sps); hsf-fileoutputstream. write (hsf-head); hsf-fileoutputstream. write (hsf-pps); log. I (TAG, "Run-write SPS/PPS to file");} catch (ioexception E1) {// todo auto-generated Catch Block e1.printstacktrace ();} while (bifrecinprocess &&(! Bifnativeorremote) {try {log. I (TAG, "** while... "); int hsf-length = datainputstream. readint (); log. I (TAG, "hsf-length:" + hsf-length); TMP = 0; offset = 0; beleft = 0; hsf-fileoutputstream. write (hsf-head); While (offset 
4 ref/related

1 http://blog.csdn.net/zblue78/article/details/6078040

2 http://blog.csdn.net/zblue78/article/details/6083374

3 http://www.docin.com/p-282454756.html

4 http://blog.csdn.net/qwertyuj/article/details/7029836

5
Http://www.cnblogs.com/skyseraph/archive/2012/03/31/2427593.html

6
Http://www.cnblogs.com/skyseraph/archive/2012/03/23/2415018.html

7
Http://www.cnblogs.com/skyseraph/archive/2012/04/01/2429384.html


Author: skyseraph
Source: http://www.cnblogs.com/skyseraph/
Email/Gtalk: zgzhaobo@gmail.com
The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.