RtmpPlayerImp.h 3.28 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
xiongziliang committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
xzl committed
25 26 27 28 29 30
 */

#ifndef SRC_RTMP_RTMPPLAYERIMP_H_
#define SRC_RTMP_RTMPPLAYERIMP_H_

#include <memory>
xiongzilaing committed
31
#include <functional>
xiongziliang committed
32
#include "Common/config.h"
xzl committed
33
#include "RtmpPlayer.h"
xiongziliang committed
34
#include "RtmpMediaSource.h"
35
#include "RtmpMuxer/RtmpDemuxer.h"
xiongzilaing committed
36
#include "Poller/Timer.h"
xzl committed
37
#include "Util/TimeTicker.h"
xiongziliang committed
38
using namespace toolkit;
xiongziliang committed
39
using namespace mediakit::Client;
xzl committed
40

xiongziliang committed
41
namespace mediakit {
xzl committed
42

43
class RtmpPlayerImp: public PlayerImp<RtmpPlayer,RtmpDemuxer> {
xzl committed
44 45
public:
    typedef std::shared_ptr<RtmpPlayerImp> Ptr;
46
    RtmpPlayerImp(const EventPoller::Ptr &poller) : PlayerImp<RtmpPlayer,RtmpDemuxer>(poller){};
xiongziliang committed
47 48 49
    virtual ~RtmpPlayerImp(){
        DebugL<<endl;
    };
xiongziliang committed
50
    float getProgress() const override{
xzl committed
51
        if(getDuration() > 0){
52
            return getProgressMilliSecond() / (getDuration() * 1000);
xzl committed
53
        }
xiongziliang committed
54
        return PlayerBase::getProgress();
xzl committed
55 56 57
    };
    void seekTo(float fProgress) override{
        fProgress = MAX(float(0),MIN(fProgress,float(1.0)));
58
        seekToMilliSecond(fProgress * getDuration() * 1000);
xzl committed
59
    };
xiongziliang committed
60
    void play(const string &strUrl) override {
xiongziliang committed
61
        _analysisMs = (*this)[kMaxAnalysisMS].as<int>();
62 63
        PlayerImp<RtmpPlayer,RtmpDemuxer>::play(strUrl);
    }
xzl committed
64 65 66
private:
    //派生类回调函数
    bool onCheckMeta(AMFValue &val)  override {
67 68 69
        _pRtmpMediaSrc = dynamic_pointer_cast<RtmpMediaSource>(_pMediaSrc);
        if(_pRtmpMediaSrc){
            _pRtmpMediaSrc->onGetMetaData(val);
xiongziliang committed
70
        }
71 72
        _parser.reset(new RtmpDemuxer(val));
        return true;
xzl committed
73
    }
xiongziliang committed
74
    void onMediaData(const RtmpPacket::Ptr &chunkData) override {
75
    	if(_pRtmpMediaSrc){
xiongziliang committed
76
            _pRtmpMediaSrc->onWrite(chunkData);
77
        }
78
        if(!_parser){
79
    	    //这个流没有metedata,那么尝试在音视频包里面还原出相关信息
80
            _parser.reset(new RtmpDemuxer());
81
            onPlayResult_l(SockException(Err_success,"play rtmp success"));
xiongziliang committed
82
        }
83
        _parser->inputRtmp(chunkData);
84
        checkInited(_analysisMs);
xzl committed
85
    }
xiongziliang committed
86
private:
87
    RtmpMediaSource::Ptr _pRtmpMediaSrc;
88
    int _analysisMs;
xzl committed
89
};
90 91


xiongziliang committed
92 93
} /* namespace mediakit */

xzl committed
94
#endif /* SRC_RTMP_RTMPPLAYERIMP_H_ */