-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #674 from RestComm/github-107
Support for OPUS Codec. Close #107
- Loading branch information
Showing
25 changed files
with
930 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<packaging>jar</packaging> | ||
|
||
<parent> | ||
<groupId>org.restcomm.media.codecs</groupId> | ||
<artifactId>opus</artifactId> | ||
<version>7.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<groupId>org.restcomm.media.codecs.opus</groupId> | ||
<artifactId>opus-java</artifactId> | ||
<name>Opus Java</name> | ||
|
||
<profiles> | ||
<profile> | ||
<id>linux-profile</id> | ||
<activation> | ||
<os> | ||
<name>Linux</name> | ||
<family>unix</family> | ||
</os> | ||
</activation> | ||
<properties> | ||
<libopus.artifactId>libopus_jni_linux</libopus.artifactId> | ||
<libopus.packaging>so</libopus.packaging> | ||
<libopus.distro>linux</libopus.distro> | ||
<libopus.libName>opus_jni_linux</libopus.libName> | ||
</properties> | ||
</profile> | ||
|
||
<profile> | ||
<id>macosx-profile</id> | ||
<activation> | ||
<os> | ||
<family>mac</family> | ||
</os> | ||
</activation> | ||
<properties> | ||
<libopus.artifactId>libopus_jni_macos</libopus.artifactId> | ||
<libopus.packaging>dylib</libopus.packaging> | ||
<libopus.distro>macosx</libopus.distro> | ||
<libopus.libName>opus_jni_macos</libopus.libName> | ||
</properties> | ||
</profile> | ||
</profiles> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.restcomm.media</groupId> | ||
<artifactId>spi</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.restcomm.media.codecs.opus</groupId> | ||
<artifactId>${libopus.artifactId}</artifactId> | ||
<version>${project.version}</version> | ||
<type>${libopus.packaging}</type> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<configuration> | ||
<argLine>-Djava.library.path=../opus-native/${libopus.distro}/target -Drestcomm.opus.library=${libopus.libName}</argLine> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>1.7</source> | ||
<target>1.7</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
<finalName>restcomm-mediaserver-codecs-opus-${project.version}</finalName> | ||
</build> | ||
|
||
</project> |
91 changes: 91 additions & 0 deletions
91
codecs/opus/opus-java/src/main/java/org/restcomm/media/codec/opus/Decoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* TeleStax, Open Source Cloud Communications | ||
* Copyright 2011-2017, Telestax Inc and individual contributors | ||
* by the @authors tag. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
|
||
package org.restcomm.media.codec.opus; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.ByteOrder; | ||
|
||
import org.apache.log4j.Logger; | ||
import org.restcomm.media.spi.dsp.Codec; | ||
import org.restcomm.media.spi.format.Format; | ||
import org.restcomm.media.spi.format.FormatFactory; | ||
import org.restcomm.media.spi.memory.Frame; | ||
import org.restcomm.media.spi.memory.Memory; | ||
|
||
/** | ||
* Implements Opus decoder. | ||
* | ||
* @author Vladimir Morosev (vladimir.morosev@telestax.com) | ||
* | ||
*/ | ||
public class Decoder implements Codec { | ||
|
||
private final static Logger log = Logger.getLogger(Encoder.class); | ||
|
||
private final static Format opus = FormatFactory.createAudioFormat("opus", 48000, 8, 2); | ||
private final static Format linear = FormatFactory.createAudioFormat("linear", 8000, 16, 1); | ||
|
||
private long decoderAddress; | ||
|
||
private final int OPUS_SAMPLE_RATE = 8000; | ||
|
||
public Decoder() { | ||
decoderAddress = OpusJni.createDecoderNative(OPUS_SAMPLE_RATE, 1); | ||
} | ||
|
||
@Override | ||
protected void finalize() throws Throwable { | ||
if (decoderAddress != 0) OpusJni.releaseDecoderNative(decoderAddress); | ||
super.finalize(); | ||
} | ||
|
||
@Override | ||
public Format getSupportedInputFormat() { | ||
return opus; | ||
} | ||
|
||
@Override | ||
public Format getSupportedOutputFormat() { | ||
return linear; | ||
} | ||
|
||
@Override | ||
public Frame process(Frame frame) { | ||
|
||
short[] decodedData = OpusJni.decodeNative(decoderAddress, frame.getData()); | ||
byte[] output = new byte[2 * decodedData.length]; | ||
ByteBuffer.wrap(output).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(decodedData); | ||
|
||
Frame res = Memory.allocate(output.length); | ||
System.arraycopy(output, 0, res.getData(), 0, output.length); | ||
|
||
res.setOffset(0); | ||
res.setLength(output.length); | ||
res.setTimestamp(frame.getTimestamp()); | ||
res.setDuration(frame.getDuration()); | ||
res.setSequenceNumber(frame.getSequenceNumber()); | ||
res.setEOM(frame.isEOM()); | ||
res.setFormat(linear); | ||
res.setHeader(frame.getHeader()); | ||
return res; | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
codecs/opus/opus-java/src/main/java/org/restcomm/media/codec/opus/Encoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* TeleStax, Open Source Cloud Communications | ||
* Copyright 2011-2017, Telestax Inc and individual contributors | ||
* by the @authors tag. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
|
||
package org.restcomm.media.codec.opus; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.ByteOrder; | ||
|
||
import org.apache.log4j.Logger; | ||
import org.restcomm.media.spi.dsp.Codec; | ||
import org.restcomm.media.spi.format.Format; | ||
import org.restcomm.media.spi.format.FormatFactory; | ||
import org.restcomm.media.spi.memory.Frame; | ||
import org.restcomm.media.spi.memory.Memory; | ||
|
||
/** | ||
* Implements Opus encoder. | ||
* | ||
* @author Vladimir Morosev (vladimir.morosev@telestax.com) | ||
* | ||
*/ | ||
public class Encoder implements Codec { | ||
|
||
private final static Logger log = Logger.getLogger(Encoder.class); | ||
|
||
private final static Format opus = FormatFactory.createAudioFormat("opus", 48000, 8, 2); | ||
private final static Format linear = FormatFactory.createAudioFormat("linear", 8000, 16, 1); | ||
|
||
private long encoderAddress; | ||
|
||
private final int OPUS_SAMPLE_RATE = 8000; | ||
private final int OPUS_BITRATE = 20000; | ||
|
||
public Encoder() { | ||
encoderAddress = OpusJni.createEncoderNative(OPUS_SAMPLE_RATE, 1, OpusJni.OPUS_APPLICATION_VOIP, OPUS_BITRATE); | ||
} | ||
|
||
@Override | ||
protected void finalize() throws Throwable { | ||
OpusJni.releaseEncoderNative(encoderAddress); | ||
super.finalize(); | ||
} | ||
|
||
@Override | ||
public Format getSupportedInputFormat() { | ||
return linear; | ||
} | ||
|
||
@Override | ||
public Format getSupportedOutputFormat() { | ||
return opus; | ||
} | ||
|
||
@Override | ||
public Frame process(Frame frame) { | ||
|
||
byte[] input = frame.getData(); | ||
short[] inputData = new short[frame.getLength() / 2]; | ||
ByteBuffer.wrap(input).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(inputData); | ||
byte[] encodedData = OpusJni.encodeNative(encoderAddress, inputData); | ||
|
||
Frame res = Memory.allocate(encodedData.length); | ||
System.arraycopy(encodedData, 0, res.getData(), 0, encodedData.length); | ||
|
||
res.setOffset(0); | ||
res.setLength(encodedData.length); | ||
res.setFormat(opus); | ||
res.setTimestamp(frame.getTimestamp()); | ||
res.setDuration(frame.getDuration()); | ||
res.setEOM(frame.isEOM()); | ||
res.setSequenceNumber(frame.getSequenceNumber()); | ||
|
||
return res; | ||
} | ||
} |
Oops, something went wrong.