Simple video streaming with apache to Samsung Galaxy Tab
Ok, so I’d like to watch videos on my Samsung Galaxy Tab without the hassle of copying them from my computer. I thought streaming with VLC would be a nice option. It probably is, but I didn’t get it to work. The tab has great support for media formats which means I only need streaming - no converting, so I thought why not use plain old apache? And it worked pretty well, playback is smooth and I can jump around nearly instantaneously. This is my setup:
First if you don’t have it install apache2 and symlink your video folder into /var/www. Then check your servers ip adress with ifconfig
$ sudo apt-get install apache2
$ sudo ln -s /home/bb/Video /var/www/video
$ ifconfig
After that just open up the browser in the tab (with your servers ip adress of course)
http://192.168.1.100/video
Depending on file type I get a choice of programs to handle the file, for some reason .mp4 files triggers a dialog with just Android-system and Browser, just choose Android-system and you’ll get a proper dialog of choices. One problem I got was that the standard video player can’t handle all the formats, I use Dice Player which works very well for me.
Posted 2 months ago
Cucumber testing with Kyuri in a browser
Cucumber is a really nice testing framework, but I dislike having to implement the step definitions in ruby when the project itself is not in ruby, but what (web) project use javascript so why not do cucumber testing with javascript?
There are a couple of solutions for this, Cucumber JVM (former cuke4duke) is an official java based version of cucumber that let’s you use any jvm language. That’s cool, and probably a good way to go, but not fun enough for me. It messes with my mojo to start the jvm for something like this. And besides, there is no documentation on defining steps in javascript. Cucumer-js is a pure javascript alternative that looks good, uses node and seems very cool. But it lacks i18n support, it seems to be actively developed on so this is certainly something to keep an eye on. Cucumber-js also has support for running it in the browser via browserify. A third cool alternative is Kyuri, another pure javascript implementation, and it has support for i18n! Kyuri is also node based, can it be used in a browser? This is the steps that I took to get it running.
Prerequisites
Install browserify and kyuri with npm.
$ sudo npm --global browserify
$ npm install kyuri
Browserify
Kyuri actually has two ways to define steps, as “cucumber” and vows, vows seems to be the main target, but it doesn’t translate well to the browser. It has dependencies that browserify can’t handle. So we cut it out, which means a little bit of tweaking:
$ browserify -r kyuri -i /node_modules/kyuri/node_modules/vows/lib/vows.js -i ./kyuri/runners/vows -o kyuri-bundle.js
This will give you a kyuri-bundle.js, it lacks vows.js and its runner but it still tries to require it so we need to edit it a bit. Remove this line (it’s line 375)
kyuri.runners.vows = require('./kyuri/runners/vows');
And these just below:
// Set default runner to vows
kyuri.runner = kyuri.runners.vows;
Browser
This is a sample html page to test it:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Kyuri</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript" language="javascript" charset="utf-8" ></script>
<script src="kyuri-bundle.js" type="text/javascript" language="javascript" charset="utf-8" ></script>
<script type="text/javascript" language="javascript" charset="utf-8">
var kyuri = require('kyuri');
var Runner = kyuri.runners.cucumber;
Runner.on('beforeTest', function (done) {
console.log('beforeTest event');
done();
});
Runner.on('beforeBackground', function (done) {
console.log('beforeBackground event');
done();
});
Runner.on('afterTest', function (done) {
console.log('afterTest event');
done();
});
$(document).ready(function(){
//features
var features = [];
$('.feature').each(function(){
console.log(kyuri.parse($(this).text()));
features.push(kyuri.parse($(this).text()));
});
//step defitions
var Steps = require('kyuri').Steps;
Steps.Given(/^I have entered (\d+) into the calculator$/, function (step, num) {
console.log('Calculator: ' + num);
step.done();
});
var module = {};
Steps.export(module);
var steps = module.exports;
//run it
kyuri.runners.cucumber.run(features, steps, function () {
console.log("Done!");
});
});
</script>
</head>
<body>
<pre class="feature">
Feature: Addition
In order to avoid silly mistakes
As a math idiot
I want to be told the sum of two numbers
Scenario: Add two numbers
Given I have entered 50 into the calculator
And I have entered 70 into the calculator
When I press add
Then the result should be 120 on the screen
</pre>
</body>
</html>
Caveats
- This only works in Firefox for me, Chrome has a minor error (probably easy to fix).
- It logs console color codes… looks weird
Posted 2 months ago
Chroot’d Ubuntu on Samsung Galaxy S (Android)
So I got my hands on a Samsung Galaxy S (or SGS) a while ago, and it’s a really nice phone using Android 2.1 as it OS. Before I settled on the SGS the Nokia N900 had caught my eye. The 900 has great hardware and its OS is more of a “standard” Linux. Its Maemo OS is really just a Linux distro with all that entails (X11,QT,GTK), this means you can easily run any Linux app you can compile, and since it’s easy to install Debian or Ubuntu chroot’d on it you can even run things like OpenOffice etc (But the speed and screen real estate won’t be good). The problem is that Maemo is being merged into to Meego and there is only one phone, the N900, which means no apps what so ever except open source ones.
Android is linux based, but it doesn’t use X11 and most applications are written in Java and executed on a custom JVM called ‘Dalvik’. Therefore I was a bit sceptical of buying a android device, it felt a bit stupid to not take advantage of the massive amount of open source programs, libraries and toolkits out there. I still do, but android struck me as a much more mature OS, an it’s very nice to have lot’s of apps :-)
Anyway, having linux underneath means that we can easily install a chroot’d Ubuntu in Android, giving us access to many nice applications… like openssh, python, webservers, transsmission (with web gui) etc. This is nothing new, it has been done before, both with Debian and Ubuntu, but here is how I did it on my SGS. If you don’t know what chroot is, read up before you do anything. For X11 application there is a trick with VNC to get them running, I haven’t tried it yet though.
A couple of prerequisites:
I got both by rooting my phone after this guide .
Partition
First of we need somewhere to put the system, android uses FAT32 for the SD card and that’s no good for us. We need a real file system, like ext4. Unfortunately the kernel in my SGS didn’t support it. You can get a list of supported filesystems with ‘cat /proc/filesystems’. This is from my
SGS:
$ cat /proc/filesystems
nodev sysfs
nodev rootfs
nodev bdev
nodev proc
nodev cgroup
nodev binfmt_misc
nodev sockfs
nodev pipefs
nodev anon_inodefs
nodev tmpfs
nodev inotifyfs
nodev devpts
ext3
ext2
cramfs
nodev ramfs
vfat
msdos
romfs
rfs
j4fs
So we settle for ext3. The easiest way to partition the SD card is to connect it to you’re computer (not through the phone), and use gparted. I used the SD card reader on my laptop with the use of an adapter that came with the card when I bought it. It’s best to just shrink the vfat partition because if you remove it your phone is going to complain that the card is broken or needs formatting. If your starting from scratch just make sure the vfat partition is the first partition.
The size of the ext3 partition of course depends on the size of your SD card. But at least 1 gigabyte is probably a good idea. I used a 2 gigabyte card, 512 megabytes for the vfat, the rest for the ext3 partition. Don’t forget to tell gparted to format your new partition.
Create a rootfs with rootstock
Rootstock is a great tool for building a basic ubuntu image to use with your ARM based device. It uses good old debootstrap, running the second stage of the installation (the one you usually need to run on the device) under qemu. Pretty smart. This means that the tarball you get is a fully finished root base file system. You can even specify some extra packages that rootstock can install. A SSH server is a good idea, I like less, screen, ne (nice editor, like nano but with ordinary key-bindings, like ctrl-s etc), build-essential so that we can compile things.
Note that you specify a user and a password. You should choose a good password.
Create a rootfs tarball:
$ sudo rootstock --fqdn ubuntu --login sgs --password temporary --imagesize 1G --seed openssh-server,less,build-essential,screen,ne
Unpack the tarball and copy the content to your fine new partition on the SD card.
Then set up googles DNS server (the ext3 partition is mounted on /mnt):
$ mv mv armel-rootfs-201008012234.tgz /mnt/
$ cd /mnt
$ tar xvzf armel-rootfs-201008012234.tgz
$ rm armel-rootfs-201008012234.tgz
$ echo 'nameserver 8.8.8.8' > etc/resolv.conf
It’s time to put the card back into the phone…
Now on your phone
For the rest you need to drop into root:
$ su
#
First mount the ext3 partition, I use a dir on the SD card called “ubuntu”, you can put it anywhere you want though. Since the SGS has internal storage too the SD card has the device /dev/block/mmcblk1p2
# busybox mount /dev/block/mmcblk1p2 /sdcard/sd/ubuntu
chroot by itself is not enough, we need to mount /proc,/dev and /sysfs too.
An the path on android is a bit strange so we need to set that too.
Tap this on your phone for a glorious chrooted ubuntu:
# busybox mount -t proc proc /sdcard/sd/ubuntu/proc
# busybox mount --bind /sys /sdcard/sd/ubuntu/sys
# busybox mount --bind /dev /sdcard/sd/ubuntu/dev
# export PATH=/bin:/usr/bin:/sbin:/usr/sbin:$PATH
# busybox chroot /sdcard/sd/ubuntu
Log in via SSH
When your chroot’d you can start the SSH server and log into it that way. This will give you full terminal emulation (as opposed to “adb shell”), i.e.bash completion, emacs, vim etc.
Start server and check what ip nr your phone has
# /etc/init.d/ssh start
# ifconfig | grep "inet addr"
Note that since this is Ubuntu the root user doesn’t have any password so you have to ssh to it with the user name and password you specified to rootstock. In my example above that’s “sgs” and “temporary”. To get root privileges use sudo, remember this is Ubuntu.
A script to help
If you think tapping all that mounts and path etc on your phone is a bit tiresome to tap you can make a script of it. Shell scripting is not my strong point so it can probably be improved, but it seems to work for me. It mounts the ext3 partition and sets up everything needed. Note that it does not drop you into a chroot’d ubuntu, it just starts the ssh server.
#!/bin/sh
#we need to setup test [
busybox ln -s /system/xbin/busybox /system/xbin/[
if [ -d /sdcard/sd/ubuntu/etc ] ; then
echo "ubuntu partition is already mounted"
else
echo "Mounting ext3 partition to /sdcard/sd/ubuntu"
busybox mount -o noatime /dev/block/mmcblk1p2 /sdcard/sd/ubuntu
echo 'Mounting proc,sys and dev'
busybox mount -t proc proc /sdcard/sd/ubuntu/proc
busybox mount --bind /sys /sdcard/sd/ubuntu/sys
busybox mount --bind /dev /sdcard/sd/ubuntu/dev
fi
echo 'Exporting a proper path'
export PATH=/bin:/usr/bin:/sbin:/usr/sbin:$PATH
echo 'Chrooting and starting ssh server'
busybox chroot /sdcard/sd/ubuntu /etc/init.d/ssh start
echo "And your ip is:"
busybox ifconfig | busybox grep "inet addr"
Put it on the sdcard or the on the internal storage and execute it like this:
$ su
# sh /sdcard/sd/ubuntu.sh
Posted 1 year ago
Re-inventing the wheel: A logging development SMTP server
I found myself in a spot of trouble while trying to debug some newsletter sending plugin in Plone called Singing & Dancing. The problem was that I had made a local installation of the application server and the Python version I had to run it on had no support for TLS/SSL. This meant my ordinary outgoing mailserver wouldn’t work.
Well anyway that would have been a bad solution to send “for real” since I just needed to see that the mails looked good and where addressed to the right people. So now I probably should have done the obvious and googled a bit, there seems to be quite a few simple SMTP servers, but I alas I did not. What I did was to re-invent the wheel a bit.
Python has some nice SMTP handling modules both for sending and receiving. There is already a debugging SMTP server that just prints any received message. You can start it directly with:
$ python -m smtpd -n -c DebuggingServer localhost:2525
This was nearly what I needed, the only problem was that the mails where Base64 encoded so it wasn’t obvious that they where correct.
Wouldn’t it be nice if we could view the mails directly in a email application? Well with a little tinkering we can. Again, the standard library in Python to the rescue. This time the mailbox module. The mailbox module can write email messages to mbox files or maildirs, both old and tried and ancient formats for mail box storage. The great thing about a maildir is that several mail readers can parse it, like evolution (but not Thunderbird! AFAIK)
To read mails in a maildir in evolution you just add a new account (it’s under settings) and then select the maildir format. It will then ask you for a folder, the script takes arguments to define it but the default is ~/.devbox
To start the server (I called it devbox, as in a “development inbox”):
$ devbox --port=2525 --maildir=foobar/mymail
Use -h to get help of the options and remember that if you need to bind against port 25 you need to be root or use sudo. Heres the code, and as you can see one third is GPL preamble, the other third is the option parsing and it’s only really one small class that does the trivial job of passing on the mail between the SMTPServer and the Maildir. I love Python standard library!
#!/usr/bin/python
# Copyright 2010 David Jensen
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <>>.
import mailbox
import smtpd
import asyncore
from optparse import OptionParser
class DevBox(smtpd.SMTPServer):
def __init__(self,port,maildir):
smtpd.SMTPServer.__init__(self,('localhost',port),('localhost',25))
self.mbox = mailbox.Maildir(maildir)
def process_message(self, peer, mailfrom, rcpttos, data):
self.mbox.lock()
self.mbox.add(data)
self.mbox.flush()
self.mbox.unlock()
print "Added a msg from ",mailfrom
if __name__ == '__main__':
usage=""" %prog [options]
%prog is debugging SMTP server bound to localhost. Useful
for when you need to test outgoing mail but do not want any
mail actually sent. All incoming messages are saved in a maildir
for later perusal regardless of any possible address.
What comes in won't come out."""
parser = OptionParser(usage,version="%prog 0.1")
parser.add_option("-m", "--maildir",action="store", type="string",
dest="maildir",default='~/.devbox',
help='sets the path to the maildir [default: %default]')
parser.add_option("-p", "--port",action="store", type="int",
dest="port",default=25,
help='sets the port the server listens to [default: %default]')
(options, args) = parser.parse_args()
print "Starting server on localhost:%i, with maildir %s" % (options.port,options.maildir)
dn = DevBox(options.port,options.maildir)
try:
asyncore.loop()
except KeyboardInterrupt:
pass
Posted 2 years ago