• 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 MEDIA_MUXER_BASE_H_
18 #define MEDIA_MUXER_BASE_H_
19 
20 #include <utils/RefBase.h>
21 #include "media/stagefright/foundation/ABase.h"
22 
23 namespace android {
24 
25 struct ABuffer;
26 struct AMessage;
27 
28 // MediaMuxer is used to mux multiple tracks into a video. Currently, we only
29 // support a mp4 file as the output.
30 // The expected calling order of the functions is:
31 // Constructor -> addTrack+ -> start -> writeSampleData+ -> stop
32 // If muxing operation need to be cancelled, the app is responsible for
33 // deleting the output file after stop.
34 struct MediaMuxerBase : public RefBase {
35 public:
36     // Please update media/java/android/media/MediaMuxer.java if the
37     // OutputFormat is updated.
38     enum OutputFormat {
39         OUTPUT_FORMAT_MPEG_4      = 0,
40         OUTPUT_FORMAT_WEBM        = 1,
41         OUTPUT_FORMAT_THREE_GPP   = 2,
42         OUTPUT_FORMAT_HEIF        = 3,
43         OUTPUT_FORMAT_OGG         = 4,
44         OUTPUT_FORMAT_LIST_END // must be last - used to validate format type
45     };
46 
47     // Construct the muxer with the file descriptor. Note that the MediaMuxer
48     // will close this file at stop().
MediaMuxerBaseMediaMuxerBase49     MediaMuxerBase() {};
50 
~MediaMuxerBaseMediaMuxerBase51     virtual ~MediaMuxerBase() {};
52 
53     /**
54      * Add a track with its format information. This should be
55      * called before start().
56      * @param format the track's format.
57      * @return the track's index or negative number if error.
58      */
59     virtual ssize_t addTrack(const sp<AMessage> &format) = 0;
60 
61     /**
62      * Start muxing. Make sure all the tracks have been added before
63      * calling this.
64      */
65     virtual status_t start() = 0;
66 
67     /**
68      * Set the orientation hint.
69      * @param degrees The rotation degrees. It has to be either 0,
70      *                90, 180 or 270.
71      * @return OK if no error.
72      */
73     virtual status_t setOrientationHint(int degrees) = 0;
74 
75     /**
76      * Set the location.
77      * @param latitude The latitude in degree x 1000. Its value must be in the range
78      * [-900000, 900000].
79      * @param longitude The longitude in degree x 1000. Its value must be in the range
80      * [-1800000, 1800000].
81      * @return OK if no error.
82      */
83     virtual status_t setLocation(int latitude, int longitude) = 0;
84 
85     /**
86      * Stop muxing.
87      * This method is a blocking call. Depending on how
88      * much data is bufferred internally, the time needed for stopping
89      * the muxer may be time consuming. UI thread is
90      * not recommended for launching this call.
91      * @return OK if no error.
92      */
93     virtual status_t stop() = 0;
94 
95     /**
96      * Send a sample buffer for muxing.
97      * The buffer can be reused once this method returns. Typically,
98      * this function won't be blocked for very long, and thus there
99      * is no need to use a separate thread calling this method to
100      * push a buffer.
101      * @param buffer the incoming sample buffer.
102      * @param trackIndex the buffer's track index number.
103      * @param timeUs the buffer's time stamp.
104      * @param flags the only supported flag for now is
105      *              MediaCodec::BUFFER_FLAG_SYNCFRAME.
106      * @return OK if no error.
107      */
108     virtual status_t writeSampleData(const sp<ABuffer> &buffer, size_t trackIndex,
109                              int64_t timeUs, uint32_t flags) = 0 ;
110 
111     /**
112      * Gets the number of tracks added successfully.  Should be called in
113      * INITIALIZED(after constructor) or STARTED(after start()) state.
114      * @return the number of tracks or -1 in wrong state.
115      */
116     virtual ssize_t getTrackCount() = 0;
117 
118     /**
119      * Gets the format of the track by their index.
120      * @param idx : index of the track whose format is wanted.
121      * @return smart pointer to AMessage containing the format details.
122      */
123     virtual sp<AMessage> getTrackFormat(size_t idx) = 0;
124 
125 private:
126 
127     DISALLOW_EVIL_CONSTRUCTORS(MediaMuxerBase);
128 };
129 
130 }  // namespace android
131 
132 #endif  // MEDIA_MUXER_BASE_H_
133 
134