You are here

function _video_zencoder_postback_jobs in Video 6.5

Same name and namespace in other branches
  1. 6.4 plugins/video_zencoder/video_zencoder.module \_video_zencoder_postback_jobs()
  2. 7 modules/video_zencoder/video_zencoder.module \_video_zencoder_postback_jobs()

This will handle Zencoder postback once video conversion is completed

@TODO move to separate file

1 string reference to '_video_zencoder_postback_jobs'
video_zencoder_menu in plugins/video_zencoder/video_zencoder.module
Implementation of hook_menu().

File

plugins/video_zencoder/video_zencoder.module, line 28
Provides wrapper functions for the s3 amazon webservices. @todo

Code

function _video_zencoder_postback_jobs() {
  ignore_user_abort(TRUE);
  $data = file_get_contents('php://input');
  if (empty($data)) {
    watchdog('zencoder', 'Empty postback received from the Zencoder Transcoding servers.', array(), WATCHDOG_WARNING);
    drupal_not_found();
    return;
  }
  $result = json_decode($data);
  watchdog('zencoder', 'Postback received from the Zencoder Transcoding servers.<br/><pre>@data</pre>', array(
    '@data' => print_r($result, TRUE),
  ), WATCHDOG_DEBUG);
  $jobid = $result->job->id;
  $zc_job_state = trim($result->job->state);
  $zc_output_state = trim($result->output->state);
  $state = 0;
  if ($zc_output_state == 'finished' && $zc_job_state == 'finished') {
    $state = VIDEO_RENDERING_COMPLETE;
  }
  elseif ($zc_output_state == 'failed' || $zc_job_state == 'failed') {
    $state = VIDEO_RENDERING_FAILED;
  }
  elseif ($zc_job_state == 'processing') {

    // This state means that one output has finished, but more outputs have still to be generated.
    return;
  }
  $zc = video_get_transcoder('video_zencoder');
  if ($zc == NULL) {
    watchdog('zencoder', 'Could not load Zencoder transcoder.', array(), WATCHDOG_CRITICAL);
    return;
  }
  $video = db_fetch_object(db_query('SELECT * FROM {video_zencoder} WHERE jobid = %d', $jobid));
  if ($video == NULL) {
    watchdog('zencoder', 'Received postback from Zencoder for unknown job @jobid, ignoring.', array(
      '@jobid' => $jobid,
    ), WATCHDOG_ERROR);
    return;
  }
  if ($video->status == VIDEO_RENDERING_COMPLETE) {
    watchdog('zencoder', 'Received postback from Zencoder for job @jobid, which was already finished, ignoring.', array(
      '@jobid' => $jobid,
    ), WATCHDOG_WARNING);
    return;
  }
  if ($state == VIDEO_RENDERING_COMPLETE) {
    db_query('UPDATE {node} SET status = %d WHERE nid = %d', 1, $video->nid);

    // Fetch the thumbnails
    $video_thumb_path = video_thumb_path($video);
    $number_of_thumbs = variable_get('video_thumbs', 5);
    $thumbsdownloaded = 0;
    for ($i = 0; $i < $number_of_thumbs; $i++) {
      $thumbfile = $video_thumb_path . '/' . $video->fid . '_' . sprintf('%04d', $i) . '.png';
      $thumbfiles3 = ltrim($thumbfile, '/');
      if (video_s3_get_object_info($thumbfiles3)) {
        if (video_s3_get_object($thumbfiles3, $thumbfile)) {
          $thumbsdownloaded++;
        }
        else {
          watchdog('zencoder', 'Could not download @thumbfile from Amazon S3 to the local file system.', array(
            '@thumbfile' => $thumbfiles3,
          ), WATCHDOG_ERROR);
        }
      }
    }
    if ($thumbsdownloaded > 0) {

      // Update the thumbnail of the video in the node that uses this file
      $node = node_load($video->nid);
      $hasupdates = FALSE;
      $fieldnames = array_keys(filefield_get_field_list($node->type));

      // Loop through all file fields and check if the file is the file that was just converted
      foreach ($fieldnames as $fieldname) {
        if (!empty($node->{$fieldname})) {
          foreach ($node->{$fieldname} as &$element) {
            if ($element != NULL && $element['fid'] == $video->fid) {
              $element['data']['video_thumb'] = $video_thumb_path . '/' . $video->fid . '_0000.png';
              $hasupdates = TRUE;
            }
          }
        }
      }
      if ($hasupdates) {
        node_save($node);
      }
    }
    $zc
      ->change_status($video, VIDEO_RENDERING_COMPLETE);
    watchdog('zencoder', 'Updated the Zencoder job @id to state @state.', array(
      '@id' => $jobid,
      '@state' => $zc_output_state,
    ), WATCHDOG_INFO);
  }
  elseif ($state == VIDEO_RENDERING_FAILED) {
    $zc
      ->change_status($video, VIDEO_RENDERING_FAILED);
    $errormsg = 'not given';
    $errorlink = '#';
    if (!empty($result->output->error_message)) {
      $errormsg = $result->output->error_message;
    }
    if (!empty($result->output->error_link)) {
      $errorlink = $result->output->error_link;
    }
    watchdog('zencoder', 'Zencoder job @jobid failed to convert video.<br/>Error message: @errormessage<br/><a href="@error-link">More information about this error</a>', array(
      '@jobid' => $jobid,
      '@errormessage' => $errormsg,
      '@errorlink' => $errorlink,
    ), WATCHDOG_ERROR);
  }
}