<commit_before>package io.clappr.player.playback

import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.analytics.AnalyticsListener
import com.google.android.exoplayer2.analytics.AnalyticsListener.EventTime
import com.google.android.exoplayer2.source.MediaSourceEventListener.LoadEventInfo
import com.google.android.exoplayer2.source.MediaSourceEventListener.MediaLoadData
import io.clappr.player.bitrate.BitrateHistory
import io.clappr.player.log.Logger

class ExoPlayerBitrateLogger(
    private val bitrateHistory: BitrateHistory = BitrateHistory { System.nanoTime() },
    private val didUpdateBitrate: (bitrate: Long) -> Unit
) : AnalyticsListener {

    private var audio = 0L
    private var video = 0L

    var lastBitrate: Long = 0
        private set(value) {

            val oldValue = field

            field = value

            try {
                bitrateHistory.addBitrate(field)
            } catch (e: BitrateHistory.BitrateLog.WrongTimeIntervalException) {
                Logger.error(ExoPlayerBitrateLogger::class.java.simpleName, e.message
                    ?: "Can not add bitrate on history")
            }

            if (oldValue != field) {
                didUpdateBitrate(field)
            }
        }

    override fun onLoadCompleted(eventTime: EventTime?, loadEventInfo: LoadEventInfo?, mediaLoadData: MediaLoadData) {
        when (mediaLoadData.trackType) {
            C.TRACK_TYPE_DEFAULT, C.TRACK_TYPE_VIDEO -> {
                mediaLoadData.trackFormat?.bitrate?.let {
                    video = it.toLong()
                }
            }
            C.TRACK_TYPE_AUDIO -> {
                mediaLoadData.trackFormat?.bitrate
                    ?.takeIf { it > 0 }
                    ?.let { audio = it.toLong() }
            }
        }
        lastBitrate = if ((video + audio) > 0) video + audio else 0
    }

    fun averageBitrate() = bitrateHistory.averageBitrate()
    fun clearHistory() = bitrateHistory.clear()
}<commit_msg>refactor: simplify lastBitrate calculation<commit_after>package io.clappr.player.playback

import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.analytics.AnalyticsListener
import com.google.android.exoplayer2.analytics.AnalyticsListener.EventTime
import com.google.android.exoplayer2.source.MediaSourceEventListener.LoadEventInfo
import com.google.android.exoplayer2.source.MediaSourceEventListener.MediaLoadData
import io.clappr.player.bitrate.BitrateHistory
import io.clappr.player.log.Logger
import kotlin.math.max

class ExoPlayerBitrateLogger(
    private val bitrateHistory: BitrateHistory = BitrateHistory { System.nanoTime() },
    private val didUpdateBitrate: (bitrate: Long) -> Unit
) : AnalyticsListener {

    private var audio = 0L
    private var video = 0L

    var lastBitrate: Long = 0
        private set(value) {

            val oldValue = field

            field = value

            try {
                bitrateHistory.addBitrate(field)
            } catch (e: BitrateHistory.BitrateLog.WrongTimeIntervalException) {
                Logger.error(ExoPlayerBitrateLogger::class.java.simpleName, e.message
                    ?: "Can not add bitrate on history")
            }

            if (oldValue != field) {
                didUpdateBitrate(field)
            }
        }

    override fun onLoadCompleted(eventTime: EventTime?, loadEventInfo: LoadEventInfo?, mediaLoadData: MediaLoadData) {
        when (mediaLoadData.trackType) {
            C.TRACK_TYPE_DEFAULT, C.TRACK_TYPE_VIDEO -> {
                mediaLoadData.trackFormat?.bitrate?.let {
                    video = it.toLong()
                }
            }
            C.TRACK_TYPE_AUDIO -> {
                mediaLoadData.trackFormat?.bitrate
                    ?.takeIf { it > 0 }
                    ?.let { audio = it.toLong() }
            }
        }
        lastBitrate = max(video + audio, 0L)
    }

    fun averageBitrate() = bitrateHistory.averageBitrate()
    fun clearHistory() = bitrateHistory.clear()
}
{'fraction_non_alphanumeric': 0.08814062886853181, 'fraction_numerical': 0.005199306759098787, 'mean_word_length': 2.3087633087633086, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4557518', 'n_tokens_mistral': 1172, 'n_tokens_neox': 1114, 'n_words': 210}