|
| 1 | +/* |
| 2 | + * TeleStax, Open Source Cloud Communications |
| 3 | + * Copyright 2011-2017, Telestax Inc and individual contributors |
| 4 | + * by the @authors tag. |
| 5 | + * |
| 6 | + * This is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU Lesser General Public License as |
| 8 | + * published by the Free Software Foundation; either version 2.1 of |
| 9 | + * the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This software is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public |
| 17 | + * License along with this software; if not, write to the Free |
| 18 | + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 19 | + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. |
| 20 | + */ |
| 21 | + |
| 22 | +package org.restcomm.media.codec.opus; |
| 23 | + |
| 24 | +import java.nio.ByteBuffer; |
| 25 | +import java.nio.ByteOrder; |
| 26 | + |
| 27 | +import org.apache.log4j.Logger; |
| 28 | +import org.restcomm.media.spi.dsp.Codec; |
| 29 | +import org.restcomm.media.spi.format.Format; |
| 30 | +import org.restcomm.media.spi.format.FormatFactory; |
| 31 | +import org.restcomm.media.spi.memory.Frame; |
| 32 | +import org.restcomm.media.spi.memory.Memory; |
| 33 | + |
| 34 | +/** |
| 35 | + * Implements Opus encoder. |
| 36 | + * |
| 37 | + * @author Vladimir Morosev (vladimir.morosev@telestax.com) |
| 38 | + * |
| 39 | + */ |
| 40 | +public class Encoder implements Codec { |
| 41 | + |
| 42 | + private final static Logger log = Logger.getLogger(Encoder.class); |
| 43 | + |
| 44 | + private final static Format opus = FormatFactory.createAudioFormat("opus", 48000, 8, 2); |
| 45 | + private final static Format linear = FormatFactory.createAudioFormat("linear", 8000, 16, 1); |
| 46 | + |
| 47 | + private long encoderAddress; |
| 48 | + |
| 49 | + private final int OPUS_SAMPLE_RATE = 8000; |
| 50 | + private final int OPUS_BITRATE = 20000; |
| 51 | + |
| 52 | + public Encoder() { |
| 53 | + encoderAddress = OpusJni.createEncoderNative(OPUS_SAMPLE_RATE, 1, OpusJni.OPUS_APPLICATION_VOIP, OPUS_BITRATE); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected void finalize() throws Throwable { |
| 58 | + OpusJni.releaseEncoderNative(encoderAddress); |
| 59 | + super.finalize(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public Format getSupportedInputFormat() { |
| 64 | + return linear; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Format getSupportedOutputFormat() { |
| 69 | + return opus; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public Frame process(Frame frame) { |
| 74 | + |
| 75 | + byte[] input = frame.getData(); |
| 76 | + short[] inputData = new short[frame.getLength() / 2]; |
| 77 | + ByteBuffer.wrap(input).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(inputData); |
| 78 | + byte[] encodedData = OpusJni.encodeNative(encoderAddress, inputData); |
| 79 | + |
| 80 | + Frame res = Memory.allocate(encodedData.length); |
| 81 | + System.arraycopy(encodedData, 0, res.getData(), 0, encodedData.length); |
| 82 | + |
| 83 | + res.setOffset(0); |
| 84 | + res.setLength(encodedData.length); |
| 85 | + res.setFormat(opus); |
| 86 | + res.setTimestamp(frame.getTimestamp()); |
| 87 | + res.setDuration(frame.getDuration()); |
| 88 | + res.setEOM(frame.isEOM()); |
| 89 | + res.setSequenceNumber(frame.getSequenceNumber()); |
| 90 | + |
| 91 | + return res; |
| 92 | + } |
| 93 | +} |
0 commit comments