MediaCodec : H264硬解碼核心代碼總結__流媒體

來源:互聯網
上載者:User

解碼過程分為兩個過程:

1.擷取h264視屏流,並進視屏流放入到解碼器中的buffer中 

2.擷取解碼器的buffer ,並擷取buffer中解碼後的資料,並將其設定到SurfaceView 上進行顯示

 

顯示介面:

mSurfaceView = (SurfaceView) findViewById(R.id.sv);mSurfaceHolder = mSurfaceView.getHolder();mSurfaceHolder.addCallback(this);

 

在surfaceCreated回調中開啟2.中讀取解碼資料的迴圈線程

public class HardDecodeThread extends Thread{@Overridepublic void run() {try {mMediaCodec = MediaCodec.createDecoderByType("video/avc");MediaFormat format = MediaFormat.createVideoFormat("video/avc", 1280, 720);mMediaCodec.configure(format, mSurfaceHolder.getSurface(), null, 0);mMediaCodec.start();//注意開啟編碼,不然後面的會報錯showLog("start get output veido info");while(isPlay){BufferInfo bufInfo = new BufferInfo();int outputIndex = 0;while((outputIndex = mMediaCodec.dequeueOutputBuffer(bufInfo,20 * 1000)) >= 0 ){mMediaCodec.releaseOutputBuffer(outputIndex, true);}if(outputIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED){showLog("INFO_OUTPUT_BUFFERS_CHANGED");}else if(outputIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED){showLog("INFO_OUTPUT_FORMAT_CHANGED");}}if(mMediaCodec != null){mMediaCodec.stop();mMediaCodec.release();mMediaCodec = null;}} catch (Exception e) {e.printStackTrace();showLog("不支援寫入程式碼_error:" + e.getMessage());}}}


進行1部分:不斷擷取h264碼流進行解碼

if(mMediaCodec != null){ByteBuffer[] buffers = mMediaCodec.getInputBuffers();int inputBufferIndex = mMediaCodec.dequeueInputBuffer(20 * 1000);showLog("inputBufferIndex:" + inputBufferIndex);if(inputBufferIndex >= 0){ByteBuffer buffer = buffers[inputBufferIndex];buffer.clear();buffer.put(data);mMediaCodec.queueInputBuffer(inputBufferIndex, 0, data.length, 0, 0);//data表示h264的位元組數組}}


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.