UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
fec.cpp
Go to the documentation of this file.
1
7/*
8 * Copyright (c) 2014-2024 CESNET
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, is permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 *
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * 3. Neither the name of CESNET nor the names of its contributors may be
23 * used to endorse or promote products derived from this software without
24 * specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
28 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
29 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
30 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
31 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
37 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
40#include "rtp/fec.h"
41
42#include <cassert> // for assert
43#include <cstdlib> // for abort, free
44#include <cstring> // for strlen, strncmp, strtok_r, strdup
45#include <exception> // for exception
46#include <ostream> // for operator<<, basic_ostream, basic_ost...
47#include <string>
48
49#include "debug.h"
50#include "rtp/ldgm.h"
51#include "rtp/rs.h"
52#include "rtp/rtp_callback.h"
53#include "rtp/rtp_types.h" // for PT_ENCRYPT_VIDEO, PT_ENCRYPT_VIDEO_LDGM
54#include "ug_runtime_error.hpp"
55#include "utils/macros.h"
56
57using std::exception;
58using std::stof;
59using std::stoi;
60using std::string;
61
62fec *fec::create_from_config(const char *c_str, bool is_audio) noexcept
63{
64 try {
65 if (strncmp(c_str, "LDGM percents ", strlen("LDGM percents ")) == 0) {
66 char *str = strdup(c_str);
67 char *ptr = str + strlen("LDGM percents ");
68 char *save_ptr = nullptr;
69 char *item = nullptr;
70 item = strtok_r(ptr, " ", &save_ptr);
71 assert (item != nullptr);
72 int mtu_len = stoi(item);
73 assert(mtu_len > 0);
74 item = strtok_r(nullptr, " ", &save_ptr);
75 assert (item != nullptr);
76 int data_len = stoi(item);
77 assert(data_len > 0);
78 item = strtok_r(nullptr, " ", &save_ptr);
79 assert (item != nullptr);
80 double loss_pct = stof(item);
81 assert(loss_pct > 0.0);
82 fec *ret = new ldgm(mtu_len, data_len, loss_pct);
83 free(str);
84 return ret;
85 }
86 if (strncmp(c_str, "LDGM cfg ", strlen("LDGM cfg ")) == 0) {
87 return new ldgm(c_str + strlen("LDGM cfg "));
88 }
89 if (strncmp(c_str, "RS cfg ", strlen("RS cfg ")) == 0) {
90 return new rs(c_str + strlen("rs cfg "), is_audio);
91 }
92 throw ug_runtime_error("Unrecognized FEC configuration!");
93 } catch (string const &s) {
94 LOG(LOG_LEVEL_ERROR) << s << "\n";
95 } catch (exception const &e) {
96 LOG(LOG_LEVEL_ERROR) << e.what() << "\n";
97 } catch (int i) {
98 if (i != 0) {
99 LOG(LOG_LEVEL_ERROR) << "FEC initialization returned " << i << "\n";
100 }
101 } catch (...) {
102 LOG(LOG_LEVEL_ERROR) << "Unknown FEC error!\n";
103 }
104 return nullptr;
105}
106
108{
109 try {
110 switch (desc.type) {
111 case FEC_LDGM:
112 return new ldgm(desc.k, desc.m, desc.c, desc.seed);
113 case FEC_RS:
114 return new rs(desc.k, desc.k + desc.m);
115 default:
116 abort();
117 }
118 } catch (string const &s) {
119 LOG(LOG_LEVEL_ERROR) << s << "\n";
120 } catch (exception const &e) {
121 LOG(LOG_LEVEL_ERROR) << e.what() << "\n";
122 } catch (int i) {
123 if (i != 0) {
124 LOG(LOG_LEVEL_ERROR) << "FEC initialization returned " << i << "\n";
125 }
126 } catch (...) {
127 LOG(LOG_LEVEL_ERROR) << "Unknown FEC error!\n";
128 }
129 return nullptr;
130
131}
132
133int fec::pt_from_fec_type(enum tx_media_type media_type, enum fec_type fec_type, bool encrypted) throw()
134{
135 if (media_type == TX_MEDIA_VIDEO) {
136 switch (fec_type) {
137 case FEC_NONE:
138 return encrypted ? PT_ENCRYPT_VIDEO : PT_VIDEO;
139 case FEC_LDGM:
140 return encrypted ? PT_ENCRYPT_VIDEO_LDGM : PT_VIDEO_LDGM;
141 case FEC_RS:
142 return encrypted ? PT_ENCRYPT_VIDEO_RS : PT_VIDEO_RS;
143 default: break;
144 }
145 } else {
146 switch (fec_type) {
147 case FEC_NONE:
148 return encrypted ? PT_ENCRYPT_AUDIO : PT_AUDIO;
149 case FEC_RS:
150 return encrypted ? PT_ENCRYPT_AUDIO_RS : PT_AUDIO_RS;
151 default: break;
152 }
153 }
154 UG_ASSERT(0 && "Unsupported media/FEC type combination");
155}
156
157enum fec_type fec::fec_type_from_pt(int pt) throw()
158{
159 switch (pt) {
160 case PT_VIDEO:
161 case PT_ENCRYPT_VIDEO:
162 return FEC_NONE;
163 case PT_VIDEO_LDGM:
165 return FEC_LDGM;
166 case PT_VIDEO_RS:
168 return FEC_RS;
169 default:
170 abort();
171 }
172}
173
174
175int fec_pt_from_fec_type(enum tx_media_type media_type, enum fec_type fec_type, bool encrypted)
176{
177 return fec::pt_from_fec_type(media_type, fec_type, encrypted);
178}
179
struct video_desc desc
#define LOG_LEVEL_ERROR
general errors
Definition debug.h:59
#define LOG(level)
Definition debug.h:313
int fec_pt_from_fec_type(enum tx_media_type media_type, enum fec_type fec_type, bool encrypted)
Definition fec.cpp:175
#define UG_ASSERT(cond)
unconditional alternative to assert that is not affected by NDEBUG macro
Definition macros.h:78
#define PT_ENCRYPT_VIDEO
Definition rtp_types.h:69
#define PT_AUDIO
Definition rtp_types.h:67
#define PT_ENCRYPT_AUDIO_RS
Definition rtp_types.h:76
#define PT_ENCRYPT_VIDEO_RS
Definition rtp_types.h:74
#define PT_VIDEO
Definition rtp_types.h:66
#define PT_VIDEO_LDGM
Definition rtp_types.h:68
#define PT_AUDIO_RS
Definition rtp_types.h:75
#define PT_VIDEO_RS
Definition rtp_types.h:72
#define PT_ENCRYPT_AUDIO
Definition rtp_types.h:70
#define PT_ENCRYPT_VIDEO_LDGM
Definition rtp_types.h:73
Definition fec.h:51
static fec * create_from_desc(struct fec_desc) noexcept
Definition fec.cpp:107
static enum fec_type fec_type_from_pt(int pt)
Definition fec.cpp:157
static fec * create_from_config(const char *str, bool is_audio) noexcept
Definition fec.cpp:62
static int pt_from_fec_type(enum tx_media_type media_type, enum fec_type fec_type, bool encrypted)
Definition fec.cpp:133
Definition hd-rum.c:27
Definition ldgm.h:54
Definition rs.h:49
tx_media_type
Definition types.h:363
@ TX_MEDIA_VIDEO
Definition types.h:365
fec_type
Definition types.h:200
@ FEC_LDGM
Definition types.h:203
@ FEC_RS
Definition types.h:204
@ FEC_NONE
Definition types.h:201