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

org.red5.server.net.rtmp.event.AudioData Maven / Gradle / Ivy

There is a newer version: 2.0.12
Show newest version
/*
 * RED5 Open Source Media Server - https://github.com/Red5/ Copyright 2006-2023 by respective authors (see below). All rights reserved. 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.red5.server.net.rtmp.event;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

import org.apache.mina.core.buffer.IoBuffer;
import org.red5.codec.AudioCodec;
import org.red5.codec.AudioPacketType;
import org.red5.io.IoConstants;
import org.red5.server.api.stream.IStreamPacket;
import org.red5.server.stream.IStreamData;

public class AudioData extends BaseEvent implements IStreamData, IStreamPacket {

    private static final long serialVersionUID = -4102940670913999407L;

    protected IoBuffer data;

    /**
     * Data type
     */
    private final byte dataType = TYPE_AUDIO_DATA;

    /**
     * Codec id
     */
    private byte codecId = -1;

    /**
     * Configuration flag
     */
    private boolean config;

    /**
     * Enhanced flag
     */
    private boolean enhanced;

    /**
     * Packet type
     */
    private AudioPacketType packetType;

    /**
     * Audio codec
     */
    //protected transient IAudioStreamCodec codec;

    /** Constructs a new AudioData. */
    public AudioData() {
        this(IoBuffer.allocate(0).flip());
    }

    public AudioData(IoBuffer data) {
        super(Type.STREAM_DATA);
        setData(data);
    }

    /**
     * Create audio data event with given data buffer
     *
     * @param data
     *            Audio data
     * @param copy
     *            true to use a copy of the data or false to use reference
     */
    public AudioData(IoBuffer data, boolean copy) {
        super(Type.STREAM_DATA);
        if (copy) {
            byte[] array = new byte[data.remaining()];
            data.mark();
            data.get(array);
            data.reset();
            setData(array);
        } else {
            setData(data);
        }
    }

    /** {@inheritDoc} */
    @Override
    public byte getDataType() {
        return dataType;
    }

    public IoBuffer getData() {
        return data;
    }

    public void setData(IoBuffer data) {
        // set some properties if we can
        if (codecId == -1 && data.remaining() > 0) {
            data.mark();
            byte flg = data.get();
            codecId = (byte) ((flg & IoConstants.MASK_SOUND_FORMAT) >> 4);
            enhanced = (codecId == AudioCodec.ExHeader.getId());
            if (enhanced) {
                packetType = AudioPacketType.valueOf(flg & 0x0f);
            } else if (codecId == 10 && data.remaining() > 0) {
                flg = data.get();
                packetType = AudioPacketType.valueOf(flg);
            }
            data.reset();
            config = (packetType == AudioPacketType.SequenceStart);
        }
        this.data = data;
    }

    public void setData(byte[] data) {
        // set some properties if we can
        if (codecId == -1 && data.length > 0) {
            codecId = (byte) ((data[0] & IoConstants.MASK_SOUND_FORMAT) >> 4);
            enhanced = (codecId == AudioCodec.ExHeader.getId());
            if (enhanced) {
                packetType = AudioPacketType.valueOf(data[0] & 0x0f);
            } else if (codecId == AudioCodec.AAC.getId() && data.length > 1) {
                packetType = AudioPacketType.valueOf(data[1]);
            }
            config = (packetType == AudioPacketType.SequenceStart);
        }
        setData(IoBuffer.wrap(data));
    }

    public int getCodecId() {
        return codecId;
    }

    public boolean isConfig() {
        return config;
    }

    /**
     * Returns the audio packet type.
     *
     * @return audio packet type
     */
    public AudioPacketType getPacketType() {
        return packetType;
    }

    public boolean isEndOfSequence() {
        return packetType == AudioPacketType.SequenceEnd;
    }

    public boolean isEnhanced() {
        return enhanced;
    }

    public void reset() {
        releaseInternal();
    }

    /** {@inheritDoc} */
    @Override
    protected void releaseInternal() {
        if (data != null) {
            data.free();
            data = null;
        }
        //codec = null;
        codecId = -1;
        config = false;
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        super.readExternal(in);
        byte[] byteBuf = (byte[]) in.readObject();
        if (byteBuf != null) {
            setData(byteBuf);
        }
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        super.writeExternal(out);
        if (data != null) {
            if (data.hasArray()) {
                out.writeObject(data.array());
            } else {
                byte[] array = new byte[data.remaining()];
                data.mark();
                data.get(array);
                data.reset();
                out.writeObject(array);
            }
        } else {
            out.writeObject(null);
        }
    }

    /**
     * Duplicate this message / event.
     *
     * @return duplicated event
     */
    public AudioData duplicate() throws IOException, ClassNotFoundException {
        AudioData result = new AudioData();
        // serialize
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        writeExternal(oos);
        oos.close();
        // convert to byte array
        byte[] buf = baos.toByteArray();
        baos.close();
        // create input streams
        ByteArrayInputStream bais = new ByteArrayInputStream(buf);
        ObjectInputStream ois = new ObjectInputStream(bais);
        // deserialize
        result.readExternal(ois);
        ois.close();
        bais.close();
        // clone the header if there is one
        if (header != null) {
            result.setHeader(header.clone());
        }
        result.setSourceType(sourceType);
        result.setSource(source);
        result.setTimestamp(timestamp);
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public String toString() {
        return String.format("Audio - ts: %s length: %s", getTimestamp(), (data != null ? data.limit() : '0'));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy