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