FFmpeg can not execute properly in SDL thread

is currently getting started learning about FFmpeg. When introducing SDL threads, there are the following problems:

1. Create demux_ thread in main thread

demux_thread

2. demux_thread

demux_thread

3. main

main

4. Complete code ffplay.c

-sharpinclude <stdio.h>
-sharpinclude <libavformat/avformat.h>
-sharpinclude <SDL.h>
-sharpinclude <SDL_thread.h>
-sharpinclude <libavutil/avstring.h>
-sharpinclude "main.h"

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

    printf("1");
    ret = avformat_open_input(&ic, is->filename, 0, 0);
    printf("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("");
    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. Why is it wrong to put it in SDL_Thread?

Mar.20,2021

when you use a thread to execute a demux_thread task, the main thread waits for it to return (available SDL_WaitThread () ), otherwise the main () function will force other threads to end when it returns.

Menu