PageRenderTime 27ms CodeModel.GetById 15ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/src/Zend/Media/Asf/Object/ScriptCommand.php

http://php-reader.googlecode.com/
PHP | 183 lines | 85 code | 14 blank | 84 comment | 6 complexity | 976bf39f559df46b4394238cc7fdea9e MD5 | raw file
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 * It is also available through the world-wide-web at this URL:
 10 * http://framework.zend.com/license/new-bsd
 11 * If you did not receive a copy of the license and are unable to
 12 * obtain it through the world-wide-web, please send an email
 13 * to license@zend.com so we can send you a copy immediately.
 14 *
 15 * @category   Zend
 16 * @package    Zend_Media
 17 * @subpackage ASF
 18 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) 
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 * @version    $Id: ScriptCommand.php 177 2010-03-09 13:13:34Z svollbehr $
 21 */
 22
 23/**#@+ @ignore */
 24require_once 'Zend/Media/Asf/Object.php';
 25/**#@-*/
 26
 27/**
 28 * The <i>Script Command Object</i> provides a list of type/parameter pairs of
 29 * strings that are synchronized to the ASF file's timeline. Types can include
 30 * URL or FILENAME values. Other type values may also be freely defined and
 31 * used. The semantics and treatment of this set of types are defined by the
 32 * local implementations. The parameter value is specific to the type field. You
 33 * can use this type/parameter pairing for many purposes, including sending URLs
 34 * to be launched by a client into an HTML frame (in other words, the URL type)
 35 * or launching another ASF file for the chained continuous play of audio or
 36 * video presentations (in other words, the FILENAME type). This object is also
 37 * used as a method to stream text, as well as to provide script commands that
 38 * you can use to control elements within the client environment.
 39 *
 40 * @category   Zend
 41 * @package    Zend_Media
 42 * @subpackage ASF
 43 * @author     Sven Vollbehr <sven@vollbehr.eu>
 44 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) 
 45 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 46 * @version    $Id: ScriptCommand.php 177 2010-03-09 13:13:34Z svollbehr $
 47 */
 48final class Zend_Media_Asf_Object_ScriptCommand extends Zend_Media_Asf_Object
 49{
 50    /** @var string */
 51    private $_reserved;
 52
 53    /** @var Array */
 54    private $_commands = array();
 55
 56    /**
 57     * Constructs the class with given parameters and reads object related data
 58     * from the ASF file.
 59     *
 60     * @param Zend_Io_Reader $reader  The reader object.
 61     * @param Array          $options The options array.
 62     */
 63    public function __construct($reader = null, &$options = array())
 64    {
 65        parent::__construct($reader, $options);
 66
 67        if ($reader === null) {
 68            return;
 69        }
 70
 71        $this->_reserved = $this->_reader->readGuid();
 72        $commandsCount = $this->_reader->readUInt16LE();
 73        $commandTypesCount = $this->_reader->readUInt16LE();
 74        $commandTypes = array();
 75        for ($i = 0; $i < $commandTypesCount; $i++) {
 76            $commandTypeNameLength = $this->_reader->readUInt16LE();
 77            $commandTypes[] = iconv
 78                ('utf-16le', $this->getOption('encoding'),
 79                 $this->_reader->readString16($commandTypeNameLength * 2));
 80        }
 81        for ($i = 0; $i < $commandsCount; $i++) {
 82            $command = array
 83                ('presentationTime' => $this->_reader->readUInt32LE(),
 84                 'type' => $commandTypes[$this->_reader->readUInt16LE()]);
 85            $commandNameLength = $this->_reader->readUInt16LE();
 86            $command['name'] = iconv
 87                ('utf-16le', $this->getOption('encoding'),
 88                 $this->_reader->readString16($commandNameLength * 2));
 89            $this->_commands[] = $command;
 90        }
 91    }
 92
 93    /**
 94     * Returns an array of index entries. Each entry consists of the following
 95     * keys.
 96     *
 97     *   o presentationTime -- Specifies the presentation time of the command,
 98     *     in milliseconds.
 99     *
100     *   o type -- Specifies the type of this command.
101     *
102     *   o name -- Specifies the name of this command.
103     *
104     * @return Array
105     */
106    public function getCommands() 
107    {
108        return $this->_commands; 
109    }
110
111    /**
112     * Sets the array of index entries. Each entry is to consist of the
113     * following keys.
114     *
115     *   o presentationTime -- Specifies the presentation time of the command,
116     *     in milliseconds.
117     *
118     *   o type -- Specifies the type of this command.
119     *
120     *   o name -- Specifies the name of this command.
121     *
122     * @param Array $commands The array of index entries.
123     */
124    public function setCommands($commands) 
125    {
126        $this->_commands = $commands; 
127    }
128
129    /**
130     * Writes the object data.
131     *
132     * @param Zend_Io_Writer $writer The writer object.
133     * @return void
134     */
135    public function write($writer)
136    {
137        require_once 'Zend/Io/StringWriter.php';
138        
139        $commandTypes = array();
140        foreach ($this->_commands as $command) {
141            if (!in_array($command['type'], $commandTypes)) {
142                $commandTypes[] = $command['type'];
143            }
144        }
145
146        $commandTypesCount = count($commandTypes);
147        $commandTypesWriter = new Zend_Io_StringWriter();
148        for ($i = 0; $i < $commandTypesCount; $i++) {
149            $commandTypesWriter
150                ->writeUInt16LE
151                    (strlen($commandType = iconv
152                     ($this->getOption('encoding'), 'utf-16le',
153                        $commandTypes[$i])) / 2)
154                ->write($commandType);
155        }
156
157        $commandsCount = count($this->_commands);
158        $commandsWriter = new Zend_Io_StringWriter();
159        for ($i = 0; $i < $commandsCount; $i++) {
160            $commandsWriter
161                ->writeUInt32LE($this->_commands[$i]['presentationTime'])
162                ->writeUInt16LE
163                    (array_search($this->_commands[$i]['type'], $commandTypes))
164                ->writeUInt16LE
165                    (strlen($command = iconv
166                     ($this->getOption('encoding'), 'utf-16le',
167                        $this->_commands[$i]['name'])) / 2)
168                ->write($command);
169        }
170
171        $this->setSize
172            (24 /* for header */ + 20 + $commandTypesWriter->getSize() +
173             $commandsWriter->getSize());
174
175        $writer->writeGuid($this->getIdentifier())
176               ->writeInt64LE($this->getSize())
177               ->writeGuid($this->_reserved)
178               ->writeUInt16LE($commandsCount)
179               ->writeUInt16LE($commandTypesCount)
180               ->write($commandTypesWriter->toString())
181               ->write($commandsWriter->toString());
182    }
183}