[asterisk-scf-commits] asterisk-scf/release/media_operations_core.git branch "master" updated.

Commits to the Asterisk SCF project code repositories asterisk-scf-commits at lists.digium.com
Thu Oct 6 16:30:31 CDT 2011


branch "master" has been updated
       via  938898df0a58862bcfaad66c6dea04f797ba8538 (commit)
      from  f652a95e1599540c3eeabc71046a2df1e597d598 (commit)

Summary of changes:
 src/ulaw_alaw.cpp            |   95 ++++++++++++++--
 test/TestMediaOperations.cpp |  244 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 327 insertions(+), 12 deletions(-)


- Log -----------------------------------------------------------------
commit 938898df0a58862bcfaad66c6dea04f797ba8538
Author: Mark Michelson <mmichelson at digium.com>
Date:   Thu Oct 6 16:30:06 2011 -0500

    Make it possible for ulaw and alaw to be translated to slin8 and vice-versa.

diff --git a/src/ulaw_alaw.cpp b/src/ulaw_alaw.cpp
index 2d3e2a9..b3f0722 100644
--- a/src/ulaw_alaw.cpp
+++ b/src/ulaw_alaw.cpp
@@ -19,6 +19,8 @@
 #include <IceUtil/UUID.h>
 #include <pjmedia.h>
 
+#include <boost/bind.hpp>
+
 #include "ulaw_alaw.h"
 #include "TranslatorSource.h"
 #include "Translator.h"
@@ -60,6 +62,26 @@ private:
         {
             return pjmedia_alaw2ulaw(alaw);
         }
+
+        short ulaw2slin(unsigned char ulaw)
+        {
+            return (short) pjmedia_ulaw2linear(ulaw);
+        }
+
+        short alaw2slin(unsigned char alaw)
+        {
+            return (short) pjmedia_alaw2linear(alaw);
+        }
+
+        unsigned char slin2ulaw(short slin)
+        {
+            return pjmedia_linear2ulaw((int) slin);
+        }
+
+        unsigned char slin2alaw(short slin)
+        {
+            return pjmedia_linear2alaw((int) slin);
+        }
         
         /**
          * Override of Translator's translate function.
@@ -72,26 +94,68 @@ private:
                 throw UnsupportedMediaFormatException();
             }
         
-            FramePtr outFrame;
-            Ice::ByteSeq outPayload;
+            FramePtr outFrame = 0;
+            Ice::ByteSeq outPayload8;
+            Ice::ShortSeq outPayload16;
         
-            ByteSeqPayloadPtr inPayload = ByteSeqPayloadPtr::dynamicCast(inFrame->payload);
-            if (!inPayload)
+            if (inFrame->mediaFormat->name == G711uLAWName)
             {
-                mLogger(Error) << "The incoming frame's payload is not a byte sequence.";
-                throw UnsupportedMediaFormatException();
+                ByteSeqPayloadPtr inPayload = ByteSeqPayloadPtr::dynamicCast(inFrame->payload);
+                if (!inPayload)
+                {
+                    mLogger(Error) << "The incoming frame's payload is not a byte sequence.";
+                    throw UnsupportedMediaFormatException();
+                }
+                if (mOutputFormat->name == G711aLAWName)
+                {
+                    std::transform(inPayload->payload.begin(), inPayload->payload.end(), std::back_inserter(outPayload8), boost::bind(&UlawAlawTranslator::ulaw2alaw, this, _1));
+                    outFrame = new Frame(mOutputFormat, new ByteSeqPayload(outPayload8));
+                }
+                else
+                {
+                    std::transform(inPayload->payload.begin(), inPayload->payload.end(), std::back_inserter(outPayload16), boost::bind(&UlawAlawTranslator::ulaw2slin, this, _1));
+                    outFrame = new Frame(mOutputFormat, new ShortSeqPayload(outPayload16));
+                }
             }
-
-            if (inFrame->mediaFormat->name == G711uLAWName)
+            else if (inFrame->mediaFormat->name == G711aLAWName)
             {
-                std::transform(inPayload->payload.begin(), inPayload->payload.end(), std::back_inserter(outPayload), std::bind1st(std::mem_fun(&UlawAlawTranslator::ulaw2alaw), this));
+                ByteSeqPayloadPtr inPayload = ByteSeqPayloadPtr::dynamicCast(inFrame->payload);
+                if (!inPayload)
+                {
+                    mLogger(Error) << "The incoming frame's payload is not a byte sequence.";
+                    throw UnsupportedMediaFormatException();
+                }
+                if (mOutputFormat->name == G711uLAWName)
+                {
+                    std::transform(inPayload->payload.begin(), inPayload->payload.end(), std::back_inserter(outPayload8), boost::bind(&UlawAlawTranslator::alaw2ulaw, this, _1));
+                    outFrame = new Frame(mOutputFormat, new ByteSeqPayload(outPayload8));
+                }
+                else
+                {
+                    std::transform(inPayload->payload.begin(), inPayload->payload.end(), std::back_inserter(outPayload16), boost::bind(&UlawAlawTranslator::alaw2slin, this, _1));
+                    outFrame = new Frame(mOutputFormat, new ShortSeqPayload(outPayload16));
+                }
             }
-            else
+            else if (inFrame->mediaFormat->name == SignedLinear8Name)
             {
-                std::transform(inPayload->payload.begin(), inPayload->payload.end(), std::back_inserter(outPayload), std::bind1st(std::mem_fun(&UlawAlawTranslator::alaw2ulaw), this));
+                ShortSeqPayloadPtr inPayload = ShortSeqPayloadPtr::dynamicCast(inFrame->payload);
+                if (!inPayload)
+                {
+                    mLogger(Error) << "The incoming frame's payload is not a short sequence.";
+                    throw UnsupportedMediaFormatException();
+                }
+                if (mOutputFormat->name == G711uLAWName)
+                {
+                    std::transform(inPayload->payload.begin(), inPayload->payload.end(), std::back_inserter(outPayload8), boost::bind(&UlawAlawTranslator::slin2ulaw, this, _1));
+                    outFrame = new Frame(mOutputFormat, new ByteSeqPayload(outPayload8));
+                }
+                else
+                {
+                    std::transform(inPayload->payload.begin(), inPayload->payload.end(), std::back_inserter(outPayload8), boost::bind(&UlawAlawTranslator::slin2alaw, this, _1));
+                    outFrame = new Frame(mOutputFormat, new ByteSeqPayload(outPayload8));
+                }
             }
         
-            outFrame = new Frame(mOutputFormat, new ByteSeqPayload(outPayload));
             return outFrame;
         }
     };
@@ -134,8 +198,15 @@ void UlawAlawFactory::buildTranslations()
     FormatPtr alaw = new G711aLAW();
     alaw->name = G711aLAWName;
 
+    FormatPtr slin8 = new SignedLinear();
+    slin8->name = SignedLinear8Name;
+
     addTranslation(ulaw, alaw, 100);
     addTranslation(alaw, ulaw, 100);
+    addTranslation(ulaw, slin8, 100);
+    addTranslation(alaw, slin8, 100);
+    addTranslation(slin8, ulaw, 100);
+    addTranslation(slin8, alaw, 100);
 }
 
 MediaOperationPrx UlawAlawFactory::createMediaOperation(
diff --git a/test/TestMediaOperations.cpp b/test/TestMediaOperations.cpp
index 7d3d79a..a5a41fc 100644
--- a/test/TestMediaOperations.cpp
+++ b/test/TestMediaOperations.cpp
@@ -545,6 +545,128 @@ BOOST_FIXTURE_TEST_CASE(translateAlawToUlaw, PerTestFixture)
     alaw2ulawTranslator->destroy();
 }
 
+BOOST_FIXTURE_TEST_CASE(translateAlawToSlin8, PerTestFixture)
+{
+    size_t numFramesToTranslate = 10;
+    MediaOperationServiceLocatorParamsPtr alaw2slin8Params = createLocatorParams(Testbed.alaw, Testbed.slin8);
+
+    MediaOperationFactoryPrx alaw2slin8Factory =
+        MediaOperationFactoryPrx::checkedCast(Testbed.locator->locate(alaw2slin8Params));
+
+    MediaOperationPrx alaw2slin8Translator =
+        alaw2slin8Factory->createMediaOperation(Testbed.alawSourceProxy, Testbed.slin8SinkProxy);
+
+    StreamSinkPrx translateSink = alaw2slin8Translator->getSink();
+    StreamSourcePrx translateSource = alaw2slin8Translator->getSource();
+
+    Testbed.alawSourceProxy->addSink(translateSink);
+    Testbed.slin8SinkProxy->setSource(translateSource);
+
+    translateSource->addSink(Testbed.slin8SinkProxy);
+    translateSink->setSource(Testbed.alawSourceProxy);
+
+    //We'll feed a few of the sample frames in.
+    FrameSeq alawFrames(numFramesToTranslate, Testbed.sampleAlawFrame);
+
+    try
+    {
+        Testbed.alawSource->feedFramesToTranslator(alawFrames);
+    }
+    catch (const Ice::Exception& ex)
+    {
+        std::stringstream str;
+        str << "Exception caught trying to translate alaw frames: " << ex.what();
+        BOOST_FAIL(str.str());
+    }
+    
+    //Since everything is synchronous, the Testbed's alawSink should
+    //have frames in it now. We need to make sure that it got
+    //the right number of frames and that the payloads are the
+    //proper size...we can't reliably check for accuracy though.
+    FrameSeq framesWritten = Testbed.slin8Sink->getFrames();
+
+    //There should be an equal number of frames written as was read
+    BOOST_CHECK(framesWritten.size() == numFramesToTranslate);
+
+    for (FrameSeq::iterator iter = framesWritten.begin();
+            iter != framesWritten.end(); ++iter)
+    {
+        //Each frame written by the translator should be alaw
+        BOOST_CHECK(SignedLinearPtr::dynamicCast((*iter)->mediaFormat) != 0);
+
+        ShortSeqPayloadPtr payload = ShortSeqPayloadPtr::dynamicCast((*iter)->payload);
+
+        BOOST_REQUIRE(payload != 0);
+
+        //Each frame written by the translator should be the
+        //same size as the frame read in.
+        BOOST_CHECK(payload->payload.size() == Testbed.sampleAlawFrameSize);
+    }
+
+    alaw2slin8Translator->destroy();
+}
+
+BOOST_FIXTURE_TEST_CASE(translateSlin8ToAlaw, PerTestFixture)
+{
+    size_t numFramesToTranslate = 10;
+    MediaOperationServiceLocatorParamsPtr slin82AlawParams = createLocatorParams(Testbed.slin8, Testbed.alaw);
+
+    MediaOperationFactoryPrx slin82AlawFactory =
+        MediaOperationFactoryPrx::checkedCast(Testbed.locator->locate(slin82AlawParams));
+
+    MediaOperationPrx slin82AlawTranslator =
+        slin82AlawFactory->createMediaOperation(Testbed.slin8SourceProxy, Testbed.alawSinkProxy);
+
+    StreamSinkPrx translateSink = slin82AlawTranslator->getSink();
+    StreamSourcePrx translateSource = slin82AlawTranslator->getSource();
+
+    Testbed.slin8SourceProxy->addSink(translateSink);
+    Testbed.alawSinkProxy->setSource(translateSource);
+
+    translateSource->addSink(Testbed.alawSinkProxy);
+    translateSink->setSource(Testbed.slin8SourceProxy);
+
+    //We'll feed a few of the sample frames in.
+    FrameSeq slin8Frames(numFramesToTranslate, Testbed.sampleSlin8Frame);
+
+    try
+    {
+        Testbed.slin8Source->feedFramesToTranslator(slin8Frames);
+    }
+    catch (const Ice::Exception& ex)
+    {
+        std::stringstream str;
+        str << "Exception caught trying to translate slin8 frames: " << ex.what();
+        BOOST_FAIL(str.str());
+    }
+    
+    //Since everything is synchronous, the Testbed's alawSink should
+    //have frames in it now. We need to make sure that it got
+    //the right number of frames and that the payloads are the
+    //proper size...we can't reliably check for accuracy though.
+    FrameSeq framesWritten = Testbed.alawSink->getFrames();
+
+    //There should be an equal number of frames written as was read
+    BOOST_CHECK(framesWritten.size() == numFramesToTranslate);
+
+    for (FrameSeq::iterator iter = framesWritten.begin();
+            iter != framesWritten.end(); ++iter)
+    {
+        //Each frame written by the translator should be alaw
+        BOOST_CHECK(G711aLAWPtr::dynamicCast((*iter)->mediaFormat) != 0);
+
+        ByteSeqPayloadPtr payload = ByteSeqPayloadPtr::dynamicCast((*iter)->payload);
+
+        BOOST_REQUIRE(payload != 0);
+
+        //Each frame written by the translator should be the
+        //same size as the frame read in.
+        BOOST_CHECK(payload->payload.size() == Testbed.sampleSlin8FrameSize);
+    }
+
+    slin82AlawTranslator->destroy();
+}
+
 BOOST_FIXTURE_TEST_CASE(translateUlawToAlaw, PerTestFixture)
 {
     size_t numFramesToTranslate = 10;
@@ -606,6 +728,128 @@ BOOST_FIXTURE_TEST_CASE(translateUlawToAlaw, PerTestFixture)
     ulaw2alawTranslator->destroy();
 }
 
+BOOST_FIXTURE_TEST_CASE(translateUlawToSlin8, PerTestFixture)
+{
+    size_t numFramesToTranslate = 10;
+    MediaOperationServiceLocatorParamsPtr ulaw2slin8Params = createLocatorParams(Testbed.ulaw, Testbed.slin8);
+
+    MediaOperationFactoryPrx ulaw2slin8Factory =
+        MediaOperationFactoryPrx::checkedCast(Testbed.locator->locate(ulaw2slin8Params));
+
+    MediaOperationPrx ulaw2slin8Translator =
+        ulaw2slin8Factory->createMediaOperation(Testbed.ulawSourceProxy, Testbed.slin8SinkProxy);
+
+    StreamSinkPrx translateSink = ulaw2slin8Translator->getSink();
+    StreamSourcePrx translateSource = ulaw2slin8Translator->getSource();
+
+    Testbed.ulawSourceProxy->addSink(translateSink);
+    Testbed.slin8SinkProxy->setSource(translateSource);
+
+    translateSource->addSink(Testbed.slin8SinkProxy);
+    translateSink->setSource(Testbed.ulawSourceProxy);
+
+    //We'll feed a few of the sample frames in.
+    FrameSeq ulawFrames(numFramesToTranslate, Testbed.sampleUlawFrame);
+
+    try
+    {
+        Testbed.ulawSource->feedFramesToTranslator(ulawFrames);
+    }
+    catch (const Ice::Exception& ex)
+    {
+        std::stringstream str;
+        str << "Exception caught trying to translate ulaw frames: " << ex.what();
+        BOOST_FAIL(str.str());
+    }
+    
+    //Since everything is synchronous, the Testbed's ulawSink should
+    //have frames in it now. We need to make sure that it got
+    //the right number of frames and that the payloads are the
+    //proper size...we can't reliably check for accuracy though.
+    FrameSeq framesWritten = Testbed.slin8Sink->getFrames();
+
+    //There should be an equal number of frames written as was read
+    BOOST_CHECK(framesWritten.size() == numFramesToTranslate);
+
+    for (FrameSeq::iterator iter = framesWritten.begin();
+            iter != framesWritten.end(); ++iter)
+    {
+        //Each frame written by the translator should be ulaw
+        BOOST_CHECK(SignedLinearPtr::dynamicCast((*iter)->mediaFormat) != 0);
+
+        ShortSeqPayloadPtr payload = ShortSeqPayloadPtr::dynamicCast((*iter)->payload);
+
+        BOOST_REQUIRE(payload != 0);
+
+        //Each frame written by the translator should be the
+        //same size as the frame read in.
+        BOOST_CHECK(payload->payload.size() == Testbed.sampleUlawFrameSize);
+    }
+
+    ulaw2slin8Translator->destroy();
+}
+
+BOOST_FIXTURE_TEST_CASE(translateSlin8ToUlaw, PerTestFixture)
+{
+    size_t numFramesToTranslate = 10;
+    MediaOperationServiceLocatorParamsPtr slin82UlawParams = createLocatorParams(Testbed.slin8, Testbed.ulaw);
+
+    MediaOperationFactoryPrx slin82UlawFactory =
+        MediaOperationFactoryPrx::checkedCast(Testbed.locator->locate(slin82UlawParams));
+
+    MediaOperationPrx slin82UlawTranslator =
+        slin82UlawFactory->createMediaOperation(Testbed.slin8SourceProxy, Testbed.ulawSinkProxy);
+
+    StreamSinkPrx translateSink = slin82UlawTranslator->getSink();
+    StreamSourcePrx translateSource = slin82UlawTranslator->getSource();
+
+    Testbed.slin8SourceProxy->addSink(translateSink);
+    Testbed.ulawSinkProxy->setSource(translateSource);
+
+    translateSource->addSink(Testbed.ulawSinkProxy);
+    translateSink->setSource(Testbed.slin8SourceProxy);
+
+    //We'll feed a few of the sample frames in.
+    FrameSeq slin8Frames(numFramesToTranslate, Testbed.sampleSlin8Frame);
+
+    try
+    {
+        Testbed.slin8Source->feedFramesToTranslator(slin8Frames);
+    }
+    catch (const Ice::Exception& ex)
+    {
+        std::stringstream str;
+        str << "Exception caught trying to translate slin8 frames: " << ex.what();
+        BOOST_FAIL(str.str());
+    }
+    
+    //Since everything is synchronous, the Testbed's ulawSink should
+    //have frames in it now. We need to make sure that it got
+    //the right number of frames and that the payloads are the
+    //proper size...we can't reliably check for accuracy though.
+    FrameSeq framesWritten = Testbed.ulawSink->getFrames();
+
+    //There should be an equal number of frames written as was read
+    BOOST_CHECK(framesWritten.size() == numFramesToTranslate);
+
+    for (FrameSeq::iterator iter = framesWritten.begin();
+            iter != framesWritten.end(); ++iter)
+    {
+        //Each frame written by the translator should be ulaw
+        BOOST_CHECK(G711uLAWPtr::dynamicCast((*iter)->mediaFormat) != 0);
+
+        ByteSeqPayloadPtr payload = ByteSeqPayloadPtr::dynamicCast((*iter)->payload);
+
+        BOOST_REQUIRE(payload != 0);
+
+        //Each frame written by the translator should be the
+        //same size as the frame read in.
+        BOOST_CHECK(payload->payload.size() == Testbed.sampleSlin8FrameSize);
+    }
+
+    slin82UlawTranslator->destroy();
+}
+
 BOOST_FIXTURE_TEST_CASE(resample8To16, PerTestFixture)
 {
     size_t numFramesToTranslate = 10;

-----------------------------------------------------------------------


-- 
asterisk-scf/release/media_operations_core.git



More information about the asterisk-scf-commits mailing list