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 ContainerWriter_h_
7 #define ContainerWriter_h_
8 
9 #include "nsTArray.h"
10 #include "EncodedFrame.h"
11 #include "TrackMetadataBase.h"
12 
13 namespace mozilla {
14 /**
15  * ContainerWriter packs encoded track data into a specific media container.
16  */
17 class ContainerWriter {
18  public:
ContainerWriter()19   ContainerWriter() : mInitialized(false), mIsWritingComplete(false) {}
~ContainerWriter()20   virtual ~ContainerWriter() {}
21   // Mapping to DOMMediaStream::TrackTypeHints
22   enum {
23     CREATE_AUDIO_TRACK = 1 << 0,
24     CREATE_VIDEO_TRACK = 1 << 1,
25   };
26   enum { END_OF_STREAM = 1 << 0 };
27 
28   /**
29    * Writes encoded track data from aData into the internal stream of container
30    * writer. aFlags is used to signal the impl of different conditions
31    * such as END_OF_STREAM. Each impl may handle different flags, and should be
32    * documented accordingly. Currently, WriteEncodedTrack doesn't support
33    * explicit track specification, though each impl may provide logic to
34    * allocate frames into different tracks.
35    */
36   virtual nsresult WriteEncodedTrack(
37       const nsTArray<RefPtr<EncodedFrame>>& aData, uint32_t aFlags = 0) = 0;
38 
39   /**
40    * Stores the metadata for all given tracks to the muxer.
41    *
42    * This method checks the integrity of aMetadata.
43    * If the metadata isn't well formatted, this method returns NS_ERROR_FAILURE.
44    * If the metadata is well formatted, it stores the metadata and returns
45    * NS_OK.
46    */
47   virtual nsresult SetMetadata(
48       const nsTArray<RefPtr<TrackMetadataBase>>& aMetadata) = 0;
49 
50   /**
51    * Indicate if the writer has finished to output data
52    */
IsWritingComplete()53   virtual bool IsWritingComplete() { return mIsWritingComplete; }
54 
55   enum { FLUSH_NEEDED = 1 << 0, GET_HEADER = 1 << 1 };
56 
57   /**
58    * Copies the final container data to a buffer if it has accumulated enough
59    * packets from WriteEncodedTrack. This buffer of data is appended to
60    * aOutputBufs, and existing elements of aOutputBufs should not be modified.
61    * aFlags is true with FLUSH_NEEDED will force OggWriter to flush an ogg page
62    * even it is not full, and copy these container data to a buffer for
63    * aOutputBufs to append.
64    */
65   virtual nsresult GetContainerData(nsTArray<nsTArray<uint8_t>>* aOutputBufs,
66                                     uint32_t aFlags = 0) = 0;
67 
68  protected:
69   bool mInitialized;
70   bool mIsWritingComplete;
71 };
72 
73 }  // namespace mozilla
74 
75 #endif
76