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