Android端视频使用ffmpeg实时解码

首先我们来看java端代码

    while (mOpened) {
                    try {
                        int read = mInputStream.read(buffer);
                        if (read != 0 && read > 0) {
                            if (mOnReceivedListener == null)
                                return;
                            byte[] bytes = Arrays.copyOf(buffer, read);
                            mOnReceivedListener.onReceived(bytes);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                        errorCount++;
                    }
                    if (errorCount > 5) {
                        try {
                            close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

因为我这里是通过usb端口aoa协议拿到码流实现一个长链关系的。我们写入流之后就可以来看c方面的处理了。

    int i, videoindex;
    int retry_time = 0;

    if (ad == NULL)
        ad = (AndroidDisplay *) av_mallocz(sizeof(AndroidDisplay));
    jclass wc = env->FindClass("com/.../MainActivity");
    if (wc == 0) {
        LOGE("Could not find com.....MainActivity class !");
    }
    //拿到java层中读取的码流缓存的方法
    ad->j_sync_stream = env->GetMethodID(wc, "syncStream", "([BI)I");
    if (ad->j_sync_stream == 0)
        LOGE("Could not find 'syncStream' method\n");

    if (id) {
        ad->android_video_window = (env)->NewGlobalRef(id);
    } else
        ad->android_video_window = NULL;

    env->DeleteLocalRef(wc);
    //声明一个缓存空间
    buf = (uint8_t *) av_mallocz(BUF_SIZE);
    
    av_log_set_callback(ffmpeg_log_handler);
    //初始化
    avcodec_register_all();
    av_register_all();
  
    // avio_alloc_context开头会读取部分数据探测流的信息,不会全部读取,  除非设置的缓存过大 av_read_frame会在读帧的时候调用avio_alloc_context中的   read_packet方法取流数据,每隔avio_ctx_buffer_size调用一次,直至读完

    pb = avio_alloc_context(buf, BUF_SIZE, 0, NULL, av_read_data, NULL, NULL);
    
    if (pb == NULL)
        LOGE("avio_alloc_context失败了!!!");
        
    bool inital_ok = false;
    
    while (!inital_ok) {
        piFmt = NULL;
        if (av_probe_input_buffer(pb, &piFmt, NULL, NULL, 0,32 * 1024) < 0) {
            LOGE("av_probe_input_buffer 失败!\n");
            continue;
        } else {
            LOGE("av_probe_input_buffer 成功!\n");
        }

        pFormatCtx = avformat_alloc_context();
        pFormatCtx->pb = pb;
        
        while (avformat_open_input(&pFormatCtx, "", piFmt, NULL) != 0) {
            LOGE("无法打开输入流\n");
            continue;
        }
        
        videoindex = -1;
        for (i = 0; i < pFormatCtx->nb_streams; i++)
            if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
                videoindex = i;
                break;
            }
            
        if (videoindex == -1) {
            LOGE("无法找到视频流.\n");
            continue;
        }
        
        pCodecCtx = pFormatCtx->streams[videoindex]->codec;
        if (pCodecCtx->width == 0 || pCodecCtx->height == 0) {
            pCodecCtx->width = 1280;
            pCodecCtx->height = 720;
        }
        if (pCodecCtx != NULL)
            LOGE("pCodecCtx is not NULL!\n");
        pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
        if (pCodec == NULL) {
            LOGE("Couldn't find Codec.\n");
            continue;
        }

        LOGE("Stream Size is %d x %d \n", pCodecCtx->width, pCodecCtx->height);
        if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
            LOGE("Couldn't open codec.\n");
            continue;
        }
        LOGE("Now Stream Size is %d x %d \n", pCodecCtx->width, pCodecCtx->height);
        
        inital_ok = true;
        // 获取native window
        ANativeWindow *nativeWindow = ANativeWindow_fromSurface(env, surface);
        // 获取视频宽高
        int videoWidth = pCodecCtx->width;
        int videoHeight = pCodecCtx->height;
        // 设置native window的buffer大小,可自动拉伸
        ANativeWindow_setBuffersGeometry(nativeWindow, videoWidth, videoHeight,
                                         WINDOW_FORMAT_RGBA_8888);
        ANativeWindow_Buffer windowBuffer;
        // Allocate video frame
        AVFrame *pFrame = av_frame_alloc();

        // 用于渲染
        AVFrame *pFrameRGBA = av_frame_alloc();
        if (pFrameRGBA == NULL || pFrame == NULL) {
            LOGE("Could not allocate video frame.");
            return;
        }
        // Determine required buffer size and allocate buffer
        // buffer中数据就是用于渲染的,且格式为RGBA
        int numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGBA, pCodecCtx->width, pCodecCtx->height,
                                                1);
        uint8_t *buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
        av_image_fill_arrays(pFrameRGBA->data, pFrameRGBA->linesize, buffer, AV_PIX_FMT_RGBA,
                             pCodecCtx->width, pCodecCtx->height, 1);

        // 由于解码出来的帧格式不是RGBA的,在渲染之前
        // 需要进行格式转换
        struct SwsContext *sws_ctx = sws_getContext(pCodecCtx->width,
                                                    pCodecCtx->height,
                                                    AV_PIX_FMT_YUV420P,
                                                    pCodecCtx->width,
                                                    pCodecCtx->height,
                                                    AV_PIX_FMT_RGBA,
                                                    SWS_BILINEAR,
                                                    NULL,
                                                    NULL,
                                                    NULL);

        int frameFinished;
        AVPacket packet;
        while (av_read_frame(pFormatCtx, &packet) >= 0) {
            // Is this a packet from the video stream?
            if (packet.stream_index == videoindex) {

                // Decode video frame
                avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);

                // 并不是decode一次就可解码出一帧
                if (frameFinished) {

                    // lock native window buffer
                    ANativeWindow_lock(nativeWindow, &windowBuffer, 0);

                    // 格式转换
                    sws_scale(sws_ctx, (uint8_t const *const *) pFrame->data,
                              pFrame->linesize, 0, pCodecCtx->height,
                              pFrameRGBA->data, pFrameRGBA->linesize);

                    // 获取stride
                    uint8_t *dst = (uint8_t *) windowBuffer.bits;
                    int dstStride = windowBuffer.stride * 4;
                    uint8_t *src = (pFrameRGBA->data[0]);
                    int srcStride = pFrameRGBA->linesize[0];

                    // 由于window的stride和帧的stride不同,因此需要逐行复制
                    int h;
                    for (h = 0; h < videoHeight; h++) {
                        memcpy(dst + h * dstStride, src + h * srcStride, srcStride);
                    }

                    ANativeWindow_unlockAndPost(nativeWindow);
                }

            }
            av_packet_unref(&packet);
        }

        av_free(buffer);
        av_free(pFrameRGBA);

        // Free the YUV frame
        av_free(pFrame);

        // Close the codecs
        avcodec_close(pCodecCtx);

        // Close the video file
        avformat_close_input(&pFormatCtx);

这里需要注意的是读视频流的时候可能会丢失pCodecCtx的一些参数,如果丢失程序会发生崩溃,我这边就遇到了丢失了视频的宽高,还有视频的格式参数等等。。。暂时还不确定为什么会丢失。所以总结就是我们只需要把码流和SurfaceView传过来,当有码流存在的时候使用while循环进行一个长链接就行了,第一次分享,可能说的不太清楚。。。欢迎指导

猜你喜欢

转载自blog.csdn.net/qq_29877851/article/details/85069267