rats_job.php

00001 <?php
00002 # Basic RATS job flow test.
00003 # Uses the RATS PHP Client to upload a src file, create a transcoding job for it, wait for the job to finish and process the results.
00004 # Note that this is made to be run from cmd line, and only for demo purposes. In a live environment, you will need to improve things like I/O buffering, exception handling...
00005 #
00006 # Copyright (C) 2008 rambla.be
00007 #
00008 # Licensed under the Apache License, Version 2.0 (the "License");
00009 # you may not use this file except in compliance with the License.
00010 # You may obtain a copy of the License at
00011 #
00012 #      http://www.apache.org/licenses/LICENSE-2.0
00013 #
00014 # Unless required by applicable law or agreed to in writing, software
00015 # distributed under the License is distributed on an "AS IS" BASIS,
00016 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00017 # See the License for the specific language governing permissions and
00018 # limitations under the License.
00019 
00020 require_once '../RawsClient/Raws/Rats.php';
00021 # transient states -> processing not complete
00022 define("REQUEST_RECEIVED", 1);
00023 define("IMPORT_IN_PROGRESS", 2);
00024 define("IMPORT_SUCCEEDED", 3);
00025 define("TRANS_IN_PROGRESS", 4);
00026 define("TRANS_SUCCEEDED", 5);
00027 define("EXPORT_IN_PROGRESS", 6);
00028 # final states
00029 define("FINISHED", 7);
00030 define("IMPORT_FAILED", 8);
00031 define("TRANS_FAILED", 9);
00032 define("EXPORT_FAILED", 10);
00033 # fixed data
00034 define('BASE', 'http://rats.enc01.rambla.be/');
00035 define('SRC_FILENAME', 'test_one_srcfile.mp4'); # src_location
00036 define('TGT_FILENAME', 'test_one'); # tgt_location
00037 
00038 # Provide your own credentials here
00039 define('USER', 'xxx');
00040 define('PWD', 'xxx');
00041 # provide a path to your local file to be used as transcoding src
00042 define('LOCAL_FILE', '../test_resources/bla.mp4');
00043 # you should specify your transcoding format ID here
00044 define('FORMAT_PROFILE', ''); # E.g. "17"
00045 # you should specify your output profile ID here
00046 define('OUTPUT_PROFILE', ''); # E.g. "10"
00047 
00048 try {
00049   # Create a Rass instance, passing it your login credentials and the base service uri
00050   $rats = new Rats(USER, PWD, BASE);
00051 
00052   # upload the src file, creating an "src" instance
00053   $src_entry = $rats->createSrc(SRC_FILENAME, LOCAL_FILE);
00054   $src_location = $src_entry->id->text; # uri to this src instance
00055   $src_filename = $src_entry->content->params->filename->text; # filename for this src on the RATS svr
00056   echo "\nUploaded a src file: \n";
00057   echo "src location: $src_location\n";
00058 
00059   # create a job, using the filename of the "src" file and a fixed format + output profile
00060   $rats = new Rats(USER, PWD, BASE);
00061   # NOTE: always use the src_filename from the src entry (returned by RATS when the src is created) 
00062   #       since RATS may have modified the original src filename (if a file with the same name already exists)
00063   $job_entry = $rats->createJobForExistingSrc(OUTPUT_PROFILE, FORMAT_PROFILE, $src_entry->content->params->filename->text, TGT_FILENAME, "40%", null, 50, null);
00064 
00065   $job_location = $job_entry->id->text; # uri to this job instance
00066   $job_id = $job_entry->content->params->id->text; # id for this job on the RATS svr
00067   echo "\nCreated a job: \n";
00068   echo "job location: $job_location\n";
00069   
00070   echo "\nWaiting for the job to be done: \n";
00071   # run a loop, polling the created job until it is finished
00072   while (1) {
00073     # retrieve the job entry
00074     $rats = new Rats(USER, PWD, BASE);
00075     $job_entry = $rats->getJobEntry($job_location);
00076     echo "Polling job " . $job_entry->content->params->id->text . ". Status= " . $job_entry->content->params->status->text . "\n";
00077     $status = (int)$job_entry->content->params->status->text; # current status
00078     # check on the status
00079     if ($status >= FINISHED) {
00080       if ($status == FINISHED) {
00081         echo "Job has succeeded, trying to retrieve the public URI for the exported file...\n";
00082         # retrieve the URI by which the transcoded file is available on the Rambla CDN
00083         $xml_entry =  $job_entry->getXML(); # get the complete entry in xml format
00084         $sxml_entry = simplexml_load_string($xml_entry);
00085         # parse the xml, looking for <public_uri> element inside <entry><reports><report><job action="export">
00086         foreach ($sxml_entry->children('http://rambla.be/raws/ns-metadata/1.0') as $reports) {
00087           foreach($reports->report->job as $job) {
00088             if ($job->attributes()->action[0] == "export") {
00089               echo "File on the CDN is accessible via URI: " . $job->public_uri[0] . "\n";
00090             }
00091           }
00092         }
00093         break;  
00094       }
00095       else {
00096         echo "Job has failed with status code = " . $job_entry->content->params->status->text . "\n";
00097         break;
00098       }
00099     }    
00100     sleep(20);
00101   }
00102   
00103   # (optional) src not longer needed on the RATS server, delete it
00104   $rats = new Rats(USER, PWD, BASE);
00105   $rats->deleteSrcFromFilename($src_filename);
00106   echo "\nSrc file has been deleted from the RATS server.";
00107 
00108 }
00109 catch(Zend_Gdata_App_Exception $e) {
00110     # Report the exception to the user
00111     echo "\nCaught exception: " . get_class($e) . "\n";
00112     echo "Message: " . $e->getMessage() . "\n";
00113     # get the HTTP status code
00114     echo "HTTP Status Code: " . $e->getResponse()->getStatus() . "\n";
00115     echo "Response Body with exception details: " . $e->getResponse()->getBody() . "\n";
00116     # get the raws:error elements
00117     $rawsResponse = Raws::parseRawsResponse($e->getResponse());
00118     echo "Raws Code: " . $rawsResponse->getCode() . "\n";
00119     echo "Raws Message: " . $rawsResponse->getMsg() . "\n";
00120     $reasons = $rawsResponse->getReasons();
00121     foreach ($reasons as $reason) {
00122       echo "Raws Reason: " . $reason . "\n";
00123     }
00124     $details = $rawsResponse->getDetails();
00125     foreach ($details as $key => $value) {
00126       echo "Raws Detail: " . $key . " -> " . $value . "\n";
00127     }
00128 }
00129 catch (Zend_Exception $e) {
00130     echo "Caught exception: " . get_class($e) . "\n";
00131     echo "Message: " . $e->getMessage() . "\n";
00132 }

Generated on Mon Mar 28 15:03:20 2011 for RawsClient PHP by  doxygen 1.5.3