RtmpSession.h 3.46 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
xiongziliang committed
6 7 8
 * 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.
xzl committed
9 10 11 12 13 14 15 16 17
 */

#ifndef SRC_RTMP_RTMPSESSION_H_
#define SRC_RTMP_RTMPSESSION_H_

#include <unordered_map>
#include "amf.h"
#include "Rtmp.h"
#include "utils.h"
xiongziliang committed
18
#include "Common/config.h"
xzl committed
19
#include "RtmpProtocol.h"
20
#include "RtmpMediaSourceImp.h"
xiongzilaing committed
21 22
#include "Util/util.h"
#include "Util/TimeTicker.h"
23
#include "Network/TcpSession.h"
xiongziliang committed
24
#include "Common/Stamp.h"
25

xiongziliang committed
26
using namespace toolkit;
xzl committed
27

xiongziliang committed
28
namespace mediakit {
xzl committed
29

30
class RtmpSession: public TcpSession ,public  RtmpProtocol , public MediaSourceEvent{
xzl committed
31
public:
32
    typedef std::shared_ptr<RtmpSession> Ptr;
xiongziliang committed
33 34 35 36
    RtmpSession(const Socket::Ptr &sock);
    ~RtmpSession() override;

    void onRecv(const Buffer::Ptr &buf) override;
37 38
    void onError(const SockException &err) override;
    void onManager() override;
xiongziliang committed
39

xzl committed
40
private:
41 42 43
    void onProcessCmd(AMFDecoder &dec);
    void onCmd_connect(AMFDecoder &dec);
    void onCmd_createStream(AMFDecoder &dec);
xzl committed
44

45 46
    void onCmd_publish(AMFDecoder &dec);
    void onCmd_deleteStream(AMFDecoder &dec);
xzl committed
47

48 49 50 51 52
    void onCmd_play(AMFDecoder &dec);
    void onCmd_play2(AMFDecoder &dec);
    void doPlay(AMFDecoder &dec);
    void doPlayResponse(const string &err,const std::function<void(bool)> &cb);
    void sendPlayResponse(const string &err,const RtmpMediaSource::Ptr &src);
53

54 55 56
    void onCmd_seek(AMFDecoder &dec);
    void onCmd_pause(AMFDecoder &dec);
    void setMetaData(AMFDecoder &dec);
xzl committed
57

58 59
    void onSendMedia(const RtmpPacket::Ptr &pkt);
    void onSendRawData(const Buffer::Ptr &buffer) override{
xiongziliang committed
60
        _total_bytes += buffer->size();
61 62
        send(buffer);
    }
xiongziliang committed
63
    void onRtmpChunk(RtmpPacket &chunk_data) override;
xzl committed
64

65 66 67
    template<typename first, typename second>
    inline void sendReply(const char *str, const first &reply, const second &status) {
        AMFEncoder invoke;
xiongziliang committed
68
        invoke << str << _recv_req_id << reply << status;
69 70
        sendResponse(MSG_CMD, invoke.data());
    }
71

72 73 74 75
    ///////MediaSourceEvent override///////
    // 关闭
    bool close(MediaSource &sender, bool force) override;
    // 播放总人数
76
    int totalReaderCount(MediaSource &sender) override;
77 78 79 80 81 82
    // 获取媒体源类型
    MediaOriginType getOriginType(MediaSource &sender) const override;
    // 获取媒体源url或者文件路径
    string getOriginUrl(MediaSource &sender) const override;
    // 获取媒体源客户端相关信息
    std::shared_ptr<SockInfo> getOriginSock(MediaSource &sender) const override;
83

84 85 86
    void setSocketFlags();
    string getStreamId(const string &str);
    void dumpMetadata(const AMFValue &metadata);
xiongziliang committed
87

xiongziliang committed
88
private:
xiongziliang committed
89
    bool _paused = false;
90
    bool _set_meta_data = false;
xiongziliang committed
91 92 93 94 95
    double _recv_req_id = 0;
    //消耗的总流量
    uint64_t _total_bytes = 0;

    std::string _tc_url;
96 97
    //时间戳修整器
    Stamp _stamp[2];
xiongziliang committed
98 99 100
    //数据接收超时计时器
    Ticker _ticker;
    MediaInfo _media_info;
101

xiongziliang committed
102 103 104
    std::weak_ptr<RtmpMediaSource> _player_src;
    std::shared_ptr<RtmpMediaSourceImp> _publisher_src;
    RtmpMediaSource::RingType::RingReader::Ptr _ring_reader;
xzl committed
105 106
};

xiongziliang committed
107 108 109 110 111
/**
 * 支持ssl加密的rtmp服务器
 */
typedef TcpSessionWithSSL<RtmpSession> RtmpSessionWithSSL;

xiongziliang committed
112
} /* namespace mediakit */
xzl committed
113
#endif /* SRC_RTMP_RTMPSESSION_H_ */