You are here

livestream.inc in Embedded Media Field 5

This include processes livestream.com media files for use by emfield.module.

File

contrib/video_cck/providers/livestream.inc
View source
<?php

/**
 * @file
 *   This include processes livestream.com media files for use by emfield.module.
 */
define('VIDEO_CCK_LIVESTREAM_MAIN_URL', 'http://www.livestream.com/');
define('VIDEO_CCK_LIVESTREAM_API_INFO', 'http://www.livestream.com/userguide/index.php/API_overview');
define('VIDEO_CCK_LIVESTREAM_API_APPLICATION_URL', 'http://securebeta.livestream.com/myaccount/api');
define('VIDEO_CCK_LIVESTREAM_REST_ENDPOINT', 'http://channel.api.livestream.com/1.0');

/* http://channel.api.livestream.com/1.0/embed?channel=proshowcase
 * returns the actual video HTML
 *
 * http://channel.api.livestream.com/1.0/livestatus?channel=proshowcase
 * returns a result indicating whether the channel is live or not
 */

/**
 * hook video_cck_PROVIDER_info
 * this returns information relevant to a specific 3rd party video provider
 * @return
 *   an array of strings requested by various admin and other forms
 *   'name' => the translated name of the provider
 *   'url' => the url to the main page for the provider
 *   'settings_description' => a description of the provider that will be posted in the admin settings form
 *   'supported_features' => an array of rows describing the state of certain supported features by the provider.
 *      These will be rendered in a table, with the columns being 'Feature', 'Supported', 'Notes'.
 */
function video_cck_livestream_info() {
  $features = array(
    array(
      t('Autoplay'),
      t('Yes'),
      '',
    ),
    array(
      t('RSS Attachment'),
      t('No'),
      '',
    ),
    array(
      t('Show related videos'),
      t('No'),
      '',
    ),
    array(
      t('Thumbnails'),
      t('Yes'),
      t(''),
    ),
    array(
      t('Custom player colors'),
      t('No'),
      '',
    ),
    array(
      t('Full screen mode'),
      t('Yes'),
      t('You may customize the player to enable or disable full screen playback. Full screen mode is enabled by default.'),
    ),
  );
  return array(
    'provider' => 'livestream',
    'name' => t('Livestream'),
    'url' => VIDEO_CCK_LIVESTREAM_MAIN_URL,
    'settings_description' => t('These settings specifically affect videos displayed from <a href="@livestream" target="_blank">Livestream</a>. You can learn more about its <a href="@api" target="_blank">API</a> here.', array(
      '@livestream' => VIDEO_CCK_LIVESTREAM_MAIN_URL,
      '@api' => VIDEO_CCK_LIVESTREAM_API_INFO,
    )),
    'supported_features' => $features,
  );
}

/**
 * hook video_cck_PROVIDER_settings
 * this should return a subform to be added to the video_cck_settings() admin settings page.
 * note that a form field will already be provided, at $form['PROVIDER'] (such as $form['livestream'])
 * so if you want specific provider settings within that field, you can add the elements to that form field.
 */
function video_cck_livestream_settings() {
  $form['livestream']['api'] = array(
    '#type' => 'fieldset',
    '#title' => t('Livestream API'),
    '#description' => t('You will first need to apply for an API Developer Key from the <a href="@livestream" target="_blank">Livestream Developer Profile page</a>. Note that you do not need this key to display Livestream videos or their thumbnails.', array(
      '@livestream' => VIDEO_CCK_LIVESTREAM_API_APPLICATION_URL,
    )),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['livestream']['api']['video_cck_livestream_api_key'] = array(
    '#type' => 'textfield',
    '#title' => t('Livestream API Key'),
    '#default_value' => variable_get('video_cck_livestream_api_key', ''),
    '#description' => t('Please enter your Livestream Developer Key here.'),
  );
  $form['livestream']['player_options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Embedded video player options'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['livestream']['player_options']['video_cck_livestream_full_screen'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow fullscreen'),
    '#default_value' => variable_get('video_cck_livestream_full_screen', 1),
    '#description' => t('Allow users to view video using the entire computer screen.'),
  );
  $form['livestream']['player_options']['video_cck_livestream_autoplay'] = array(
    '#type' => 'checkbox',
    '#title' => t('Auto play'),
    '#default_value' => variable_get('video_cck_livestream_autoplay', 0),
    '#description' => t('Automatically play the video when users visit its page.'),
  );
  return $form;
}

/**
 * hook emfield_PROVIDER_data
 *
 * provides an array to be serialised and made available with $item elsewhere
 */
function video_cck_livestream_data($field, $item) {
  $data = array();

  // create some 'field' version control
  $data['video_cck_livestream_version'] = 2;

  // Thumbnail creation
  // The links from the RSS 2.0 API contain ondemand in the video string
  // So we need to handle that seperatly
  // Explodes mychannel?..&amp;clip=videoID into parts
  $boom = explode('?', $item['value']);
  $channel = $boom[0];
  if (strpos(strtolower($item['value']), 'clip') !== false) {
    preg_match('/clip=([^&]+)/', $item['value'], $matches);

    // Example API 2.0 call : http://xMYCHANNELNAMEx.channel-api.livestream-api.com/2.0/thumbnail?id=VIDEOID
    $api_url = 'http://x' . $channel . 'x.channel-api.livestream-api.com/2.0/clipdetails?id=' . $matches[1];

    // Get the thumbnail via an API 2.0 call
    $result = drupal_http_request($api_url);
    if ($result->code == 200) {
      $parser = drupal_xml_parser_create($result->data);
      $vals = array();
      $index = array();
      xml_parse_into_struct($parser, $result->data, $vals);
      xml_parser_free($parser);
      if (count($vals)) {
        foreach ($vals as $val) {
          if ($val['tag'] == 'MEDIA:THUMBNAIL') {
            $data['thumbnail']['url'] = $val['attributes']['URL'];
          }
          elseif ($val['tag'] == 'MEDIA:CONTENT') {
            $data['duration'] = $val['attributes']['DURATION'];
          }
        }
      }
    }
  }
  else {

    // live URL (thumbnail will change while stream progresses)
    $data['thumbnail']['url'] = 'http://thumbnail.api.livestream.com/thumbnail?name=' . $channel;
  }

  // gather info about the item
  // RSS / MRSS feeds with the item would have enough info
  // alternative try getting the minimum from an http get
  $url = 'http://www.livestream.com/' . $item['value'];
  $response = emfield_request_header('livestream', $url);
  if ($response->code == 200) {

    // probably shouldn't give the 303 path
    $data['flash']['url'] = $url;
    $data['flash']['size'] = $response->headers['Content-Length'];
    $data['flash']['mime'] = $response->headers['Content-Type'];
  }
  return $data;
}

/**
 *
 */
function video_cck_livestream_rss($item, $teaser = NULL) {
  if ($item['value']) {
    if (!empty($item['data']['video_cck_livestream_data_version']) && $item['data']['video_cck_livestream_data_version'] >= 1) {
      $data = $item['data'];
    }
    else {
      $data = video_cck_livestream_data(NULL, $item);
    }
    $file = array();
    if (is_array($data['flash'])) {
      $file['filepath'] = $data['flash']['url'];
      $file['filesize'] = $data['flash']['size'];
      $file['filemime'] = $data['flash']['mime'];
    }
    $file['thumbnail']['filepath'] = $data['thumbnail']['url'];
    return $file;
  }
}

/**
 * this is a wrapper for video_cck_request_xml that includes livestream's api key
 */
function video_cck_livestream_request($method, $args = array(), $cached = TRUE) {
  $args['dev_id'] = trim(variable_get('video_cck_livestream_api_key', ''));
  $args['method'] = $method;
  $request = emfield_request_xml('livestream', VIDEO_CCK_LIVESTREAM_REST_ENDPOINT, $args, $cached);
  return $request;
}

/**
 * hook video_cck_PROVIDER_extract
 * this is called to extract the video code from a pasted URL or embed code.
 * @param $embed
 *   an optional string with the pasted URL or embed code
 * @return
 *   either an array of regex expressions to be tested, or a string with the video code to be used
 *   if the hook tests the code itself, it should return either the string of the video code (if matched), or an empty array.
 *   otherwise, the calling function will handle testing the embed code against each regex string in the returned array.
 */
function video_cck_livestream_extract($embed = '') {

  // http://channel.api.livestream.com/1.0/embed?channel=whatstrending
  // http://www.livestream.com/whatstrending
  // http://www.livestream.com/whatstrending/video?clipId=flv_083673e4-c87b-4af1-9553-c5057495d111&time=0
  // <iframe width="560" height="340" src="http://cdn.livestream.com/embed/whatstrending?layout=4&amp;clip=flv_083673e4-c87b-4af1-9553-c5057495d111&amp;autoplay=false" style="border:0;outline:0" frameborder="0" scrolling="no"></iframe><div style="font-size: 11px;padding-top:10px;text-align:center;width:560px">Watch <a href="http://www.livestream.com/?utm_source=lsplayer&amp;utm_medium=embed&amp;utm_campaign=footerlinks" title="live streaming video">live streaming video</a> from <a href="http://www.livestream.com/whatstrending?utm_source=lsplayer&amp;utm_medium=embed&amp;utm_campaign=footerlinks" title="Watch whatstrending at livestream.com">whatstrending</a> at livestream.com</div>
  // <iframe width="560" height="340" src="http://cdn.livestream.com/embed/whatstrending?layout=4&amp;autoplay=false" style="border:0;outline:0" frameborder="0" scrolling="no"></iframe><div style="font-size: 11px;padding-top:10px;text-align:center;width:560px">Watch <a href="http://www.livestream.com/?utm_source=lsplayer&amp;utm_medium=embed&amp;utm_campaign=footerlinks" title="live streaming video">live streaming video</a> from <a href="http://www.livestream.com/whatstrending?utm_source=lsplayer&amp;utm_medium=embed&amp;utm_campaign=footerlinks" title="Watch whatstrending at livestream.com">whatstrending</a> at livestream.com</div>
  if (preg_match('@livestream\\.com/1.0/embed\\?channel=([^"\\& ]+)@i', $embed, $matches)) {
    return $matches[1];
  }
  if (preg_match('@livestream\\.com/(embed/)?([^/\\? ]+).*?((clipId|clip)=([^\\&\'" ]+))@i', $embed, $matches)) {
    return $matches[2] . '?clip=' . $matches[5];
  }
  else {
    if (preg_match('@livestream\\.com/(embed/)?([^/\\? ]+)@i', $embed, $matches)) {
      return $matches[2];
    }
  }
}

/**
 * hook video_cck_PROVIDER_embedded_link($video_code)
 * returns a link to view the video at the provider's site
 *  @param $video_code
 *    the string containing the video to watch
 *  @return
 *    a string containing the URL to view the video at the original provider's site
 */
function video_cck_livestream_embedded_link($video_code) {
  return 'http://www.livestream.com/' . $video_code;
}

/**
 * The embedded flash displaying the livestream video.
 */
function theme_video_cck_livestream_flash($embed, $width, $height, $autoplay, $options = array()) {
  static $count;
  $output = '';
  if ($embed) {
    $fullscreen = isset($options['fullscreen']) ? $options['fullscreen'] : variable_get('video_cck_livestream_full_screen', 1);
    $fullscreen_value = $fullscreen ? "true" : "false";
    $autoplay = isset($autoplay) ? $autoplay : (isset($options['autoplay']) ? $options['autoplay'] : variable_get('video_cck_livestream_autoplay', 0));
    $autoplay_value = $autoplay ? 'true' : 'false';

    // Micah's ugly hack.
    $embed = str_replace('?', '&amp;', $embed);
    $embed = str_replace('clipId', 'clip', $embed);

    // Livestream URLs from the rss api contain /ondemand/ instead of ?clip=
    // So we fix it
    $embed = str_replace('/ondemand/', '&amp;clip=', $embed);

    //    $id = isset($options['id']) ? $options['id'] : 'video_cck-livestream-flash-'. (++$count);
    $div_id = isset($options['div_id']) ? $options['div_id'] : 'video_cck-livestream-flash-wrapper-' . $count;
    $output .= <<<FLASH
      <div id="{<span class="php-variable">$div_id</span>}">

      <object width="{<span class="php-variable">$width</span>}" height="{<span class="php-variable">$height</span>}" id="lsplayer" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">
        <param name="movie" value="http://cdn.livestream.com/grid/LSPlayer.swf?channel={<span class="php-variable">$embed</span>}&amp;autoPlay={<span class="php-variable">$autoplay_value</span>}"></param>
        <param name="allowScriptAccess" value="always"></param>
        <param name="allowFullScreen" value="{<span class="php-variable">$fullscreen_value</span>}"></param>
        <embed name="lsplayer"
          wmode="transparent"
          src="http://cdn.livestream.com/grid/LSPlayer.swf?channel={<span class="php-variable">$embed</span>}&amp;autoPlay={<span class="php-variable">$autoplay_value</span>}"
          width="{<span class="php-variable">$width</span>}"
          height="{<span class="php-variable">$height</span>}"
          allowScriptAccess="always"
          allowFullScreen="{<span class="php-variable">$fullscreen_value</span>}"
          type="application/x-shockwave-flash">
        </embed>

      </object>
      </div>
FLASH;
  }
  return $output;
}

/**
 * hook video_cck_PROVIDER_thumbnail
 * returns the external url for a thumbnail of a specific video
 * TODO: make the args: ($embed, $field, $item), with $field/$item provided if we need it, but otherwise simplifying things
 *  @param $field
 *    the field of the requesting node
 *  @param $item
 *    the actual content of the field from the requesting node
 *  @return
 *    a URL pointing to the thumbnail
 */
function video_cck_livestream_thumbnail($field, $item, $formatter, $node, $width, $height) {
  global $base_url;
  global $base_path;
  global $theme;
  if ($item['data']['thumbnail']['url']) {
    return $item['data']['thumbnail']['url'];
  }
  else {
    return $base_url . $base_path . drupal_get_path('theme', $theme) . '/no-pre.png';
  }

  // Always return the larger image, since we're storing images locally.
  // $tn = larger image
  // return $tn;
}

/**
 * hook video_cck_PROVIDER_video
 * this actually displays the full/normal-sized video we want, usually on the default page view
 *  @param $embed
 *    the video code for the video to embed
 *  @param $width
 *    the width to display the video
 *  @param $height
 *    the height to display the video
 *  @param $field
 *    the field info from the requesting node
 *  @param $item
 *    the actual content from the field
 *  @return
 *    the html of the embedded video
 */
function video_cck_livestream_video($embed, $width, $height, $field, $item, $autoplay, $options = array()) {
  $output = theme('video_cck_livestream_flash', $embed, $width, $height, $autoplay, $options);
  return $output;
}

/**
 * hook video_cck_PROVIDER_preview
 * this actually displays the preview-sized video we want, commonly for the teaser
 *  @param $embed
 *    the video code for the video to embed
 *  @param $width
 *    the width to display the video
 *  @param $height
 *    the height to display the video
 *  @param $field
 *    the field info from the requesting node
 *  @param $item
 *    the actual content from the field
 *  @return
 *    the html of the embedded video
 */
function video_cck_livestream_preview($embed, $width, $height, $field, $item, $autoplay, $options = array()) {
  $output = theme('video_cck_livestream_flash', $embed, $width, $height, $autoplay, $options);
  return $output;
}

/**
 * Implementation of hook_emfield_subtheme.
 */
function video_cck_livestream_emfield_subtheme() {
  return array(
    'video_cck_livestream_flash' => array(
      'arguments' => array(
        'embed' => NULL,
        'width' => NULL,
        'height' => NULL,
        'autoplay' => NULL,
      ),
      'file' => 'providers/livestream.inc',
    ),
  );
}

Functions

Namesort descending Description
theme_video_cck_livestream_flash The embedded flash displaying the livestream video.
video_cck_livestream_data hook emfield_PROVIDER_data
video_cck_livestream_embedded_link hook video_cck_PROVIDER_embedded_link($video_code) returns a link to view the video at the provider's site
video_cck_livestream_emfield_subtheme Implementation of hook_emfield_subtheme.
video_cck_livestream_extract hook video_cck_PROVIDER_extract this is called to extract the video code from a pasted URL or embed code.
video_cck_livestream_info hook video_cck_PROVIDER_info this returns information relevant to a specific 3rd party video provider
video_cck_livestream_preview hook video_cck_PROVIDER_preview this actually displays the preview-sized video we want, commonly for the teaser
video_cck_livestream_request this is a wrapper for video_cck_request_xml that includes livestream's api key
video_cck_livestream_rss
video_cck_livestream_settings hook video_cck_PROVIDER_settings this should return a subform to be added to the video_cck_settings() admin settings page. note that a form field will already be provided, at $form['PROVIDER'] (such as $form['livestream']) so if…
video_cck_livestream_thumbnail hook video_cck_PROVIDER_thumbnail returns the external url for a thumbnail of a specific video TODO: make the args: ($embed, $field, $item), with $field/$item provided if we need it, but otherwise simplifying things
video_cck_livestream_video hook video_cck_PROVIDER_video this actually displays the full/normal-sized video we want, usually on the default page view

Constants