aboutsummaryrefslogtreecommitdiffstats
path: root/src/sls.h
blob: 6dc9d47eb4adf4717bad4e24ced4ff2da84902cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
    Copyright (C) 2014 CSP Innovazione nelle ICT s.c.a r.l. (http://rd.csp.it/)

    Copyright (C) 2014, 2015 Matthias P. Braendli (http://opendigitalradio.org)

    Copyright (C) 2015, 2016, 2017 Stefan Pöschel (http://opendigitalradio.org)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/*!
    \file sls.h
    \brief Slideshow related code

    \author Sergio Sagliocco <sergio.sagliocco@csp.it>
    \author Matthias P. Braendli <matthias@mpb.li>
    \author Stefan Pöschel <odr@basicmaster.de>
*/

#ifndef SLS_H_
#define SLS_H_

#include "common.h"
#include "pad_common.h"

#if HAVE_MAGICKWAND
#  include <wand/magick_wand.h>
#endif

#include <sys/stat.h>
#include <deque>
#include <fstream>
#include <iostream>
#include <algorithm>


// --- MSCDG -----------------------------------------------------------------
struct MSCDG {
    // MSC Data Group Header (extension field not supported)
    unsigned char extflag;      //  1 bit
    unsigned char crcflag;      //  1 bit
    unsigned char segflag;      //  1 bit
    unsigned char accflag;      //  1 bit
    unsigned char dgtype;       //  4 bits
    unsigned char cindex;       //  4 bits
    unsigned char rindex;       //  4 bits
    /// Session header - Segment field
    unsigned char last;         //  1 bit
    unsigned short int segnum;  // 16 bits
    // Session header - User access field
    unsigned char rfa;          //  3 bits
    unsigned char tidflag;      //  1 bit
    unsigned char lenid;        //  4 bits - Fixed to value 2 in this implemntation
    unsigned short int tid;     // 16 bits
    // MSC data group data field
    //  Mot Segmentation header
    unsigned char rcount;       //  3 bits
    unsigned short int seglen;  // 13 bits
    // Mot segment
    unsigned char* segdata;
    // MSC data group CRC
    unsigned short int crc;     // 16 bits
};


// --- slide_metadata_t -----------------------------------------------------------------
/*! Between collection of slides and transmission, the slide data is saved
 * in this structure.
 */
struct slide_metadata_t {
    // complete path to slide
    std::string filepath;

    // index, values from 0 to MAXSLIDEID, rolls over
    int fidx;

    // This is used to define the order in which several discovered
    // slides are transmitted
    bool operator<(const slide_metadata_t& other) const {
        return this->fidx < other.fidx;
    }
};


// --- fingerprint_t -----------------------------------------------------------------
/*! A simple fingerprint for each slide transmitted.
 * Allows us to reuse the same fidx if the same slide
 * is transmitted more than once.
 */
struct fingerprint_t {
    // file name
    std::string s_name;
    // file size, in bytes
    off_t s_size;
    // time of last modification
    unsigned long s_mtime;

    // assigned fidx, -1 means invalid
    int fidx;

    /*! The comparison is not done on fidx, only
     * on the file-specific data
     */
    bool operator==(const fingerprint_t& other) const {
        return (((s_name == other.s_name &&
                 s_size == other.s_size) &&
                s_mtime == other.s_mtime));
    }

    void disp(void) {
        printf("%s_%ld_%lu:%d\n", s_name.c_str(), s_size, s_mtime, fidx);
    }

    void load_from_file(const char* filepath)
    {
        struct stat file_attribue;
        const char * final_slash;

        stat(filepath, &file_attribue);
        final_slash = strrchr(filepath, '/');

        // load filename, size and mtime
        // Save only the basename of the filepath
        this->s_name.assign((final_slash == NULL) ? filepath : final_slash + 1);
        this->s_size = file_attribue.st_size;
        this->s_mtime = file_attribue.st_mtime;

        this->fidx = -1;
    }
};


// --- History -----------------------------------------------------------------
/*! We keep track of transmitted files so that we can retransmit
 * identical slides with the same index, in case the receivers cache
 * them.
 *
 * \c MAXHISTORYLEN defines for how how many slides we want to keep this
 * history.
 */
class History {
    public:
        History() : History(MAXHISTORYLEN) {}
        History(size_t hist_size) :
            m_hist_size(hist_size),
            m_last_given_fidx(0) {}
        void disp_database();
        // controller of id base on database
        int get_fidx(const char* filepath);

    private:
        static const size_t MAXHISTORYLEN;
        static const int    MAXSLIDEID;

        std::deque<fingerprint_t> m_database;

        size_t m_hist_size;

        int m_last_given_fidx;

        // find the fingerprint fp in database.
        // returns the fidx when found,
        //    or   -1 if not found
        int find(const fingerprint_t& fp) const;

        // add a new fingerprint into database
        // returns its fidx
        void add(fingerprint_t& fp);
};


// --- MOTHeader -----------------------------------------------------------------
class MOTHeader {
private:
    size_t header_size;
    uint8_vector_t data;

    void IncrementHeaderSize(size_t size);
    void AddParamHeader(int pli, int param_id) {data.push_back((pli << 6) | (param_id & 0x3F));}

    void AddExtensionFixedSize(int pli, int param_id, const uint8_t* data_field, size_t data_field_len);
    void AddExtensionVarSize(int param_id, const uint8_t* data_field, size_t data_field_len);
public:
    MOTHeader(size_t body_size, int content_type, int content_subtype);

    void AddExtension(int param_id, const uint8_t* data_field, size_t data_field_len);
    const uint8_vector_t GetData() {return data;}
};


// --- SLSManager -----------------------------------------------------------------
class SLSManager {
private:
    static const size_t MAXSEGLEN;
    static const size_t MAXSLIDESIZE;
    static const int    MINQUALITY;
    static const std::string SLS_PARAMS_SUFFIX;

    void warnOnSmallerImage(size_t height, size_t width, const std::string& fname);
#if HAVE_MAGICKWAND
    size_t resizeImage(MagickWand* m_wand, unsigned char** blob, const std::string& fname, bool* jfif_not_png);
#endif
    bool parse_sls_param_id(const std::string &key, const std::string &value, uint8_t &target);
    bool check_sls_param_len(const std::string &key, size_t len, size_t len_max);
    void process_mot_params_file(MOTHeader& header, const std::string &params_fname);
    uint8_vector_t createMotHeader(size_t blobsize, int fidx, bool jfif_not_png, const std::string &params_fname);
    void createMscDG(MSCDG* msc, unsigned short int dgtype,
            int *cindex, unsigned short int segnum, unsigned short int lastseg,
            unsigned short int tid, unsigned char* data,
            unsigned short int datalen);
    DATA_GROUP* packMscDG(MSCDG* msc);

    PADPacketizer* pad_packetizer;
    int cindex_header;
    int cindex_body;
public:
    SLSManager(PADPacketizer* pad_packetizer) : pad_packetizer(pad_packetizer), cindex_header(0), cindex_body(0) {}

    bool encodeFile(const std::string& fname, int fidx, bool raw_slides);
    static bool isSlideParamFileFilename(const std::string& filename);
};

#endif /* SLS_H_ */