ZLMediaKit/src/Rtmp/RtmpPusher.h

128 lines
3.8 KiB
C
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2017-09-27 16:20:30 +08:00
* MIT License
2017-04-01 16:35:56 +08:00
*
2017-09-27 16:20:30 +08:00
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
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"
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
class RtmpPusher: public RtmpProtocol , public TcpClient{
public:
typedef std::shared_ptr<RtmpPusher> Ptr;
2017-06-06 20:06:31 +08:00
typedef std::function<void(const SockException &ex)> Event;
RtmpPusher(const char *strVhost,const char *strApp,const char *strStream);
2017-09-30 13:00:12 +08:00
RtmpPusher(const RtmpMediaSource::Ptr &src);
2017-04-01 16:35:56 +08:00
virtual ~RtmpPusher();
void publish(const char* strUrl);
void teardown();
2017-06-06 20:06:31 +08:00
void setOnPublished(Event onPublished) {
2018-10-24 15:43:52 +08:00
_onPublished = onPublished;
2017-06-06 20:06:31 +08:00
}
void setOnShutdown(Event onShutdown) {
2018-10-24 15:43:52 +08:00
_onShutdown = onShutdown;
2017-06-06 20:06:31 +08:00
}
2017-04-01 16:35:56 +08:00
protected:
//for Tcpclient override
2018-02-23 15:36:51 +08:00
void onRecv(const Buffer::Ptr &pBuf) override;
2017-04-01 16:35:56 +08:00
void onConnect(const SockException &err) override;
void onErr(const SockException &ex) override;
//for RtmpProtocol override
2017-04-01 16:35:56 +08:00
void onRtmpChunk(RtmpPacket &chunkData) override;
void onSendRawData(const Buffer::Ptr &buffer) override{
send(buffer);
2017-12-04 23:55:09 +08:00
}
2017-04-01 16:35:56 +08:00
private:
2017-09-30 13:00:12 +08:00
void init(const RtmpMediaSource::Ptr &src);
2017-06-06 20:06:31 +08:00
void onShutdown(const SockException &ex) {
2018-10-24 15:43:52 +08:00
_pPublishTimer.reset();
if(_onShutdown){
_onShutdown(ex);
2017-06-06 20:06:31 +08:00
}
2018-10-24 15:43:52 +08:00
_pRtmpReader.reset();
2017-04-01 16:35:56 +08:00
}
2017-06-06 20:06:31 +08:00
void onPublishResult(const SockException &ex) {
2018-10-24 15:43:52 +08:00
_pPublishTimer.reset();
if(_onPublished){
_onPublished(ex);
2017-06-06 20:06:31 +08:00
}
2017-04-01 16:35:56 +08:00
}
template<typename FUN>
inline void addOnResultCB(const FUN &fun) {
2018-10-24 15:43:52 +08:00
lock_guard<recursive_mutex> lck(_mtxOnResultCB);
_mapOnResultCB.emplace(_iReqID, fun);
2017-04-01 16:35:56 +08:00
}
template<typename FUN>
inline void addOnStatusCB(const FUN &fun) {
2018-10-24 15:43:52 +08:00
lock_guard<recursive_mutex> lck(_mtxOnStatusCB);
_dqOnStatusCB.emplace_back(fun);
2017-04-01 16:35:56 +08:00
}
void onCmd_result(AMFDecoder &dec);
void onCmd_onStatus(AMFDecoder &dec);
void onCmd_onMetaData(AMFDecoder &dec);
inline void send_connect();
inline void send_createStream();
inline void send_publish();
inline void send_metaData();
2018-10-24 15:43:52 +08:00
string _strApp;
string _strStream;
string _strTcUrl;
2017-04-01 16:35:56 +08:00
2018-10-24 15:43:52 +08:00
unordered_map<int, function<void(AMFDecoder &dec)> > _mapOnResultCB;
recursive_mutex _mtxOnResultCB;
deque<function<void(AMFValue &dec)> > _dqOnStatusCB;
recursive_mutex _mtxOnStatusCB;
2017-04-01 16:35:56 +08:00
typedef void (RtmpPusher::*rtmpCMDHandle)(AMFDecoder &dec);
static unordered_map<string, rtmpCMDHandle> g_mapCmd;
//超时功能实现
2018-10-24 15:43:52 +08:00
std::shared_ptr<Timer> _pPublishTimer;
2017-04-01 16:35:56 +08:00
//源
2018-10-24 15:43:52 +08:00
std::weak_ptr<RtmpMediaSource> _pMediaSrc;
RtmpMediaSource::RingType::RingReader::Ptr _pRtmpReader;
2017-06-06 20:06:31 +08:00
//事件监听
2018-10-24 15:43:52 +08:00
Event _onShutdown;
Event _onPublished;
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_ */