Mikes Coding Oddities

Saturday, December 19, 2015

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:

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).

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

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.

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).

Wednesday, November 25, 2015

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:

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)

Also from gnu classpath get the BufferedInputStream class (this is not supplied in java.io on J2ME/CLDC devices).

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

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 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:

Friday, August 28, 2015

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.

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

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:

Start any Android emulator on any Ubuntu Linux machine

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.