forked from AlexanderBabansky/EasyRTMP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
61 lines (52 loc) · 1.78 KB
/
utils.cpp
File metadata and controls
61 lines (52 loc) · 1.78 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
#include "utils.h"
#include <cassert>
#include "rtmp_exception.h"
using namespace librtmp;
using namespace std;
#define CHECK_POS(pos, url) (pos != url.npos) ? true : false
ParsedUrl librtmp::ParseURL(std::string url)
{
ParsedUrl res;
auto pos_proto = url.find("://", 0);
if (!CHECK_POS(pos_proto, url)) {
throw librtmp::rtmp_exception(RTMPError::BAD_URL);
}
if (pos_proto != 5 && pos_proto != 4) {
throw librtmp::rtmp_exception(RTMPError::BAD_URL);
}
auto proto_name = url.substr(0, pos_proto);
if (proto_name == "rtmp") {
res.type = ProtoType::RTMP;
} else if (proto_name == "rtmps") {
res.type = ProtoType::RTMPS;
} else {
throw librtmp::rtmp_exception(RTMPError::BAD_URL);
}
auto pos_port = url.find(":", pos_proto + 3);
auto pos_app = url.find("/", pos_proto + 3);
auto pos_key = url.find("/", pos_app + 1);
if (!CHECK_POS(pos_app, url) || !CHECK_POS(pos_key, url)) {
throw librtmp::rtmp_exception(RTMPError::BAD_URL);
}
if (!CHECK_POS(pos_port, url)) {
if (res.type == ProtoType::RTMP) {
res.port = 1935;
} else if (res.type == ProtoType::RTMPS) {
res.port = 443;
} else {
assert(false);
//TODO: add new proto port
}
res.url = url.substr(pos_proto + 3, pos_app - pos_proto - 3);
} else {
auto port_name = url.substr(pos_port + 1, pos_app - pos_port - 1);
res.port = atoi(port_name.c_str());
if (!res.port) {
throw librtmp::rtmp_exception(RTMPError::BAD_URL);
}
res.url = url.substr(pos_proto + 3, pos_port - pos_proto - 3);
}
res.app = url.substr(pos_app + 1, pos_key - pos_app - 1);
res.key = url.substr(pos_key + 1);
return res;
}