常用h264编码流程分析

发送端逻辑

StreamController implements OnVideoEncodeListener

onVideoEncode 会回调 mPacker.onVideoData

onVideoData 调用 mAnnexbHelper.analyseVideoDataonlyH264
analyseVideoDataonlyH264 调用 onVideo
onVideo 调用 packetListener.onPacket
onPacket 调用 mSender.onData
onData 调用 mSendQueue.putFrame

@Override
public void run() {
    super.run();
    while (startFlag) {
        Frame frame = iSendQueue.takeFrame();
        if (frame == null) {
            continue;
        }
        if (frame.data instanceof Video) {
            sendData(((Video) frame.data).getData());
        }
    }
}

接收端逻辑

服务端启动socket监听
然后启动一个接收线程
在接收线程里不断读取数据,区分出音视频
mListener.onVideo(frame, isKeyFrame ? Frame.KEY_FRAME : Frame.NORMAL_FRAME);

            @Override
            public void onVideo(byte[] video, int type) {
                Frame frame = new Frame();
                switch (type) {
                    case Frame.KEY_FRAME:
                        frame.setType(Frame.KEY_FRAME);
                        frame.setBytes(video);
                        Log.d("AcceptH264MsgThread", "key frame ...");
                        AcceptStreamDataThread.this.listener.acceptBuff(frame);
                        break;
                    case Frame.NORMAL_FRAME:
                        frame.setType(Frame.NORMAL_FRAME);
                        frame.setBytes(video);
                        Log.d("AcceptH264MsgThread", "normal frame ...");
                        AcceptStreamDataThread.this.listener.acceptBuff(frame);
                        break;
                    case Frame.AUDIO_FRAME:
                        frame.setType(Frame.AUDIO_FRAME);
                        frame.setBytes(video);
                        Log.d("AcceptH264MsgThread", "audio frame ...");
                        AcceptStreamDataThread.this.listener.acceptBuff(frame);
                        break;
                    default:
                        Log.e("AcceptH264MsgThread", "other video...");
                        break;
                }

            }
然后把frame加入播放队列
private OnAcceptBuffListener mAcceptBuffListener = new OnAcceptBuffListener() {
    @Override
    public void acceptBuff(Frame frame) {
        if (mPlayQueue != null) mPlayQueue.putByte(frame);
    }
};
然后有个解码线程解码,最后经过surface显示出来

你可能感兴趣的:(音视频)