channelStateInformation.c 1.48 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
    return csi;
18
19
20

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

24
void PrrtChannelStateInformation_update_rtprop(PrrtChannelStateInformation *csi, prrtTimedelta_t rtprop)
25
{
26
    pthread_mutex_lock(&csi->lock);
27
28
29
30
31
32
    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;
    }
33
    pthread_mutex_unlock(&csi->lock);
34
35
}

36

37
prrtTimedelta_t PrrtChannelStateInformation_get_rtprop(PrrtChannelStateInformation *csi)
38
{
39
    pthread_mutex_lock(&csi->lock);
40
    prrtTimedelta_t res = csi->rtprop;
41
42
    pthread_mutex_unlock(&csi->lock);
    return res;
43
44
45
46
}

bool PrrtChannelStateInformation_destroy(PrrtChannelStateInformation *csi)
{
47
    check(pthread_mutex_destroy(&csi->lock) == EXIT_SUCCESS, "Destroy mutex failed.");
48
49
    free(csi);
    return true;
50
51
    error:
        return false;
52
}