-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathwriter.cpp
More file actions
222 lines (192 loc) · 4.59 KB
/
writer.cpp
File metadata and controls
222 lines (192 loc) · 4.59 KB
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
#include "sdptransform.hpp"
#include <cstddef> // size_t
#include <sstream> // std::stringstream
#include <stdexcept>
#include "string_view.hpp"
namespace sdptransform
{
void makeLine(
std::stringstream& sdpstream,
char type,
const grammar::Rule& rule,
const json& location
);
std::string write(json& session)
{
// RFC specified order.
static const std::vector<char> OuterOrder =
{ 'v', 'o', 's', 'i', 'u', 'e', 'p', 'c', 'b', 't', 'r', 'z', 'a' };
static const std::vector<char> InnerOrder =
{ 'i', 'c', 'b', 'a' };
if (!session.is_object())
throw std::invalid_argument("given session is not a JSON object");
// Ensure certain properties exist.
if (session.find("version") == session.end())
session["version"] = 0;
if (session.find("name") == session.end())
session["name"] = "-";
if (session.find("media") == session.end())
session["media"] = json::array();
for (auto& mLine : session.at("media"))
{
if (mLine.find("payloads") == mLine.end())
mLine["payloads"] = "";
}
std::stringstream sdpstream;
// Loop through OuterOrder for matching properties on session.
for (auto type : OuterOrder)
{
for (auto& rule : grammar::rulesMap.at(type))
{
json::const_iterator it;
if (
!rule.name.empty() &&
(it = session.find(rule.name)) != session.end() &&
!it->is_null()
)
{
makeLine(sdpstream, type, rule, session);
}
else if (
!rule.push.empty() &&
(it = session.find(rule.push)) != session.end() &&
it->is_array()
)
{
for (auto& el : session.at(rule.push))
{
makeLine(sdpstream, type, rule, el);
}
}
}
}
// Then for each media line, follow the InnerOrder.
for (auto& mLine : session.at("media"))
{
makeLine(sdpstream, 'm', grammar::rulesMap.at('m')[0], mLine);
for (auto type : InnerOrder)
{
for (auto& rule : grammar::rulesMap.at(type))
{
json::const_iterator it;
if (
!rule.name.empty() &&
(it = mLine.find(rule.name)) != mLine.end() &&
!it->is_null()
)
{
makeLine(sdpstream, type, rule, mLine);
}
else if (
!rule.push.empty() &&
(it = mLine.find(rule.push)) != mLine.end() &&
it->is_array()
)
{
for (auto& el : mLine.at(rule.push))
{
makeLine(sdpstream, type, rule, el);
}
}
}
}
}
return sdpstream.str();
}
void makeLine(
std::stringstream& sdpstream,
char type,
const grammar::Rule& rule,
const json& location
)
{
static const std::regex FormatRegex("%[sdv%]");
const std::string format = rule.format.empty()
? rule.formatFunc(
!rule.push.empty()
? location
: !rule.name.empty()
? location.at(rule.name)
: location)
: rule.format;
std::vector<json> args;
auto it = location.find(rule.name);
if (!rule.names.empty())
{
for (auto& name : rule.names)
{
json::const_iterator it;
if (
!rule.name.empty() &&
(it = location.find(rule.name)) != location.end() &&
it->find(name) != it->end()
)
{
args.push_back(location.at(rule.name).at(name));
}
// For mLine and push attributes.
else if (location.find(name) != location.end())
{
args.push_back(location.at(name));
}
// NOTE: Otherwise ensure an empty value is inserted into args array.
else
{
args.push_back("");
}
}
}
else if (it != location.end())
{
args.push_back(*it);
}
std::stringstream linestream;
size_t i = 0;
size_t len = args.size();
linestream << type << "=";
string_view runningFormat(format);
size_t nextPos = 0U;
for(
auto pos = runningFormat.find('%'); pos != std::string::npos; pos = runningFormat.find('%',nextPos))
{
const char nextChar = (pos+1<runningFormat.size()) ? runningFormat[pos+1] : '0';
const string_view prefix = runningFormat.substr(0,pos);
if(nextChar != '%' && nextChar != 'v' && nextChar != 'd' && nextChar != 's') {
nextPos = pos +2;
continue;
}
if (i >= len)
{
linestream << prefix << '%' << nextChar;
}
else
{
auto& arg = args[i];
i++;
linestream << prefix;
if (nextChar == '%')
{
linestream << "%";
}
else if (nextChar == 's' || nextChar == 'd')
{
if (arg.is_string())
linestream << arg.get<std::string>();
else
linestream << arg;
}
else if (nextChar == 'v')
{
// Do nothing.
}
}
runningFormat=runningFormat.substr(pos+2);
nextPos=0;
}
if(!runningFormat.empty()) {
linestream << runningFormat;
}
linestream << "\r\n";
sdpstream << linestream.str();
}
}