Browse code

core: moved SDP RTCP attribute parsing to AmSdp

Raphael Coeffic authored on 19/09/2013 07:05:21 • Václav Kubart committed on 16/10/2013 09:19:54
Showing 3 changed files
... ...
@@ -27,81 +27,6 @@ static SimpleRelayController simple_relay_ctrl;
27 27
 
28 28
 static const string zero_ip("0.0.0.0");
29 29
 
30
-//////////////////////////////////////////////////////////////////////////////////
31
-// handling SDP attribute "rtcp"
32
-
33
-class RtcpAddress
34
-{
35
-  private:
36
-    string nettype, addrtype, address;
37
-    bool parse(const string &src);
38
-    int port;
39
-
40
-  public:
41
-    RtcpAddress(const string &attr_value);
42
-    bool hasAddress() { return !address.empty(); }
43
-    void setAddress(const string &addr) { address = addr; }
44
-    void setPort(int _port) { port = _port; }
45
-    string print();
46
-};
47
-
48
-bool RtcpAddress::parse(const string &src)
49
-{
50
-  port = 0;
51
-  nettype.clear();
52
-  addrtype.clear();
53
-  address.clear();
54
-
55
-  int len = src.size();
56
-  if (len < 1) return false;
57
-
58
-  enum { PORT, NET_TYPE, ADDR_TYPE, ADDR } s = PORT;
59
-
60
-  // parsing (somehow) according to RFC 3605
61
-  //    rtcp-attribute =  "a=rtcp:" port  [nettype space addrtype space
62
-  //                             connection-address] CRLF
63
-  // nettype, addrtype is ignored
64
-  for (int i = 0; i < len; ++i) {
65
-    switch (s) {
66
-
67
-      case (PORT):
68
-        if (src[i] >= '0' && src[i] <= '9') port = port * 10 + (src[i] - '0');
69
-        else if (src[i] == ' ') s = NET_TYPE;
70
-        else return false; // error
71
-        break;
72
-
73
-      case NET_TYPE:
74
-          if (src[i] == ' ') s = ADDR_TYPE;
75
-          else nettype += src[i];
76
-          break;
77
-
78
-      case ADDR_TYPE:
79
-          if (src[i] == ' ') s = ADDR;
80
-          else addrtype += src[i];
81
-          break;
82
-
83
-      case ADDR:
84
-          address += src[i];
85
-          break;
86
-    }
87
-  }
88
-  return s == PORT ||
89
-    (s == ADDR && !address.empty()); // FIXME: nettype, addrtype and addr should be verified
90
-}
91
-
92
-string RtcpAddress::print()
93
-{
94
-  string s(int2str(port));
95
-  if (!address.empty())
96
-    s += " IN " + addrtype + " " + address;
97
-  return s;
98
-}
99
-
100
-RtcpAddress::RtcpAddress(const string &attr_value): port(0)
101
-{
102
-  if (!parse(attr_value)) throw runtime_error("can't parse rtcp attribute value");
103
-}
104
-
105 30
 static void replaceRtcpAttr(SdpMedia &m, const string& relay_address, int rtcp_port)
106 31
 {
107 32
   for (std::vector<SdpAttribute>::iterator a = m.attributes.begin(); a != m.attributes.end(); ++a) {
... ...
@@ -18,6 +18,8 @@
18 18
 #include "amci/amci.h"
19 19
 #include "log.h"
20 20
 
21
+#include <stdexcept>
22
+
21 23
 #include <string>
22 24
 #include <map>
23 25
 using std::string;
... ...
@@ -191,6 +193,69 @@ bool SdpMedia::operator == (const SdpMedia& other) const
191 193
     && send == other.send && recv == other.recv;
192 194
 }
193 195
 
196
+//
197
+// class RtcpAddress: Methods
198
+//
199
+bool RtcpAddress::parse(const string &src)
200
+{
201
+  port = 0;
202
+  nettype.clear();
203
+  addrtype.clear();
204
+  address.clear();
205
+
206
+  int len = src.size();
207
+  if (len < 1) return false;
208
+
209
+  enum { PORT, NET_TYPE, ADDR_TYPE, ADDR } s = PORT;
210
+
211
+  // parsing (somehow) according to RFC 3605
212
+  //    rtcp-attribute =  "a=rtcp:" port  [nettype space addrtype space
213
+  //                             connection-address] CRLF
214
+  // nettype, addrtype is ignored
215
+  for (int i = 0; i < len; ++i) {
216
+    switch (s) {
217
+
218
+      case (PORT):
219
+        if (src[i] >= '0' && src[i] <= '9') port = port * 10 + (src[i] - '0');
220
+        else if (src[i] == ' ') s = NET_TYPE;
221
+        else return false; // error
222
+        break;
223
+
224
+      case NET_TYPE:
225
+          if (src[i] == ' ') s = ADDR_TYPE;
226
+          else nettype += src[i];
227
+          break;
228
+
229
+      case ADDR_TYPE:
230
+          if (src[i] == ' ') s = ADDR;
231
+          else addrtype += src[i];
232
+          break;
233
+
234
+      case ADDR:
235
+          address += src[i];
236
+          break;
237
+    }
238
+  }
239
+  return s == PORT ||
240
+    (s == ADDR && !address.empty()); 
241
+  // FIXME: nettype, addrtype and addr should be verified
242
+}
243
+
244
+string RtcpAddress::print()
245
+{
246
+  string s(int2str(port));
247
+  if (!address.empty())
248
+    s += " IN " + addrtype + " " + address;
249
+  return s;
250
+}
251
+
252
+RtcpAddress::RtcpAddress(const string &attr_value): port(0)
253
+{
254
+  if (!parse(attr_value)) 
255
+    throw std::runtime_error("can't parse rtcp attribute value");
256
+}
257
+
258
+
194 259
 //
195 260
 // class AmSdp: Methods
196 261
 //
... ...
@@ -205,6 +205,26 @@ struct SdpMedia
205 205
 		  SdpMedia& answer) const;
206 206
 };
207 207
 
208
+/**
209
+ * \brief handling SDP attribute "rtcp"
210
+ */
211
+class RtcpAddress
212
+{
213
+  private:
214
+    string nettype, addrtype, address;
215
+    bool parse(const string &src);
216
+    int port;
217
+
218
+  public:
219
+    RtcpAddress(const string &attr_value);
220
+    bool hasAddress() { return !address.empty(); }
221
+    void setAddress(const string &addr) { address = addr; }
222
+    const string& getAddress() { return address; }
223
+    void setPort(int _port) { port = _port; }
224
+    int getPort() { return port; }
225
+    string print();
226
+};
227
+
208 228
 /**
209 229
  * \brief The SDP parser class.
210 230
  */