RtspDemuxer.cpp 4.12 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Copyright (c) 2016 xiongziliang <771730766@qq.com>
 *
 * 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
 */

xiongzilaing committed
27 28
#include <cctype>
#include <algorithm>
29
#include "RtspDemuxer.h"
xiongziliang committed
30
#include "Util/base64.h"
xzl committed
31
#include "H264/SPSParser.h"
32
#include "Common/Factory.h"
xiongzilaing committed
33

xzl committed
34
using namespace std;
xiongzilaing committed
35

xiongziliang committed
36
namespace mediakit {
xzl committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

static int getTimeInSDP(const string &sdp) {
	auto strRange = FindField(sdp.data(), "a=range:npt=", "\r\n");
	strRange.append(" ");
	auto iPos = strRange.find('-');
	if (iPos == string::npos) {
		return 0;
	}
	auto strStart = strRange.substr(0, iPos);
	auto strEnd = strRange.substr(iPos + 1);
	strEnd.pop_back();
	if (strStart == "now") {
		strStart = "0";
	}
	return atof(strEnd.data()) - atof(strStart.data());
}
53
RtspDemuxer::RtspDemuxer(const string& sdp) {
xzl committed
54 55 56 57 58
	RtspTrack tmp[2];
	int cnt = parserSDP(sdp, tmp);
	for (int i = 0; i < cnt; i++) {
		switch (tmp[i].type) {
		case TrackVideo: {
xiongziliang committed
59
			makeVideoTrack(tmp[i]);
xzl committed
60 61 62
		}
			break;
		case TrackAudio: {
xiongziliang committed
63
			makeAudioTrack(tmp[i]);
xzl committed
64 65 66 67 68 69
		}
			break;
		default:
			break;
		}
	}
70
	_fDuration = getTimeInSDP(sdp);
xzl committed
71 72
}

73
bool RtspDemuxer::inputRtp(const RtpPacket::Ptr & rtp) {
74
	switch (rtp->type) {
xiongziliang committed
75 76 77 78 79 80 81
	case TrackVideo:{
		if(_videoRtpDecoder){
			return _videoRtpDecoder->inputRtp(rtp, true);
		}
		return false;
	}
	case TrackAudio:{
xiongziliang committed
82 83 84 85
		if(_audioRtpDecoder){
			_audioRtpDecoder->inputRtp(rtp, false);
			return false;
		}
xiongziliang committed
86 87
		return false;
	}
xzl committed
88 89 90 91 92 93
	default:
		return false;
	}
}


xiongziliang committed
94
void RtspDemuxer::makeAudioTrack(const RtspTrack &audio) {
95
	//生成Track对象
96
    _audioTrack = dynamic_pointer_cast<AudioTrack>(Factory::getTrackBySdp(audio.trackSdp));
xiongziliang committed
97
    if(_audioTrack){
98
    	//生成RtpCodec对象以便解码rtp
99
		_audioRtpDecoder = Factory::getRtpDecoderById(_audioTrack->getCodecId(),_audioTrack->getAudioSampleRate());
100 101 102
		if(_audioRtpDecoder){
			//设置rtp解码器代理,生成的frame写入该Track
			_audioRtpDecoder->setDelegate(_audioTrack);
xiongziliang committed
103 104 105
		} else{
			//找不到相应的rtp解码器,该track无效
			_audioTrack.reset();
106
		}
xiongziliang committed
107
    }
xzl committed
108 109
}

xiongziliang committed
110
void RtspDemuxer::makeVideoTrack(const RtspTrack &video) {
111
	//生成Track对象
112
	_videoTrack = dynamic_pointer_cast<VideoTrack>(Factory::getTrackBySdp(video.trackSdp));
xiongziliang committed
113
	if(_videoTrack){
114
		//生成RtpCodec对象以便解码rtp
115
		_videoRtpDecoder = Factory::getRtpDecoderById(_videoTrack->getCodecId(),90000);
116 117 118
		if(_videoRtpDecoder){
			//设置rtp解码器代理,生成的frame写入该Track
			_videoRtpDecoder->setDelegate(_videoTrack);
xiongziliang committed
119 120 121
		}else{
			//找不到相应的rtp解码器,该track无效
			_videoTrack.reset();
122
		}
xiongziliang committed
123
	}
xzl committed
124 125
}

126
vector<Track::Ptr> RtspDemuxer::getTracks() const {
xiongziliang committed
127 128 129 130 131 132 133 134
	vector<Track::Ptr> ret;
	if(_videoTrack){
		ret.emplace_back(_videoTrack);
	}
	if(_audioTrack){
		ret.emplace_back(_audioTrack);
	}
	return ret;
xzl committed
135 136
}

xiongziliang committed
137
bool RtspDemuxer::isInited() const {
138 139 140 141 142 143 144 145 146 147 148
    bool videoReady = true ,auidoReady = true;

    if(_videoTrack){
        videoReady = _videoTrack->ready();
    }

    if(_audioTrack){
        auidoReady = _audioTrack->ready();
    }

    return videoReady && auidoReady;
xiongziliang committed
149 150 151 152 153 154
}

float RtspDemuxer::getDuration() const {
	return _fDuration;
}

xiongziliang committed
155

xiongziliang committed
156
} /* namespace mediakit */