Skip to content

Commit

Permalink
Fixed issue #12
Browse files Browse the repository at this point in the history
Put leading uppercase letter in error messages
  • Loading branch information
Chung Leong committed May 7, 2014
1 parent 6055ecf commit ecba39a
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 31 deletions.
81 changes: 53 additions & 28 deletions av.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ int av_copy_metadata(AVDictionary **dict, zval *options TSRMLS_DC) {
}
}
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "metadata should be an array");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Metadata should be an array");
return FALSE;
}

Expand Down Expand Up @@ -1083,12 +1083,12 @@ PHP_FUNCTION(av_stream_open)
if(Z_TYPE_P(z_id) == IS_STRING) {
media_type = av_get_stream_type(Z_STRVAL_P(z_id) TSRMLS_CC);
if(media_type < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "parameter 2 should be \"video\", \"audio\", \"subtitle\", or an stream index");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Parameter 2 should be \"video\", \"audio\", \"subtitle\", or an stream index");
return;
}
stream_index = av_find_best_stream(file->format_cxt, media_type, -1, -1, &codec, 0);
if(stream_index < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot find a stream of type '%s'", Z_STRVAL_P(z_id));
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot find a stream of type '%s'", Z_STRVAL_P(z_id));
return;
}
} else if(Z_TYPE_P(z_id) == IS_LONG || Z_TYPE_P(z_id) == IS_DOUBLE) {
Expand All @@ -1099,11 +1099,11 @@ PHP_FUNCTION(av_stream_open)
media_type = stream->codec->codec_type;
codec = avcodec_find_decoder(stream->codec->codec_id);
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "stream index must be between 0 and %d", file->stream_count);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Stream index must be between 0 and %d", file->stream_count);
return;
}
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "parameter 2 should be \"video\", \"audio\", \"subtitle\", or an stream index");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Parameter 2 should be \"video\", \"audio\", \"subtitle\", or an stream index");
return;
}
if(stream_index < (int32_t) file->stream_count) {
Expand All @@ -1115,7 +1115,7 @@ PHP_FUNCTION(av_stream_open)
file->open_stream_count++;
ZEND_REGISTER_RESOURCE(return_value, strm, le_av_strm);
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "stream #%d is already open", stream_index);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Stream #%d is already open", stream_index);
}
return;
}
Expand All @@ -1124,15 +1124,15 @@ PHP_FUNCTION(av_stream_open)
if(Z_TYPE_P(z_id) == IS_STRING) {
media_type = av_get_stream_type(Z_STRVAL_P(z_id) TSRMLS_CC);
if(media_type < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "parameter 2 should be \"video\", \"audio\", \"subtitle\"");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Parameter 2 should be \"video\", \"audio\", \"subtitle\"");
return;
}
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "parameter 2 should be \"video\", \"audio\", \"subtitle\"");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Parameter 2 should be \"video\", \"audio\", \"subtitle\"");
return;
}
if(file->flags & AV_FILE_HEADER_WRITTEN) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot add additional streams as encoding has already begun");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot add additional streams as encoding has already begun");
return;
}
stream_index = file->stream_count;
Expand Down Expand Up @@ -1194,25 +1194,25 @@ PHP_FUNCTION(av_stream_open)

if(av_get_element_string(z_options, "codec", &codec_name)) {
if(!av_find_codec(codec_name, &codec, NULL)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to find codec '%s'", codec_name);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find codec '%s'", codec_name);
return;
} else if(!codec) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "no encoding capability for codec '%s'", codec_name);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "No encoding capability for codec '%s'", codec_name);
return;
}
} else {
enum AVCodecID codec_id = av_guess_codec((AVOutputFormat *) file->output_format, NULL, NULL, NULL, media_type);
codec = avcodec_find_encoder(codec_id);
if(!codec) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to find codec");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find codec");
return;
}
}

// add the stream
stream = avformat_new_stream(file->format_cxt, codec);
if(!stream) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to open stream");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open stream");
return;
}

Expand All @@ -1237,7 +1237,7 @@ PHP_FUNCTION(av_stream_open)
if(pixel_format_name) {
pix_fmt = av_get_pix_fmt(pixel_format_name);
if(pix_fmt == AV_PIX_FMT_NONE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid pixel format '%s'", pixel_format_name);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid pixel format '%s'", pixel_format_name);
return;
}
} else {
Expand Down Expand Up @@ -2019,7 +2019,7 @@ static int av_write_file_header(av_file *file) {
if(avformat_write_header(file->format_cxt, NULL) < 0) {
if(!(file->flags & AV_FILE_HEADER_ERROR_ENCOUNTERED)) {
TSRMLS_FETCH();
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error encountered writing file header");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error encountered writing file header");
file->flags |= AV_FILE_HEADER_ERROR_ENCOUNTERED;
}
return FALSE;
Expand Down Expand Up @@ -2265,7 +2265,7 @@ static int av_encode_pcm_from_zval(av_stream *strm, zval *buffer, double time TS
uint32_t src_samples_remaining;

if(Z_TYPE_P(buffer) != IS_STRING) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "audio data must be contained in a string");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Audio data must be contained in a string");
return FALSE;
}

Expand Down Expand Up @@ -2372,36 +2372,36 @@ static int av_encode_subtitle_from_zval(av_stream *strm, zval *buffer, double ti
uint32_t i;

if(Z_TYPE_P(buffer) != IS_ARRAY) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "subtitle data must be contained in an array");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Subtitle data must be contained in an array");
return FALSE;
}
if(!av_get_element_double(buffer, "start", &start_time) || !av_get_element_double(buffer, "end", &end_time)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "subtitle data must contain start and end time");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Subtitle data must contain start and end time");
return FALSE;
}
if(!av_get_element_hash(buffer, "rects", &ht)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "subtitle data must contain rects");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Subtitle data must contain rects");
return FALSE;
}
for(p = ht->pListHead; p; p = p->pListNext) {
zval **p_element = p->pData;
if(!av_get_element_resource(*p_element, "image", &z_image)
&& !av_get_element_string(*p_element, "text", &text)
&& !av_get_element_string(*p_element, "ass", &ass)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "subtitle rectangle must contain image, text, or ass");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Subtitle rectangle must contain image, text, or ass");
return FALSE;
}
if(z_image) {
image = (gdImagePtr) zend_fetch_resource(&z_image TSRMLS_CC, -1, NULL, NULL, 1, le_gd);
if(!image || image->trueColor) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "subtitle image must use a palette");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Subtitle image must use a palette");
return FALSE;
}
if(!av_get_element_long(*p_element, "x", &x)
|| !av_get_element_long(*p_element, "y", &y)
|| !av_get_element_long(*p_element, "width", &width)
|| !av_get_element_long(*p_element, "height", &height)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "subtitle rectangle must contain x, y, width, and height");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Subtitle rectangle must contain x, y, width, and height");
return FALSE;
}
}
Expand Down Expand Up @@ -2535,7 +2535,11 @@ PHP_FUNCTION(av_stream_write_image)
av_set_log_level(TSRMLS_C);

if(strm->codec->type != AVMEDIA_TYPE_VIDEO) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "not a video stream");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not a video stream");
return;
}
if(!(strm->file->flags & AV_FILE_WRITE)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not a writable stream");
return;
}
if(av_encode_image_from_gd(strm, image, time TSRMLS_CC)) {
Expand All @@ -2562,7 +2566,11 @@ PHP_FUNCTION(av_stream_write_pcm)
av_set_log_level(TSRMLS_C);

if(strm->codec->type != AVMEDIA_TYPE_AUDIO) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "not an audio stream");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not an audio stream");
return;
}
if(!(strm->file->flags & AV_FILE_WRITE)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not a writable stream");
return;
}
if(av_encode_pcm_from_zval(strm, z_buffer, time TSRMLS_CC)) {
Expand All @@ -2589,7 +2597,11 @@ PHP_FUNCTION(av_stream_write_subtitle)
av_set_log_level(TSRMLS_C);

if(strm->codec->type != AVMEDIA_TYPE_SUBTITLE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "not a subtitle stream");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not a subtitle stream");
return;
}
if(!(strm->file->flags & AV_FILE_WRITE)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not a writable stream");
return;
}
if(av_encode_subtitle_from_zval(strm, z_buffer, time TSRMLS_CC)) {
Expand Down Expand Up @@ -2618,9 +2630,14 @@ PHP_FUNCTION(av_stream_read_image)
av_set_log_level(TSRMLS_C);

if(strm->codec->type != AVMEDIA_TYPE_VIDEO) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "not a video stream");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not a video stream");
return;
}
if(!(strm->file->flags & AV_FILE_READ)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not a readable stream");
return;
}

if(av_decode_image_to_gd(strm, image, &time TSRMLS_CC)) {
if(z_time) {
zval_dtor(z_time);
Expand Down Expand Up @@ -2649,7 +2666,11 @@ PHP_FUNCTION(av_stream_read_pcm)
av_set_log_level(TSRMLS_C);

if(strm->codec->type != AVMEDIA_TYPE_AUDIO) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "not an audio stream");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not an audio stream");
return;
}
if(!(strm->file->flags & AV_FILE_READ)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not a readable stream");
return;
}
if(av_decode_pcm_to_zval(strm, z_buffer, &time TSRMLS_CC)) {
Expand Down Expand Up @@ -2680,7 +2701,11 @@ PHP_FUNCTION(av_stream_read_subtitle)
av_set_log_level(TSRMLS_C);

if(strm->codec->type != AVMEDIA_TYPE_SUBTITLE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "not a subtitle stream");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not a subtitle stream");
return;
}
if(!(strm->file->flags & AV_FILE_READ)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not a readable stream");
return;
}
if(av_decode_subtitle_to_zval(strm, z_subtitle, &time TSRMLS_CC)) {
Expand Down
22 changes: 22 additions & 0 deletions tests/bug-12.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Issue #12: Read from write stream test
--SKIPIF--
<?php
if(!in_array('avi', av_get_encoders())) print 'skip AVI encoder not avilable';
?>
--FILE--
<?php

$folder = dirname(__FILE__);
$filename = "test.avi";
$path = "$folder/$filename";

$file = av_file_open($path, "w");
$videoStream = av_stream_open($file, "video", array( "width" => 100, "height" => 100, "frame_rate" => 12, "bit_rate" => 1024 * 1000, "gop" => 0));

$image = imagecreatetruecolor(50, 50);
av_stream_read_image($videoStream, $image, $videoTime);

?>
--EXPECTREGEX--
.*Warning.*Not a readable stream.*
4 changes: 2 additions & 2 deletions tests/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function create() {
}

if(!$file || ($this->hasVideo && !$videoStream) || ($this->hasAudio && !$audioStream)) {
throw new Exception("Error opening $path for writing\n");
throw new Exception("Error opening {$this->path} for writing\n");
}

if($this->hasVideo) {
Expand Down Expand Up @@ -205,7 +205,7 @@ public function verify() {
}

if(!$file || ($this->hasVideo && !$videoStream) || ($this->hasAudio && !$audioStream)) {
throw new Exception("Error opening $path for writing\n");
throw new Exception("Error opening {$this->path} for writing\n");
}

if($this->hasVideo) {
Expand Down
24 changes: 24 additions & 0 deletions tests/mp3.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
MP3 test
--SKIPIF--
<?php
if(!in_array('mp3', av_get_encoders())) print 'skip MP3 encoder not avilable';
?>
--FILE--
<?php

require("helpers.php");

$folder = dirname(__FILE__);
$filename = "test.mp3";

$testVideo = new TestVideo("$folder/$filename", 640, 480, 24, 5.0, false, true);
$testVideo->create();
$testVideo->verify();
$testVideo->delete();

echo "OK\n";

?>
--EXPECT--
OK
24 changes: 24 additions & 0 deletions tests/ogg.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
OGG test
--SKIPIF--
<?php
if(!in_array('ogg', av_get_encoders())) print 'skip OGG encoder not avilable';
?>
--FILE--
<?php

require("helpers.php");

$folder = dirname(__FILE__);
$filename = "test.ogg";

$testVideo = new TestVideo("$folder/$filename", 640, 480, 24, 5.0, false, true);
$testVideo->create();
$testVideo->verify();
$testVideo->delete();

echo "OK\n";

?>
--EXPECT--
OK
2 changes: 1 addition & 1 deletion tests/webm.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require("helpers.php");
$folder = dirname(__FILE__);
$filename = "test.webm";

$testVideo = new TestVideo("$folder/$filename", 640, 480, 24, 5.0);
$testVideo = new TestVideo("$folder/$filename", 640, 480, 24, 5.0, true, false);
$testVideo->create();
$testVideo->verify();
$testVideo->delete();
Expand Down

0 comments on commit ecba39a

Please sign in to comment.