Simple RTSP Player Based on FFMPEG + sdl2

Source: Internet
Author: User

References:

Compile reference: http://blog.chinaunix.net/uid-20718335-id-2980793.html

Code reference: http://blog.csdn.net/leixiaohua1020/article/details/8652605


Compile FFMPEG in windows and use a test program to play the local file and RTSP video stream based on the FFMPEG dynamic library.

Why is it so troublesome for a csdn blog to insert images? After the previous article has cut so many images, I uploaded them all, and I want to see the color-changing results in the Code and published them all in the program, really speechless...

1. Download and install mingw, http://sourceforge.net/projects/mingw/ download and install mingw installation manager, choose mingw-develop-Toolkit, mingw32-base, mingw32-gcc-g ++, mysys-base, and then apply changes

2. Go to the mingw installation directory. The default value is c: \ mingw. Edit c: \ mingw \ msys \ 1.0 \ msys. bat, add call "C: \ Program Files \ Microsoft Visual Studio 10.0 \ Vc \ bin \ vcvars32.bat" to the file and save it according to the specific vs path.

3. Download The FFMPEG http://www.ffmpeg.org/releases/ to choose the latest version, which is ffmpeg-2.2.4.tar.gz.

Download yasm http://yasm.tortall.net/Download.html (FFMPEG assembler compiler)

Download mp3lame http://sourceforge.net/projects/lame/files/ (MP3 supported)

Download x264 http://www.videolan.org/developers/x264.html (264 supported)

Download sdl2 http://www.libsdl.org/release/SDL2-devel-2.0.3-VC.zip (SDL multimedia development library for display, so download compiled directly)

Other options:

Xxid http://www.xvid.org/

Faad2 http://www.audiocoding.com/downloads.html

Faac http://www.audiocoding.com/downloads.html

Aacenc http://sourceforge.net/projects/opencore-amr/files/

Amr-Nb Amr-WB http://www.penguin.cz /~ Utx/AMR

Opencore-Amr http://sourceforge.net/projects/opencore-amr/files/

4. decompress yasm and put the EXE under c: \ windows \ system32.

Decompress FFMPEG and decompress the third-party packages to the FFMPEG folder.

Run mysys. bat

Install mp3lame

cd lame-3.99.2./configure --disable-sharedmakemake install


Install x264

cd x264-snapshot-20140703-2245./configure --enable-sharedmake make install

Install other third-party libraries.
Installed in c: \ mingw \ msys \ 1.0 \ Local by default

Install FFMPEG

cd ffmpeg-2.2.4./configure --enable-shared --disable-static --enable-ffplay --enable-nonfree --enable-memalign-hack --enable-libmp3lame --enable-gpl --enable-libx264 --enable-version3 --extra-cflags=-IC:/MinGW/msys/1.0/local/include --extra-ldflags=-LC:/MinGW/msys/1.0/local/libmakemake install
Make install error:
C: \ mingw \ bin \ strip.exe: Unable to rename 'C:/mingw/msys/1.0/local/bin/avcodec-55
. Dll '; reason: file exists

Later, I found that it would be nice to close 360.


OK. The annoying compilation process is over. Now you can write and test the demo happily.

========================================================== ========================================================== ========================================================== ============

1. Create a VC ++ Win32 console project in vs2010

2. Create the lib directory and include directory under the project directory, copy the compiled lib to the Lib, include to the include directory, and copy the DLL to the DEBUG directory, sdl2 decompress the compiled file and copy it to the corresponding directory.

3. Project properties-configuration properties-VC ++ directory-include directory, add the FFMPEG header file directory and other third-party header file directories

Linker -- General -- add the library directory and add the lib directory

Linker -- input -- add dependency and add each lib name

4. Add # define ffmpeg_version "2.2.4" to the new text document under include and save it as version. h.

5. Add ffmpeg-test.cpp

// Ffmpeg-test.cpp: defines the entry point for the console application. // # Include "stdafx. H "# ifdef _ cplusplusextern" C "{# endif # include <avcodec. h> # include <avdevice. h> # include <avformat. h> # include <avfilter. h> # include <avutil. h> # include <swscale. h> # include <SDL/SDL. h> # ifdef _ cplusplus} # endifint _ tmain (INT argc, _ tchar * argv []) {avformatcontext * pformatctx; Inti, videoindex; avcodeccontext * pcodecctx; avcodec * pcodec; char filepath [] = "nwn.mp4"; // char rtspurl [] = "rtsp: // 192. 168.11.19/live0.264 "; // char rtspurl [] =" rtsp: // 218.204.223.237: 554/live/1/0547424f573b085c/gsfp90ef4k0a6iap. SDP "; char rtspurl [] =" rtsp: // 211.139.194.small: 554/live/2/13e6330a31193128/5ild2inl5nq2s8r8. SDP "; av_register_all (); // register the component avformat_network_init (); // supports the network stream pformatctx = avformat_alloc_context (); // initialize avformatcontextif (avformat_open_input (& pformatctx, /* filepath */rtspurl, null, null )! = 0) {// open the file printf ("file cannot be opened \ n"); Return-1;} If (av_find_stream_info (pformatctx) <0) // find the stream information {printf ("couldn't find stream information. \ n "); Return-1 ;}videoindex =-1; for (I = 0; I <pformatctx-> nb_streams; I ++) // obtain the video stream idif (pformatctx-> streams [I]-> codec-> codec_type = avmedia_type_video) {videoindex = I; break;} If (videoindex =-1) {printf ("didn't find a video stream. \ n "); Return-1;} pcodecctx = pformatctx-> streams [videoindex ]-> Codec; pcodec = avcodec_find_decoder (pcodecctx-> codec_id); // find the decoder if (pcodec = NULL) {printf ("codec not found. \ n "); Return-1;} If (avcodec_open2 (pcodecctx, pcodec, null) <0) // open the decoder {printf (" cocould not open codec. \ n "); Return-1;} avframe * pframe, * pframeyuv; pframe = avcodec_alloc_frame (); // store the decoded avframepframeyuv = avcodec_alloc_frame (); // avframe after storage conversion (Why is conversion required? As explained later) uint8_t * out_buffer; out_buffer = new uint8_t [avpicture_get_size (pix_fmt_yuv420p, pcodecctx-> width, pcodecctx-> height)]; // allocate avpicture_fill (avpicture *) pframeyuv, out_buffer, pix_fmt_yuv420p, pcodecctx-> width, pcodecctx-> height) for avframe ); // fill avframe // ------------ SDL initialization -------- if (sdl_init (sdl_init_video | sdl_init_audio | sdl_init_timer) {printf ("cocould not initialize SDL-% s \ n", sdl_geterror (); Return-1;} sdl_window * screen = sdl_createwindow ("RTSP client Demo", sdl_windowpos_undefined, sdl_windowpos_undefined, pcodecctx-> width, pcodecctx-> height, sdl_window_resizable/* sdl_window_hidden */| sdl_window_opengl); If (! Screen) {printf ("SDL: cocould not set video mode-exiting \ n"); Return-1;} sdl_renderer * sdlrenderer = sdl_createrenderer (screen,-1, 0 ); sdl_texture * sdltexture = transform (sdlrenderer, expire, gradient, pcodecctx-> width, pcodecctx-> height); sdl_rect rect; // specify int ret, got_picture; static struct swscontext * img_convert_ctx; int y_siz E = pcodecctx-> width * pcodecctx-> height; sdl_event event; avpacket * packet = (avpacket *) malloc (sizeof (avpacket )); // store the data packet avpacketav_new_packet (packet, y_size) before decoding; // output the information ----------------------------- printf ("File Information encoding \ n"); av_dump_format (pformatctx, 0, filepath, 0); printf ("----------------------------------------------- \ n"); // ------------------------------ while (av_re Ad_frame (pformatctx, packet)> = 0) // obtain the compressed data packet avpacket {If (packet-> stream_index = videoindex) {ret = avcodec_decode_video2 (pcodecctx, pframe, & got_picture, packet); // decode. The input is avpacket, and the output is avframeif (Ret <0) {printf ("decoding error \ n"); Return-1 ;}if (got_picture) {// pixel format conversion. Pframe to pframeyuv. Img_convert_ctx = sws_getcontext (pcodecctx-> width, pcodecctx-> height, pcodecctx-> pix_fmt, pcodecctx-> width, pcodecctx-> height, distance, sws_bicubic, null ); sws_scale (img_convert_ctx, (const uint8_t * const *) pframe-> data, pframe-> linesize, 0, pcodecctx-> height, pframeyuv-> data, pframeyuv-> linesize ); sws_freecontext (img_convert_ctx); // ------------ SDL display -------- rect. X = 0; rect. y = 0; rect. W = pcodecctx-> width; rect. H = pcodecctx-> height; values (sdltexture, & rect, pframeyuv-> data [0], pframeyuv-> linesize [0]); sdl_renderclear (sdlrenderer); sdl_rendercopy (sdlrenderer, sdltexture, & rect, & rect); sdl_renderpresent (sdlrenderer); // latency (20); // ------------ SDL -----------} av_free_packet (packet); sdl_pollevent (& event ); switch (event. type) {Case sdl_quit: sdl_quit (); exit (0); break; default: Break ;}} sdl_destroytexture (sdltexture); Delete [] out_buffer; av_free (pframeyuv ); avcodec_close (pcodecctx); avformat_close_input (& pformatctx); Return 0 ;}
I have made some changes based on the information on the Internet, mainly including changes to the FFMPEG interface and sdl2 interface. For sdl2 modified interfaces, refer to http://wiki.libsdl.org/migrationguideand the docichapichangesfile in the source code directory.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.