Rtmp.cpp 4.56 KB
Newer Older
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
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.
9
 */
xiongziliang committed
10

xiongziliang committed
11 12
#include "Rtmp.h"
#include "Extension/Factory.h"
xiongziliang committed
13 14
namespace mediakit{

15
VideoMeta::VideoMeta(const VideoTrack::Ptr &video,int datarate ){
xiongziliang committed
16
    if(video->getVideoWidth() > 0 ){
17
        _metadata.set("width", video->getVideoWidth());
xiongziliang committed
18 19
    }
    if(video->getVideoHeight() > 0 ){
20
        _metadata.set("height", video->getVideoHeight());
xiongziliang committed
21 22
    }
    if(video->getVideoFps() > 0 ){
23
        _metadata.set("framerate", video->getVideoFps());
xiongziliang committed
24
    }
25
    _metadata.set("videodatarate", datarate);
xiongziliang committed
26
    _codecId = video->getCodecId();
27
    _metadata.set("videocodecid", Factory::getAmfByCodecId(_codecId));
xiongziliang committed
28 29
}

30 31
AudioMeta::AudioMeta(const AudioTrack::Ptr &audio,int datarate){
    _metadata.set("audiodatarate", datarate);
xiongziliang committed
32
    if(audio->getAudioSampleRate() > 0){
33
        _metadata.set("audiosamplerate", audio->getAudioSampleRate());
xiongziliang committed
34 35
    }
    if(audio->getAudioSampleBit() > 0){
36
        _metadata.set("audiosamplesize", audio->getAudioSampleBit());
xiongziliang committed
37 38
    }
    if(audio->getAudioChannel() > 0){
39
        _metadata.set("stereo", audio->getAudioChannel() > 1);
xiongziliang committed
40 41
    }
    _codecId = audio->getCodecId();
42
    _metadata.set("audiocodecid", Factory::getAmfByCodecId(_codecId));
xiongziliang committed
43 44
}

xiongziliang committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
uint8_t getAudioRtmpFlags(const Track::Ptr &track){
    switch (track->getTrackType()){
        case TrackAudio : {
            auto audioTrack = dynamic_pointer_cast<AudioTrack>(track);
            if (!audioTrack) {
                WarnL << "获取AudioTrack失败";
                return 0;
            }
            auto iSampleRate = audioTrack->getAudioSampleRate();
            auto iChannel = audioTrack->getAudioChannel();
            auto iSampleBit = audioTrack->getAudioSampleBit();

            uint8_t flvAudioType ;
            switch (track->getCodecId()){
                case CodecG711A : flvAudioType = FLV_CODEC_G711A; break;
                case CodecG711U : flvAudioType = FLV_CODEC_G711U; break;
61 62 63 64 65 66 67 68
                case CodecOpus : {
                    flvAudioType = FLV_CODEC_OPUS;
                    //opus不通过flags获取音频相关信息
                    iSampleRate = 44100;
                    iSampleBit = 16;
                    iChannel = 2;
                    break;
                }
xiongziliang committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
                case CodecAAC : {
                    flvAudioType = FLV_CODEC_AAC;
                    //aac不通过flags获取音频相关信息
                    iSampleRate = 44100;
                    iSampleBit = 16;
                    iChannel = 2;
                    break;
                }
                default: WarnL << "该编码格式不支持转换为RTMP: " << track->getCodecName(); return 0;
            }

            uint8_t flvSampleRate;
            switch (iSampleRate) {
                case 44100:
                    flvSampleRate = 3;
                    break;
                case 22050:
                    flvSampleRate = 2;
                    break;
                case 11025:
                    flvSampleRate = 1;
                    break;
                case 16000: // nellymoser only
                case 8000: // nellymoser only
                case 5512: // not MP3
                    flvSampleRate = 0;
                    break;
                default:
                    WarnL << "FLV does not support sample rate " << iSampleRate << " ,choose from (44100, 22050, 11025)";
                    return 0;
            }

            uint8_t flvStereoOrMono = (iChannel > 1);
            uint8_t flvSampleBit = iSampleBit == 16;
            return (flvAudioType << 4) | (flvSampleRate << 2) | (flvSampleBit << 1) | flvStereoOrMono;
        }

        default : return 0;
    }
}


111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
void Metadata::addTrack(AMFValue &metadata, const Track::Ptr &track) {
    Metadata::Ptr new_metadata;
    switch (track->getTrackType()) {
        case TrackVideo: {
            new_metadata = std::make_shared<VideoMeta>(dynamic_pointer_cast<VideoTrack>(track));
        }
            break;
        case TrackAudio: {
            new_metadata = std::make_shared<AudioMeta>(dynamic_pointer_cast<AudioTrack>(track));
        }
            break;
        default:
            return;
    }

    new_metadata->getMetadata().object_for_each([&](const std::string &key, const AMFValue &value) {
        metadata.set(key, value);
    });
}
xiongziliang committed
130
}//namespace mediakit