Saturday, December 19, 2015

The HTTP dance required make Chrome play video tags

Sometimes in an app you need to use an embedded HTTP server to make HTML content work properly.  In theory you could serve up content from the FileSystem directly: but then HTML5 localstorage, media, cookies, requests for resources from other directories misbehave.  What does cross-domain mean on a filesystem?

In our app we run the excellent NanoHTTPD to serve up content from epub (zipped) files.  Chrome has some (seemingly undocumented) requirements:

  1. You must support HTTP range requests.  You don't need to support multiple ranges: but you must implement returning of 206 partial content in response to a range request.  Interestingly on Android 5.1 Chrome seems to test this by sending a range request for the first byte (0-1).
  2. You must give chrome a means of validating the file remains the same and hasn't changed: A strong (normal) etag works for this.  Setting the last-modified header might work as well.

When using Java inputstreams this of course means using the skip method: be careful that needs to go in a loop because the skip method often does not skill all the bytes requested.  We implemented range support in our EmbeddedHTTPD and RangeInputStream .

Monday, November 30, 2015

The ways around J2ME JSR-135 OutOfMemoryException

Playing media in J2ME seems relatively straightforward: provide an InputStream or address and the media should play.  Unfortunately Nokia's implementation of Manager.createPlayer(InputStream, mimeType) will attempt to buffer the stream in full in memory.  It will always run out of memory because it will attempt to buffer the full media object before playing anything back.  A slower stream just makes you wait a bit before it runs out of memory and doesn't play.

Partial Solution: If you use Manager.createPlayer("file://E:/fileonsdcard.3gp") instead with the jad attribute progressive_download: enabled the file will play smoothly and it won't run out of memory (hoorah!).  But what if the media you want to play isn't just sitting around in raw form on the file system?

I tested out using Manager.createPlayer("http://server/some/file.3gp").  Playing over a 2G or 3G internet connection my half hour 3GP clip played OK.  But using a Nokia Asha 500 (which runs series 40) over a WiFi network the clip only plays successfully one time in 5 or so.  Again seems like the underlying logic thinks "AH!!! Data - let's download everything!!!  Ehh... I ate too much... collapse...".  When I used mod_ratelimit in the apache server I was running on my laptop to limit the speed to 128kbps (16K) behavior returned to more or less what it was with the Internet: Generates an interesting Apache log showing that it downloads a small chunk, then a large chunk, then some smaller chunks.  Towards the end playback started sputtering cutting out for 10-15seconds at a time, playing back for less than a minute, and repeating that cycle. - - [30/Nov/2015:12:59:57 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 200 6847 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:12:59:57 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 367505 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:12:59:56 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 200 130375 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1 UNTRUSTED/1.0" - - [30/Nov/2015:13:00:20 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 16860717 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:13:23:06 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 37080 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:13:23:42 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 90564 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:13:24:24 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 58872 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:13:25:01 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 77460 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:13:25:39 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 30528 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:13:26:15 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 147324 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:13:26:57 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 629964 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:13:28:09 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 81803 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:13:28:48 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 30527 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:13:29:23 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 159739 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1"

Now let's consider some workarounds one might have in mind: and how unfortunately they get thwarted:

  • If I know the duration of a stream: slow the stream down to the rate of playback.  
    • Unfortunately the duration is not available for players created using an InputStream : and the duration is also not available ; so calculating the bitrate would involve manual file reading and codec work.
  • Use a DataSource / SourceStream for Manager.createPlayer
    • I didn't have any luck with this one: I tested my implementation on the emulator with a wav file - was happy.  Tested it on a Series 40 phone: can't get beyond "Error connecting to data source"
  • Let's close the stream at some time to force an interruption / replay
    • Getting this to play in the best of circumstances isn't so much fun... and partial streams (e.g. truncated files) aren't understood
  • Let's slow down the stream to internet like speeds: then use an Internal HTTP Server
    • Well this should produce the same performance as running it over the internet: the problem is the implementation of playing over HTTP misbehaves.
Which leaves us with one final workaround: Write the entire stream to a file and then use Manager.createPlayer with the created file URL.  In our particular implementation we have files downloaded locally but in epub (zipped) files.  Unzipping an 18MB 3gp file (which plays for 30mins) took 2mins on a Nokia 206 (mid range phone).  

For a good overview (that even mentions the let's buffer everything problem) check out this post on InformIT .

Wednesday, November 25, 2015

Using InputStreams with JSR-135 J2ME Sound Playback

In our J2ME app we need to be able to play back sounds for the user for files that are already downloaded (albeit inside zipped epubs).  So connecting the two using Manager.createPlayer(in, mimeType) seems fairly straightforward.  However on the devices themselves with longer files it will croak after a certain amount of time (maybe a buffer under run happens... still not sure).  With some other files that we were accessing for performance and to ensure any active file connections are closed properly we simply read the whole content into a byte array.  Not a good idea when it comes to long mp3 files: here's our recipe that seems to work:

  1. Obtain an inputstream from the file source (in our case, the file source is inside a zip for which we use the gnu classpath project to unzip)
  2. Also from gnu classpath get the BufferedInputStream class (this is not supplied in on J2ME/CLDC devices).
  3. Feed the player a BufferedInputStream instead of the raw stream itself: e.g.
    return new BufferedInputStream(src, 20*1024);
I haven't yet played around with the buffer sizes; occasionally it very briefly misses a fraction of a beat on lower end devices (e.g. the Nokia 110)

Monday, November 9, 2015

J2ME Images stop loading mystery

In our app we need to be able to show images from EPUB files on J2ME .. and those images change as the user goes from page to page.  We're using the rather neat LWUIT HTMLComponent to render the HTML pages with some custom additions to handle media (published here on GitHub).

Mysteriously using a Nokia 206 phone we could load about 19 900x900 pixel jpegs (showing one after the other; with a garbage collect System.gc() call in between) before it would give up - and any subsequent request to load an image would throw an IOException.  This was being caused by an IOException being thrown inside the ResourceThread as it was trying to load the image.  This of course was happening only on the devices: not on the emulator.

J2ME phones is that they vary widely in resolution and memory - from a Nokia 110 which has a 120 pixel or so wide screen to an Asha which has 240x320.  Perhaps images can simply be bounded to a maximum of 320x320 ; or perhaps we need to have different epub files for different phones.  Not sure of that yet.

Note to anyone (still) working with J2ME to avoid tearing hair out : keep images as small as they can be.

Saturday, September 5, 2015

J2ME FileConnection Gotchas: Overwrite and beginning with .

J2ME gives you all the power of what was in a 1997 PC to put apps on billions of phones: with none of the debugging tools.

1. Nokia Series 40 will now allow you to create a filename that begins with "." - do that and you will get an exception .

2. Even in the emulator: if you want to overwrite the entire file when getting an output stream to an existing file: the only way I have seen consistent results is to delete the file first.  Incredibly even openOutputStream(0) - to explicitly open the outputstream at the start seemed to overwrite some but not all of the file (e.g. if you want to overwrite with a smaller file than it was originally there will be trailing leftovers from the previous file).  Something like this works:

public OutputStream openFileOutputStream(String fileURI, boolean autocreate) throws IOException{
        FileConnection con = null;
        IOException e = null;
        try {
            con = (FileConnection), Connector.READ_WRITE);
            if(con.exists()) {
        }catch(IOException e2) {
            e = e2;
        }finally {
            if(e != null) throw e;
        OutputStream out = Connector.openOutputStream(fileURI);
        return out;        

Friday, August 28, 2015

No Java 8: No JSON for you says Android: UnsupportedClassVersionError: org/json/JSONObject

Let's say you are crazy enough to be running the Ubuntu Long Term Support distribution.  Maybe you would even rather like to use the jdk package from the Ubuntu repo so it gets automatically updated?  Android says: No; not if you feel like using JSON.

java.lang.UnsupportedClassVersionError: org/json/JSONObject : Unsupported major.minor version 52.0

No you won't be able to just change the JSON version in Maven like others who get stuck with it.  You will upgrade to JDK 8 or your code will not run.  Developer's using JSON: wow, what an edge case, eh?  Who would possibly expect Google to conduct enough testing to figure that one out and say "Hey - you have to use JDK 8 to compile from now on".

Wednesday, August 12, 2015

Android emulator SNAFU: Eating 100% CPU when idle

Fresh from the "who could have expected Google to try that edge case?" list: the case of Android emulators eating 100% of the CPU when Idle on Linux.  It's very complex to reproduce:

  1. Start any Android emulator on any Ubuntu Linux machine
  2. Watch it eat 100% of a CPU core when doing nothing after bootup has completed
The issue is noted on a bug report where someone from Google looked at it 4 years ago to say "huh I can't make it happen".

The solution? Hope you don't need audio: run export QEMU_AUDIO_DRV=none ; then run your emulator.

SNAFU: Regularly used in Afghanistan, particularly if you speak to anyone who was working with the military. Situation Normal All F***ed Up.  Also highly applicable to many Android development situations it seems.

Sunday, May 31, 2015

Android command line project creation and gradle

Here's another instance where Android's lousy documentation is just plain wrong AGAIN.  When you create a project using android project create as per their documentation you will see there is no gradle file.  I've tested this on my Ubuntu 15.04 running openjdk7.

It will then say to build or run your project run the non-existant ./gradlew file.  What you need to do is as per stackoverflow is to add a --gradle and a --gradle-version to the project create command that is nowhere mentioned in google's documentation.  Seriously Google, you don't have enough money or staff to run once through your docs to discover that when people RTFM they don't actually work.

Again one has to find the answers on stackoverflow.  I'd find the right place to report it, but someone already told you that you have the wrong packages listed for Ubuntu since years and you never bothered with it.

Part 1: change how you create the project:

$ android create project --path FrustratedDev2 --activity FrustratedDev2 --package com.somewhere.killme --target "android-21" --gradle --gradle-version '1.2.3'

Part 2: Play with config file it never mentioned that is created wrong by default:

$ vi gradle/wrapper/





Part 3: Update proguard minify options that don't work android put there somehow:
$ vi build.gradle


runProguard false
proguardFile getDefaultProguardFile('proguard-android.txt')


minifyEnabled false

And if everything is in order; now you can build a debug APK file:

$ ./gradlew assembleDebug

PS: Even though the docs say you need to run chmod +x gradlew; that's wrong too - you don't have to now ; it's created executable by default.

PS: Reported it to Google here on their bug report for documentation : Let's see how long it's ignored for.

Friday, April 24, 2015

Android tablet as a second Ubuntu screen with xrandr and x11vnc

Let's say you have a laptop, an Android tablet and you're working on debugging something.  Wouldn't it be nice to have some extra screen real estate?  The second answer here seemed to be going in the right direction... only one gets stuck moving the mouse into the extended screen area.

So here's a recipe that works (on Ubuntu 14.10 at least). Let's say the actual laptop screen is 1366x768 as in my case; and we want to create another screen of the same resolution:

$ sudo apt-get install x11vnc
$ sudo xrandr --fb 2732x768 --output LVDS1 --panning 2732x768+0+0/2732x768+0+0
$ sleep 3 # wait a moment
$ sudo xrandr --fb 2732x768 --output LVDS1 --panning 1366x768+0+0/2732x768+0+0

Explanation: xrandr (X Resize and Rotate) sets the screen size. To make it think the screen is bigger without doing strange things we first tell it to make a panning setup. With panning the section before the / is the area in which that screen can pan around, after the / is the very important tracking area in which the mouse can move.

Now we can use x11vnc with a clip

sudo x11vnc -clip 1366x768+1367+0 -nocursorshape -nocursorpos

We need to use -nocursorshape and -nocursorpos so the cursor is not dealt with by VNC; the cursor is directly painted on as part of the image.

Now you can take your pick of Android VNC apps and then point it at your laptop's IP address:

The new right half of the screen
Now there's only two and a bit problems left: VNC is not encrypted; so it should run over an SSH tunnel/VPN or something of the like.  Another problem can be lousy WiFi; I'm hoping to kill two bids with one stone by making this run using port forwarding on over the USB cable.

Thanks to this AskUbuntu post.

Monday, April 13, 2015

Ubuntu 14.04 'headless' LTS with Jenkins-CI for Android emulation

So you want one of these magic cheap cloud servers to do handle unit testing and building your Android app?  Makes sense if you have a cross platform approach; the tests need to run automatically on each platform.  And some of us need to do more customization than the millions of cloudy IDEs let us do (like install an Ubuntu package even)

Problem 1: Amazon AWS (and the like) servers are virtual themselves and performance will be awful.  So bad in fact that Cordova's default timeouts will be exceeded.  So you need a real server; in fact with many providers (e.g. Hetzner) they are cheaper for the same performance than what Amazon provides.

Problem 2: Google can make an OS that's on a billion devices.  What they apparently cannot do is maintain documentation on installing an SDK and it's actual requirements.

Problem 3: Ubuntu minimal (what you normally get with a cloud server) and the Android emulator do not play ball.  You will get strange meaningless error messages like can't load swrast.  glxinfo will segfault.


Take an Ubuntu 14.04 real dedicated server, add the full ubuntu desktop, add a few extra packages, then use VNC to login exactly as you would 'normally'

$ apt-get update && apt-get install ubuntu-desktop
$ apt-get install x11vnc
$ adduser <someusername>
$ init 6

...Wait for server to restart with the lightdm desktop manager running, login as root, and then...

$ x11vnc -xkb -noxrecord -noxfixes -noxdamage -display :0 -auth /var/run/lightdm/root/:0 -usepw

Now use a VNC client to connect to display 0 using the public IP address of the server: you should be able to connect and see the 'normal' ubuntu login.  You can now login with the username created before.

Note: VNC itself does not use encryption.  You'll need to use a VPN like OpenVPN for that.

Now use another SSH connection to install what's needed for Jenkins:
$ apt-get install openjdk-7-jdk
$ wget -q -O - | sudo apt-key add -
$ echo 'deb binary/' > /etc/apt/sources.list.d/jenkins.list
$ apt-get update && apt-get install jenkins
$ nano /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf

Add a line so you can login with a graphical desktop as jenkins :

Restart lightdm to enable the change (this will cut your VNC session):
$ service lightdm restart

Re-run the VNC service:
$ x11vnc -xkb -noxrecord -noxfixes -noxdamage -display :0 -auth /var/run/lightdm/root/:0 -usepw

Login to the server as jenkins using vnc now and use firefox to download the android development toolkit as you would on a normal laptop/desktop.  Before you run it (as root); add the packages Android needs:

$ dpkg --add-architecture i386
$ apt-get update
$ apt-get install libncurses5:i386 libstdc++6:i386 zlib1g:i386
$ #install these packages that google didn't tell you about to avoid no such file or directory mystery error message
$ apt-get install libc6:i386 libstdc++6:i386 lib32z1

Add Jenkins to the KVM group needed to run android vms at a reasonable speed:
$ adduser jenkins libvirtd
$ adduser jenkins kvm

Now use the android command to install the components as normal and android avd to setup virtual devices.  I recommend not using super screen resolutions:

Now for continuous integration purposes; you'll need to make sure the system is logged in and add this to your scripts before they need something that launches a window:

export DISPLAY=:0 && export XAUTHORITY=/home/$USER/.Xauthority

References + Thanks: