1 /*
2  * Copyright (C) 2009 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 #include "SineSource.h"
18 
19 #include <binder/ProcessState.h>
20 #include <datasource/FileSource.h>
21 #include <media/stagefright/foundation/ADebug.h>
22 #include <media/stagefright/foundation/ALooper.h>
23 #include <media/stagefright/foundation/AMessage.h>
24 #include <media/stagefright/CameraSource.h>
25 #include <media/stagefright/MediaBufferGroup.h>
26 #include <media/stagefright/MediaDefs.h>
27 #include <media/stagefright/MediaCodecSource.h>
28 #include <media/stagefright/MetaData.h>
29 #include <media/stagefright/MediaExtractor.h>
30 #include <media/stagefright/MediaExtractorFactory.h>
31 #include <media/stagefright/MPEG4Writer.h>
32 #include <media/stagefright/SimpleDecodingSource.h>
33 #include <media/MediaPlayerInterface.h>
34 
35 #include "AudioPlayer.h"
36 
37 using namespace android;
38 
39 static const int32_t kAudioBitRate = 12200;
40 #if 0
41 static const int32_t kFramerate = 24;  // fps
42 static const int32_t kIFramesIntervalSec = 1;
43 static const int32_t kVideoBitRate = 512 * 1024;
44 static const int64_t kDurationUs = 10000000LL;  // 10 seconds
45 
46 class DummySource : public MediaSource {
47 
48 public:
49     DummySource(int width, int height, int colorFormat)
50         : mWidth(width),
51           mHeight(height),
52           mColorFormat(colorFormat),
53           mSize((width * height * 3) / 2) {
54         mGroup.add_buffer(new MediaBuffer(mSize));
55 
56         // Check the color format to make sure
57         // that the buffer size mSize it set correctly above.
58         CHECK(colorFormat == OMX_COLOR_FormatYUV420SemiPlanar ||
59               colorFormat == OMX_COLOR_FormatYUV420Planar);
60     }
61 
62     virtual sp<MetaData> getFormat() {
63         sp<MetaData> meta = new MetaData;
64         meta->setInt32(kKeyWidth, mWidth);
65         meta->setInt32(kKeyHeight, mHeight);
66         meta->setInt32(kKeyColorFormat, mColorFormat);
67         meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
68 
69         return meta;
70     }
71 
72     virtual status_t start(MetaData *params) {
73         mNumFramesOutput = 0;
74         return OK;
75     }
76 
77     virtual status_t stop() {
78         return OK;
79     }
80 
81     virtual status_t read(
82             MediaBuffer **buffer, const MediaSource::ReadOptions *options) {
83         if (mNumFramesOutput == kFramerate * 10) {
84             // Stop returning data after 10 secs.
85             return ERROR_END_OF_STREAM;
86         }
87 
88         // printf("DummySource::read\n");
89         status_t err = mGroup.acquire_buffer(buffer);
90         if (err != OK) {
91             return err;
92         }
93 
94         char x = (char)((double)rand() / RAND_MAX * 255);
95         memset((*buffer)->data(), x, mSize);
96         (*buffer)->set_range(0, mSize);
97         (*buffer)->meta_data()->clear();
98         (*buffer)->meta_data()->setInt64(
99                 kKeyTime, (mNumFramesOutput * 1000000) / kFramerate);
100         ++mNumFramesOutput;
101 
102         // printf("DummySource::read - returning buffer\n");
103         // ALOGI("DummySource::read - returning buffer");
104         return OK;
105     }
106 
107 protected:
108     virtual ~DummySource() {}
109 
110 private:
111     MediaBufferGroup mGroup;
112     int mWidth, mHeight;
113     int mColorFormat;
114     size_t mSize;
115     int64_t mNumFramesOutput;;
116 
117     DummySource(const DummySource &);
118     DummySource &operator=(const DummySource &);
119 };
120 
121 sp<MediaSource> createSource(const char *filename) {
122     sp<MediaSource> source;
123 
124     sp<MediaExtractor> extractor =
125         MediaExtractorFactory::Create(new FileSource(filename));
126     if (extractor == NULL) {
127         return NULL;
128     }
129 
130     size_t num_tracks = extractor->countTracks();
131 
132     sp<MetaData> meta;
133     for (size_t i = 0; i < num_tracks; ++i) {
134         meta = extractor->getTrackMetaData(i);
135         CHECK(meta.get() != NULL);
136 
137         const char *mime;
138         if (!meta->findCString(kKeyMIMEType, &mime)) {
139             continue;
140         }
141 
142         if (strncasecmp(mime, "video/", 6)) {
143             continue;
144         }
145 
146         source = extractor->getTrack(i);
147         break;
148     }
149 
150     return source;
151 }
152 
153 enum {
154     kYUV420SP = 0,
155     kYUV420P  = 1,
156 };
157 
158 // returns -1 if mapping of the given color is unsuccessful
159 // returns an omx color enum value otherwise
160 static int translateColorToOmxEnumValue(int color) {
161     switch (color) {
162         case kYUV420SP:
163             return OMX_COLOR_FormatYUV420SemiPlanar;
164         case kYUV420P:
165             return OMX_COLOR_FormatYUV420Planar;
166         default:
167             fprintf(stderr, "Unsupported color: %d\n", color);
168             return -1;
169     }
170 }
171 
172 int main(int argc, char **argv) {
173     android::ProcessState::self()->startThreadPool();
174 
175 #if 1
176     if (argc != 3) {
177         fprintf(stderr, "usage: %s <filename> <input_color_format>\n", argv[0]);
178         fprintf(stderr, "       <input_color_format>:  0 (YUV420SP) or 1 (YUV420P)\n");
179         return 1;
180     }
181 
182     int colorFormat = translateColorToOmxEnumValue(atoi(argv[2]));
183     if (colorFormat == -1) {
184         fprintf(stderr, "input color format must be 0 (YUV420SP) or 1 (YUV420P)\n");
185         return 1;
186     }
187     status_t err = OK;
188 
189 #if 0
190     sp<MediaSource> source = createSource(argv[1]);
191 
192     if (source == NULL) {
193         fprintf(stderr, "Unable to find a suitable video track.\n");
194         return 1;
195     }
196 
197     sp<MetaData> meta = source->getFormat();
198 
199     sp<MediaSource> decoder = SimpleDecodingSource::Create(source);
200 
201     int width, height;
202     bool success = meta->findInt32(kKeyWidth, &width);
203     success = success && meta->findInt32(kKeyHeight, &height);
204     CHECK(success);
205 #else
206     int width = 720;
207     int height = 480;
208     sp<MediaSource> decoder = new DummySource(width, height, colorFormat);
209 #endif
210 
211     sp<AMessage> enc_meta = new AMessage;
212     // enc_meta->setString("mime", MEDIA_MIMETYPE_VIDEO_H263);
213     // enc_meta->setString("mime", MEDIA_MIMETYPE_VIDEO_MPEG4);
214     enc_meta->setString("mime", MEDIA_MIMETYPE_VIDEO_AVC);
215     enc_meta->setInt32("width", width);
216     enc_meta->setInt32("height", height);
217     enc_meta->setInt32("sample-rate", kFramerate);
218     enc_meta->setInt32("bitrate", kVideoBitRate);
219     // enc_meta->setInt32("stride", width);
220     // enc_meta->setInt32("slice-height", height);
221     enc_meta->setInt32("i-frame-interval", kIFramesIntervalSec);
222     enc_meta->setInt32("color-format", colorFormat);
223 
224     sp<MediaSource> encoder =
225         MediaCodecSource::Create(looper, format, decoder);
226 
227 #if 1
228     sp<MPEG4Writer> writer = new MPEG4Writer("/sdcard/output.mp4");
229     writer->addSource(encoder);
230     writer->setMaxFileDuration(kDurationUs);
231     CHECK_EQ((status_t)OK, writer->start());
232     while (!writer->reachedEOS()) {
233         fprintf(stderr, ".");
234         usleep(100000);
235     }
236     err = writer->stop();
237 #else
238     CHECK_EQ((status_t)OK, encoder->start());
239 
240     MediaBuffer *buffer;
241     while (encoder->read(&buffer) == OK) {
242         printf(".");
243         fflush(stdout);
244         int32_t isSync;
245         if (!buffer->meta_data()->findInt32(kKeyIsSyncFrame, &isSync)) {
246             isSync = false;
247         }
248 
249         printf("got an output frame of size %d%s\n", buffer->range_length(),
250                isSync ? " (SYNC)" : "");
251 
252         buffer->release();
253         buffer = NULL;
254     }
255 
256     err = encoder->stop();
257 #endif
258 
259     printf("$\n");
260 #endif
261 
262     if (err != OK && err != ERROR_END_OF_STREAM) {
263         fprintf(stderr, "record failed: %d\n", err);
264         return 1;
265     }
266     return 0;
267 }
268 #else
269 
main(int,char **)270 int main(int /* argc */, char ** /* argv */) {
271     android::ProcessState::self()->startThreadPool();
272 
273     const int32_t kSampleRate = 22050;
274     const int32_t kNumChannels = 2;
275     sp<MediaSource> audioSource = new SineSource(kSampleRate, kNumChannels);
276 
277 #if 0
278     sp<MediaPlayerBase::AudioSink> audioSink;
279     AudioPlayer *player = new AudioPlayer(audioSink);
280     player->setSource(audioSource);
281     player->start();
282 
283     sleep(10);
284 
285     player->stop();
286 #endif
287 
288     sp<AMessage> encMeta = new AMessage;
289     encMeta->setString("mime",
290             0 ? MEDIA_MIMETYPE_AUDIO_AMR_WB : MEDIA_MIMETYPE_AUDIO_AAC);
291     encMeta->setInt32("sample-rate", kSampleRate);
292     encMeta->setInt32("channel-count", kNumChannels);
293     encMeta->setInt32("max-input-size", 8192);
294     encMeta->setInt32("bitrate", kAudioBitRate);
295 
296     sp<ALooper> looper = new ALooper;
297     looper->setName("record");
298     looper->start();
299 
300     sp<MediaSource> encoder =
301         MediaCodecSource::Create(looper, encMeta, audioSource);
302 
303     encoder->start();
304 
305     int32_t n = 0;
306     status_t err;
307     MediaBufferBase *buffer;
308     while ((err = encoder->read(&buffer)) == OK) {
309         printf(".");
310         fflush(stdout);
311 
312         buffer->release();
313         buffer = NULL;
314 
315         if (++n == 100) {
316             break;
317         }
318     }
319     printf("$\n");
320 
321     encoder->stop();
322 
323     return 0;
324 }
325 #endif
326