> **Full Stack Engineer** · February 2012 to 2013, at Digital Arts Network
> Source: https://www.simonbetton.com/case-studies/tvnz-ondemand.md (markdown only; there is no HTML page for this document).
> Index of case studies: https://www.simonbetton.com/case-studies/index.md

# Case study: TVNZ OnDemand mobile apps

## Quick facts

|  |  |
| --- | --- |
| **Role** | Full Stack Engineer at Digital Arts Network. I was the only engineer on the client apps |
| **Client** | TVNZ, New Zealand's national broadcaster |
| **Partner** | Brightcove (Boston): Video Cloud platform, App Cloud runtime, media player and DRM |
| **Product** | TVNZ OnDemand for iPad and iPhone (internal name ODMOB); an Android build followed, which I also built |
| **Timeline** | Pitch won February 2012; six months of research; development from August 2012; iPad app late 2012; iPhone and iPod touch app 25 February 2013 |
| **Runtime** | Brightcove App Cloud: HTML5 in native containers, five isolated web views, a WebKit without the Nitro JavaScript engine |
| **Client stack** | App Cloud SDK (jQuery and tap events only), Markup.js, Moment.js, lscache over localStorage |
| **Recognition** | 2013 Best Design Awards: Gold (Value of Design, Emerging Value) and Bronze (Digital Products), to Digital Arts Network, with me credited as a team member |

## Overview

In 2012 TVNZ OnDemand on the web still played video through Flash, which iOS did not run, and TVNZ's geolocked DRM blocked the Flash-translation workarounds people used on iPads. TVNZ wanted OnDemand on iPad and iPhone with the same rights protection and the same advertising.

TBWA's Digital Arts Network (DAN) won the pitch in February 2012. Brightcove built the media player, the DRM layer and a plug-in into TVNZ's asset management system that transcoded programmes for the app. DAN was responsible for the app itself: research, design, layout and the HTML5 client. Three development teams worked across several time zones, and TVNZ, DAN and Brightcove shared one codebase. The DAN team credited on the awards entry was a creative and design director, three team members including me, and one contributor. I was the only engineer writing the client, and the architecture and measurements that follow are mine.

The iPad app shipped late in 2012 and, according to TVNZ, became the number one free app on the iTunes Store. The iPhone and iPod touch app followed on 25 February 2013; TVNZ's release called it the first video on demand app for iPad and iPhone in New Zealand. The same week, Brightcove announced it was discontinuing App Cloud, with support for existing customers running to June 2014.

Brightcove's player, DRM and ad integration were not mine, so what follows covers the client only.

## The problem

The product had to feel like a native TV app. The runtime was a hybrid web view with a tight memory budget and a JavaScript engine without Nitro.

App Cloud gave us five separate web views, like tabs, that could not talk to each other. Each view received three lifecycle events: init on application load, focus and blur. When iOS sent a low-memory warning the container tore a view down to keep the process alive, and the user saw a blank white page until it reloaded.

WebKit on the devices of the time would crash the whole app if a render pass was too large. Passing the full shows catalogue through the templating library and injecting it in one go usually did exactly that.

Memory was spent on decoded bitmaps, not on bytes over the wire. A decoded image costs width × height × 4 bytes, and a 2x retina asset costs four times a 1x one. The maths for the home screen set the budget for the whole app.

Touch was hostile. A click event waited 300 ms, a delay iOS added so that a tap during a coasting scroll stops the scroll instead of firing a click at the wrong place. The SDK also injected an iframe into the DOM as its JavaScript-to-native bridge, the technique PhoneGap had used, and that iframe blocked touch and click events and turned vertical scrolls horizontal.

We also did not know every screen we were shipping to. Development started before the iPhone 5 introduced a new aspect ratio, so DAN adopted a responsive layout from the start rather than a fixed layout per device.

## Decisions

I kept the client small enough that rendering, memory and caching stayed under direct control. The App Cloud SDK bundled jQuery, Markup.js and native bridging; I used jQuery and the SDK's tap events and nothing else in it. Moment.js handled dates. lscache sat on localStorage for caching feed data.

I did not use a UI framework. The ones available imposed a theme that would have been hard to change to suit the design, and none of them were both good at UI and fast on this WebKit.

App Cloud's documentation said to declare a Content Feed per view in the app manifest. Compiled, that rewrote every request URL to be per application and per view, which added a caching layer between the app and TVNZ's data that we did not want. We requested feeds by ID from JavaScript instead.

Three further decisions came from measuring the runtime rather than trusting defaults: size images by decoded memory, not file size; render the catalogue in batches because WebKit could not survive a single pass; and remove the 300 ms click delay, accepting a known mis-tap case. The last of these was TVNZ's call once the trade-off had been tested.

## Execution

### Image memory

We shipped 1.5x assets displayed in 1x layout boxes instead of 2x retina assets. My calculation at the time put the home-screen image-memory saving at 14.4 MB. This was a calculated saving, not an on-device measurement. Because the 1.5x files were being downscaled on display, we could also lower their image quality, which cut file size further. Devices with a pixel ratio of 1 never received high-resolution assets.

Images were CSS backgrounds rather than inline image elements: on Mobile Safari they scrolled better and were not subject to the same image memory limit, at the cost of taking longer to paint. Every image was lazy-loaded from a data attribute so WebKit never decoded the whole catalogue at once.

### Rendering in batches

Markup.js rendered every dynamic screen. The shows page could not take the full dataset, run the template and inject once; I looped through the shows six at a time, injecting each batch, because larger batches usually crashed WebKit and took the app with it. I would not choose Markup.js again. It was a measurable bottleneck, and Handlebars has similar enough syntax that a port would have been straightforward.

Early on, JavaScript computed width, height and position for the page and recomputed on orientation change, which is how most mobile UI libraries of the time laid out screens. It was slow. I removed it and did layout in CSS with absolute positioning and fluid sizing, so a rotation did not rewrite the DOM. That was a large performance gain, and a fluid layout was also how the app adapted to orientation and screen size.

Selector work stayed cheap: cache DOM references once, keep selectors short and read them right to left, chain rather than re-query, and use native APIs where jQuery only added weight. I measured jQuery itself at roughly 3 MB of memory, around 47 ms of script evaluation and around 24 ms to DOMContentLoaded in desktop Chrome, and on device it could only be slower. It stayed because the SDK shipped it.

### Touch and scrolling

The SDK's tap event replaced click, removing the 300 ms wait. Many libraries did the same thing; this one was already in the bundle. Because views reloaded their content on navigation, jQuery handlers accumulated; I unbound before binding so nothing was attached twice.

Removing the delay had a cost. If the user taps while a scroll is still coasting, the tap registers a short distance from the finger, and without the delay it can select the neighbouring tile. TVNZ preferred the responsiveness over that case. It was a deliberate product decision, not an oversight.

Native touch scrolling beat the JavaScript scrolling libraries available, which were as flaky or worse. Native scrolling had one rule: content inside a touch-scroll region had to land in the DOM in a single inject, because WebKit's paint process did not update the scroll mechanism when content was added incrementally. After each inject I forced a repaint with a small jQuery helper that set the element's transform scale to 1.

### Teardown and state

When iOS warned of low memory, the container tore down inactive views and the user came back to a white page. To reduce how often that happened, we called a routine that cleared the data and images out of a view before the container had to, which stopped most of the white flashes.

localStorage writes were fast and reads were among the slowest things we did in JavaScript, though still faster than cookies, which a hybrid app running from the file protocol does not have. I tried using localStorage to carry navigation state between the isolated views. Reading it on tap was too slow, so that approach died.

If I rebuilt the client I would run the whole app in one web view. Each App Cloud view instance cost memory even when idle, and the isolation made navigation between screens harder than it needed to be.

## Technologies and standards

Runtime: Brightcove App Cloud (HTML5 in native containers, WebKit without Nitro) with App Cloud Content Feeds for data, Brightcove Video Cloud for playback and Brightcove-supplied DRM. Client: the App Cloud SDK (jQuery and tap events), Markup.js, Moment.js, lscache over localStorage. Rendering: CSS absolute positioning with fluid layout, CSS background images with lazy loading, native touch scrolling with forced repaints. DAN also built an HTML5 advertising framework alongside the app that let brands host their own creative and use device features such as the accelerometer; it was not part of the client I built.

## Results

| Outcome | Detail |
| --- | --- |
| Shipped | iPad app late 2012; iPhone and iPod touch app 25 February 2013 |
| First in market | TVNZ described it as the first video on demand app for iPad and iPhone in New Zealand |
| Store charts | The iOS and Android apps each reached number one on their respective store charts. TVNZ also reported the initial iPad app as the number one free app on the iTunes Store after its late-2012 launch |
| Awards | 2013 Best Design Awards, Designers Institute of New Zealand: Gold, Value of Design (Emerging Value, New Initiative) and Bronze, Digital Products, both to Digital Arts Network for "TVNZ Ondemand for Mobile", with me credited as a team member |
| Home-screen image memory | 14.4 MB saving calculated at the time by using 1.5x rather than 2x assets; not measured on-device |
| Catalogue rendering | Shows page rendered six titles per inject; the single-pass version crashed WebKit |
| Platform lifespan | Brightcove announced App Cloud's discontinuation on 26 February 2013 and supported existing customers to June 2014 |

TVNZ said in February 2013 that a Samsung Galaxy app was in development; an Android tablet build followed in November 2013, and I built that app as well. The client architecture and performance work described above concern the iOS app; the store-chart results cover both platforms.

I do not have concurrent-stream, crash-rate, session-length or download figures for the apps.

## What I would take from it

Budget for decoded pixels as well as JPEG kilobytes. Calculating image-memory costs helped guide asset sizing on memory-constrained devices.

Do not hand WebKit the whole catalogue. A templating library that wants the full array in one pass is a crash on a constrained web view.

Layout belongs in CSS. JavaScript that sizes and positions on rotate was the default in hybrid kits of the time, and it was the slow path.

Isolated web views are a memory model, not a navigation model. Five tabs that cannot share state, plus teardown under memory pressure, is why the DOM-clearing routine existed.

Make the trade-off visible and let the client own it. The 300 ms decision was TVNZ's to make once we had shown them what they were trading.
