1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef MOZILLA_LATENCY_H
8 #define MOZILLA_LATENCY_H
9 
10 #include "mozilla/TimeStamp.h"
11 #include "mozilla/Logging.h"
12 #include "nsCOMPtr.h"
13 #include "nsIThread.h"
14 #include "mozilla/Monitor.h"
15 #include "nsISupportsImpl.h"
16 #include "nsIObserver.h"
17 
18 class AsyncLatencyLogger;
19 
20 mozilla::LogModule *GetLatencyLog();
21 
22 // This class is a singleton. It is refcounted.
23 class AsyncLatencyLogger : public nsIObserver {
24   NS_DECL_THREADSAFE_ISUPPORTS
25   NS_DECL_NSIOBSERVER
26 
27  public:
28   enum LatencyLogIndex {
29     AudioMediaStreamTrack = 0,
30     VideoMediaStreamTrack,
31     Cubeb,
32     AudioStream,
33     NetEQ,
34     AudioCaptureBase,     // base time for capturing an audio stream
35     AudioCapture,         // records number of samples captured and the time
36     AudioTrackInsertion,  // # of samples inserted into a mediastreamtrack and
37                           // the time
38     MediaPipelineAudioInsertion,  // Timestamp and time of timestamp
39     AudioTransmit,                // Timestamp and socket send time
40     AudioReceive,                 // Timestamp and receive time
41     MediaPipelineAudioPlayout,    // Timestamp and playout into MST time
42     MediaStreamCreate,            // Source and TrackUnion streams
43     AudioStreamCreate,            // TrackUnion stream and AudioStream
44     AudioSendRTP,
45     AudioRecvRTP,
46     _MAX_INDEX
47   };
48   // Log with a null timestamp
49   void Log(LatencyLogIndex index, uint64_t aID, int64_t aValue);
50   // Log with a timestamp
51   void Log(LatencyLogIndex index, uint64_t aID, int64_t aValue,
52            mozilla::TimeStamp &aTime);
53   // Write a log message to NSPR
54   void WriteLog(LatencyLogIndex index, uint64_t aID, int64_t aValue,
55                 mozilla::TimeStamp timestamp);
56   // Get the base time used by the logger for delta calculations
57   void GetStartTime(mozilla::TimeStamp &aStart);
58 
59   static AsyncLatencyLogger *Get(bool aStartTimer = false);
60   static void InitializeStatics();
61   // After this is called, the global log object may go away
62   static void ShutdownLogger();
63 
64  private:
65   AsyncLatencyLogger();
66   virtual ~AsyncLatencyLogger();
67   int64_t GetTimeStamp();
68   void Init();
69   // Shut down the thread associated with this, and make sure it doesn't
70   // start up again.
71   void Shutdown();
72   // The thread on which the IO happens
73   nsCOMPtr<nsIThread> mThread;
74   // This can be initialized on multiple threads, but is protected by a
75   // monitor. After the initialization phase, it is accessed on the log
76   // thread only.
77   mozilla::TimeStamp mStart;
78   // This monitor protects mStart and mMediaLatencyLog for the
79   // initialization sequence. It is initialized at layout startup, and
80   // destroyed at layout shutdown.
81   mozilla::Mutex mMutex;
82 };
83 
84 // need uint32_t versions for access from webrtc/trunk code
85 // Log without a time delta
86 void LogLatency(AsyncLatencyLogger::LatencyLogIndex index, uint64_t aID,
87                 int64_t aValue);
88 void LogLatency(uint32_t index, uint64_t aID, int64_t aValue);
89 // Log TimeStamp::Now() (as delta)
90 void LogTime(AsyncLatencyLogger::LatencyLogIndex index, uint64_t aID,
91              int64_t aValue);
92 void LogTime(uint32_t index, uint64_t aID, int64_t aValue);
93 // Log the specified time (as delta)
94 void LogTime(AsyncLatencyLogger::LatencyLogIndex index, uint64_t aID,
95              int64_t aValue, mozilla::TimeStamp &aTime);
96 
97 // For generating unique-ish ids for logged sources
98 #define LATENCY_STREAM_ID(source, trackID) \
99   ((((uint64_t)(source)) & ~0x0F) | (trackID))
100 
101 #endif
102