TimedPacketizer.h (2421B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 4 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #ifndef DOM_MEDIA_TIMEPACKETIZER_H_ 7 #define DOM_MEDIA_TIMEPACKETIZER_H_ 8 9 #include "AudioPacketizer.h" 10 #include "TimeUnits.h" 11 12 namespace mozilla { 13 /** 14 * This class wraps an AudioPacketizer and provides packets of audio with 15 * timestamps. 16 */ 17 template <typename InputType, typename OutputType> 18 class TimedPacketizer { 19 public: 20 TimedPacketizer(uint32_t aPacketSize, uint32_t aChannels, 21 int64_t aInitialTick, int64_t aBase) 22 : mPacketizer(aPacketSize, aChannels), 23 mTimeStamp(media::TimeUnit(aInitialTick, aBase)), 24 mBase(aBase) {} 25 26 void Input(const InputType* aFrames, uint32_t aFrameCount) { 27 mPacketizer.Input(aFrames, aFrameCount); 28 } 29 30 media::TimeUnit Output(OutputType* aOutputBuffer) { 31 MOZ_ASSERT(mPacketizer.PacketsAvailable()); 32 media::TimeUnit pts = mTimeStamp; 33 mPacketizer.Output(aOutputBuffer); 34 mTimeStamp += media::TimeUnit(mPacketizer.mPacketSize, mBase); 35 return pts; 36 } 37 38 media::TimeUnit Drain(OutputType* aOutputBuffer, uint32_t& aWritten) { 39 MOZ_ASSERT(!mPacketizer.PacketsAvailable(), 40 "Consume all packets before calling drain"); 41 media::TimeUnit pts = mTimeStamp; 42 aWritten = mPacketizer.Output(aOutputBuffer); 43 mTimeStamp += media::TimeUnit(mPacketizer.mPacketSize, mBase); 44 return pts; 45 } 46 47 // Call this when a discontinuity in input has been detected, e.g. based on 48 // the pts of input packets. 49 void Discontinuity(int64_t aNewTick) { 50 MOZ_ASSERT(!mPacketizer.FramesAvailable(), 51 "Drain before enqueueing discontinuous audio"); 52 mTimeStamp = media::TimeUnit(aNewTick, mBase); 53 } 54 55 void Clear() { mPacketizer.Clear(); } 56 57 uint32_t PacketSize() const { return mPacketizer.mPacketSize; } 58 59 uint32_t ChannelCount() const { return mPacketizer.mChannels; } 60 61 uint32_t PacketsAvailable() const { return mPacketizer.PacketsAvailable(); } 62 63 uint32_t FramesAvailable() const { return mPacketizer.FramesAvailable(); } 64 65 private: 66 AudioPacketizer<InputType, OutputType> mPacketizer; 67 media::TimeUnit mTimeStamp; 68 uint64_t mBase; 69 }; 70 71 } // namespace mozilla 72 73 #endif // DOM_MEDIA_TIMEPACKETIZER_H_