Commit a7e99b9d by xiongziliang

整理代码

parent fbd711a6
......@@ -91,14 +91,14 @@ void RtmpPlayer::onErr(const SockException &ex){
onPlayResult_l(ex, !_play_timer);
}
void RtmpPlayer::onPlayResult_l(const SockException &ex, bool handshakeCompleted) {
void RtmpPlayer::onPlayResult_l(const SockException &ex, bool handshake_done) {
if (ex.getErrCode() == Err_shutdown) {
//主动shutdown的,不触发回调
return;
}
WarnL << ex.getErrCode() << " " << ex.what();
if (!handshakeCompleted) {
if (!handshake_done) {
//开始播放阶段
_play_timer.reset();
//是否为性能测试模式
......@@ -152,14 +152,14 @@ void RtmpPlayer::onConnect(const SockException &err){
});
}
void RtmpPlayer::onRecv(const Buffer::Ptr &pBuf){
void RtmpPlayer::onRecv(const Buffer::Ptr &buf){
try {
if (_benchmark_mode && !_play_timer) {
//在性能测试模式下,如果rtmp握手完毕后,不再解析rtmp包
_rtmp_recv_ticker.resetTime();
return;
}
onParseRtmp(pBuf->data(), pBuf->size());
onParseRtmp(buf->data(), buf->size());
} catch (exception &e) {
SockException ex(Err_other, e.what());
//定时器_pPlayTimer为空后表明握手结束了
......@@ -226,21 +226,21 @@ inline void RtmpPlayer::send_play() {
addOnStatusCB(fun);
}
inline void RtmpPlayer::send_pause(bool bPause) {
inline void RtmpPlayer::send_pause(bool pause) {
AMFEncoder enc;
enc << "pause" << ++_send_req_id << nullptr << bPause;
enc << "pause" << ++_send_req_id << nullptr << pause;
sendRequest(MSG_CMD, enc.data());
auto fun = [this, bPause](AMFValue &val) {
auto fun = [this, pause](AMFValue &val) {
//TraceL << "pause onStatus";
auto level = val["level"].as_string();
auto code = val["code"].as_string();
if (level != "status") {
if (!bPause) {
if (!pause) {
throw std::runtime_error(StrPrinter << "pause 恢复播放失败:" << level << " " << code << endl);
}
} else {
_paused = bPause;
if (!bPause) {
_paused = pause;
if (!pause) {
onPlayResult_l(SockException(Err_success, "resum rtmp success"), true);
} else {
//暂停播放
......@@ -251,7 +251,7 @@ inline void RtmpPlayer::send_pause(bool bPause) {
addOnStatusCB(fun);
_beat_timer.reset();
if (bPause) {
if (pause) {
weak_ptr<RtmpPlayer> weakSelf = dynamic_pointer_cast<RtmpPlayer>(shared_from_this());
_beat_timer.reset(new Timer((*this)[kBeatIntervalMS].as<int>() / 1000.0, [weakSelf]() {
auto strongSelf = weakSelf.lock();
......@@ -314,32 +314,32 @@ void RtmpPlayer::onCmd_onMetaData(AMFDecoder &dec) {
_metadata_got = true;
}
void RtmpPlayer::onStreamDry(uint32_t stream_id) {
//TraceL << stream_id;
void RtmpPlayer::onStreamDry(uint32_t stream_index) {
//TraceL << stream_index;
onPlayResult_l(SockException(Err_other, "rtmp stream dry"), true);
}
void RtmpPlayer::onMediaData_l(const RtmpPacket::Ptr &packet) {
void RtmpPlayer::onMediaData_l(const RtmpPacket::Ptr &chunk_data) {
_rtmp_recv_ticker.resetTime();
if (!_play_timer) {
//已经触发了onPlayResult事件,直接触发onMediaData事件
onMediaData(packet);
onMediaData(chunk_data);
return;
}
if (packet->isCfgFrame()) {
if (chunk_data->isCfgFrame()) {
//输入配置帧以便初始化完成各个track
onMediaData(packet);
onMediaData(chunk_data);
} else {
//先触发onPlayResult事件,这个时候解码器才能初始化完毕
onPlayResult_l(SockException(Err_success, "play rtmp success"), false);
//触发onPlayResult事件后,再把帧数据输入到解码器
onMediaData(packet);
onMediaData(chunk_data);
}
}
void RtmpPlayer::onRtmpChunk(RtmpPacket &chunkData) {
void RtmpPlayer::onRtmpChunk(RtmpPacket &chunk_data) {
typedef void (RtmpPlayer::*rtmp_func_ptr)(AMFDecoder &dec);
static unordered_map<string, rtmp_func_ptr> s_func_map;
static onceToken token([]() {
......@@ -349,12 +349,12 @@ void RtmpPlayer::onRtmpChunk(RtmpPacket &chunkData) {
s_func_map.emplace("onMetaData", &RtmpPlayer::onCmd_onMetaData);
});
switch (chunkData.type_id) {
switch (chunk_data.type_id) {
case MSG_CMD:
case MSG_CMD3:
case MSG_DATA:
case MSG_DATA3: {
AMFDecoder dec(chunkData.buffer, 0);
AMFDecoder dec(chunk_data.buffer, 0);
std::string type = dec.load<std::string>();
auto it = s_func_map.find(type);
if (it != s_func_map.end()) {
......@@ -368,10 +368,10 @@ void RtmpPlayer::onRtmpChunk(RtmpPacket &chunkData) {
case MSG_AUDIO:
case MSG_VIDEO: {
auto idx = chunkData.type_id % 2;
auto idx = chunk_data.type_id % 2;
if (_now_stamp_ticker[idx].elapsedTime() > 500) {
//计算播放进度时间轴用
_now_stamp[idx] = chunkData.time_stamp;
_now_stamp[idx] = chunk_data.time_stamp;
}
if (!_metadata_got) {
if (!onCheckMeta(TitleMeta().getMetadata())) {
......@@ -379,7 +379,7 @@ void RtmpPlayer::onRtmpChunk(RtmpPacket &chunkData) {
}
_metadata_got = true;
}
onMediaData_l(std::make_shared<RtmpPacket>(std::move(chunkData)));
onMediaData_l(std::make_shared<RtmpPacket>(std::move(chunk_data)));
break;
}
......
......@@ -41,34 +41,34 @@ public:
void teardown() override;
protected:
virtual bool onCheckMeta(const AMFValue &val) =0;
virtual void onMediaData(const RtmpPacket::Ptr &chunkData) =0;
virtual bool onCheckMeta(const AMFValue &val) = 0;
virtual void onMediaData(const RtmpPacket::Ptr &chunk_data) = 0;
uint32_t getProgressMilliSecond() const;
void seekToMilliSecond(uint32_t ms);
protected:
void onMediaData_l(const RtmpPacket::Ptr &chunkData);
void onMediaData_l(const RtmpPacket::Ptr &chunk_data);
//在获取config帧后才触发onPlayResult_l(而不是收到play命令回复),所以此时所有track都初始化完毕了
void onPlayResult_l(const SockException &ex, bool handshakeCompleted);
void onPlayResult_l(const SockException &ex, bool handshake_done);
//form Tcpclient
void onRecv(const Buffer::Ptr &pBuf) override;
void onRecv(const Buffer::Ptr &buf) override;
void onConnect(const SockException &err) override;
void onErr(const SockException &ex) override;
//from RtmpProtocol
void onRtmpChunk(RtmpPacket &chunkData) override;
void onStreamDry(uint32_t ui32StreamId) override;
void onSendRawData(const Buffer::Ptr &buffer) override{
void onRtmpChunk(RtmpPacket &chunk_data) override;
void onStreamDry(uint32_t stream_index) override;
void onSendRawData(const Buffer::Ptr &buffer) override {
send(buffer);
}
template<typename FUNC>
inline void addOnResultCB(const FUNC &func) {
void addOnResultCB(const FUNC &func) {
lock_guard<recursive_mutex> lck(_mtx_on_result);
_map_on_result.emplace(_send_req_id, func);
}
template<typename FUNC>
inline void addOnStatusCB(const FUNC &func) {
void addOnStatusCB(const FUNC &func) {
lock_guard<recursive_mutex> lck(_mtx_on_status);
_deque_on_status.emplace_back(func);
}
......@@ -77,10 +77,10 @@ protected:
void onCmd_onStatus(AMFDecoder &dec);
void onCmd_onMetaData(AMFDecoder &dec);
inline void send_connect();
inline void send_createStream();
inline void send_play();
inline void send_pause(bool bPause);
void send_connect();
void send_createStream();
void send_play();
void send_pause(bool pause);
private:
string _app;
......
......@@ -15,11 +15,6 @@
#include "Thread/ThreadPool.h"
using namespace toolkit;
#ifdef ENABLE_OPENSSL
#include "Util/SSLBox.h"
#include <openssl/hmac.h>
#include <openssl/opensslv.h>
#define C1_DIGEST_SIZE 32
#define C1_KEY_SIZE 128
#define C1_SCHEMA_SIZE 764
......@@ -29,6 +24,11 @@ using namespace toolkit;
#define S2_FMS_KEY_SIZE 68
#define C1_OFFSET_SIZE 4
#ifdef ENABLE_OPENSSL
#include "Util/SSLBox.h"
#include <openssl/hmac.h>
#include <openssl/opensslv.h>
static string openssl_HMACsha256(const void *key, unsigned int key_len, const void *data,unsigned int data_len){
std::shared_ptr<char> out(new char[32], [](char *ptr) { delete[] ptr; });
unsigned int out_len;
......
......@@ -18,46 +18,47 @@
namespace mediakit {
class RtmpPusher: public RtmpProtocol , public TcpClient , public PusherBase{
class RtmpPusher : public RtmpProtocol, public TcpClient, public PusherBase {
public:
typedef std::shared_ptr<RtmpPusher> Ptr;
RtmpPusher(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &src);
virtual ~RtmpPusher();
void publish(const string &strUrl) override ;
~RtmpPusher() override;
void publish(const string &url) override ;
void teardown() override;
void setOnPublished(const Event &cb) override {
_onPublished = cb;
_on_published = cb;
}
void setOnShutdown(const Event &cb) override{
_onShutdown = cb;
_on_shutdown = cb;
}
protected:
//for Tcpclient override
void onRecv(const Buffer::Ptr &pBuf) override;
void onRecv(const Buffer::Ptr &buf) override;
void onConnect(const SockException &err) override;
void onErr(const SockException &ex) override;
//for RtmpProtocol override
void onRtmpChunk(RtmpPacket &chunkData) override;
void onRtmpChunk(RtmpPacket &chunk_data) override;
void onSendRawData(const Buffer::Ptr &buffer) override{
send(buffer);
}
private:
void onPublishResult(const SockException &ex,bool handshakeCompleted);
void onPublishResult(const SockException &ex, bool handshake_done);
template<typename FUN>
inline void addOnResultCB(const FUN &fun) {
lock_guard<recursive_mutex> lck(_mtxOnResultCB);
_mapOnResultCB.emplace(_send_req_id, fun);
lock_guard<recursive_mutex> lck(_mtx_on_result);
_map_on_result.emplace(_send_req_id, fun);
}
template<typename FUN>
inline void addOnStatusCB(const FUN &fun) {
lock_guard<recursive_mutex> lck(_mtxOnStatusCB);
_dqOnStatusCB.emplace_back(fun);
lock_guard<recursive_mutex> lck(_mtx_on_status);
_deque_on_status.emplace_back(fun);
}
void onCmd_result(AMFDecoder &dec);
......@@ -69,23 +70,25 @@ private:
inline void send_publish();
inline void send_metaData();
void setSocketFlags();
private:
string _strApp;
string _strStream;
string _strTcUrl;
unordered_map<int, function<void(AMFDecoder &dec)> > _mapOnResultCB;
recursive_mutex _mtxOnResultCB;
deque<function<void(AMFValue &dec)> > _dqOnStatusCB;
recursive_mutex _mtxOnStatusCB;
//超时功能实现
std::shared_ptr<Timer> _pPublishTimer;
//源
std::weak_ptr<RtmpMediaSource> _pMediaSrc;
RtmpMediaSource::RingType::RingReader::Ptr _pRtmpReader;
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;
//事件监听
Event _onShutdown;
Event _onPublished;
Event _on_shutdown;
Event _on_published;
//推流超时定时器
std::shared_ptr<Timer> _publish_timer;
std::weak_ptr<RtmpMediaSource> _publish_src;
RtmpMediaSource::RingType::RingReader::Ptr _rtmp_reader;
};
} /* namespace mediakit */
......
......@@ -732,14 +732,14 @@ void RtspPlayer::onRecvRTP_l(const RtpPacket::Ptr &rtp, const SdpTrack::Ptr &tra
}
}
void RtspPlayer::onPlayResult_l(const SockException &ex , bool handshakeCompleted) {
void RtspPlayer::onPlayResult_l(const SockException &ex , bool handshake_done) {
if (ex.getErrCode() == Err_shutdown) {
//主动shutdown的,不触发回调
return;
}
WarnL << ex.getErrCode() << " " << ex.what();
if (!handshakeCompleted) {
if (!handshake_done) {
//开始播放阶段
_play_check_timer.reset();
onPlayResult(ex);
......
......@@ -87,7 +87,7 @@ protected:
private:
void onRecvRTP_l(const RtpPacket::Ptr &rtp, const SdpTrack::Ptr &track);
void onPlayResult_l(const SockException &ex , bool handshakeCompleted);
void onPlayResult_l(const SockException &ex , bool handshake_done);
int getTrackIndexByInterleaved(int interleaved) const;
int getTrackIndexByTrackType(TrackType track_type) const;
......
......@@ -25,46 +25,51 @@ using namespace toolkit;
namespace mediakit {
class RtspPlayerImp: public PlayerImp<RtspPlayer,RtspDemuxer> {
class RtspPlayerImp : public PlayerImp<RtspPlayer,RtspDemuxer> {
public:
typedef std::shared_ptr<RtspPlayerImp> Ptr;
RtspPlayerImp(const EventPoller::Ptr &poller) : PlayerImp<RtspPlayer,RtspDemuxer>(poller){}
virtual ~RtspPlayerImp(){
DebugL<<endl;
};
float getProgress() const override{
if(getDuration() > 0){
RtspPlayerImp(const EventPoller::Ptr &poller) : PlayerImp<RtspPlayer, RtspDemuxer>(poller) {}
~RtspPlayerImp() override{
DebugL << endl;
}
float getProgress() const override {
if (getDuration() > 0) {
return getProgressMilliSecond() / (getDuration() * 1000);
}
return PlayerBase::getProgress();
};
void seekTo(float fProgress) override{
fProgress = MAX(float(0),MIN(fProgress,float(1.0)));
}
void seekTo(float fProgress) override {
fProgress = MAX(float(0), MIN(fProgress, float(1.0)));
seekToMilliSecond(fProgress * getDuration() * 1000);
};
}
private:
//派生类回调函数
bool onCheckSDP(const string &sdp) override {
_pRtspMediaSrc = dynamic_pointer_cast<RtspMediaSource>(_pMediaSrc);
if(_pRtspMediaSrc){
_pRtspMediaSrc->setSdp(sdp);
_rtsp_media_src = dynamic_pointer_cast<RtspMediaSource>(_pMediaSrc);
if (_rtsp_media_src) {
_rtsp_media_src->setSdp(sdp);
}
_delegate.reset(new RtspDemuxer);
_delegate->loadSdp(sdp);
return true;
}
void onRecvRTP(const RtpPacket::Ptr &rtp, const SdpTrack::Ptr &track) override {
if(_pRtspMediaSrc){
if (_rtsp_media_src) {
// rtsp直接代理是无法判断该rtp是否是I帧,所以GOP缓存基本是无效的
// 为了减少内存使用,那么我们设置为一直关键帧以便清空GOP缓存
_pRtspMediaSrc->onWrite(rtp,true);
_rtsp_media_src->onWrite(rtp, true);
}
_delegate->inputRtp(rtp);
if(_maxAnalysisMS && _delegate->isInited(_maxAnalysisMS)){
PlayerImp<RtspPlayer,RtspDemuxer>::onPlayResult(SockException(Err_success,"play rtsp success"));
_maxAnalysisMS = 0;
if (_max_analysis_ms && _delegate->isInited(_max_analysis_ms)) {
PlayerImp<RtspPlayer, RtspDemuxer>::onPlayResult(SockException(Err_success, "play rtsp success"));
_max_analysis_ms = 0;
}
}
......@@ -74,17 +79,18 @@ private:
//如果超过这个时间还未获取成功,那么会强制触发onPlayResult事件(虽然此时有些track还未初始化成功)
void onPlayResult(const SockException &ex) override {
//isInited判断条件:无超时
if(ex || _delegate->isInited(0)){
if (ex || _delegate->isInited(0)) {
//已经初始化成功,说明sdp里面有完善的信息
PlayerImp<RtspPlayer,RtspDemuxer>::onPlayResult(ex);
}else{
PlayerImp<RtspPlayer, RtspDemuxer>::onPlayResult(ex);
} else {
//还没初始化成功,说明sdp里面信息不完善,还有一些track未初始化成功
_maxAnalysisMS = (*this)[Client::kMaxAnalysisMS];
_max_analysis_ms = (*this)[Client::kMaxAnalysisMS];
}
}
private:
RtspMediaSource::Ptr _pRtspMediaSrc;
int _maxAnalysisMS = 0;
int _max_analysis_ms = 0;
RtspMediaSource::Ptr _rtsp_media_src;
};
} /* namespace mediakit */
......
......@@ -31,39 +31,39 @@ class RtspPusher : public TcpClient, public RtspSplitter, public PusherBase {
public:
typedef std::shared_ptr<RtspPusher> Ptr;
RtspPusher(const EventPoller::Ptr &poller,const RtspMediaSource::Ptr &src);
virtual ~RtspPusher();
void publish(const string &strUrl) override;
~RtspPusher() override;
void publish(const string &url) override;
void teardown() override;
void setOnPublished(const Event &cb) override {
_onPublished = cb;
_on_published = cb;
}
void setOnShutdown(const Event & cb) override{
_onShutdown = cb;
_on_shutdown = cb;
}
protected:
//for Tcpclient override
void onRecv(const Buffer::Ptr &pBuf) override;
void onRecv(const Buffer::Ptr &buf) override;
void onConnect(const SockException &err) override;
void onErr(const SockException &ex) override;
//RtspSplitter override
void onWholeRtspPacket(Parser &parser) override ;
void onRtpPacket(const char *data,uint64_t len) override {};
private:
void onPublishResult(const SockException &ex, bool handshakeCompleted);
void onPublishResult(const SockException &ex, bool handshake_done);
void sendAnnounce();
void sendSetup(unsigned int uiTrackIndex);
void sendSetup(unsigned int track_idx);
void sendRecord();
void sendOptions();
void handleResAnnounce(const Parser &parser);
void handleResSetup(const Parser &parser, unsigned int uiTrackIndex);
bool handleAuthenticationFailure(const string &paramsStr);
void handleResSetup(const Parser &parser, unsigned int track_idx);
bool handleAuthenticationFailure(const string &params_str);
inline int getTrackIndexByTrackType(TrackType type);
......@@ -73,33 +73,30 @@ private:
void createUdpSockIfNecessary(int track_idx);
void setSocketFlags();
private:
//rtsp鉴权相关
string _rtspMd5Nonce;
string _rtspRealm;
unsigned int _cseq = 1;
Rtsp::eRtpType _rtp_type = Rtsp::RTP_TCP;
//rtsp鉴权相关
string _nonce;
string _realm;
string _url;
string _session_id;
string _content_base;
SdpParser _sdp_parser;
vector<SdpTrack::Ptr> _track_vec;
Socket::Ptr _udp_socks[2];
//超时功能实现
std::shared_ptr<Timer> _pPublishTimer;
//源
std::weak_ptr<RtspMediaSource> _pMediaSrc;
RtspMediaSource::RingType::RingReader::Ptr _pRtspReader;
//事件监听
Event _onShutdown;
Event _onPublished;
string _strUrl;
SdpParser _sdpParser;
vector<SdpTrack::Ptr> _aTrackInfo;
string _strSession;
unsigned int _uiCseq = 1;
string _strContentBase;
Rtsp::eRtpType _eType = Rtsp::RTP_TCP;
Socket::Ptr _apUdpSock[2];
function<void(const Parser&)> _onHandshake;
std::shared_ptr<Timer> _publish_timer;
//心跳定时器
std::shared_ptr<Timer> _pBeatTimer;
std::shared_ptr<Timer> _beat_timer;
std::weak_ptr<RtspMediaSource> _push_src;
RtspMediaSource::RingType::RingReader::Ptr _rtsp_reader;
//事件监听
Event _on_shutdown;
Event _on_published;
function<void(const Parser&)> _on_res_func;
};
} /* namespace mediakit */
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论