1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef WEBMWRITER_H_ 18 #define WEBMWRITER_H_ 19 20 #include "WebmConstants.h" 21 #include "WebmFrameThread.h" 22 #include "LinkedBlockingQueue.h" 23 24 #include <media/stagefright/MediaSource.h> 25 #include <media/stagefright/MediaWriter.h> 26 27 #include <utils/Errors.h> 28 #include <utils/Mutex.h> 29 #include <utils/StrongPointer.h> 30 31 #include <stdint.h> 32 33 using namespace webm; 34 35 namespace android { 36 37 class WebmWriter : public MediaWriter { 38 public: 39 // Returns true if the file descriptor is opened using a mode 40 // which is compatible with WebmWriter. 41 // Note that this overloads that method in the base class. 42 static bool isFdOpenModeValid(int fd); 43 explicit WebmWriter(int fd); ~WebmWriter()44 ~WebmWriter() { reset(); } 45 46 47 virtual status_t addSource(const sp<MediaSource> &source); 48 virtual status_t start(MetaData *param = NULL); 49 virtual status_t stop(); 50 virtual status_t pause(); 51 virtual bool reachedEOS(); 52 setStartTimeOffsetMs(int ms)53 virtual void setStartTimeOffsetMs(int ms) { mStartTimeOffsetMs = ms; } getStartTimeOffsetMs()54 virtual int32_t getStartTimeOffsetMs() const { return mStartTimeOffsetMs; } 55 56 private: 57 int mFd; 58 status_t mInitCheck; 59 60 uint64_t mTimeCodeScale; 61 int64_t mStartTimestampUs; 62 int32_t mStartTimeOffsetMs; 63 64 uint64_t mSegmentOffset; 65 uint64_t mSegmentDataStart; 66 uint64_t mInfoOffset; 67 uint64_t mInfoSize; 68 uint64_t mTracksOffset; 69 uint64_t mCuesOffset; 70 71 bool mPaused; 72 bool mStarted; 73 bool mIsFileSizeLimitExplicitlyRequested; 74 bool mIsRealTimeRecording; 75 bool mStreamableFile; 76 uint64_t mEstimatedCuesSize; 77 78 Mutex mLock; 79 List<sp<WebmElement> > mCuePoints; 80 81 enum { 82 kAudioIndex = 0, 83 kVideoIndex = 1, 84 kMaxStreams = 2, 85 }; 86 87 struct WebmStream { 88 int mType; 89 const char *mName; 90 sp<WebmElement> (*mMakeTrack)(const sp<MetaData>&); 91 92 sp<MediaSource> mSource; 93 sp<WebmElement> mTrackEntry; 94 sp<WebmFrameSourceThread> mThread; 95 LinkedBlockingQueue<const sp<WebmFrame> > mSink; 96 WebmStreamWebmStream97 WebmStream() 98 : mType(kInvalidType), 99 mName("Invalid"), 100 mMakeTrack(NULL) { 101 } 102 WebmStreamWebmStream103 WebmStream(int type, const char *name, sp<WebmElement> (*makeTrack)(const sp<MetaData>&)) 104 : mType(type), 105 mName(name), 106 mMakeTrack(makeTrack) { 107 } 108 109 WebmStream &operator=(const WebmStream &other) { 110 mType = other.mType; 111 mName = other.mName; 112 mMakeTrack = other.mMakeTrack; 113 return *this; 114 } 115 }; 116 WebmStream mStreams[kMaxStreams]; 117 Vector<sp<WebmElement>> mStreamsInOrder; 118 119 sp<WebmFrameSinkThread> mSinkThread; 120 121 size_t numTracks(); 122 uint64_t estimateCuesSize(int32_t bitRate); 123 void initStream(size_t idx); 124 void release(); 125 status_t reset(); 126 127 static sp<WebmElement> videoTrack(const sp<MetaData>& md); 128 static sp<WebmElement> audioTrack(const sp<MetaData>& md); 129 130 DISALLOW_EVIL_CONSTRUCTORS(WebmWriter); 131 }; 132 133 } /* namespace android */ 134 #endif /* WEBMWRITER_H_ */ 135