[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
Wed Sep 21 09:12:30 CDT 2011


branch "master" has been updated
       via  e1cb6f9361e048b2a93abdd58f7050ef4e81a9c2 (commit)
      from  2f1f5f2b6a16000e46a51bc6b83f99e8b2c40dda (commit)

Summary of changes:
 src/resample.cpp |   52 +++++++++++++++++++++++++++++++++++++---------------
 src/resample.h   |    3 +++
 2 files changed, 40 insertions(+), 15 deletions(-)


- Log -----------------------------------------------------------------
commit e1cb6f9361e048b2a93abdd58f7050ef4e81a9c2
Author: Brent Eagles <beagles at digium.com>
Date:   Wed Sep 21 11:39:45 2011 -0230

    Fix a problem where pjX methods were being called from the IceBox start thread
    which does not have the pjthread hook installed. I altered the initialization
    to be "lazy" so the pjlib methods are only called through the Ice communicator
    that has the appropriate hook installed.

diff --git a/src/resample.cpp b/src/resample.cpp
index 572bc1a..5d986d1 100644
--- a/src/resample.cpp
+++ b/src/resample.cpp
@@ -48,24 +48,17 @@ private:
                 const FormatPtr& outputFormat,
                 const Logger& logger,
                 pj_caching_pool *caching_pool)
-            : Translator(source, inputFormat, outputFormat, logger)
+            : Translator(source, inputFormat, outputFormat, logger),
+              mParentPool(caching_pool),
+              mPool(0),
+              mResample(0)
         {
-            mPool = pj_pool_create(&caching_pool->factory, "Who cares", 256, 256, NULL);
-
             AudioFormatPtr inputAudio = AudioFormatPtr::dynamicCast(inputFormat);
             AudioFormatPtr outputAudio = AudioFormatPtr::dynamicCast(outputFormat);
 
             mOutputFrameSize = (outputAudio->sampleRate * inputAudio->frameSize) / inputAudio->sampleRate;
             mLogger(Debug) << "Output frames for resampler will contain " << mOutputFrameSize << " samples";
 
-            pjmedia_resample_create(mPool,
-                    true,
-                    true,
-                    1,
-                    inputAudio->sampleRate,
-                    outputAudio->sampleRate,
-                    inputAudio->frameSize,
-                    &mResample);
         }
 
         ~Resampler()
@@ -75,6 +68,23 @@ private:
 
         FramePtr translate(const FramePtr inFrame)
         {
+            //
+            // Lazy initialization to avoid issues with when the pjlib thread hook is installed.
+            //
+            if (!mPool)
+            {
+                mPool = pj_pool_create(&mParentPool->factory, "Who cares", 256, 256, NULL);
+                AudioFormatPtr inputAudio = AudioFormatPtr::dynamicCast(mInputFormat);
+                AudioFormatPtr outputAudio = AudioFormatPtr::dynamicCast(mOutputFormat);
+                pjmedia_resample_create(mPool,
+                        true,
+                        true,
+                        1,
+                        inputAudio->sampleRate,
+                        outputAudio->sampleRate,
+                        inputAudio->frameSize,
+                        &mResample);
+            }
             if (!TranslatorOperationFactory::formatsEqual(inFrame->mediaFormat, mInputFormat))
             {
                 mLogger(Error) << "Cannot resample frame because the format is not what was expected";
@@ -106,6 +116,7 @@ private:
             return new Frame(outFormat, outPayload);
         }
     private:
+        pj_caching_pool* mParentPool;
         pj_pool_t *mPool;
         pjmedia_resample *mResample;
         int mOutputFrameSize;
@@ -137,12 +148,10 @@ typedef IceUtil::Handle<ResampleOperation> ResampleOperationPtr;
 ResampleFactory::ResampleFactory(const Ice::ObjectAdapterPtr& adapter,
         const AsteriskSCF::System::Logging::Logger& logger,
         const MediaOperationReplicationContextPtr& replicationContext)
-    : TranslatorOperationFactory(adapter, logger, replicationContext, "ResampleFactory")
+    : TranslatorOperationFactory(adapter, logger, replicationContext, "ResampleFactory"),
+      mInitialized(false)
 {
     buildTranslations();
-
-    pj_memset(&mCachingPool, 0, sizeof(mCachingPool));
-    pj_caching_pool_init(&mCachingPool, NULL, 1024 * 1024);
 }
 
 MediaOperationPrx ResampleFactory::createMediaOperation(
@@ -150,6 +159,19 @@ MediaOperationPrx ResampleFactory::createMediaOperation(
         const FormatPtr& sinkFormat,
         const std::string& operationId)
 {
+    {
+        //
+        // Lazy initialization to avoid calling pjlib functions in a thread before the
+        // required hooks can be called.
+        //
+        boost::mutex::scoped_lock lock(mLock);
+        if (!mInitialized)
+        {
+            pj_memset(&mCachingPool, 0, sizeof(mCachingPool));
+            pj_caching_pool_init(&mCachingPool, NULL, 1024 * 1024);
+            mInitialized = true;
+        }
+    }
     ResampleOperationPtr operation(
             new ResampleOperation(
                 mAdapter,
diff --git a/src/resample.h b/src/resample.h
index b3ae897..11719c0 100644
--- a/src/resample.h
+++ b/src/resample.h
@@ -19,6 +19,7 @@
 #include <pjmedia.h>
 
 #include "TranslatorOperationFactory.h"
+#include <boost/thread/locks.hpp>
 
 namespace AsteriskSCF
 {
@@ -40,6 +41,8 @@ public:
 private:
     void buildTranslations();
 
+    boost::mutex mLock;
+    bool mInitialized;
     pj_caching_pool mCachingPool;
 };
 

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


-- 
asterisk-scf/release/media_operations_core.git



More information about the asterisk-scf-commits mailing list