1/*
2 * Copyright (C) 2011 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//#define LOG_NDEBUG 0
18#define LOG_TAG "DummyVideoSource"
19#include <stdlib.h>
20#include <utils/Log.h>
21#include <media/stagefright/foundation/ADebug.h>
22#include <media/stagefright/MediaErrors.h>
23#include <media/stagefright/MediaDefs.h>
24#include <media/stagefright/MediaBuffer.h>
25#include <media/stagefright/MediaBufferGroup.h>
26#include <media/stagefright/MetaData.h>
27#include "VideoEditorTools.h"
28#include "DummyVideoSource.h"
29
30
31namespace android {
32
33sp<DummyVideoSource> DummyVideoSource::Create(
34        uint32_t width, uint32_t height,
35        uint64_t clipDuration, const char *imageUri) {
36
37    ALOGV("Create");
38    return new DummyVideoSource(
39                    width, height, clipDuration, imageUri);
40
41}
42
43
44DummyVideoSource::DummyVideoSource(
45        uint32_t width, uint32_t height,
46        uint64_t clipDuration, const char *imageUri) {
47
48    ALOGV("Constructor: E");
49
50    mFrameWidth = width;
51    mFrameHeight = height;
52    mImageClipDuration = clipDuration;
53    mUri = imageUri;
54    mImageBuffer = NULL;
55
56    ALOGV("%s", mUri);
57    ALOGV("Constructor: X");
58}
59
60
61DummyVideoSource::~DummyVideoSource() {
62    /* Do nothing here? */
63    ALOGV("~DummyVideoSource");
64}
65
66
67
68status_t DummyVideoSource::start(MetaData *params) {
69    ALOGV("start: E");
70
71    // Get the frame buffer from the rgb file, mUri,
72    // and store its content into a MediaBuffer
73    status_t err = LvGetImageThumbNail(
74                    (const char *)mUri,
75                    mFrameHeight, mFrameWidth,
76                    (M4OSA_Void **) &mImageBuffer);
77    if (err != OK) {
78        ALOGE("LvGetImageThumbNail failed: %d", err);
79        return err;
80    }
81
82    mIsFirstImageFrame = true;
83    mImageSeekTime = 0;
84    mImagePlayStartTime = 0;
85    mFrameTimeUs = 0;
86
87    ALOGV("start: X");
88    return OK;
89}
90
91
92status_t DummyVideoSource::stop() {
93    ALOGV("stop");
94    status_t err = OK;
95
96    if (mImageBuffer != NULL) {
97        free(mImageBuffer);
98        mImageBuffer = NULL;
99    }
100
101    return err;
102}
103
104
105sp<MetaData> DummyVideoSource::getFormat() {
106    ALOGV("getFormat");
107
108    sp<MetaData> meta = new MetaData;
109    meta->setInt32(kKeyColorFormat, OMX_COLOR_FormatYUV420Planar);
110    meta->setInt32(kKeyWidth, mFrameWidth);
111    meta->setInt32(kKeyHeight, mFrameHeight);
112    meta->setInt64(kKeyDuration, mImageClipDuration);
113    meta->setCString(kKeyDecoderComponent, "DummyVideoSource");
114
115    return meta;
116}
117
118status_t DummyVideoSource::read(
119        MediaBuffer **out,
120        const MediaSource::ReadOptions *options) {
121
122    ALOGV("read: E");
123
124    const int32_t kTimeScale = 1000;  /* time scale in ms */
125    bool seeking = false;
126    int64_t seekTimeUs;
127    ReadOptions::SeekMode seekMode;
128    if (options && options->getSeekTo(&seekTimeUs, &seekMode)) {
129        seeking = true;
130        mImageSeekTime = seekTimeUs;
131        M4OSA_clockGetTime(&mImagePlayStartTime, kTimeScale);
132    }
133
134    if ((mImageSeekTime == mImageClipDuration) ||
135        (mFrameTimeUs == (int64_t)mImageClipDuration)) {
136        ALOGV("read: EOS reached");
137        *out = NULL;
138        return ERROR_END_OF_STREAM;
139    }
140
141    status_t err = OK;
142    MediaBuffer *buffer = new MediaBuffer(
143            mImageBuffer, (mFrameWidth * mFrameHeight * 1.5));
144
145    // Set timestamp of buffer
146    if (mIsFirstImageFrame) {
147        M4OSA_clockGetTime(&mImagePlayStartTime, kTimeScale);
148        mFrameTimeUs =  (mImageSeekTime + 1);
149        ALOGV("read: jpg 1st frame timeUs = %lld, begin cut time = %ld",
150            mFrameTimeUs, mImageSeekTime);
151
152        mIsFirstImageFrame = false;
153    } else {
154        M4OSA_Time  currentTimeMs;
155        M4OSA_clockGetTime(&currentTimeMs, kTimeScale);
156
157        mFrameTimeUs = mImageSeekTime +
158            (currentTimeMs - mImagePlayStartTime) * 1000LL;
159
160        ALOGV("read: jpg frame timeUs = %lld", mFrameTimeUs);
161    }
162
163    buffer->meta_data()->setInt64(kKeyTime, mFrameTimeUs);
164    buffer->set_range(buffer->range_offset(),
165                mFrameWidth * mFrameHeight * 1.5);
166
167    *out = buffer;
168    return err;
169}
170
171}// namespace android
172