content/media/encoder/fmp4_muxer/ISOMediaWriter.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/encoder/fmp4_muxer/ISOMediaWriter.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,103 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef ISOMediaWriter_h_
    1.10 +#define ISOMediaWriter_h_
    1.11 +
    1.12 +#include "ContainerWriter.h"
    1.13 +#include "nsIRunnable.h"
    1.14 +
    1.15 +namespace mozilla {
    1.16 +
    1.17 +class ISOControl;
    1.18 +class FragmentBuffer;
    1.19 +class ISOMediaWriterRunnable;
    1.20 +
    1.21 +class ISOMediaWriter : public ContainerWriter
    1.22 +{
    1.23 +public:
    1.24 +  // Generate an fragmented MP4 stream, ISO/IEC 14496-12.
    1.25 +  // Brand names in 'ftyp' box are 'isom' and 'mp42'.
    1.26 +  const static uint32_t TYPE_FRAG_MP4 = 1 << 0;
    1.27 +
    1.28 +  // Generate an fragmented 3GP stream, 3GPP TS 26.244,
    1.29 +  // '5.4.3 Basic profile'.
    1.30 +  // Brand names in 'ftyp' box are '3gp9' and 'isom'.
    1.31 +  const static uint32_t TYPE_FRAG_3GP = 1 << 1;
    1.32 +
    1.33 +  // aType is the combination of CREATE_AUDIO_TRACK and CREATE_VIDEO_TRACK.
    1.34 +  // It is a hint to muxer that the output streaming contains audio, video
    1.35 +  // or both.
    1.36 +  //
    1.37 +  // aHint is one of the value in TYPE_XXXXXXXX. It is a hint to muxer what kind
    1.38 +  // of ISO format should be generated.
    1.39 +  ISOMediaWriter(uint32_t aType, uint32_t aHint = TYPE_FRAG_MP4);
    1.40 +  ~ISOMediaWriter();
    1.41 +
    1.42 +  // ContainerWriter methods
    1.43 +  nsresult WriteEncodedTrack(const EncodedFrameContainer &aData,
    1.44 +                             uint32_t aFlags = 0) MOZ_OVERRIDE;
    1.45 +
    1.46 +  nsresult GetContainerData(nsTArray<nsTArray<uint8_t>>* aOutputBufs,
    1.47 +                            uint32_t aFlags = 0) MOZ_OVERRIDE;
    1.48 +
    1.49 +  nsresult SetMetadata(TrackMetadataBase* aMetadata) MOZ_OVERRIDE;
    1.50 +
    1.51 +protected:
    1.52 +  /**
    1.53 +   * The state of each state will generate one or more blob.
    1.54 +   * Each blob will be a moov, moof, moof... until receiving EOS.
    1.55 +   * The generated sequence is:
    1.56 +   *
    1.57 +   *   moov -> moof -> moof -> ... -> moof -> moof
    1.58 +   *
    1.59 +   * Following is the details of each state.
    1.60 +   *   MUXING_HEAD:
    1.61 +   *     It collects the metadata to generate a moov. The state transits to
    1.62 +   *     MUXING_HEAD after output moov blob.
    1.63 +   *
    1.64 +   *   MUXING_FRAG:
    1.65 +   *     It collects enough audio/video data to generate a fragment blob. This
    1.66 +   *     will be repeated until END_OF_STREAM and then transiting to MUXING_DONE.
    1.67 +   *
    1.68 +   *   MUXING_DONE:
    1.69 +   *     End of ISOMediaWriter life cycle.
    1.70 +   */
    1.71 +  enum MuxState {
    1.72 +    MUXING_HEAD,
    1.73 +    MUXING_FRAG,
    1.74 +    MUXING_DONE,
    1.75 +  };
    1.76 +
    1.77 +private:
    1.78 +  nsresult RunState();
    1.79 +
    1.80 +  // True if one of following conditions hold:
    1.81 +  //   1. Audio/Video accumulates enough data to generate a moof.
    1.82 +  //   2. Get EOS signal.
    1.83 +  //   aEOS will be assigned to true if it gets EOS signal.
    1.84 +  bool ReadyToRunState(bool& aEOS);
    1.85 +
    1.86 +  // The main class to generate and iso box. Its life time is same as
    1.87 +  // ISOMediaWriter and deleted only if ISOMediaWriter is destroyed.
    1.88 +  nsAutoPtr<ISOControl> mControl;
    1.89 +
    1.90 +  // Buffers to keep audio/video data frames, they are created when metadata is
    1.91 +  // received. Only one instance for each media type is allowed and they will be
    1.92 +  // deleted only if ISOMediaWriter is destroyed.
    1.93 +  nsAutoPtr<FragmentBuffer> mAudioFragmentBuffer;
    1.94 +  nsAutoPtr<FragmentBuffer> mVideoFragmentBuffer;
    1.95 +
    1.96 +  MuxState mState;
    1.97 +
    1.98 +  // A flag to indicate the output buffer is ready to blob out.
    1.99 +  bool mBlobReady;
   1.100 +
   1.101 +  // Combination of Audio_Track or Video_Track.
   1.102 +  uint32_t mType;
   1.103 +};
   1.104 +
   1.105 +} // namespace mozilla
   1.106 +#endif // ISOMediaWriter_h_

mercurial