record.cpp revision 6d43768514650d65f4f3695da4f7c3e2eefd483c
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 <binder/ProcessState.h> 18#include <media/stagefright/CameraSource.h> 19#include <media/stagefright/MediaBufferGroup.h> 20#include <media/stagefright/MediaDebug.h> 21#include <media/stagefright/MetaData.h> 22#include <media/stagefright/MPEG4Extractor.h> 23#include <media/stagefright/MPEG4Writer.h> 24#include <media/stagefright/MmapSource.h> 25#include <media/stagefright/OMXClient.h> 26#include <media/stagefright/OMXCodec.h> 27 28using namespace android; 29 30class DummySource : public MediaSource { 31public: 32 DummySource(int width, int height) 33 : mWidth(width), 34 mHeight(height), 35 mSize((width * height * 3) / 2) { 36 mGroup.add_buffer(new MediaBuffer(mSize)); 37 } 38 39 virtual sp<MetaData> getFormat() { 40 sp<MetaData> meta = new MetaData; 41 meta->setInt32(kKeyWidth, mWidth); 42 meta->setInt32(kKeyHeight, mHeight); 43 meta->setCString(kKeyMIMEType, "video/raw"); 44 45 return meta; 46 } 47 48 virtual status_t getMaxSampleSize(size_t *max_size) { 49 *max_size = mSize; 50 return OK; 51 } 52 53 virtual status_t start(MetaData *params) { 54 return OK; 55 } 56 57 virtual status_t stop() { 58 return OK; 59 } 60 61 virtual status_t read( 62 MediaBuffer **buffer, const MediaSource::ReadOptions *options) { 63 status_t err = mGroup.acquire_buffer(buffer); 64 if (err != OK) { 65 return err; 66 } 67 68 char x = (char)((double)rand() / RAND_MAX * 255); 69 memset((*buffer)->data(), x, mSize); 70 (*buffer)->set_range(0, mSize); 71 72 return OK; 73 } 74 75protected: 76 virtual ~DummySource() {} 77 78private: 79 MediaBufferGroup mGroup; 80 int mWidth, mHeight; 81 size_t mSize; 82 83 DummySource(const DummySource &); 84 DummySource &operator=(const DummySource &); 85}; 86 87sp<MediaSource> createSource(const char *filename) { 88 sp<MediaSource> source; 89 90 sp<MPEG4Extractor> extractor = 91 new MPEG4Extractor(new MmapSource(filename)); 92 93 size_t num_tracks = extractor->countTracks(); 94 95 sp<MetaData> meta; 96 for (size_t i = 0; i < num_tracks; ++i) { 97 meta = extractor->getTrackMetaData(i); 98 CHECK(meta.get() != NULL); 99 100 const char *mime; 101 if (!meta->findCString(kKeyMIMEType, &mime)) { 102 continue; 103 } 104 105 if (strncasecmp(mime, "video/", 6)) { 106 continue; 107 } 108 109 source = extractor->getTrack(i); 110 break; 111 } 112 113 return source; 114} 115 116int main(int argc, char **argv) { 117 android::ProcessState::self()->startThreadPool(); 118 119#if 1 120 if (argc != 2) { 121 fprintf(stderr, "usage: %s filename\n", argv[0]); 122 return 1; 123 } 124 125 OMXClient client; 126 CHECK_EQ(client.connect(), OK); 127 128#if 0 129 sp<MediaSource> source = createSource(argv[1]); 130 131 if (source == NULL) { 132 fprintf(stderr, "Unable to find a suitable video track.\n"); 133 return 1; 134 } 135 136 sp<MetaData> meta = source->getFormat(); 137 138 sp<OMXCodec> decoder = OMXCodec::Create( 139 client.interface(), meta, false /* createEncoder */, source); 140 141 int width, height; 142 bool success = meta->findInt32(kKeyWidth, &width); 143 success = success && meta->findInt32(kKeyHeight, &height); 144 CHECK(success); 145#else 146 int width = 320; 147 int height = 240; 148 sp<MediaSource> decoder = new DummySource(width, height); 149#endif 150 151 sp<MetaData> enc_meta = new MetaData; 152 // enc_meta->setCString(kKeyMIMEType, "video/3gpp"); 153 enc_meta->setCString(kKeyMIMEType, "video/mp4v-es"); 154 enc_meta->setInt32(kKeyWidth, width); 155 enc_meta->setInt32(kKeyHeight, height); 156 157 sp<OMXCodec> encoder = 158 OMXCodec::Create( 159 client.interface(), enc_meta, true /* createEncoder */, decoder); 160 161#if 1 162 sp<MPEG4Writer> writer = new MPEG4Writer("/sdcard/output.mp4"); 163 writer->addSource(enc_meta, encoder); 164 writer->start(); 165 sleep(20); 166 printf("stopping now.\n"); 167 writer->stop(); 168#else 169 encoder->start(); 170 171 MediaBuffer *buffer; 172 while (encoder->read(&buffer) == OK) { 173 printf("got an output frame of size %d\n", buffer->range_length()); 174 175 buffer->release(); 176 buffer = NULL; 177 } 178 179 encoder->stop(); 180#endif 181 182 client.disconnect(); 183#endif 184 185#if 0 186 CameraSource *source = CameraSource::Create(); 187 printf("source = %p\n", source); 188 189 for (int i = 0; i < 100; ++i) { 190 MediaBuffer *buffer; 191 status_t err = source->read(&buffer); 192 CHECK_EQ(err, OK); 193 194 printf("got a frame, data=%p, size=%d\n", 195 buffer->data(), buffer->range_length()); 196 197 buffer->release(); 198 buffer = NULL; 199 } 200 201 delete source; 202 source = NULL; 203#endif 204 205 return 0; 206} 207 208