channelStateInformation.c 2.6 KB
Newer Older
1
2
3
4
5
#include <stdlib.h>
#include "../defines.h"
#include "../util/common.h"
#include "../util/dbg.h"
#include "channelStateInformation.h"
6
#include "clock.h"
7

8
PrrtChannelStateInformation * PrrtChannelStateInformation_create()
9
{
10
11
    PrrtChannelStateInformation *csi = calloc(1, sizeof(PrrtChannelStateInformation));
    check_mem(csi);
12

13
14
    check(pthread_mutex_init(&csi->lock, NULL) == 0, "Mutex init failed.");

15
16
    csi->rtprop = TIMESTAMP_SPACE;
    csi->rtprop_filter_length_us = 2 * 1000 * 1000; // 2 seconds
17
    csi->deliveryRate = 0;
18
    return csi;
19
20
21

    error:
    PERROR("Should not happen.%s","");
22
    return NULL;
23
24
}

25
void PrrtChannelStateInformation_update_rtprop(PrrtChannelStateInformation *csi, prrtTimedelta_t rtprop)
26
{
27
    pthread_mutex_lock(&csi->lock);
28
29
30
31
32
33
    prrtTimestamp_t now = PrrtClock_get_current_time_us();
    csi->rtprop_expired = now > (csi->rtprop_stamp + csi->rtprop_filter_length_us);
    if (rtprop >= 0 && (rtprop <= csi->rtprop || csi->rtprop_expired)) {
        csi->rtprop = rtprop;
        csi->rtprop_stamp = now;
    }
34
    pthread_mutex_unlock(&csi->lock);
35
36
}

Andreas Schmidt's avatar
Andreas Schmidt committed
37
38
39
40
41
42
43
void PrrtChannelStateInformation_update_plr(PrrtChannelStateInformation *csi, prrtSequenceNumber_t erasures,
                                                   prrtSequenceNumber_t packets) {
    pthread_mutex_lock(&csi->lock);
    csi->plr = ((float) erasures) / packets;
    pthread_mutex_unlock(&csi->lock);
}

44

45
46
47
48
49
void PrrtChannelStateInformation_update_delivery_rate(PrrtChannelStateInformation *csi, prrtDeliveryRate_t rate) {
    pthread_mutex_lock(&csi->lock);
    csi->deliveryRate = rate;
    pthread_mutex_unlock(&csi->lock);
}
50
51
52
53
54
void PrrtChannelStateInformation_update_app_limited(PrrtChannelStateInformation *csi, bool appLimited) {
    pthread_mutex_lock(&csi->lock);
    csi->appLimited = appLimited;
    pthread_mutex_unlock(&csi->lock);
}
55

56
prrtTimedelta_t PrrtChannelStateInformation_get_rtprop(PrrtChannelStateInformation *csi)
57
{
58
    pthread_mutex_lock(&csi->lock);
59
    prrtTimedelta_t res = csi->rtprop;
60
61
    pthread_mutex_unlock(&csi->lock);
    return res;
62
63
64
65
}

bool PrrtChannelStateInformation_destroy(PrrtChannelStateInformation *csi)
{
66
    check(pthread_mutex_destroy(&csi->lock) == EXIT_SUCCESS, "Destroy mutex failed.");
67
68
    free(csi);
    return true;
69
70
    error:
        return false;
71
}
Andreas Schmidt's avatar
Andreas Schmidt committed
72
73
74
75

prrtPacketLossRate_t PrrtChannelStateInformation_get_plr(PrrtChannelStateInformation *csi) {
    return csi->plr;
}
76
77
78

prrtDeliveryRate_t PrrtChannelStateInformation_get_delivery_rate(PrrtChannelStateInformation *csi) {
    return csi->deliveryRate;
79
80
81
82
}

bool PrrtChannelStateInformation_get_app_limited(PrrtChannelStateInformation *csi) {
    return csi->appLimited;
83
}