Android real-time video encoding-H.264 hard Encoding

Source: Internet
Author: User

Http://my.eoe.cn/682121/archive/1839.html

Author: changkai245 published in March 21 visit (533) Comment (0)

1 hard Encoding & 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 ++CodeGenerally, the binary files generated by compilation are all at a low speed. For example, use Android ndk to compile h264 to generate the so library, write the JNI interface, and then use Java to call the so library.

2 Principle
① Process: video is collected through mediarecoder, and then the video stream is mapped to localsocket for sending and receiving.
② Principle: see [streaming media] H264-MP4 format and extraction of h264 in MP4 files SPS, PPs and code stream and "[streaming media] Android real-time video acquisition-mediarecoder recording"

3. Core code
① Mediarecoder video collection
Video collection uses the mediarecorde recording method. For details, refer to "[streaming media] Android real-time video collection-mediarecoder recording"

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

1 2 3 4 5 6 7 8 9101112131415
// 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.

12345
Private void startrecording () {log. I (TAG, "# startrecording..."); New thread (this). Start ();}

Run:

@ Override
Public void run ()
{
Log. I (TAG, "# Run ....");

1 2 3 4 5 6 7 8 910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
// 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: view the pre-recorded video, the first 32 bytes of mdat need to be skipped (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

Statement: EOEArticleCopyright belongs to the author and is protected by law. The following information must be included in the form of a hyperlink during reprinting.

Author:
Changkai245

Original article address:

Http://my.eoe.cn/682121/archive/1839.html

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.