SRGSSR / pillarbox-apple

A next-generation reactive media playback ecosystem for Apple platforms.
https://testflight.apple.com/join/TS6ngLqf
MIT License
60 stars 10 forks source link

Provide insights about player CPU / memory usage #1071

Closed defagos closed 1 day ago

defagos commented 3 days ago

As a developer using Pillarbox, and as a Pillarbox developer as well, I would like to better understand resources required by media playback so that I can make a better use of them in contexts where several player instances are needed.

Acceptance criteria

Tasks

defagos commented 3 days ago

As described in AVFoundation legacy documentation, media playback is actually performed by an out-of-process daemon:

In iOS and tvOS, the situation can be even more serious because media operations are performed by the shared media services daemon

This daemon is mediaplaybackd and its activity can be investigated with Instruments Activity Monitor instrument (Instrument might take quite a while before starting a session, be patient). There are other interesting media-related daemons like mediaparserd which, I guess, handles HLS playlist parsing:

Image

defagos commented 3 days ago

Investigation method

We can check what happens with the simple following code which makes it possible to interactively add players loaded with the same content:

import AVFoundation
import SwiftUI

struct ContentView: View {
    @State private var players: [AVPlayer] = []

    var body: some View {
        Button(action: addPlayer) {
            Text("Add player")
        }
    }

    private func addPlayer() {
        players.append(.init(
            url: URL(string: "...")!)
        )
    }
}

#Preview {
    ContentView()
}

and the following procedure:

  1. Tap to add a player (paused).
  2. Wait until CPU activity drops to 0 (all content buffered).
  3. Check memory in use.

I repeated the procedure with two different HLS streams, Bip Bop and the trailer for episode 1 of the Morning Show. Each series of screenshots shows the status reached after having loaded 0 to 4 players.

Bip Bop

Image Image Image Image Image

The Morning Show

Image Image Image Image Image

Learnings