michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MOZILLA_LATENCY_H michael@0: #define MOZILLA_LATENCY_H michael@0: michael@0: #include "mozilla/TimeStamp.h" michael@0: #include "prlog.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsIThread.h" michael@0: #include "mozilla/Monitor.h" michael@0: #include "nsISupportsImpl.h" michael@0: #include "nsIObserver.h" michael@0: michael@0: class AsyncLatencyLogger; michael@0: class LogEvent; michael@0: michael@0: PRLogModuleInfo* GetLatencyLog(); michael@0: michael@0: // This class is a singleton. It is refcounted. michael@0: class AsyncLatencyLogger : public nsIObserver michael@0: { michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: NS_DECL_NSIOBSERVER michael@0: michael@0: public: michael@0: michael@0: enum LatencyLogIndex { michael@0: AudioMediaStreamTrack = 0, michael@0: VideoMediaStreamTrack, michael@0: Cubeb, michael@0: AudioStream, michael@0: NetEQ, michael@0: AudioCaptureBase, // base time for capturing an audio stream michael@0: AudioCapture, // records number of samples captured and the time michael@0: AudioTrackInsertion, // # of samples inserted into a mediastreamtrack and the time michael@0: MediaPipelineAudioInsertion, // Timestamp and time of timestamp michael@0: AudioTransmit, // Timestamp and socket send time michael@0: AudioReceive, // Timestamp and receive time michael@0: MediaPipelineAudioPlayout, // Timestamp and playout into MST time michael@0: MediaStreamCreate, // Source and TrackUnion streams michael@0: AudioStreamCreate, // TrackUnion stream and AudioStream michael@0: AudioSendRTP, michael@0: AudioRecvRTP, michael@0: _MAX_INDEX michael@0: }; michael@0: // Log with a null timestamp michael@0: void Log(LatencyLogIndex index, uint64_t aID, int64_t aValue); michael@0: // Log with a timestamp michael@0: void Log(LatencyLogIndex index, uint64_t aID, int64_t aValue, michael@0: mozilla::TimeStamp &aTime); michael@0: // Write a log message to NSPR michael@0: void WriteLog(LatencyLogIndex index, uint64_t aID, int64_t aValue, michael@0: mozilla::TimeStamp timestamp); michael@0: // Get the base time used by the logger for delta calculations michael@0: void GetStartTime(mozilla::TimeStamp &aStart); michael@0: michael@0: static AsyncLatencyLogger* Get(bool aStartTimer = false); michael@0: static void InitializeStatics(); michael@0: // After this is called, the global log object may go away michael@0: static void ShutdownLogger(); michael@0: private: michael@0: AsyncLatencyLogger(); michael@0: virtual ~AsyncLatencyLogger(); michael@0: int64_t GetTimeStamp(); michael@0: void Init(); michael@0: // Shut down the thread associated with this, and make sure it doesn't michael@0: // start up again. michael@0: void Shutdown(); michael@0: // The thread on which the IO happens michael@0: nsCOMPtr mThread; michael@0: // This can be initialized on multiple threads, but is protected by a michael@0: // monitor. After the initialization phase, it is accessed on the log michael@0: // thread only. michael@0: mozilla::TimeStamp mStart; michael@0: // This monitor protects mStart and mMediaLatencyLog for the michael@0: // initialization sequence. It is initialized at layout startup, and michael@0: // destroyed at layout shutdown. michael@0: mozilla::Mutex mMutex; michael@0: }; michael@0: michael@0: // need uint32_t versions for access from webrtc/trunk code michael@0: // Log without a time delta michael@0: void LogLatency(AsyncLatencyLogger::LatencyLogIndex index, uint64_t aID, int64_t aValue); michael@0: void LogLatency(uint32_t index, uint64_t aID, int64_t aValue); michael@0: // Log TimeStamp::Now() (as delta) michael@0: void LogTime(AsyncLatencyLogger::LatencyLogIndex index, uint64_t aID, int64_t aValue); michael@0: void LogTime(uint32_t index, uint64_t aID, int64_t aValue); michael@0: // Log the specified time (as delta) michael@0: void LogTime(AsyncLatencyLogger::LatencyLogIndex index, uint64_t aID, int64_t aValue, michael@0: mozilla::TimeStamp &aTime); michael@0: michael@0: // For generating unique-ish ids for logged sources michael@0: #define LATENCY_STREAM_ID(source, trackID) \ michael@0: ((((uint64_t) (source)) & ~0x0F) | (trackID)) michael@0: michael@0: #endif