#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
//constant
#define RECV_PORT 3020
//#define Debug OPEN
//#define DebugSchedule_refresh OPEN
//#define DebugMainTimer OPEN
//#define video_display_show OPEN
#ifdef video_display_show
int i=0;
int FF_REFRESH_EVENT_TIMES=0;
#endif
//#define packet_queue_get_v OPEN
#ifdef packet_queue_get_v
int j=0;
#endif
#define packet_queue_put_v OPEN
#ifdef packet_queue_put_v
int put_j=0;
#endif
#define socket_thread OPEN
#ifdef socket_thread
int socket_thread_times=0;
#endif
#define VIDEO_PICTURE_QUEUE_SIZE 100
#define MAX_VIDEOQ_SIZE 100
#define FF_QUIT_EVENT SDL_USEREVENT + 1
#define FF_REFRESH_EVENT SDL_USEREVENT + 2
#define FF_ALLOC_EVENT SDL_USEREVENT + 3
typedef struct PacketQueue {
AVPacketList *first_pkt, *last_pkt;
int nb_packets;
int size;
SDL_mutex *mutex;
SDL_cond *cond;
} PacketQueue;
typedef struct VideoPicture {
SDL_Overlay *bmp;
int width, height; /* source height & width */
int allocated;
// double pts;
} VideoPicture;
typedef struct VideoState {
AVCodecContext *dec; //保存了相应流的详细编码信息,比如视频的宽、高,编码类型等。
AVCodec *codec;
AVFormatContext *pFormatCtx; //保存需要读入的文件的格式信息,比如流的个数以及流数据等
int videoStream;
PacketQueue videoq; //视频队列
VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
int pictq_size, pictq_rindex, pictq_windex;
SDL_mutex *pictq_mutex;
SDL_cond *pictq_cond;
SDL_Thread *parse_tid;
SDL_Thread *video_tid;
int quit;
double video_clock;
double audio_clock;
//double frame_last_pts;
double frame_last_delay;
double frame_timer;
} VideoState;
struct sockaddr_in from;
struct sockaddr_in sin2;
int quit = 0;
SDL_Surface *screen = NULL;
//uint64_t global_video_pkt_pts = AV_NOPTS_VALUE;
VideoState *global_video_state;
int queue_picture(VideoState *is, AVFrame *pFrame) {
#ifdef Debug
printf("\ncall queue_picture 1===================\n");
#endif
VideoPicture *vp;
int dst_pix_fmt;
AVPicture pict;
SDL_LockMutex(is->pictq_mutex);
#ifdef Debug
printf("\nis->pictq_size=%d\tis->quit=%d==================\n",is->pictq_size,is->quit);
#endif
while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE && !is->quit) {
#ifdef Debug
printf("\ncall SDL_CondWait(is->pictq_cond, is->pictq_mutex); 3.1===================\n");
#endif
SDL_CondWait(is->pictq_cond, is->pictq_mutex);//there wrong
#ifdef Debug
printf("\ncall SDL_CondWait(is->pictq_cond, is->pictq_mutex); 3.2===================\n");
#endif
}
#ifdef Debug
printf("\ncall queue_picture 4===================\n");
#endif
SDL_UnlockMutex(is->pictq_mutex);
#ifdef Debug
printf("\ncall queue_picture 5===================\n");
#endif
if (is->quit){
#ifdef Debug
printf("\ncall queue_picture 6 is->quit<0===================\n");
#endif
return -1;
}
#ifdef Debug
printf("\ncall queue_picture 7===================\n");
#endif
// windex is set to 0 initially
vp = &is->pictq[is->pictq_windex];
#ifdef Debug
printf("\ncall queue_picture 8===================\n");
#endif
if (!vp->bmp || vp->width != is->dec->width || vp->height
!= is->dec->height) {
SDL_Event event;
vp->allocated = 0;
event.type = FF_ALLOC_EVENT;
event.user.data1 = is;
SDL_PushEvent(&event);
SDL_LockMutex(is->pictq_mutex);
while (!vp->allocated && !is->quit) {
#ifdef Debug
printf("\ncall queue_picture SDL_CondWait 8.5===================\n");
#endif
SDL_CondWait(is->pictq_cond, is->pictq_mutex);
#ifdef Debug
printf("\ncall queue_picture SDL_CondWait 8.6===================\n");
#endif
}
SDL_UnlockMutex(is->pictq_mutex);
if (is->quit) {
#ifdef Debug
printf("\ncall queue_picture 9 is->quit<0===================\n");
#endif
return -1;
}
#ifdef Debug
printf("\ncall queue_picture 10===================\n");
#endif
}
if(vp->bmp) {
static struct SwsContext *img_convert_ctx = NULL;
SDL_LockYUVOverlay(vp->bmp);
dst_pix_fmt = PIX_FMT_YUV420P;
pict.data[0] = vp->bmp->pixels[0];
pict.data[1] = vp->bmp->pixels[2];
pict.data[2] = vp->bmp->pixels[1];
pict.linesize[0] = vp->bmp->pitches[0];
pict.linesize[1] = vp->bmp->pitches[2];
pict.linesize[2] = vp->bmp->pitches[1];
if (img_convert_ctx == NULL) {
img_convert_ctx = sws_getContext(is->dec->width,
is->dec->height, is->dec->pix_fmt,
is->dec->width, is->dec->height,
dst_pix_fmt, SWS_BICUBIC, NULL, NULL, NULL);
}
sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize,
0, is->dec->height, pict.data, pict.linesize);
SDL_UnlockYUVOverlay(vp->bmp);
//vp->pts = pts;
if(++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE) {
is->pictq_windex = 0;
}
SDL_LockMutex(is->pictq_mutex);
is->pictq_size++;
SDL_UnlockMutex(is->pictq_mutex);
}
return 0;
}
int our_get_buffer(struct AVCodecContext *c, AVFrame *pic) {
int ret = avcodec_default_get_buffer(c, pic);
/* uint64_t *pts = av_malloc(sizeof(uint64_t));
*pts = global_video_pkt_pts;
pic->opaque = pts;
*/
return ret;
}
void our_release_buffer(struct AVCodecContext *c, AVFrame *pic) {
if(pic) av_freep(&pic->opaque);
avcodec_default_release_buffer(c, pic);
}
void video_display(VideoState *is) {
#ifdef video_display_show
printf("\nvideo_display=%d\n",i++);
#endif
SDL_Rect rect;
VideoPicture *vp;
AVPicture pict;
float aspect_ratio;
int w, h, x, y;
int i;
vp = &is->pictq[is->pictq_rindex];
if (vp->bmp) {
if (is->dec->sample_aspect_ratio.num == 0) {
aspect_ratio = 0;
} else {
aspect_ratio = av_q2d(is->dec->sample_aspect_ratio)
* is->dec->width / is->dec->height;
}
if (aspect_ratio <= 0.0) {
aspect_ratio = (float) is->dec->width
/ (float) is->dec->height;
}
h = screen->h;
w = ((int) rint(h * aspect_ratio)) & -3;
if (w > screen->w) {
w = screen->w;
h = ((int) rint(w / aspect_ratio)) & -3;
}
x = (screen->w - w) / 2;
y = (screen->h - h) / 2;
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
SDL_DisplayYUVOverlay(vp->bmp, &rect);
}
}
static Uint32 sdl_refresh_timer_cb(Uint32 interval, void *opaque) {
SDL_Event event;
event.type = FF_REFRESH_EVENT;
event.user.data1 = opaque;
SDL_PushEvent(&event);
return 0; /* 0 means stop timer */
}
/* schedule a video refresh in 'delay' ms */
static void schedule_refresh(VideoState *is, int delay) {
#ifdef DebugMainTimer
printf("\nDebugMainTimer call===%d\n",(double)av_gettime());
#endif
SDL_AddTimer(delay, sdl_refresh_timer_cb, is); //定时器
}
void video_refresh_timer(void *userdata) {
VideoState *is = (VideoState *) userdata;
VideoPicture *vp;
if (is->dec) {
if (is->pictq_size == 0) {
/*#ifdef Debug
printf("\nis->pictq_size == 0\n");
#endif
*/
#ifdef video_display_show
printf("\nis->pictq_size == 0 FF_REFRESH_EVENT_TIMES=%d\n",FF_REFRESH_EVENT_TIMES++);
#endif
schedule_refresh(is, 1);
} else {
#ifdef video_display_show
printf("\nFF_REFRESH_EVENT_TIMES=%d\n",FF_REFRESH_EVENT_TIMES++);
#endif
#ifdef DebugSchedule_refresh
printf("\nis->pictq_size != 0-------------------------------->>\n");
#endif
vp = &is->pictq[is->pictq_rindex];
//schedule_refresh(is,80);
schedule_refresh(is,80);
video_display(is);
/* update queue for next picture! */
#ifdef Debug
printf("\nis->pictq_rindex=%d------------>\n",is->pictq_rindex);
#endif
if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE) {
is->pictq_rindex = 0;
}
SDL_LockMutex(is->pictq_mutex);
is->pictq_size--;
SDL_CondSignal(is->pictq_cond);
SDL_UnlockMutex(is->pictq_mutex);
}
} else {
schedule_refresh(is, 100);
// schedule_refresh(is, 1000);
}
}
/*
double synchronize_video(VideoState *is, AVFrame *src_frame, double pts) {
double frame_delay;
if(pts != 0) {
is->video_clock = pts;
} else {
pts = is->video_clock;
}
frame_delay = av_q2d(is->dec->time_base);
frame_delay += src_frame->repeat_pict * (frame_delay * 0.5); //why multiply 0.5?
is->video_clock += frame_delay;
return pts;
}
*/
void alloc_picture(void *userdata) {
#ifdef Debug
printf("\ncall alloc_picture=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
#endif
VideoState *is = (VideoState *) userdata;
VideoPicture *vp;
if (!screen) {
screen = SDL_SetVideoMode(is->dec->width, is->dec->height, 0, 0);
SDL_WM_SetCaption("Video System",NULL);
if (!screen) {
fprintf(stderr, "SDL: could not set video mode - exiting\n");
exit(1);
}
}
vp = &is->pictq[is->pictq_windex];
if (vp->bmp) {
// we already have one make another, bigger/smaller
SDL_FreeYUVOverlay(vp->bmp);
}
// Allocate a place to put our YUV image on that screen
vp->bmp = SDL_CreateYUVOverlay(is->dec->width,
is->dec->height, SDL_YV12_OVERLAY, screen);
vp->width = is->dec->width;
vp->height = is->dec->height;
SDL_LockMutex(is->pictq_mutex);
vp->allocated = 1;
SDL_CondSignal(is->pictq_cond);
SDL_UnlockMutex(is->pictq_mutex);
}
static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block) {
#ifdef Debug
printf("\npacket_queue_get and queue nb_packets=%d\n",q->nb_packets);
#endif
AVPacketList *pkt1;
int ret;
SDL_LockMutex(q->mutex);
for (;;) {
if (quit) {
ret = -1;
break;
}
pkt1 = q->first_pkt;
if (pkt1) {
q->first_pkt = pkt1->next;
if (!q->first_pkt)
q->last_pkt = NULL;
q->nb_packets--;
q->size -= pkt1->pkt.size;
*pkt = pkt1->pkt;
av_free(pkt1);
ret = 1;
break;
} else if (!block) {
ret = 0;
break;
} else {
#ifdef Debug
printf("\npacket_queue_get SDL_CondWait");
#endif
SDL_CondWait(q->cond, q->mutex);
}
}
SDL_UnlockMutex(q->mutex);
return ret;
}
int packet_queue_put(PacketQueue *q, AVPacket *pkt) {
#ifdef Debug
printf("\npacket_queue_put and queue nb_packets=%d\n",q->nb_packets);
#endif
AVPacketList *pkt1;
if (av_dup_packet(pkt) < 0) {
return -1;
}
pkt1 = av_malloc(sizeof(AVPacketList));
if (!pkt1)
return -1;
pkt1->pkt = *pkt;
pkt1->next = NULL;
SDL_LockMutex(q->mutex);
if (!q->last_pkt)
q->first_pkt = pkt1;
else
q->last_pkt->next = pkt1;
q->last_pkt = pkt1;
q->nb_packets++;
q->size += pkt1->pkt.size;
SDL_CondSignal(q->cond);
SDL_UnlockMutex(q->mutex);
return 0;
}
void packet_queue_init(PacketQueue *q) {
memset(q, 0, sizeof(PacketQueue));
q->mutex = SDL_CreateMutex();
q->cond = SDL_CreateCond();
#ifdef Debug
printf("\npacket_queue_init\n");
#endif
}
int video_thread(void *data){
#ifdef Debug
printf("\n----------------- video_thread---------------------\n");
#endif
VideoState *is = (VideoState*) data;
// double pts = 0;
int frameFinished;
int ret;
AVPacket getFrmQuePkt;
AVFrame *frame = avcodec_alloc_frame(); ////分配一个帧指针,指向解码后的原始帧
for (;;) {
#ifdef Debug
printf("\nfor(;;) packet_queue_get\n");
#endif
#ifdef packet_queue_get_v
printf("\npacket_queue_get_v=%d\n",j++);
#endif
if(packet_queue_get(&is->videoq,&getFrmQuePkt,1) < 0){ //取出一帧视频数据包
#ifdef Debug
printf("\n------>packet_queue_get < 0\n");
#endif
break;
}
// pts = 0;
// global_video_pkt_pts = getFrmQuePkt.pts;
// 解压
ret = avcodec_decode_video2(is->dec, frame, &frameFinished, &getFrmQuePkt); ////解码该帧
#ifdef Debug
printf("\ncall avcodec_decode_video2 6 and ret=%d------------------>\n",ret);
#endif
//add for show
/*
if (getFrmQuePkt.dts == AV_NOPTS_VALUE && frame->opaque
&& *(uint64_t*) frame->opaque != AV_NOPTS_VALUE) {
pts = *(uint64_t*) frame->opaque;
} else if (getFrmQuePkt.dts != AV_NOPTS_VALUE) {
pts = getFrmQuePkt.dts;
} else {
pts = 0;
}
pts *= av_q2d(is->dec->time_base);
*/
if (frameFinished) {
//show now
#ifdef Debug
printf("\ndecode ret=%d\n",ret);
printf("\ngo show now\n");
printf("\dec->width=%d\n",is->dec->width);
#endif
// pts = synchronize_video(is, frame, pts);
#ifdef Debug
printf("\ncall avcodec_decode_video2 14------------------>\n");
#endif
if (queue_picture(is, frame) < 0) { //将解码后的帧和数据插入图像队列
#ifdef Debug
printf("\ncall avcodec_decode_video2 15 something error in there------------------>\n");
#endif
#ifdef Debug
printf("\nqueue_picture(is, frame, pts) < 0 then break\n");
#endif
//break;
}
#ifdef Debug
printf("\ncall avcodec_decode_video2 16------------------>\n");
#endif
}
#ifdef Debug
printf("\ncall avcodec_decode_video2 17------------------>\n");
#endif
//av_free_packet(&getFrmQuePkt);
//add for show end
}
#ifdef Debug
printf("\ncall avcodec_decode_video2 18------------------>\n");
#endif
// wrong here
av_free(frame);
#ifdef Debug
printf("\ncall avcodec_decode_video2 19------------------>\n");
#endif
return 0;
}
int socket_thread(void *data){
VideoState *is = (VideoState*) data;
//socket ready start
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1) {
fprintf(stderr, "ERR: create sock err\n");
exit(-1);
}
sin2.sin_family = AF_INET;
sin2.sin_port = htons(RECV_PORT);
sin2.sin_addr.s_addr = inet_addr("192.168.1.178");
if (bind(sock, (struct sockaddr*)&sin2, sizeof(sin2)) < 0) {
fprintf(stderr, "ERR: bind %d\n", RECV_PORT);
exit(-1);
}
unsigned char *buf = (unsigned char*)alloca(65536);
if (!buf) {
fprintf(stderr, "ERR: alloca 65536 err\n");
exit(-1);
}
//socket ready end
//void *shower = 0; // 成功解码第一帧后, 才知道大小
for (; ; ) {
printf("\nsocket_thread-----%d \n",socket_thread_times++);
if (is->quit) {
break;
}
// seek stuff goes here
if (is->videoq.size > MAX_VIDEOQ_SIZE) {
#ifdef Debug
printf("\nSDL_Delay(10);====-----====----=-======-----\n");
#endif
SDL_Delay(100);
continue;
}
#ifdef Debug
printf("\nSDL_Delay(after);====-----====----=-======-----\n");
#endif
//printf("\nlistening\n");
socklen_t fromlen = sizeof(from);
int rc = recvfrom(sock, buf, 65536, 0, (struct sockaddr*)&from, &fromlen);
if (rc > 0) {
//printf("\nrecv data \n");
AVPacket recvPkt;
recvPkt.data = buf;
recvPkt.size = rc;
#ifdef packet_queue_put_v
//printf("\nrecv data \n");
//printf("\npacket_queue_put_v=%d\n",put_j++);
#endif
packet_queue_put(&is->videoq,&recvPkt);
}else{
#ifdef Debug
printf("\nerror socket\n");
#endif
}
}
//recv data end
while (!is->quit) {
SDL_Delay(100);
}
fail: if (1) {
SDL_Event event;
event.type = FF_QUIT_EVENT;
event.user.data1 = is;
SDL_PushEvent(&event);
}
close(sock);
return 0;
}
int decode_thread(void *data) {
#ifdef Debug
printf("\n----------------decode_thread-------------------\n");
#endif
sleep(1);
VideoState *is = (VideoState*) data;
//decodec ready start
avformat_network_init(); //需要播放网络视频
avcodec_register_all(); //初始化ffmpeg库,如果系统里面的ffmpeg
is->codec = avcodec_find_decoder(CODEC_ID_H264); //在库里面查找支持该CODEC_ID_H264格式的解码器
is->dec = avcodec_alloc_context(); //创建一个AVCodecContext,并用默认值初始化
if (avcodec_open(is->dec, is->codec) < 0) { //打开解码器 AVCodecContext
fprintf(stderr, "ERR: open H264 decoder err\n");
exit(-1);
}
//decodec ready end
//init packet_queue_init
packet_queue_init(&is->videoq); //初始化视频队列
is->video_tid = SDL_CreateThread(video_thread, is); //创建视频子线程
//is->frame_timer = (double)av_gettime() / 1000000.0;
//is->frame_last_delay = 40e-3;
is->dec->get_buffer = our_get_buffer;//新增
is->dec->release_buffer = our_release_buffer;
return 0;
}
int decode_interrupt_cb(void) {
return (global_video_state && global_video_state->quit);
}
void main(){
SDL_Event event; //SDL_Event 结构体是SDL事件句柄的核心. SDL_Event 是一个SDL所有事件类型结构体的联合体.
VideoState *is;
is = av_mallocz(sizeof(VideoState));
is->pictq_mutex = SDL_CreateMutex();
is->pictq_cond = SDL_CreateCond();
//DL由八个子系统组成——音频、CDROM、事件处理、文件I/O、游戏杆、线程、记时器和视频。
//使用前必须调用SDL_Init或SDL_InitSubSystem初始化。SDL_Init必须早于其他所有SDL调用,它
//将自动初始化事件处理、文件I/O和线程子系统,并根据参数选择启动其他子系统。
//sdl ready start
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
exit(1);
}
//sdl ready end
schedule_refresh(is, 1);
SDL_CreateThread(socket_thread,is);
// sleep(1);
//todo start other thread for decode
is->parse_tid = SDL_CreateThread(decode_thread, is); //创建解码线程
if (!is->parse_tid) {
#ifdef Debug
printf("\ncreate decode_thread error\n");
#endif
av_free(is);
return -1;
}
#ifdef Debug
printf("\n--------------------------Main Thread--------------\n");
#endif
for (;;) {
SDL_WaitEvent(&event);
switch (event.type) {
case FF_ALLOC_EVENT:
alloc_picture(event.user.data1);
break;
case FF_REFRESH_EVENT:
video_refresh_timer(event.user.data1);
break;
case FF_QUIT_EVENT:
break;
case SDL_QUIT:
SDL_Quit();
exit(0);
break;
}
}
}