-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
162 lines (159 loc) · 6.16 KB
/
main.cpp
File metadata and controls
162 lines (159 loc) · 6.16 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
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "modulesdk.h"
#include <stdio.h>
#include <netinet/in.h>
#include "xop/RtmpServer.h"
#include "xop/HttpFlvServer.h"
#include "xop/RtmpPublisher.h"
#include "xop/RtmpClient.h"
#include "xop/HttpFlvServer.h"
#include "xop/H264Parser.h"
#include "net/EventLoop.h"
#include "cJSON.h"
#include "log.h"
// ServeInvoke主要是解析来自客户端的请求,解析其中的参数,然后根据参数连接rtmp服务器,若此处需要做一个重定向,可以获取rtmp的连接地址,然后做一个更改
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
const char *conf_path = "conf/rtmp_config.json";
extern "C"
{
void *run(void *adsfjiaf)
{
//加载配置文件
std::string rtmpaddress, httpaddress;
int rtmpport, httpport, enablehttp;
if (access(conf_path, F_OK))
{
extrapidLog(LOG_WARN, "RTMP", "未找到配置文件,正在生成默认配置文件");
cJSON *json = cJSON_CreateObject();
cJSON_AddStringToObject(json, "RtmpBindAddress", "0.0.0.0");
cJSON_AddNumberToObject(json, "RtmpPort", 1935);
cJSON_AddBoolToObject(json, "EnableHttpFlv", cJSON_True);
cJSON_AddStringToObject(json, "HttpFlvAddress", "0.0.0.0");
cJSON_AddNumberToObject(json, "HttpFlvPort", 1936);
char *p = cJSON_Print(json);
FILE *fp = fopen(conf_path, "w");
if (fp == NULL)
{
extrapidLog(LOG_ERROR, "RTMP", "无法保存配置文件!RTMP模块启动失败");
free(p);
cJSON_Delete(json);
return NULL;
}
fprintf(fp, "%s", p);
fclose(fp);
cJSON_Delete(json);
free(p);
extrapidLog(LOG_INFO, "RTMP", "配置文件%s保存成功", conf_path);
rtmpaddress = "0.0.0.0";
rtmpport = 1935;
enablehttp = 1;
httpaddress = "0.0.0.0";
httpport = 1936;
}
else
{
FILE *fp = fopen(conf_path, "r");
if (fp == NULL)
{
extrapidLog(LOG_ERROR, "RTMP", "配置文件%s加载失败:%s,RTMP模块启动失败", conf_path, strerror(errno));
return NULL;
}
char *jsondata = (char *)malloc(1024 * 2);
fread(jsondata, 1, 1024 * 2, fp);
fclose(fp);
cJSON *json = cJSON_Parse(jsondata);
free(jsondata);
if (json == NULL)
{
extrapidLog(LOG_ERROR, "RTMP", "%s加载失败:%s,RTMP模块启动失败", conf_path, cJSON_GetErrorPtr());
return NULL;
}
cJSON *temp = cJSON_GetObjectItem(json, "RtmpBindAddress");
if (temp == NULL || temp->type != cJSON_String || temp->valuestring == NULL)
{
extrapidLog(LOG_ERROR, "RTMP", "%s加载失败,RtmpBindAddress错误,RTMP模块启动失败", conf_path);
cJSON_Delete(json);
return NULL;
}
rtmpaddress = temp->valuestring;
temp = cJSON_GetObjectItem(json, "RtmpPort");
if (temp == NULL || temp->type != cJSON_Number)
{
extrapidLog(LOG_ERROR, "RTMP", "%s加载失败,RtmpPort错误,RTMP模块启动失败", conf_path);
cJSON_Delete(json);
return NULL;
}
rtmpport = temp->valueint;
temp = cJSON_GetObjectItem(json, "EnableHttpFlv");
if (temp == NULL)
{
extrapidLog(LOG_ERROR, "RTMP", "%s加载失败,EnableHttpFlv错误,RTMP模块启动失败", conf_path);
cJSON_Delete(json);
return NULL;
}
enablehttp = temp->valueint;
temp = cJSON_GetObjectItem(json, "HttpFlvAddress");
if (temp == NULL || temp->type != cJSON_String)
{
extrapidLog(LOG_ERROR, "RTMP", "%s加载失败,HttpFlvAddress错误,RTMP模块启动失败", conf_path);
cJSON_Delete(json);
return NULL;
}
httpaddress = temp->valuestring;
temp = cJSON_GetObjectItem(json, "HttpFlvPort");
if (temp == NULL || temp->type != cJSON_Number)
{
extrapidLog(LOG_ERROR, "RTMP", "%s加载失败,HttpFlvPort错误,RTMP模块启动失败", conf_path);
cJSON_Delete(json);
return NULL;
}
httpport = temp->valueint;
cJSON_Delete(json);
}
int count = 1;
xop::EventLoop event_loop(count);
/* rtmp server example */
auto rtmp_server = xop::RtmpServer::Create(&event_loop);
rtmp_server->SetChunkSize(60000);
rtmp_server->SetEventCallback([](std::string type, std::string stream_path)
{ extrapidLog(LOG_INFO, "RTMP", "事件:%s,流:%s", type.c_str(), stream_path.c_str()); });
if (!rtmp_server->Start(rtmpaddress, rtmpport))
{
extrapidLog(LOG_ERROR, "RTMP", "RTMP服务创建失败");
}
if (enablehttp == 1)
{
xop::HttpFlvServer *http_flv_server = new xop::HttpFlvServer;
http_flv_server->Attach(rtmp_server);
if (!http_flv_server->Start(httpaddress, httpport))
{
extrapidLog(LOG_ERROR, "RTMP", "HttpFlv服务创建失败");
}
}
else
{
extrapidLog(LOG_INFO, "RTMP", "HttpFlv服务已禁用");
}
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&lock);
pthread_mutex_lock(&lock);
return NULL;
}
void thread()
{
pthread_t tid;
pthread_create(&tid, NULL, run, NULL);
pthread_detach(tid);
}
ModuleType_t _init_(int i)
{
logInit("logs/rtmp");
ModuleType_t mod = SDK_CreateModule("rtmp", "一个rtmp模块,原作者:https://github.com/PHZ76/rtmp", 1);
SDK_AddFunction(&mod, RUN_AT_START, (void *)(thread), 10);
return mod;
}
}