RtspDemuxer.cpp 3.47 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
xiongziliang committed
5
 *
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
    auto tracks = attr.getAvailableTrack();
27
    for (auto &track : tracks) {
28 29 30 31 32 33 34 35 36 37 38 39 40
        switch (track->_type) {
            case TrackVideo: {
                makeVideoTrack(track);
            }
                break;
            case TrackAudio: {
                makeAudioTrack(track);
            }
                break;
            default:
                break;
        }
    }
41 42 43
    //rtsp能通过sdp立即知道有多少个track
    addTrackCompleted();

44
    auto titleTrack = attr.getTrack(TrackTitle);
45 46
    if (titleTrack) {
        _duration = titleTrack->_duration;
47
    }
xzl committed
48
}
49 50 51 52 53 54

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

bool RtspDemuxer::inputRtp(const RtpPacket::Ptr &rtp) {
55
    switch (rtp->type) {
56
        case TrackVideo: {
57 58
            if (_video_rtp_decoder) {
                return _video_rtp_decoder->inputRtp(rtp, true);
59 60
            }
            return false;
61
        }
62
        case TrackAudio: {
63 64
            if (_audio_rtp_decoder) {
                _audio_rtp_decoder->inputRtp(rtp, false);
65 66
                return false;
            }
67 68
            return false;
        }
69
        default: return false;
70
    }
xzl committed
71 72
}

73
static void setBitRate(const SdpTrack::Ptr &sdp, const Track::Ptr &track) {
xiongziliang committed
74 75 76 77 78 79 80 81
    if (!sdp->_b.empty()) {
        int data_rate = 0;
        sscanf(sdp->_b.data(), "AS:%d", &data_rate);
        if (data_rate) {
            track->setBitRate(data_rate * 1024);
        }
    }
}
xzl committed
82

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

xiongziliang committed
105
void RtspDemuxer::makeVideoTrack(const SdpTrack::Ptr &video) {
106 107 108
    if (_video_rtp_decoder) {
        return;
    }
109
    //生成Track对象
110 111 112 113 114 115
    _video_track = dynamic_pointer_cast<VideoTrack>(Factory::getTrackBySdp(video));
    if (!_video_track) {
        return;
    }
    setBitRate(video, _video_track);
    //生成RtpCodec对象以便解码rtp
116 117
    _video_rtp_decoder = Factory::getRtpDecoderByTrack(_video_track);
    if (!_video_rtp_decoder) {
118 119 120
        //找不到相应的rtp解码器,该track无效
        _video_track.reset();
        return;
121
    }
122
    //设置rtp解码器代理,生成的frame写入该Track
123
    _video_rtp_decoder->addDelegate(_video_track);
124
    addTrack(_video_track);
xzl committed
125 126
}

xiongziliang committed
127
} /* namespace mediakit */