static int create_ses_by_remote_sdp(int local_port, char *sdp)
{
    pj_media_session_t *ses = NULL;
    pjsdp_session_desc *sdp_ses;
    pj_media_sock_info skinfo;
    pj_pool_t *pool;
    char s[4];
    const pj_media_stream_info *info[2];
    int i, count;

    pool = pj_pool_create(pf, "sdp", 1024, 0, NULL);
    if (!pool) {
	PJ_LOG(1,(THIS_FILE, "Unable to create pool"));
	return -1;
    }

    pj_bzero(&skinfo, sizeof(skinfo));
    skinfo.rtp_sock = skinfo.rtcp_sock = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, 0);
    if (skinfo.rtp_sock == PJ_INVALID_SOCKET) {
	PJ_LOG(1,(THIS_FILE, "Unable to create socket"));
	goto on_error;
    }

    pj_sockaddr_init2(&skinfo.rtp_addr_name, "0.0.0.0", local_port);
    if (pj_sock_bind(skinfo.rtp_sock, (struct pj_sockaddr*)&skinfo.rtp_addr_name, sizeof(pj_sockaddr_in)) != 0) {
	PJ_LOG(1,(THIS_FILE, "Unable to bind socket"));
	goto on_error;
    }

    sdp_ses = pjsdp_parse(sdp, strlen(sdp), pool);
    if (!sdp_ses) {
	PJ_LOG(1,(THIS_FILE, "Error parsing SDP"));
	goto on_error;
    }

    ses = pj_media_session_create_from_sdp(mm, sdp_ses, &skinfo);
    if (!ses) {
	PJ_LOG(1,(THIS_FILE, "Unable to create session from SDP"));
	goto on_error;
    }

    if (pj_media_session_activate(ses) != 0) {
	PJ_LOG(1,(THIS_FILE, "Error activating session"));
	goto on_error;
    }

    count = pj_media_session_enum_streams(ses, 2, info);
    printf("\nDumping streams: \n");
    for (i=0; i<count; ++i) {
	const char *dir;
	char *local_ip;

	switch (info[i]->dir) {
	case PJMEDIA_DIR_NONE:
	    dir = "- NONE -"; break;
	case PJMEDIA_DIR_ENCODING:
	    dir = "SENDONLY"; break;
	case PJMEDIA_DIR_DECODING:
	    dir = "RECVONLY"; break;
	case PJMEDIA_DIR_ENCODING_DECODING:
	    dir = "SENDRECV"; break;
	default:
	    dir = "?UNKNOWN"; break;
	}

	local_ip = pj_sockaddr_get_str_addr(&info[i]->sock_info.rtp_addr_name);

	printf("  Stream %d: %.*s %s local=%s:%d remote=%.*s:%d\n",
	       i, info[i]->type.slen, info[i]->type.ptr,
	       dir, 
	       local_ip, pj_sockaddr_get_port(&info[i]->sock_info.rtp_addr_name),
	       info[i]->rem_addr.slen, info[i]->rem_addr.ptr, info[i]->rem_port);
    }

    puts("Press <ENTER> to quit");
    fgets(s, sizeof(s), stdin);

    pj_media_session_destroy(ses);
    pj_sock_close(skinfo.rtp_sock);
    pj_pool_release(pool);

    return 0;

on_error:
    if (ses)
	pj_media_session_destroy(ses);
    if (skinfo.rtp_sock != PJ_INVALID_SOCKET)
	pj_sock_close(skinfo.rtp_sock);
    if (pool)
	pj_pool_release(pool);
    return -1;
}
Beispiel #2
0
pj_status_t session_test (pj_pool_factory *pf)
{
    pj_med_mgr_t *mm;
    pj_media_session_t *s1, *s2;
    pj_pool_t *pool;
    pjsdp_session_desc *sdp;
    pj_media_stream_info sd_info;
    char buf[1024];
    int len;
    pj_media_stream_stat tx_stat, rx_stat;

    pool = pj_pool_create(pf, "test", 4096, 1024, NULL);

    // Init media manager.
    mm = pj_med_mgr_create ( pf );

    // Create caller session.
    // THIS WILL DEFINITELY CRASH (NULL as argument)!
    s1 = pj_media_session_create (mm, NULL);

    // Set caller's media to send-only.
    sd_info.dir = PJMEDIA_DIR_ENCODING;
    pj_media_session_modify_stream (s1, 0, PJMEDIA_STREAM_MODIFY_DIR, &sd_info);

    // Create caller SDP.
    sdp = pj_media_session_create_sdp (s1, pool, 0);
    len = pjsdp_print (sdp, buf, sizeof(buf));
    buf[len] = '\0';
    printf("Caller's initial SDP:\n<BEGIN>\n%s\n<END>\n", buf);

    // Parse SDP from caller.
    sdp = pjsdp_parse (buf, len, pool);

    // Create callee session based on caller's SDP.
    // THIS WILL DEFINITELY CRASH (NULL as argument)!
    s2 = pj_media_session_create_from_sdp (mm, sdp, NULL);
    
    // Create callee SDP
    sdp = pj_media_session_create_sdp (s2, pool, 0);
    len = pjsdp_print (sdp, buf, sizeof(buf));
    buf[len] = '\0';
    printf("Callee's SDP:\n<BEGIN>\n%s\n<END>\n", buf);

    // Parse SDP from callee.
    sdp = pjsdp_parse (buf, len, pool);

    // Update caller
    pj_media_session_update (s1, sdp);
    sdp = pj_media_session_create_sdp (s1, pool, 0);
    pjsdp_print (sdp, buf, sizeof(buf));
    printf("Caller's SDP after update:\n<BEGIN>\n%s\n<END>\n", buf);

    // Now start media.
    pj_media_session_activate (s2);
    pj_media_session_activate (s1);

    // Wait
    for (;;) {
	int has_stat;

	printf("Enter q to exit, 1 or 2 to print statistics.\n");
	fgets (buf, 10, stdin);
	has_stat = 0;

	switch (buf[0]) {
	case 'q':
	case 'Q':
	    goto done;
	    break;
	case '1':
	    pj_media_session_get_stat (s1, 0, &tx_stat, &rx_stat);
	    has_stat = 1;
	    break;
	case '2':
	    pj_media_session_get_stat (s2, 0, &tx_stat, &rx_stat);
	    has_stat = 1;
	    break;
	}

	if (has_stat) {
	    pj_media_stream_stat *stat[2] = { &tx_stat, &rx_stat };
	    const char *statname[2] = { "TX", "RX" };
	    int i;

	    for (i=0; i<2; ++i) {
		printf("%s statistics:\n", statname[i]);
		printf(" Pkt      TX=%d RX=%d\n", stat[i]->pkt_tx, stat[i]->pkt_rx);
		printf(" Octets   TX=%d RX=%d\n", stat[i]->oct_tx, stat[i]->oct_rx);
		printf(" Jitter   %d ms\n", stat[i]->jitter);
		printf(" Pkt lost %d\n", stat[i]->pkt_lost);
	    }
	    printf("\n");
	}
    }

done:

    // Done.
    pj_pool_release (pool);
    pj_media_session_destroy (s2);
    pj_media_session_destroy (s1);
    pj_med_mgr_destroy (mm);

    return 0;
}