]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/device-ti-common-open.git/commitdiff
audio: utils: Add PCM port name
authorMisael Lopez Cruz <misael.lopez@ti.com>
Mon, 20 Jan 2014 19:10:20 +0000 (13:10 -0600)
committerMisael Lopez Cruz <misael.lopez@ti.com>
Wed, 22 Jan 2014 17:18:36 +0000 (11:18 -0600)
Add PCM port name methods in order to provide more context in
info/debug messages and for the PCM reader/writer thread names.

Change-Id: I3609a8c9308b562468655d4f693ab4baccd4873f
Signed-off-by: Misael Lopez Cruz <misael.lopez@ti.com>
audio/utils/include/tiaudioutils/ALSAPcm.h
audio/utils/include/tiaudioutils/Base.h
audio/utils/include/tiaudioutils/NullPcm.h
audio/utils/include/tiaudioutils/Pcm.h
audio/utils/src/ALSAPcm.cpp
audio/utils/src/Base.cpp
audio/utils/src/NullPcm.cpp
audio/utils/src/Stream.cpp

index 030db84a2246282f04eb8562b959ac6eda19be8e..88b2b715237c9767aad63ee8e3a2cead0369904a 100644 (file)
@@ -79,6 +79,15 @@ class ALSAInPort : public PcmInPort {
      */
     uint32_t getPortId() const { return mPortId; }
 
+    /**
+     * \brief Get the port name
+     *
+     * Gets the name of the ALSA capture port: hw:<CARD>,<DEVICE>.
+     *
+     * \return The name of the PCM port
+     */
+    const char* getName() const { return mName.c_str(); }
+
     /**
      * \brief Open the PCM port for capture
      *
@@ -151,6 +160,7 @@ class ALSAInPort : public PcmInPort {
  protected:
     uint32_t mCardId;      /**< Id of the ALSA card */
     uint32_t mPortId;      /**< Id of the ALSA PCM device */
+    string mName;          /**< Name of the ALSA PCM port */
     uint32_t mPeriodCount; /**< Period count of this port */
     struct pcm *mPcm;      /**< tinyalsa pcm handle */
     mutable Mutex mLock;   /**< Synchronize PCM port use */
@@ -202,6 +212,15 @@ class ALSAOutPort : public PcmOutPort {
      */
     uint32_t getPortId() const { return mPortId; }
 
+    /**
+     * \brief Get the port name
+     *
+     * Gets the name of the ALSA playback port: hw:<CARD>,<DEVICE>.
+     *
+     * \return The name of the PCM port
+     */
+    const char* getName() const { return mName.c_str(); }
+
     /**
      * \brief Open the PCM port for playback
      *
@@ -275,6 +294,7 @@ class ALSAOutPort : public PcmOutPort {
  protected:
     uint32_t mCardId;      /**< Id of the ALSA card */
     uint32_t mPortId;      /**< Id of the ALSA PCM device */
+    string mName;          /**< Name of the ALSA PCM port */
     uint32_t mPeriodCount; /**< Period count of this port */
     struct pcm *mPcm;      /**< tinyalsa pcm handle */
     mutable Mutex mLock;   /**< Synchronize PCM port use */
index dc2a6f1eadd5c33bb7ef49a6422a96345294aeb5..720355d53e97d01babe5396b3f8f467c432f413b 100644 (file)
@@ -208,10 +208,20 @@ class ThreadBase {
      *
      * Get the name of the thread.
      *
-     * \return Thread name as a C-style char pointer
+     * \return Thread name as a C-style string
      */
     const char *name() const { return mName.c_str(); }
 
+    /**
+     * \brief Set the thread name
+     *
+     * Set the name of the thread.
+     *
+     * \param newname New name of the thread
+     * \return 0 on success, otherwise negative error code
+     */
+    int setName(string newname);
+
     friend void* threadWrapper(void *me);
 
  private:
@@ -233,6 +243,8 @@ class ThreadBase {
     volatile bool mRunning; /**< Running status of the thread */
     mutable Mutex mMutex;
 
+    static const int mNameMaxLength = 16; /* Max length of the thread name */
+
  protected:
     /**
      * \brief Implementation-specific thread function
index 977faaa3ccd7cccb902315b8cc690f91a3958fc0..0998a72667d6a3a32f4c6209d63db636b5e83379 100644 (file)
@@ -76,6 +76,15 @@ class NullInPort : public PcmInPort {
      */
     uint32_t getPortId() const { return 0; }
 
+    /**
+     * \brief Get the port name
+     *
+     * Gets the name of the null capture port.
+     *
+     * \return The name of the PCM port
+     */
+    const char* getName() const { return mName.c_str(); }
+
     /**
      * \brief Open the null port for capture
      *
@@ -144,6 +153,7 @@ class NullInPort : public PcmInPort {
 
  protected:
     bool mOpen;         /**< State of the port: open or closed */
+    string mName;       /**< Name of the null PCM port */
     PcmParams mParams;  /**< PCM params of the port */
     Mutex mLock;        /**< Synchronize PCM port use */
 };
@@ -194,6 +204,15 @@ class NullOutPort : public PcmOutPort {
      */
     uint32_t getPortId() const { return 0; }
 
+    /**
+     * \brief Get the port name
+     *
+     * Gets the name of the null playback port.
+     *
+     * \return The name of the PCM port
+     */
+    const char* getName() const { return mName.c_str(); }
+
     /**
      * \brief Open the null port for playback
      *
@@ -261,6 +280,7 @@ class NullOutPort : public PcmOutPort {
 
  protected:
     bool mOpen;         /**< State of the port: open or closed */
+    string mName;       /**< Name of the null PCM port */
     PcmParams mParams;  /**< PCM params of the port */
     Mutex mLock;        /**< Synchronize PCM port use */
 };
index 7d3ecd8aaa5139f6b4e2b943cf3b9a25870d1cc9..0bc50d2341799ad78c29d3ea97e986f4f0bc6e9a 100644 (file)
@@ -287,6 +287,15 @@ class PcmPort {
      */
     virtual uint32_t getPortId() const = 0;
 
+    /**
+     * \brief Get the port name
+     *
+     * Get the name of the PCM port.
+     *
+     * \return The name of the PCM port
+     */
+    virtual const char* getName() const = 0;
+
     /**
      * \brief Open the PCM port
      *
index cea2199f0e7a8f0f612f11ba914b3f0d6c234716..adbed0353d0bf2a872b0ee67ba17685e0789da69 100644 (file)
@@ -17,6 +17,7 @@
 // #define LOG_NDEBUG 0
 // #define VERY_VERBOSE_LOGGING
 
+#include <sstream>
 #include <errno.h>
 #include <tinyalsa/asoundlib.h>
 
@@ -28,6 +29,9 @@ namespace tiaudioutils {
 ALSAInPort::ALSAInPort(uint32_t card, uint32_t port, uint32_t period_count)
     : mCardId(card), mPortId(port), mPeriodCount(period_count), mPcm(NULL)
 {
+    std::stringstream s;
+    s << "hw:" << mCardId << "," << mPortId;
+    mName = s.str();
 }
 
 ALSAInPort::~ALSAInPort()
@@ -55,18 +59,19 @@ int ALSAInPort::open(const PcmParams &params)
     uint32_t periodSize = config.period_size;
     uint32_t periodCount = config.period_count;
 
-    ALOGI("ALSAInPort: open capture port hw:%u,%u", mCardId, mPortId);
+    ALOGI("ALSAInPort: %s: open capture port", getName());
 
     mPcm = pcm_open(mCardId, mPortId, PCM_IN, &config);
     if (!pcm_is_ready(mPcm)) {
-        ALOGE("ALSAInPort: failed to open capture port hw:%u,%u: %s",
-              mCardId, mPortId, pcm_get_error(mPcm));
+        ALOGE("ALSAInPort: %s: failed to open capture port %s",
+              getName(), pcm_get_error(mPcm));
         return -ENODEV;
     }
 
     if ((periodSize != config.period_size) || (periodCount != config.period_count)) {
-        ALOGW("ALSAInPort: params were updated period_size=%u->%u periods=%u->%u",
-              periodSize, config.period_size, periodCount, config.period_count);
+        ALOGW("ALSAInPort: %s: params were updated period_size=%u->%u periods=%u->%u",
+              getName(), periodSize, config.period_size,
+              periodCount, config.period_count);
     }
 
     return 0;
@@ -74,7 +79,7 @@ int ALSAInPort::open(const PcmParams &params)
 
 void ALSAInPort::close()
 {
-    ALOGI("ALSAInPort: close capture port hw:%u,%u", mCardId, mPortId);
+    ALOGI("ALSAInPort: %s: close capture port", getName());
 
     AutoMutex lock(mLock);
     if (mPcm && pcm_is_ready(mPcm)) {
@@ -98,17 +103,18 @@ int ALSAInPort::read(void *buffer, size_t frames)
     AutoMutex lock(mLock);
 
     if (!mPcm || !pcm_is_ready(mPcm)) {
-        ALOGE("ALSAInPort: port hw:%u,%u is closed, cannot read", mCardId, mPortId);
+        ALOGE("ALSAInPort: %s: port is closed, cannot read", getName());
         return -EAGAIN;
     }
 
     uint32_t bytes = pcm_frames_to_bytes(mPcm, frames);
 
-    ALOGVV("ALSAInPort: read %u frames (%u bytes) buffer %p", frames, bytes, buffer);
+    ALOGVV("ALSAInPort: %s: read %u frames (%u bytes) buffer %p",
+           getName(), frames, bytes, buffer);
 
     int ret = pcm_read(mPcm, buffer, bytes);
     if (ret) {
-        ALOGE("ALSAInPort: failed to read %d", ret);
+        ALOGE("ALSAInPort: %s: failed to read %d", getName(), ret);
         return ret;
     }
 
@@ -120,7 +126,7 @@ int ALSAInPort::start()
     AutoMutex lock(mLock);
 
     if (!mPcm || !pcm_is_ready(mPcm)) {
-        ALOGE("ALSAInPort: port hw:%u,%u is closed, cannot start", mCardId, mPortId);
+        ALOGE("ALSAInPort: %s: port is closed, cannot start", getName());
         return -EAGAIN;
     }
 
@@ -134,7 +140,7 @@ int ALSAInPort::stop()
      * blocked reads which runs with mLock held
      */
     if (!mPcm || !pcm_is_ready(mPcm)) {
-        ALOGE("ALSAInPort: port hw:%u,%u is closed, cannot stop", mCardId, mPortId);
+        ALOGE("ALSAInPort: %s: port is closed, cannot stop", getName());
         return -EAGAIN;
     }
 
@@ -146,6 +152,9 @@ int ALSAInPort::stop()
 ALSAOutPort::ALSAOutPort(uint32_t card, uint32_t port, uint32_t period_count)
     : mCardId(card), mPortId(port), mPeriodCount(period_count), mPcm(NULL)
 {
+    std::stringstream s;
+    s << "hw:" << mCardId << "," << mPortId;
+    mName = s.str();
 }
 
 ALSAOutPort::~ALSAOutPort()
@@ -173,18 +182,19 @@ int ALSAOutPort::open(const PcmParams &params)
     uint32_t periodSize = config.period_size;
     uint32_t periodCount = config.period_count;
 
-    ALOGI("ALSAOutPort: open playback port hw:%u,%u", mCardId, mPortId);
+    ALOGI("ALSAOutPort: %s: open playback port", getName());
 
     mPcm = pcm_open(mCardId, mPortId, PCM_OUT, &config);
     if (!pcm_is_ready(mPcm)) {
-        ALOGE("ALSAOutPort: failed to open playback port hw:%u,%u: %s",
-              mCardId, mPortId, pcm_get_error(mPcm));
+        ALOGE("ALSAOutPort: %s: failed to open playback port %s",
+              getName(), pcm_get_error(mPcm));
         return -ENODEV;
     }
 
     if ((periodSize != config.period_size) || (periodCount != config.period_count)) {
-        ALOGW("ALSAOutPort: params were updated period_size=%u->%u periods=%u->%u",
-              periodSize, config.period_size, periodCount, config.period_count);
+        ALOGW("ALSAOutPort: %s: params were updated period_size=%u->%u periods=%u->%u",
+              getName(), periodSize, config.period_size,
+              periodCount, config.period_count);
     }
 
     return 0;
@@ -192,7 +202,7 @@ int ALSAOutPort::open(const PcmParams &params)
 
 void ALSAOutPort::close()
 {
-    ALOGI("ALSAOutPort: close playback port hw:%u,%u", mCardId, mPortId);
+    ALOGI("ALSAOutPort: %s: close playback port", getName());
 
     AutoMutex lock(mLock);
     if (mPcm && pcm_is_ready(mPcm)) {
@@ -216,17 +226,18 @@ int ALSAOutPort::write(const void *buffer, size_t frames)
     AutoMutex lock(mLock);
 
     if (!mPcm || !pcm_is_ready(mPcm)) {
-        ALOGE("ALSAOutPort: port hw:%u,%u is closed, cannot write", mCardId, mPortId);
+        ALOGE("ALSAOutPort: %s: port is closed, cannot write", getName());
         return -EAGAIN;
     }
 
     uint32_t bytes = pcm_frames_to_bytes(mPcm, frames);
 
-    ALOGVV("ALSAOutPort: write %u frames (%u bytes) buffer %p", frames, bytes, buffer);
+    ALOGVV("ALSAOutPort: %s: write %u frames (%u bytes) buffer %p",
+           getName(), frames, bytes, buffer);
 
     int ret = pcm_write(mPcm, buffer, bytes);
     if (ret) {
-        ALOGE("ALSAOutPort: failed to write %d", ret);
+        ALOGE("ALSAOutPort: %s: failed to write %d", getName(), ret);
         return ret;
     }
 
@@ -238,7 +249,7 @@ int ALSAOutPort::start()
     AutoMutex lock(mLock);
 
     if (!mPcm || !pcm_is_ready(mPcm)) {
-        ALOGE("ALSAOutPort: port hw:%u,%u is closed, cannot start", mCardId, mPortId);
+        ALOGE("ALSAOutPort: %s: port is closed, cannot start", getName());
         return -EAGAIN;
     }
 
@@ -252,7 +263,7 @@ int ALSAOutPort::stop()
      * blocked writes which runs with mLock held
      */
     if (!mPcm || !pcm_is_ready(mPcm)) {
-        ALOGE("ALSAOutPort: port hw:%u,%u is closed, cannot stop", mCardId, mPortId);
+        ALOGE("ALSAOutPort: %s: port is closed, cannot stop", getName());
         return -EAGAIN;
     }
 
index 97263f83cddc01d143737215fbe19b272c205edf..8a862706037af2537a561fd0140327f5383c0daf 100644 (file)
@@ -108,12 +108,12 @@ ThreadBase::ThreadBase()
 }
 
 ThreadBase::ThreadBase(const string &name)
-    : mName("Thread " + name), mRunning(false)
+    : mName(name), mRunning(false)
 {
 }
 
 ThreadBase::ThreadBase(const char *name)
-    : mName("Thread "), mRunning(false)
+    : mName(""), mRunning(false)
 {
     if (name)
         mName += name;
@@ -134,16 +134,19 @@ int ThreadBase::run()
 {
     AutoMutex lock(mMutex);
 
-    ALOGI("%s is starting", name());
+    ALOGI("Thread %s is starting", name());
+    mRunning = true;
     int ret = pthread_create(&mThread, NULL, threadWrapper, this);
     if (ret) {
-        ALOGE("%s: failed to create pthread %d", name(), ret);
+        ALOGE("Thread %s: failed to create pthread %d", name(), ret);
         return ret;
     }
 
-    mRunning = true;
+    ret = pthread_setname_np(mThread, mName.substr(0, mNameMaxLength - 1).c_str());
+    if (ret)
+        ALOGE("Thread %s: failed to set thread name %d", name(), ret);
 
-    return 0;
+    return ret;
 }
 
 int ThreadBase::stop()
@@ -151,7 +154,7 @@ int ThreadBase::stop()
     AutoMutex lock(mMutex);
     void *res;
 
-    ALOGI("%s is exiting", name());
+    ALOGI("Thread %s is exiting", name());
     mRunning = false;
     pthread_join(mThread, &res);
 
@@ -164,6 +167,21 @@ bool ThreadBase::isRunning() const
     return mRunning;
 }
 
+int ThreadBase::setName(string newname)
+{
+    AutoMutex lock(mMutex);
+    int ret = 0;
+
+    mName = newname;
+    if (mRunning) {
+        ret = pthread_setname_np(mThread, mName.substr(0, mNameMaxLength - 1).c_str());
+        if (ret)
+            ALOGE("Thread %s: failed to set new name %d", name(), ret);
+    }
+
+    return ret;
+}
+
 int ThreadBase::_threadLoop()
 {
     int ret = 0;
index 3e0a0b05a15189ccb1cb80dcddf8c7c67f358392..3718771c4420fcb70cca80767d5cef97d3a6c9c0 100644 (file)
@@ -25,7 +25,7 @@
 namespace tiaudioutils {
 
 NullInPort::NullInPort()
-    : mOpen(false)
+    : mOpen(false), mName("NullIn")
 {
 }
 
@@ -61,7 +61,7 @@ int NullInPort::read(void *buffer, size_t frames)
 /* ---------------------------------------------------------------------------------------- */
 
 NullOutPort::NullOutPort()
-    : mOpen(false)
+    : mOpen(false), mName("NullOut")
 {
 }
 
index 402c3f79fa88616fc1732362d73a8b65bd203450..bfac4185aebc5171b6144e34ae206a4d820df806 100644 (file)
@@ -624,6 +624,9 @@ PcmReader::PcmReader(PcmInPort *port, const PcmParams &params)
       mUnMerge(params),
       mUsers(0)
 {
+    if (mPort)
+        setName(string("Reader-") + port->getName());
+
     mBuffer.i8 = new int8_t[mParams.bufferSize()];
     mBuffer.frameCount = mParams.frameCount;
 }
@@ -655,17 +658,17 @@ bool PcmReader::initCheck() const
     }
 
     if (!mParams.isValid() || !mParams.frameCount) {
-        ALOGE("PcmReader: params are not valid");
+        ALOGE("PcmReader: %s: params are not valid", mPort->getName());
         return false;
     }
 
     if (mBuffer.raw == NULL) {
-        ALOGE("PcmReader: intermediate buffer allocation failed");
+        ALOGE("PcmReader: %s: intermediate buffer allocation failed", mPort->getName());
         return false;
     }
 
     if (!mUnMerge.initCheck()) {
-        ALOGE("PcmReader: un-merge failed to initialize");
+        ALOGE("PcmReader: %s: un-merge failed to initialize", mPort->getName());
         return false;
     }
 
@@ -675,22 +678,23 @@ bool PcmReader::initCheck() const
 int PcmReader::registerStream(sp<InStream>& stream)
 {
     if (stream == NULL) {
-        ALOGE("PcmReader: stream is invalid, cannot register");
+        ALOGE("PcmReader: %s: stream is invalid, cannot register", mPort->getName());
         return -EINVAL;
     }
 
     const PcmParams &outParams = stream->getParams();
     if (!outParams.isValid() || !outParams.frameCount) {
-        ALOGE("PcmReader: stream has invalid params");
+        ALOGE("PcmReader: %s: stream has invalid params", mPort->getName());
         return -EINVAL;
     }
 
-    ALOGI("PcmReader: register stream %p src 0x%04x dst 0x%04x",
-          stream.get(), stream->mMap.getSrcMask(), stream->mMap.getDstMask());
+    ALOGI("PcmReader: %s: register stream %p src 0x%04x dst 0x%04x",
+          mPort->getName(), stream.get(),
+          stream->mMap.getSrcMask(), stream->mMap.getDstMask());
 
     AutoMutex lock(mLock);
     if (mStreams.find(stream) != mStreams.end()) {
-        ALOGE("PcmReader: stream is already registered");
+        ALOGE("PcmReader: %s: stream is already registered", mPort->getName());
         return -EINVAL;
     }
 
@@ -703,19 +707,20 @@ int PcmReader::registerStream(sp<InStream>& stream)
     if ((mParams.sampleRate != outParams.sampleRate) && (outParams.sampleBits == 16)) {
         int ret = attachResampler(stream.get());
         if (ret) {
-            ALOGE("PcmReader: failed to create and attached resampler");
+            ALOGE("PcmReader: %s: failed to create and attached resampler",
+                  mPort->getName());
             return ret;
         }
     } else if ((mParams.sampleBits != outParams.sampleBits) ||
                (mParams.sampleRate != outParams.sampleRate)) {
         /* Channels of the reader and stream are different if un-merge is used */
-        ALOGE("PcmReader: reader doesn't support stream's params");
+        ALOGE("PcmReader: %s: reader doesn't support stream's params", mPort->getName());
         return -EINVAL;
     }
 
     int ret = mUnMerge.registerStream(stream);
     if (ret) {
-        ALOGE("PcmReader: failed to register stream %d", ret);
+        ALOGE("PcmReader: %s: failed to register stream %d", mPort->getName(), ret);
         detachResampler(stream.get());
         return ret;
     }
@@ -730,18 +735,19 @@ int PcmReader::registerStream(sp<InStream>& stream)
 void PcmReader::unregisterStream(sp<InStream>& stream)
 {
     if (stream == NULL) {
-        ALOGE("PcmReader: stream is invalid, cannot unregister");
+        ALOGE("PcmReader: %s: stream is invalid, cannot unregister", mPort->getName());
         return;
     }
 
     if (stream->isStarted()) {
-        ALOGE("PcmReader: stream %p is not stopped, cannot unregister",
-              stream.get());
+        ALOGE("PcmReader: %s: stream %p is not stopped, cannot unregister",
+              mPort->getName(), stream.get());
         return;
     }
 
-    ALOGI("PcmReader: unregister stream %p src 0x%04x dst 0x%04x",
-          stream.get(), stream->mMap.getSrcMask(), stream->mMap.getDstMask());
+    ALOGI("PcmReader: %s: unregister stream %p src 0x%04x dst 0x%04x",
+          mPort->getName(), stream.get(),
+          stream->mMap.getSrcMask(), stream->mMap.getDstMask());
 
     AutoMutex lock(mLock);
     if (mStreams.find(stream) != mStreams.end()) {
@@ -750,7 +756,8 @@ void PcmReader::unregisterStream(sp<InStream>& stream)
         stream->mReader = NULL;
         mStreams.erase(stream);
     } else {
-        ALOGE("PcmReader: stream is not registered or already unregistered");
+        ALOGE("PcmReader: %s: stream is not registered or already unregistered",
+              mPort->getName());
     }
 }
 
@@ -764,21 +771,21 @@ int PcmReader::open()
 {
     AutoMutex lock(mLock);
 
-    ALOGV("PcmReader: users %d->%d", mUsers, mUsers+1);
+    ALOGV("PcmReader: %s: users %d->%d", mPort->getName(), mUsers, mUsers+1);
     if (mUsers++)
         return 0;
 
-    ALOGV("PcmReader: open PCM port and start reader thread");
+    ALOGV("PcmReader: %s: open PCM port and start reader thread", mPort->getName());
     int ret = mPort->open(mParams);
     if (ret) {
-        ALOGE("PcmReader: failed to open PCM port %d", ret);
+        ALOGE("PcmReader: %s: failed to open PCM port %d", mPort->getName(), ret);
         return ret;
     }
 
     /* Start the reader thread */
     ret = run();
     if (ret)
-        ALOGE("PcmReader: failed to start reader thread %d", ret);
+        ALOGE("PcmReader: %s: failed to start reader thread %d", mPort->getName(), ret);
 
     return ret;
 }
@@ -787,12 +794,12 @@ void PcmReader::close()
 {
     AutoMutex lock(mLock);
 
-    ALOGV("PcmReader: users %d->%d", mUsers, mUsers-1);
+    ALOGV("PcmReader: %s: users %d->%d", mPort->getName(), mUsers, mUsers-1);
     ALOG_ASSERT(mUsers);
     if (--mUsers)
         return;
 
-    ALOGV("PcmReader: stop reader thread and close PCM port");
+    ALOGV("PcmReader: %s: stop reader thread and close PCM port", mPort->getName());
     stop();
     mPort->close();
 }
@@ -801,7 +808,7 @@ int PcmReader::threadFunc()
 {
     int ret = mPort->read(mBuffer.raw, mParams.frameCount);
     if (ret < 0) {
-        ALOGE("PcmReader: failed to read PCM data %d", ret);
+        ALOGE("PcmReader: %s: failed to read PCM data %d", mPort->getName(), ret);
         /*
          * Set frameCount to 0, this will result in an error being returned to
          * the buffer adaptor read call after the unmerge process call
@@ -810,7 +817,8 @@ int PcmReader::threadFunc()
     }
     else {
         if (ret != (int)mBuffer.frameCount) {
-            ALOGW("PcmReader: read fewer PCM frames than requested %d", ret);
+            ALOGW("PcmReader: %s: read fewer PCM frames than requested %d",
+                  mPort->getName(), ret);
         }
         /* Set the frame count to the actual amount read */
         mBuffer.frameCount = (size_t)ret;
@@ -818,7 +826,7 @@ int PcmReader::threadFunc()
 
     ret = mUnMerge.process(mBuffer);
     if (ret) {
-        ALOGE("PcmReader: unmerge failed %d", ret);
+        ALOGE("PcmReader: %s: unmerge failed %d", mPort->getName(), ret);
     }
 
     return 0;
@@ -868,6 +876,9 @@ PcmWriter::PcmWriter(PcmOutPort *port, const PcmParams &params)
       mMerge(params),
       mUsers(0)
 {
+    if (mPort)
+        setName(string("Writer-") + port->getName());
+
     mBuffer.i8 = new int8_t[mParams.bufferSize()];
     mBuffer.frameCount = mParams.frameCount;
 }
@@ -899,17 +910,17 @@ bool PcmWriter::initCheck() const
     }
 
     if (!mParams.isValid() || !mParams.frameCount) {
-        ALOGE("PcmWriter: params are not valid");
+        ALOGE("PcmWriter: %s: params are not valid", mPort->getName());
         return false;
     }
 
     if (mBuffer.raw == NULL) {
-        ALOGE("PcmWriter: intermediate buffer allocation failed");
+        ALOGE("PcmWriter: %s: intermediate buffer allocation failed", mPort->getName());
         return false;
     }
 
     if (!mMerge.initCheck()) {
-        ALOGE("PcmWriter: merge failed to initialize");
+        ALOGE("PcmWriter: %s: merge failed to initialize", mPort->getName());
         return false;
     }
 
@@ -919,22 +930,23 @@ bool PcmWriter::initCheck() const
 int PcmWriter::registerStream(sp<OutStream>& stream)
 {
     if (stream == NULL) {
-        ALOGE("PcmWriter: stream is invalid, cannot register");
+        ALOGE("PcmWriter: %s: stream is invalid, cannot register", mPort->getName());
         return -EINVAL;
     }
 
     const PcmParams &inParams = stream->getParams();
     if (!inParams.isValid() || !inParams.frameCount) {
-        ALOGE("PcmWriter: stream has invalid params");
+        ALOGE("PcmWriter: %s: stream has invalid params", mPort->getName());
         return -EINVAL;
     }
 
-    ALOGI("PcmWriter: register stream %p src 0x%04x dst 0x%04x",
-          stream.get(), stream->mMap.getSrcMask(), stream->mMap.getDstMask());
+    ALOGI("PcmWriter: %s: register stream %p src 0x%04x dst 0x%04x",
+          mPort->getName(), stream.get(),
+          stream->mMap.getSrcMask(), stream->mMap.getDstMask());
 
     AutoMutex lock(mLock);
     if (mStreams.find(stream) != mStreams.end()) {
-        ALOGE("PcmWriter: stream is already registered");
+        ALOGE("PcmWriter: %s: stream is already registered", mPort->getName());
         return -EINVAL;
     }
 
@@ -947,19 +959,20 @@ int PcmWriter::registerStream(sp<OutStream>& stream)
     if ((mParams.sampleRate != inParams.sampleRate) && (inParams.sampleBits == 16)) {
         int ret = attachResampler(stream.get());
         if (ret) {
-            ALOGE("PcmWriter: failed to create and attached resampler");
+            ALOGE("PcmWriter: %s: failed to create and attached resampler",
+                  mPort->getName());
             return ret;
         }
     } else if ((mParams.sampleBits != inParams.sampleBits) ||
                (mParams.sampleRate != inParams.sampleRate)) {
         /* Channels of the writer and stream are different if merge is used */
-        ALOGE("PcmWriter: writer doesn't support stream's params");
+        ALOGE("PcmWriter: %s: writer doesn't support stream's params", mPort->getName());
         return -EINVAL;
     }
 
     int ret = mMerge.registerStream(stream);
     if (ret) {
-        ALOGE("PcmWriter: failed to register stream %d", ret);
+        ALOGE("PcmWriter: %s: failed to register stream %d", mPort->getName(), ret);
         detachResampler(stream.get());
         return ret;
     }
@@ -974,18 +987,19 @@ int PcmWriter::registerStream(sp<OutStream>& stream)
 void PcmWriter::unregisterStream(sp<OutStream>& stream)
 {
     if (stream == NULL) {
-        ALOGE("PcmWriter: stream is invalid, cannot unregister");
+        ALOGE("PcmWriter: %s: stream is invalid, cannot unregister", mPort->getName());
         return;
     }
 
     if (stream->isStarted()) {
-        ALOGE("PcmWriter: stream %p is not stopped, cannot unregister",
-              stream.get());
+        ALOGE("PcmWriter: %s: stream %p is not stopped, cannot unregister",
+              mPort->getName(), stream.get());
         return;
     }
 
-    ALOGI("PcmWriter: unregister stream %p src 0x%04x dst 0x%04x",
-          stream.get(), stream->mMap.getSrcMask(), stream->mMap.getDstMask());
+    ALOGI("PcmWriter: %s: unregister stream %p src 0x%04x dst 0x%04x",
+          mPort->getName(), stream.get(),
+          stream->mMap.getSrcMask(), stream->mMap.getDstMask());
 
     AutoMutex lock(mLock);
     if (mStreams.find(stream) != mStreams.end()) {
@@ -994,7 +1008,8 @@ void PcmWriter::unregisterStream(sp<OutStream>& stream)
         stream->mWriter = NULL;
         mStreams.erase(stream);
     } else {
-        ALOGE("PcmWriter: stream is not registered or already unregistered");
+        ALOGE("PcmWriter: %s: stream is not registered or already unregistered",
+              mPort->getName());
     }
 }
 
@@ -1008,21 +1023,21 @@ int PcmWriter::open()
 {
     AutoMutex lock(mLock);
 
-    ALOGV("PcmWriter: users %d->%d", mUsers, mUsers+1);
+    ALOGV("PcmWriter: %s: users %d->%d", mPort->getName(), mUsers, mUsers+1);
     if (mUsers++)
         return 0;
 
-    ALOGV("PcmWriter: open PCM port and start writer thread");
+    ALOGV("PcmWriter: %s: open PCM port and start writer thread", mPort->getName());
     int ret = mPort->open(mParams);
     if (ret) {
-        ALOGE("PcmWriter: failed to open PCM port %d", ret);
+        ALOGE("PcmWriter: %s: failed to open PCM port %d", mPort->getName(), ret);
         return ret;
     }
 
     /* Start the writer thread */
     ret = run();
     if (ret)
-        ALOGE("PcmWriter: failed to start writer thread %d", ret);
+        ALOGE("PcmWriter: %s: failed to start writer thread %d", mPort->getName(), ret);
 
     return ret;
 }
@@ -1031,12 +1046,12 @@ void PcmWriter::close()
 {
     AutoMutex lock(mLock);
 
-    ALOGV("PcmWriter: users %d->%d", mUsers, mUsers-1);
+    ALOGV("PcmWriter: %s: users %d->%d", mPort->getName(), mUsers, mUsers-1);
     ALOG_ASSERT(mUsers);
     if (--mUsers)
         return;
 
-    ALOGV("PcmWriter: stop writer thread and close PCM port");
+    ALOGV("PcmWriter: %s: stop writer thread and close PCM port", mPort->getName());
     stop();
     mPort->close();
 }
@@ -1045,12 +1060,13 @@ int PcmWriter::threadFunc()
 {
     int ret = mMerge.process(mBuffer);
     if (ret) {
-        ALOGE("PcmWriter: error processing data from provider %d", ret);
+        ALOGE("PcmWriter: %s: error processing data from provider %d",
+              mPort->getName(), ret);
     }
 
     ret = mPort->write(mBuffer.raw, mBuffer.frameCount);
     if (ret < 0) {
-        ALOGE("PcmWriter: failed to write PCM data %d", ret);
+        ALOGE("PcmWriter: %s: failed to write PCM data %d", mPort->getName(), ret);
         /*
          * mPort->write errors will not propagate to the user through the buffer
          * adaptor. So, perform the sleep here to lessen the number of errors.