|
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 ISOMediaWriter_h_ |
|
7 #define ISOMediaWriter_h_ |
|
8 |
|
9 #include "ContainerWriter.h" |
|
10 #include "nsIRunnable.h" |
|
11 |
|
12 namespace mozilla { |
|
13 |
|
14 class ISOControl; |
|
15 class FragmentBuffer; |
|
16 class ISOMediaWriterRunnable; |
|
17 |
|
18 class ISOMediaWriter : public ContainerWriter |
|
19 { |
|
20 public: |
|
21 // Generate an fragmented MP4 stream, ISO/IEC 14496-12. |
|
22 // Brand names in 'ftyp' box are 'isom' and 'mp42'. |
|
23 const static uint32_t TYPE_FRAG_MP4 = 1 << 0; |
|
24 |
|
25 // Generate an fragmented 3GP stream, 3GPP TS 26.244, |
|
26 // '5.4.3 Basic profile'. |
|
27 // Brand names in 'ftyp' box are '3gp9' and 'isom'. |
|
28 const static uint32_t TYPE_FRAG_3GP = 1 << 1; |
|
29 |
|
30 // aType is the combination of CREATE_AUDIO_TRACK and CREATE_VIDEO_TRACK. |
|
31 // It is a hint to muxer that the output streaming contains audio, video |
|
32 // or both. |
|
33 // |
|
34 // aHint is one of the value in TYPE_XXXXXXXX. It is a hint to muxer what kind |
|
35 // of ISO format should be generated. |
|
36 ISOMediaWriter(uint32_t aType, uint32_t aHint = TYPE_FRAG_MP4); |
|
37 ~ISOMediaWriter(); |
|
38 |
|
39 // ContainerWriter methods |
|
40 nsresult WriteEncodedTrack(const EncodedFrameContainer &aData, |
|
41 uint32_t aFlags = 0) MOZ_OVERRIDE; |
|
42 |
|
43 nsresult GetContainerData(nsTArray<nsTArray<uint8_t>>* aOutputBufs, |
|
44 uint32_t aFlags = 0) MOZ_OVERRIDE; |
|
45 |
|
46 nsresult SetMetadata(TrackMetadataBase* aMetadata) MOZ_OVERRIDE; |
|
47 |
|
48 protected: |
|
49 /** |
|
50 * The state of each state will generate one or more blob. |
|
51 * Each blob will be a moov, moof, moof... until receiving EOS. |
|
52 * The generated sequence is: |
|
53 * |
|
54 * moov -> moof -> moof -> ... -> moof -> moof |
|
55 * |
|
56 * Following is the details of each state. |
|
57 * MUXING_HEAD: |
|
58 * It collects the metadata to generate a moov. The state transits to |
|
59 * MUXING_HEAD after output moov blob. |
|
60 * |
|
61 * MUXING_FRAG: |
|
62 * It collects enough audio/video data to generate a fragment blob. This |
|
63 * will be repeated until END_OF_STREAM and then transiting to MUXING_DONE. |
|
64 * |
|
65 * MUXING_DONE: |
|
66 * End of ISOMediaWriter life cycle. |
|
67 */ |
|
68 enum MuxState { |
|
69 MUXING_HEAD, |
|
70 MUXING_FRAG, |
|
71 MUXING_DONE, |
|
72 }; |
|
73 |
|
74 private: |
|
75 nsresult RunState(); |
|
76 |
|
77 // True if one of following conditions hold: |
|
78 // 1. Audio/Video accumulates enough data to generate a moof. |
|
79 // 2. Get EOS signal. |
|
80 // aEOS will be assigned to true if it gets EOS signal. |
|
81 bool ReadyToRunState(bool& aEOS); |
|
82 |
|
83 // The main class to generate and iso box. Its life time is same as |
|
84 // ISOMediaWriter and deleted only if ISOMediaWriter is destroyed. |
|
85 nsAutoPtr<ISOControl> mControl; |
|
86 |
|
87 // Buffers to keep audio/video data frames, they are created when metadata is |
|
88 // received. Only one instance for each media type is allowed and they will be |
|
89 // deleted only if ISOMediaWriter is destroyed. |
|
90 nsAutoPtr<FragmentBuffer> mAudioFragmentBuffer; |
|
91 nsAutoPtr<FragmentBuffer> mVideoFragmentBuffer; |
|
92 |
|
93 MuxState mState; |
|
94 |
|
95 // A flag to indicate the output buffer is ready to blob out. |
|
96 bool mBlobReady; |
|
97 |
|
98 // Combination of Audio_Track or Video_Track. |
|
99 uint32_t mType; |
|
100 }; |
|
101 |
|
102 } // namespace mozilla |
|
103 #endif // ISOMediaWriter_h_ |