Bug 902003: Dispatch getStats to STS thread and back. r=jesup
authorByron Campen [:bwc] <docfaraday@gmail.com>
Fri, 11 Oct 2013 17:13:09 -0700
changeset 164993 2a6d4fa91d5b89fc536536b2bbce0da7bd392dd4
parent 164992 5372aea57cdb429e4fcf6a951e2ae1e754d64034
child 164994 c0aa048f0c2f247101740708d50c70a5e464e77e
push id3066
push userakeybl@mozilla.com
push dateMon, 09 Dec 2013 19:58:46 +0000
treeherdermozilla-beta@a31a0dce83aa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjesup
bugs902003
milestone27.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 902003: Dispatch getStats to STS thread and back. r=jesup
media/webrtc/signaling/src/peerconnection/PeerConnectionImpl.cpp
media/webrtc/signaling/src/peerconnection/PeerConnectionImpl.h
--- a/media/webrtc/signaling/src/peerconnection/PeerConnectionImpl.cpp
+++ b/media/webrtc/signaling/src/peerconnection/PeerConnectionImpl.cpp
@@ -48,20 +48,22 @@
 #include "nsDOMJSUtils.h"
 #include "nsIDocument.h"
 #include "nsIScriptError.h"
 #include "nsPrintfCString.h"
 #include "nsURLHelper.h"
 #include "nsNetUtil.h"
 #include "nsIDOMDataChannel.h"
 #include "mozilla/dom/RTCConfigurationBinding.h"
+#include "mozilla/dom/RTCStatsReportBinding.h"
 #include "mozilla/dom/RTCPeerConnectionBinding.h"
 #include "mozilla/dom/PeerConnectionImplBinding.h"
 #include "mozilla/dom/DataChannelBinding.h"
 #include "MediaStreamList.h"
+#include "MediaStreamTrack.h"
 #include "nsIScriptGlobalObject.h"
 #include "jsapi.h"
 #include "DOMMediaStream.h"
 #endif
 
 #ifndef USE_FAKE_MEDIA_STREAMS
 #include "MediaSegment.h"
 #endif
@@ -1122,17 +1124,30 @@ PeerConnectionImpl::GetTimeSinceEpoch(DO
   return NS_OK;
 }
 #endif
 
 NS_IMETHODIMP
 PeerConnectionImpl::GetStats(mozilla::dom::MediaStreamTrack *aSelector) {
   PC_AUTO_ENTER_API_CALL(true);
 
-  // TODO: Insert dispatch to STS here.
+#ifdef MOZILLA_INTERNAL_API
+  uint32_t track = aSelector ? aSelector->GetTrackID() : 0;
+  DOMHighResTimeStamp now;
+  nsresult rv = GetTimeSinceEpoch(&now);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  nsRefPtr<PeerConnectionImpl> pc(this);
+  RUN_ON_THREAD(mSTSThread,
+                WrapRunnable(pc,
+                             &PeerConnectionImpl::GetStats_s,
+                             track,
+                             now),
+                NS_DISPATCH_NORMAL);
+#endif
   return NS_OK;
 }
 
 NS_IMETHODIMP
 PeerConnectionImpl::AddIceCandidate(const char* aCandidate, const char* aMid, unsigned short aLevel) {
   PC_AUTO_ENTER_API_CALL(true);
 
   Timecard *tc = mTimeCard;
@@ -1625,16 +1640,58 @@ PeerConnectionImpl::IceStateChange_m(PCI
                 WrapRunnable(pco,
                              &PeerConnectionObserver::OnStateChange,
                              PCObserverStateType::IceState,
                              rv, static_cast<JSCompartment*>(nullptr)),
                 NS_DISPATCH_NORMAL);
   return NS_OK;
 }
 
+#ifdef MOZILLA_INTERNAL_API
+void PeerConnectionImpl::GetStats_s(
+    uint32_t trackId,
+    DOMHighResTimeStamp now) {
+
+  nsresult result = NS_OK;
+  nsAutoPtr<RTCStatsReportInternal> report(new RTCStatsReportInternal);
+  if (!report) {
+    result = NS_ERROR_FAILURE;
+  }
+  nsRefPtr<PeerConnectionImpl> pc(this);
+  RUN_ON_THREAD(mThread,
+                WrapRunnable(pc,
+                             &PeerConnectionImpl::OnStatsReport_m,
+                             trackId,
+                             result,
+                             report),
+                NS_DISPATCH_NORMAL);
+}
+
+void PeerConnectionImpl::OnStatsReport_m(
+    uint32_t trackId,
+    nsresult result,
+    nsAutoPtr<mozilla::dom::RTCStatsReportInternal> report) {
+  PeerConnectionObserver* pco = mPCObserver.MayGet();
+  if (pco) {
+    JSErrorResult rv;
+    if (NS_SUCCEEDED(result)) {
+      pco->OnGetStatsSuccess(*report, rv);
+    } else {
+      pco->OnGetStatsError(kInternalError,
+                           ObString("Failed to fetch statistics"),
+                           rv);
+    }
+
+    if (rv.Failed()) {
+      CSFLogError(logTag, "Error firing stats observer callback");
+    }
+  }
+}
+#endif
+
 void
 PeerConnectionImpl::IceStreamReady(NrIceMediaStream *aStream)
 {
   PC_AUTO_ENTER_API_CALL_NO_CHECK();
   MOZ_ASSERT(aStream);
 
   CSFLogDebug(logTag, "%s: %s", __FUNCTION__, aStream->name().c_str());
 }
--- a/media/webrtc/signaling/src/peerconnection/PeerConnectionImpl.h
+++ b/media/webrtc/signaling/src/peerconnection/PeerConnectionImpl.h
@@ -56,16 +56,17 @@ typedef Fake_DOMMediaStream DOMMediaStre
 #else
 class DOMMediaStream;
 #endif
 
 namespace dom {
 class RTCConfiguration;
 class MediaConstraintsInternal;
 class MediaStreamTrack;
+class RTCStatsReportInternal;
 
 #ifdef USE_FAKE_PCOBSERVER
 typedef test::AFakePCObserver PeerConnectionObserver;
 typedef const char *PCObserverString;
 #else
 class PeerConnectionObserver;
 typedef NS_ConvertUTF8toUTF16 PCObserverString;
 #endif
@@ -471,16 +472,27 @@ private:
 #endif
 
   // Shut down media - called on main thread only
   void ShutdownMedia();
 
   // ICE callbacks run on the right thread.
   nsresult IceStateChange_m(mozilla::dom::PCImplIceState aState);
 
+#ifdef MOZILLA_INTERNAL_API
+  // Fills in an RTCStatsReportInternal. Must be run on STS.
+  void GetStats_s(uint32_t trackId,
+                  DOMHighResTimeStamp now);
+
+  // Sends an RTCStatsReport to JS. Must run on main thread.
+  void OnStatsReport_m(uint32_t trackId,
+                       nsresult result,
+                       nsAutoPtr<mozilla::dom::RTCStatsReportInternal> report);
+#endif
+
   // Timecard used to measure processing time. This should be the first class
   // attribute so that we accurately measure the time required to instantiate
   // any other attributes of this class.
   Timecard *mTimeCard;
 
   // The call
   mozilla::ScopedDeletePtr<Internal> mInternal;
   mozilla::dom::PCImplReadyState mReadyState;