Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 "MediaSourceSplitter" |
| 19 | #include <utils/Log.h> |
| 20 | |
James Dong | 8e9d67a | 2012-02-06 23:46:37 -0800 | [diff] [blame] | 21 | #include <media/stagefright/foundation/ADebug.h> |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 22 | #include <media/stagefright/MediaSourceSplitter.h> |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 23 | #include <media/stagefright/MediaBuffer.h> |
| 24 | #include <media/stagefright/MetaData.h> |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | MediaSourceSplitter::MediaSourceSplitter(sp<MediaSource> mediaSource) { |
| 29 | mNumberOfClients = 0; |
| 30 | mSource = mediaSource; |
| 31 | mSourceStarted = false; |
| 32 | |
| 33 | mNumberOfClientsStarted = 0; |
| 34 | mNumberOfCurrentReads = 0; |
| 35 | mCurrentReadBit = 0; |
| 36 | mLastReadCompleted = true; |
| 37 | } |
| 38 | |
| 39 | MediaSourceSplitter::~MediaSourceSplitter() { |
| 40 | } |
| 41 | |
| 42 | sp<MediaSource> MediaSourceSplitter::createClient() { |
| 43 | Mutex::Autolock autoLock(mLock); |
| 44 | |
| 45 | sp<MediaSource> client = new Client(this, mNumberOfClients++); |
| 46 | mClientsStarted.push(false); |
| 47 | mClientsDesiredReadBit.push(0); |
| 48 | return client; |
| 49 | } |
| 50 | |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 51 | status_t MediaSourceSplitter::start(int clientId, MetaData *params) { |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 52 | Mutex::Autolock autoLock(mLock); |
| 53 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 54 | ALOGV("start client (%d)", clientId); |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 55 | if (mClientsStarted[clientId]) { |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 56 | return OK; |
| 57 | } |
| 58 | |
| 59 | mNumberOfClientsStarted++; |
| 60 | |
| 61 | if (!mSourceStarted) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 62 | ALOGV("Starting real source from client (%d)", clientId); |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 63 | status_t err = mSource->start(params); |
| 64 | |
| 65 | if (err == OK) { |
| 66 | mSourceStarted = true; |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 67 | mClientsStarted.editItemAt(clientId) = true; |
| 68 | mClientsDesiredReadBit.editItemAt(clientId) = !mCurrentReadBit; |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | return err; |
| 72 | } else { |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 73 | mClientsStarted.editItemAt(clientId) = true; |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 74 | if (mLastReadCompleted) { |
| 75 | // Last read was completed. So join in the threads for the next read. |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 76 | mClientsDesiredReadBit.editItemAt(clientId) = !mCurrentReadBit; |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 77 | } else { |
| 78 | // Last read is ongoing. So join in the threads for the current read. |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 79 | mClientsDesiredReadBit.editItemAt(clientId) = mCurrentReadBit; |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 80 | } |
| 81 | return OK; |
| 82 | } |
| 83 | } |
| 84 | |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 85 | status_t MediaSourceSplitter::stop(int clientId) { |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 86 | Mutex::Autolock autoLock(mLock); |
| 87 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 88 | ALOGV("stop client (%d)", clientId); |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 89 | CHECK(clientId >= 0 && clientId < mNumberOfClients); |
| 90 | CHECK(mClientsStarted[clientId]); |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 91 | |
| 92 | if (--mNumberOfClientsStarted == 0) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 93 | ALOGV("Stopping real source from client (%d)", clientId); |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 94 | status_t err = mSource->stop(); |
| 95 | mSourceStarted = false; |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 96 | mClientsStarted.editItemAt(clientId) = false; |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 97 | return err; |
| 98 | } else { |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 99 | mClientsStarted.editItemAt(clientId) = false; |
| 100 | if (!mLastReadCompleted && (mClientsDesiredReadBit[clientId] == mCurrentReadBit)) { |
| 101 | // !mLastReadCompleted implies that buffer has been read from source, but all |
| 102 | // clients haven't read it. |
| 103 | // mClientsDesiredReadBit[clientId] == mCurrentReadBit implies that this |
| 104 | // client would have wanted to read from this buffer. (i.e. it has not yet |
| 105 | // called read() for the current read buffer.) |
| 106 | // Since other threads may be waiting for all the clients' reads to complete, |
| 107 | // signal that this read has been aborted. |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 108 | signalReadComplete_lock(true); |
| 109 | } |
| 110 | return OK; |
| 111 | } |
| 112 | } |
| 113 | |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 114 | sp<MetaData> MediaSourceSplitter::getFormat(int clientId) { |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 115 | Mutex::Autolock autoLock(mLock); |
| 116 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 117 | ALOGV("getFormat client (%d)", clientId); |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 118 | return mSource->getFormat(); |
| 119 | } |
| 120 | |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 121 | status_t MediaSourceSplitter::read(int clientId, |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 122 | MediaBuffer **buffer, const MediaSource::ReadOptions *options) { |
| 123 | Mutex::Autolock autoLock(mLock); |
| 124 | |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 125 | CHECK(clientId >= 0 && clientId < mNumberOfClients); |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 126 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 127 | ALOGV("read client (%d)", clientId); |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 128 | *buffer = NULL; |
| 129 | |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 130 | if (!mClientsStarted[clientId]) { |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 131 | return OK; |
| 132 | } |
| 133 | |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 134 | if (mCurrentReadBit != mClientsDesiredReadBit[clientId]) { |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 135 | // Desired buffer has not been read from source yet. |
| 136 | |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 137 | // If the current client is the special client with clientId = 0 |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 138 | // then read from source, else wait until the client 0 has finished |
| 139 | // reading from source. |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 140 | if (clientId == 0) { |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 141 | // Wait for all client's last read to complete first so as to not |
| 142 | // corrupt the buffer at mLastReadMediaBuffer. |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 143 | waitForAllClientsLastRead_lock(clientId); |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 144 | |
| 145 | readFromSource_lock(options); |
| 146 | *buffer = mLastReadMediaBuffer; |
| 147 | } else { |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 148 | waitForReadFromSource_lock(clientId); |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 149 | |
| 150 | *buffer = mLastReadMediaBuffer; |
| 151 | (*buffer)->add_ref(); |
| 152 | } |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 153 | CHECK(mCurrentReadBit == mClientsDesiredReadBit[clientId]); |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 154 | } else { |
| 155 | // Desired buffer has already been read from source. Use the cached data. |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 156 | CHECK(clientId != 0); |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 157 | |
| 158 | *buffer = mLastReadMediaBuffer; |
| 159 | (*buffer)->add_ref(); |
| 160 | } |
| 161 | |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 162 | mClientsDesiredReadBit.editItemAt(clientId) = !mClientsDesiredReadBit[clientId]; |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 163 | signalReadComplete_lock(false); |
| 164 | |
| 165 | return mLastReadStatus; |
| 166 | } |
| 167 | |
| 168 | void MediaSourceSplitter::readFromSource_lock(const MediaSource::ReadOptions *options) { |
| 169 | mLastReadStatus = mSource->read(&mLastReadMediaBuffer , options); |
| 170 | |
| 171 | mCurrentReadBit = !mCurrentReadBit; |
| 172 | mLastReadCompleted = false; |
| 173 | mReadFromSourceCondition.broadcast(); |
| 174 | } |
| 175 | |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 176 | void MediaSourceSplitter::waitForReadFromSource_lock(int32_t clientId) { |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 177 | mReadFromSourceCondition.wait(mLock); |
| 178 | } |
| 179 | |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 180 | void MediaSourceSplitter::waitForAllClientsLastRead_lock(int32_t clientId) { |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 181 | if (mLastReadCompleted) { |
| 182 | return; |
| 183 | } |
| 184 | mAllReadsCompleteCondition.wait(mLock); |
| 185 | CHECK(mLastReadCompleted); |
| 186 | } |
| 187 | |
| 188 | void MediaSourceSplitter::signalReadComplete_lock(bool readAborted) { |
| 189 | if (!readAborted) { |
| 190 | mNumberOfCurrentReads++; |
| 191 | } |
| 192 | |
| 193 | if (mNumberOfCurrentReads == mNumberOfClientsStarted) { |
| 194 | mLastReadCompleted = true; |
| 195 | mNumberOfCurrentReads = 0; |
| 196 | mAllReadsCompleteCondition.broadcast(); |
| 197 | } |
| 198 | } |
| 199 | |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 200 | status_t MediaSourceSplitter::pause(int clientId) { |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 201 | return ERROR_UNSUPPORTED; |
| 202 | } |
| 203 | |
| 204 | // Client |
| 205 | |
| 206 | MediaSourceSplitter::Client::Client( |
| 207 | sp<MediaSourceSplitter> splitter, |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 208 | int32_t clientId) { |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 209 | mSplitter = splitter; |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 210 | mClientId = clientId; |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | status_t MediaSourceSplitter::Client::start(MetaData *params) { |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 214 | return mSplitter->start(mClientId, params); |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | status_t MediaSourceSplitter::Client::stop() { |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 218 | return mSplitter->stop(mClientId); |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | sp<MetaData> MediaSourceSplitter::Client::getFormat() { |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 222 | return mSplitter->getFormat(mClientId); |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | status_t MediaSourceSplitter::Client::read( |
| 226 | MediaBuffer **buffer, const ReadOptions *options) { |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 227 | return mSplitter->read(mClientId, buffer, options); |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | status_t MediaSourceSplitter::Client::pause() { |
Nipun Kwatra | ea434da | 2010-08-27 14:19:01 -0700 | [diff] [blame] | 231 | return mSplitter->pause(mClientId); |
Nipun Kwatra | f83cba7 | 2010-08-26 15:59:19 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | } // namespace android |