From 99b4162ccf5bf1ab2f695849d4309f4db302c46a Mon Sep 17 00:00:00 2001 From: "henrike@webrtc.org" Date: Wed, 21 May 2014 20:42:17 +0000 Subject: [PATCH] Rebase webrtc/base 6163:6216 (svn diff -r 6163:6216 http://webrtc.googlecode.com/svn/trunk/talk/base, apply diff manually) BUG=3379 TBR=wu@webrtc.org Review URL: https://webrtc-codereview.appspot.com/17619004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@6217 4adac7df-926f-26a2-2b94-8c16560cd09d --- webrtc/base/messagequeue.cc | 19 ++++--------------- webrtc/base/messagequeue.h | 6 +----- webrtc/base/thread.cc | 4 +--- webrtc/base/timeutils.cc | 14 ++++++++++++++ webrtc/base/timeutils.h | 11 +++++++++++ webrtc/base/timeutils_unittest.cc | 23 +++++++++++++++++++++++ 6 files changed, 54 insertions(+), 23 deletions(-) diff --git a/webrtc/base/messagequeue.cc b/webrtc/base/messagequeue.cc index bf31a05960..1b312ff7af 100644 --- a/webrtc/base/messagequeue.cc +++ b/webrtc/base/messagequeue.cc @@ -109,7 +109,7 @@ void MessageQueueManager::ClearInternal(MessageHandler *handler) { // MessageQueue MessageQueue::MessageQueue(SocketServer* ss) - : ss_(ss), fStop_(false), fPeekKeep_(false), active_(false), + : ss_(ss), fStop_(false), fPeekKeep_(false), dmsgq_next_num_(0) { if (!ss_) { // Currently, MessageQueue holds a socket server, and is the base class for @@ -121,6 +121,7 @@ MessageQueue::MessageQueue(SocketServer* ss) ss_ = default_ss_.get(); } ss_->SetMessageQueue(this); + MessageQueueManager::Add(this); } MessageQueue::~MessageQueue() { @@ -128,10 +129,8 @@ MessageQueue::~MessageQueue() { // that it always gets called when the queue // is going away. SignalQueueDestroyed(); - if (active_) { - MessageQueueManager::Remove(this); - Clear(NULL); - } + MessageQueueManager::Remove(this); + Clear(NULL); if (ss_) { ss_->SetMessageQueue(NULL); } @@ -279,7 +278,6 @@ void MessageQueue::Post(MessageHandler *phandler, uint32 id, // Signal for the multiplexer to return CritScope cs(&crit_); - EnsureActive(); Message msg; msg.phandler = phandler; msg.message_id = id; @@ -301,7 +299,6 @@ void MessageQueue::DoDelayPost(int cmsDelay, uint32 tstamp, // Signal for the multiplexer to return. CritScope cs(&crit_); - EnsureActive(); Message msg; msg.phandler = phandler; msg.message_id = id; @@ -384,12 +381,4 @@ void MessageQueue::Dispatch(Message *pmsg) { pmsg->phandler->OnMessage(pmsg); } -void MessageQueue::EnsureActive() { - ASSERT(crit_.CurrentThreadIsOwner()); - if (!active_) { - active_ = true; - MessageQueueManager::Add(this); - } -} - } // namespace rtc diff --git a/webrtc/base/messagequeue.h b/webrtc/base/messagequeue.h index 958a39eb99..41c1e24b00 100644 --- a/webrtc/base/messagequeue.h +++ b/webrtc/base/messagequeue.h @@ -58,7 +58,7 @@ class MessageQueueManager { void ClearInternal(MessageHandler *handler); static MessageQueueManager* instance_; - // This list contains 'active' MessageQueues. + // This list contains all live MessageQueues. std::vector message_queues_; CriticalSection crit_; }; @@ -230,7 +230,6 @@ class MessageQueue { void reheap() { make_heap(c.begin(), c.end(), comp); } }; - void EnsureActive(); void DoDelayPost(int cmsDelay, uint32 tstamp, MessageHandler *phandler, uint32 id, MessageData* pdata); @@ -241,9 +240,6 @@ class MessageQueue { bool fStop_; bool fPeekKeep_; Message msgPeek_; - // A message queue is active if it has ever had a message posted to it. - // This also corresponds to being in MessageQueueManager's global list. - bool active_; MessageList msgq_; PriorityQueue dmsgq_; uint32 dmsgq_next_num_; diff --git a/webrtc/base/thread.cc b/webrtc/base/thread.cc index 0e4f0f35fe..3963b3886a 100644 --- a/webrtc/base/thread.cc +++ b/webrtc/base/thread.cc @@ -140,8 +140,7 @@ Thread::Thread(SocketServer* ss) Thread::~Thread() { Stop(); - if (active_) - Clear(NULL); + Clear(NULL); } bool Thread::SleepMs(int milliseconds) { @@ -386,7 +385,6 @@ void Thread::Send(MessageHandler *phandler, uint32 id, MessageData *pdata) { bool ready = false; { CritScope cs(&crit_); - EnsureActive(); _SendMessage smsg; smsg.thread = current_thread; smsg.msg = msg; diff --git a/webrtc/base/timeutils.cc b/webrtc/base/timeutils.cc index aefa285182..dcf83e3dd9 100644 --- a/webrtc/base/timeutils.cc +++ b/webrtc/base/timeutils.cc @@ -186,4 +186,18 @@ int32 TimeDiff(uint32 later, uint32 earlier) { #endif } +TimestampWrapAroundHandler::TimestampWrapAroundHandler() + : last_ts_(0), num_wrap_(0) {} + +int64 TimestampWrapAroundHandler::Unwrap(uint32 ts) { + if (ts < last_ts_) { + if (last_ts_ > 0xf0000000 && ts < 0x0fffffff) { + ++num_wrap_; + } + } + last_ts_ = ts; + int64_t unwrapped_ts = ts + (num_wrap_ << 32); + return unwrapped_ts; +} + } // namespace rtc diff --git a/webrtc/base/timeutils.h b/webrtc/base/timeutils.h index bdf73cc037..ca041a7d11 100644 --- a/webrtc/base/timeutils.h +++ b/webrtc/base/timeutils.h @@ -80,6 +80,17 @@ inline int64 UnixTimestampNanosecsToNtpMillisecs(int64 unix_ts_ns) { return unix_ts_ns / kNumNanosecsPerMillisec + kJan1970AsNtpMillisecs; } +class TimestampWrapAroundHandler { + public: + TimestampWrapAroundHandler(); + + int64 Unwrap(uint32 ts); + + private: + uint32 last_ts_; + int64 num_wrap_; +}; + } // namespace rtc #endif // WEBRTC_BASE_TIMEUTILS_H_ diff --git a/webrtc/base/timeutils_unittest.cc b/webrtc/base/timeutils_unittest.cc index 86a18179c9..087fb0c28b 100644 --- a/webrtc/base/timeutils_unittest.cc +++ b/webrtc/base/timeutils_unittest.cc @@ -143,4 +143,27 @@ TEST(TimeTest, DISABLED_CurrentTmTime) { EXPECT_TRUE(0 <= microseconds && microseconds < 1000000); } +class TimestampWrapAroundHandlerTest : public testing::Test { + public: + TimestampWrapAroundHandlerTest() {} + + protected: + TimestampWrapAroundHandler wraparound_handler_; +}; + +TEST_F(TimestampWrapAroundHandlerTest, Unwrap) { + uint32 ts = 0xfffffff2; + int64 unwrapped_ts = ts; + EXPECT_EQ(ts, wraparound_handler_.Unwrap(ts)); + ts = 2; + unwrapped_ts += 0x10; + EXPECT_EQ(unwrapped_ts, wraparound_handler_.Unwrap(ts)); + ts = 0xfffffff2; + unwrapped_ts += 0xfffffff0; + EXPECT_EQ(unwrapped_ts, wraparound_handler_.Unwrap(ts)); + ts = 0; + unwrapped_ts += 0xe; + EXPECT_EQ(unwrapped_ts, wraparound_handler_.Unwrap(ts)); +} + } // namespace rtc