vlc sdl 播放视频可随窗口改变大小

#include <stdio.h>
#include <stdint.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
 
#include <SDL2/SDL.h>
#include <SDL2/SDL_mutex.h>
 
#include <vlc/vlc.h>
 
#define VIDEOWIDTH 1920
#define VIDEOHEIGHT 1080
 
struct context {
    SDL_Renderer *renderer;
    SDL_Texture *texture;
    SDL_mutex *mutex;
    int n;
 
    SDL_Rect displayrect;
    int window_w;
    int window_h;
};
 
// VLC prepares to render a video frame.
static void *lock(void *data, void **p_pixels) {
 
    struct context *c = (struct context *) data;
 
    int pitch;
    SDL_LockMutex(c->mutex);
    SDL_LockTexture(c->texture, NULL, p_pixels, &pitch);
 
    return NULL ; // Picture identifier, not needed here.
}
 
// VLC just rendered a video frame.
static void unlock(void *data, void *id, void * const *p_pixels) {
 
    struct context *c = (struct context *) data;
/*
 
    uint16_t *pixels = (uint16_t *) *p_pixels;
 
    // We can also render stuff.
    int x, y;
    for (y = 10; y < 40; y++) {
        for (x = 10; x < 40; x++) {
            if (x < 13 || y < 13 || x > 36 || y > 36) {
                pixels[y * VIDEOWIDTH + x] = 0xffff;
            } else {
                // RV16 = 5+6+5 pixels per color, BGR.
                pixels[y * VIDEOWIDTH + x] = 0x02ff;
            }
        }
    }
*/
 
    SDL_UnlockTexture(c->texture);
    SDL_UnlockMutex(c->mutex);
}
 
 
 
 
// VLC wants to display a video frame.
static void display(void *data, void *id) {
 
    struct context *ctx = (struct context *) data;
    ctx->displayrect.x = 0;
    ctx->displayrect.y = 0;
    ctx->displayrect.w = ctx->window_w;
    ctx->displayrect.h = ctx->window_h;
 
//    SDL_SetRenderDrawColor(c->renderer, 0, 80, 0, 255);
    SDL_RenderClear(ctx->renderer);
    SDL_RenderCopy(ctx->renderer, ctx->texture, NULL, &ctx->displayrect);
    SDL_RenderPresent(ctx->renderer);
}
 
static void quit(int c) {
    SDL_Quit();
    exit(c);
}
 
int main(int argc, char *argv[]) {
 
    struct context ctx;
 
    libvlc_instance_t *libvlc;
    libvlc_media_t *m;
    libvlc_media_player_t *mp;
    char const *vlc_argv[] = { "--no-audio", // Don't play audio.
            "--no-xlib", // Don't use Xlib.
 
            // Apply a video filter.
            //"--video-filter", "sepia",
            //"--sepia-intensity=200"
            };
    int vlc_argc = sizeof(vlc_argv) / sizeof(*vlc_argv);
 
    SDL_Event event;
    int done = 0, action = 0, pause = 0, n = 0;
 
//    if(argc < 2) {
//        printf("Usage: %s <filename>\n", argv[0]);
//        return EXIT_FAILURE;
//    }
 
    // Initialise libSDL.
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("Could not initialize SDL: %s.\n", SDL_GetError());
        return EXIT_FAILURE;
    }
 
    // Create SDL graphics objects.
    SDL_Window * window = SDL_CreateWindow("Fartplayer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 720,
            SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
    ctx.window_w = 1280;
    ctx.window_h = 720;
 
    if (!window) {
        fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
        quit(3);
    }
 
    int nRenderDrivers = SDL_GetNumRenderDrivers();
    int i = 0;
    for (; i < nRenderDrivers; i++) {
        SDL_RendererInfo info;
        SDL_GetRenderDriverInfo(i, &info);    //d3d
        printf("====info name %d: %s =====\n", i, info.name);
        printf("====max_texture_height %d  =====\n", i, info.max_texture_height);
        printf("====max_texture_width %d  =====\n", i, info.max_texture_width);
 
    }
 
    SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
    SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");
 
    ctx.renderer = SDL_CreateRenderer(window, 1, 0);
    if (!ctx.renderer) {
        fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
        quit(4);
    }
 
    ctx.texture = SDL_CreateTexture(ctx.renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, VIDEOWIDTH, VIDEOHEIGHT);
    if (!ctx.texture) {
        fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
        quit(5);
    }
 
    ctx.mutex = SDL_CreateMutex();
 
    // If you don't have this variable set you must have plugins directory
    // with the executable or libvlc_new() will not work!
    printf("VLC_PLUGIN_PATH=%s\n", getenv("VLC_PLUGIN_PATH"));
 
    // Initialise libVLC.
    libvlc = libvlc_new(vlc_argc, vlc_argv);
    if (NULL == libvlc) {
        printf("LibVLC initialization failure.\n");
        return EXIT_FAILURE;
    }
 
    m = libvlc_media_new_path(libvlc, "./test.mp4");
    mp = libvlc_media_player_new_from_media(m);
    libvlc_media_release(m);
 
    libvlc_video_set_callbacks(mp, lock, unlock, display, &ctx);
    libvlc_video_set_format(mp, "RV32", VIDEOWIDTH, VIDEOHEIGHT, VIDEOWIDTH * 4);
    libvlc_media_player_play(mp);
 
    // Main loop.
    while (!done ) {
 
        action = 0;
 
        // Keys: enter (fullscreen), space (pause), escape (quit).
        while (SDL_PollEvent(&event)) {
 
            switch (event.type) {
            case SDL_WINDOWEVENT:
                if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
                    SDL_RenderSetViewport(ctx.renderer, NULL );
                    ctx.displayrect.w = ctx.window_w = event.window.data1;
                    ctx.displayrect.h = ctx.window_h = event.window.data2;
                }
                break;
            case SDL_QUIT:
                done = 1;
                break;
            case SDL_KEYDOWN:
                action = event.key.keysym.sym;
                break;
            }
        }
 
        switch (action) {
        case SDLK_ESCAPE:
        case SDLK_q:
            done = 1;
            break;
        case ' ':
            printf("Pause toggle.\n");
            pause = !pause;
            break;
        }
        if (!pause) {
            ctx.n++;
        }
 
        SDL_Delay(100);
    }
 
    // Stop stream and clean up libVLC.
    libvlc_media_player_stop(mp);
    libvlc_media_player_release(mp);
    libvlc_release(libvlc);
 
    // Close window and clean up libSDL.
    SDL_DestroyMutex(ctx.mutex);
    SDL_DestroyRenderer(ctx.renderer);
 
    quit(0);
 
    return 0;
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C++中使用VLC 3.0播放视频,你需要先安装VLC 3.0库。下面是一些基本的步骤: 1. 下载和安装VLC 3.0库:你可以从官方网站下载VLC 3.0库,然后按照官方指南进行安装。 2. 创建一个新的C++项目:在你的集成开发环境(IDE)中创建一个新的C++项目。 3. 添加VLC 3.0库到你的项目中:将VLC 3.0库添加到你的C++项目中。这可以通过在你的项目设置中添加库的路径和名称来完成。 4. 在代码中导入必要的头文件:在你的代码中导入必要的VLC 3.0头文件,这些头文件包括libvlc.h和libvlc_media.h。 5. 初始化VLC 3.0库:在你的代码中编写初始化VLC 3.0库的代码,这将初始化VLC库并准备好播放视频。 6. 创建VLC 3.0媒体:在你的代码中创建一个VLC 3.0媒体,这将告诉VLC播放哪个视频文件。 7. 创建VLC 3.0播放器:在你的代码中创建一个VLC 3.0播放器,这将允许你控制视频的播放和暂停。 8. 播放视频:在你的代码中编写播放视频的代码。这可以通过调用VLC 3.0播放器的play()方法来完成。 下面是一个简单的C++代码示例,可以使用VLC 3.0播放视频: ```c++ #include <vlc/vlc.h> int main(int argc, char* argv[]) { libvlc_instance_t* inst; libvlc_media_player_t* mp; libvlc_media_t* m; // Initialize VLC library inst = libvlc_new(0, NULL); // Create a new media item m = libvlc_media_new_path(inst, "path/to/your/video.mp4"); // Create a new media player mp = libvlc_media_player_new_from_media(m); // Play the media libvlc_media_player_play(mp); // Wait for the media to finish playing while (libvlc_media_player_get_state(mp) != libvlc_Ended) { // Do nothing } // Free memory and clean up libvlc_media_player_release(mp); libvlc_media_release(m); libvlc_release(inst); return 0; } ``` 注意:以上代码仅为参考,具体实现可能需要根据你的项目需要进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值