record.cpp revision 66326a5ee0869f1ee4d136a477e6effba428b3cb
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 <media/stagefright/AudioPlayer.h>
21#include <media/stagefright/CameraSource.h>
22#include <media/stagefright/MediaBufferGroup.h>
23#include <media/stagefright/MediaDebug.h>
24#include <media/stagefright/MediaDefs.h>
25#include <media/stagefright/MetaData.h>
26#include <media/stagefright/MediaExtractor.h>
27#include <media/stagefright/MPEG4Writer.h>
28#include <media/stagefright/MmapSource.h>
29#include <media/stagefright/OMXClient.h>
30#include <media/stagefright/OMXCodec.h>
31#include <media/MediaPlayerInterface.h>
32
33using namespace android;
34
35#if 0
36class DummySource : public MediaSource {
37public:
38    DummySource(int width, int height)
39        : mWidth(width),
40          mHeight(height),
41          mSize((width * height * 3) / 2) {
42        mGroup.add_buffer(new MediaBuffer(mSize));
43    }
44
45    virtual sp<MetaData> getFormat() {
46        sp<MetaData> meta = new MetaData;
47        meta->setInt32(kKeyWidth, mWidth);
48        meta->setInt32(kKeyHeight, mHeight);
49        meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
50
51        return meta;
52    }
53
54    virtual status_t start(MetaData *params) {
55        return OK;
56    }
57
58    virtual status_t stop() {
59        return OK;
60    }
61
62    virtual status_t read(
63            MediaBuffer **buffer, const MediaSource::ReadOptions *options) {
64        status_t err = mGroup.acquire_buffer(buffer);
65        if (err != OK) {
66            return err;
67        }
68
69        char x = (char)((double)rand() / RAND_MAX * 255);
70        memset((*buffer)->data(), x, mSize);
71        (*buffer)->set_range(0, mSize);
72
73        return OK;
74    }
75
76protected:
77    virtual ~DummySource() {}
78
79private:
80    MediaBufferGroup mGroup;
81    int mWidth, mHeight;
82    size_t mSize;
83
84    DummySource(const DummySource &);
85    DummySource &operator=(const DummySource &);
86};
87
88sp<MediaSource> createSource(const char *filename) {
89    sp<MediaSource> source;
90
91    sp<MediaExtractor> extractor =
92        MediaExtractor::Create(new MmapSource(filename));
93
94    size_t num_tracks = extractor->countTracks();
95
96    sp<MetaData> meta;
97    for (size_t i = 0; i < num_tracks; ++i) {
98        meta = extractor->getTrackMetaData(i);
99        CHECK(meta.get() != NULL);
100
101        const char *mime;
102        if (!meta->findCString(kKeyMIMEType, &mime)) {
103            continue;
104        }
105
106        if (strncasecmp(mime, "video/", 6)) {
107            continue;
108        }
109
110        source = extractor->getTrack(i);
111        break;
112    }
113
114    return source;
115}
116
117int main(int argc, char **argv) {
118    android::ProcessState::self()->startThreadPool();
119
120#if 1
121    if (argc != 2) {
122        fprintf(stderr, "usage: %s filename\n", argv[0]);
123        return 1;
124    }
125
126    OMXClient client;
127    CHECK_EQ(client.connect(), OK);
128
129#if 0
130    sp<MediaSource> source = createSource(argv[1]);
131
132    if (source == NULL) {
133        fprintf(stderr, "Unable to find a suitable video track.\n");
134        return 1;
135    }
136
137    sp<MetaData> meta = source->getFormat();
138
139    sp<OMXCodec> decoder = OMXCodec::Create(
140            client.interface(), meta, false /* createEncoder */, source);
141
142    int width, height;
143    bool success = meta->findInt32(kKeyWidth, &width);
144    success = success && meta->findInt32(kKeyHeight, &height);
145    CHECK(success);
146#else
147    int width = 320;
148    int height = 240;
149    sp<MediaSource> decoder = new DummySource(width, height);
150#endif
151
152    sp<MetaData> enc_meta = new MetaData;
153    // enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
154    enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
155    enc_meta->setInt32(kKeyWidth, width);
156    enc_meta->setInt32(kKeyHeight, height);
157
158    sp<OMXCodec> encoder =
159        OMXCodec::Create(
160                client.interface(), enc_meta, true /* createEncoder */, decoder);
161
162#if 0
163    sp<MPEG4Writer> writer = new MPEG4Writer("/sdcard/output.mp4");
164    writer->addSource(enc_meta, encoder);
165    writer->start();
166    sleep(20);
167    printf("stopping now.\n");
168    writer->stop();
169#else
170    encoder->start();
171
172    MediaBuffer *buffer;
173    while (encoder->read(&buffer) == OK) {
174        printf("got an output frame of size %d\n", buffer->range_length());
175
176        buffer->release();
177        buffer = NULL;
178    }
179
180    encoder->stop();
181#endif
182
183    client.disconnect();
184#endif
185
186#if 0
187    CameraSource *source = CameraSource::Create();
188    printf("source = %p\n", source);
189
190    for (int i = 0; i < 100; ++i) {
191        MediaBuffer *buffer;
192        status_t err = source->read(&buffer);
193        CHECK_EQ(err, OK);
194
195        printf("got a frame, data=%p, size=%d\n",
196               buffer->data(), buffer->range_length());
197
198        buffer->release();
199        buffer = NULL;
200    }
201
202    delete source;
203    source = NULL;
204#endif
205
206    return 0;
207}
208#else
209
210int main(int argc, char **argv) {
211    android::ProcessState::self()->startThreadPool();
212
213    OMXClient client;
214    CHECK_EQ(client.connect(), OK);
215
216    const int32_t kSampleRate = 22050;
217    const int32_t kNumChannels = 2;
218    sp<MediaSource> audioSource = new SineSource(kSampleRate, kNumChannels);
219
220#if 0
221    sp<MediaPlayerBase::AudioSink> audioSink;
222    AudioPlayer *player = new AudioPlayer(audioSink);
223    player->setSource(audioSource);
224    player->start();
225
226    sleep(10);
227
228    player->stop();
229#endif
230
231    sp<MetaData> encMeta = new MetaData;
232    encMeta->setCString(kKeyMIMEType,
233            1 ? MEDIA_MIMETYPE_AUDIO_AMR_WB : MEDIA_MIMETYPE_AUDIO_AAC);
234    encMeta->setInt32(kKeySampleRate, kSampleRate);
235    encMeta->setInt32(kKeyChannelCount, kNumChannels);
236    encMeta->setInt32(kKeyMaxInputSize, 8192);
237
238    sp<MediaSource> encoder =
239        OMXCodec::Create(client.interface(), encMeta, true, audioSource);
240
241    encoder->start();
242
243    int32_t n = 0;
244    status_t err;
245    MediaBuffer *buffer;
246    while ((err = encoder->read(&buffer)) == OK) {
247        printf(".");
248        fflush(stdout);
249
250        buffer->release();
251        buffer = NULL;
252
253        if (++n == 100) {
254            break;
255        }
256    }
257    printf("$\n");
258
259    encoder->stop();
260
261    client.disconnect();
262
263    return 0;
264}
265#endif
266