• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 ANDROID_MEDIA_APPENDER_H
18 #define ANDROID_MEDIA_APPENDER_H
19 
20 #include <media/stagefright/MediaMuxer.h>
21 #include <media/stagefright/NuMediaExtractor.h>
22 #include <stack>
23 
24 namespace android {
25 
26 struct MediaAppender : public MediaMuxerBase {
27 public:
28     enum AppendMode {
29         APPEND_MODE_FIRST = 0,
30         APPEND_MODE_IGNORE_LAST_VIDEO_GOP = APPEND_MODE_FIRST,
31         APPEND_MODE_ADD_TO_EXISTING_DATA = 1,
32         APPEND_MODE_LAST = APPEND_MODE_ADD_TO_EXISTING_DATA,
33     };
34 
35     static sp<MediaAppender> create(int fd, AppendMode mode);
36 
37     virtual ~MediaAppender();
38 
39     status_t init();
40 
41     status_t start();
42 
43     status_t stop();
44 
45     status_t writeSampleData(const sp<ABuffer>& buffer, size_t trackIndex, int64_t timeUs,
46                              uint32_t flags);
47 
48     status_t setOrientationHint(int degrees);
49 
50     status_t setLocation(int latitude, int longitude);
51 
52     ssize_t addTrack(const sp<AMessage> &format);
53 
54     ssize_t getTrackCount();
55 
56     sp<AMessage> getTrackFormat(size_t idx);
57 
58 private:
59     MediaAppender(int fd, AppendMode mode);
60 
61     int mFd;
62     MediaMuxer::OutputFormat mFormat;
63     AppendMode mMode;
64     sp<NuMediaExtractor> mExtractor;
65     sp<MediaMuxer> mMuxer;
66     size_t mTrackCount;
67     // Map track index given by extractor to the ones received from muxer.
68     std::map<size_t, ssize_t> mTrackIndexMap;
69     // Count of the samples in each track, indexed by extractor track ids.
70     std::vector<size_t> mSampleCountVect;
71     // Extractor track index of samples.
72     std::vector<size_t> mSampleIndexVect;
73     // Track format indexed by extractor track ids.
74     std::map<size_t, sp<AMessage>> mFmtIndexMap;
75     // Size of samples.
76     std::vector<size_t> mSampleSizeVect;
77     // Presentation time stamp of samples.
78     std::vector<int64_t> mSampleTimeVect;
79     // Timestamp of last sample of tracks.
80     std::vector<int64_t> mMaxTimestampVect;
81     // Metadata of samples.
82     std::vector<sp<MetaData>> mSampleMetaVect;
83     std::mutex mMutex;
84     // Timestamp of the last sync sample of tracks.
85     std::vector<int64_t> mLastSyncSampleTimeVect;
86 
87     struct sampleDataInfo;
88     std::vector<sampleDataInfo> mSDI;
89 
90     enum : int {
91         UNINITIALIZED,
92         INITIALIZED,
93         STARTED,
94         STOPPED,
95         ERROR,
96     } mState GUARDED_BY(mMutex);
97 };
98 
99 }  // namespace android
100 #endif  // ANDROID_MEDIA_APPENDER_H