SRGSSR / srgmediaplayer-apple

An advanced media player library, simple and reliable
MIT License
158 stars 33 forks source link

Reduce player activity #75

Closed defagos closed 4 years ago

defagos commented 4 years ago

Version 3.0.0 introduced KVO for more controller properties, reducing the need for periodic time observation during playback.

Further improvements In the way timers are managed can be made. This should further reduce the rate of update and thus improve player energy efficiency.

defagos commented 4 years ago

In 3.0.0 we are using basically two kinds of timers:

We also have a periodic time observer concept, similar to the one of AVPlayer, but ensuring that updates also occur when playback is paused. Those were essentially meant for correct UI refreshes when playing DVR streams.

Looking into timers a bit more, this situation is sub-optimal:

To improve the situation, I propose the following internal changes:

A good thing is that NSTimers are automatically suspended by the system when the app is sent to the background. For media playback apps, having background audio enabled, they will continue to fire in background if content is being played, which is similar to what periodic time observers do (they are themselves internally implemented with GCD dispatch sources).

Contrary to what I initially thought, this does not require us to implement a special kind of timer to support the kind of behavior we need:

In any case, if nothing is being played and the application is sent to the background, timers will not be fired.

defagos commented 4 years ago

To measure the result of above measures, the energy protocol we thought of should be applied for all players in the demo:

The protocol should be repeated in the following cases:

By comparing the values we get for we can evaluate the efficiency of our implementation against the standard Apple experience.

It is important to check livestreams and on-demand streams separately, as the refresh needs are different. Checking with Instruments time profiler tool, this is true for the system player as well, which exhibits extra refresh activity for livestreams, probably due to time range changes.

It would probably be interesting to deliver a visual representation of the above cases, reduced to 1 minute, as delivered by Instruments with its time and energy profiler tools (including results of 2.0.0 for comparison purposes).

defagos commented 4 years ago

Here are a few activity traces, captured for a minute in the above cases. Only for audio (video would be the same, though with more activity).

I compared the native AVPlayerViewController with the AdvancedPlayerViewController demo, which uses all views provided by our library. Background corresponds bot to the app being sent to the background or the device being locked. The advanced player is compared with the latest release (3.0) and the one updated for energy efficiency.

Note that the native AVPlayerViewController provides basic control center integration, which can lead to additional CPU time being used (but this should not be reasonable, as they probably let the system calculate the position).

Test setup:

The complete Instrument traces can be downloaded here for further analysis.

AOD, playing in foreground

AOD, playing in background

Audio livestream, playing in foreground

Audio livestream, playing in background

AOD, paused in foreground

AOD, paused in background

Audio livestream, paused in foreground

Audio livestream, paused in background

defagos commented 4 years ago

After looking at Instrument time profiling statistics, I suspected we could gain some more battery life by replacing the NSDateComponentsFormatters we use, and by having accessibility labels being returns as readonly properties somehow (i.e. only when an item gets the focus).

After having checked with a simple formatter implementation costing far less more according to Instruments, I didn't experience better mAh consumed in comparison to the current implementation. I therefore recommend we stick with the current system formatters.

defagos commented 4 years ago

Both the system and the advanced player use ~120 mAh on my test iPhone 7 for 1 hour of AOD playback in foreground (screen always on, min brightness). The measures were repeated 3 times to eliminate any discrepancy.