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

xiongzilaing committed
11 12
#include <cctype>
#include <algorithm>
13
#include "RtspDemuxer.h"
xiongziliang committed
14
#include "Util/base64.h"
15
#include "Extension/Factory.h"
xiongzilaing committed
16

xzl committed
17
using namespace std;
xiongzilaing committed
18

xiongziliang committed
19
namespace mediakit {
xzl committed
20

21
void RtspDemuxer::loadSdp(const string &sdp){
22
    loadSdp(SdpParser(sdp));
xiongziliang committed
23 24
}

xiongziliang committed
25
void RtspDemuxer::loadSdp(const SdpParser &attr) {
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    auto tracks = attr.getAvailableTrack();
    for (auto &track : tracks){
        switch (track->_type) {
            case TrackVideo: {
                makeVideoTrack(track);
            }
                break;
            case TrackAudio: {
                makeAudioTrack(track);
            }
                break;
            default:
                break;
        }
    }
    auto titleTrack = attr.getTrack(TrackTitle);
    if(titleTrack){
        _fDuration = titleTrack->_duration;
    }
xzl committed
45
}
46
bool RtspDemuxer::inputRtp(const RtpPacket::Ptr & rtp) {
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    switch (rtp->type) {
    case TrackVideo:{
        if(_videoRtpDecoder){
            return _videoRtpDecoder->inputRtp(rtp, true);
        }
        return false;
    }
    case TrackAudio:{
        if(_audioRtpDecoder){
            _audioRtpDecoder->inputRtp(rtp, false);
            return false;
        }
        return false;
    }
    default:
        return false;
    }
xzl committed
64 65 66
}


xiongziliang committed
67
void RtspDemuxer::makeAudioTrack(const SdpTrack::Ptr &audio) {
68
    //生成Track对象
xiongziliang committed
69
    _audioTrack = dynamic_pointer_cast<AudioTrack>(Factory::getTrackBySdp(audio));
xiongziliang committed
70
    if(_audioTrack){
71 72 73 74 75 76 77 78 79 80
        //生成RtpCodec对象以便解码rtp
        _audioRtpDecoder = Factory::getRtpDecoderByTrack(_audioTrack);
        if(_audioRtpDecoder){
            //设置rtp解码器代理,生成的frame写入该Track
            _audioRtpDecoder->addDelegate(_audioTrack);
            onAddTrack(_audioTrack);
        } else{
            //找不到相应的rtp解码器,该track无效
            _audioTrack.reset();
        }
xiongziliang committed
81
    }
xzl committed
82 83
}

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

xiongziliang committed
101
} /* namespace mediakit */