Home > other >  Multithreaded SDL_DestroyWindow blocking problem
Multithreaded SDL_DestroyWindow blocking problem

Time:11-20

using multi-threading implementation player, using ffmpeg decoding SDL, showing off the air in destruction form SDL_DestroyWindow and
The following is a part of the code:

//SDL thread 
Int sfp_refresh_thread (void * opaque)
{
While (thread_exit==0)
{
SDL_Event event;
Event. The type=SFM_REFRESH_EVENT;
SDL_PushEvent (& amp; The event);
}
//the Quit
SDL_Event event;
Event. The type=SFM_BREAK_EVENT;
SDL_PushEvent (& amp; The event);
return 0;
}

//video playback function
Int simplest_ffmpeg_player (LPVOID lpParam)
{
Int I, videoindex;
AVCodecContext * pCodecCtx;
AVCodec * pCodec;
PFrame AVFrame * and * pFrameYUV;
Uint8_t * out_buffer;
AVPacket packet;
Int ret, got_picture;

//-- -- -- -- -- -- -- -- -- -- -- -- SDL -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Int screen_w screen_h;
SDL_Window * screen;
SDL_Renderer * sdlRenderer;
SDL_Texture * sdlTexture;
SDL_Rect sdlRect;
SDL_Thread * video_tid;
SDL_Event event;

Struct SwsContext * img_convert_ctx;
DLG CRTSPPlayerDlg *=(CRTSPPlayerDlg *) lpParam;

The static AVInputFormat * file_iformat;
AVFormatContext * IC=NULL;
* * opts AVDictionary;
AVDictionaryEntry * t;

Av_register_all ();
Avformat_network_init ();
AVCodec * pACodec=avcodec_find_decoder (AV_CODEC_ID_H264);
PCodecCtx=NULL;
PCodecCtx=avcodec_alloc_context3 (pACodec);
PCodecCtx - & gt; Codec_type=AVMEDIA_TYPE_VIDEO;
PCodecCtx - & gt; Pix_fmt=PIX_FMT_YUV420P;
PCodecCtx - & gt; Width=1920;
PCodecCtx - & gt; Height=1080;

If (avcodec_open2 (pCodecCtx pACodec, NULL) & lt; 0)
{
AfxMessageBox (" Could not open the codec. \ n ");
return -1;
}

PFrame=av_frame_alloc ();
PFrameYUV=av_frame_alloc ();
Out_buffer=(uint8_t *) av_malloc (avpicture_get_size (PIX_FMT_YUV420P pCodecCtx - & gt; Width, pCodecCtx - & gt; Height));
Avpicture_fill ((AVPicture *) pFrameYUV out_buffer, PIX_FMT_YUV420P, pCodecCtx - & gt; Width, pCodecCtx - & gt; Height);

Img_convert_ctx=sws_getContext (pCodecCtx - & gt; Width, pCodecCtx - & gt; Height, pCodecCtx - & gt; Pix_fmt,
PCodecCtx - & gt; Width, pCodecCtx - & gt; Height, PIX_FMT_YUV420P SWS_BICUBIC, NULL, NULL, NULL);


If (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER))
{
AfxMessageBox (" Could not initialize the SDL \ n ");
return -1;
}
//SDL 2.0 Support for multiple Windows
Screen_w=pCodecCtx - & gt; Width;
Screen_h=pCodecCtx - & gt; Height;

//===========================================
//display on the MFC controls
- screen=SDL_CreateWindowFrom (DLG & gt; GetDlgItem (IDC_SCREEN) - & gt; GetSafeHwnd ());
//===========================================
if(! Screen)
{
AfxMessageBox (" SDL: could not create Windows - the exiting \ n ");
return -1;
}
SdlRenderer=SDL_CreateRenderer (screen, 1, 0);
//IYUV: Y + U + V (3 planes)
//YV12: Y + V + U (3 planes)
SdlTexture=SDL_CreateTexture (sdlRenderer SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING, pCodecCtx - & gt; Width, pCodecCtx - & gt; Height);

SdlRect. X=0;
SdlRect. Y=0;
SdlRect. W=screen_w;
SdlRect. H=screen_h;

Video_tid=SDL_CreateThread (sfp_refresh_thread, NULL, NULL);
//-- -- -- -- -- -- -- -- -- -- -- -- SDL End -- -- -- -- -- -- -- -- -- -- -- --
//the Event Loop
Av_init_packet (& amp; Packet);
Char dateBuf [DUMMY_SINK_RECEIVE_BUFFER_SIZE]="";
For (;; )
{
//Wait
SDL_WaitEvent (& amp; The event);
If (event. Type==SFM_REFRESH_EVENT)
{
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Memset (dateBuf, 0, sizeof (dateBuf));
Int len=DLG - & gt; Read (dateBuf, 1);
If (len==0)
{
If (thread_exit==1)
{
break;
}
Sleep (20);
continue;
}
Packet data=https://bbs.csdn.net/topics/(uint8_t *) dateBuf;
Packet. Size=len;
Ret=avcodec_decode_video2 (pCodecCtx pFrame, & amp; Got_picture, & amp; Packet);
If (ret & lt; 0)
{
continue;
AfxMessageBox (" Decode Error. \ n ");
return -1;
}
If (got_picture)
{
Const sws_scale (img_convert_ctx, (const uint8_t * *) pFrame - & gt; Data, pFrame - & gt; Linesize, 0, pCodecCtx - & gt; Height, pFrameYUV - & gt; The data, pFrameYUV - & gt; Linesize);
SDL_UpdateTexture (sdlTexture, NULL, pFrameYUV - & gt; Data [0], pFrameYUV - & gt; Linesize [0]);
SDL_RenderClear (sdlRenderer);
SDL_RenderCopy (sdlRenderer sdlTexture, NULL, NULL);
SDL_RenderPresent (sdlRenderer);
}
Av_free_packet (& amp; Packet);
}
Else if (event type==SDL_QUIT)
{
Thread_exit=1;
}
Else if (event type==SFM_BREAK_EVENT)
{
break;
}

}

Sws_freeContext (img_convert_ctx);
SDL_DestroyWindow (screen);//this function will block the
SDL_DestroyRenderer (sdlRenderer);
SDL_DestroyTexture (sdlTexture);
SDL_Quit ();//this function will block the
DLG - & gt; GetDlgItem (IDC_SCREEN) - & gt; ShowWindow (SW_SHOWNORMAL);
Av_free (out_buffer);
Av_frame_free (& amp; PFrameYUV);
Av_frame_free (& amp; PFrame);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related