RtmpPlayerImp.h 2.49 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
 */

#ifndef SRC_RTMP_RTMPPLAYERIMP_H_
#define SRC_RTMP_RTMPPLAYERIMP_H_

#include <memory>
xiongzilaing committed
15
#include <functional>
xiongziliang committed
16
#include "Common/config.h"
xzl committed
17
#include "RtmpPlayer.h"
xiongziliang committed
18
#include "RtmpMediaSource.h"
xiongziliang committed
19
#include "RtmpDemuxer.h"
xiongzilaing committed
20
#include "Poller/Timer.h"
xzl committed
21
#include "Util/TimeTicker.h"
xiongziliang committed
22
using namespace toolkit;
xiongziliang committed
23
using namespace mediakit::Client;
xzl committed
24

xiongziliang committed
25
namespace mediakit {
xzl committed
26

27
class RtmpPlayerImp: public PlayerImp<RtmpPlayer,RtmpDemuxer> {
xzl committed
28 29
public:
    typedef std::shared_ptr<RtmpPlayerImp> Ptr;
xiongziliang committed
30 31 32 33 34 35 36 37 38

    RtmpPlayerImp(const EventPoller::Ptr &poller) : PlayerImp<RtmpPlayer, RtmpDemuxer>(poller) {};

    ~RtmpPlayerImp() override {
        DebugL << endl;
    }

    float getProgress() const override {
        if (getDuration() > 0) {
39
            return getProgressMilliSecond() / (getDuration() * 1000);
xzl committed
40
        }
xiongziliang committed
41
        return PlayerBase::getProgress();
xiongziliang committed
42 43 44 45
    }

    void seekTo(float fProgress) override {
        fProgress = MAX(float(0), MIN(fProgress, float(1.0)));
46
        seekToMilliSecond(fProgress * getDuration() * 1000);
xiongziliang committed
47 48
    }

xiongziliang committed
49
    void play(const string &strUrl) override {
xiongziliang committed
50
        PlayerImp<RtmpPlayer, RtmpDemuxer>::play(strUrl);
51
    }
xiongziliang committed
52

xzl committed
53 54
private:
    //派生类回调函数
55
    bool onCheckMeta(const AMFValue &val) override {
xiongziliang committed
56 57 58
        _rtmp_src = dynamic_pointer_cast<RtmpMediaSource>(_pMediaSrc);
        if (_rtmp_src) {
            _rtmp_src->setMetaData(val);
59
            _set_meta_data = true;
xiongziliang committed
60
        }
xiongziliang committed
61 62
        _delegate.reset(new RtmpDemuxer);
        _delegate->loadMetaData(val);
63
        return true;
xzl committed
64
    }
xiongziliang committed
65

xiongziliang committed
66
    void onMediaData(const RtmpPacket::Ptr &chunkData) override {
xiongziliang committed
67 68
        if (_rtmp_src) {
            if (!_set_meta_data && !chunkData->isCfgFrame()) {
69
                _set_meta_data = true;
xiongziliang committed
70
                _rtmp_src->setMetaData(TitleMeta().getMetadata());
71
            }
xiongziliang committed
72
            _rtmp_src->onWrite(chunkData);
73
        }
xiongziliang committed
74
        if (!_delegate) {
75
            //这个流没有metadata
xiongziliang committed
76
            _delegate.reset(new RtmpDemuxer());
xiongziliang committed
77
        }
xiongziliang committed
78
        _delegate->inputRtmp(chunkData);
xzl committed
79
    }
xiongziliang committed
80

xiongziliang committed
81
private:
xiongziliang committed
82
    RtmpMediaSource::Ptr _rtmp_src;
83
    bool _set_meta_data = false;
xzl committed
84
};
85 86


xiongziliang committed
87 88
} /* namespace mediakit */

xzl committed
89
#endif /* SRC_RTMP_RTMPPLAYERIMP_H_ */