ZLMediaKit/src/Rtmp/RtmpPusher.h

97 lines
2.7 KiB
C++
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2017-09-27 16:20:30 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
2017-09-27 16:20:30 +08:00
*
2020-04-04 20:30:09 +08:00
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
2017-04-01 16:35:56 +08:00
*/
#ifndef SRC_RTMP_RTMPPUSHER_H_
#define SRC_RTMP_RTMPPUSHER_H_
#include "RtmpProtocol.h"
#include "RtmpMediaSource.h"
2017-04-25 11:35:41 +08:00
#include "Network/TcpClient.h"
2019-03-27 18:41:52 +08:00
#include "Pusher/PusherBase.h"
2017-04-01 16:35:56 +08:00
2018-10-24 17:17:55 +08:00
namespace mediakit {
2017-04-01 16:35:56 +08:00
2020-08-30 11:40:03 +08:00
class RtmpPusher : public RtmpProtocol, public TcpClient, public PusherBase {
2017-04-01 16:35:56 +08:00
public:
2020-03-20 11:51:24 +08:00
typedef std::shared_ptr<RtmpPusher> Ptr;
RtmpPusher(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &src);
2020-08-30 11:40:03 +08:00
~RtmpPusher() override;
2017-04-01 16:35:56 +08:00
2020-08-30 11:40:03 +08:00
void publish(const string &url) override ;
2020-03-20 11:51:24 +08:00
void teardown() override;
2017-06-06 20:06:31 +08:00
2020-03-20 11:51:24 +08:00
void setOnPublished(const Event &cb) override {
2020-08-30 11:40:03 +08:00
_on_published = cb;
2020-03-20 11:51:24 +08:00
}
2017-06-06 20:06:31 +08:00
2020-03-20 11:51:24 +08:00
void setOnShutdown(const Event &cb) override{
2020-08-30 11:40:03 +08:00
_on_shutdown = cb;
2020-03-20 11:51:24 +08:00
}
2020-08-30 11:40:03 +08:00
2017-04-01 16:35:56 +08:00
protected:
2020-03-20 11:51:24 +08:00
//for Tcpclient override
2020-08-30 11:40:03 +08:00
void onRecv(const Buffer::Ptr &buf) override;
2020-03-20 11:51:24 +08:00
void onConnect(const SockException &err) override;
void onErr(const SockException &ex) override;
2017-04-01 16:35:56 +08:00
2020-03-20 11:51:24 +08:00
//for RtmpProtocol override
2021-02-04 17:58:51 +08:00
void onRtmpChunk(RtmpPacket::Ptr chunk_data) override;
void onSendRawData(Buffer::Ptr buffer) override{
send(std::move(buffer));
2020-03-20 11:51:24 +08:00
}
2020-08-30 11:40:03 +08:00
2017-04-01 16:35:56 +08:00
private:
2020-08-30 11:40:03 +08:00
void onPublishResult(const SockException &ex, bool handshake_done);
2017-04-01 16:35:56 +08:00
2020-03-20 11:51:24 +08:00
template<typename FUN>
inline void addOnResultCB(const FUN &fun) {
2020-08-30 11:40:03 +08:00
lock_guard<recursive_mutex> lck(_mtx_on_result);
_map_on_result.emplace(_send_req_id, fun);
2020-03-20 11:51:24 +08:00
}
template<typename FUN>
inline void addOnStatusCB(const FUN &fun) {
2020-08-30 11:40:03 +08:00
lock_guard<recursive_mutex> lck(_mtx_on_status);
_deque_on_status.emplace_back(fun);
2020-03-20 11:51:24 +08:00
}
2017-04-01 16:35:56 +08:00
2020-03-20 11:51:24 +08:00
void onCmd_result(AMFDecoder &dec);
void onCmd_onStatus(AMFDecoder &dec);
void onCmd_onMetaData(AMFDecoder &dec);
2017-04-01 16:35:56 +08:00
2020-03-20 11:51:24 +08:00
inline void send_connect();
inline void send_createStream();
inline void send_publish();
inline void send_metaData();
void setSocketFlags();
2020-08-30 11:40:03 +08:00
2019-03-26 18:04:06 +08:00
private:
2020-08-30 11:40:03 +08:00
string _app;
string _stream_id;
string _tc_url;
recursive_mutex _mtx_on_result;
recursive_mutex _mtx_on_status;
deque<function<void(AMFValue &dec)> > _deque_on_status;
unordered_map<int, function<void(AMFDecoder &dec)> > _map_on_result;
2017-06-06 20:06:31 +08:00
//事件监听
2020-08-30 11:40:03 +08:00
Event _on_shutdown;
Event _on_published;
//推流超时定时器
std::shared_ptr<Timer> _publish_timer;
std::weak_ptr<RtmpMediaSource> _publish_src;
RtmpMediaSource::RingType::RingReader::Ptr _rtmp_reader;
2017-04-01 16:35:56 +08:00
};
2018-10-24 17:17:55 +08:00
} /* namespace mediakit */
2017-04-01 16:35:56 +08:00
#endif /* SRC_RTMP_RTMPPUSHER_H_ */