blob: 339d8fe52b87915333fea591fc042688ee20bf82 [file] [log] [blame]
Chih-Chung Chang99698662011-06-30 14:21:38 +08001/*
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
James Dong00f742c2012-01-13 17:34:42 -080017//#define LOG_NDEBUG 0
Chih-Chung Chang99698662011-06-30 14:21:38 +080018#define LOG_TAG "DummyVideoSource"
James Dong00f742c2012-01-13 17:34:42 -080019#include <stdlib.h>
20#include <utils/Log.h>
Chih-Chung Chang99698662011-06-30 14:21:38 +080021#include <media/stagefright/MediaErrors.h>
22#include <media/stagefright/MediaDebug.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>
James Dong00f742c2012-01-13 17:34:42 -080027#include "VideoEditorTools.h"
Chih-Chung Chang99698662011-06-30 14:21:38 +080028#include "DummyVideoSource.h"
29
Chih-Chung Chang99698662011-06-30 14:21:38 +080030
31namespace android {
32
James Dong00f742c2012-01-13 17:34:42 -080033sp<DummyVideoSource> DummyVideoSource::Create(
34 uint32_t width, uint32_t height,
35 uint64_t clipDuration, const char *imageUri) {
Chih-Chung Chang99698662011-06-30 14:21:38 +080036
James Dong00f742c2012-01-13 17:34:42 -080037 ALOGV("Create");
38 return new DummyVideoSource(
39 width, height, clipDuration, imageUri);
40
Chih-Chung Chang99698662011-06-30 14:21:38 +080041}
42
43
James Dong00f742c2012-01-13 17:34:42 -080044DummyVideoSource::DummyVideoSource(
45 uint32_t width, uint32_t height,
46 uint64_t clipDuration, const char *imageUri) {
Chih-Chung Chang99698662011-06-30 14:21:38 +080047
James Dong00f742c2012-01-13 17:34:42 -080048 ALOGV("Constructor: E");
49
Chih-Chung Chang99698662011-06-30 14:21:38 +080050 mFrameWidth = width;
51 mFrameHeight = height;
52 mImageClipDuration = clipDuration;
53 mUri = imageUri;
54 mImageBuffer = NULL;
55
James Dong00f742c2012-01-13 17:34:42 -080056 ALOGV("%s", mUri);
57 ALOGV("Constructor: X");
Chih-Chung Chang99698662011-06-30 14:21:38 +080058}
59
60
James Dong00f742c2012-01-13 17:34:42 -080061DummyVideoSource::~DummyVideoSource() {
Chih-Chung Chang99698662011-06-30 14:21:38 +080062 /* Do nothing here? */
James Dong00f742c2012-01-13 17:34:42 -080063 ALOGV("~DummyVideoSource");
Chih-Chung Chang99698662011-06-30 14:21:38 +080064}
65
66
67
68status_t DummyVideoSource::start(MetaData *params) {
James Dong00f742c2012-01-13 17:34:42 -080069 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 }
Chih-Chung Chang99698662011-06-30 14:21:38 +080081
82 mIsFirstImageFrame = true;
83 mImageSeekTime = 0;
84 mImagePlayStartTime = 0;
85 mFrameTimeUs = 0;
Chih-Chung Chang99698662011-06-30 14:21:38 +080086
James Dong00f742c2012-01-13 17:34:42 -080087 ALOGV("start: X");
88 return OK;
Chih-Chung Chang99698662011-06-30 14:21:38 +080089}
90
91
92status_t DummyVideoSource::stop() {
James Dong00f742c2012-01-13 17:34:42 -080093 ALOGV("stop");
Chih-Chung Chang99698662011-06-30 14:21:38 +080094 status_t err = OK;
95
Chih-Chung Chang99698662011-06-30 14:21:38 +080096 if (mImageBuffer != NULL) {
97 free(mImageBuffer);
98 mImageBuffer = NULL;
99 }
Chih-Chung Chang99698662011-06-30 14:21:38 +0800100
101 return err;
102}
103
104
105sp<MetaData> DummyVideoSource::getFormat() {
James Dong00f742c2012-01-13 17:34:42 -0800106 ALOGV("getFormat");
Chih-Chung Chang99698662011-06-30 14:21:38 +0800107
108 sp<MetaData> meta = new MetaData;
Chih-Chung Chang99698662011-06-30 14:21:38 +0800109 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(
James Dong00f742c2012-01-13 17:34:42 -0800119 MediaBuffer **out,
120 const MediaSource::ReadOptions *options) {
Chih-Chung Chang99698662011-06-30 14:21:38 +0800121
James Dong00f742c2012-01-13 17:34:42 -0800122 ALOGV("read: E");
123
124 const int32_t kTimeScale = 1000; /* time scale in ms */
Chih-Chung Chang99698662011-06-30 14:21:38 +0800125 bool seeking = false;
126 int64_t seekTimeUs;
127 ReadOptions::SeekMode seekMode;
Chih-Chung Chang99698662011-06-30 14:21:38 +0800128 if (options && options->getSeekTo(&seekTimeUs, &seekMode)) {
129 seeking = true;
130 mImageSeekTime = seekTimeUs;
James Dong00f742c2012-01-13 17:34:42 -0800131 M4OSA_clockGetTime(&mImagePlayStartTime, kTimeScale);
Chih-Chung Chang99698662011-06-30 14:21:38 +0800132 }
133
James Dong00f742c2012-01-13 17:34:42 -0800134 if ((mImageSeekTime == mImageClipDuration) ||
135 (mFrameTimeUs == (int64_t)mImageClipDuration)) {
136 ALOGV("read: EOS reached");
Chih-Chung Chang99698662011-06-30 14:21:38 +0800137 *out = NULL;
138 return ERROR_END_OF_STREAM;
139 }
140
James Dong00f742c2012-01-13 17:34:42 -0800141 status_t err = OK;
142 MediaBuffer *buffer = new MediaBuffer(
143 mImageBuffer, (mFrameWidth * mFrameHeight * 1.5));
Chih-Chung Chang99698662011-06-30 14:21:38 +0800144
James Dong00f742c2012-01-13 17:34:42 -0800145 // Set timestamp of buffer
Chih-Chung Chang99698662011-06-30 14:21:38 +0800146 if (mIsFirstImageFrame) {
James Dong00f742c2012-01-13 17:34:42 -0800147 M4OSA_clockGetTime(&mImagePlayStartTime, kTimeScale);
Chih-Chung Chang99698662011-06-30 14:21:38 +0800148 mFrameTimeUs = (mImageSeekTime + 1);
James Dong00f742c2012-01-13 17:34:42 -0800149 ALOGV("read: jpg 1st frame timeUs = %lld, begin cut time = %ld",
150 mFrameTimeUs, mImageSeekTime);
151
Chih-Chung Chang99698662011-06-30 14:21:38 +0800152 mIsFirstImageFrame = false;
153 } else {
154 M4OSA_Time currentTimeMs;
James Dong00f742c2012-01-13 17:34:42 -0800155 M4OSA_clockGetTime(&currentTimeMs, kTimeScale);
Chih-Chung Chang99698662011-06-30 14:21:38 +0800156
James Dong00f742c2012-01-13 17:34:42 -0800157 mFrameTimeUs = mImageSeekTime +
158 (currentTimeMs - mImagePlayStartTime) * 1000LL;
159
160 ALOGV("read: jpg frame timeUs = %lld", mFrameTimeUs);
Chih-Chung Chang99698662011-06-30 14:21:38 +0800161 }
James Dong00f742c2012-01-13 17:34:42 -0800162
Chih-Chung Chang99698662011-06-30 14:21:38 +0800163 buffer->meta_data()->setInt64(kKeyTime, mFrameTimeUs);
James Dong00f742c2012-01-13 17:34:42 -0800164 buffer->set_range(buffer->range_offset(),
165 mFrameWidth * mFrameHeight * 1.5);
166
Chih-Chung Chang99698662011-06-30 14:21:38 +0800167 *out = buffer;
168 return err;
169}
170
171}// namespace android