-
Notifications
You must be signed in to change notification settings - Fork 3
/
rtmpbase.cpp
192 lines (172 loc) · 4.07 KB
/
rtmpbase.cpp
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
#include "rtmpbase.h"
#include "dlog.h"
#include <stdio.h>
#include "librtmp/rtmp_sys.h"
#include "librtmp/log.h"
#define DEF_TIMEOUT 30 /* seconds */
#define DEF_BUFTIME (10 * 60 * 60 * 1000) /* 10 hours default */
#ifdef _WIN32
#pragma comment(lib, "ws2_32.lib")
#endif
namespace LQF
{
bool RTMPBase::initRtmp()
{
bool ret_code = true;
#ifdef WIN32
WORD version;
WSADATA wsaData;
version = MAKEWORD(1, 1);
ret_code = (WSAStartup(version, &wsaData) == 0) ? true : false;
#endif
LogInfo("at rtmp object create");
rtmp_ = RTMP_Alloc();
RTMP_Init(rtmp_);
return ret_code;
}
RTMPBase::RTMPBase():
rtmp_obj_type_(RTMP_BASE_TYPE_UNKNOW),
enable_video_(true),
enable_audio_(true)
{
initRtmp();
}
RTMPBase::RTMPBase(RTMP_BASE_TYPE rtmp_obj_type):
rtmp_obj_type_(rtmp_obj_type),
enable_video_(true),
enable_audio_(true)
{
initRtmp();
}
RTMPBase::RTMPBase(RTMP_BASE_TYPE rtmp_obj_type,std::string& url):
rtmp_obj_type_(rtmp_obj_type),
url_(url),
enable_video_(true),
enable_audio_(true)
{
initRtmp();
}
RTMPBase::RTMPBase(std::string& url,bool is_recv_audio,bool is_recv_video):
rtmp_obj_type_(RTMP_BASE_TYPE_PLAY),
url_(url),
enable_video_(is_recv_audio),
enable_audio_(is_recv_video)
{
initRtmp();
}
RTMPBase::~RTMPBase()
{
if (IsConnect())
{
Disconnect();
}
RTMP_Free(rtmp_);
rtmp_ = NULL;
#ifdef WIN32
WSACleanup();
#endif // WIN32
}
void RTMPBase::SetConnectUrl(std::string& url)
{
url_ = url;
}
bool RTMPBase::SetReceiveAudio(bool is_recv_audio)
{
if(is_recv_audio == enable_audio_)
return true;
if(IsConnect())
{
LogInfo("zkzszd RTMP_SendReceiveAudio");
if(RTMP_SendReceiveAudio(rtmp_,is_recv_audio))
{
is_recv_audio = enable_audio_;
return true;
}
}
else
is_recv_audio = enable_audio_;
return false;
}
bool RTMPBase::SetReceiveVideo(bool is_recv_video)
{
if(is_recv_video == enable_video_)
return true;
if(IsConnect())
{
if(RTMP_SendReceiveVideo(rtmp_, is_recv_video))
{
is_recv_video = enable_video_;
return true;
}
}
else
is_recv_video = enable_video_;
return false;
}
bool RTMPBase::IsConnect()
{
return RTMP_IsConnected(rtmp_);
}
void RTMPBase::Disconnect()
{
RTMP_Close(rtmp_);
}
bool RTMPBase::Connect()
{
//断线重连必须执行次操作,才能重现连上(比较疑惑)
RTMP_Free(rtmp_);
rtmp_ = RTMP_Alloc();
RTMP_Init(rtmp_);
LogInfo("base begin connect");
//set connection timeout,default 30s
rtmp_->Link.timeout = 10;
if (RTMP_SetupURL(rtmp_, (char*)url_.c_str())<0)
{
LogInfo("RTMP_SetupURL failed!");
return FALSE;
}
rtmp_->Link.lFlags |= RTMP_LF_LIVE; // then we can't seek/resume
RTMP_SetBufferMS(rtmp_, 3600*1000);//1hour
if(rtmp_obj_type_ == RTMP_BASE_TYPE_PUSH)
RTMP_EnableWrite(rtmp_); /*设置可写,即发布流,这个函数必须在连接前使用,否则无效*/
if (!RTMP_Connect(rtmp_, NULL))
{
LogInfo("RTMP_Connect failed!");
return FALSE;
}
if (!RTMP_ConnectStream(rtmp_, 0))
{
LogInfo("RTMP_ConnectStream failed");
return FALSE;
}
//判断是否打开音视频,默认打开
if(rtmp_obj_type_ == RTMP_BASE_TYPE_PUSH)
{
if(!enable_video_)
{
RTMP_SendReceiveVideo(rtmp_, enable_video_);
}
if(!enable_audio_)
{
RTMP_SendReceiveAudio(rtmp_, enable_audio_);
}
}
return true;
}
bool RTMPBase::Connect(std::string url)
{
url_ = url;
return Connect();
}
uint32_t RTMPBase::GetSampleRateByFreqIdx(uint8_t freq_idx )
{
uint32_t freq_idx_table[] = {96000, 88200, 64000, 48000, 44100, 32000,
24000, 22050, 16000, 12000, 11025, 8000, 7350};
if(freq_idx < 13)
{
return freq_idx_table[freq_idx];
}
LogError("freq_idx:%d is error", freq_idx);
return 44100;
}
}