protected function writeMetaBlock($stream) { $this->shiftMetaSize(); $this->metadataTag->write($stream); $stream->write($this->metadata->write()); $this->metadataTagSize->write($stream); }
public function testReadWrite() { $bytes = chr(0) . chr(0) . chr(0) . chr(1); // 32-bit big-endian integer /* preparing mock streams */ $inputStub = $this->getMock('Yamdi_InputStream', array(), array(''), '', false, false); $inputStub->expects($this->any())->method('read')->will($this->returnValue($bytes)); $outputMock = $this->getMock('Yamdi_OutputStream', array(), array(''), '', false, false); $outputMock->expects($this->once())->method('write')->with($this->equalTo($bytes))->will($this->returnValue(4)); /* reading tag */ $tag = new Yamdi_FlvTagSize(); $tag->read($inputStub); $this->assertEquals(1, $tag->size); /* writing tag */ $tag->write($outputMock); }
public function testReadWrite() { $bytes = chr(0) . chr(0) . chr(0) . chr(1); // 32-bit big-endian integer /* preparing mock streams */ $inputStream = new MockYamdi_InputStream(); $inputStream->setReturnValue('read', $bytes); $outputStream = new MockYamdi_OutputStream(); $outputStream->setReturnValue('write', 4); $outputStream->expectOnce('write', array($bytes)); /* reading tag */ $tag = new Yamdi_FlvTagSize(); $tag->read($inputStream); $this->assertEqual($tag->size, 1); /* writing tag */ $tag->write($outputStream); }
/** * 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 testMetadataWriteback() { /* * File to read */ $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(); $tagBody = $tag->readTagBody($stream); $metadata->read($tagBody); $this->assertEqual(strlen($tagBody), strlen($metadata->write())); /* * Cleanup */ $stream->close(); }