RtspDemuxer.cpp 3.87 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
RtspDemuxer::RtspDemuxer(const string& sdp) {
xiongziliang committed
39 40 41 42 43 44 45 46 47 48
	loadSdp(SdpAttr(sdp));
}

RtspDemuxer::RtspDemuxer(const SdpAttr &attr) {
	loadSdp(attr);
}

void RtspDemuxer::loadSdp(const SdpAttr &attr) {
	auto tracks = attr.getAvailableTrack();
	for (auto &track : tracks){
49
		switch (track->_type) {
xiongziliang committed
50 51 52 53 54 55 56 57 58 59
			case TrackVideo: {
				makeVideoTrack(track);
			}
				break;
			case TrackAudio: {
				makeAudioTrack(track);
			}
				break;
			default:
				break;
xzl committed
60 61
		}
	}
xiongziliang committed
62 63 64 65
	auto titleTrack = attr.getTrack(TrackTitle);
	if(titleTrack){
		_fDuration = titleTrack->_duration;
	}
xzl committed
66
}
67
bool RtspDemuxer::inputRtp(const RtpPacket::Ptr & rtp) {
68
	switch (rtp->type) {
xiongziliang committed
69 70 71 72 73 74 75
	case TrackVideo:{
		if(_videoRtpDecoder){
			return _videoRtpDecoder->inputRtp(rtp, true);
		}
		return false;
	}
	case TrackAudio:{
xiongziliang committed
76 77 78 79
		if(_audioRtpDecoder){
			_audioRtpDecoder->inputRtp(rtp, false);
			return false;
		}
xiongziliang committed
80 81
		return false;
	}
xzl committed
82 83 84 85 86 87
	default:
		return false;
	}
}


xiongziliang committed
88
void RtspDemuxer::makeAudioTrack(const SdpTrack::Ptr &audio) {
89
	//生成Track对象
xiongziliang committed
90
    _audioTrack = dynamic_pointer_cast<AudioTrack>(Factory::getTrackBySdp(audio));
xiongziliang committed
91
    if(_audioTrack){
92
    	//生成RtpCodec对象以便解码rtp
93
		_audioRtpDecoder = Factory::getRtpDecoderById(_audioTrack->getCodecId());
94 95
		if(_audioRtpDecoder){
			//设置rtp解码器代理,生成的frame写入该Track
96
			_audioRtpDecoder->addDelegate(_audioTrack);
xiongziliang committed
97 98 99
		} else{
			//找不到相应的rtp解码器,该track无效
			_audioTrack.reset();
100
		}
xiongziliang committed
101
    }
xzl committed
102 103
}

xiongziliang committed
104
void RtspDemuxer::makeVideoTrack(const SdpTrack::Ptr &video) {
105
	//生成Track对象
xiongziliang committed
106
	_videoTrack = dynamic_pointer_cast<VideoTrack>(Factory::getTrackBySdp(video));
xiongziliang committed
107
	if(_videoTrack){
108
		//生成RtpCodec对象以便解码rtp
109
		_videoRtpDecoder = Factory::getRtpDecoderById(_videoTrack->getCodecId());
110 111
		if(_videoRtpDecoder){
			//设置rtp解码器代理,生成的frame写入该Track
112
			_videoRtpDecoder->addDelegate(_videoTrack);
xiongziliang committed
113 114 115
		}else{
			//找不到相应的rtp解码器,该track无效
			_videoTrack.reset();
116
		}
xiongziliang committed
117
	}
xzl committed
118 119
}

120
vector<Track::Ptr> RtspDemuxer::getTracks() const {
xiongziliang committed
121 122 123 124 125 126 127 128
	vector<Track::Ptr> ret;
	if(_videoTrack){
		ret.emplace_back(_videoTrack);
	}
	if(_audioTrack){
		ret.emplace_back(_audioTrack);
	}
	return ret;
xzl committed
129 130
}

xiongziliang committed
131
bool RtspDemuxer::isInited() const {
132 133 134 135 136 137 138 139 140 141 142
    bool videoReady = true ,auidoReady = true;

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

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

    return videoReady && auidoReady;
xiongziliang committed
143 144 145 146 147 148
}

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

xiongziliang committed
149
} /* namespace mediakit */