ZLMediaKit/src/Rtmp/RtmpPlayer.h

109 lines
3.4 KiB
C++
Raw Permalink Normal View History

2017-10-09 22:11:01 +08:00
/*
2023-12-09 16:23:51 +08:00
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
2017-09-27 16:20:30 +08:00
*
2023-12-09 16:23:51 +08:00
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
2017-09-27 16:20:30 +08:00
*
2023-12-09 16:23:51 +08:00
* Use of this source code is governed by MIT-like license that can be found in the
2020-04-04 20:30:09 +08:00
* 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
*/
2020-08-30 10:48:34 +08:00
#ifndef SRC_RTMP_RtmpPlayer_H_
#define SRC_RTMP_RtmpPlayer_H_
2017-04-01 16:35:56 +08:00
2017-04-25 11:35:41 +08:00
#include <memory>
2017-04-01 16:35:56 +08:00
#include <string>
#include <functional>
#include "amf.h"
2017-04-25 11:35:41 +08:00
#include "Rtmp.h"
2017-04-01 16:35:56 +08:00
#include "RtmpProtocol.h"
2017-04-25 11:35:41 +08:00
#include "Player/PlayerBase.h"
#include "Util/TimeTicker.h"
#include "Network/Socket.h"
#include "Network/TcpClient.h"
2019-03-27 18:56:49 +08:00
2018-10-24 17:17:55 +08:00
namespace mediakit {
2020-08-30 10:48:34 +08:00
2019-08-30 11:17:27 +08:00
//实现了rtmp播放器协议部分的功能及数据接收功能
class RtmpPlayer : public PlayerBase, public toolkit::TcpClient, public RtmpProtocol {
2017-04-01 16:35:56 +08:00
public:
2022-12-02 14:43:06 +08:00
using Ptr = std::shared_ptr<RtmpPlayer>;
RtmpPlayer(const toolkit::EventPoller::Ptr &poller);
2020-08-30 10:48:34 +08:00
~RtmpPlayer() override;
2017-04-01 16:35:56 +08:00
void play(const std::string &strUrl) override;
2020-03-20 11:51:24 +08:00
void pause(bool bPause) override;
2021-08-12 16:07:31 +08:00
void speed(float speed) override;
2020-03-20 11:51:24 +08:00
void teardown() override;
2020-08-30 10:48:34 +08:00
2017-04-01 16:35:56 +08:00
protected:
2023-06-17 10:29:27 +08:00
virtual bool onMetadata(const AMFValue &val) = 0;
virtual void onRtmpPacket(RtmpPacket::Ptr chunk_data) = 0;
2020-03-20 11:51:24 +08:00
uint32_t getProgressMilliSecond() const;
void seekToMilliSecond(uint32_t ms);
2020-08-30 10:48:34 +08:00
2019-02-28 18:03:49 +08:00
protected:
2021-02-04 17:58:51 +08:00
void onMediaData_l(RtmpPacket::Ptr chunk_data);
2020-03-20 11:51:24 +08:00
//在获取config帧后才触发onPlayResult_l(而不是收到play命令回复)所以此时所有track都初始化完毕了
void onPlayResult_l(const toolkit::SockException &ex, bool handshake_done);
2017-04-01 16:35:56 +08:00
2020-03-20 11:51:24 +08:00
//form Tcpclient
void onRecv(const toolkit::Buffer::Ptr &buf) override;
void onConnect(const toolkit::SockException &err) override;
2023-04-28 22:03:16 +08:00
void onError(const toolkit::SockException &ex) override;
2020-03-20 11:51:24 +08:00
//from RtmpProtocol
2021-02-04 17:58:51 +08:00
void onRtmpChunk(RtmpPacket::Ptr chunk_data) override;
2020-08-30 11:40:03 +08:00
void onStreamDry(uint32_t stream_index) override;
void onSendRawData(toolkit::Buffer::Ptr buffer) override {
send(std::move(buffer));
2018-01-30 11:23:57 +08:00
}
2017-04-01 16:35:56 +08:00
2020-08-30 10:48:34 +08:00
template<typename FUNC>
2020-08-30 11:40:03 +08:00
void addOnResultCB(const FUNC &func) {
2020-08-30 10:48:34 +08:00
_map_on_result.emplace(_send_req_id, func);
2020-03-20 11:51:24 +08:00
}
2020-08-30 10:48:34 +08:00
template<typename FUNC>
2020-08-30 11:40:03 +08:00
void addOnStatusCB(const FUNC &func) {
2020-08-30 10:48:34 +08:00
_deque_on_status.emplace_back(func);
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-08-30 11:40:03 +08:00
void send_connect();
void send_createStream();
void send_play();
void send_pause(bool pause);
2020-08-30 10:48:34 +08:00
private:
std::string _app;
std::string _stream_id;
std::string _tc_url;
2017-04-01 16:35:56 +08:00
2020-08-30 10:48:34 +08:00
bool _paused = false;
2020-03-20 11:51:24 +08:00
bool _metadata_got = false;
2020-04-08 11:16:09 +08:00
//是否为性能测试模式
bool _benchmark_mode = false;
2020-08-30 10:48:34 +08:00
//播放进度控制
uint32_t _seek_ms = 0;
uint32_t _fist_stamp[2] = {0, 0};
uint32_t _now_stamp[2] = {0, 0};
toolkit::Ticker _now_stamp_ticker[2];
std::deque<std::function<void(AMFValue &dec)> > _deque_on_status;
std::unordered_map<int, std::function<void(AMFDecoder &dec)> > _map_on_result;
2020-08-30 10:48:34 +08:00
//rtmp接收超时计时器
toolkit::Ticker _rtmp_recv_ticker;
2020-08-30 10:48:34 +08:00
//心跳发送定时器
std::shared_ptr<toolkit::Timer> _beat_timer;
2020-08-30 10:48:34 +08:00
//播放超时定时器
std::shared_ptr<toolkit::Timer> _play_timer;
2020-08-30 10:48:34 +08:00
//rtmp接收超时定时器
std::shared_ptr<toolkit::Timer> _rtmp_recv_timer;
2017-04-01 16:35:56 +08:00
};
2018-10-24 17:17:55 +08:00
} /* namespace mediakit */
2020-08-30 10:48:34 +08:00
#endif /* SRC_RTMP_RtmpPlayer_H_ */