Remove the thread switching between the SocketReaderNode and the RtpDecoderNode

A design flaw in thread switching can cause latency in thread scheduling when other applications on the AP side are congested. To eliminate this latency, the ImsMedia RX stream has been modified to avoid switching threads between the SocketReaderNode, which receives data frames from the socket, and the RtpDecoderNode, which performs RTP depacketization.

1. Remove the thread switching between the SocketReaderNode and the RtpDecoderNode
2. Stop the node before update in the AudioStreamGraphRtpRx

Bug: 304901905
Bug: 308062939
Test: Verified by the VoWifi call in ATT 19093 certification test. atest
ImsMediaNativeTests

Change-Id: I601629d027a3d27be8a9fdd165782f0407d8ef5b
diff --git a/service/src/com/android/telephony/imsmedia/lib/libimsmedia/core/audio/AudioStreamGraphRtpRx.cpp b/service/src/com/android/telephony/imsmedia/lib/libimsmedia/core/audio/AudioStreamGraphRtpRx.cpp
index 7266551..0f19b63 100644
--- a/service/src/com/android/telephony/imsmedia/lib/libimsmedia/core/audio/AudioStreamGraphRtpRx.cpp
+++ b/service/src/com/android/telephony/imsmedia/lib/libimsmedia/core/audio/AudioStreamGraphRtpRx.cpp
@@ -109,24 +109,15 @@
     }
 
     ImsMediaResult ret = RESULT_NOT_READY;
+    bool needsToStart = false;
 
     if (mGraphState == kStreamStateRunning)
     {
-        mScheduler->Stop();
-
-        for (auto& node : mListNodeStarted)
-        {
-            IMLOGD1("[update] update node[%s]", node->GetNodeName());
-            ret = node->UpdateConfig(mConfig);
-
-            if (ret != RESULT_SUCCESS)
-            {
-                IMLOGE2("[update] error in update node[%s], ret[%d]", node->GetNodeName(), ret);
-            }
-        }
-        mScheduler->Start();
+        stop();
+        needsToStart = true;
     }
-    else if (mGraphState == kStreamStateCreated)
+
+    if (mGraphState == kStreamStateCreated)
     {
         for (auto& node : mListNodeToStart)
         {
@@ -148,6 +139,11 @@
         return start();
     }
 
+    if (needsToStart)
+    {
+        return start();
+    }
+
     return ret;
 }
 
diff --git a/service/src/com/android/telephony/imsmedia/lib/libimsmedia/core/include/nodes/SocketReaderNode.h b/service/src/com/android/telephony/imsmedia/lib/libimsmedia/core/include/nodes/SocketReaderNode.h
index 15d13a1..37892bb 100644
--- a/service/src/com/android/telephony/imsmedia/lib/libimsmedia/core/include/nodes/SocketReaderNode.h
+++ b/service/src/com/android/telephony/imsmedia/lib/libimsmedia/core/include/nodes/SocketReaderNode.h
@@ -30,7 +30,6 @@
     virtual bool Prepare();
     virtual ImsMediaResult Start();
     virtual void Stop();
-    virtual void ProcessData();
     virtual bool IsRunTime();
     virtual bool IsSourceNode();
     void SetConfig(void* config);
diff --git a/service/src/com/android/telephony/imsmedia/lib/libimsmedia/core/nodes/SocketReaderNode.cpp b/service/src/com/android/telephony/imsmedia/lib/libimsmedia/core/nodes/SocketReaderNode.cpp
index 549056d..4bd392b 100644
--- a/service/src/com/android/telephony/imsmedia/lib/libimsmedia/core/nodes/SocketReaderNode.cpp
+++ b/service/src/com/android/telephony/imsmedia/lib/libimsmedia/core/nodes/SocketReaderNode.cpp
@@ -53,8 +53,6 @@
 
 ImsMediaResult SocketReaderNode::Start()
 {
-    ClearDataQueue();  // clear the old data stacked
-
     if (mSocketOpened)
     {
         IMLOGD0("[Start] opened already");
@@ -74,34 +72,13 @@
 void SocketReaderNode::Stop()
 {
     IMLOGD2("[Stop] media[%d], protocolType[%d]", mMediaType, mProtocolType);
+    std::lock_guard<std::mutex> guard(mMutex);
     mNodeState = kNodeStateStopped;
 }
 
-void SocketReaderNode::ProcessData()
-{
-    uint8_t* data = nullptr;
-    uint32_t dataSize = 0;
-    uint32_t timeStamp = 0;
-    bool bMark = false;
-    uint32_t seqNum = 0;
-    ImsMediaSubType subtype;
-    ImsMediaSubType dataType;
-    uint32_t arrivalTime;
-
-    while (GetData(
-            &subtype, &data, &dataSize, &timeStamp, &bMark, &seqNum, &dataType, &arrivalTime))
-    {
-        IMLOGD_PACKET3(IM_PACKET_LOG_SOCKET, "[ProcessData] media[%d], size[%d], arrivalTime[%u]",
-                mMediaType, dataSize, arrivalTime);
-        SendDataToRearNode(MEDIASUBTYPE_UNDEFINED, reinterpret_cast<uint8_t*>(data), dataSize,
-                timeStamp, bMark, seqNum, dataType, arrivalTime);
-        DeleteData();
-    }
-}
-
 bool SocketReaderNode::IsRunTime()
 {
-    return false;
+    return true;
 }
 
 bool SocketReaderNode::IsSourceNode()
@@ -198,27 +175,22 @@
 
 void SocketReaderNode::OnReadDataFromSocket()
 {
-    IMLOGD_PACKET1(IM_PACKET_LOG_SOCKET, "[OnReadDataFromSocket] media[%d]", mMediaType);
     std::lock_guard<std::mutex> guard(mMutex);
 
-    // prevent infinite frame stacked in the queue
-    if (mDataQueue.GetCount() > MAX_BUFFER_QUEUE)
-    {
-        mDataQueue.Delete();
-    }
-
     if (mSocketOpened && mSocket != nullptr)
     {
-        int nLen = mSocket->ReceiveFrom(mBuffer, DEFAULT_MTU);
+        int len = mSocket->ReceiveFrom(mBuffer, DEFAULT_MTU);
 
-        if (nLen > 0)
+        if (len > 0)
         {
-            IMLOGD_PACKET3(IM_PACKET_LOG_SOCKET,
-                    "[OnReadDataFromSocket] media[%d], data size[%d], queue size[%d]", mMediaType,
-                    nLen, GetDataCount());
+            IMLOGD_PACKET2(IM_PACKET_LOG_SOCKET, "[OnReadDataFromSocket] media[%d], data size[%d]",
+                    mMediaType, len);
 
-            OnDataFromFrontNode(MEDIASUBTYPE_UNDEFINED, mBuffer, nLen, 0, 0, 0,
-                    MEDIASUBTYPE_UNDEFINED, ImsMediaTimer::GetTimeInMilliSeconds());
+            if (mNodeState == kNodeStateRunning)
+            {
+                SendDataToRearNode(MEDIASUBTYPE_UNDEFINED, mBuffer, len, 0, 0, 0,
+                        MEDIASUBTYPE_UNDEFINED, ImsMediaTimer::GetTimeInMilliSeconds());
+            }
         }
     }
 }
diff --git a/tests/native/service/src/com/android/telephony/imsmedia/lib/libimsmedia/core/nodes/SocketNodeTest.cpp b/tests/native/service/src/com/android/telephony/imsmedia/lib/libimsmedia/core/nodes/SocketNodeTest.cpp
index 71432b0..cb39fb6 100644
--- a/tests/native/service/src/com/android/telephony/imsmedia/lib/libimsmedia/core/nodes/SocketNodeTest.cpp
+++ b/tests/native/service/src/com/android/telephony/imsmedia/lib/libimsmedia/core/nodes/SocketNodeTest.cpp
@@ -251,16 +251,22 @@
     EXPECT_EQ(mWriter->Start(), RESULT_SUCCESS);
 
     mReader->Stop();
+    EXPECT_EQ(mReader->GetState(), kNodeStateStopped);
 
     uint8_t testPacket[] = {0x80, 0x68, 0x00, 0x0b, 0xbc, 0xbc, 0xe8, 0xa4, 0x00, 0x04, 0x11, 0x68,
             0xf4, 0xfa, 0xfe, 0x67, 0x58, 0x84, 0x80};
 
+    ON_CALL(mMockNode, GetState).WillByDefault(Return(kNodeStateStopped));
+    EXPECT_CALL(mMockNode,
+            OnDataFromFrontNode(MEDIASUBTYPE_UNDEFINED, Pointee(Eq(*testPacket)),
+                    sizeof(testPacket), 0, false, 0, _, _))
+            .Times(0);
+
     mWriter->OnDataFromFrontNode(
             MEDIASUBTYPE_UNDEFINED, testPacket, sizeof(testPacket), 0, false, 0);
     mCondition.wait_timeout(20);
-    EXPECT_EQ(mReader->GetDataCount(), 1);
     EXPECT_EQ(mReader->Start(), RESULT_SUCCESS);
-    EXPECT_EQ(mReader->GetDataCount(), 0);
+    EXPECT_EQ(mReader->GetState(), kNodeStateRunning);
     mWriter->Stop();
     mReader->Stop();
 }