OpenTok-API

 view release on metacpan or  search on metacpan

lib/OpenTok/API.pm  view on Meta::CPAN

http://www.tokbox.com/opentok/api/tools/as3/documentation/overview/token_creation.html

=item * C<< expire_time => int >>

Optional. The time when the token will expire, defined as an integer value for a Unix timestamp (in seconds).
If you do not specify this value, tokens expire in 24 hours after being created.
The expiration_time value, if specified, must be within 30 days of the creation time.

=back

=cut

sub generate_token {
    my $self = shift;
    my $create_time = time();
    my %arg = (
        session_id => '',
        role => $RoleConstants{PUBLISHER},
        expire_time => ($create_time +24*3600),
        @_,        
    );
    my $nonce = rand();
    my $query_string = "role=" . $arg{role} .
                       "&session_id=" . $arg{session_id} .
                       "&create_time=" . $create_time .
                       "&nonce=" . $nonce .
                       "&expire_time=" . $arg{expire_time} .
                       "&connection_data=";
    my $signature = hmac_sha1_hex($query_string, $self->{api_secret});
    return "T1==" . encode_base64("partner_id=".$self->{api_key}."&sdk_version=$API_VERSION&sig=$signature:$query_string",'');
     
}

=head2 create_session

Creates and returns OpenTok::API::Session object

    my $session_id = $ot->create_session( 
        location => '', 
        'p2p.preference' => "enabled"|"disabled" 
    )->getSessionId();

=over 4

=item * C<< location => string >>

An IP address that TokBox will use to situate the session in its global network. 
In general, you should not specify a location hint; if no location hint is specified,
the session uses a media server based on the location of the first client connecting to the session. 
Specify a location hint only if you know the general geographic region (and a representative IP address) 
for the session and you think the first client connecting may not be in that region.

=item * C<<  'p2p.preference' => 'enabled' >>

The properties option includes any following key value pairs. Currently only the following property exists:

p2p.preference (String) . Whether the session's streams will be transmitted directly between peers. You can set the following possible values:

"disabled" (the default) . The session's streams will all be relayed using the OpenTok servers. More than two clients can connect to the session.

"enabled" . The session will attempt to transmit streams directly between clients. If peer-to-peer streaming fails (either when streams are 
initially published or during the course of a session), the session falls back to using the OpenTok servers for relaying streams. 
(Peer-to-peer streaming uses UDP, which may be blocked by a firewall.) For a session created with peer-to-peer streaming enabled, 
only two clients can connect to the session at a time. If an additional client attempts to connect, 
the TB object on the client dispatches an exception event.
By removing the server, peer-to-peer streaming decreases latency and improves quality.

Note that the properties object previously included settings for multiplexing and server-side echo suppression. 
However, these features were deleted in OpenTok v0.91.48. (Server-side echo suppression was replaced with the 
acoustic echo cancellation feature added in OpenTok v0.91.18.)

=back

=cut

sub create_session {
    my $self = shift;
    my %arg = (
        location => '',
        api_key => $self->{api_key},
        @_,
    );
    my $session_raw = $self->_do_request("/session/create", %arg);
    my $session_xml;
    
    eval {
       $session_xml = XML::XPath->new( xml => $session_raw ) or OpenTok::API::Exception->throw( error => "Failed to create session: Invalid response from server: $!" );
    };    
    
    return if (Exception::Class->caught('OpenTok::API::Exception'));
    
    if($session_xml->exists('/Errors')) {
        my $err_msg = $session_xml->find('//@message');
        $err_msg = 'Unknown error' unless $err_msg;
        
        OpenTok::API::Exception::Auth->throw(error => "Error " . $session_xml->find('//@code') ." ". $session_xml->find('local-name(//error/*[1])') . ": " . $err_msg );
        
        return;       
    }
    
    return OpenTok::API::Session->new( map {  $_->getName => $_->string_value } $session_xml->find('//Session/*')->get_nodelist);       
    
}

# private methods

sub _do_request {
    my $self = shift;
    my $cmd = shift;
    my %arg = (
      @_,       
    );
    
    my $url = $API_SERVER{$self->{api_mode}}.$cmd;
    
    my $data =  join '&', map { "$_=".$self->_urlencode($arg{$_}) } keys %arg;
    
    my $ua = LWP::UserAgent->new;
    #$ua->agent("$0/0.1 " . $ua->agent);
    
    my $request = HTTP::Request->new(POST => $url);
    $request->header('X-TB-PARTNER-AUTH' => $self->{api_key}.':'.$self->{api_secret});
    $request->content_type('application/x-www-form-urlencoded');
    $request->content($data);
    
    my $result = $ua->request($request);



( run in 1.260 second using v1.01-cache-2.11-cpan-140bd7fdf52 )