[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/video/plugins/video_zencoder/ -> video_zencoder.module (source)

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Provides wrapper functions for the s3 amazon webservices.
   6   * @todo
   7   * - cleand up the _video_zencoder_postback_jobs() function
   8   * - Add to select random thumbnails to the download image.
   9   */
  10  defined('VIDEO_RENDERING_PENDING') ||
  11      define('VIDEO_RENDERING_PENDING', 1);
  12  defined('VIDEO_RENDERING_ACTIVE') ||
  13      define('VIDEO_RENDERING_ACTIVE', 5);
  14  defined('VIDEO_RENDERING_COMPLETE') ||
  15      define('VIDEO_RENDERING_COMPLETE', 10);
  16  defined('VIDEO_RENDERING_FAILED') ||
  17      define('VIDEO_RENDERING_FAILED', 20);
  18  
  19  /**
  20   * Implementation of hook_menu().
  21   */
  22  function video_zencoder_menu() {
  23    $items = array();
  24    $items['postback/jobs'] = array(
  25      'page callback' => '_video_zencoder_postback_jobs',
  26      'access callback' => TRUE,
  27      'type' => MENU_CALLBACK,
  28    );
  29    return $items;
  30  }
  31  
  32  /**
  33   * This will handle Zencoder postback once video conversion is completed
  34   *
  35   * @TODO move to separate file
  36   */
  37  function _video_zencoder_postback_jobs() {
  38    ignore_user_abort(TRUE);
  39    
  40    $data = file_get_contents('php://input');
  41    
  42    if (empty($data)) {
  43      watchdog('zencoder', 'Empty postback received from the Zencoder Transcoding servers.', array(), WATCHDOG_WARNING);
  44      drupal_not_found();
  45      return;
  46    }
  47  
  48    $result = json_decode($data);
  49  
  50    watchdog('zencoder', 'Postback received from the Zencoder Transcoding servers.<br/><pre>@data</pre>', array('@data' => print_r($result, TRUE)), WATCHDOG_DEBUG);
  51  
  52    $jobid = $result->job->id;
  53    $zc_job_state = trim($result->job->state);
  54    $zc_output_state = trim($result->output->state);
  55    $state = 0;
  56  
  57    if ($zc_output_state == 'finished' && $zc_job_state == 'finished') {
  58      $state = VIDEO_RENDERING_COMPLETE;
  59    }
  60    elseif ($zc_output_state == 'failed' || $zc_job_state == 'failed') {
  61      $state = VIDEO_RENDERING_FAILED;
  62    }
  63    elseif ($zc_job_state == 'processing') {
  64      // This state means that one output has finished, but more outputs have still to be generated.
  65      return;
  66    }
  67  
  68    module_load_include('inc', 'video_zencoder', 'transcoders/video_zencoder');
  69    $zc = new video_zencoder();
  70    $videodb = db_fetch_object(db_query('SELECT * FROM {video_zencoder} WHERE jobid = %d', $jobid));
  71  
  72    if ($videodb == NULL) {
  73      watchdog('zencoder', 'Received postback from Zencoder for unknown job @jobid, ignoring.', array('@jobid' => $jobid), WATCHDOG_ERROR);
  74      return;
  75    }
  76    if ($videodb->status == VIDEO_RENDERING_COMPLETE) {
  77      watchdog('zencoder', 'Received postback from Zencoder for job @jobid, which was already finished, ignoring.', array('@jobid' => $jobid), WATCHDOG_WARNING);
  78      return;
  79    }
  80    
  81    if ($state == VIDEO_RENDERING_COMPLETE) {
  82      db_query('UPDATE {node} SET status = %d WHERE nid = %d', 1, $videodb->nid);
  83  
  84      // Fetch the thumbnails
  85      $video_thumb_path = video_thumb_path($videodb);
  86      $number_of_thumbs = variable_get('video_thumbs', 5);
  87      $thumbsdownloaded = 0;
  88      for ($i = 0; $i < $number_of_thumbs; $i++) {
  89        $thumbfile = $video_thumb_path .'/'. $videodb->fid .'_'. sprintf('%04d', $i) .'.png';
  90        
  91        if (video_s3_get_object_info($thumbfile)) {
  92          if (video_s3_get_object($thumbfile, $thumbfile)) {
  93            $thumbsdownloaded++;
  94          }
  95          else {
  96            watchdog('zencoder', 'Could not download @thumbfile from Amazon S3 to the local file system.', array('@thumbfile' => $thumbfile), WATCHDOG_ERROR);
  97          }
  98        }
  99      }
 100  
 101      if ($thumbsdownloaded > 0) {
 102        // Update the thumbnail of the video in the node that uses this file
 103        $node = node_load($videodb->nid);
 104        $hasupdates = FALSE;
 105        $fieldnames = array_keys(filefield_get_field_list($node->type));
 106  
 107        // Loop through all file fields and check if the file is the file that was just converted
 108        foreach ($fieldnames as $fieldname) {
 109          if (!empty($node->$fieldname)) {
 110            foreach ($node->$fieldname as &$element) {
 111              if ($element != NULL && $element['fid'] == $videodb->fid) {
 112                $element['data']['video_thumb'] = $video_thumb_path .'/'. $videodb->fid .'_0000.png';
 113                $hasupdates = TRUE;
 114              }
 115            }
 116          }
 117        }
 118  
 119        if ($hasupdates) {
 120          node_save($node);
 121        }
 122      }
 123  
 124      $zc->change_status($videodb->vid, VIDEO_RENDERING_COMPLETE);
 125      watchdog('zencoder', 'Updated the Zencoder job @id to state @state.', array('@id' => $jobid, '@state' => $zc_output_state), WATCHDOG_INFO);
 126    }
 127    elseif ($state == VIDEO_RENDERING_FAILED) {
 128      $zc->change_status($videodb->vid, VIDEO_RENDERING_FAILED);
 129      
 130      $errormsg = 'not given';
 131      if (!empty($result->output->error_message)) {
 132      $errormsg = $result->output->error_message;
 133      }
 134      $errorlink = '#';
 135      if (!empty($result->output->error_link)) {
 136        $errorlink = $result->output->error_link;
 137      }
 138      
 139      watchdog('zencoder', 'Zencoder job @jobid failed to convert video.<br/>Error message: @errormessage<br/><a href="@error-link">More information about this error</a>',
 140        array('@jobid' => $jobid, '@errormessage' => $errormsg, '@errorlink' => $errorlink), WATCHDOG_ERROR);
 141    }
 142  }
 143  
 144  /**
 145   * Implementation of hook_mail().
 146   */
 147  function video_zencoder_mail($key, &$message, $params) {
 148    $language = $message['language'];
 149    $message['subject'] .= 'Zencoder Registration Details for Drupal Video';
 150    $message['body'][] = video_zencoder_mail_default($params);
 151  }
 152  
 153  function video_zencoder_mail_default($params) {
 154    return t(
 155  'Welcome to Zencoder for Drupal
 156  -------------------------------
 157  
 158  Your account has been created and is ready to start processing.
 159  
 160  Your account details are as below.
 161  
 162  E-mail address (login): @email
 163  API Key : @api_key
 164  Password : @password
 165  
 166  * Login URL: https://app.zencoder.com/login
 167  
 168  You can get help at the following places:
 169  
 170  * Our chat room at http://zencoder.com/chat
 171  * Customer forums at https://help.zencoder.com/forums
 172  * The help desk at https://help.zencoder.com/tickets/new
 173  
 174  We\'d love to hear from you. Let us know how we can help. Thanks!
 175  
 176  Thanks,
 177  -Zencoder for Drupal Team', array('@api_key' => $params['api_key'], '@password' => $params['password']));
 178  }
 179  


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7