RtspDemuxer.cpp 3.31 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
 */

xiongzilaing committed
27 28
#include <cctype>
#include <algorithm>
29
#include "RtspDemuxer.h"
xiongziliang committed
30
#include "Util/base64.h"
31
#include "Extension/Factory.h"
xiongzilaing committed
32

xzl committed
33
using namespace std;
xiongzilaing committed
34

xiongziliang committed
35
namespace mediakit {
xzl committed
36

37
RtspDemuxer::RtspDemuxer(const string& sdp) {
xiongziliang committed
38
	loadSdp(SdpParser(sdp));
xiongziliang committed
39 40
}

xiongziliang committed
41
RtspDemuxer::RtspDemuxer(const SdpParser &attr) {
xiongziliang committed
42 43 44
	loadSdp(attr);
}

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


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

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

xiongziliang committed
119
} /* namespace mediakit */