<commit_before>/*
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jitsi.impl.neomedia;

import org.jitsi.service.neomedia.*;

/**
 * The JVB implementation of a {@link MediaStreamTrack}.
 *
 * @author <NAME>
 */
public class MediaStreamTrackImpl
    implements MediaStreamTrack
{
    /**
     * The minimum time (in millis) that is required for the media engine to
     * generate a new key frame.
     */
    private static final int MIN_KEY_FRAME_WAIT_MS = 300;

    /**
     * The {@link RTPEncodingImpl}s that this {@link MediaStreamTrackImpl}
     * possesses, ordered by their subjective quality from low to high.
     */
    private final RTPEncodingImpl[] rtpEncodings;

    /**
     * The {@link MediaStreamTrackReceiver} that receives this instance.
     */
    private final MediaStreamTrackReceiver mediaStreamTrackReceiver;

    /**
     * The time (in millis) that this instance last saw a keyframe.
     */
    private long lastKeyframeMs;

    /**
     * Ctor.
     *
     * @param mediaStreamTrackReceiver The {@link MediaStreamTrackReceiver} that
     * receives this instance.
     * @param rtpEncodings The {@link RTPEncoding}s that this instance
     * possesses.
     */
    public MediaStreamTrackImpl(
        MediaStreamTrackReceiver mediaStreamTrackReceiver,
        RTPEncodingImpl[] rtpEncodings)
    {
        this.rtpEncodings = rtpEncodings;
        this.mediaStreamTrackReceiver = mediaStreamTrackReceiver;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public RTPEncodingImpl[] getRTPEncodings()
    {
        return rtpEncodings;
    }

    /**
     * Gets the {@link MediaStreamTrackReceiver} that receives this instance.
     *
     * @return The {@link MediaStreamTrackReceiver} that receives this instance.
     */
    MediaStreamTrackReceiver getMediaStreamTrackReceiver()
    {
        return mediaStreamTrackReceiver;
    }

    /**
     * Updates rate statistics for the encodings of the tracks that this
     * receiver is managing. Detects simulcast stream suspension/resuming.
     *
     * @param pkt the {@link RawPacket} that is causing the update of this
     * instance.
     *
     * @param encoding the {@link RTPEncodingImpl} of the {@link RawPacket} that
     * is passed as an argument.
     */
    void update(RawPacket pkt, RTPEncodingImpl encoding)
    {
        long nowMs = System.currentTimeMillis();

        // Update the encoding.
        SourceFrameDesc sourceFrameDesc = encoding.update(pkt, nowMs);
        if (sourceFrameDesc == null /* no frame was changed */
            || !sourceFrameDesc.isIndependent() /* frame is dependent */
            || nowMs - lastKeyframeMs < MIN_KEY_FRAME_WAIT_MS)
        {
            return;
        }

        // media engines may decide to suspend a stream for congestion control.
        // This is the case with the webrtc.org simulcast implementation. This
        // behavior induces a streaming dependency between the encodings of a
        // given track. The following piece of code assumes that the subjective
        // quality array is ordered in a way to represent the streaming
        // dependencies.

        lastKeyframeMs = nowMs;<commit_msg>doc: Documents logic behind stream suspension detection.<commit_after>/*
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jitsi.impl.neomedia;

import org.jitsi.service.neomedia.*;

/**
 * An implementation of a {@link MediaStreamTrack} that provides webrtc
 * simulcast stream suspension detection.
 *
 * @author <NAME>
 */
public class MediaStreamTrackImpl
    implements MediaStreamTrack
{
    /**
     * The minimum time (in millis) that is required for the media engine to
     * generate a new key frame.
     */
    private static final int MIN_KEY_FRAME_WAIT_MS = 300;

    /**
     * The {@link RTPEncodingImpl}s that this {@link MediaStreamTrackImpl}
     * possesses, ordered by their subjective quality from low to high.
     */
    private final RTPEncodingImpl[] rtpEncodings;

    /**
     * The {@link MediaStreamTrackReceiver} that receives this instance.
     */
    private final MediaStreamTrackReceiver mediaStreamTrackReceiver;

    /**
     * The time (in millis) that this instance last saw a keyframe.
     */
    private long lastKeyframeMs;

    /**
     * Ctor.
     *
     * @param mediaStreamTrackReceiver The {@link MediaStreamTrackReceiver} that
     * receives this instance.
     * @param rtpEncodings The {@link RTPEncoding}s that this instance
     * possesses.
     */
    public MediaStreamTrackImpl(
        MediaStreamTrackReceiver mediaStreamTrackReceiver,
        RTPEncodingImpl[] rtpEncodings)
    {
        this.rtpEncodings = rtpEncodings;
        this.mediaStreamTrackReceiver = mediaStreamTrackReceiver;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public RTPEncodingImpl[] getRTPEncodings()
    {
        return rtpEncodings;
    }

    /**
     * Gets the {@link MediaStreamTrackReceiver} that receives this instance.
     *
     * @return The {@link MediaStreamTrackReceiver} that receives this instance.
     */
    MediaStreamTrackReceiver getMediaStreamTrackReceiver()
    {
        return mediaStreamTrackReceiver;
    }

    /**
     * Updates rate statistics for the encodings of the tracks that this
     * receiver is managing. Detects simulcast stream suspension/resuming.
     *
     * @param pkt the {@link RawPacket} that is causing the update of this
     * instance.
     *
     * @param encoding the {@link RTPEncodingImpl} of the {@link RawPacket} that
     * is passed as an argument.
     */
    void update(RawPacket pkt, RTPEncodingImpl encoding)
    {
        long nowMs = System.currentTimeMillis();

        // Update the encoding.
        SourceFrameDesc sourceFrameDesc = encoding.update(pkt, nowMs);
        if (sourceFrameDesc == null /* no frame was changed */
            || !sourceFrameDesc.isIndependent() /* frame is dependent */

            // The webrtc engine is sending keyframes from high to low and less
            // often than 300 millis. The first keyframe that we observe after
            // we've waited for that long determines the streams that are
            // streaming (not suspended).
            || nowMs - lastKeyframeMs < MIN_KEY_FRAME_WAIT_MS)
        {
            return;
        }

        // media engines may decide to suspend a stream for congestion control.
        // This is the case with the webrtc.org simulcast implementation. This
        // behavior induces a streaming dependency between the encodings of a
        // given track. The following piece of code assumes that the subjective
        // quality array is ordered in a way to represent the streaming
        // dependencies.

        lastKeyframeMs = nowMs;
{'fraction_non_alphanumeric': 0.07504451793436785, 'fraction_numerical': 0.003179852454846095, 'mean_word_length': 2.658911121451838, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '672545', 'n_tokens_mistral': 2071, 'n_tokens_neox': 2035, 'n_words': 767}