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 /**
18  * @addtogroup Media
19  * @{
20  */
21 
22 /**
23  * @file NdkMediaMuxer.h
24  */
25 
26 /*
27  * This file defines an NDK API.
28  * Do not remove methods.
29  * Do not change method signatures.
30  * Do not change the value of constants.
31  * Do not change the size of any of the classes defined in here.
32  * Do not reference types that are not part of the NDK.
33  * Do not #include files that aren't part of the NDK.
34  */
35 
36 #ifndef _NDK_MEDIA_MUXER_H
37 #define _NDK_MEDIA_MUXER_H
38 
39 #include <sys/cdefs.h>
40 #include <sys/types.h>
41 
42 #include "NdkMediaCodec.h"
43 #include "NdkMediaError.h"
44 #include "NdkMediaFormat.h"
45 
46 __BEGIN_DECLS
47 
48 struct AMediaMuxer;
49 typedef struct AMediaMuxer AMediaMuxer;
50 
51 /**
52  * Defines the output format. These constants are used with constructor.
53  *
54  * These enums match the ones used in {@link android.media.MediaMuxer.OutputFormat}
55  */
56 typedef enum {
57     /** MPEG4 media file format*/
58     AMEDIAMUXER_OUTPUT_FORMAT_MPEG_4 = 0,
59     /** WEBM media file format*/
60     AMEDIAMUXER_OUTPUT_FORMAT_WEBM = 1,
61     /** 3GPP media file format*/
62     AMEDIAMUXER_OUTPUT_FORMAT_THREE_GPP = 2,
63     /** HEIF media file format*/
64     AMEDIAMUXER_OUTPUT_FORMAT_HEIF = 3,  // introduced in API 34
65     /** Ogg media file format*/
66     AMEDIAMUXER_OUTPUT_FORMAT_OGG = 4,  // introduced in API 34
67 } OutputFormat;
68 
69 typedef enum {
70     /* Last group of pictures(GOP) of video track can be incomplete, so it would be safe to
71      * scrap that and rewrite.  If both audio and video tracks are present in a file, then
72      * samples of audio track after last GOP of video would be scrapped too.
73      * If only audio track is present, then no sample would be discarded.
74      */
75     AMEDIAMUXER_APPEND_IGNORE_LAST_VIDEO_GOP = 0,
76     // Keep all existing samples as it is and append new samples after that only.
77     AMEDIAMUXER_APPEND_TO_EXISTING_DATA = 1,
78 } AppendMode;
79 
80 /**
81  * Create new media muxer.
82  *
83  * Available since API level 21.
84  */
85 AMediaMuxer* AMediaMuxer_new(int fd, OutputFormat format) __INTRODUCED_IN(21);
86 
87 /**
88  * Delete a previously created media muxer.
89  *
90  * Available since API level 21.
91  */
92 media_status_t AMediaMuxer_delete(AMediaMuxer*) __INTRODUCED_IN(21);
93 
94 /**
95  * Set and store the geodata (latitude and longitude) in the output file.
96  * This method should be called before AMediaMuxer_start. The geodata is stored
97  * in udta box if the output format is AMEDIAMUXER_OUTPUT_FORMAT_MPEG_4, and is
98  * ignored for other output formats.
99  * The geodata is stored according to ISO-6709 standard.
100  *
101  * Both values are specified in degrees.
102  * Latitude must be in the range [-90, 90].
103  * Longitude must be in the range [-180, 180].
104  *
105  * Available since API level 21.
106  */
107 media_status_t AMediaMuxer_setLocation(AMediaMuxer*,
108         float latitude, float longitude) __INTRODUCED_IN(21);
109 
110 /**
111  * Sets the orientation hint for output video playback.
112  * This method should be called before AMediaMuxer_start. Calling this
113  * method will not rotate the video frame when muxer is generating the file,
114  * but add a composition matrix containing the rotation angle in the output
115  * video if the output format is AMEDIAMUXER_OUTPUT_FORMAT_MPEG_4, so that a
116  * video player can choose the proper orientation for playback.
117  * Note that some video players may choose to ignore the composition matrix
118  * during playback.
119  * The angle is specified in degrees, clockwise.
120  * The supported angles are 0, 90, 180, and 270 degrees.
121  *
122  * Available since API level 21.
123  */
124 media_status_t AMediaMuxer_setOrientationHint(AMediaMuxer*, int degrees) __INTRODUCED_IN(21);
125 
126 /**
127  * Adds a track with the specified format.
128  * Returns the index of the new track or a negative value in case of failure,
129  * which can be interpreted as a media_status_t.
130  *
131  * Available since API level 21.
132  */
133 ssize_t AMediaMuxer_addTrack(AMediaMuxer*, const AMediaFormat* format) __INTRODUCED_IN(21);
134 
135 /**
136  * Start the muxer. Should be called after AMediaMuxer_addTrack and
137  * before AMediaMuxer_writeSampleData.
138  *
139  * Available since API level 21.
140  */
141 media_status_t AMediaMuxer_start(AMediaMuxer*) __INTRODUCED_IN(21);
142 
143 /**
144  * Stops the muxer.
145  * Once the muxer stops, it can not be restarted.
146  *
147  * Available since API level 21.
148  */
149 media_status_t AMediaMuxer_stop(AMediaMuxer*) __INTRODUCED_IN(21);
150 
151 /**
152  * Writes an encoded sample into the muxer.
153  * The application needs to make sure that the samples are written into
154  * the right tracks. Also, it needs to make sure the samples for each track
155  * are written in chronological order (e.g. in the order they are provided
156  * by the encoder.)
157  *
158  * Available since API level 21.
159  */
160 media_status_t AMediaMuxer_writeSampleData(AMediaMuxer *muxer,
161         size_t trackIdx, const uint8_t *data,
162         const AMediaCodecBufferInfo *info) __INTRODUCED_IN(21);
163 
164 /**
165  * Creates a new media muxer for appending data to an existing MPEG4 file.
166  * This is a synchronous API call and could take a while to return if the existing file is large.
167  * Only works for MPEG4 files matching one of the following characteristics:
168  * <ul>
169  *    <li>a single audio track.</li>
170  *    <li>a single video track.</li>
171  *    <li>a single audio and a single video track.</li>
172  * </ul>
173  * @param fd Must be opened with read and write permission. Does not take ownership of
174  * this fd i.e., caller is responsible for closing fd.
175  * @param mode Specifies how data will be appended; the AppendMode enum describes
176  *             the possible methods for appending..
177  * @return Pointer to AMediaMuxer if the file(fd) has tracks already, otherwise, nullptr.
178  * {@link AMediaMuxer_delete} should be used to free the returned pointer.
179  *
180  * Available since API level 31.
181  */
182 AMediaMuxer* AMediaMuxer_append(int fd, AppendMode mode) __INTRODUCED_IN(31);
183 
184 /**
185  * Returns the number of tracks added in the file passed to {@link AMediaMuxer_new} or
186  * the number of existing tracks in the file passed to {@link AMediaMuxer_append}.
187  * Should be called in INITIALIZED or STARTED state, otherwise returns -1.
188  *
189  * Available since API level 31.
190  */
191 ssize_t AMediaMuxer_getTrackCount(AMediaMuxer*) __INTRODUCED_IN(31);
192 
193 /**
194  * Returns AMediaFormat of the added track with index idx in the file passed to
195  * {@link AMediaMuxer_new} or the AMediaFormat of the existing track with index idx
196  * in the file passed to {@link AMediaMuxer_append}.
197  * Should be called in INITIALIZED or STARTED state, otherwise returns nullptr.
198  * {@link AMediaFormat_delete} should be used to free the returned pointer.
199  *
200  * Available since API level 31.
201  */
202 AMediaFormat* AMediaMuxer_getTrackFormat(AMediaMuxer* muxer, size_t idx) __INTRODUCED_IN(31);
203 
204 __END_DECLS
205 
206 #endif // _NDK_MEDIA_MUXER_H
207 
208 /** @} */
209