Latency.h 3.6 KB

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