zoukankan      html  css  js  c++  java
  • vlc sdl2.0 播放示例

    #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>
    
    struct context {
    	SDL_Renderer *renderer;
    	SDL_Texture *texture;
    	SDL_mutex *mutex;
    	int n;
    
    	uint32_t *pixels;
    
    	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);
    
    	c->pixels = (uint16_t *) *p_pixels;
    
    	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;
    
    	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;
    
    	SDL_Rect displayrect;
    
    	uint32_t *pixels = ctx->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 * ctx->window_w + x] = 0xffffffff;
    			} else {
    				// RV16 = 5+6+5 pixels per color, BGR.
    				pixels[y * ctx->window_w + x] = 0x02ff02ff;
    			}
    		}
    	}
    
    	displayrect.x = 0;
    	displayrect.y = 0;
    	displayrect.w = ctx->window_w;
    	displayrect.h = ctx->window_h;
    //	printf("----%d, %d
    ", displayrect.w, displayrect.h);
    
    	SDL_SetRenderDrawColor(ctx->renderer, 0, 0, 0, 128);
    	SDL_RenderClear(ctx->renderer);
    	SDL_RenderCopy(ctx->renderer, ctx->texture, NULL, &displayrect);
    	SDL_RenderPresent(ctx->renderer);
    }
    
    static void quit(int c) {
    	SDL_Quit();
    	exit(c);
    }
    
    int main_video(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.
    			"-vvv",
    //			"--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;
    	unsigned video_w = 0, video_h = 0;
    
    //    if(argc < 2) {
    //        printf("Usage: %s <filename>
    ", argv[0]);
    //        return EXIT_FAILURE;
    //    }
    
    	// Initialise libSDL.
    	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    		printf("Could not initialize SDL: %s.
    ", SDL_GetError());
    		return EXIT_FAILURE;
    	}
    
    	int nRenderDrivers = SDL_GetNumRenderDrivers();
    	int i = 0;
    	for (; i < nRenderDrivers; i++) {
    		SDL_RendererInfo info;
    		SDL_GetRenderDriverInfo(i, &info);    //d3d
    		printf("====info name %d: %s =====
    ", i, info.name);
    		printf("====max_texture_height %d  =====
    ", i, info.max_texture_height);
    		printf("====max_texture_width %d  =====
    ", i, info.max_texture_width);
    
    	}
    
    	SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
    	SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");
    
    	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
    ", getenv("VLC_PLUGIN_PATH"));
    
    	// Initialise libVLC.
    	libvlc = libvlc_new(vlc_argc, vlc_argv);
    	if (NULL == libvlc) {
    		printf("LibVLC initialization failure.
    ");
    		return EXIT_FAILURE;
    	}
    
    	m = libvlc_media_new_path(libvlc, "./test5.mp4");
    	mp = libvlc_media_player_new_from_media(m);
    	libvlc_media_parse(m); //get file information
    	libvlc_video_get_size(mp, 0, &video_w, &video_h);
    	printf("----%d, %d
    ", video_w, video_h);
    	if (video_w == 0) {
    		printf("cannot get video size
    ");
    		exit(-1);
    	}
    	//init w,h
    	ctx.window_w = video_w;
    	ctx.window_h = video_h;
    
    	libvlc_media_release(m);
    
    	//initialize window and render
    	// Create SDL graphics objects.
    	SDL_Window * window = SDL_CreateWindow("Fartplayer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, video_w, video_h,
    			SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
    
    	if (!window) {
    		fprintf(stderr, "Couldn't create window: %s
    ", SDL_GetError());
    		quit(3);
    	}
    
    	ctx.renderer = SDL_CreateRenderer(window, 1, 0);
    	if (!ctx.renderer) {
    		fprintf(stderr, "Couldn't create renderer: %s
    ", SDL_GetError());
    		quit(4);
    	}
    
    	//init texture
    	ctx.texture = SDL_CreateTexture(ctx.renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, video_w, video_h);
    	if (!ctx.texture) {
    		fprintf(stderr, "Couldn't create texture: %s
    ", SDL_GetError());
    		quit(5);
    	}
    
    	libvlc_video_set_callbacks(mp, lock, unlock, display, &ctx);
    	libvlc_video_set_format(mp, "RV32", video_w, video_h, video_w * 4);
    //	libvlc_video_set_format(mp, "UYVY", 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.window_w = event.window.data1;
    					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.
    ");
    			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;
    }
    

      

  • 相关阅读:
    异常处理 Exception
    C#使用SQLite出错:无法加载 DLL“SQLite.Interop.dll”,找不到指定的模块
    NullableKey:解决Dictionary中键不能为null的问题 zt
    STSdb
    C# 如何获取某个类型或类型实例对象的大小
    CORREL
    C# 深复制
    mysql数据库创建函数过程
    mysql 数据库怎样快速的复制表以及表中的数据
    代码优化(一)
  • 原文地址:https://www.cnblogs.com/bigben0123/p/3510381.html
Copyright © 2011-2022 走看看