/**
  * Decomposes a file into flv tags while reading. Searches for keyframes
  * 
  * @param string $filename
  * @return Yamdi_InputStream
  */
 protected function read($filename)
 {
     $stream = parent::read($filename);
     $this->filepositions = array();
     $this->times = array();
     $tag = new Yamdi_FlvTag();
     while (!$stream->isEnd()) {
         $tag_position = $stream->getPosition();
         /*
          * Tag
          */
         if (!$tag->read($stream)) {
             break;
         }
         if (!$tag->isValid()) {
             throw new Exception('Invalid tag found');
         }
         if ($tag->isVideo()) {
             if ($tag->checkIfKeyFrame($stream)) {
                 $this->filepositions[] = $tag_position;
                 $this->times[] = $tag->getTimestamp() / 1000.0;
             }
         }
         $tag->skipTagBody($stream);
         // wind forward to next tag
         /*
          * Previous tag size
          */
         $tagSize = new Yamdi_FlvTagSize();
         if (!$tagSize->read($stream)) {
             break;
         }
     }
     return null;
     //nothing else to read
 }
 /**
  * Decomposes a file into flv tags while reading. Searches for given keyframe
  * 
  * @param string $filename
  * @return Yamdi_InputStream
  */
 protected function read($filename)
 {
     $stream = parent::read($filename);
     /*
      * Looking for needed keyframe video tag
      */
     if ($this->metadata->keyframes !== null) {
         /*
          * Keyframes present in meta, getting position of needed tag
          */
         $filepositions = $this->metadata->keyframes->filepositions;
         if (isset($filepositions[$this->desiredKeyframeNumber])) {
             $this->videoTagPosition = $filepositions[$this->desiredKeyframeNumber];
         } else {
             $this->videoTagPosition = $filepositions[0];
         }
         /*
          * Reading tag to fetch block size
          */
         $stream->seek($this->videoTagPosition, SEEK_SET);
         $tag = new Yamdi_FlvTag();
         if (!$tag->read($stream)) {
             throw new Exception('Invalid tag fileposition');
         }
         if (!$tag->isValid()) {
             throw new Exception('Invalid tag found');
         }
         if (!$tag->isVideo()) {
             throw new Exception('Specified tag is not video');
         }
         if (!$tag->checkIfKeyFrame($stream)) {
             throw new Exception('Specified tag is not a keyframe');
         }
         $this->videoBlockSize = $tag->getDataSize() + 4 + 11;
     } else {
         /*
          * Keyframes absent in meta, reading tags until needed keyframe is found
          */
         $tag = new Yamdi_FlvTag();
         $keyframe_number = 0;
         while (!$stream->isEnd()) {
             $tag_position = $stream->getPosition();
             /*
              * Tag
              */
             if (!$tag->read($stream)) {
                 break;
             }
             if (!$tag->isValid()) {
                 throw new Exception('Invalid tag found');
             }
             if ($tag->isVideo()) {
                 if ($tag->checkIfKeyFrame($stream)) {
                     if ($this->videoTagPosition === null) {
                         // initializing with first tag position and size
                         $this->videoTagPosition = $tag_position;
                         $this->videoBlockSize = $tag->getDataSize() + 4 + 11;
                     }
                     if (++$keyframe_number == $this->desiredKeyframeNumber) {
                         $this->videoTagPosition = $tag_position;
                         //found keyframe
                         $this->videoBlockSize = $tag->getDataSize() + 4 + 11;
                         break;
                     }
                 }
             }
             $tag->skipTagBody($stream);
             // wind forward to next tag
             /*
              * Previous tag size
              */
             $tagSize = new Yamdi_FlvTagSize();
             if (!$tagSize->read($stream)) {
                 break;
             }
         }
     }
     return null;
     //nothing else to read
 }
 /**
  * @deprecated 
  */
 public function testFileRead()
 {
     /*
      * Here we need a real correct flv file
      */
     $stream = new Yamdi_InputStream(dirname(__FILE__) . '/../trailer_2.flv');
     /*
      * Constant header
      */
     $header = new Yamdi_FlvFileHeader();
     $header->read($stream);
     $this->assertEqual(true, $header->isValid());
     /*
      * Constant size of zero tag
      */
     $zeroTagSize = new Yamdi_FlvTagSize();
     $zeroTagSize->read($stream);
     $this->assertEqual(0, $zeroTagSize->size);
     // allways 0
     /*
      * First tag (should be meta)
      */
     $tag = new Yamdi_FlvTag();
     $tag->read($stream);
     $this->assertTrue($tag->isMeta());
     $metadata = new Yamdi_FlvMetadataBody();
     $metadata->read($tag->readTagBody($stream));
     /*
      * Previous tag size
      */
     $tagSize = new Yamdi_FlvTagSize();
     $tagSize->read($stream);
     while (!$stream->isEnd()) {
         $tag_position = $stream->getPosition();
         /*
          * Tag
          */
         if (!$tag->read($stream)) {
             break;
         }
         $this->assertTrue($tag->isValid());
         if ($tag->isVideo()) {
             if ($tag->checkIfKeyFrame($stream)) {
                 $new_filepositions[] = $tag_position;
                 $new_times[] = $tag->getTimestamp() / 1000.0;
             }
         }
         $tag->skipTagBody($stream);
         // wind forward to next tag
         /*
          * Previous tag size
          */
         $tagSize = new Yamdi_FlvTagSize();
         if (!$tagSize->read($stream)) {
             break;
         }
     }
     $this->assertTrue(is_array($new_filepositions));
     $this->assertTrue(count($new_filepositions) > 0);
     $this->assertTrue(is_array($new_times));
     $this->assertTrue(count($new_times) > 0);
     $this->assertEqual(count($new_times), count($new_filepositions));
     /*
      * Cleanup
      */
     $stream->close();
 }