"Streaming media" Android real-time video encoding-h.264 hard-coded

Source: Internet
Author: User

"Streaming media" Android real-time video encoding-h.264 hard-coded

Skyseraph APR 4th 2012

Email:[email protected].com

1 hard-coded & Soft-coded

hard-coded : by calling the Android system's own C-Amera recording video, actually calls the underlying HD encoded hardware module, that is, the graphics card, not using the CPU, Fast

Soft Coding : Use the CPU to encode, such as common C + + Code, the general compilation of the generated binary is yes, the speed is relatively slow. For example , use the Android NDK to compile H264 to generate so libraries, write JNI interfaces, and then call the so library using Java .

2 principle

① process : Capture video through Mediarecoder, then map the video stream to Localsocket for transceiver

② principle : See the "Media" h264-mp4 format and extract H264 SPS, pps and bitstream in the MP4 file and "Media" Android live video capture-mediarecoder recording "

3 Core code ① mediarecoder video capture

Video capture using Mediarecorde recording method, see "Media" Android real-time video capture-mediarecoder recording "

② in initiativevideo setoutputfile set stream output

            Set output file mode: Direct 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 the output in stream mode
LOG.I (TAG, "Start send into sender~");
}
③ startrecording

function: Use this function to start the thread to implement the video stream mapping to Localsocket, while implementing the encoding

    private void Startrecording ()
{
LOG.I (TAG, "# #startRecording ....");
New Thread (This). Start ();
}
Run:
@Override
public void Run ()
{
LOG.I (TAG, "# #run ....");

Defines
DataInputStream 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 ();
}

① Way One: By viewing prerecorded video, you need to skip Mdat front 32 bytes (different hardware platforms are different!). )
Try
{
Datainputstream.read (h264frame, 0, 32);
LOG.I (TAG, "read Mdat from");
} catch (IOException E1)
{
TODO auto-generated Catch block
E1.printstacktrace ();
}
② Mode II: Automated search (to be supplemented)


Try
{
H264fileoutputstream = new FileOutputStream ("/sdcard/avss/h264.264");//264 bitstream
LOG.I (TAG, "H264fileoutputstream");
} catch (FileNotFoundException e)
{
TODO auto-generated Catch block
E.printstacktrace ();
}
Try
{
H264fileoutputstream.write (H264head);
H264fileoutputstream.write (H264sps);
H264fileoutputstream.write (H264head);
H264fileoutputstream.write (H264pps);
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 h264length = Datainputstream.readint ();
LOG.I (TAG, "h264length:" +h264length);
Tmp=0; OffSet = 0; Beleft = 0;

H264fileoutputstream.write (H264head);
while (OffSet < h264length)
{
Beleft = H264length-offset;
TMP = Datainputstream.read (h264frame, 0, read_size < beleft? Read_size:beleft);
LOG.I (TAG, String.Format ("H264%d,%d,%d", H264length, OffSet, TMP));
OffSet + = tmp;
H264fileoutputstream.write (h264frame, 0, TMP);
}

} catch (Exception e)
{
Todo:handle exception
E.printstacktrace ();
}
}
//
}
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

"Streaming media" Android real-time video encoding-h.264 hard-coded

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.