Chinaunix首页 | 论坛 | 博客
  • 博客访问: 407073
  • 博文数量: 48
  • 博客积分: 1820
  • 博客等级: 上尉
  • 技术积分: 705
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-28 23:10
文章分类

全部博文(48)

文章存档

2012年(1)

2011年(12)

2010年(34)

2009年(1)

分类: 嵌入式

2010-07-24 00:05:50

#include "SDL.h"   /* All SDL App's need this */
#include
#include
#include
using namespace std;
#include

#include "decoder.h"
#include "encoder.h"
#include "log.h"
#include "rtp_receive.h"
#include "singleton.h"

int base_port = 5000;
const string dest_host = "192.168.1.6";
const int dest_port = 6000;

SDL_Surface *screen;
SDL_Overlay *overlay;
SDL_Event event;
SDL_Rect rect;
const int WIDTH = 640;
const int HEIGHT = 480;
static int XDIM=640,YDIM=480;
int SHXDIM =640;//display size
int SHYDIM =480;

int paused=0;
int resized=0;
AVFrame   pict;
//AVPicture pict;
CDecoder aDecoder;
CEncoder aEncoder("target.mp4",WIDTH,HEIGHT);
CEncoder aRawEncoder("target.avi",WIDTH,HEIGHT);

int ret;
AVFrame* mainFrame;
AVFrame* rawFrame;
FILE* ftrRawDta;
void show_rtp_pict()
{
string rtp_data;
SDL_LockSurface(screen);
    SDL_LockYUVOverlay(overlay);
        
    pict.data[0] = overlay->pixels[0];
    pict.data[1] = overlay->pixels[2];
    pict.data[2] = overlay->pixels[1];

    pict.linesize[0] = overlay->pitches[0];
    pict.linesize[1] = overlay->pitches[2];
    pict.linesize[2] = overlay->pitches[1];
    //cout << "begin to read...\n";
    rtp_data = singleton::instance().pop_frame();
    fwrite(rtp_data.data(),rtp_data.size(),1,ftrRawDta);
    if(rtp_data.size()<6000)
{
     cout << "begin to read size="<
goto end;
}
    aDecoder.push_frame((void*)rtp_data.data(),rtp_data.size());
//cin >> cmd;
ret = aDecoder.try_show(&pict,640,480,mainFrame,rawFrame);
if(ret<0) goto end;
//ret = aDecoder.try_show(mainFrame,640,480);
//if(ret<0) goto end;
//cin >> cmd;
//if(num!=0)continue;
aEncoder.try_encode(mainFrame);
aRawEncoder.try_encode(rawFrame);
end:
SDL_UnlockYUVOverlay(overlay);
    SDL_UnlockSurface(screen);
    
    SDL_DisplayYUVOverlay(overlay, &rect);
}
int main(int argc, char *argv[]) {

if(argc>1)
base_port = atoi(argv[1]);
cout <<"RTP info:\n"
<<"\tRTP base port:"<
<<"\tRTP dest host:"<
<<"\tRTP dest port:"<
<<"\n";
cout <<"SDL info:\n"
<<"\tSDL window width:640\n"
<<"\tSDL windor height:480\n"
<<"\n";
cout <<"FFMPEG info:\n"
<<"\toutput filename:target.mp4\n"
<<"\tencoder:H264\n"
<<"\n";
cout <<"main begin base port="<
singleton::instance().start(base_port,dest_host,dest_port);
mainFrame = aDecoder.do_alloc_picture(PIX_FMT_YUV420P,WIDTH,HEIGHT);
rawFrame = aDecoder.do_alloc_picture(PIX_FMT_YUV420P,WIDTH,HEIGHT);
const char* rawdata_filename = "rawdata.mpeg";
ftrRawDta = fopen(rawdata_filename,"wb");
if(ftrRawDta == NULL)
{
cout <<"main con't open="<
exit(1);
}
sleep(1);
    printf("Initializing SDL.\n");
    
    /* Initialize defaults, Video and Audio */
    if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO| SDL_INIT_TIMER)==-1)) { 
        printf("Could not initialize SDL: %s.\n", SDL_GetError());
        exit(-1);
    }
    atexit (SDL_Quit);

    printf("SDL initialized.\n");
    
    screen = SDL_SetVideoMode (WIDTH, HEIGHT, 0, SDL_HWSURFACE
                                              | SDL_DOUBLEBUF
                                              | SDL_ANYFORMAT
                                              | SDL_RESIZABLE);
    if (screen == NULL)
    {
        fprintf(stderr, "SDL_SetVideoMode ==>> %s\n", SDL_GetError());
        goto fatal;
    }
    if (0 == (screen->flags & SDL_HWSURFACE))
    {
       fprintf(stderr,"Can't get hardware surface\n");
    }
    SDL_WM_SetCaption ("USB Camera By Breeze", NULL);      
    overlay = SDL_CreateYUVOverlay(XDIM, YDIM, SDL_YV12_OVERLAY, screen);
    if (!overlay)
     {
            fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError());
            exit(4);
     }   
    printf("SDL_CreateYUVOverlay info: %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes,
               overlay->hw_overlay?"hardware":"software",
               overlay->format==SDL_YV12_OVERLAY?"YV12":
               overlay->format==SDL_IYUV_OVERLAY?"IYUV":
               overlay->format==SDL_YUY2_OVERLAY?"YUY2":
               overlay->format==SDL_UYVY_OVERLAY?"UYVY":
               overlay->format==SDL_YVYU_OVERLAY?"YVYU":
               "Unknown");
    rect.x=0;
    rect.y=0;
    rect.w=SHXDIM;
    rect.h=SHYDIM;
    

    do{
   while (SDL_PollEvent(&event))
   {
    switch (event.type)
    {
    case SDL_VIDEORESIZE:
    {
             screen=SDL_SetVideoMode(event.resize.w, event.resize.h, 0, SDL_RESIZABLE | SDL_SWSURFACE);
             rect.w=event.resize.w;
             rect.h=event.resize.h;
             if (paused)
              {
                 resized=1;
              }   
    }break;
    case SDL_QUIT:
    {
    aEncoder.finish_coder();
   
    fclose(ftrRawDta);
    goto release_all;
   
    }break;
    }//! end switch 
   
   }//! end while (SDL_PollEvent(&event))
   show_rtp_pict();
   SDL_Delay(10);//! delay 1 ms
   //!printf("Wvent Poll.\n");
    }while(1);
    //cin >>cmd;
    printf("Quiting SDL.\n");

release_all:
    printf("Quiting....\n");

    exit(0);
fatal:
printf("fatal!!,exit\n");
return -1;;
}

阅读(2093) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~