Example #1
0
int main(int argc, char *argv[]) {

    SDL_Event       event;    
    VideoState      *is;

    is = av_mallocz(sizeof(VideoState));

    if(argc < 2) {
        fprintf(stderr, "Usage: %s <file>\n", argv[0]);
        exit(1);
    }

    // Register all formats and codecs
    av_register_all();

    if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
        fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
        exit(1);
    }

    // Make a screen to put our video
#ifndef __DARWIN__
    screen = SDL_SetVideoMode(640, 480, 0, 0);
#else
    screen = SDL_SetVideoMode(640, 480, 24, 0);
#endif
    if(!screen) {
        fprintf(stderr, "SDL: could not set video mode - exiting\n");
        exit(1);
    }

    av_strlcpy(is->filename, argv[1], 1024);

    is->pictq_mutex = SDL_CreateMutex();
    is->pictq_cond  = SDL_CreateCond();

    schedule_refresh(is, 40);

    is->av_sync_type = DEFAULT_AV_SYNC_TYPE;
    is->parse_tid    = SDL_CreateThread(decode_thread, is);
    if(!is->parse_tid) {
        av_free(is);
        return -1;
    }

    for(;;) {
        SDL_WaitEvent(&event);
        switch(event.type) {
        case FF_QUIT_EVENT:
        case SDL_QUIT:
            is->quit = 1;
            /*
             * If the video has finished playing, then both the picture and
             * audio queues are waiting for more data.  Make them stop
             * waiting and terminate normally.
             */
            SDL_CondSignal(is->audioq.cond);
            SDL_CondSignal(is->videoq.cond);
            SDL_Quit();
            exit(0);
            break;

        case FF_ALLOC_EVENT:
            alloc_picture(event.user.data1);
            break;

        case FF_REFRESH_EVENT:
            video_refresh_timer(event.user.data1);
            break;

        default:
            break;
        }
    }
    return 0;

}
int main(int argc, char *argv[]) {

        SDL_Event event;
        //double pts;
        VideoState *is;

        is = av_mallocz(sizeof(VideoState));

        if (argc < 2) {
                fprintf(stderr, "Usage: test <file>\n");
                exit(1);
        }
        // Register all formats and codecs
        av_register_all();

        if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
                fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
                exit(1);
        }

        // Make a screen to put our video
#ifndef __DARWIN__
        screen = SDL_SetVideoMode(640, 480, 0, 0);
#else
        screen = SDL_SetVideoMode(640, 480, 24, 0);
#endif
        if (!screen) {
                fprintf(stderr, "SDL: could not set video mode - exiting\n");
                exit(1);
        }

        av_strlcpy(is->filename, argv[1], sizeof(is->filename));

        is->pictq_mutex = SDL_CreateMutex();
        is->pictq_cond = SDL_CreateCond();

        schedule_refresh(is, 40);

        is->av_sync_type = DEFAULT_AV_SYNC_TYPE;

        is->parse_tid = SDL_CreateThread(decode_thread, is);
        if (!is->parse_tid) {
                av_free(is);
                return -1;
        }
        av_init_packet(&flush_pkt);
        flush_pkt.data = (unsigned char *) "FLUSH";
        for (;;) {
                double incr, pos;
                SDL_WaitEvent(&event);
                switch (event.type) {
                case SDL_KEYDOWN:
                        switch (event.key.keysym.sym) {
                        case SDLK_LEFT:
                                incr = -10.0;
                                goto do_seek;
                        case SDLK_RIGHT:
                                incr = 10.0;
                                goto do_seek;
                        case SDLK_UP:
                                incr = 60.0;
                                goto do_seek;
                        case SDLK_DOWN:
                                incr = -60.0;
                                goto do_seek;
                                do_seek: if (global_video_state) {
                                        pos = get_master_clock(global_video_state);
                                        pos += incr;
                                        stream_seek(global_video_state,
                                                        (int64_t) (pos * AV_TIME_BASE), incr);
                                }
                                break;
                        default:
                                break;
                        }
                        break;
                case FF_QUIT_EVENT:
                case SDL_QUIT:
                        SDL_CondSignal(is->audioq.cond);
                        SDL_CondSignal(is->videoq.cond);
                        is->quit = 1;
                        SDL_Quit();
                        exit(0);
                        break;
                case FF_ALLOC_EVENT:
                        alloc_picture(event.user.data1);
                        break;
                case FF_REFRESH_EVENT:
                        video_refresh_timer(event.user.data1);
                        break;
                default:
                        break;
                }
        }
        return 0;

}
Example #3
0
int main(int argc, char *argv[]) {
  
	int i;
	choice=VID1;
	channel_req = 0;
	SDL_Event event;

	if (argc != 3) {
		fprintf(stderr, "Usage: test <file>:program needs 2 video files\n");
		exit(1);
	}
	// Register all formats and codecs
	av_register_all();

	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
		fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
		exit(1);
	}

	// Make a screen to put our video
#ifndef __DARWIN__
	screen = SDL_SetVideoMode(640, 480, 0, 0);
#else
	screen = SDL_SetVideoMode(640, 480, 24, 0);
#endif
	if (!screen) {
		fprintf(stderr, "SDL: could not set video mode - exiting\n");
		exit(1);
	}
	/* create 4 video states, for 4 channels: 1)vid1.2) vid2. 3) small vid. 4) small vid inside vid */
	for (i = 3 ; i >=0  ; --i) {
		create_videostates(argv[i%2+1],i);
		sleep(1);
	}
	channel_req = 0;
	for (;;) {

		SDL_WaitEvent(&event);
		switch (event.type) {
		case SDL_KEYDOWN:
			switch (event.key.keysym.sym) {
		
			

			case SDLK_1:	// video and audio 1
				channel_req = 0; 
				audio_req = 0;
				choice=VID1;
				break;
			case SDLK_2:	// video and audio 2
				channel_req = 1;				
				audio_req = 1;
				choice=VID2;
				break;
			case SDLK_3:	// video 2 in video 1
				channel_req = 2;
				choice=VID2_IN_VID1;
				break;
			case SDLK_4:	//audio 1
				audio_req = 0;
				break;
			case SDLK_5:	//audio 2
				audio_req = 1;
				break;
			case SDLK_b:	//paint blue
				channels[channel_req]->color_choice = BLUE;
				break;
			case SDLK_r:	//paint red
				channels[channel_req]->color_choice = RED;
				break;
			case SDLK_g:	//paint green
				channels[channel_req]->color_choice = GREEN;
				break;
			case SDLK_w:	//paint grayscale
				channels[channel_req]->color_choice = BLACK_N_WHITE;
				break;
			case SDLK_x:	//cancel paints
				channels[channel_req]->color_choice = NONE;
				break;
			default:
				break;
			}
			break;
		case FF_QUIT_EVENT:
		case SDL_QUIT:
			for (i = 0 ; i < 4 ; i++ ) {
				SDL_CondSignal(channels[i]->audioq.cond);
				SDL_CondSignal(channels[i]->videoq.cond);
				channels[i]->quit = 1;
			}
			SDL_Quit();
			exit(0);
			break;
		case FF_ALLOC_EVENT:
			alloc_picture(event.user.data1);
			break;
		case FF_REFRESH_EVENT:
			video_refresh_timer(event.user.data1);
			break;
		default:
			break;
		}
	}
	return 0;

}
Example #4
0
int main(int argc, char *argv[]) {

  SDL_Event       event;

  VideoState      *is;

  is = (VideoState*)av_mallocz(sizeof(VideoState));

  if(argc < 2) {
    fprintf(stderr, "Usage: test <file>\n");
    exit(1);
  }
  // Register all formats and codecs
  av_register_all();
  
  if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
    fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
    exit(1);
  }

  // Make a screen to put our video
#ifndef __DARWIN__
        screen = SDL_SetVideoMode(640, 480, 0, 0);
#else
        screen = SDL_SetVideoMode(640, 480, 24, 0);
#endif
  if(!screen) {
    fprintf(stderr, "SDL: could not set video mode - exiting\n");
    exit(1);
  }

  //pstrcpy(is->filename, sizeof(is->filename), argv[1]);
  strcpy(is->filename, argv[1]);

  is->pictq_mutex = SDL_CreateMutex();
  is->pictq_cond = SDL_CreateCond();

  schedule_refresh(is, 40);

  is->parse_tid = SDL_CreateThread(decode_thread, is);
  if(!is->parse_tid) {
    av_free(is);
    return -1;
  }
  for(;;) {

    SDL_WaitEvent(&event);
    switch(event.type) {
    case FF_QUIT_EVENT:
    case SDL_QUIT:
      is->quit = 1;
      SDL_Quit();
      return 0;
      break;
    case FF_ALLOC_EVENT:
      alloc_picture(event.user.data1);
      break;
    case FF_REFRESH_EVENT:
      video_refresh_timer(event.user.data1);
      break;
    default:
      break;
    }
  }
  return 0;

}
Example #5
0
int decode_thread(void *arg) {

    VideoState *is = (VideoState *)arg;
    AVFormatContext *pFormatCtx = NULL;
    AVPacket pkt1, *packet = &pkt1;

    AVDictionary *io_dict = NULL;
    AVIOInterruptCB callback;

    int video_index = -1;
    int audio_index = -1;
    int i;

    is->videoStream=-1;
    is->audioStream=-1;
    is->audio_need_resample = 0;

    global_video_state = is;
    // will interrupt blocking functions if we quit!
    callback.callback = decode_interrupt_cb;
    callback.opaque = is;
    if (avio_open2(&is->io_context, is->filename, 0, &callback, &io_dict)) {
        fprintf(stderr, "Unable to open I/O for %s\n", is->filename);
        return -1;
    }

    // Open video file
    if(avformat_open_input(&pFormatCtx, is->filename, NULL, NULL)!=0)
        return -1; // Couldn't open file

    is->pFormatCtx = pFormatCtx;

    // Retrieve stream information
    if(avformat_find_stream_info(pFormatCtx, NULL)<0)
        return -1; // Couldn't find stream information

    // Dump information about file onto standard error
    av_dump_format(pFormatCtx, 0, is->filename, 0);

    // Find the first video stream
    for(i=0; i<pFormatCtx->nb_streams; i++) {
        if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO &&
                video_index < 0) {
            video_index=i;
        } else if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO &&
                  audio_index < 0) {
            audio_index=i;
        }
    }
    if(audio_index >= 0) {
        stream_component_open(is, audio_index);
    }
    if(video_index >= 0) {
        stream_component_open(is, video_index);
    }

    if(is->videoStream < 0 && is->audioStream < 0) {
        fprintf(stderr, "%s: could not open codecs\n", is->filename);
        goto fail;
    }

#ifdef __RESAMPLER__
    if( audio_index >= 0
            && pFormatCtx->streams[audio_index]->codec->sample_fmt != AV_SAMPLE_FMT_S16) {
        is->audio_need_resample=1;
        is->pResampledOut=NULL;
        is->pSwrCtx = NULL;

        printf("Configure resampler: ");

#ifdef __LIBAVRESAMPLE__
        printf("libAvResample\n");
        is->pSwrCtx = avresample_alloc_context();
#endif

#ifdef __LIBSWRESAMPLE__
        printf("libSwResample\n");
        is->pSwrCtx = swr_alloc();
#endif

        // Some MP3/WAV don't tell this so make assumtion that
        // They are stereo not 5.1
        if (pFormatCtx->streams[audio_index]->codec->channel_layout == 0
                && pFormatCtx->streams[audio_index]->codec->channels == 2) {
            pFormatCtx->streams[audio_index]->codec->channel_layout = AV_CH_LAYOUT_STEREO;
        } else if (pFormatCtx->streams[audio_index]->codec->channel_layout == 0
                   && pFormatCtx->streams[audio_index]->codec->channels == 1) {
            pFormatCtx->streams[audio_index]->codec->channel_layout = AV_CH_LAYOUT_MONO;
        } else if (pFormatCtx->streams[audio_index]->codec->channel_layout == 0
                   && pFormatCtx->streams[audio_index]->codec->channels == 0) {
            pFormatCtx->streams[audio_index]->codec->channel_layout = AV_CH_LAYOUT_STEREO;
            pFormatCtx->streams[audio_index]->codec->channels = 2;
        }

        av_opt_set_int(is->pSwrCtx,"in_channel_layout",
                       pFormatCtx->streams[audio_index]->codec->channel_layout, 0);
        av_opt_set_int(is->pSwrCtx,"in_sample_fmt",
                       pFormatCtx->streams[audio_index]->codec->sample_fmt, 0);
        av_opt_set_int(is->pSwrCtx,"in_sample_rate",
                       pFormatCtx->streams[audio_index]->codec->sample_rate, 0);

        av_opt_set_int(is->pSwrCtx,"out_channel_layout", AV_CH_LAYOUT_STEREO, 0);
        av_opt_set_int(is->pSwrCtx,"out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
        av_opt_set_int(is->pSwrCtx,"out_sample_rate", 44100, 0);

#ifdef __LIBAVRESAMPLE__
        if (avresample_open(is->pSwrCtx) < 0) {
#else
        if (swr_init(is->pSwrCtx) < 0) {
#endif
            fprintf(stderr, " ERROR!! From Samplert: %d Hz Sample format: %s\n",
                    pFormatCtx->streams[audio_index]->codec->sample_rate,
                    av_get_sample_fmt_name(pFormatCtx->streams[audio_index]->codec->sample_fmt));
            fprintf(stderr, "         To 44100 Sample format: s16\n");
            is->audio_need_resample=0;
            is->pSwrCtx = NULL;;
        }

    }
#endif

    // main decode loop

    for(;;) {
        if(is->quit) {
            break;
        }
        // seek stuff goes here
        if(is->seek_req) {
            int stream_index= -1;

            int64_t seek_target = is->seek_pos;

            if     (is->videoStream >= 0) stream_index = is->videoStream;
            else if(is->audioStream >= 0) stream_index = is->audioStream;

            if(stream_index>=0) {
                seek_target= av_rescale_q(seek_target, AV_TIME_BASE_Q,
                                          pFormatCtx->streams[stream_index]->time_base);
            }
            printf("Seek_target: %ld/%ld stream_index: %ld\n",is->seek_pos, seek_target, stream_index);

            if(av_seek_frame(is->pFormatCtx, stream_index, seek_target,
                             is->seek_flags) < 0) {
                fprintf(stderr, "%s: error while seeking\n", is->pFormatCtx->filename);
            } else {
                if(is->audioStream >= 0) {
                    packet_queue_flush(&is->audioq);
                    packet_queue_put(&is->audioq, &flush_pkt);
                }
                if(is->videoStream >= 0) {
                    packet_queue_flush(&is->videoq);
                    packet_queue_put(&is->videoq, &flush_pkt);
                }
            }
            is->seek_req = 0;
        }

        if(is->audioq.size > MAX_AUDIOQ_SIZE ||
                is->videoq.size > MAX_VIDEOQ_SIZE) {
            SDL_Delay(10);
            continue;
        }
        if(av_read_frame(is->pFormatCtx, packet) < 0) {
            if(is->pFormatCtx->pb->error == 0) {
                SDL_Delay(100); /* no error; wait for user input */
                continue;
            } else {
                break;
            }
        }
        // Is this a packet from the video stream?
        if(packet->stream_index == is->videoStream) {
            packet_queue_put(&is->videoq, packet);
        } else if(packet->stream_index == is->audioStream) {
            packet_queue_put(&is->audioq, packet);
        } else {
            av_free_packet(packet);
        }
    }
    /* all done - wait for it */
    while(!is->quit) {
        SDL_Delay(100);
    }
fail: {
        SDL_Event event;
        event.type = FF_QUIT_EVENT;
        event.user.data1 = is;
        SDL_PushEvent(&event);
    }
    return 0;
}

void stream_seek(VideoState *is, int64_t pos, int rel) {

  if(!is->seek_req) {
    is->seek_pos = pos;
    is->seek_flags = rel < 0 ? AVSEEK_FLAG_BACKWARD : 0;
    is->seek_req = 1;
  }
}
int main(int argc, char *argv[]) {

  SDL_Event       event;
  //double          pts;
  VideoState      *is;

  is = av_mallocz(sizeof(VideoState));

  if(argc < 2) {
    fprintf(stderr, "Usage: test <file>\n");
    exit(1);
  }
  // Register all formats and codecs
  av_register_all();
  
  if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
    fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
    exit(1);
  }

  // Make a screen to put our video
#ifndef __DARWIN__
  screen = SDL_SetVideoMode(640, 480, 0, 0);
#else
  screen = SDL_SetVideoMode(640, 480, 24, 0);
#endif
  if(!screen) {
    fprintf(stderr, "SDL: could not set video mode - exiting\n");
    exit(1);
  }

  av_strlcpy(is->filename, argv[1], 1024);

  is->pictq_mutex = SDL_CreateMutex();
  is->pictq_cond = SDL_CreateCond();

  schedule_refresh(is, 40);

  is->av_sync_type = DEFAULT_AV_SYNC_TYPE;
  is->parse_tid = SDL_CreateThread(decode_thread, is);
  if(!is->parse_tid) {
    av_free(is);
    return -1;
  }

  av_init_packet(&flush_pkt);
  flush_pkt.data = (unsigned char *)"FLUSH";
  
  for(;;) {
    double incr, pos;
    SDL_WaitEvent(&event);
    switch(event.type) {
    case SDL_KEYDOWN:
      switch(event.key.keysym.sym) {
      case SDLK_LEFT:
	incr = -10.0;
	goto do_seek;
      case SDLK_RIGHT:
	incr = 10.0;
	goto do_seek;
      case SDLK_UP:
	incr = 60.0;
	goto do_seek;
      case SDLK_DOWN:
	incr = -60.0;
	goto do_seek;
      do_seek:
	if(global_video_state) {
	  pos = get_master_clock(global_video_state);
	  pos += incr;
	  stream_seek(global_video_state, (int64_t)(pos * AV_TIME_BASE), incr);
	}
	break;
      default:
	break;
      }
      break;
    case FF_QUIT_EVENT:
    case SDL_QUIT:
      is->quit = 1;
      /*
       * If the video has finished playing, then both the picture and
       * audio queues are waiting for more data.  Make them stop
       * waiting and terminate normally.
       */
      SDL_CondSignal(is->audioq.cond);
      SDL_CondSignal(is->videoq.cond);
      SDL_Quit();
      exit(0);
      break;
    case FF_ALLOC_EVENT:
      alloc_picture(event.user.data1);
      break;
    case FF_REFRESH_EVENT:
      video_refresh_timer(event.user.data1);
      break;
    default:
      break;
    }
  }
  return 0;
}
static Uint32 sdl_refresh_timer_cb(Uint32 interval, void *opaque) {
  video_refresh_timer(opaque);
  return 0; /* 0 means stop timer */
}
Example #7
0
int main(int argc, char *argv[]) {
  SDL_Event       event;
  VideoState      *is;
  is = av_mallocz(sizeof(VideoState));
  if(argc < 2) {
    fprintf(stderr, "Usage: test <file>\n");
    exit(1);
  }
  // Register all formats and codecs
  av_register_all();

  if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
    fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
    exit(1);
  }

  // Make a screen to put our video
#ifndef __DARWIN__
  screen = SDL_SetVideoMode(640, 480, 0, 0);
#else
  screen = SDL_SetVideoMode(640, 480, 24, 0);
#endif
  if(!screen) {
    fprintf(stderr, "SDL: could not set video mode - exiting\n");
    exit(1);
  }

  av_strlcpy(is->filename, argv[1], 1024);


  // 初始化为视频缓冲准备的锁(pictq)
  // 因为一旦事件驱动调用视频函数, 视频函数会从 pictq 抽出预解码帧。
  // 同时, 视频解码器会把信息放进去, 我们不知道那个动作会先发生。
  is->pictq_mutex = SDL_CreateMutex();
  is->pictq_cond = SDL_CreateCond();

  // schedule_refresh 是一个将要定义的函数。它的动作是告诉系统在某个特定的毫秒数后弹出 FF_REFRESH_EVENT 事件。
  schedule_refresh(is, 40);

  is->av_sync_type = DEFAULT_AV_SYNC_TYPE;
  // 生成一个新线程能完全访问原始进程中的内存,启动我们给的线程,在这种情况下, 调用 decode_thread()并与 VideoState 结构体连接。
  is->parse_tid = SDL_CreateThread(decode_thread, is);
  if(!is->parse_tid) {
    av_free(is);
    return -1;
  }

//  事件循环
  for(;;) {

    SDL_WaitEvent(&event);
    switch(event.type) {
    case FF_QUIT_EVENT:
    case SDL_QUIT:
      is->quit = 1;
      /*
       * If the video has finished playing, then both the picture and
       * audio queues are waiting for more data.  Make them stop
       * waiting and terminate normally.
       */
      SDL_CondSignal(is->audioq.cond);
      SDL_CondSignal(is->videoq.cond);
      SDL_Quit();
      exit(0);
      break;
    case FF_ALLOC_EVENT:
      alloc_picture(event.user.data1);
      break;
    case FF_REFRESH_EVENT:
      video_refresh_timer(event.user.data1);
      break;
    default:
      break;
    }
  }
  return 0;
}
Example #8
0
int main(int argc, char* argv[]) {
//	SetUnhandledExceptionFilter(callback);
	SDL_Event event;
	VideoState* is = NULL;
	is = (VideoState*) av_mallocz(sizeof(VideoState));

	if (argc < 2) {
		fprintf(stderr, "Usage: test <file>\n");
		exit(1);
	}

	av_register_all();

	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
		fprintf(stderr, "Could't not initialize SDL - %s\n", SDL_GetError());
		exit(1);
	}

	screen = SDL_CreateWindow("Hello World", SDL_WINDOWPOS_CENTERED,
	SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL);
	if (!screen) {
		printf("Could not initialize SDL -%s\n", SDL_GetError());
		return -1;
	}
	render = SDL_CreateRenderer(screen, -1, 0);

	screen_mutex = SDL_CreateMutex();

	av_strlcpy(is->filename, argv[1], sizeof(is->filename));

	is->pictq_mutex = SDL_CreateMutex();
	is->pictq_cond = SDL_CreateCond();

	schedule_refresh(is, 40);

	is->av_sync_type = DEFAULT_AV_SYNC_TYPE;
	is->parse_tid = SDL_CreateThread(decode_thread, "decode_thread", is);
	if (!is->parse_tid) {
		av_free(is);
		return -1;
	}

	av_init_packet(&flush_pkt);
	flush_pkt.data = (unsigned char*) "FLUSH";

	for (;;) {
		double incr, pos;
		SDL_WaitEvent(&event);
		switch (event.type) {
		case SDL_KEYDOWN:
			switch (event.key.keysym.sym) {
			case SDLK_LEFT:
				incr = -10.0;
				goto do_seek;
			case SDLK_RIGHT:
				incr = 10.0;
				goto do_seek;
			case SDLK_UP:
				incr = 60.0;
				goto do_seek;
			case SDLK_DOWN:
				incr = -60.0;
				goto do_seek;
				do_seek: if (global_video_state) {
					pos = get_master_clock(global_video_state);
					pos += incr;
					stream_seek(global_video_state,
							(int64_t) (pos * AV_TIME_BASE), incr);
				}
				break;
			default:
				break;
			}
			break;
		case FF_QUIT_EVENT:
		case SDL_QUIT:
			is->quit = 1;
			SDL_Quit();
			return 0;
			break;
		case FF_REFRESH_EVENT:
			video_refresh_timer(event.user.data1);
			break;
		default:
			break;
		}
	}
	return 0;
}
Example #9
0
int main(int argc, char*argv[])
{
  if (argc > 1 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0))
  {
    usage();
    return 0;
  }
	signal( SIGINT, &onExit);

  ftime(&startTime);

  av_register_all();

  paused = 0;

  // Create space for all the video data
  VideoState      *is;
  is = av_mallocz(sizeof(VideoState));

  // Set up SDL window
  SDL_Event    event;
  memset(&event, 0, sizeof(SDL_Event));

  if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
    fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
    exit(1);
  }

  screen = SDL_SetVideoMode(640, 480, 24, 0);

  if(!screen) {
    fprintf(stderr, "SDL: could not set video mode - exiting\n");
    exit(1);
  }

	char * name = malloc(16);
	if(argc > 1){
		strcpy(name, argv[1]);
  }
	else
	{
	  printf("Enter target name of the server: ");
	  scanf("%s", name);
	}
  char * ip = nameserver_init(name);
  free(name);

  // Get the audio & video stream information
  parse_nameserver_msg(ip);

  global_video_state = is;
  //init_gis(global_video_state);
  init_gis(is);
  establish_peer_connections();

	//control_packet* cp = read_control_packet();
  // Initialize the audio & video streams
  /*
  stream_component_open(global_video_state, &cp->audio_codec_ctx);
  printf("[PLAYER] Opened Audio stream\n");
  stream_component_open(global_video_state, &cp->video_codec_ctx);
  printf("[PLAYER] Opened Video stream\n");
  */

	is->quit = 0;

  // Create thread locks
  is->pictq_mutex = SDL_CreateMutex();
  is->pictq_cond = SDL_CreateCond();

  schedule_refresh(is, 40);

  // Create the decode thread
  is->parse_tid = SDL_CreateThread(decode_thread, is);
  printf("[PLAYER] SDL intialized\n");

/*
  if(!is->parse_tid) {
    av_free(is);
    return -1;
  }
  */

	pthread_create(&keyboard_thread_id, NULL,  captureKeyboard, NULL);
	pthread_create(&stats_thread_id, NULL,  calculate_player_stats, NULL);
//	printf("[PLAYER] Keyboard thread :%d\n", keyboard_thread_id);

//  while(global_video_state->quit == 0) 
  while(1) {
    SDL_WaitEvent(&event);
    switch(event.type)
    {
      // Done decoding
      case FF_QUIT_EVENT:
      case SDL_QUIT:
        is->quit = 1;
        exit(0);
        break;
      // Get frame
      case FF_ALLOC_EVENT:
        alloc_picture(event.user.data1);
        break;

      // Refresh screen
      case FF_REFRESH_EVENT:
        video_refresh_timer(event.user.data1);
        break;

      default:
        break;
    }
  }

  printf("Frames per second: %i\n", 5);
  printf("[MAIN] Quit player\n");
  return 0;
}
Example #10
0
void *event_loop(void *data)
{
	SDLMOD_Event event;
	VideoState *is;
	is = (VideoState *)data;
    for(;;) {
		double incr, pos;

        SDLMOD_WaitEvent(&event);
        switch(event.type) {
#if 0
		case SDL_VIDEORESIZE:
			g_video_width = event.resize.w;
			g_video_height = event.resize.h;
			g_video_resized = 1;
			break;
		
		case SDL_KEYDOWN:
			switch(event.key.keysym.sym) {
			case SDLK_LEFT:
				incr = -1.0;
				goto do_seek;
			case SDLK_RIGHT:
				incr = 1.0;
				goto do_seek;
			case SDLK_UP:
				incr = 6.0;
				goto do_seek;
			case SDLK_DOWN:
				incr = -6.0;
				goto do_seek;
			do_seek:
				if(global_video_state) {
					/* 获取当前播放位置 */
					pos = get_master_clock(global_video_state);
					pos += incr;
					stream_seek(global_video_state, (int64_t)(pos * AV_TIME_BASE), incr);
				}
			default: 
				break;
			}
			break;
#endif

        case FF_QUIT_EVENT:
        case SDLMOD_QUIT:
            SDLMOD_TimerQuit(); //FIXME: no refresh op
            SDLMOD_StopEventLoop();
            is->quit = 1;
            exit(0);
            break;
		case FF_ALLOC_EVENT:
			//printf("FF_ALLOC_EVENT begin\n");
			alloc_picture(event.user.data1);
			//printf("FF_ALLOC_EVENT end\n");
			break;
			
		case FF_REFRESH_EVENT:
			//printf("FF_REFRESH_EVENT begin\n");
			video_refresh_timer(event.user.data1);
			//printf("FF_REFRESH_EVENT end\n");
			break;
			
        default:
			printf("event.type == %d\n", event.type);
            break;
        }
    }
}
Example #11
0
int main(int argc, char *argv[]) {

	SDL_Event event;
	VideoState *is;
	int i;
	puts("start");
	global_mutex_lock = SDL_CreateMutex();

	is = av_mallocz(sizeof(VideoState));
	if (argc < 2){
		fprintf(stderr, "Usage: test <file>\n");
		exit(1);
	}
		
	av_register_all();		// Register all formats and codecs
	puts("avregister");
	//if (av_register_protocol(&e2URLProtocol) < 0){
	//	printf("Error - URL protocol \n");
	//	exit(-1)
	//}
	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)){
		fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
		exit(1);
	}
	for(i=0; i<MAX_CHANNELS;i++){
		// Make a screen to put our video
		#ifndef __DARWIN__
		screen[i] = SDL_SetVideoMode(640, 480, 0, 0);
		#else
		screen[i] = SDL_SetVideoMode(640, 480, 24, 0);
		#endif
		if (!screen[i]){
			fprintf(stderr, "SDL: could not set video mode - exiting\n");
			exit(1);
		}
	}
	for(i=0; i<MAX_CHANNELS;i++){
		global_video_state[i] = av_mallocz(sizeof(VideoState));
		global_video_state[i]->videoIndex = i;
		puts("screen created");
		printf("i is: %d\n",i);
		av_strlcpy(global_video_state[i]->filename, argv[i+1], sizeof(global_video_state[i]->filename));
		puts("avstrlcpy");	
		global_video_state[i]->pictq_mutex = SDL_CreateMutex();
		global_video_state[i]->pictq_cond = SDL_CreateCond();
		schedule_refresh(global_video_state[i], 40);
		global_video_state[i]->av_sync_type = DEFAULT_AV_SYNC_TYPE;
		global_video_state[i]->parse_tid = SDL_CreateThread(decode_thread, global_video_state[i]);
		puts("main var created");
		if (!global_video_state[i]->parse_tid) {
			av_free(global_video_state[i]);
			return -1;
		}
	}
	av_init_packet(&f_pkt);
	puts("av_init_packet");
	f_pkt.data = (unsigned char*)"FLUSH";

	for (;;) {
		double inc , pos;
		SDL_WaitEvent(&event);
		switch (event.type) {
			case SDL_KEYDOWN:
				switch (event.key.keysym.sym) {
					case SDLK_LEFT:
						inc = -10.0;
						goto do_seek;
					case SDLK_RIGHT:
						inc = 10.0;
						goto do_seek;
					case SDLK_UP:
						inc = 60.0;
						goto do_seek;
					case SDLK_DOWN:
						inc = -60.0;
						goto do_seek;
					do_seek:
						SDL_LockMutex(global_mutex_lock);
						if (global_video_state[global_videoIndex]){
							pos = get_master_clock(global_video_state[global_videoIndex]);
							pos += inc;
							stream_seek(global_video_state[global_videoIndex],(int64_t)(pos *AV_TIME_BASE),inc);
						}
						SDL_UnlockMutex(global_mutex_lock);
						break;
					case SDLK_b:
						global_video_state[global_videoIndex]->color_req = 'b';
						break;
					case SDLK_r:
						global_video_state[global_videoIndex]->color_req = 'r';
						break;
					case SDLK_g:
						global_video_state[global_videoIndex]->color_req = 'g';
						break;
					case SDLK_w:
						global_video_state[global_videoIndex]->color_req = 'w';
						break;
					case SDLK_n:
						global_video_state[global_videoIndex]->color_req = 'n';
						break;
					case SDLK_1:
						change_channel(1);
						break;
					case SDLK_2:
						change_channel(2);
						break;
					case SDLK_3:
						change_channel(3);
						break;
					case SDLK_4:
						change_vidchannel(1);
						break;
					case SDLK_5:
						change_vidchannel(2);
						break;
					case SDLK_6:
						change_vidchannel(3);
						break;
					case SDLK_7:
						change_audchannel(1);
						break;
					case SDLK_8:
						change_audchannel(2);
						break;
					case SDLK_9:
						change_audchannel(3);
						break;
				default:
					break;
				}
				break;
			case FF_QUIT_EVENT:
				case SDL_QUIT:
					for(i=0; i<MAX_CHANNELS; i++){
						global_video_state[i]->quit = 1;
						SDL_CondSignal(global_video_state[i]->audioq.cond);
						SDL_CondSignal(global_video_state[i]->videoq.cond);
					}
					SDL_Quit();
					exit(0);
					break;
				case FF_ALLOC_EVENT:
					alloc_picture(event.user.data1);
					break;
				case FF_REFRESH_EVENT:
					video_refresh_timer(event.user.data1);
					break;
			default:
				break;
		}
	}
	return 0;

}