Improved call statistics: only compute them when dialog is visible + fixed leak

This commit is contained in:
Sylvain Berfini 2022-03-07 15:06:14 +01:00
parent c93ea7a851
commit 4c616cc395
3 changed files with 52 additions and 11 deletions

View file

@ -38,6 +38,7 @@ import org.linphone.activities.navigateToActiveCall
import org.linphone.activities.voip.viewmodels.CallsViewModel import org.linphone.activities.voip.viewmodels.CallsViewModel
import org.linphone.activities.voip.viewmodels.ConferenceViewModel import org.linphone.activities.voip.viewmodels.ConferenceViewModel
import org.linphone.activities.voip.viewmodels.ControlsViewModel import org.linphone.activities.voip.viewmodels.ControlsViewModel
import org.linphone.activities.voip.viewmodels.StatisticsListViewModel
import org.linphone.compatibility.Compatibility import org.linphone.compatibility.Compatibility
import org.linphone.core.Call import org.linphone.core.Call
import org.linphone.core.tools.Log import org.linphone.core.tools.Log
@ -50,6 +51,7 @@ class CallActivity : ProximitySensorActivity() {
private lateinit var controlsViewModel: ControlsViewModel private lateinit var controlsViewModel: ControlsViewModel
private lateinit var callsViewModel: CallsViewModel private lateinit var callsViewModel: CallsViewModel
private lateinit var conferenceViewModel: ConferenceViewModel private lateinit var conferenceViewModel: ConferenceViewModel
private lateinit var statsViewModel: StatisticsListViewModel
private var foldingFeature: FoldingFeature? = null private var foldingFeature: FoldingFeature? = null
@ -79,6 +81,8 @@ class CallActivity : ProximitySensorActivity() {
conferenceViewModel = ViewModelProvider(navControllerStoreOwner)[ConferenceViewModel::class.java] conferenceViewModel = ViewModelProvider(navControllerStoreOwner)[ConferenceViewModel::class.java]
statsViewModel = ViewModelProvider(navControllerStoreOwner)[StatisticsListViewModel::class.java]
callsViewModel.noMoreCallEvent.observe( callsViewModel.noMoreCallEvent.observe(
this this
) { ) {
@ -114,6 +118,12 @@ class CallActivity : ProximitySensorActivity() {
Compatibility.enableAutoEnterPiP(this, enabled) Compatibility.enableAutoEnterPiP(this, enabled)
} }
controlsViewModel.callStatsVisible.observe(
this
) { visible ->
if (visible) statsViewModel.enable() else statsViewModel.disable()
}
callsViewModel.currentCallData.observe( callsViewModel.currentCallData.observe(
this this
) { callData -> ) { callData ->

View file

@ -20,7 +20,6 @@
package org.linphone.activities.voip.data package org.linphone.activities.voip.data
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.contact.GenericContactData import org.linphone.contact.GenericContactData
import org.linphone.core.* import org.linphone.core.*
@ -31,18 +30,17 @@ class CallStatisticsData(val call: Call) : GenericContactData(call.remoteAddress
val isVideoEnabled = MutableLiveData<Boolean>() val isVideoEnabled = MutableLiveData<Boolean>()
private val listener = object : CoreListenerStub() { private var enabled = false
override fun onCallStatsUpdated(core: Core, call: Call, stats: CallStats) {
if (call == this@CallStatisticsData.call) { private val listener = object : CallListenerStub() {
override fun onStatsUpdated(call: Call, stats: CallStats) {
isVideoEnabled.value = call.currentParams.isVideoEnabled isVideoEnabled.value = call.currentParams.isVideoEnabled
updateCallStats(stats) updateCallStats(stats)
} }
} }
}
init { init {
coreContext.core.addListener(listener) enabled = false
audioStats.value = arrayListOf() audioStats.value = arrayListOf()
videoStats.value = arrayListOf() videoStats.value = arrayListOf()
@ -52,8 +50,18 @@ class CallStatisticsData(val call: Call) : GenericContactData(call.remoteAddress
isVideoEnabled.value = videoEnabled isVideoEnabled.value = videoEnabled
} }
fun enable() {
enabled = true
call.addListener(listener)
}
fun disable() {
enabled = false
call.removeListener(listener)
}
override fun destroy() { override fun destroy() {
coreContext.core.removeListener(listener) if (enabled) disable()
super.destroy() super.destroy()
} }

View file

@ -30,6 +30,8 @@ import org.linphone.core.CoreListenerStub
class StatisticsListViewModel : ViewModel() { class StatisticsListViewModel : ViewModel() {
val callStatsList = MutableLiveData<ArrayList<CallStatisticsData>>() val callStatsList = MutableLiveData<ArrayList<CallStatisticsData>>()
private var enabled = false
private val listener = object : CoreListenerStub() { private val listener = object : CoreListenerStub() {
override fun onCallStateChanged( override fun onCallStateChanged(
core: Core, core: Core,
@ -49,6 +51,20 @@ class StatisticsListViewModel : ViewModel() {
computeCallsList() computeCallsList()
} }
fun enable() {
enabled = true
for (stat in callStatsList.value.orEmpty()) {
stat.enable()
}
}
fun disable() {
enabled = false
for (stat in callStatsList.value.orEmpty()) {
stat.disable()
}
}
override fun onCleared() { override fun onCleared() {
callStatsList.value.orEmpty().forEach(CallStatisticsData::destroy) callStatsList.value.orEmpty().forEach(CallStatisticsData::destroy)
coreContext.core.removeListener(listener) coreContext.core.removeListener(listener)
@ -57,12 +73,19 @@ class StatisticsListViewModel : ViewModel() {
} }
private fun computeCallsList() { private fun computeCallsList() {
callStatsList.value.orEmpty().forEach(CallStatisticsData::destroy)
val list = arrayListOf<CallStatisticsData>() val list = arrayListOf<CallStatisticsData>()
for (call in coreContext.core.calls) { for (call in coreContext.core.calls) {
if (call.state != Call.State.End && call.state != Call.State.Released && call.state != Call.State.Error) { if (call.state != Call.State.End && call.state != Call.State.Released && call.state != Call.State.Error) {
list.add(CallStatisticsData(call)) val data = CallStatisticsData(call)
list.add(data)
if (enabled) {
data.enable()
} }
} }
}
callStatsList.value = list callStatsList.value = list
} }
} }