RtmpSession.h 3.08 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 33 34 35 36 37
    typedef std::shared_ptr<RtmpSession> Ptr;
    RtmpSession(const Socket::Ptr &_sock);
    virtual ~RtmpSession();
    void onRecv(const Buffer::Ptr &pBuf) override;
    void onError(const SockException &err) override;
    void onManager() override;
xzl committed
38
private:
39 40 41
    void onProcessCmd(AMFDecoder &dec);
    void onCmd_connect(AMFDecoder &dec);
    void onCmd_createStream(AMFDecoder &dec);
xzl committed
42

43 44
    void onCmd_publish(AMFDecoder &dec);
    void onCmd_deleteStream(AMFDecoder &dec);
xzl committed
45

46 47 48 49 50
    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);
51

52 53 54
    void onCmd_seek(AMFDecoder &dec);
    void onCmd_pause(AMFDecoder &dec);
    void setMetaData(AMFDecoder &dec);
xzl committed
55

56 57
    void onSendMedia(const RtmpPacket::Ptr &pkt);
    void onSendRawData(const Buffer::Ptr &buffer) override{
58
        _ui64TotalBytes += buffer->size();
59 60 61
        send(buffer);
    }
    void onRtmpChunk(RtmpPacket &chunkData) override;
xzl committed
62

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

70 71 72
    //MediaSourceEvent override
    bool close(MediaSource &sender,bool force) override ;
    int totalReaderCount(MediaSource &sender) override;
73

74 75 76
    void setSocketFlags();
    string getStreamId(const string &str);
    void dumpMetadata(const AMFValue &metadata);
xiongziliang committed
77
private:
78 79 80 81 82
    std::string _strTcUrl;
    MediaInfo _mediaInfo;
    double _dNowReqID = 0;
    bool _set_meta_data = false;
    Ticker _ticker;//数据接收时间
xiongziliang committed
83
    RtmpMediaSource::RingType::RingReader::Ptr _pRingReader;
84 85 86 87 88 89
    std::shared_ptr<RtmpMediaSourceImp> _pPublisherSrc;
    std::weak_ptr<RtmpMediaSource> _pPlayerSrc;
    //时间戳修整器
    Stamp _stamp[2];
    //消耗的总流量
    uint64_t _ui64TotalBytes = 0;
xiongziliang committed
90
    bool _paused = false;
91

xzl committed
92 93
};

xiongziliang committed
94 95 96 97 98 99

/**
 * 支持ssl加密的rtmp服务器
 */
typedef TcpSessionWithSSL<RtmpSession> RtmpSessionWithSSL;

xiongziliang committed
100
} /* namespace mediakit */
xzl committed
101 102

#endif /* SRC_RTMP_RTMPSESSION_H_ */