Skip to content

Commit

Permalink
Use liveStartedAt and liveEndedAt to calculate live duration (#1149)
Browse files Browse the repository at this point in the history
  • Loading branch information
liviu-timar authored Jul 25, 2024
1 parent 69aa83f commit 1bcab18
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public final class io/getstream/video/android/core/CallState {
public final fun getErrors ()Lkotlinx/coroutines/flow/StateFlow;
public final fun getIngress ()Lkotlinx/coroutines/flow/StateFlow;
public final fun getLive ()Lkotlinx/coroutines/flow/StateFlow;
public final fun getLiveDuration ()Lkotlinx/coroutines/flow/StateFlow;
public final fun getLiveDurationInMs ()Lkotlinx/coroutines/flow/StateFlow;
public final fun getLivestream ()Lkotlinx/coroutines/flow/StateFlow;
public final fun getLocalParticipant ()Lkotlinx/coroutines/flow/StateFlow;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.transform
import kotlinx.coroutines.isActive
Expand Down Expand Up @@ -125,6 +125,7 @@ import java.util.Date
import java.util.Locale
import java.util.SortedMap
import java.util.UUID
import kotlin.time.Duration
import kotlin.time.DurationUnit
import kotlin.time.toDuration

Expand Down Expand Up @@ -372,7 +373,7 @@ public class CallState(
}

/** how long the call has been running, rounded to seconds, null if the call didn't start yet */
public val duration: StateFlow<kotlin.time.Duration?> =
public val duration: StateFlow<Duration?> =
_durationInMs.transform { emit(((it ?: 0L) / 1000L).toDuration(DurationUnit.SECONDS)) }
.stateIn(scope, SharingStarted.WhileSubscribed(10000L), null)

Expand Down Expand Up @@ -422,17 +423,35 @@ public class CallState(
/** the opposite of backstage, if we are live or not */
val live: StateFlow<Boolean> = _backstage.mapState { !it }

/** how many milliseconds the call has been running, null if the call didn't start yet */
public val liveDurationInMs: StateFlow<Long?> =
_durationInMs
.map {
if (live.value) {
it
} else {
null
}
/**
* How long the call has been live for, in milliseconds, or null if the call hasn't been live yet.
* Keeps its value when live ends and resets when live starts again.
*
* @see [liveDuration]
*/
public val liveDurationInMs = flow {
while (currentCoroutineContext().isActive) {
delay(1000)

val liveStartedAt = _session.value?.liveStartedAt
val liveEndedAt = _session.value?.liveEndedAt ?: OffsetDateTime.now()

liveStartedAt?.let {
val duration = liveEndedAt.toInstant().toEpochMilli() - liveStartedAt.toInstant().toEpochMilli()
emit(duration)
}
.stateIn(scope, SharingStarted.WhileSubscribed(10000L), null)
}
}.distinctUntilChanged().stateIn(scope, SharingStarted.WhileSubscribed(10000L), null)

/**
* How long the call has been live for, represented as [Duration], or null if the call hasn't been live yet.
* Keeps its value when live ends and resets when live starts again.
*
* @see [liveDurationInMs]
*/
public val liveDuration = liveDurationInMs.mapState { durationInMs ->
durationInMs?.takeIf { it >= 1000 }?.let { (it / 1000).toDuration(DurationUnit.SECONDS) }
}

private val _egress: MutableStateFlow<EgressResponse?> = MutableStateFlow(null)
val egress: StateFlow<EgressResponse?> = _egress
Expand Down

0 comments on commit 1bcab18

Please sign in to comment.