Problems installing ffmpeg: warning: rpmts_HdrFromFdno: V3 DSA signature: NOKEY, key ID X

Here is how to install ffmpeg:

1 - Create a file named “dag.repo” (no quotes) in “/etc/yum.repos.d”
2 - Copy and paste these lines in it:

[dag]
name=Dag RPM Repository for Red Hat Enterprise Linux
baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag
gpgcheck=0
enabled=1

You will need a newline at the end of the file.

3 - Then run “yum install ffmpeg ffmpeg-devel” (no quotes)

This should install it with no issues, note that I set gpgcheck=0, if you search for it you will see a lot of people are suggesting: gpgcheck=1 which yields to something like:
warning: rpmts_HdrFromFdno: V3 DSA signature: NOKEY, key ID X

Archived under General, Server Comments

Flash CS3 is slow on Windows Vista

Here is how to fix it:
1 - Right click on Flash’s desktop (or whatever) icon and then click “Properties”
2 - Go to compatibility tab
3 - Check the box “Run this program in compatibility mode for:”
4 - From the drop down select “Windows XP (Service Pack 2)”
5 - Open Flash

And that’s it, it will run smoothly, or at least it did for me.

Archived under Actionscript, Annoying Stuff, Flash Comments

Red5 flash server problems; impossible to get it working

I worked 2 full days to get Red5 0.7.0 working with custom applications but I had no luck.

I couldn’t even get the tutorials written by Red5 developers working, it doesn’t have proper documentation either so it was a real nightmare.
So many other people have these problems too and there seem to be no answer to these problems.

So here is what I did, I installed Red5 0.6.2 and voila! it worked, all the tutorials worked and all the examples worked too!

I’m not a Java guru and I’m glad I’m not, it looks really huge and complicated, it doesn’t look fun to program in at all. I’m sure so many people like it very much but I really think even C is nicer than Java.
For example, to make an even “Hello World” Red5 application you have to make ~5 folders, 4 XML files, 1 Java class, compile your class, upload it to your server and restart Red5!!!

Update:
Funny, I kind of started liking Java :)

Archived under Actionscript, Annoying Stuff, Flash Comments

service X does not support chkconfig

Here is how to fix this:

(Assume the name of my script is myscript)

1 - Copy your script into /etc/init.d folder
2 - cd /etc/init.d
3 - chmod +x myscript
4 - Add these lines, including #, right after #!/bin/bash or #!/bin/sh:

# chkconfig: 2345 95 20
# description: Some description
# What your script does (not sure if this is necessary though)
# processname: myscript

5 - chkconfig –level 2345 myscript on

Archived under General, Server Comments

NetStream.publish Actionscript 3, recording with the best quality

After some time of working on this I found that with the following code, I could get the best quality when recording videos with Flash:

camera.setMode(300, 240, 30);
// camera.setKeyFrameInterval(CAMERA_KEY_FRAME_INTERVAL); /* Comment this out */
camera.setQuality(0, 0);

Funny, with 15 frames per second, there was a lot of problems, but 30 frames per second fixed all the issues for me.

Please do let me know if you have a better solution :)

Archived under Actionscript, Flash Comments

Bill Gates in Doom

I know many of you probably know about this but here is the video for those who don’t:

http://en.wikipedia.org/wiki/Doom_(video_game)#Release_and_later_history

Archived under Fun, General, Hobbies Comments

Detecting the end of FLV stream in Actionscript 3, part 2

Read this post first:
http://blog.code-head.com/detecting-the-end-of-flv-stream-in-actionscript-3

Funny today, I get the events in reverse order:

NetStream.Buffer.Empty
NetStream.Buffer.Flush

Yesterday it was:

NetStream.Buffer.Flush
NetStream.Buffer.Empty

I will find the solution though ;)

Archived under Actionscript, Annoying Stuff, Flash Comments

Detecting the end of FLV stream in Actionscript 3

There is an issue with this post, read it and then read this:
http://blog.code-head.com/detecting-the-end-of-flv-stream-in-actionscript-3-part-2

This is also one of those things that I couldn’t really find a real solution to online, so with some tests and experiments, I came up with this solution which I’m not sure if it’s the best solution.

First you will need to attach an event to your incoming stream, assuming your stream is clientStream:

/* ... */
clientStream = new NetStream(serverConnection);
clientStream.addEventListener(NetStatusEvent.NET_STATUS, PlayerStatusHandler);

Then you will need a member variable or a variable in the global scope to keep the latest status:

var playerLastEvent:String = new String();

Now the PlayerStatusHandler which again, I don’t think it’s very pretty:

function PlayerStatusHandler(event:NetStatusEvent) {
	if (playerLastEvent == "NetStream.Buffer.Flush" && 
		event.info.code == "NetStream.Buffer.Empty") { /* Stopped */
		StopPlayback(event); /* Or any other thing you want to do */
	} else if (playerLastEvent == "NetStream.Buffer.Flush" &&
			   event.info.code != "NetStream.Buffer.Empty")
		playerLastEvent = ""; /* Sometimes it throws the Flush event */
	else if (event.info.code == "NetStream.Buffer.Flush")
		playerLastEvent = "NetStream.Buffer.Flush";
}

So as you can see, we are looking for the Flush event that is followed by an Empty event.

This is just tested on my connection and I don’t really know how this works on slow connections, where the buffer is empty often but maybe you can tell me :)

I wish there was an event to show the real end of the stream.

Archived under Actionscript, Flash Comments (1)

Internet Explorer 7 and viruses

On this page:
http://www.microsoft.com/windows/products/winfamily/ie/default.mspx

It says “See how it helps to protect you from viruses, spyware, and other risks, plus more easily find the information you need.”.

Do you know how? It’s a virus itself!

Archived under Annoying Stuff, Web Browsers Comments

How to show a microphone activity indicator in Actionscript 3

Here is what I did, first setup a timer:

var micDelay:uint	= 100;
var micRepeat:uint	= 0; /* Run forever */
var micTimer:Timer = new Timer(micDelay, micRepeat);

And then tell it to call our function every 100 milliseconds:

micTimer.addEventListener(TimerEvent.TIMER, ShowMicActivity);

Then the function:

var micActivityIndicator:Shape = new Shape();
function ShowMicActivity(e:TimerEvent):void {
	if (mic.activityLevel > 0) {
		var h:int = mic.activityLevel;
		var y:int = 150 - mic.activityLevel;
	} else {
		var h:int = 5;
		var y:int = 150 - 5;
	}
	micActivityIndicator.graphics.clear();
	micActivityIndicator.graphics.beginFill(0x000000);
	micActivityIndicator.graphics.drawRect(0, y, 10, h);
	micActivityIndicator.graphics.endFill();
}

I hope this helps someone, I couldn’t find anything online.

Archived under Actionscript, Flash Comments

Cursor hand when mouse over images, Actionscript 3-Flash

Funny, I couldn’t find the answer to this anywhere, either the answers are unrelated or they are overly complicated. So here is what I did:

         var imageLoader:Loader     = new Loader();
	imageLoader.load(new URLRequest("path to your image/some image.png"));
	var button:Sprite    = new Sprite();
	button.addChild(imageLoader);
	button.buttonMode    = true;
	button.useHandCursor = true;
	addChild(button);

THAT’S IT!!!

Archived under Actionscript, Flash Comments

PHP Script for recursively deleting a folder and all of its contents, subfolders and files

<?php
 
	set_time_limit(900);
 
	delete_folder(dirname(__FILE__) .'/');
 
	function delete_folder($folder) {
		$folder_contents = get_folder_contents($folder);
		if ($folder_contents) {
			foreach ($folder_contents as $__content) {
				// echo $__content['item'] .'<br />';
				if (is_dir($__content['item']))
					delete_folder($__content['item']);
				else
					unlink($__content['item']);
			}
		}
		rmdir($folder);
	}
 
	function get_folder_contents($folder) {
		if( !is_dir($folder) ) { 
			return false;
		}
		$return_array = array();
		$count		  = 0;
		if( $dh = opendir($folder) ) {
			while( ($file = readdir($dh)) !== false ) {
				if( $file == '.' || $file == '..' ) continue;													
				$return_array[$count]['item']	= $folder .$file .(is_dir($folder .$file) ? DIRECTORY_SEPARATOR : '');
 
				$count++;				
			}
			closedir($dh);
		}
		return $return_array;
	}
 
?>

Put it *inside* the folder that you want to delete and run it.
Be very careful when using this code, I warned you! Don’t use this if you don’t know any PHP.

Archived under PHP, Server Comments (1)

Idea: A load balancer for the streets

Wouldn’t it be cool if everyone had a GPS device in their car, used it during rush hours and the device would connect to a central server, maybe through AT&T (!!!) and load balance the streets?
For example, it could find the best route based on traffic load on different routes to a destination.

Archived under Ideas Comments

IE 7 tabs are slow, I know why now

It turns out that it’s Google toolbar, try uninstalling it and you will see the difference.
I tried installing the latest version of Google toolbar but it didn’t really make much difference.

It made some difference when I disabled some buttons on the toolbar though, such as bookmarks.
My buttons are now: news, pop up blocker, PageRank, spell checker and Autofill.

Probably some of these buttons try to contact some server for some reason when you open a new tab and it makes everything slow.

Archived under Annoying Stuff, Web Browsers Comments

Finding and fixing bottlenecks-slow parts in your PHP code

Sometimes you can see that your script is running slow but if you don’t have a lot of experience you don’t know how to find out what’s the problem and where to start looking for it.

You then naturally start looking at your loops and things like that, but from my experience the bottleneck is almost always somewhere you are not expecting.
It’s often in your database queries or calling external resources. The ones that look so innocent :)

To find out where the issue is, you can use these two functions:

	function benchmark() {
		static $start = NULL;
		if( is_null($start) ) {
			$start = get_microtime();
		} else {
			$benchmark = get_microtime() - $start;
			$start = get_microtime();
			return $benchmark;
		}		
	}
 
	function get_microtime() {
		list($usec, $sec) = explode(" ", microtime());
		return ((float)$usec + (float)$sec);
	}

Place these 2 functions on top of your code and place these in your code:

benchmark(); /* The first call will initialize the function */
 
/* Some code */
 
echo benchmark() .'<br />';
 
/* Some more code */
 
echo benchmark() .'<br />';
 
/* Yet more code */
 
echo benchmark() .'<br />';

This will show you numbers like 0.02341… etc.
People usually don’t say how long part of a script should run or how long is too long but I’ll tell you that if you get numbers like 0.1, 0.2, 0.4 or 1.4 then you have a problem and your application might not scale well.
If this is the case then you should use PHPCache and cache the results of those slow parts, even for 1 minute at a time, this will bring those numbers down to 0.02 or 0.01…

The other thing you should do first is to check out your queries, use PHPMyAdmin, try “EXPLAIN your query” and see if MySQL is using your indexes properly. (If you indexed your tables well to begin with)
Query optimization is a big topic, I will write about that more later but sometimes query optimization won’t help you because your query is too nasty and you can’t do it any other way. (With your level of experience)

I hope this helps someone :)

Archived under PHP, Server Performance, Web Development Comments

PHPCache; a new version of PHPCache is available

A new version of PHPCache is available.

This one has 5 new methods:

PHPCache::set_expire($key)
Which will set the expiration of the cache record for $key in the past, I think this method has some advantages over deleting the key all together.

PHPCache::remove($key)
Will completely remove the row.

PHPCache::clean_up()
This method will be called when ever you construct/configure a new PHPCache object with PHPCache::configure($database) method and does two things:

1 - Will delete all the old keys on the table, you can control how often this happens with 2 constants:
PHPCACHE_GC_PROBABILITY & PHPCACHE_GC_DIVISOR
If you set PHPCACHE_GC_PROBABILITY to 10 and PHPCACHE_GC_DIVISOR to 100, then when ever you configure the PHPCache object with PHPCache::configure($database); there will be 10% chance that the garbage collector will delete the old rows.
The default value is 1%.

2 - It will optimize the table PHPCache is using, you can also control how often this happens through 2 other constants:
PHPCACHE_TO_PROBABILITY & PHPCACHE_TO_DIVISOR
It works similar to #1 and default value is 10%.

PHPCache::gc()
Will delete the old rows anytime you call this method, it doesn’t care about PHPCACHE_GC_PROBABILITY & PHPCACHE_GC_DIVISOR
One place to use this would be a cron tab.

PHPCache::optimize_table()
Will optimize PHPCache’s table and doesn’t care about
PHPCACHE_TO_PROBABILITY & PHPCACHE_TO_DIVISOR

This new version has some more minor improvements over the old one too.

Archived under PHP, Projects, Server Performance, Web Development Comments

Frog leap test, see if you can do it

http://funstufftosee.com/frogleaptest.html

It can be done:

:)

Archived under Fun Comments

WHM-Cpanel account transfer function is NOT flawless

I thought it was great but we have so many problems now with big accounts and I don’t know why they offer this problematic feature.

Archived under Annoying Stuff, Server Comments

A cool break_words function for PHP

I wrote this a while back, if you wrote a better one please let me know :)

             function break_words($text, $length, $result_length, $append) {
		preg_match_all('#([^ ]{' .$length .',})#', $text, $matches);
		foreach($matches[1] as $word) {
			$word = trim($word);
			$text = str_replace($word, substr($word, 0, $result_length) .$append, $text);
		}
		return $text;
	}

This function will break the long words in string $text, which are more than $length to be maximum $result_length characters and appends what ever you pass in as $append.

Example usage:

$post = break_words($post, 30, 30, '');

I’m not convinced that this is the best solution.

Archived under PHP, Web Development Comments

A binary tree node insertion function

I was working on this for a few days and came up with 3 versions of this function, don’t even ask for the first one.
Here is the second version which is inspired by K&R’s example:

(Just read BTree as Binary Tree)

/** First Version **/
void BTree_Store(BTree *t, char *key, void *elem)
{
       t->root = BTree_Install(t, t->root, key, elem);
}
 
Node *BTree_Install(BTree *t, Node *parent, char *key, void *elem)
{
       int cmpResult;
 
       if (parent == NULL)
              parent        = BTree_NewNode(t, key, elem);
       else {
              cmpResult = t->cmpfn(parent->key, key, BTREE_MAX_KEY_SIZE);
              if (cmpResult == 0) 
                     memcpy(parent->elem, elem, t->elemSize);
              else if (cmpResult < 0)
                     parent->lnode = BTree_Install(t, parent->lnode, key, elem);
              else
                     parent->rnode = BTree_Install(t, parent->rnode, key, elem);
       }
       return parent;
}

The problem with this (or my problem ;)) is that I don’t like creation of stack frames and local variables over and over here, it might need like 40 of them and I knew I could do better so I came up with this version:

/** Second Version **/
void BTree_Store(BTree *t, char *key, void *elem)
{
       Node **n;
       int cmpResult;
 
       n = &t->root;
       while (*n != NULL) {            
              cmpResult = t->cmpfn((*n)->key, key, BTREE_MAX_KEY_SIZE);
              if (cmpResult == 0) 
                     break;
              else if (cmpResult < 0)
                     n = &((*n)->lnode);
              else
                     n = &((*n)->rnode);
       }
 
       if (cmpResult == 0)
              memcpy((*n)->elem, elem, t->elemSize);
       else
              *n = BTree_NewNode(t, key, elem);
}

I actually love this version, it’s quick, compact and right to the point.
Here is how the node and the tree structures look like:

struct node {
       char   *key;         /* Owned by tree */
       void   *elem;        /* Owned by tree */
       struct node *lnode;
       struct node *rnode;
};
 
typedef struct node Node;
 
struct btree {
       Node   *root;
       int    elemSize;
       int    numNodes;
       int    memUsed;
       int    (*cmpfn)(const char *, const char *, size_t);
};
 
typedef struct btree BTree;

The cmpfn is strncmp by defaults because keys are char *, here are the tree & node construction and destruction functions:

void BTree_Init(BTree *t, int elemSize, int (*cmpfn)(const char *, const char *, size_t))
{
       t->elemSize = elemSize;
       t->cmpfn    = cmpfn != NULL ? cmpfn : BTREE_DFLT_CMP_FN;
       t->numNodes = 0;
       t->memUsed  = 0;
       t->root     = NULL;
}
 
void BTree_Dispose(BTree *t) 
{
       BTree_DisposeNodeRecursive(t, t->root);
}
 
static Node *BTree_NewNode(BTree *t, char *key, void *elem)
{
       Node *n;
 
       if (BTREE_NODE_SIZE(t, key) > BTREE_MEM_LIMIT) {
              printf("BTree Error: Not enough memory");
              BTree_Dispose(t);
              exit(0);
       }
 
       n             = (Node *) malloc(sizeof(Node));
       n->elem       = malloc(t->elemSize);
       memcpy(n->elem, elem, t->elemSize);
 
       n->key        = (char *) malloc(strlen(key) + 1); /* +1 for '\0' */
       strcpy(n->key, key);
 
       n->lnode      = NULL;
       n->rnode      = NULL;
 
       t->memUsed   += BTREE_NODE_SIZE(t, key);
       t->numNodes++;
 
       return n;
}
 
static void BTree_DisposeNode(BTree * t, Node *n) 
{
       t->memUsed  -= BTREE_NODE_SIZE(t, n->key);
       t->numNodes--;
       free(n->key);
       free(n->elem);
       free(n);
}
 
static void BTree_DisposeNodeRecursive(BTree *t, Node *n)
{
       if (n->lnode != NULL)
              BTree_DisposeNodeRecursive(t, n->lnode);
 
       if (n->rnode != NULL)
              BTree_DisposeNodeRecursive(t, n->rnode);
 
       BTree_DisposeNode(t, n);
}

Here is the BTREE_NODE_SIZE macro:

#define BTREE_NODE_SIZE(t, k)      sizeof(Node) + strlen(k) + 1 + t->elemSize

And here is the search function:

Node *BTree_FindNode(BTree *t, char *key) 
{
       Node *n;
       int cmpResult;
 
       n = t->root;
       while (n != NULL) {
              cmpResult = t->cmpfn(n->key, key, BTREE_MAX_KEY_SIZE);              
              if (cmpResult == 0)
                     return n;
              else if (cmpResult < 0 && n->lnode != NULL)
                     n = n->lnode;
              else if (n->rnode != NULL)
                     n = n->rnode;
              else
                     break;
       }
 
       return BTREE_KEY_NOT_FOUND;
}
 
void *BTree_Get(BTree *t, char *key)
{
       Node *n;
       n = BTree_FindNode(t, key);
       return n != BTREE_KEY_NOT_FOUND ? n->elem : BTREE_KEY_NOT_FOUND;
}

The only problem with this tree is that if you store sorted data you will end up with a linked list rather than a binary search tree so I’m working on a self balancing binary search tree which is very chalanging but I love chalanges ;)

Archived under C Programming, Low Level Comments

« Previous entries