Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 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 | |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 17 | //#define LOG_NDEBUG 0 |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 18 | #define LOG_TAG "DummyVideoSource" |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 19 | #include <stdlib.h> |
| 20 | #include <utils/Log.h> |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 21 | #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 Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 27 | #include "VideoEditorTools.h" |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 28 | #include "DummyVideoSource.h" |
| 29 | |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 33 | sp<DummyVideoSource> DummyVideoSource::Create( |
| 34 | uint32_t width, uint32_t height, |
| 35 | uint64_t clipDuration, const char *imageUri) { |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 36 | |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 37 | ALOGV("Create"); |
| 38 | return new DummyVideoSource( |
| 39 | width, height, clipDuration, imageUri); |
| 40 | |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 44 | DummyVideoSource::DummyVideoSource( |
| 45 | uint32_t width, uint32_t height, |
| 46 | uint64_t clipDuration, const char *imageUri) { |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 47 | |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 48 | ALOGV("Constructor: E"); |
| 49 | |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 50 | mFrameWidth = width; |
| 51 | mFrameHeight = height; |
| 52 | mImageClipDuration = clipDuration; |
| 53 | mUri = imageUri; |
| 54 | mImageBuffer = NULL; |
| 55 | |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 56 | ALOGV("%s", mUri); |
| 57 | ALOGV("Constructor: X"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 61 | DummyVideoSource::~DummyVideoSource() { |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 62 | /* Do nothing here? */ |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 63 | ALOGV("~DummyVideoSource"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | |
| 67 | |
| 68 | status_t DummyVideoSource::start(MetaData *params) { |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 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 | } |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 81 | |
| 82 | mIsFirstImageFrame = true; |
| 83 | mImageSeekTime = 0; |
| 84 | mImagePlayStartTime = 0; |
| 85 | mFrameTimeUs = 0; |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 86 | |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 87 | ALOGV("start: X"); |
| 88 | return OK; |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | |
| 92 | status_t DummyVideoSource::stop() { |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 93 | ALOGV("stop"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 94 | status_t err = OK; |
| 95 | |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 96 | if (mImageBuffer != NULL) { |
| 97 | free(mImageBuffer); |
| 98 | mImageBuffer = NULL; |
| 99 | } |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 100 | |
| 101 | return err; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | sp<MetaData> DummyVideoSource::getFormat() { |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 106 | ALOGV("getFormat"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 107 | |
| 108 | sp<MetaData> meta = new MetaData; |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 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 | |
| 118 | status_t DummyVideoSource::read( |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 119 | MediaBuffer **out, |
| 120 | const MediaSource::ReadOptions *options) { |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 121 | |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 122 | ALOGV("read: E"); |
| 123 | |
| 124 | const int32_t kTimeScale = 1000; /* time scale in ms */ |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 125 | bool seeking = false; |
| 126 | int64_t seekTimeUs; |
| 127 | ReadOptions::SeekMode seekMode; |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 128 | if (options && options->getSeekTo(&seekTimeUs, &seekMode)) { |
| 129 | seeking = true; |
| 130 | mImageSeekTime = seekTimeUs; |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 131 | M4OSA_clockGetTime(&mImagePlayStartTime, kTimeScale); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 132 | } |
| 133 | |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 134 | if ((mImageSeekTime == mImageClipDuration) || |
| 135 | (mFrameTimeUs == (int64_t)mImageClipDuration)) { |
| 136 | ALOGV("read: EOS reached"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 137 | *out = NULL; |
| 138 | return ERROR_END_OF_STREAM; |
| 139 | } |
| 140 | |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 141 | status_t err = OK; |
| 142 | MediaBuffer *buffer = new MediaBuffer( |
| 143 | mImageBuffer, (mFrameWidth * mFrameHeight * 1.5)); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 144 | |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 145 | // Set timestamp of buffer |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 146 | if (mIsFirstImageFrame) { |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 147 | M4OSA_clockGetTime(&mImagePlayStartTime, kTimeScale); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 148 | mFrameTimeUs = (mImageSeekTime + 1); |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 149 | ALOGV("read: jpg 1st frame timeUs = %lld, begin cut time = %ld", |
| 150 | mFrameTimeUs, mImageSeekTime); |
| 151 | |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 152 | mIsFirstImageFrame = false; |
| 153 | } else { |
| 154 | M4OSA_Time currentTimeMs; |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 155 | M4OSA_clockGetTime(¤tTimeMs, kTimeScale); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 156 | |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 157 | mFrameTimeUs = mImageSeekTime + |
| 158 | (currentTimeMs - mImagePlayStartTime) * 1000LL; |
| 159 | |
| 160 | ALOGV("read: jpg frame timeUs = %lld", mFrameTimeUs); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 161 | } |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 162 | |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 163 | buffer->meta_data()->setInt64(kKeyTime, mFrameTimeUs); |
James Dong | 00f742c | 2012-01-13 17:34:42 -0800 | [diff] [blame^] | 164 | buffer->set_range(buffer->range_offset(), |
| 165 | mFrameWidth * mFrameHeight * 1.5); |
| 166 | |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 167 | *out = buffer; |
| 168 | return err; |
| 169 | } |
| 170 | |
| 171 | }// namespace android |