Statistics
| Branch: | Revision:

chunker-player / chunker_streamer / chunk_pusher.c @ 9996b19f

History | View | Annotate | Download (2.29 KB)

1
#include <stdlib.h>
2
#include <string.h>
3

    
4
#include "external_chunk_transcoding.h"
5
#include "chunker_streamer.h"
6

    
7
//#define DEBUG_PUSHER
8

    
9

    
10
int pushChunkHttp(ExternalChunk *echunk, char *url);
11

    
12
extern ChunkerMetadata *cmeta;
13

    
14

    
15
int pushChunkHttp(ExternalChunk *echunk, char *url) {
16

    
17
        Chunk gchunk;
18
        void *grapes_chunk_attributes_block = NULL;
19
        int ret = STREAMER_FAIL_RETURN;
20
        //we need to pack 5 int32s + 2 timeval structs + 1 double
21
        static size_t ExternalChunk_header_size = 5*CHUNK_TRANSCODING_INT_SIZE + 2*CHUNK_TRANSCODING_INT_SIZE + 2*CHUNK_TRANSCODING_INT_SIZE + 1*CHUNK_TRANSCODING_INT_SIZE*2;
22
        
23
        //update the chunk len here because here we know the external chunk header size
24
        echunk->len = echunk->payload_len + ExternalChunk_header_size;
25

    
26
        /* first pack the chunk info that we get from the streamer into an "attributes" block of a regular GRAPES chunk */
27
        if(        (grapes_chunk_attributes_block = packExternalChunkToAttributes(echunk, ExternalChunk_header_size)) != NULL ) {
28
                struct timeval now;
29

    
30
                /* then fill-up a proper GRAPES chunk */
31
                gchunk.size = echunk->payload_len;
32
                /* then fill the timestamp */
33
                gettimeofday(&now, NULL);
34
                gchunk.timestamp = now.tv_sec * 1000000ULL + now.tv_usec;
35

    
36
                //decide how to create the chunk ID
37
                if(cmeta->cid == 0) {
38
                        gchunk.id = echunk->seq;
39
#ifdef DEBUG_PUSHER
40
                        fprintf(stderr, "PUSHER: packaged SEQ chunkID %d\n", gchunk.id);
41
#endif
42
                }
43
                else if(cmeta->cid == 1) {
44
                        gchunk.id = gchunk.timestamp; //its ID is its start time
45
#ifdef DEBUG_PUSHER
46
                        fprintf(stderr, "PUSHER: packaged TS chunkID %d\n", gchunk.id);
47
#endif
48
                }
49
                else if(cmeta->cid == 2) {
50
                        //its ID is offset by actual time in seconds
51
                        gchunk.id = echunk->seq + cmeta->base_chunkid_sequence_offset;
52
#ifdef DEBUG_PUSHER
53
                        fprintf(stderr, "PUSHER: packaged SEQ %d + %d offset chunkID %d\n", echunk->seq, cmeta->base_chunkid_sequence_offset, gchunk.id);
54
#endif
55
                }
56
                gchunk.attributes = grapes_chunk_attributes_block;
57
                gchunk.attributes_size = ExternalChunk_header_size;
58
                gchunk.data = echunk->data;
59

    
60
#ifdef NHIO
61
                write_chunk(&gchunk);
62
#else
63
                /* 20 bytes are needed to put the chunk header info on the wire + attributes size + payload */
64
                ret = sendViaCurl(gchunk, GRAPES_ENCODED_CHUNK_HEADER_SIZE + gchunk.attributes_size + gchunk.size, url);
65
#endif
66

    
67
                free(grapes_chunk_attributes_block);
68
                return ret;
69
        }
70
        return ret;
71
}