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

Source: Internet
Author: User
ArticleDirectory
    •  
[Streaming media] Android real-time video encoding-H.264 hard Encoding

Skyseraph APR 4-th 2012

Email: zgzhaobo@gmail.com QQ: 452728574

1 Hard Coding & Soft Encoding

Hard Coding: By callingAndroidCAmeraVideo Recording actually calls the underlying HD encoding hardware module, that is, the video card.CPU, Fast

Soft Encoding: Use CPU Encoding, such as common C/C ++ CodeGenerally, the binary files generated by compilation are all at a low speed. For example Android ndk Compile H264 Generate So Library, write JNI Interface, and then use Java Call So Library.

 

2 Principle

① Process: Use mediarecoder to collect videos and map the video streams 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 Medium Setoutputfile Set 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
Log. I (TAG, "Start write into file ~ ");
}
Else // Remote
{
Mmediarecorder. setoutputfile (sender. getfiledescriptor ()); // Set output in streaming mode
Log. I (TAG, "Start send into sender ~ ");
}
③ Startrecording

Function:Use this function to start a thread to map video streamsLocalsocketAnd code

 
Private VoidStartrecording ()
{
Log. I (TAG, "# startrecording ....");
NewThread (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 (ER er. 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 a pre-recorded video, you must 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 streams
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 {
Beleft = hsf-length-offset;
TMP = datainputstream. Read (hsf-frame, 0, read_size <beleft? Read_size: beleft );
Log. I (TAG, String. Format ("h264% d, % d, % d", hsf-length, offset, TMP ));
Offset + = TMP;
Hsf-fileoutputstream. Write (hsf-frame, 0, TMP );
}

} Catch (Exception E)
{
// Todo: handle exception
E. printstacktrace ();
}
}
//
}
4 ref/related

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

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

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

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

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

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

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

 

 

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.