RtspPlayerImp.h 3.35 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_RTP_RTPPARSERTESTER_H_
#define SRC_RTP_RTPPARSERTESTER_H_

#include <memory>
xiongzilaing committed
15 16
#include <algorithm>
#include <functional>
xiongziliang committed
17
#include "Common/config.h"
xiongzilaing committed
18
#include "RtspPlayer.h"
xiongziliang committed
19
#include "RtspDemuxer.h"
xiongzilaing committed
20
#include "Poller/Timer.h"
xzl committed
21 22 23
#include "Util/TimeTicker.h"

using namespace std;
xiongziliang committed
24
using namespace toolkit;
xzl committed
25

xiongziliang committed
26
namespace mediakit {
xzl committed
27

xiongziliang committed
28
class RtspPlayerImp : public PlayerImp<RtspPlayer,RtspDemuxer> {
xzl committed
29
public:
30
    typedef std::shared_ptr<RtspPlayerImp> Ptr;
xiongziliang committed
31 32 33 34 35 36 37 38

    RtspPlayerImp(const EventPoller::Ptr &poller) : PlayerImp<RtspPlayer, RtspDemuxer>(poller) {}
    ~RtspPlayerImp() 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 46

    }

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

xzl committed
50
private:
51 52
    //派生类回调函数
    bool onCheckSDP(const string &sdp) override {
xiongziliang committed
53 54 55
        _rtsp_media_src = dynamic_pointer_cast<RtspMediaSource>(_pMediaSrc);
        if (_rtsp_media_src) {
            _rtsp_media_src->setSdp(sdp);
56
        }
57 58
        _delegate.reset(new RtspDemuxer);
        _delegate->loadSdp(sdp);
59
        return true;
60
    }
xiongziliang committed
61

62
    void onRecvRTP(const RtpPacket::Ptr &rtp, const SdpTrack::Ptr &track) override {
xiongziliang committed
63
        if (_rtsp_media_src) {
xiongziliang committed
64 65
            // rtsp直接代理是无法判断该rtp是否是I帧,所以GOP缓存基本是无效的
            // 为了减少内存使用,那么我们设置为一直关键帧以便清空GOP缓存
xiongziliang committed
66
            _rtsp_media_src->onWrite(rtp, true);
67
        }
xiongziliang committed
68
        _delegate->inputRtp(rtp);
69

xiongziliang committed
70 71 72
        if (_max_analysis_ms && _delegate->isInited(_max_analysis_ms)) {
            PlayerImp<RtspPlayer, RtspDemuxer>::onPlayResult(SockException(Err_success, "play rtsp success"));
            _max_analysis_ms = 0;
73
        }
74
    }
xiongziliang committed
75

76 77 78 79 80 81
    //在RtspPlayer中触发onPlayResult事件只是代表收到play回复了,
    //并不代表所有track初始化成功了(这跟rtmp播放器不一样)
    //如果sdp里面信息不完整,只能尝试延后从rtp中恢复关键信息并初始化track
    //如果超过这个时间还未获取成功,那么会强制触发onPlayResult事件(虽然此时有些track还未初始化成功)
    void onPlayResult(const SockException &ex) override {
        //isInited判断条件:无超时
xiongziliang committed
82
        if (ex || _delegate->isInited(0)) {
83
            //已经初始化成功,说明sdp里面有完善的信息
xiongziliang committed
84 85
            PlayerImp<RtspPlayer, RtspDemuxer>::onPlayResult(ex);
        } else {
86
            //还没初始化成功,说明sdp里面信息不完善,还有一些track未初始化成功
xiongziliang committed
87
            _max_analysis_ms = (*this)[Client::kMaxAnalysisMS];
88
        }
89
    }
xiongziliang committed
90

xiongziliang committed
91
private:
xiongziliang committed
92 93
    int _max_analysis_ms = 0;
    RtspMediaSource::Ptr _rtsp_media_src;
xzl committed
94 95
};

xiongziliang committed
96
} /* namespace mediakit */
xzl committed
97 98

#endif /* SRC_RTP_RTPPARSERTESTER_H_ */