/*
 * create_players
 *
 * INTERNAL: This function should only be called from the create_match_state
 * function and is used to create the players for use in a match.
 */
void create_players(TEAM **teams,
                    int players_per_team,
                    AUTOMATON_HANDLER *automaton_handler)
{
  /*
   * Local Variables.
   */
  AUTOMATON *team_0_start_automaton;
  AUTOMATON *team_1_start_automaton;
  AUTOMATON_STATE *team_0_start_state;
  AUTOMATON_STATE *team_1_start_state;
  int ii;

  /*
   * Based on which team is on offence set up the two teams starting automaton.
   * This also defines the starting state for the players inside the automaton.
   */
  if (teams[0]->is_offense)
  {
    team_0_start_automaton = automaton_handler->offensive_set->start_automaton;
    team_1_start_automaton = automaton_handler->defensive_set->start_automaton;
  }
  else
  {
    team_0_start_automaton = automaton_handler->defensive_set->start_automaton;
    team_1_start_automaton = automaton_handler->offensive_set->start_automaton;
  }
  team_0_start_state = team_0_start_automaton->start_state;
  team_1_start_state = team_1_start_automaton->start_state;

  for (ii = 0; ii < players_per_team; ii++)
  {
    /*
     * Create the player object with the player id and team id.
     */
    teams[0]->players[ii] = create_player(ii, 0);
    teams[1]->players[ii] = create_player(ii, 1);

    /*
     * TODO: Sort out proper width for players.
     */
    teams[0]->players[ii]->width_x = 2.0f;
    teams[1]->players[ii]->width_x = 2.0f;
    teams[0]->players[ii]->width_y = 2.0f;
    teams[1]->players[ii]->width_y = 2.0f;
    teams[0]->players[ii]->height = 2.0f;
    teams[1]->players[ii]->height = 2.0f;

    /*
     * The player has a reference to the starting automaton for their team. We
     * also fill in the initial automaton state here.
     */
    teams[0]->players[ii]->automaton = team_0_start_automaton;
    teams[1]->players[ii]->automaton = team_1_start_automaton;
    teams[0]->players[ii]->automaton_state = team_0_start_state;
    teams[1]->players[ii]->automaton_state = team_1_start_state;
  }
}
Beispiel #2
0
void test_handles_draw() {
  Board* board = draw_board();
  Player* p1 = create_player('X', &fake_move);
  Player* p2 = create_player('O', &fake_move);
  start_game(board, p1, p2);
  destroy_board(board);
  destroy_player(p1);
  destroy_player(p2);
  assert(strstr(writer_log, "Draw"));
}
Beispiel #3
0
void test_gets_valid_move() {
  Board* board = draw_board();
  unset_move(board, 1);
  Player* p1 = create_player('X', &incrementing_move);
  Player* p2 = create_player('O', &fake_move);
  start_game(board, p1, p2);
  destroy_board(board);
  destroy_player(p1);
  destroy_player(p2);
  current_move = 0;
  assert(strstr(writer_log, "Player 1's turn"));
  assert(strstr(writer_log, "Invalid move"));
  assert(strstr(writer_log, "X wins!"));
}
Beispiel #4
0
void test_handles_win() {
  Board* board = draw_board();
  unset_move(board, 0);
  Player* p1 = create_player('O', &fake_move);
  Player* p2 = create_player('X', &fake_move);
  start_game(board, p1, p2);
  destroy_board(board);
  destroy_player(p1);
  destroy_player(p2);
  assert(strstr(writer_log, "1 O X O X O O X O"));
  assert(strstr(writer_log, "Player 1's turn"));
  assert(strstr(writer_log, "O O X O X O O X O"));
  assert(strstr(writer_log, "O wins!"));
}
Beispiel #5
0
void test_switches_turns() {
  Board* board = draw_board();
  unset_move(board, 0);
  unset_move(board, 1);
  Player* p1 = create_player('X', &incrementing_move);
  Player* p2 = create_player('O', &incrementing_move);
  start_game(board, p1, p2);
  destroy_board(board);
  destroy_player(p1);
  destroy_player(p2);
  current_move = 0;
  assert(strstr(writer_log, "Player 1's turn"));
  assert(strstr(writer_log, "Player 2's turn"));
  assert(strstr(writer_log, "Draw"));
}
Beispiel #6
0
void		eht(t_env *e, int fd, char **arg)
{
    t_players	*player;
    t_players	*playeregg;
    int		n;
    char		*str;

    (void) arg;
    player = get_player_from_id(e->players, fd);
    n = get_available_slots(e->parser->teams, player->team_name);
    change_available_slots(e->parser->teams, player->team_name, n + 1);
    create_player(e->players, e->parser, player->idegg);
    if ((playeregg = get_player_from_id(e->players, player->idegg)) != NULL)
    {
        printf("New client (ghost | fork): %d\n", player->idegg);
        playeregg->playerx = player->playerxegg;
        playeregg->playery = player->playeryegg;
        playeregg->isegg = TRUE;
        playeregg->ghost = TRUE;
        playeregg->team_name = strdup(player->team_name);
        playeregg->idegg = player->idegg;
        player->idegg = -1;
        asprintf(&str, "eht %d\n", playeregg->id);
        send_mess_graphic_client(e, str);
    }
}
Beispiel #7
0
void player_login(rpacket_t rpk,player_t ply)
{
	uint32_t gateident = rpk_read_uint32(rpk);
	const char *actname = rpk_read_string(rpk);
	ply = find_player_by_actname(actname);
	if(ply){
		//对象还未销毁
		if(ply->_agentsession != 0){
			//通知gate直接断掉连接
			shortmsg2gate(CMD_GAME2GATE_INVID_CON,gateident);
		}else{
			//重新绑定
		}		
	}else{
		ply = create_player(actname,gateident);
		if(!ply){
			//通知玩家系统繁忙
			shortmsg2gate(CMD_GAME2GATE_BUSY,gateident);
			return;
		}
		if(0 != load_player_info(ply)){
			shortmsg2gate(CMD_GAME2GATE_BUSY,gateident);
			remove_player(ply);
		}
	}
}
Beispiel #8
0
/*****************************************************************************
 * test: play WAV file
 */
static void systest_play_wav(unsigned path_cnt, const char *paths[])
{
    pjsua_player_id play_id = PJSUA_INVALID_ID;
    enum gui_key key;
    test_item_t *ti;
    const char *title = "WAV File Playback Test";
    pj_status_t status;

    ti = systest_alloc_test_item(title);
    if (!ti)
	return;

    pj_ansi_snprintf(textbuf, sizeof(textbuf),
		     "This test will play %s file to "
		     "the speaker. Please listen carefully for audio "
		     "impairments such as stutter. Let this test run "
		     "for a while to make sure that everything is okay."
		     " Press OK to start, CANCEL to skip",
		     paths[0]);

    key = gui_msgbox(title, textbuf,
		     WITH_OKCANCEL);
    if (key != KEY_OK) {
	ti->skipped = PJ_TRUE;
	return;
    }

    PJ_LOG(3,(THIS_FILE, "Running %s", title));

    /* WAV port */
    status = create_player(path_cnt, paths, &play_id);
    if (status != PJ_SUCCESS)
	goto on_return;

    status = pjsua_conf_connect(pjsua_player_get_conf_port(play_id), 0);
    if (status != PJ_SUCCESS)
	goto on_return;

    key = gui_msgbox(title,
		     "WAV file should be playing now in the "
		     "speaker. Press OK to stop. ", WITH_OK);

    status = PJ_SUCCESS;

on_return:
    if (play_id != -1)
	pjsua_player_destroy(play_id);

    if (status != PJ_SUCCESS) {
	systest_perror("Sorry we've encountered error", status);
	ti->success = PJ_FALSE;
	pj_strerror(status, ti->reason, sizeof(ti->reason));
    } else {
	key = gui_msgbox(title, "Is the audio okay?", WITH_YESNO);
	ti->success = (key == KEY_YES);
	if (!ti->success)
	    pj_ansi_strcpy(ti->reason, USER_ERROR);
    }
    return;
}
Beispiel #9
0
void		init_player(int *ix, int *iy, t_board *map)
{
  int		i;
  int		j;

  i = 0;
  j = 0;
  while (map->map && map->map[i])
    {
      while (map->map[i] && map->map[i] != endLine)
	{
	  if (map->map[i] == Entry)
	    {
	      *ix = (i % (map->size + 1));
	      *iy = j;
	    }
	  if (map->map[i] == Exit)
	    {
	      map->player.exx = (i % (map->size + 1));
	      map->player.exy = j;
	    }
	  i++;
	}
      j++;
      i++;
    }
  create_player(&map->player);
}
Beispiel #10
0
t_game  *create_game(SDL_Renderer *ren)
{
    const unsigned int windowWidth = 640;
    const unsigned int windowHeight = 480;

    t_game *game = malloc(sizeof(t_game*));
    t_texture *texture = malloc(sizeof(t_texture*));
    t_map* m;

    m = ChargerMap("./data/level.txt");

    game->maps = m;
    const char* imagePath = "./data/background/map1.png";
    texture->surface = IMG_Load(imagePath);
    if ( texture->surface == NULL )
        {
            fprintf(stderr,"Erreur de chargement de l'image %s : %s\n",texture->surface,IMG_GetError());
            return -3;
        }
    texture->texture = SDL_CreateTextureFromSurface(ren, texture->surface);

    game->background = texture;


    game->velocityBack = 0;

    game->player = create_player(ren);

    return (game);
}
Beispiel #11
0
void World::load(std::string const &filename) {
    std::ifstream map;
    map.open(filename, std::fstream::in);

    if (!map.is_open()) {
        throw std::runtime_error(filename + " does not exist");
    }

    std::string line;
    std::vector<Vertex> vertices;
    sectors.clear();
    while (std::getline(map, line)) {
        std::istringstream ss(line);
        std::string type;
        ss >> type;

        if (type == "vertex") {
            add_vertices(ss, vertices);
        } else if (type == "sector") {
            add_sector(ss, vertices);
        } else if (type == "player") {
            player = create_player(ss);
        } else {
            throw std::runtime_error("Could not load map. Invalid data format found in " + filename);
        }
    }

    map.close();
}
Beispiel #12
0
static Player * get_or_create_plajur(int id) {
	if(id != me->id && id < NUM_PLAYERS) {
		Player * p = players[id];
		if(p == NULL)
			p = create_player("PLAJUR", id);
		return p;
	} else {
		return NULL;
	}
}
Beispiel #13
0
int main(int argc, char**argv)
{
    int running = 1;

    m = create_mixer();
    p = create_player(m);
    if(argc == 2){
        player_load(p, argv[1]);
    }else{
        player_load(p, "res/silent_light.mid");
    }
    /*
    mixer_add_instrument(m,create_instrument(OSC_SQU));
    mixer_add_instrument(m,create_instrument(OSC_SAW));
    mixer_add_instrument(m,create_instrument(OSC_SAW));
    mixer_add_instrument(m,create_instrument(OSC_SAW));
    mixer_add_instrument(m,create_instrument(OSC_SAW));
    mixer_add_instrument(m,create_instrument(OSC_SAW));
    mixer_add_instrument(m,create_instrument(OSC_SAW));
    mixer_add_instrument(m,create_instrument(OSC_SAW));
    mixer_add_instrument(m,create_instrument(OSC_SAW));
    mixer_add_instrument(m,create_instrument(OSC_SAW));
    mixer_add_instrument(m,create_instrument(OSC_SAW));
    mixer_add_instrument(m,create_instrument(OSC_SAW));
    mixer_add_instrument(m,create_instrument(OSC_SAW));
    mixer_add_instrument(m,create_instrument(OSC_SAW));
    mixer_add_instrument(m,create_instrument(OSC_SAW));
    mixer_add_instrument(m,create_instrument(OSC_SAW));
    */

    //SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
	// Open the audio
	open_audio();
	while(running){
	    while(SDL_PollEvent( &event )){
	        switch( event.type ){
            case SDL_KEYDOWN:
                switch( event.key.keysym.sym ){
                    case SDLK_ESCAPE:
                        running = 0;
                    default:;
                }
                break;
            case SDL_QUIT:
                running = 0;
                break;
	        }
	    }
	    SDL_Delay(1);
	}
	// Close audio
	close_audio();
	return 0;
}
Beispiel #14
0
struct game_state *create_game(char *player_name) {
	struct game_state *game = NULL;
	game = calloc(sizeof(struct game_state));

	game->whos_turn =  0; // remote player goes first
	uint8_t seed = 0;
	// would be a good vuln, but too easy for fuzzer to hit.
	if ('\0' != player_name[0]) {
		seed = player_name[0];
	}
	game->pool = get_shuffled_deck(seed); // seed: first byte of player_name
	game->p_remote = create_player(0, player_name);
	char *bot = NULL;
	bot = calloc(4);
	strncpy(bot, "Bot", 3);

	game->p_bot = create_player(1, bot);

	return game;
}
Beispiel #15
0
void player_login(rpacket_t rpk)
{
	string_t actname = rpk_read_string(rpk);
	uint32_t gateident = rpk_read_uint32(rpk);
	player_t ply = find_player_by_actname(actname);
	if(ply){
		//对象还未销毁,重新绑定关系

	}else{
		create_player(actname,gateident);
		load_player_info(ply);//从数据库导入玩家信息
	}
}
Beispiel #16
0
Code game(SDL_Renderer *renderer, CommandTable *table) {
  char name[MAXIMUM_PLAYER_NAME_SIZE];
  Player player;
  Game game;
  Code code;
  code = read_player_name(name, MAXIMUM_PLAYER_NAME_SIZE, renderer);
  if (code == CODE_QUIT || code == CODE_CLOSE) {
    return code;
  }
  player = create_player(name, table);
  game = create_game(&player);
  code = run_game(&game, renderer);
  destroy_game(&game);
  return code;
}
Beispiel #17
0
world* create_world(int width, int height) {
  world* result = (world*) malloc(sizeof(world));

  result->width = width;
  result->height = height;
  result->game_player = create_player(0, 0);
  result->pellet = create_collectible(0, 0, 1);
  result->walls = (point*)malloc(sizeof(point) * width * height);

  reset_world(result);

  srand(0);

  return result;
}
Beispiel #18
0
void *AudioInOutThread( void *ptr ) 
{ 
    SLresult result; 
    create_player(); 

    result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, recorderBufferA, RECORDER_FRAMES*sizeof(short));
    __android_log_print(ANDROID_LOG_INFO, "NativeAudio", "enqueue saw play %d", result); 

// set the player's state to playing 
    result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_PLAYING); 
    assert(SL_RESULT_SUCCESS == result); 

    __android_log_print(ANDROID_LOG_INFO, "NativeAudio", "started playing %d", result); 

} 
Beispiel #19
0
dbref create_guest(char *name, char *password)
{
	dbref player;
	char *buff;

	if(!Wizard(mudconf.guest_nuker) || !Good_obj(mudconf.guest_nuker))
		mudconf.guest_nuker = 1;

	buff = alloc_lbuf("create_guest");

	/*
	 * Make the player. 
	 */

	player = create_player(name, password, mudconf.guest_nuker, 0, 1);

	if(player == NOTHING) {
		log_text("GUEST: failed in create_player\n");
		return NOTHING;
	}
	/*
	 * Turn the player into a guest. 
	 */

	s_Guest(player);
	move_object(player, mudconf.start_room);
	s_Flags(player, Flags(player) & ~WIZARD);
	s_Pennies(player, Pennies(mudconf.guest_char));
	s_Zone(player, Zone(mudconf.guest_char));
	s_Parent(player, Parent(mudconf.guest_char));

	/*
	 * Make sure the guest is locked. 
	 */
	do_lock(player, player, A_LOCK, tprintf("#%d", player), "me");
	do_lock(player, player, A_LENTER, tprintf("#%d", player), "me");

	/*
	 * Copy all attributes. 
	 */
	atr_cpy(GOD, player, mudconf.guest_char);
	free_lbuf(buff);
	return player;
}
Beispiel #20
0
int read_player(struct player *player, char *name)
{
	char *path;
	int ret;
	FILE *file;

	assert(name != NULL);
	assert(player != NULL);

	if (!(path = get_path(name)))
		return 0;
	if (!(file = fopen(path, "r"))) {
		if (errno == ENOENT)
			return create_player(player, name);
		else
			return perror(path), 0;
	}

	init_player(player, name);

	errno = 0;
	ret = fscanf(file, "%s %d %u\n", player->clan,
	             &player->elo, &player->rank);
	if (ret == EOF && errno != 0) {
		perror(path);
		goto fail;
	} else if (ret == EOF || ret == 0) {
		fprintf(stderr, "%s: Cannot match player clan\n", path);
		goto fail;
	} else if (ret == 1) {
		fprintf(stderr, "%s: Cannot match player rank\n", path);
		goto fail;
	} else if (ret == 2) {
		fprintf(stderr, "%s: Cannot match player score\n", path);
		goto fail;
	}

	fclose(file);
	return 1;

fail:
	fclose(file);
	return 0;
}
Beispiel #21
0
void            accept_new_connection()
{
   player         *p;
   int             new_socket;

   new_socket = current_players + 1;

   if (current_players == max_players)
   {
      write(new_socket, full_msg.where, full_msg.length);
      return;
   }
   p = create_player();
   input_player = p;
   current_player = p;
   p->fd = new_socket;
   strncpy(p->inet_addr, "Internal Test", MAX_INET_ADDR);
   connect(p);
   current_player = 0;
}
Beispiel #22
0
END_TEST

START_TEST (test_nserv_create_player)
{
#if 0
typedef struct { /* player */
    int points;
    deck current_deck;
    int water_level;
    bool dead;
    int played;
    char *name;
    int fd;
} player;
#endif

    int i;
    player *p, q;

    q.points = q.water_level = q.played = 0;
    q.name = 0;
    q.dead = true;
    q.fd = 1;

    for (i = 0; i < 12; i++)
        q.current_deck.weathercards[i] = 0;
    q.current_deck.lifebelts = 0;

    fail_if((p = create_player(1)) == 0);

    fail_unless(p->points == q.points);
    fail_unless(p->water_level == q.water_level);
    fail_unless(p->played == q.played);
    fail_unless(p->current_deck.lifebelts == q.current_deck.lifebelts);
    fail_unless(p->name == q.name);
    fail_unless(p->dead == q.dead);
    fail_unless(p->fd == q.fd);

    for (i = 0; i < 12; i++)
        fail_unless(p->current_deck.weathercards[i] == q.current_deck.weathercards[i]);
}
Beispiel #23
0
/* scan all robots and run them if necessary - this is a parse.c bit that
   gets called in timer_function */
void process_robots(void)
{
  robot *rscan;
  player *rc;

  if (sys_flags & PANIC)
    return;

  for (rscan = robot_start; rscan; rscan = rscan->next)
  {
    /* SPANG - MOVE THIS SOMEWHERE ELSE AND DO IT JUST ONCE? */
    /* check to make sure robot hasnt fallen off :-) */
    if (rscan->counter == 0 && !(rscan->flags & STORED))
    {
      rc = find_player_absolute_quiet(rscan->lower_name);
      /* check for nightmare crashes/etc ;) */
      if (!rc)
      {
	rc = create_player();
	rc->fd = -1;
	connect_robot_to_prog(rc, rscan->lower_name);
	rscan->actual_player = rc;

      }
      else if (!(rc->flags & PANIC))
      {
	/* be cheeky here, safetly thing really.  set actual_player to rc!
	   this is actually because i --KNOW-- someone will try logging in
	   as a robot and that could conceivably blow it away. */
	rscan->actual_player = rc;
	rscan->counter = rscan->speed;
	current_player = rscan->actual_player;
	current_room = rscan->actual_player->location;
	run_action(rscan);
	current_player = 0;
	current_room = 0;
      }
    }
  }
}
Beispiel #24
0
int			main_init(int argc, char **argv)
{
    int		semid;

    semid = get_semaph();
    semaph_wait_lock(semid);
    if (get_shmem() == NULL || get_semaph() == 0)
        return (-1);
    if (get_shmem()->nb_user > MAX_PLAYER)
    {
        printf("There is already enough player.\n");
        return (-1);
    }
    printf("Nb user on this mem %zu\n", get_shmem()->nb_user);
    if (create_player(argc, argv) == -1)
        return (-1);
    signal(SIGINT, sig_handler);
    signal(SIGQUIT, sig_handler);
    signal(SIGTERM, sig_handler);
    semaph_unlock(semid);
    return (0);
}
Beispiel #25
0
t_client	*check_egg(t_server *server, t_client *client)
{
  t_client	*egg;
  char		*s;

  if ((egg = get_client_egg_by_team(server->clients, client->team)) != NULL)
    {
      asprintf(&s, "eht #%d\n", egg->socket);
      broadcast(server->clients, s, FD_GRAPHIC);
      free(s);

      asprintf(&s, "ebo #%d\n", egg->socket);
      broadcast(server->clients, s, FD_GRAPHIC);
      free(s);

      client->player = egg->player;
      server->clients = pop_client(server->clients, egg->socket);
    }
  else
    create_player(server, client);

  return (server->clients);
}
Beispiel #26
0
// handler for call-media-state-change-events
static void on_call_media_state(pjsua_call_id call_id)
{
	// get call infos
	pjsua_call_info ci; 
	pjsua_call_get_info(call_id, &ci);
	
	pj_status_t status = PJ_ENOTFOUND;

	// check state if call is established/active
	if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
	
		log_message("Call media activated.\n");
		
		// create and start media player
		create_player(call_id);
		
		// create and start call recorder
		if (app_cfg.record_call)
		{
			create_recorder(ci);
		}
	} 
}
Beispiel #27
0
/**
 * Handles network traffic.
 */
void network() {
	char buffer[1024];
	size_t size;
	struct sockaddr src_addr;
	socklen_t addrlen;

	FD_ZERO(&readset);
	FD_SET(sockfd,&readset);

	tv.tv_sec = 0;
	tv.tv_usec = 0;

	if(select(sockfd+1,&readset,NULL,NULL,&tv) > 0) {
		size = recvfrom(sockfd, buffer, 1024, 0, &src_addr, &addrlen);
		//printf(">> %s\n", buffer);
		if(strncmp(buffer,"omg ",4)==0) {
				char * data = buffer+4;
				switch(state) {
					case STATE_INIT:
						break;
					case STATE_REQUESTED:
						if(CMD("nak")) {
							int slot;
							sscanf(data, "nak %d", &slot);
							printf("Recived nak for slot %i\n", slot);
							++slot;
							if(slot < 4) 
								request_slot(slot);
							else {
								printf("No free slots, shutting down");
								exit(2);
							}
						}
						break;
					case STATE_PLAYING: 
						if(CMD("req")) {
							int slot;
							sscanf(data, "req %d", &slot);
							if(me != NULL && me->id == slot) {
								sprintf(buffer, "omg nak %d", slot);
								send_msg(buffer);
							} else { 
								sprintf(buffer, "omg hai %d %f %f %f", me->id, me->pos.x, me->pos.y, me->angle);
								send_msg(buffer);
							}
						} else if(CMD("mov")) {
							int id;
							sscanf(data, "mov %d", &id);
							Player * p = get_or_create_plajur(id);
							if(p != NULL) {
								sscanf(data, "mov %d %f %f %f %d %f %f %f",&id, &p->pos.x, &p->pos.y, &p->angle, (int*)&p->current_base_texture, &p->velocity.x, &p->velocity.y, &p->da);
							}
						} else if(CMD("hai")) {
							int id;
							sscanf(data, "hai %d", &id);
							Player * p = get_or_create_plajur(id);
							if(p != NULL) {
								sscanf(data, "hai %d %f %f %f",&id, &p->pos.x, &p->pos.y, &p->angle);
							}
						} else if(CMD("rot")) {
							int id;
							sscanf(data, "rot %d", &id);
							Player * p = get_or_create_plajur(id);
							if(p != NULL) {
								sscanf(data, "rot %d %f %f",&id, &p->angle, &p->da);
							}
						} else if(CMD("fir")) {
							int id;
							sscanf(data, "fir %d", &id);
							Player * p = get_or_create_plajur(id);
							if(id != me->id && id < NUM_PLAYERS) {
								p->fire = true;
							}
						} else if(CMD("nof")) {
							int id;
							sscanf(data, "nof %d", &id);
							Player * p = players[id];
							if(id != me->id && p!=NULL) {
								p->fire = false;
							}
						} else if(CMD("kil")) {
							int id;
							sscanf(data, "kil %d", &id);
							Player * p = players[id];
							if(p!=NULL) {
								p->dead = 1;
							}
						}
						break;
				}
		} else {
			buffer[size] = 0;
			fprintf(stderr,"Recieved invalid data: %s\n", buffer);
		}
	}

	//Check if the slot request has time out (aka succeded)
	if(request_sent_time+0.5 < curtime() && state == STATE_REQUESTED) {
		ready = true;
		me = create_player(myname, requested_slot);
		state = STATE_PLAYING;
	}
}
Beispiel #28
0
static void systest_aec_test(void)
{
    const char *ref_wav_paths[] = { add_path(res_path, WAV_PLAYBACK_PATH),
				    ALT_PATH1 WAV_PLAYBACK_PATH };
    pjsua_player_id player_id = PJSUA_INVALID_ID;
    pjsua_recorder_id writer_id = PJSUA_INVALID_ID;
    enum gui_key key;
    test_item_t *ti;
    const char *title = "AEC/AES Test";
    unsigned last_ec_tail = 0;
    pj_status_t status;
    pj_str_t tmp;

    ti = systest_alloc_test_item(title);
    if (!ti)
	return;

    key = gui_msgbox(title,
		     "This test will try to find whether the AEC/AES "
		     "works good on this system. Test will play a file "
		     "while recording from mic. The recording will be "
		     "played back later so you can check if echo is there. "
		     "Press OK to start.",
		     WITH_OKCANCEL);
    if (key != KEY_OK) {
	ti->skipped = PJ_TRUE;
	return;
    }

    /* Save current EC tail */
    status = pjsua_get_ec_tail(&last_ec_tail);
    if (status != PJ_SUCCESS)
	goto on_return;

    /* Set EC tail setting to default */
    status = pjsua_set_ec(PJSUA_DEFAULT_EC_TAIL_LEN, 0);
    if (status != PJ_SUCCESS)
	goto on_return;

    /*
     * Create player and recorder
     */
    status = create_player(PJ_ARRAY_SIZE(ref_wav_paths), ref_wav_paths,
			   &player_id);
    if (status != PJ_SUCCESS) {
	PJ_PERROR(1,(THIS_FILE, status, "Error opening WAV file %s",
		     WAV_PLAYBACK_PATH));
	goto on_return;
    }

    status = pjsua_recorder_create(
                 pj_cstr(&tmp, add_path(doc_path, AEC_REC_PATH)), 0, 0, -1,
                 0, &writer_id);
    if (status != PJ_SUCCESS) {
	PJ_PERROR(1,(THIS_FILE, status, "Error writing WAV file %s",
		     AEC_REC_PATH));
	goto on_return;
    }

    /*
     * Start playback and recording.
     */
    pjsua_conf_connect(pjsua_player_get_conf_port(player_id), 0);
    pj_thread_sleep(100);
    pjsua_conf_connect(0, pjsua_recorder_get_conf_port(writer_id));

    /* Wait user signal */
    gui_msgbox(title, "AEC/AES test is running. Press OK to stop this test.",
	       WITH_OK);

    /*
     * Stop and close playback and recorder
     */
    pjsua_conf_disconnect(0, pjsua_recorder_get_conf_port(writer_id));
    pjsua_conf_disconnect(pjsua_player_get_conf_port(player_id), 0);
    pjsua_recorder_destroy(writer_id);
    pjsua_player_destroy(player_id);
    player_id = PJSUA_INVALID_ID;
    writer_id = PJSUA_INVALID_ID;

    /*
     * Play the result.
     */
    status = pjsua_player_create(
                 pj_cstr(&tmp, add_path(doc_path, AEC_REC_PATH)),
                 0, &player_id);
    if (status != PJ_SUCCESS) {
	PJ_PERROR(1,(THIS_FILE, status, "Error opening WAV file %s", AEC_REC_PATH));
	goto on_return;
    }
    pjsua_conf_connect(pjsua_player_get_conf_port(player_id), 0);

    /* Wait user signal */
    gui_msgbox(title, "We are now playing the captured audio from the mic. "
		      "Check if echo (of the audio played back previously) is "
		      "present in the audio. The recording is stored in "
		      AEC_REC_PATH " for offline analysis. "
		      "Press OK to stop.",
		      WITH_OK);

    pjsua_conf_disconnect(pjsua_player_get_conf_port(player_id), 0);

    key = gui_msgbox(title,
		     "Did you notice any echo in the recording?",
		     WITH_YESNO);


on_return:
    if (player_id != PJSUA_INVALID_ID)
	pjsua_player_destroy(player_id);
    if (writer_id != PJSUA_INVALID_ID)
	pjsua_recorder_destroy(writer_id);

    /* Wait until sound device closed before restoring back EC tail setting */
    while (pjsua_snd_is_active())
	pj_thread_sleep(10);
    pjsua_set_ec(last_ec_tail, 0);


    if (status != PJ_SUCCESS) {
	systest_perror("Sorry we encountered an error: ", status);
	ti->success = PJ_FALSE;
	pj_strerror(status, ti->reason, sizeof(ti->reason));
    } else if (key == KEY_YES) {
	ti->success = PJ_FALSE;
	if (!ti->success) {
	    pj_ansi_strcpy(ti->reason, USER_ERROR);
	}
    } else {
	char msg[200];

	pj_ansi_snprintf(msg, sizeof(msg), "Test succeeded.\r\n");

	ti->success = PJ_TRUE;
	pj_ansi_strncpy(ti->reason, msg, sizeof(ti->reason));
	ti->reason[sizeof(ti->reason)-1] = '\0';
    }
}
Beispiel #29
0
static void systest_latency_test(void)
{
    const char *ref_wav_paths[] = { add_path(res_path, WAV_TOCK8_PATH), ALT_PATH1 WAV_TOCK8_PATH };
    pj_str_t rec_wav_file;
    pjsua_player_id play_id = PJSUA_INVALID_ID;
    pjsua_conf_port_id play_slot = PJSUA_INVALID_ID;
    pjsua_recorder_id rec_id = PJSUA_INVALID_ID;
    pjsua_conf_port_id rec_slot = PJSUA_INVALID_ID;
    pj_pool_t *pool = NULL;
    pjmedia_port *wav_port = NULL;
    unsigned lat_sum=0, lat_cnt=0, lat_min=0, lat_max=0;
    enum gui_key key;
    test_item_t *ti;
    const char *title = "Audio Latency Test";
    pj_status_t status;

    ti = systest_alloc_test_item(title);
    if (!ti)
	return;

    key = gui_msgbox(title,
		     "This test will try to find the audio device's "
		     "latency. We will play a special WAV file to the "
		     "speaker for ten seconds, then at the end "
		     "calculate the latency. Please don't do anything "
		     "until the test is done.", WITH_OKCANCEL);
    if (key != KEY_OK) {
	ti->skipped = PJ_TRUE;
	return;
    }
    key = gui_msgbox(title,
		     "For this test to work, we must be able to capture "
		     "the audio played in the speaker (the echo), and only"
		     " that audio (i.e. you must be in relatively quiet "
		     "place to run this test). "
		     "Press OK to start, or CANCEL to skip.",
		     WITH_OKCANCEL);
    if (key != KEY_OK) {
	ti->skipped = PJ_TRUE;
	return;
    }

    PJ_LOG(3,(THIS_FILE, "Running %s", title));

    status = create_player(PJ_ARRAY_SIZE(ref_wav_paths), ref_wav_paths,
			   &play_id);
    if (status != PJ_SUCCESS)
	goto on_return;

    play_slot = pjsua_player_get_conf_port(play_id);

    rec_wav_file = pj_str(add_path(doc_path, WAV_LATENCY_OUT_PATH));
    status = pjsua_recorder_create(&rec_wav_file, 0, NULL, -1, 0, &rec_id);
    if (status != PJ_SUCCESS)
	goto on_return;

    rec_slot = pjsua_recorder_get_conf_port(rec_id);

    /* Setup the test */
    //status = pjsua_conf_connect(0, 0);
    status = pjsua_conf_connect(play_slot, 0);
    status = pjsua_conf_connect(0, rec_slot);
    status = pjsua_conf_connect(play_slot, rec_slot);


    /* We're running */
    PJ_LOG(3,(THIS_FILE, "Please wait while test is running (~10 sec)"));
    gui_sleep(10);

    /* Done with the test */
    //status = pjsua_conf_disconnect(0, 0);
    status = pjsua_conf_disconnect(play_slot, rec_slot);
    status = pjsua_conf_disconnect(0, rec_slot);
    status = pjsua_conf_disconnect(play_slot, 0);

    pjsua_recorder_destroy(rec_id);
    rec_id = PJSUA_INVALID_ID;

    pjsua_player_destroy(play_id);
    play_id = PJSUA_INVALID_ID;

    /* Confirm that echo is heard */
    gui_msgbox(title,
	       "Test is done. Now we need to confirm that we indeed "
	       "captured the echo. We will play the captured audio "
	       "and please confirm that you can hear the 'tock' echo.",
	       WITH_OK);

    status = pjsua_player_create(&rec_wav_file, 0, &play_id);
    if (status != PJ_SUCCESS)
	goto on_return;

    play_slot = pjsua_player_get_conf_port(play_id);

    status = pjsua_conf_connect(play_slot, 0);
    if (status != PJ_SUCCESS)
	goto on_return;

    key = gui_msgbox(title,
		     "The captured audio is being played back now. "
		     "Can you hear the 'tock' echo?",
		     WITH_YESNO);

    pjsua_player_destroy(play_id);
    play_id = PJSUA_INVALID_ID;

    if (key != KEY_YES)
	goto on_return;

    /* Now analyze the latency */
    pool = pjsua_pool_create("latency", 512, 512);

    status = pjmedia_wav_player_port_create(pool, rec_wav_file.ptr, 0, 0, 0, &wav_port);
    if (status != PJ_SUCCESS)
	goto on_return;

    status = calculate_latency(pool, wav_port, &lat_sum, &lat_cnt,
			       &lat_min, &lat_max);
    if (status != PJ_SUCCESS)
	goto on_return;

on_return:
    if (wav_port)
	pjmedia_port_destroy(wav_port);
    if (pool)
	pj_pool_release(pool);
    if (play_id != PJSUA_INVALID_ID)
	pjsua_player_destroy(play_id);
    if (rec_id != PJSUA_INVALID_ID)
	pjsua_recorder_destroy(rec_id);

    if (status != PJ_SUCCESS) {
	systest_perror("Sorry we encountered an error: ", status);
	ti->success = PJ_FALSE;
	pj_strerror(status, ti->reason, sizeof(ti->reason));
    } else if (key != KEY_YES) {
	ti->success = PJ_FALSE;
	if (!ti->success) {
	    pj_ansi_strcpy(ti->reason, USER_ERROR);
	}
    } else {
	char msg[200];
	pj_size_t msglen;

	pj_ansi_snprintf(msg, sizeof(msg),
			 "The sound device latency:\r\n"
			 " Min=%u, Max=%u, Avg=%u\r\n",
			 lat_min, lat_max, lat_sum/lat_cnt);
	msglen = strlen(msg);

	if (lat_sum/lat_cnt > 500) {
	    pj_ansi_snprintf(msg+msglen, sizeof(msg)-msglen,
			     "The latency is huge!\r\n");
	    msglen = strlen(msg);
	} else if (lat_sum/lat_cnt > 200) {
	    pj_ansi_snprintf(msg+msglen, sizeof(msg)-msglen,
			     "The latency is quite high\r\n");
	    msglen = strlen(msg);
	}

	key = gui_msgbox(title, msg, WITH_OK);

	ti->success = PJ_TRUE;
	pj_ansi_strncpy(ti->reason, msg, sizeof(ti->reason));
	ti->reason[sizeof(ti->reason)-1] = '\0';
    }
}
Beispiel #30
0
Player* build_player(char piece, PlayerType type) {
  MoveHandler handler = get_handler(type);
  return create_player(piece, handler);
}