blob: 6dbcf2ac685353f6c0c9a0233d04456999fc5d5f [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"
Mark Salyzyndb43b342014-04-04 14:47:28 -070019#include <inttypes.h>
James Dong00f742c2012-01-13 17:34:42 -080020#include <stdlib.h>
21#include <utils/Log.h>
James Dongc4689fa2012-02-08 13:51:46 -080022#include <media/stagefright/foundation/ADebug.h>
Chih-Chung Chang99698662011-06-30 14:21:38 +080023#include <media/stagefright/MediaErrors.h>
Chih-Chung Chang99698662011-06-30 14:21:38 +080024#include <media/stagefright/MediaDefs.h>
25#include <media/stagefright/MediaBuffer.h>
26#include <media/stagefright/MediaBufferGroup.h>
27#include <media/stagefright/MetaData.h>
James Dong00f742c2012-01-13 17:34:42 -080028#include "VideoEditorTools.h"
Chih-Chung Chang99698662011-06-30 14:21:38 +080029#include "DummyVideoSource.h"
30
Chih-Chung Chang99698662011-06-30 14:21:38 +080031
32namespace android {
33
James Dong00f742c2012-01-13 17:34:42 -080034sp<DummyVideoSource> DummyVideoSource::Create(
35 uint32_t width, uint32_t height,
36 uint64_t clipDuration, const char *imageUri) {
Chih-Chung Chang99698662011-06-30 14:21:38 +080037
James Dong00f742c2012-01-13 17:34:42 -080038 ALOGV("Create");
39 return new DummyVideoSource(
40 width, height, clipDuration, imageUri);
41
Chih-Chung Chang99698662011-06-30 14:21:38 +080042}
43
44
James Dong00f742c2012-01-13 17:34:42 -080045DummyVideoSource::DummyVideoSource(
46 uint32_t width, uint32_t height,
47 uint64_t clipDuration, const char *imageUri) {
Chih-Chung Chang99698662011-06-30 14:21:38 +080048
James Dong00f742c2012-01-13 17:34:42 -080049 ALOGV("Constructor: E");
50
Chih-Chung Chang99698662011-06-30 14:21:38 +080051 mFrameWidth = width;
52 mFrameHeight = height;
53 mImageClipDuration = clipDuration;
54 mUri = imageUri;
55 mImageBuffer = NULL;
56
James Dong00f742c2012-01-13 17:34:42 -080057 ALOGV("%s", mUri);
58 ALOGV("Constructor: X");
Chih-Chung Chang99698662011-06-30 14:21:38 +080059}
60
61
James Dong00f742c2012-01-13 17:34:42 -080062DummyVideoSource::~DummyVideoSource() {
Chih-Chung Chang99698662011-06-30 14:21:38 +080063 /* Do nothing here? */
James Dong00f742c2012-01-13 17:34:42 -080064 ALOGV("~DummyVideoSource");
Chih-Chung Chang99698662011-06-30 14:21:38 +080065}
66
67
68
69status_t DummyVideoSource::start(MetaData *params) {
James Dong00f742c2012-01-13 17:34:42 -080070 ALOGV("start: E");
71
72 // Get the frame buffer from the rgb file, mUri,
73 // and store its content into a MediaBuffer
74 status_t err = LvGetImageThumbNail(
75 (const char *)mUri,
76 mFrameHeight, mFrameWidth,
77 (M4OSA_Void **) &mImageBuffer);
78 if (err != OK) {
79 ALOGE("LvGetImageThumbNail failed: %d", err);
80 return err;
81 }
Chih-Chung Chang99698662011-06-30 14:21:38 +080082
83 mIsFirstImageFrame = true;
84 mImageSeekTime = 0;
85 mImagePlayStartTime = 0;
86 mFrameTimeUs = 0;
Chih-Chung Chang99698662011-06-30 14:21:38 +080087
James Dong00f742c2012-01-13 17:34:42 -080088 ALOGV("start: X");
89 return OK;
Chih-Chung Chang99698662011-06-30 14:21:38 +080090}
91
92
93status_t DummyVideoSource::stop() {
James Dong00f742c2012-01-13 17:34:42 -080094 ALOGV("stop");
Chih-Chung Chang99698662011-06-30 14:21:38 +080095 status_t err = OK;
96
Chih-Chung Chang99698662011-06-30 14:21:38 +080097 if (mImageBuffer != NULL) {
98 free(mImageBuffer);
99 mImageBuffer = NULL;
100 }
Chih-Chung Chang99698662011-06-30 14:21:38 +0800101
102 return err;
103}
104
105
106sp<MetaData> DummyVideoSource::getFormat() {
James Dong00f742c2012-01-13 17:34:42 -0800107 ALOGV("getFormat");
Chih-Chung Chang99698662011-06-30 14:21:38 +0800108
109 sp<MetaData> meta = new MetaData;
Chih-Chung Chang99698662011-06-30 14:21:38 +0800110 meta->setInt32(kKeyColorFormat, OMX_COLOR_FormatYUV420Planar);
111 meta->setInt32(kKeyWidth, mFrameWidth);
112 meta->setInt32(kKeyHeight, mFrameHeight);
113 meta->setInt64(kKeyDuration, mImageClipDuration);
114 meta->setCString(kKeyDecoderComponent, "DummyVideoSource");
115
116 return meta;
117}
118
119status_t DummyVideoSource::read(
James Dong00f742c2012-01-13 17:34:42 -0800120 MediaBuffer **out,
121 const MediaSource::ReadOptions *options) {
Chih-Chung Chang99698662011-06-30 14:21:38 +0800122
James Dong00f742c2012-01-13 17:34:42 -0800123 ALOGV("read: E");
124
125 const int32_t kTimeScale = 1000; /* time scale in ms */
Chih-Chung Chang99698662011-06-30 14:21:38 +0800126 bool seeking = false;
127 int64_t seekTimeUs;
128 ReadOptions::SeekMode seekMode;
Chih-Chung Chang99698662011-06-30 14:21:38 +0800129 if (options && options->getSeekTo(&seekTimeUs, &seekMode)) {
130 seeking = true;
131 mImageSeekTime = seekTimeUs;
James Dong00f742c2012-01-13 17:34:42 -0800132 M4OSA_clockGetTime(&mImagePlayStartTime, kTimeScale);
Chih-Chung Chang99698662011-06-30 14:21:38 +0800133 }
134
James Dong00f742c2012-01-13 17:34:42 -0800135 if ((mImageSeekTime == mImageClipDuration) ||
136 (mFrameTimeUs == (int64_t)mImageClipDuration)) {
137 ALOGV("read: EOS reached");
Chih-Chung Chang99698662011-06-30 14:21:38 +0800138 *out = NULL;
139 return ERROR_END_OF_STREAM;
140 }
141
James Dong00f742c2012-01-13 17:34:42 -0800142 status_t err = OK;
143 MediaBuffer *buffer = new MediaBuffer(
144 mImageBuffer, (mFrameWidth * mFrameHeight * 1.5));
Chih-Chung Chang99698662011-06-30 14:21:38 +0800145
James Dong00f742c2012-01-13 17:34:42 -0800146 // Set timestamp of buffer
Chih-Chung Chang99698662011-06-30 14:21:38 +0800147 if (mIsFirstImageFrame) {
James Dong00f742c2012-01-13 17:34:42 -0800148 M4OSA_clockGetTime(&mImagePlayStartTime, kTimeScale);
Chih-Chung Chang99698662011-06-30 14:21:38 +0800149 mFrameTimeUs = (mImageSeekTime + 1);
Mark Salyzyndb43b342014-04-04 14:47:28 -0700150 ALOGV("read: jpg 1st frame timeUs = %lld, begin cut time = %" PRIu32,
James Dong00f742c2012-01-13 17:34:42 -0800151 mFrameTimeUs, mImageSeekTime);
152
Chih-Chung Chang99698662011-06-30 14:21:38 +0800153 mIsFirstImageFrame = false;
154 } else {
155 M4OSA_Time currentTimeMs;
James Dong00f742c2012-01-13 17:34:42 -0800156 M4OSA_clockGetTime(&currentTimeMs, kTimeScale);
Chih-Chung Chang99698662011-06-30 14:21:38 +0800157
James Dong00f742c2012-01-13 17:34:42 -0800158 mFrameTimeUs = mImageSeekTime +
159 (currentTimeMs - mImagePlayStartTime) * 1000LL;
160
161 ALOGV("read: jpg frame timeUs = %lld", mFrameTimeUs);
Chih-Chung Chang99698662011-06-30 14:21:38 +0800162 }
James Dong00f742c2012-01-13 17:34:42 -0800163
Chih-Chung Chang99698662011-06-30 14:21:38 +0800164 buffer->meta_data()->setInt64(kKeyTime, mFrameTimeUs);
James Dong00f742c2012-01-13 17:34:42 -0800165 buffer->set_range(buffer->range_offset(),
166 mFrameWidth * mFrameHeight * 1.5);
167
Chih-Chung Chang99698662011-06-30 14:21:38 +0800168 *out = buffer;
169 return err;
170}
171
172}// namespace android