当前位置: 首页 > 知识库问答 >
问题:

外挂机Hls质量

杜元明
2023-03-14
MappingTrackSelector.MappedTrackInfo trackInfo = mDefaultTrackSelector.getCurrentMappedTrackInfo();
                        mDefaultTrackSelector.selectTracks(

//这里应该去什么?

  , trackInfo.getTrackGroups(4));

共有1个答案

柳羽
2023-03-14

关于这个线程:https://github.com/google/exoplayer/issues/2250,我在播放前一个时改变了exo player的视频质量,所以它不会立即进入缓冲。

所以我有下一节课:

public enum HLSQuality {
    Auto, Quality1080, Quality720, Quality480, NoValue
}

class HLSUtil {

    private HLSUtil() {
    }

    @NonNull
    static HLSQuality getQuality(@NonNull Format format) {
             switch (format.height) {
            case 1080: {
                return HLSQuality.Quality1080;
            }
            case 720: {
                return HLSQuality.Quality720;
            }
            case 480:
            case 486: {
                return HLSQuality.Quality480;
            }
            default: {
                return HLSQuality.NoValue;
            }
        }
    }

    static boolean isQualityPlayable(@NonNull Format format) {
        return format.height <= 1080;
    }
}


public class ClassAdaptiveTrackSelection extends BaseTrackSelection {

    public static final class Factory implements TrackSelection.Factory {
        private final BandwidthMeter bandwidthMeter;
        private final int maxInitialBitrate = 2000000;
        private final int minDurationForQualityIncreaseMs = 10000;
        private final int maxDurationForQualityDecreaseMs = 25000;
        private final int minDurationToRetainAfterDiscardMs = 25000;
        private final float bandwidthFraction = 0.75f;
        private final float bufferedFractionToLiveEdgeForQualityIncrease = 0.75f;

        public Factory(BandwidthMeter bandwidthMeter) {
            this.bandwidthMeter = bandwidthMeter;
        }

        @Override
        public ClassAdaptiveTrackSelection createTrackSelection(TrackGroup group, int... tracks) {
            Log.d(ClassAdaptiveTrackSelection.class.getSimpleName(), " Video player quality reset to Auto");
            sHLSQuality = HLSQuality.Auto;

            return new ClassAdaptiveTrackSelection(
                    group,
                    tracks,
                    bandwidthMeter,
                    maxInitialBitrate,
                    minDurationForQualityIncreaseMs,
                    maxDurationForQualityDecreaseMs,
                    minDurationToRetainAfterDiscardMs,
                    bandwidthFraction,
                    bufferedFractionToLiveEdgeForQualityIncrease
            );
        }
    }

    private static HLSQuality sHLSQuality = HLSQuality.Auto;
    private final BandwidthMeter bandwidthMeter;
    private final int maxInitialBitrate;
    private final long minDurationForQualityIncreaseUs;
    private final long maxDurationForQualityDecreaseUs;
    private final long minDurationToRetainAfterDiscardUs;
    private final float bandwidthFraction;
    private final float bufferedFractionToLiveEdgeForQualityIncrease;

    private int selectedIndex;
    private int reason;

    private ClassAdaptiveTrackSelection(TrackGroup group,
                                        int[] tracks,
                                        BandwidthMeter bandwidthMeter,
                                        int maxInitialBitrate,
                                        long minDurationForQualityIncreaseMs,
                                        long maxDurationForQualityDecreaseMs,
                                        long minDurationToRetainAfterDiscardMs,
                                        float bandwidthFraction,
                                        float bufferedFractionToLiveEdgeForQualityIncrease) {
        super(group, tracks);
        this.bandwidthMeter = bandwidthMeter;
        this.maxInitialBitrate = maxInitialBitrate;
        this.minDurationForQualityIncreaseUs = minDurationForQualityIncreaseMs * 1000L;
        this.maxDurationForQualityDecreaseUs = maxDurationForQualityDecreaseMs * 1000L;
        this.minDurationToRetainAfterDiscardUs = minDurationToRetainAfterDiscardMs * 1000L;
        this.bandwidthFraction = bandwidthFraction;
        this.bufferedFractionToLiveEdgeForQualityIncrease = bufferedFractionToLiveEdgeForQualityIncrease;
        selectedIndex = determineIdealSelectedIndex(Long.MIN_VALUE);
        reason = C.SELECTION_REASON_INITIAL;
    }

    @Override
    public void updateSelectedTrack(long playbackPositionUs, long bufferedDurationUs, long availableDurationUs) {
        long nowMs = SystemClock.elapsedRealtime();
        // Stash the current selection, then make a new one.
        int currentSelectedIndex = selectedIndex;
        selectedIndex = determineIdealSelectedIndex(nowMs);
        if (selectedIndex == currentSelectedIndex) {
            return;
        }

        if (!isBlacklisted(currentSelectedIndex, nowMs)) {
            // Revert back to the current selection if conditions are not suitable for switching.
            Format currentFormat = getFormat(currentSelectedIndex);
            Format selectedFormat = getFormat(selectedIndex);
            if (selectedFormat.bitrate > currentFormat.bitrate
                    && bufferedDurationUs < minDurationForQualityIncreaseUs(availableDurationUs)) {
                // The selected track is a higher quality, but we have insufficient buffer to safely switch
                // up. Defer switching up for now.
                selectedIndex = currentSelectedIndex;
            } else if (selectedFormat.bitrate < currentFormat.bitrate
                    && bufferedDurationUs >= maxDurationForQualityDecreaseUs) {
                // The selected track is a lower quality, but we have sufficient buffer to defer switching
                // down for now.
                selectedIndex = currentSelectedIndex;
            }
        }
        // If we adapted, update the trigger.
        if (selectedIndex != currentSelectedIndex) {
            reason = C.SELECTION_REASON_ADAPTIVE;
        }
    }

    @Override
    public int getSelectedIndex() {
        return selectedIndex;
    }

    @Override
    public int getSelectionReason() {
        return reason;
    }

    @Override
    public Object getSelectionData() {
        return null;
    }

    @Override
    public int evaluateQueueSize(long playbackPositionUs, List<? extends MediaChunk> queue) {
        if (queue.isEmpty()) {
            return 0;
        }
        int queueSize = queue.size();
        long bufferedDurationUs = queue.get(queueSize - 1).endTimeUs - playbackPositionUs;
        if (bufferedDurationUs < minDurationToRetainAfterDiscardUs) {
            return queueSize;
        }
        int idealSelectedIndex = determineIdealSelectedIndex(SystemClock.elapsedRealtime());
        Format idealFormat = getFormat(idealSelectedIndex);
        // If the chunks contain video, discard from the first SD chunk beyond
        // minDurationToRetainAfterDiscardUs whose resolution and bitrate are both lower than the ideal
        // track.
        for (int i = 0; i < queueSize; i++) {
            MediaChunk chunk = queue.get(i);
            Format format = chunk.trackFormat;
            long durationBeforeThisChunkUs = chunk.startTimeUs - playbackPositionUs;
            if (durationBeforeThisChunkUs >= minDurationToRetainAfterDiscardUs
                    && format.bitrate < idealFormat.bitrate
                    && format.height != Format.NO_VALUE && format.height < 720
                    && format.width != Format.NO_VALUE && format.width < 1280
                    && format.height < idealFormat.height) {
                return i;
            }
        }
        return queueSize;
    }

    private int determineIdealSelectedIndex(long nowMs) {
        if (sHLSQuality != HLSQuality.Auto) {
            Log.d(ClassAdaptiveTrackSelection.class.getSimpleName(), " Video player quality seeking for " + String.valueOf(sHLSQuality));
            for (int i = 0; i < length; i++) {
                Format format = getFormat(i);
                if (HLSUtil.getQuality(format) == sHLSQuality) {
                    Log.d(ClassAdaptiveTrackSelection.class.getSimpleName(), " Video player quality set to " + String.valueOf(sHLSQuality));
                    return i;
                }
            }
        }

        Log.d(ClassAdaptiveTrackSelection.class.getSimpleName(), " Video player quality seeking for auto quality " + String.valueOf(sHLSQuality));
        long bitrateEstimate = bandwidthMeter.getBitrateEstimate();
        long effectiveBitrate = bitrateEstimate == BandwidthMeter.NO_ESTIMATE
                ? maxInitialBitrate : (long) (bitrateEstimate * bandwidthFraction);
        int lowestBitrateNonBlacklistedIndex = 0;
        for (int i = 0; i < length; i++) {
            if (nowMs == Long.MIN_VALUE || !isBlacklisted(i, nowMs)) {
                Format format = getFormat(i);
                if (format.bitrate <= effectiveBitrate && HLSUtil.isQualityPlayable(format)) {
                    Log.d(ClassAdaptiveTrackSelection.class.getSimpleName(), " Video player quality auto quality found " + String.valueOf(sHLSQuality));
                    return i;
                } else {
                    lowestBitrateNonBlacklistedIndex = i;
                }
            }
        }
        return lowestBitrateNonBlacklistedIndex;
    }

    private long minDurationForQualityIncreaseUs(long availableDurationUs) {
        boolean isAvailableDurationTooShort = availableDurationUs != C.TIME_UNSET
                && availableDurationUs <= minDurationForQualityIncreaseUs;
        return isAvailableDurationTooShort
                ? (long) (availableDurationUs * bufferedFractionToLiveEdgeForQualityIncrease)
                : minDurationForQualityIncreaseUs;
    }

    static void setHLSQuality(HLSQuality HLSQuality) {
        sHLSQuality = HLSQuality;
    }
}

希望能帮到别人。

 类似资料:
  • 我正试图在ExoPlayer中设置视频比特率。我已经这样设置了: 但不管用。在我发现的任何地方,我都发现人们在使用HlsChunk源代码,这在ExoPlayer2.6中是私有的。有人能帮我吗?

  • 我对ExoPlayer没有什么问题。当我尝试播放hls流的视频时,几乎一切都很好。hls流包含3组不同的块列表,每个块列表用于不同的带宽。 但是hls自适应流不起作用,播放器只能与一个块列表一起工作,对于慢速的互联网连接,这个解决方案是不可用的。 我也尝试实现MediaSourceEventListener,并且onDownstreamFormatChanged只在播放器初始化时调用一次。 谢谢你

  • 还在回家的路上,绝影就发短信把辞职的事情告诉了大爷,原以为大爷在四川等自己辞职等了这么久,收到消息肯定欣喜若狂。但事情往往就是这样,你没有女朋友的时候想有个女朋友,但当你费尽千辛万苦台词排练了几百遍终于半骗半哄有了一个女朋友的时候,你才发现有个女朋友也不过就是那个样子,甚至有时候还不如一个人单身过。 大爷还是用那副面孔对着绝影:“辞了?” “辞了。那天谈判,我第一句话就是‘陈董,我决定离开公司’。

  • SRS支持HLS/RTMP两种成熟而且广泛应用的流媒体分发方式。 RTMP指Adobe的RTMP(Realtime Message Protocol),广泛应用于低延时直播,也是编码器和服务器对接的实际标准协议,在PC(Flash)上有最佳观看体验和最佳稳定性。 HLS指Apple的HLS(Http Live Streaming),本身就是Live(直播)的,不过Vod(点播)也能支持。HLS是A

  • 由于公司业务,需要实现基于WEB查看监控画面,经过各种百度,补充了不少相关知识,了解到了很多大神的实现方法,也因为很多过时的帖子,而踩了不少的坑。 在被动积累了一些知识后,动手实现了v1.0初级版本,但是部署工作量较大,因为需要手动安装组件、配置、调试。 后来在发布到gitee后,被多位朋友咨询“如何使用”的问题后,决定对该版本再做提升,所以有了现在的v2.0版本,提高了部署效率,直接打包配置设备

  • 在这一点上,我的解决方案是工作的,但只能作为RTMP,我可以使用URL完美地观看我的流: 但问题是我的LG智能电视使用WebOS不支持RTMP,我真的很想在那里播放我的流。我现在能看到的唯一解决方案是使用HLS。使用HLS也很好,但我需要在打开电视中的HLS流之前执行我的ffmpeg命令,否则它将不会创建在电视上显示流所需的文件。