Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 4 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "ISOMediaWriter.h" |
michael@0 | 7 | #include "ISOControl.h" |
michael@0 | 8 | #include "ISOMediaBoxes.h" |
michael@0 | 9 | #include "ISOTrackMetadata.h" |
michael@0 | 10 | #include "nsThreadUtils.h" |
michael@0 | 11 | #include "MediaEncoder.h" |
michael@0 | 12 | #include "VideoUtils.h" |
michael@0 | 13 | |
michael@0 | 14 | #undef LOG |
michael@0 | 15 | #ifdef MOZ_WIDGET_GONK |
michael@0 | 16 | #include <android/log.h> |
michael@0 | 17 | #define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "MediaEncoder", ## args); |
michael@0 | 18 | #else |
michael@0 | 19 | #define LOG(args, ...) |
michael@0 | 20 | #endif |
michael@0 | 21 | |
michael@0 | 22 | namespace mozilla { |
michael@0 | 23 | |
michael@0 | 24 | const static uint32_t FRAG_DURATION = 2 * USECS_PER_S; // microsecond per unit |
michael@0 | 25 | |
michael@0 | 26 | ISOMediaWriter::ISOMediaWriter(uint32_t aType, uint32_t aHint) |
michael@0 | 27 | : ContainerWriter() |
michael@0 | 28 | , mState(MUXING_HEAD) |
michael@0 | 29 | , mBlobReady(false) |
michael@0 | 30 | , mType(0) |
michael@0 | 31 | { |
michael@0 | 32 | if (aType & CREATE_AUDIO_TRACK) { |
michael@0 | 33 | mType |= Audio_Track; |
michael@0 | 34 | } |
michael@0 | 35 | if (aType & CREATE_VIDEO_TRACK) { |
michael@0 | 36 | mType |= Video_Track; |
michael@0 | 37 | } |
michael@0 | 38 | mControl = new ISOControl(aHint); |
michael@0 | 39 | MOZ_COUNT_CTOR(ISOMediaWriter); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | ISOMediaWriter::~ISOMediaWriter() |
michael@0 | 43 | { |
michael@0 | 44 | MOZ_COUNT_DTOR(ISOMediaWriter); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | nsresult |
michael@0 | 48 | ISOMediaWriter::RunState() |
michael@0 | 49 | { |
michael@0 | 50 | nsresult rv; |
michael@0 | 51 | switch (mState) { |
michael@0 | 52 | case MUXING_HEAD: |
michael@0 | 53 | { |
michael@0 | 54 | rv = mControl->GenerateFtyp(); |
michael@0 | 55 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 56 | rv = mControl->GenerateMoov(); |
michael@0 | 57 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 58 | mState = MUXING_FRAG; |
michael@0 | 59 | break; |
michael@0 | 60 | } |
michael@0 | 61 | case MUXING_FRAG: |
michael@0 | 62 | { |
michael@0 | 63 | rv = mControl->GenerateMoof(mType); |
michael@0 | 64 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 65 | |
michael@0 | 66 | bool EOS; |
michael@0 | 67 | if (ReadyToRunState(EOS) && EOS) { |
michael@0 | 68 | mState = MUXING_DONE; |
michael@0 | 69 | } |
michael@0 | 70 | break; |
michael@0 | 71 | } |
michael@0 | 72 | case MUXING_DONE: |
michael@0 | 73 | { |
michael@0 | 74 | break; |
michael@0 | 75 | } |
michael@0 | 76 | } |
michael@0 | 77 | mBlobReady = true; |
michael@0 | 78 | return NS_OK; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | nsresult |
michael@0 | 82 | ISOMediaWriter::WriteEncodedTrack(const EncodedFrameContainer& aData, |
michael@0 | 83 | uint32_t aFlags) |
michael@0 | 84 | { |
michael@0 | 85 | // Muxing complete, it doesn't allowed to reentry again. |
michael@0 | 86 | if (mState == MUXING_DONE) { |
michael@0 | 87 | MOZ_ASSERT(false); |
michael@0 | 88 | return NS_ERROR_FAILURE; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | FragmentBuffer* frag = nullptr; |
michael@0 | 92 | uint32_t len = aData.GetEncodedFrames().Length(); |
michael@0 | 93 | |
michael@0 | 94 | if (!len) { |
michael@0 | 95 | // no frame? why bother to WriteEncodedTrack |
michael@0 | 96 | return NS_OK; |
michael@0 | 97 | } |
michael@0 | 98 | for (uint32_t i = 0; i < len; i++) { |
michael@0 | 99 | nsRefPtr<EncodedFrame> frame(aData.GetEncodedFrames()[i]); |
michael@0 | 100 | EncodedFrame::FrameType type = frame->GetFrameType(); |
michael@0 | 101 | if (type == EncodedFrame::AAC_AUDIO_FRAME || |
michael@0 | 102 | type == EncodedFrame::AAC_CSD || |
michael@0 | 103 | type == EncodedFrame::AMR_AUDIO_FRAME || |
michael@0 | 104 | type == EncodedFrame::AMR_AUDIO_CSD) { |
michael@0 | 105 | frag = mAudioFragmentBuffer; |
michael@0 | 106 | } else if (type == EncodedFrame::AVC_I_FRAME || |
michael@0 | 107 | type == EncodedFrame::AVC_P_FRAME || |
michael@0 | 108 | type == EncodedFrame::AVC_B_FRAME || |
michael@0 | 109 | type == EncodedFrame::AVC_CSD) { |
michael@0 | 110 | frag = mVideoFragmentBuffer; |
michael@0 | 111 | } else { |
michael@0 | 112 | MOZ_ASSERT(0); |
michael@0 | 113 | return NS_ERROR_FAILURE; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | frag->AddFrame(frame); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | // Encoder should send CSD (codec specific data) frame before sending the |
michael@0 | 120 | // audio/video frames. When CSD data is ready, it is sufficient to generate a |
michael@0 | 121 | // moov data. If encoder doesn't send CSD yet, muxer needs to wait before |
michael@0 | 122 | // generating anything. |
michael@0 | 123 | if (mType & Audio_Track && (!mAudioFragmentBuffer || |
michael@0 | 124 | !mAudioFragmentBuffer->HasCSD())) { |
michael@0 | 125 | return NS_OK; |
michael@0 | 126 | } |
michael@0 | 127 | if (mType & Video_Track && (!mVideoFragmentBuffer || |
michael@0 | 128 | !mVideoFragmentBuffer->HasCSD())) { |
michael@0 | 129 | return NS_OK; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | // Only one FrameType in EncodedFrameContainer so it doesn't need to be |
michael@0 | 133 | // inside the for-loop. |
michael@0 | 134 | if (frag && (aFlags & END_OF_STREAM)) { |
michael@0 | 135 | frag->SetEndOfStream(); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | nsresult rv; |
michael@0 | 139 | bool EOS; |
michael@0 | 140 | if (ReadyToRunState(EOS)) { |
michael@0 | 141 | // TODO: |
michael@0 | 142 | // The MediaEncoder doesn't use nsRunnable, so thread will be |
michael@0 | 143 | // stocked on that part and the new added nsRunnable won't get to run |
michael@0 | 144 | // before MediaEncoder completing. Before MediaEncoder change, it needs |
michael@0 | 145 | // to call RunState directly. |
michael@0 | 146 | // https://bugzilla.mozilla.org/show_bug.cgi?id=950429 |
michael@0 | 147 | rv = RunState(); |
michael@0 | 148 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | return NS_OK; |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | bool |
michael@0 | 155 | ISOMediaWriter::ReadyToRunState(bool& aEOS) |
michael@0 | 156 | { |
michael@0 | 157 | aEOS = false; |
michael@0 | 158 | bool bReadyToMux = true; |
michael@0 | 159 | if ((mType & Audio_Track) && (mType & Video_Track)) { |
michael@0 | 160 | if (!mAudioFragmentBuffer->HasEnoughData()) { |
michael@0 | 161 | bReadyToMux = false; |
michael@0 | 162 | } |
michael@0 | 163 | if (!mVideoFragmentBuffer->HasEnoughData()) { |
michael@0 | 164 | bReadyToMux = false; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | if (mAudioFragmentBuffer->EOS() && mVideoFragmentBuffer->EOS()) { |
michael@0 | 168 | aEOS = true; |
michael@0 | 169 | bReadyToMux = true; |
michael@0 | 170 | } |
michael@0 | 171 | } else if (mType == Audio_Track) { |
michael@0 | 172 | if (!mAudioFragmentBuffer->HasEnoughData()) { |
michael@0 | 173 | bReadyToMux = false; |
michael@0 | 174 | } |
michael@0 | 175 | if (mAudioFragmentBuffer->EOS()) { |
michael@0 | 176 | aEOS = true; |
michael@0 | 177 | bReadyToMux = true; |
michael@0 | 178 | } |
michael@0 | 179 | } else if (mType == Video_Track) { |
michael@0 | 180 | if (!mVideoFragmentBuffer->HasEnoughData()) { |
michael@0 | 181 | bReadyToMux = false; |
michael@0 | 182 | } |
michael@0 | 183 | if (mVideoFragmentBuffer->EOS()) { |
michael@0 | 184 | aEOS = true; |
michael@0 | 185 | bReadyToMux = true; |
michael@0 | 186 | } |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | return bReadyToMux; |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | nsresult |
michael@0 | 193 | ISOMediaWriter::GetContainerData(nsTArray<nsTArray<uint8_t>>* aOutputBufs, |
michael@0 | 194 | uint32_t aFlags) |
michael@0 | 195 | { |
michael@0 | 196 | if (mBlobReady) { |
michael@0 | 197 | if (mState == MUXING_DONE) { |
michael@0 | 198 | mIsWritingComplete = true; |
michael@0 | 199 | } |
michael@0 | 200 | mBlobReady = false; |
michael@0 | 201 | return mControl->GetBufs(aOutputBufs); |
michael@0 | 202 | } |
michael@0 | 203 | return NS_OK; |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | nsresult |
michael@0 | 207 | ISOMediaWriter::SetMetadata(TrackMetadataBase* aMetadata) |
michael@0 | 208 | { |
michael@0 | 209 | if (aMetadata->GetKind() == TrackMetadataBase::METADATA_AAC || |
michael@0 | 210 | aMetadata->GetKind() == TrackMetadataBase::METADATA_AMR) { |
michael@0 | 211 | mControl->SetMetadata(aMetadata); |
michael@0 | 212 | mAudioFragmentBuffer = new FragmentBuffer(Audio_Track, FRAG_DURATION); |
michael@0 | 213 | mControl->SetFragment(mAudioFragmentBuffer); |
michael@0 | 214 | return NS_OK; |
michael@0 | 215 | } |
michael@0 | 216 | if (aMetadata->GetKind() == TrackMetadataBase::METADATA_AVC) { |
michael@0 | 217 | mControl->SetMetadata(aMetadata); |
michael@0 | 218 | mVideoFragmentBuffer = new FragmentBuffer(Video_Track, FRAG_DURATION); |
michael@0 | 219 | mControl->SetFragment(mVideoFragmentBuffer); |
michael@0 | 220 | return NS_OK; |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | return NS_ERROR_FAILURE; |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | } // namespace mozilla |