鍍金池/ 問(wèn)答/C  C++  Linux  網(wǎng)絡(luò)安全/ SDL線程中FFmpeg不能正常執(zhí)行問(wèn)題

SDL線程中FFmpeg不能正常執(zhí)行問(wèn)題

目前在入門(mén)學(xué)習(xí)FFmpeg相關(guān)內(nèi)容,在引入SDL線程時(shí),出現(xiàn)了如下問(wèn)題:

1. main線程中創(chuàng)建demux_thread線程

創(chuàng)建demux_thread線程

2. demux_thread中打開(kāi)文件

demux_thread中打開(kāi)文件

3. main線程中打開(kāi)文件

main線程中打開(kāi)文件

4. 完整的代碼ffplay.c

#include <stdio.h>
#include <libavformat/avformat.h>
#include <SDL.h>
#include <SDL_thread.h>
#include <libavutil/avstring.h>
#include "main.h"

int demux_thread(void *opaque) {
    VideoState *is = (VideoState *) opaque;
    AVFormatContext *ic = NULL;
    int ret;

    printf("執(zhí)行了。。。1");
    ret = avformat_open_input(&ic, is->filename, 0, 0);
    printf("執(zhí)行了。。。2");
    if (ret < 0) {
        char buf[1024];
        av_strerror(ret, buf, sizeof(buf));
        fprintf(stderr, "Failed avformat_open_input: %s\n", buf);
        return -1;
    }
    printf("執(zhí)行成功");
    return 0;
}

int main(int argc, char *argv[]) {
    VideoState *is = NULL;

    is = av_mallocz(sizeof(VideoState));

    av_register_all();

    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
        fprintf(stderr, "Failed SDL_Init: %s", SDL_GetError());
        exit(1);
    }

    char *filename = "/Users/xxxx/Workspace/C/ffplay/test.mp4";
    av_strlcpy(is->filename, filename, sizeof(is->filename));

    demux_thread(is);

//    is->demux_tid = SDL_CreateThread(demux_thread, "demux_thread", is);
//    if (!is->demux_tid) {
//        fprintf(stderr, "Failed SDL_CreateThread\n");
//        av_free(is);
//        return -1;
//    }

    return 0;
}

5. 為什么放在SDL_Thread中就不對(duì)了呢?

回答
編輯回答
久不遇

使用線程執(zhí)行 demux_thread 任務(wù)時(shí),主線程要等待它返回(可用 SDL_WaitThread()),否則 main() 函數(shù)返回時(shí)將強(qiáng)制結(jié)束其他線程。

2018年8月20日 01:05