Muxer.h (2583B)
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_ENCODER_MUXER_H_ 7 #define DOM_MEDIA_ENCODER_MUXER_H_ 8 9 #include "MediaQueue.h" 10 #include "mozilla/media/MediaUtils.h" 11 12 namespace mozilla { 13 14 class ContainerWriter; 15 class EncodedFrame; 16 class TrackMetadataBase; 17 18 // Generic Muxer class that helps pace the output from track encoders to the 19 // ContainerWriter, so time never appears to go backwards. 20 // Note that the entire class is written for single threaded access. 21 class Muxer { 22 public: 23 Muxer(UniquePtr<ContainerWriter> aWriter, 24 MediaQueue<EncodedFrame>& aEncodedAudioQueue, 25 MediaQueue<EncodedFrame>& aEncodedVideoQueue); 26 ~Muxer() = default; 27 28 // Disconnects MediaQueues such that they will no longer be consumed. 29 // Idempotent. 30 void Disconnect(); 31 32 // Returns true when all tracks have ended, and all data has been muxed and 33 // fetched. 34 bool IsFinished(); 35 36 // Returns true if this muxer has not been given metadata yet. 37 bool NeedsMetadata() const { return !mMetadataSet; } 38 39 // Sets metadata for all tracks. This may only be called once. 40 nsresult SetMetadata(const nsTArray<RefPtr<TrackMetadataBase>>& aMetadata); 41 42 // Gets the data that has been muxed and written into the container so far. 43 nsresult GetData(nsTArray<nsTArray<uint8_t>>* aOutputBuffers); 44 45 private: 46 // Writes data in MediaQueues to the ContainerWriter. 47 nsresult Mux(); 48 49 // Audio frames that have been encoded and are pending write to the muxer. 50 MediaQueue<EncodedFrame>& mEncodedAudioQueue; 51 // Video frames that have been encoded and are pending write to the muxer. 52 MediaQueue<EncodedFrame>& mEncodedVideoQueue; 53 // Listeners driving the muxing as encoded data gets produced. 54 MediaEventListener mAudioPushListener; 55 MediaEventListener mAudioFinishListener; 56 MediaEventListener mVideoPushListener; 57 MediaEventListener mVideoFinishListener; 58 // The writer for the specific container we're recording into. 59 UniquePtr<ContainerWriter> mWriter; 60 // True once metadata has been set in the muxer. 61 bool mMetadataSet = false; 62 // True once metadata has been written to file. 63 bool mMetadataEncoded = false; 64 // True if metadata is set and contains an audio track. 65 bool mHasAudio = false; 66 // True if metadata is set and contains a video track. 67 bool mHasVideo = false; 68 }; 69 } // namespace mozilla 70 71 #endif