OSDN Git Service

libaah_rtp: address http://b/issue?id=5755419
authorJohn Grossman <johngro@google.com>
Mon, 9 Jan 2012 18:15:50 +0000 (10:15 -0800)
committerJohn Grossman <johngro@google.com>
Mon, 9 Jan 2012 20:23:37 +0000 (12:23 -0800)
Introduce a heartbeat timeout for TX senders.  If none of the TX
sender's clients send any packets for this timeout period (10 minutes
right now), then the sender will stop sending keep-alive heartbeats.
This should cause RX clients to release any held media resources,
including any timed audio track.  Releasing timeed audio tracks allows
the audio system to go into its idle state, which allows the power amp
in Tungsten to power all of the way down, saving both heat and power.

Change-Id: Ib7a96d334e1064ddea3f07a6b21e6efedefc549a
Signed-off-by: John Grossman <johngro@google.com>
media/libaah_rtp/aah_tx_sender.cpp
media/libaah_rtp/aah_tx_sender.h

index d894b58..cbe5514 100644 (file)
@@ -38,13 +38,16 @@ const char* AAH_TXSender::kSendPacketTRTPPacket = "trtp";
 const int AAH_TXSender::kRetryTrimIntervalUs = 100000;
 const int AAH_TXSender::kHeartbeatIntervalUs = 1000000;
 const int AAH_TXSender::kRetryBufferCapacity = 100;
+const nsecs_t AAH_TXSender::kHeartbeatTimeout = 600ull * 1000000000ull;
 
 Mutex AAH_TXSender::sLock;
 wp<AAH_TXSender> AAH_TXSender::sInstance;
 uint32_t AAH_TXSender::sNextEpoch;
 bool AAH_TXSender::sNextEpochValid = false;
 
-AAH_TXSender::AAH_TXSender() : mSocket(-1) { }
+AAH_TXSender::AAH_TXSender() : mSocket(-1) {
+    mLastSentPacketTime = systemTime();
+}
 
 sp<AAH_TXSender> AAH_TXSender::GetInstance() {
     Mutex::Autolock autoLock(sLock);
@@ -215,6 +218,7 @@ void AAH_TXSender::onSendPacket(const sp<AMessage>& msg) {
 
     Mutex::Autolock lock(mEndpointLock);
     doSendPacket_l(packet, Endpoint(ipAddr, port));
+    mLastSentPacketTime = systemTime();
 }
 
 void AAH_TXSender::doSendPacket_l(const sp<TRTPPacket>& packet,
@@ -296,18 +300,20 @@ void AAH_TXSender::trimRetryBuffers() {
 void AAH_TXSender::sendHeartbeats() {
     Mutex::Autolock lock(mEndpointLock);
 
-    for (size_t i = 0; i < mEndpointMap.size(); i++) {
-        EndpointState* eps = mEndpointMap.editValueAt(i);
-        const Endpoint& ep = mEndpointMap.keyAt(i);
+    if (shouldSendHeartbeats_l()) {
+        for (size_t i = 0; i < mEndpointMap.size(); i++) {
+            EndpointState* eps = mEndpointMap.editValueAt(i);
+            const Endpoint& ep = mEndpointMap.keyAt(i);
 
-        sp<TRTPControlPacket> packet = new TRTPControlPacket();
-        packet->setCommandID(TRTPControlPacket::kCommandNop);
+            sp<TRTPControlPacket> packet = new TRTPControlPacket();
+            packet->setCommandID(TRTPControlPacket::kCommandNop);
 
-        packet->setExpireTime(systemTime() +
-                              AAH_TXPlayer::kAAHRetryKeepAroundTimeNs);
-        packet->pack();
+            packet->setExpireTime(systemTime() +
+                                  AAH_TXPlayer::kAAHRetryKeepAroundTimeNs);
+            packet->pack();
 
-        doSendPacket_l(packet, ep);
+            doSendPacket_l(packet, ep);
+        }
     }
 
     // schedule the next heartbeat
@@ -318,6 +324,11 @@ void AAH_TXSender::sendHeartbeats() {
     }
 }
 
+bool AAH_TXSender::shouldSendHeartbeats_l() {
+    // assert(holding endpoint lock)
+    return (systemTime() < (mLastSentPacketTime + kHeartbeatTimeout));
+}
+
 // Receiver
 
 // initial 4-byte ID of a retry request packet
index df4d162..47814de 100644 (file)
@@ -108,11 +108,13 @@ class AAH_TXSender : public virtual RefBase {
                         const Endpoint& endpoint);
     void trimRetryBuffers();
     void sendHeartbeats();
+    bool shouldSendHeartbeats_l();
 
     sp<ALooper> mLooper;
     sp<AHandlerReflector<AAH_TXSender> > mReflector;
 
     int mSocket;
+    nsecs_t mLastSentPacketTime;
 
     DefaultKeyedVector<Endpoint, EndpointState*> mEndpointMap;
     Mutex mEndpointLock;
@@ -120,6 +122,7 @@ class AAH_TXSender : public virtual RefBase {
     static const int kRetryTrimIntervalUs;
     static const int kHeartbeatIntervalUs;
     static const int kRetryBufferCapacity;
+    static const nsecs_t kHeartbeatTimeout;
 
     class RetryReceiver : public Thread {
       private: