Repository navigation
Expand file tree
/
Copy pathRtmpServer.cpp
More file actions
109 lines (91 loc) · 2.82 KB
/
Copy pathRtmpServer.cpp
File metadata and controls
109 lines (91 loc) · 2.82 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
/*
* @Author: Duanran 995122760@qq.com
* @Date: 2024-07-02 19:45:45
* @LastEditors: Duanran 995122760@qq.com
* @LastEditTime: 2024-07-05 11:09:22
* @FilePath: /VideoServer/src/mmedia/rtmp/RtmpServer.cpp
* @Description: IMP RTMPSERVER.H
*
* Copyright (c) 2024 by ${git_name_email}, All Rights Reserved.
*/
#include "mmedia/rtmp/RtmpServer.h"
#include "mmedia/base/MMediaLog.h"
#include "base/StringUtils.h"
#include <functional>
using namespace vdse::mmedia;
RtmpServer::RtmpServer(EventLoop *loop, InetAddress &local, RtmpCallBack *handler)
:TcpServer(loop, local), rtmp_callback_(handler)
{
}
RtmpServer::~RtmpServer()
{
Stop();
}
void RtmpServer::Start()
{
TcpServer::SetNewConnectionCallBack(std::bind(&RtmpServer::OnNewConneciton, this, std::placeholders::_1));
TcpServer::SetDestroyConnectionCallBack(std::bind(&RtmpServer::OnDestroyed, this, std::placeholders::_1));
TcpServer::SetMsgCallBack(std::bind(&RtmpServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
TcpServer::SetWriteCompleteCallBack(std::bind(&RtmpServer::OnWriteComplete, this, std::placeholders::_1));
TcpServer::SetActiveCallBack(std::bind(&RtmpServer::OnActive, this, std::placeholders::_1));
TcpServer::Start();
RTMP_TRACE << "Rtmp Server Start..\n";
}
void RtmpServer::Stop()
{
TcpServer::Stop();
}
// b /home/duanran/workSpace/VideoServer/src/mmedia/rtmp/RtmpServer.cpp:52
void RtmpServer::OnNewConneciton(const TcpConnectionPtr &conn)
{
RTMP_TRACE << " RtmpServer::OnNewConneciton(const TcpConnectionPtr &conn) \n";
if (rtmp_callback_)
{
rtmp_callback_->OnNewConnection(conn);
}
auto shake = std::make_shared<RtmpContext>(conn, rtmp_callback_);
conn -> SetContext(kRtmpContext, shake);
shake -> StartHandshake();
}
void RtmpServer::OnDestroyed(const TcpConnectionPtr &conn)
{
if (rtmp_callback_)
{
rtmp_callback_->OnConnectionDestroy(conn);
}
conn->ClearContext(kRtmpContext);
}
void RtmpServer::OnMessage(const TcpConnectionPtr &conn, MsgBuffer &buf)
{
auto shake = conn -> GetContext<RtmpContext>(kRtmpContext);
if (shake)
{
int ret = shake -> Parse(buf);
// 握手成功
if (ret == 0)
{
RTMP_TRACE << " host " << conn -> PeerAddr().ToIpPort() << "handshake finish \n";
}
else if (ret == -1)
{
conn -> ForceClose();
RTMP_ERROR << " host " << conn -> PeerAddr().ToIpPort() << "handshake error ";
return;
}
}
}
void RtmpServer::OnWriteComplete(const ConnectionPtr &conn)
{
auto shake = conn -> GetContext<RtmpContext>(kRtmpContext);
if (shake)
{
shake -> OnWriteComplete();
}
}
void RtmpServer::OnActive(const ConnectionPtr &conn)
{
if (rtmp_callback_)
{
rtmp_callback_ -> OnActive(conn);
}
}