All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jitsi.service.neomedia.AbstractMediaStream Maven / Gradle / Ivy

/*
 * 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.service.neomedia;

import java.beans.*;
import java.util.*;

import org.jitsi.impl.neomedia.codec.*;
import org.jitsi.impl.neomedia.rtp.*;
import org.jitsi.impl.neomedia.transform.*;
import org.jitsi.service.neomedia.format.*;
import org.jitsi.utils.*;

/**
 * Abstract base implementation of MediaStream to ease the
 * implementation of the interface.
 *
 * @author Lyubomir Marinov
 * @author George Politis
 */
public abstract class AbstractMediaStream
    implements MediaStream
{
    /**
     * The name of this stream, that some protocols may use for diagnostic
     * purposes.
     */
    private String name;

    /**
     * The opaque properties of this MediaStream.
     */
    private final Map properties
        = Collections.synchronizedMap(new HashMap());

    /**
     * The delegate of this instance which implements support for property
     * change notifications for its
     * {@link #addPropertyChangeListener(PropertyChangeListener)} and
     * {@link #removePropertyChangeListener(PropertyChangeListener)}.
     */
    private final PropertyChangeSupport propertyChangeSupport
        = new PropertyChangeSupport(this);

    /**
     * The RTPTranslator, if any, which forwards RTP and RTCP traffic
     * between this and other MediaStreams.
     */
    protected RTPTranslator rtpTranslator;

    /**
     * Adds a PropertyChangelistener to this stream which is to be
     * notified upon property changes such as a SSRC ID which becomes known.
     *
     * @param listener the PropertyChangeListener to register for
     * PropertyChangeEvents
     * @see MediaStream#addPropertyChangeListener(PropertyChangeListener)
     */
    @Override
    public void addPropertyChangeListener(PropertyChangeListener listener)
    {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    /**
     * Asserts that the state of this instance will remain consistent if a
     * specific MediaDirection (i.e. direction) and a
     * MediaDevice with a specific MediaDirection (i.e.
     * deviceDirection) are both set on this instance.
     *
     * @param direction the MediaDirection to validate against the
     * specified deviceDirection
     * @param deviceDirection the MediaDirection of a
     * MediaDevice to validate against the specified direction
     * @param illegalArgumentExceptionMessage the message of the
     * IllegalArgumentException to be thrown if the state of this
     * instance would've been compromised if direction and the
     * MediaDevice associated with deviceDirection were both
     * set on this instance
     * @throws IllegalArgumentException if the state of this instance would've
     * been compromised were both direction and the
     * MediaDevice associated with deviceDirection set on this
     * instance
     */
    protected void assertDirection(
            MediaDirection direction,
            MediaDirection deviceDirection,
            String illegalArgumentExceptionMessage)
        throws IllegalArgumentException
    {
        if ((direction != null)
                && !direction.and(deviceDirection).equals(direction))
            throw new IllegalArgumentException(illegalArgumentExceptionMessage);
    }

    /**
     * Fires a new PropertyChangeEvent to the
     * PropertyChangeListeners registered with this instance in order
     * to notify about a change in the value of a specific property which had
     * its old value modified to a specific new value.
     *
     * @param property the name of the property of this instance which had its
     * value changed
     * @param oldValue the value of the property with the specified name before
     * the change
     * @param newValue the value of the property with the specified name after
     * the change
     */
    protected void firePropertyChange(
        String property,
        Object oldValue,
        Object newValue)
    {
        propertyChangeSupport.firePropertyChange(property, oldValue, newValue);
    }

    /**
     * Returns the name of this stream or null if no name has been
     * set. A stream name is used by some protocols, for diagnostic purposes
     * mostly. In XMPP for example this is the name of the content element that
     * describes a stream.
     *
     * @return the name of this stream or null if no name has been
     * set.
     */
    @Override
    public String getName()
    {
        return name;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object getProperty(String propertyName)
    {
        return properties.get(propertyName);
    }

    /**
     * Handles attributes contained in MediaFormat.
     *
     * @param format the MediaFormat to handle the attributes of
     * @param attrs the attributes Map to handle
     */
    protected void handleAttributes(
            MediaFormat format,
            Map attrs)
    {
    }

    /**
     * Sends a given RTP or RTCP packet to the remote peer/side.
     *
     * @param pkt the packet to send.
     * @param data {@code true} to send an RTP packet or {@code false} to send
     * an RTCP packet.
     * @throws TransmissionFailedException if the transmission failed.
     */
    public void injectPacket(RawPacket pkt, boolean data)
        throws TransmissionFailedException
    {
        injectPacket(pkt, data, /* after */ null);
    }

    /**
     * Removes the specified PropertyChangeListener from this stream so
     * that it won't receive further property change events.
     *
     * @param listener the PropertyChangeListener to remove
     * @see MediaStream#removePropertyChangeListener(PropertyChangeListener)
     */
    @Override
    public void removePropertyChangeListener(PropertyChangeListener listener)
    {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }

    /**
     * Sets the name of this stream. Stream names are used by some protocols,
     * for diagnostic purposes mostly. In XMPP for example this is the name of
     * the content element that describes a stream.
     *
     * @param name the name of this stream or null if no name has been
     * set.
     */
    @Override
    public void setName(String name)
    {
        this.name = name;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setProperty(String propertyName, Object value)
    {
        if (value == null)
            properties.remove(propertyName);
        else
            properties.put(propertyName, value);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setRTPTranslator(RTPTranslator rtpTranslator)
    {
        if (this.rtpTranslator != rtpTranslator)
            this.rtpTranslator = rtpTranslator;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public RTPTranslator getRTPTranslator()
    {
        return rtpTranslator;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public TransformEngineChain getTransformEngineChain()
    {
        return null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public byte getDynamicRTPPayloadType(String codec)
    {
        return -1;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public MediaStreamTrackReceiver getMediaStreamTrackReceiver()
    {
        return null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public MediaFormat getFormat(byte pt)
    {
        return null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setTransportCCEngine(TransportCCEngine engine)
    {
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public REDBlock getPrimaryREDBlock(ByteArrayBuffer baf)
    {
        return null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public REDBlock getPrimaryREDBlock(RawPacket pkt)
    {
        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy