Statistics
| Branch: | Revision:

chunker-player / external_chunk_transcoding.c @ a7177cd5

History | View | Annotate | Download (3.85 KB)

1
#include <string.h>
2
#include <stdint.h>
3
#include <stdio.h>
4

    
5
#include "external_chunk_transcoding.h"
6

    
7
static inline int bit32_encoded_pull(uint8_t *p)
8
{
9
        int tmp;
10
  
11
        memcpy(&tmp, p, CHUNK_TRANSCODING_INT_SIZE);
12
        tmp = ntohl(tmp);
13

    
14
        return tmp;
15
}
16

    
17
static inline void bit32_encoded_push(uint32_t v, uint8_t *p)
18
{
19
        uint32_t tmp;
20
  
21
        tmp = htonl(v);
22
        memcpy(p, &tmp, CHUNK_TRANSCODING_INT_SIZE);
23
}
24

    
25
void print_block(const uint8_t *b, int size) {
26
int i=0;
27
printf("BEGIN OF %d BYTES---\n", size);
28
for(i=0; i<size; i++) {
29
printf("%d ", *(b+i));
30
}
31
printf("END OF %d BYTES---\n", size);
32
}
33

    
34
void chunker_logger(const char *s) {
35
        printf("%s\n", s);
36
}
37

    
38

    
39
void *packExternalChunkToAttributes(ExternalChunk *echunk, size_t attr_size) {
40
        void *attr_block = NULL;
41
        int64_t half_prio;
42
        int64_t prio = 0.0;
43
        static size_t needed_space = 0;
44
        
45
        needed_space = 5*CHUNK_TRANSCODING_INT_SIZE + 2*CHUNK_TRANSCODING_INT_SIZE + 2*CHUNK_TRANSCODING_INT_SIZE + 1*CHUNK_TRANSCODING_INT_SIZE*2;
46
        
47
        if(attr_size != needed_space) {
48
                chunker_logger("space mismatch in expected allocation of attrib block size!");
49
                return NULL;
50
        }
51
        
52
        if( (attr_block = malloc(needed_space)) == NULL ) {
53
                chunker_logger("attrib block malloc failed!");
54
                return NULL;
55
        }
56
        
57
        /* copy the content of the external_chunk structure into a proper attributes block */
58
        /* also network-encoding the 4bytes pieces */
59
        bit32_encoded_push(echunk->seq, attr_block);
60
        bit32_encoded_push(echunk->frames_num, attr_block + CHUNK_TRANSCODING_INT_SIZE);
61
        
62
        /* unfold the timeval structure fields */
63
        bit32_encoded_push(echunk->start_time.tv_sec, attr_block + CHUNK_TRANSCODING_INT_SIZE*2);
64
        bit32_encoded_push(echunk->start_time.tv_usec, attr_block + CHUNK_TRANSCODING_INT_SIZE*3);
65
        bit32_encoded_push(echunk->end_time.tv_sec, attr_block + CHUNK_TRANSCODING_INT_SIZE*4);
66
        bit32_encoded_push(echunk->end_time.tv_usec, attr_block + CHUNK_TRANSCODING_INT_SIZE*5);
67
        
68
        bit32_encoded_push(echunk->payload_len, attr_block + CHUNK_TRANSCODING_INT_SIZE*6);
69
        bit32_encoded_push(echunk->len, attr_block + CHUNK_TRANSCODING_INT_SIZE*7);
70
        bit32_encoded_push(echunk->category, attr_block + CHUNK_TRANSCODING_INT_SIZE*8);
71
        /* this is a double, should be 64bits, split it */
72
        prio = (uint64_t)echunk->priority;
73
        half_prio = prio >> 32;
74
        bit32_encoded_push(half_prio, attr_block + CHUNK_TRANSCODING_INT_SIZE*9);
75
        half_prio = prio;
76
        bit32_encoded_push(half_prio, attr_block + CHUNK_TRANSCODING_INT_SIZE*10);
77
        /* ref count is not needed over the wire */
78
        
79
        return attr_block;
80
}
81

    
82
ExternalChunk *grapesChunkToExternalChunk(Chunk *gchunk) {
83
        uint64_t tmp_prio;
84
        ExternalChunk *echunk = (ExternalChunk *)malloc(sizeof(ExternalChunk));
85
        if(!echunk) {
86
                printf("Memory error in chunkToExternalchunk!\n");
87
                return NULL;
88
        }
89
        /* pull out info from the attributes block from the grapes chunk */
90
                echunk->seq = bit32_encoded_pull(gchunk->attributes);
91
                echunk->frames_num = bit32_encoded_pull(gchunk->attributes + CHUNK_TRANSCODING_INT_SIZE);
92
                echunk->start_time.tv_sec = bit32_encoded_pull(gchunk->attributes + CHUNK_TRANSCODING_INT_SIZE*2);
93
                echunk->start_time.tv_usec = bit32_encoded_pull(gchunk->attributes + CHUNK_TRANSCODING_INT_SIZE*3);
94
                echunk->end_time.tv_sec = bit32_encoded_pull(gchunk->attributes + CHUNK_TRANSCODING_INT_SIZE*4);
95
                echunk->end_time.tv_usec = bit32_encoded_pull(gchunk->attributes + CHUNK_TRANSCODING_INT_SIZE*5);
96
                echunk->payload_len = bit32_encoded_pull(gchunk->attributes + CHUNK_TRANSCODING_INT_SIZE*6);
97
                echunk->len = bit32_encoded_pull(gchunk->attributes + CHUNK_TRANSCODING_INT_SIZE*7);
98
                echunk->category = bit32_encoded_pull(gchunk->attributes + CHUNK_TRANSCODING_INT_SIZE*8);
99
                tmp_prio = bit32_encoded_pull(gchunk->attributes + CHUNK_TRANSCODING_INT_SIZE*9);
100
                tmp_prio = tmp_prio << 32;
101
                tmp_prio |= bit32_encoded_pull(gchunk->attributes + CHUNK_TRANSCODING_INT_SIZE*10);
102
                echunk->priority = (double)tmp_prio;
103

    
104
                /* pass the payload along */
105
                echunk->data = gchunk->data;
106

    
107
        return echunk;
108
}