• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 package com.android.media.benchmark.library;
17 
18 import android.content.Context;
19 import android.media.MediaCodec;
20 import android.media.MediaFormat;
21 import android.media.MediaMuxer;
22 
23 import java.io.IOException;
24 import java.nio.ByteBuffer;
25 import java.util.ArrayList;
26 
27 public class Muxer {
28     private Stats mStats;
29     private MediaMuxer mMuxer;
30 
31     /**
32      * Creates a Media Muxer for the specified path
33      *
34      * @param context      App context to specify the output file path
35      * @param outputFormat Format of the output media file
36      * @param trackFormat  Format of the current track
37      * @return Returns the track index of the newly added track, -1 otherwise
38      */
setUpMuxer(Context context, int outputFormat, MediaFormat trackFormat)39     public int setUpMuxer(Context context, int outputFormat, MediaFormat trackFormat) {
40         try {
41             mStats = new Stats();
42             long sTime = mStats.getCurTime();
43             mMuxer = new MediaMuxer(context.getFilesDir().getPath() + "/mux.out.", outputFormat);
44             int trackIndex = mMuxer.addTrack(trackFormat);
45             mMuxer.start();
46             long eTime = mStats.getCurTime();
47             long timeTaken = mStats.getTimeDiff(sTime, eTime);
48             mStats.setInitTime(timeTaken);
49             return trackIndex;
50         } catch (IllegalArgumentException | IOException e) {
51             e.printStackTrace();
52             return -1;
53         }
54     }
55 
56     /**
57      * Performs the Mux operation
58      *
59      * @param trackIndex           Track index of the sample
60      * @param inputExtractedBuffer Buffer containing encoded samples
61      * @param inputBufferInfo      Buffer information related to these samples
62      * @return Returns Status as 0 if write operation is successful, -1 otherwise
63      */
mux(int trackIndex, ArrayList<ByteBuffer> inputExtractedBuffer, ArrayList<MediaCodec.BufferInfo> inputBufferInfo)64     public int mux(int trackIndex, ArrayList<ByteBuffer> inputExtractedBuffer,
65                    ArrayList<MediaCodec.BufferInfo> inputBufferInfo) {
66         mStats.setStartTime();
67         for (int sampleCount = 0; sampleCount < inputExtractedBuffer.size(); sampleCount++) {
68             try {
69                 mMuxer.writeSampleData(trackIndex, inputExtractedBuffer.get(sampleCount),
70                         inputBufferInfo.get(sampleCount));
71                 mStats.addOutputTime();
72                 mStats.addFrameSize(inputBufferInfo.get(sampleCount).size);
73             } catch (IllegalArgumentException | IllegalStateException e) {
74                 e.printStackTrace();
75                 return -1;
76             }
77         }
78         return 0;
79     }
80 
81     /**
82      * Stops the muxer and free up the resources
83      */
deInitMuxer()84     public void deInitMuxer() {
85         long sTime = mStats.getCurTime();
86         mMuxer.stop();
87         mMuxer.release();
88         long eTime = mStats.getCurTime();
89         long timeTaken = mStats.getTimeDiff(sTime, eTime);
90         mStats.setDeInitTime(timeTaken);
91     }
92 
93     /**
94      * Resets the stats
95      */
resetMuxer()96     public void resetMuxer() {
97         mStats.reset();
98     }
99 
100     /**
101      * Write the benchmark logs for the given input file
102      *
103      * @param inputReference Name of the input file
104      * @param muxFormat      Format of the muxed output
105      * @param clipDuration   Duration of the given inputReference file
106      * @param statsFile      The output file where the stats data is written
107      */
dumpStatistics(String inputReference, String muxFormat, long clipDuration, String statsFile)108     public void dumpStatistics(String inputReference, String muxFormat, long clipDuration,
109                                String statsFile) throws IOException {
110         String operation = "mux";
111         mStats.dumpStatistics(inputReference, operation, muxFormat, "", clipDuration, statsFile);
112     }
113 }
114