The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2008, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | // Proxy for media player implementations |
| 19 | |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | #define LOG_TAG "MediaPlayerService" |
| 22 | #include <utils/Log.h> |
| 23 | |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <dirent.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #include <string.h> |
Mathias Agopian | 6f74b0c | 2009-06-03 17:32:49 -0700 | [diff] [blame] | 30 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | #include <cutils/atomic.h> |
Nicolas Catania | 14d2747 | 2009-07-13 14:37:49 -0700 | [diff] [blame] | 32 | #include <cutils/properties.h> // for property_get |
Mathias Agopian | 6f74b0c | 2009-06-03 17:32:49 -0700 | [diff] [blame] | 33 | |
| 34 | #include <utils/misc.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
| 36 | #include <android_runtime/ActivityManager.h> |
Mathias Agopian | 6f74b0c | 2009-06-03 17:32:49 -0700 | [diff] [blame] | 37 | |
Mathias Agopian | 7562408 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 38 | #include <binder/IPCThreadState.h> |
| 39 | #include <binder/IServiceManager.h> |
| 40 | #include <binder/MemoryHeapBase.h> |
| 41 | #include <binder/MemoryBase.h> |
Nicolas Catania | 1d187f1 | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 42 | #include <utils/Errors.h> // for status_t |
| 43 | #include <utils/String8.h> |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 44 | #include <utils/SystemClock.h> |
Nicolas Catania | 1d187f1 | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 45 | #include <utils/Vector.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | #include <cutils/properties.h> |
| 47 | |
| 48 | #include <media/MediaPlayerInterface.h> |
| 49 | #include <media/mediarecorder.h> |
| 50 | #include <media/MediaMetadataRetrieverInterface.h> |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 51 | #include <media/Metadata.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | #include <media/AudioTrack.h> |
| 53 | |
| 54 | #include "MediaRecorderClient.h" |
| 55 | #include "MediaPlayerService.h" |
| 56 | #include "MetadataRetrieverClient.h" |
| 57 | |
| 58 | #include "MidiFile.h" |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | #include <media/PVPlayer.h> |
Nicolas Catania | 14d2747 | 2009-07-13 14:37:49 -0700 | [diff] [blame] | 60 | #include "TestPlayerStub.h" |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 61 | #include "StagefrightPlayer.h" |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 62 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 63 | #include <OMX.h> |
Nicolas Catania | 14d2747 | 2009-07-13 14:37:49 -0700 | [diff] [blame] | 64 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | /* desktop Linux needs a little help with gettid() */ |
| 66 | #if defined(HAVE_GETTID) && !defined(HAVE_ANDROID_OS) |
| 67 | #define __KERNEL__ |
| 68 | # include <linux/unistd.h> |
| 69 | #ifdef _syscall0 |
| 70 | _syscall0(pid_t,gettid) |
| 71 | #else |
| 72 | pid_t gettid() { return syscall(__NR_gettid);} |
| 73 | #endif |
| 74 | #undef __KERNEL__ |
| 75 | #endif |
| 76 | |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 77 | namespace { |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 78 | using android::media::Metadata; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 79 | using android::status_t; |
| 80 | using android::OK; |
| 81 | using android::BAD_VALUE; |
| 82 | using android::NOT_ENOUGH_DATA; |
| 83 | using android::Parcel; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 84 | |
| 85 | // Max number of entries in the filter. |
| 86 | const int kMaxFilterSize = 64; // I pulled that out of thin air. |
| 87 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 88 | // FIXME: Move all the metadata related function in the Metadata.cpp |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 89 | |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 90 | |
| 91 | // Unmarshall a filter from a Parcel. |
| 92 | // Filter format in a parcel: |
| 93 | // |
| 94 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 95 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 96 | // | number of entries (n) | |
| 97 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 98 | // | metadata type 1 | |
| 99 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 100 | // | metadata type 2 | |
| 101 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 102 | // .... |
| 103 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 104 | // | metadata type n | |
| 105 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 106 | // |
| 107 | // @param p Parcel that should start with a filter. |
| 108 | // @param[out] filter On exit contains the list of metadata type to be |
| 109 | // filtered. |
| 110 | // @param[out] status On exit contains the status code to be returned. |
| 111 | // @return true if the parcel starts with a valid filter. |
| 112 | bool unmarshallFilter(const Parcel& p, |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 113 | Metadata::Filter *filter, |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 114 | status_t *status) |
| 115 | { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 116 | int32_t val; |
| 117 | if (p.readInt32(&val) != OK) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 118 | { |
| 119 | LOGE("Failed to read filter's length"); |
| 120 | *status = NOT_ENOUGH_DATA; |
| 121 | return false; |
| 122 | } |
| 123 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 124 | if( val > kMaxFilterSize || val < 0) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 125 | { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 126 | LOGE("Invalid filter len %d", val); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 127 | *status = BAD_VALUE; |
| 128 | return false; |
| 129 | } |
| 130 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 131 | const size_t num = val; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 132 | |
| 133 | filter->clear(); |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 134 | filter->setCapacity(num); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 135 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 136 | size_t size = num * sizeof(Metadata::Type); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 137 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 138 | |
| 139 | if (p.dataAvail() < size) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 140 | { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 141 | LOGE("Filter too short expected %d but got %d", size, p.dataAvail()); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 142 | *status = NOT_ENOUGH_DATA; |
| 143 | return false; |
| 144 | } |
| 145 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 146 | const Metadata::Type *data = |
| 147 | static_cast<const Metadata::Type*>(p.readInplace(size)); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 148 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 149 | if (NULL == data) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 150 | { |
| 151 | LOGE("Filter had no data"); |
| 152 | *status = BAD_VALUE; |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | // TODO: The stl impl of vector would be more efficient here |
| 157 | // because it degenerates into a memcpy on pod types. Try to |
| 158 | // replace later or use stl::set. |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 159 | for (size_t i = 0; i < num; ++i) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 160 | { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 161 | filter->add(*data); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 162 | ++data; |
| 163 | } |
| 164 | *status = OK; |
| 165 | return true; |
| 166 | } |
| 167 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 168 | // @param filter Of metadata type. |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 169 | // @param val To be searched. |
| 170 | // @return true if a match was found. |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 171 | bool findMetadata(const Metadata::Filter& filter, const int32_t val) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 172 | { |
| 173 | // Deal with empty and ANY right away |
| 174 | if (filter.isEmpty()) return false; |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 175 | if (filter[0] == Metadata::kAny) return true; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 176 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 177 | return filter.indexOf(val) >= 0; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | } // anonymous namespace |
| 181 | |
| 182 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | namespace android { |
| 184 | |
| 185 | // TODO: Temp hack until we can register players |
| 186 | typedef struct { |
| 187 | const char *extension; |
| 188 | const player_type playertype; |
| 189 | } extmap; |
| 190 | extmap FILE_EXTS [] = { |
| 191 | {".mid", SONIVOX_PLAYER}, |
| 192 | {".midi", SONIVOX_PLAYER}, |
| 193 | {".smf", SONIVOX_PLAYER}, |
| 194 | {".xmf", SONIVOX_PLAYER}, |
| 195 | {".imy", SONIVOX_PLAYER}, |
| 196 | {".rtttl", SONIVOX_PLAYER}, |
| 197 | {".rtx", SONIVOX_PLAYER}, |
| 198 | {".ota", SONIVOX_PLAYER}, |
Andreas Huber | b49676a | 2010-01-20 16:11:15 -0800 | [diff] [blame] | 199 | #ifndef NO_OPENCORE |
| 200 | {".wma", PV_PLAYER}, |
| 201 | {".wmv", PV_PLAYER}, |
| 202 | {".asf", PV_PLAYER}, |
| 203 | #endif |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | // TODO: Find real cause of Audio/Video delay in PV framework and remove this workaround |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | /* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4; |
| 208 | /* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false; |
| 209 | |
| 210 | void MediaPlayerService::instantiate() { |
| 211 | defaultServiceManager()->addService( |
| 212 | String16("media.player"), new MediaPlayerService()); |
| 213 | } |
| 214 | |
| 215 | MediaPlayerService::MediaPlayerService() |
| 216 | { |
| 217 | LOGV("MediaPlayerService created"); |
| 218 | mNextConnId = 1; |
| 219 | } |
| 220 | |
| 221 | MediaPlayerService::~MediaPlayerService() |
| 222 | { |
| 223 | LOGV("MediaPlayerService destroyed"); |
| 224 | } |
| 225 | |
| 226 | sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(pid_t pid) |
| 227 | { |
Gloria Wang | dac6a31 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 228 | sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid); |
| 229 | wp<MediaRecorderClient> w = recorder; |
| 230 | Mutex::Autolock lock(mLock); |
| 231 | mMediaRecorderClients.add(w); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | LOGV("Create new media recorder client from pid %d", pid); |
| 233 | return recorder; |
| 234 | } |
| 235 | |
Gloria Wang | dac6a31 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 236 | void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client) |
| 237 | { |
| 238 | Mutex::Autolock lock(mLock); |
| 239 | mMediaRecorderClients.remove(client); |
| 240 | LOGV("Delete media recorder client"); |
| 241 | } |
| 242 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever(pid_t pid) |
| 244 | { |
| 245 | sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid); |
| 246 | LOGV("Create new media retriever from pid %d", pid); |
| 247 | return retriever; |
| 248 | } |
| 249 | |
Andreas Huber | 2db8455 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 250 | sp<IMediaPlayer> MediaPlayerService::create( |
| 251 | pid_t pid, const sp<IMediaPlayerClient>& client, const char* url, |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 252 | const KeyedVector<String8, String8> *headers, int audioSessionId) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | { |
| 254 | int32_t connId = android_atomic_inc(&mNextConnId); |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 255 | sp<Client> c = new Client(this, pid, connId, client, audioSessionId); |
| 256 | LOGV("Create new client(%d) from pid %d, url=%s, connId=%d, audioSessionId=%d", |
| 257 | connId, pid, url, connId, audioSessionId); |
Andreas Huber | 2db8455 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 258 | if (NO_ERROR != c->setDataSource(url, headers)) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | { |
| 260 | c.clear(); |
| 261 | return c; |
| 262 | } |
| 263 | wp<Client> w = c; |
| 264 | Mutex::Autolock lock(mLock); |
| 265 | mClients.add(w); |
| 266 | return c; |
| 267 | } |
| 268 | |
| 269 | sp<IMediaPlayer> MediaPlayerService::create(pid_t pid, const sp<IMediaPlayerClient>& client, |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 270 | int fd, int64_t offset, int64_t length, int audioSessionId) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | { |
| 272 | int32_t connId = android_atomic_inc(&mNextConnId); |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 273 | sp<Client> c = new Client(this, pid, connId, client, audioSessionId); |
| 274 | LOGV("Create new client(%d) from pid %d, fd=%d, offset=%lld, length=%lld, audioSessionId=%d", |
| 275 | connId, pid, fd, offset, length, audioSessionId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | if (NO_ERROR != c->setDataSource(fd, offset, length)) { |
| 277 | c.clear(); |
| 278 | } else { |
| 279 | wp<Client> w = c; |
| 280 | Mutex::Autolock lock(mLock); |
| 281 | mClients.add(w); |
| 282 | } |
| 283 | ::close(fd); |
| 284 | return c; |
| 285 | } |
| 286 | |
Andreas Huber | 318ad9c | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 287 | sp<IOMX> MediaPlayerService::getOMX() { |
| 288 | Mutex::Autolock autoLock(mLock); |
| 289 | |
| 290 | if (mOMX.get() == NULL) { |
| 291 | mOMX = new OMX; |
| 292 | } |
| 293 | |
| 294 | return mOMX; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 295 | } |
| 296 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const |
| 298 | { |
| 299 | const size_t SIZE = 256; |
| 300 | char buffer[SIZE]; |
| 301 | String8 result; |
| 302 | |
| 303 | result.append(" AudioCache\n"); |
| 304 | if (mHeap != 0) { |
| 305 | snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d), device(%s)\n", |
| 306 | mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice()); |
| 307 | result.append(buffer); |
| 308 | } |
| 309 | snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%ld)\n", |
| 310 | mMsecsPerFrame, mChannelCount, mFormat, mFrameCount); |
| 311 | result.append(buffer); |
| 312 | snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n", |
| 313 | mSampleRate, mSize, mError, mCommandComplete?"true":"false"); |
| 314 | result.append(buffer); |
| 315 | ::write(fd, result.string(), result.size()); |
| 316 | return NO_ERROR; |
| 317 | } |
| 318 | |
| 319 | status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const |
| 320 | { |
| 321 | const size_t SIZE = 256; |
| 322 | char buffer[SIZE]; |
| 323 | String8 result; |
| 324 | |
| 325 | result.append(" AudioOutput\n"); |
| 326 | snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", |
| 327 | mStreamType, mLeftVolume, mRightVolume); |
| 328 | result.append(buffer); |
| 329 | snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n", |
| 330 | mMsecsPerFrame, mLatency); |
| 331 | result.append(buffer); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 332 | snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n", |
| 333 | mAuxEffectId, mSendLevel); |
| 334 | result.append(buffer); |
| 335 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | ::write(fd, result.string(), result.size()); |
| 337 | if (mTrack != 0) { |
| 338 | mTrack->dump(fd, args); |
| 339 | } |
| 340 | return NO_ERROR; |
| 341 | } |
| 342 | |
| 343 | status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const |
| 344 | { |
| 345 | const size_t SIZE = 256; |
| 346 | char buffer[SIZE]; |
| 347 | String8 result; |
| 348 | result.append(" Client\n"); |
| 349 | snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n", |
| 350 | mPid, mConnId, mStatus, mLoop?"true": "false"); |
| 351 | result.append(buffer); |
| 352 | write(fd, result.string(), result.size()); |
| 353 | if (mAudioOutput != 0) { |
| 354 | mAudioOutput->dump(fd, args); |
| 355 | } |
| 356 | write(fd, "\n", 1); |
| 357 | return NO_ERROR; |
| 358 | } |
| 359 | |
| 360 | static int myTid() { |
| 361 | #ifdef HAVE_GETTID |
| 362 | return gettid(); |
| 363 | #else |
| 364 | return getpid(); |
| 365 | #endif |
| 366 | } |
| 367 | |
| 368 | #if defined(__arm__) |
| 369 | extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize, |
| 370 | size_t* infoSize, size_t* totalMemory, size_t* backtraceSize); |
| 371 | extern "C" void free_malloc_leak_info(uint8_t* info); |
| 372 | |
Andreas Huber | 1eea7f5 | 2009-10-27 15:50:04 -0700 | [diff] [blame] | 373 | // Use the String-class below instead of String8 to allocate all memory |
| 374 | // beforehand and not reenter the heap while we are examining it... |
| 375 | struct MyString8 { |
| 376 | static const size_t MAX_SIZE = 256 * 1024; |
| 377 | |
| 378 | MyString8() |
| 379 | : mPtr((char *)malloc(MAX_SIZE)) { |
| 380 | *mPtr = '\0'; |
| 381 | } |
| 382 | |
| 383 | ~MyString8() { |
| 384 | free(mPtr); |
| 385 | } |
| 386 | |
| 387 | void append(const char *s) { |
| 388 | strcat(mPtr, s); |
| 389 | } |
| 390 | |
| 391 | const char *string() const { |
| 392 | return mPtr; |
| 393 | } |
| 394 | |
| 395 | size_t size() const { |
| 396 | return strlen(mPtr); |
| 397 | } |
| 398 | |
| 399 | private: |
| 400 | char *mPtr; |
| 401 | |
| 402 | MyString8(const MyString8 &); |
| 403 | MyString8 &operator=(const MyString8 &); |
| 404 | }; |
| 405 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | void memStatus(int fd, const Vector<String16>& args) |
| 407 | { |
| 408 | const size_t SIZE = 256; |
| 409 | char buffer[SIZE]; |
Andreas Huber | 1eea7f5 | 2009-10-27 15:50:04 -0700 | [diff] [blame] | 410 | MyString8 result; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | |
| 412 | typedef struct { |
| 413 | size_t size; |
| 414 | size_t dups; |
| 415 | intptr_t * backtrace; |
| 416 | } AllocEntry; |
| 417 | |
| 418 | uint8_t *info = NULL; |
| 419 | size_t overallSize = 0; |
| 420 | size_t infoSize = 0; |
| 421 | size_t totalMemory = 0; |
| 422 | size_t backtraceSize = 0; |
| 423 | |
| 424 | get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory, &backtraceSize); |
| 425 | if (info) { |
| 426 | uint8_t *ptr = info; |
| 427 | size_t count = overallSize / infoSize; |
| 428 | |
| 429 | snprintf(buffer, SIZE, " Allocation count %i\n", count); |
| 430 | result.append(buffer); |
James Dong | 1edee16 | 2010-02-25 10:06:32 -0800 | [diff] [blame] | 431 | snprintf(buffer, SIZE, " Total memory %i\n", totalMemory); |
| 432 | result.append(buffer); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | |
| 434 | AllocEntry * entries = new AllocEntry[count]; |
| 435 | |
| 436 | for (size_t i = 0; i < count; i++) { |
| 437 | // Each entry should be size_t, size_t, intptr_t[backtraceSize] |
| 438 | AllocEntry *e = &entries[i]; |
| 439 | |
| 440 | e->size = *reinterpret_cast<size_t *>(ptr); |
| 441 | ptr += sizeof(size_t); |
| 442 | |
| 443 | e->dups = *reinterpret_cast<size_t *>(ptr); |
| 444 | ptr += sizeof(size_t); |
| 445 | |
| 446 | e->backtrace = reinterpret_cast<intptr_t *>(ptr); |
| 447 | ptr += sizeof(intptr_t) * backtraceSize; |
| 448 | } |
| 449 | |
| 450 | // Now we need to sort the entries. They come sorted by size but |
| 451 | // not by stack trace which causes problems using diff. |
| 452 | bool moved; |
| 453 | do { |
| 454 | moved = false; |
| 455 | for (size_t i = 0; i < (count - 1); i++) { |
| 456 | AllocEntry *e1 = &entries[i]; |
| 457 | AllocEntry *e2 = &entries[i+1]; |
| 458 | |
| 459 | bool swap = e1->size < e2->size; |
| 460 | if (e1->size == e2->size) { |
| 461 | for(size_t j = 0; j < backtraceSize; j++) { |
| 462 | if (e1->backtrace[j] == e2->backtrace[j]) { |
| 463 | continue; |
| 464 | } |
| 465 | swap = e1->backtrace[j] < e2->backtrace[j]; |
| 466 | break; |
| 467 | } |
| 468 | } |
| 469 | if (swap) { |
| 470 | AllocEntry t = entries[i]; |
| 471 | entries[i] = entries[i+1]; |
| 472 | entries[i+1] = t; |
| 473 | moved = true; |
| 474 | } |
| 475 | } |
| 476 | } while (moved); |
| 477 | |
| 478 | for (size_t i = 0; i < count; i++) { |
| 479 | AllocEntry *e = &entries[i]; |
| 480 | |
James Dong | 1edee16 | 2010-02-25 10:06:32 -0800 | [diff] [blame] | 481 | snprintf(buffer, SIZE, "size %8i, dup %4i, ", e->size, e->dups); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | result.append(buffer); |
| 483 | for (size_t ct = 0; (ct < backtraceSize) && e->backtrace[ct]; ct++) { |
| 484 | if (ct) { |
| 485 | result.append(", "); |
| 486 | } |
| 487 | snprintf(buffer, SIZE, "0x%08x", e->backtrace[ct]); |
| 488 | result.append(buffer); |
| 489 | } |
| 490 | result.append("\n"); |
| 491 | } |
| 492 | |
| 493 | delete[] entries; |
| 494 | free_malloc_leak_info(info); |
| 495 | } |
| 496 | |
| 497 | write(fd, result.string(), result.size()); |
| 498 | } |
| 499 | #endif |
| 500 | |
| 501 | status_t MediaPlayerService::dump(int fd, const Vector<String16>& args) |
| 502 | { |
| 503 | const size_t SIZE = 256; |
| 504 | char buffer[SIZE]; |
| 505 | String8 result; |
| 506 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 507 | snprintf(buffer, SIZE, "Permission Denial: " |
| 508 | "can't dump MediaPlayerService from pid=%d, uid=%d\n", |
| 509 | IPCThreadState::self()->getCallingPid(), |
| 510 | IPCThreadState::self()->getCallingUid()); |
| 511 | result.append(buffer); |
| 512 | } else { |
| 513 | Mutex::Autolock lock(mLock); |
| 514 | for (int i = 0, n = mClients.size(); i < n; ++i) { |
| 515 | sp<Client> c = mClients[i].promote(); |
| 516 | if (c != 0) c->dump(fd, args); |
| 517 | } |
James Dong | b914122 | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 518 | if (mMediaRecorderClients.size() == 0) { |
| 519 | result.append(" No media recorder client\n\n"); |
| 520 | } else { |
| 521 | for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) { |
| 522 | sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote(); |
| 523 | snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid); |
| 524 | result.append(buffer); |
| 525 | write(fd, result.string(), result.size()); |
| 526 | result = "\n"; |
| 527 | c->dump(fd, args); |
| 528 | } |
Gloria Wang | dac6a31 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 529 | } |
| 530 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | result.append(" Files opened and/or mapped:\n"); |
| 532 | snprintf(buffer, SIZE, "/proc/%d/maps", myTid()); |
| 533 | FILE *f = fopen(buffer, "r"); |
| 534 | if (f) { |
| 535 | while (!feof(f)) { |
| 536 | fgets(buffer, SIZE, f); |
| 537 | if (strstr(buffer, " /sdcard/") || |
| 538 | strstr(buffer, " /system/sounds/") || |
| 539 | strstr(buffer, " /system/media/")) { |
| 540 | result.append(" "); |
| 541 | result.append(buffer); |
| 542 | } |
| 543 | } |
| 544 | fclose(f); |
| 545 | } else { |
| 546 | result.append("couldn't open "); |
| 547 | result.append(buffer); |
| 548 | result.append("\n"); |
| 549 | } |
| 550 | |
| 551 | snprintf(buffer, SIZE, "/proc/%d/fd", myTid()); |
| 552 | DIR *d = opendir(buffer); |
| 553 | if (d) { |
| 554 | struct dirent *ent; |
| 555 | while((ent = readdir(d)) != NULL) { |
| 556 | if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) { |
| 557 | snprintf(buffer, SIZE, "/proc/%d/fd/%s", myTid(), ent->d_name); |
| 558 | struct stat s; |
| 559 | if (lstat(buffer, &s) == 0) { |
| 560 | if ((s.st_mode & S_IFMT) == S_IFLNK) { |
| 561 | char linkto[256]; |
| 562 | int len = readlink(buffer, linkto, sizeof(linkto)); |
| 563 | if(len > 0) { |
| 564 | if(len > 255) { |
| 565 | linkto[252] = '.'; |
| 566 | linkto[253] = '.'; |
| 567 | linkto[254] = '.'; |
| 568 | linkto[255] = 0; |
| 569 | } else { |
| 570 | linkto[len] = 0; |
| 571 | } |
| 572 | if (strstr(linkto, "/sdcard/") == linkto || |
| 573 | strstr(linkto, "/system/sounds/") == linkto || |
| 574 | strstr(linkto, "/system/media/") == linkto) { |
| 575 | result.append(" "); |
| 576 | result.append(buffer); |
| 577 | result.append(" -> "); |
| 578 | result.append(linkto); |
| 579 | result.append("\n"); |
| 580 | } |
| 581 | } |
| 582 | } else { |
| 583 | result.append(" unexpected type for "); |
| 584 | result.append(buffer); |
| 585 | result.append("\n"); |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | closedir(d); |
| 591 | } else { |
| 592 | result.append("couldn't open "); |
| 593 | result.append(buffer); |
| 594 | result.append("\n"); |
| 595 | } |
| 596 | |
| 597 | #if defined(__arm__) |
| 598 | bool dumpMem = false; |
| 599 | for (size_t i = 0; i < args.size(); i++) { |
| 600 | if (args[i] == String16("-m")) { |
| 601 | dumpMem = true; |
| 602 | } |
| 603 | } |
| 604 | if (dumpMem) { |
| 605 | memStatus(fd, args); |
| 606 | } |
| 607 | #endif |
| 608 | } |
| 609 | write(fd, result.string(), result.size()); |
| 610 | return NO_ERROR; |
| 611 | } |
| 612 | |
| 613 | void MediaPlayerService::removeClient(wp<Client> client) |
| 614 | { |
| 615 | Mutex::Autolock lock(mLock); |
| 616 | mClients.remove(client); |
| 617 | } |
| 618 | |
| 619 | MediaPlayerService::Client::Client(const sp<MediaPlayerService>& service, pid_t pid, |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 620 | int32_t connId, const sp<IMediaPlayerClient>& client, int audioSessionId) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 621 | { |
| 622 | LOGV("Client(%d) constructor", connId); |
| 623 | mPid = pid; |
| 624 | mConnId = connId; |
| 625 | mService = service; |
| 626 | mClient = client; |
| 627 | mLoop = false; |
| 628 | mStatus = NO_INIT; |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 629 | mAudioSessionId = audioSessionId; |
| 630 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | #if CALLBACK_ANTAGONIZER |
| 632 | LOGD("create Antagonizer"); |
| 633 | mAntagonizer = new Antagonizer(notify, this); |
| 634 | #endif |
| 635 | } |
| 636 | |
| 637 | MediaPlayerService::Client::~Client() |
| 638 | { |
| 639 | LOGV("Client(%d) destructor pid = %d", mConnId, mPid); |
| 640 | mAudioOutput.clear(); |
| 641 | wp<Client> client(this); |
| 642 | disconnect(); |
| 643 | mService->removeClient(client); |
| 644 | } |
| 645 | |
| 646 | void MediaPlayerService::Client::disconnect() |
| 647 | { |
| 648 | LOGV("disconnect(%d) from pid %d", mConnId, mPid); |
| 649 | // grab local reference and clear main reference to prevent future |
| 650 | // access to object |
| 651 | sp<MediaPlayerBase> p; |
| 652 | { |
| 653 | Mutex::Autolock l(mLock); |
| 654 | p = mPlayer; |
| 655 | } |
Dave Sparks | 795fa58 | 2009-03-24 17:57:12 -0700 | [diff] [blame] | 656 | mClient.clear(); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 657 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 658 | mPlayer.clear(); |
| 659 | |
| 660 | // clear the notification to prevent callbacks to dead client |
| 661 | // and reset the player. We assume the player will serialize |
| 662 | // access to itself if necessary. |
| 663 | if (p != 0) { |
| 664 | p->setNotifyCallback(0, 0); |
| 665 | #if CALLBACK_ANTAGONIZER |
| 666 | LOGD("kill Antagonizer"); |
| 667 | mAntagonizer->kill(); |
| 668 | #endif |
| 669 | p->reset(); |
| 670 | } |
| 671 | |
| 672 | IPCThreadState::self()->flushCommands(); |
| 673 | } |
| 674 | |
Andreas Huber | 47f59cf | 2009-08-07 09:30:32 -0700 | [diff] [blame] | 675 | static player_type getDefaultPlayerType() { |
Andreas Huber | 608d77b | 2010-06-23 16:40:57 -0700 | [diff] [blame] | 676 | return STAGEFRIGHT_PLAYER; |
Andreas Huber | 47f59cf | 2009-08-07 09:30:32 -0700 | [diff] [blame] | 677 | } |
| 678 | |
James Dong | 148c1a2 | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 679 | player_type getPlayerType(int fd, int64_t offset, int64_t length) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 680 | { |
| 681 | char buf[20]; |
| 682 | lseek(fd, offset, SEEK_SET); |
| 683 | read(fd, buf, sizeof(buf)); |
| 684 | lseek(fd, offset, SEEK_SET); |
| 685 | |
| 686 | long ident = *((long*)buf); |
| 687 | |
| 688 | // Ogg vorbis? |
| 689 | if (ident == 0x5367674f) // 'OggS' |
Andreas Huber | 608d77b | 2010-06-23 16:40:57 -0700 | [diff] [blame] | 690 | return STAGEFRIGHT_PLAYER; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 691 | |
Andreas Huber | b49676a | 2010-01-20 16:11:15 -0800 | [diff] [blame] | 692 | #ifndef NO_OPENCORE |
| 693 | if (ident == 0x75b22630) { |
| 694 | // The magic number for .asf files, i.e. wmv and wma content. |
| 695 | // These are not currently supported through stagefright. |
| 696 | return PV_PLAYER; |
| 697 | } |
| 698 | #endif |
| 699 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 700 | // Some kind of MIDI? |
| 701 | EAS_DATA_HANDLE easdata; |
| 702 | if (EAS_Init(&easdata) == EAS_SUCCESS) { |
| 703 | EAS_FILE locator; |
| 704 | locator.path = NULL; |
| 705 | locator.fd = fd; |
| 706 | locator.offset = offset; |
| 707 | locator.length = length; |
| 708 | EAS_HANDLE eashandle; |
| 709 | if (EAS_OpenFile(easdata, &locator, &eashandle) == EAS_SUCCESS) { |
| 710 | EAS_CloseFile(easdata, eashandle); |
| 711 | EAS_Shutdown(easdata); |
| 712 | return SONIVOX_PLAYER; |
| 713 | } |
| 714 | EAS_Shutdown(easdata); |
| 715 | } |
| 716 | |
Andreas Huber | 47f59cf | 2009-08-07 09:30:32 -0700 | [diff] [blame] | 717 | return getDefaultPlayerType(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 718 | } |
| 719 | |
James Dong | 148c1a2 | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 720 | player_type getPlayerType(const char* url) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | { |
Nicolas Catania | 14d2747 | 2009-07-13 14:37:49 -0700 | [diff] [blame] | 722 | if (TestPlayerStub::canBeUsed(url)) { |
| 723 | return TEST_PLAYER; |
| 724 | } |
| 725 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | // use MidiFile for MIDI extensions |
| 727 | int lenURL = strlen(url); |
| 728 | for (int i = 0; i < NELEM(FILE_EXTS); ++i) { |
| 729 | int len = strlen(FILE_EXTS[i].extension); |
| 730 | int start = lenURL - len; |
| 731 | if (start > 0) { |
Atsushi Eno | fc1c7b9 | 2010-03-19 23:18:02 +0900 | [diff] [blame] | 732 | if (!strncasecmp(url + start, FILE_EXTS[i].extension, len)) { |
Andreas Huber | 608d77b | 2010-06-23 16:40:57 -0700 | [diff] [blame] | 733 | return FILE_EXTS[i].playertype; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | } |
| 737 | |
James Dong | 27fde95 | 2010-04-13 21:33:26 -0700 | [diff] [blame] | 738 | if (!strncasecmp(url, "rtsp://", 7)) { |
Andreas Huber | cf7b9c7 | 2010-06-07 15:19:40 -0700 | [diff] [blame] | 739 | char value[PROPERTY_VALUE_MAX]; |
| 740 | if (!property_get("media.stagefright.enable-rtsp", value, NULL) |
| 741 | || (strcmp(value, "1") && strcasecmp(value, "true"))) { |
| 742 | // For now, we're going to use PV for rtsp-based playback |
| 743 | // by default until we can clear up a few more issues. |
| 744 | return PV_PLAYER; |
| 745 | } |
James Dong | 27fde95 | 2010-04-13 21:33:26 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Andreas Huber | 47f59cf | 2009-08-07 09:30:32 -0700 | [diff] [blame] | 748 | return getDefaultPlayerType(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | static sp<MediaPlayerBase> createPlayer(player_type playerType, void* cookie, |
| 752 | notify_callback_f notifyFunc) |
| 753 | { |
| 754 | sp<MediaPlayerBase> p; |
| 755 | switch (playerType) { |
Jean-Baptiste Queru | 6c5b210 | 2009-03-21 11:40:18 -0700 | [diff] [blame] | 756 | #ifndef NO_OPENCORE |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 757 | case PV_PLAYER: |
| 758 | LOGV(" create PVPlayer"); |
| 759 | p = new PVPlayer(); |
| 760 | break; |
Jean-Baptiste Queru | 6c5b210 | 2009-03-21 11:40:18 -0700 | [diff] [blame] | 761 | #endif |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 762 | case SONIVOX_PLAYER: |
| 763 | LOGV(" create MidiFile"); |
| 764 | p = new MidiFile(); |
| 765 | break; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 766 | case STAGEFRIGHT_PLAYER: |
| 767 | LOGV(" create StagefrightPlayer"); |
| 768 | p = new StagefrightPlayer; |
| 769 | break; |
Nicolas Catania | 14d2747 | 2009-07-13 14:37:49 -0700 | [diff] [blame] | 770 | case TEST_PLAYER: |
| 771 | LOGV("Create Test Player stub"); |
| 772 | p = new TestPlayerStub(); |
| 773 | break; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 774 | } |
| 775 | if (p != NULL) { |
| 776 | if (p->initCheck() == NO_ERROR) { |
| 777 | p->setNotifyCallback(cookie, notifyFunc); |
| 778 | } else { |
| 779 | p.clear(); |
| 780 | } |
| 781 | } |
| 782 | if (p == NULL) { |
| 783 | LOGE("Failed to create player object"); |
| 784 | } |
| 785 | return p; |
| 786 | } |
| 787 | |
| 788 | sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType) |
| 789 | { |
| 790 | // determine if we have the right player type |
| 791 | sp<MediaPlayerBase> p = mPlayer; |
| 792 | if ((p != NULL) && (p->playerType() != playerType)) { |
| 793 | LOGV("delete player"); |
| 794 | p.clear(); |
| 795 | } |
| 796 | if (p == NULL) { |
| 797 | p = android::createPlayer(playerType, this, notify); |
| 798 | } |
| 799 | return p; |
| 800 | } |
| 801 | |
Andreas Huber | 2db8455 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 802 | status_t MediaPlayerService::Client::setDataSource( |
| 803 | const char *url, const KeyedVector<String8, String8> *headers) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 804 | { |
| 805 | LOGV("setDataSource(%s)", url); |
| 806 | if (url == NULL) |
| 807 | return UNKNOWN_ERROR; |
| 808 | |
| 809 | if (strncmp(url, "content://", 10) == 0) { |
| 810 | // get a filedescriptor for the content Uri and |
| 811 | // pass it to the setDataSource(fd) method |
| 812 | |
| 813 | String16 url16(url); |
| 814 | int fd = android::openContentProviderFile(url16); |
| 815 | if (fd < 0) |
| 816 | { |
| 817 | LOGE("Couldn't open fd for %s", url); |
| 818 | return UNKNOWN_ERROR; |
| 819 | } |
| 820 | setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus |
| 821 | close(fd); |
| 822 | return mStatus; |
| 823 | } else { |
| 824 | player_type playerType = getPlayerType(url); |
| 825 | LOGV("player type = %d", playerType); |
| 826 | |
| 827 | // create the right type of player |
| 828 | sp<MediaPlayerBase> p = createPlayer(playerType); |
| 829 | if (p == NULL) return NO_INIT; |
| 830 | |
| 831 | if (!p->hardwareOutput()) { |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 832 | mAudioOutput = new AudioOutput(mAudioSessionId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 833 | static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput); |
| 834 | } |
| 835 | |
| 836 | // now set data source |
| 837 | LOGV(" setDataSource"); |
Andreas Huber | 2db8455 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 838 | mStatus = p->setDataSource(url, headers); |
Nicolas Catania | 14d2747 | 2009-07-13 14:37:49 -0700 | [diff] [blame] | 839 | if (mStatus == NO_ERROR) { |
| 840 | mPlayer = p; |
| 841 | } else { |
| 842 | LOGE(" error: %d", mStatus); |
| 843 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 844 | return mStatus; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length) |
| 849 | { |
| 850 | LOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length); |
| 851 | struct stat sb; |
| 852 | int ret = fstat(fd, &sb); |
| 853 | if (ret != 0) { |
| 854 | LOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno)); |
| 855 | return UNKNOWN_ERROR; |
| 856 | } |
| 857 | |
| 858 | LOGV("st_dev = %llu", sb.st_dev); |
| 859 | LOGV("st_mode = %u", sb.st_mode); |
| 860 | LOGV("st_uid = %lu", sb.st_uid); |
| 861 | LOGV("st_gid = %lu", sb.st_gid); |
| 862 | LOGV("st_size = %llu", sb.st_size); |
| 863 | |
| 864 | if (offset >= sb.st_size) { |
| 865 | LOGE("offset error"); |
| 866 | ::close(fd); |
| 867 | return UNKNOWN_ERROR; |
| 868 | } |
| 869 | if (offset + length > sb.st_size) { |
| 870 | length = sb.st_size - offset; |
| 871 | LOGV("calculated length = %lld", length); |
| 872 | } |
| 873 | |
| 874 | player_type playerType = getPlayerType(fd, offset, length); |
| 875 | LOGV("player type = %d", playerType); |
| 876 | |
| 877 | // create the right type of player |
| 878 | sp<MediaPlayerBase> p = createPlayer(playerType); |
| 879 | if (p == NULL) return NO_INIT; |
| 880 | |
| 881 | if (!p->hardwareOutput()) { |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 882 | mAudioOutput = new AudioOutput(mAudioSessionId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 883 | static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput); |
| 884 | } |
| 885 | |
| 886 | // now set data source |
| 887 | mStatus = p->setDataSource(fd, offset, length); |
| 888 | if (mStatus == NO_ERROR) mPlayer = p; |
| 889 | return mStatus; |
| 890 | } |
| 891 | |
Andreas Huber | 5daeb12 | 2010-08-16 08:49:37 -0700 | [diff] [blame^] | 892 | status_t MediaPlayerService::Client::setVideoISurface(const sp<ISurface>& surface) |
| 893 | { |
| 894 | LOGV("[%d] setVideoISurface(%p)", mConnId, surface.get()); |
| 895 | sp<MediaPlayerBase> p = getPlayer(); |
| 896 | if (p == 0) return UNKNOWN_ERROR; |
| 897 | return p->setVideoISurface(surface); |
| 898 | } |
| 899 | |
| 900 | status_t MediaPlayerService::Client::setVideoSurface(const sp<Surface>& surface) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 901 | { |
| 902 | LOGV("[%d] setVideoSurface(%p)", mConnId, surface.get()); |
| 903 | sp<MediaPlayerBase> p = getPlayer(); |
| 904 | if (p == 0) return UNKNOWN_ERROR; |
| 905 | return p->setVideoSurface(surface); |
| 906 | } |
| 907 | |
Nicolas Catania | 1d187f1 | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 908 | status_t MediaPlayerService::Client::invoke(const Parcel& request, |
| 909 | Parcel *reply) |
| 910 | { |
| 911 | sp<MediaPlayerBase> p = getPlayer(); |
| 912 | if (p == NULL) return UNKNOWN_ERROR; |
| 913 | return p->invoke(request, reply); |
| 914 | } |
| 915 | |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 916 | // This call doesn't need to access the native player. |
| 917 | status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter) |
| 918 | { |
| 919 | status_t status; |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 920 | media::Metadata::Filter allow, drop; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 921 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 922 | if (unmarshallFilter(filter, &allow, &status) && |
| 923 | unmarshallFilter(filter, &drop, &status)) { |
| 924 | Mutex::Autolock lock(mLock); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 925 | |
| 926 | mMetadataAllow = allow; |
| 927 | mMetadataDrop = drop; |
| 928 | } |
| 929 | return status; |
| 930 | } |
| 931 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 932 | status_t MediaPlayerService::Client::getMetadata( |
| 933 | bool update_only, bool apply_filter, Parcel *reply) |
Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 934 | { |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 935 | sp<MediaPlayerBase> player = getPlayer(); |
| 936 | if (player == 0) return UNKNOWN_ERROR; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 937 | |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 938 | status_t status; |
| 939 | // Placeholder for the return code, updated by the caller. |
| 940 | reply->writeInt32(-1); |
| 941 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 942 | media::Metadata::Filter ids; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 943 | |
| 944 | // We don't block notifications while we fetch the data. We clear |
| 945 | // mMetadataUpdated first so we don't lose notifications happening |
| 946 | // during the rest of this call. |
| 947 | { |
| 948 | Mutex::Autolock lock(mLock); |
| 949 | if (update_only) { |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 950 | ids = mMetadataUpdated; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 951 | } |
| 952 | mMetadataUpdated.clear(); |
| 953 | } |
Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 954 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 955 | media::Metadata metadata(reply); |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 956 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 957 | metadata.appendHeader(); |
| 958 | status = player->getMetadata(ids, reply); |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 959 | |
| 960 | if (status != OK) { |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 961 | metadata.resetParcel(); |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 962 | LOGE("getMetadata failed %d", status); |
| 963 | return status; |
| 964 | } |
| 965 | |
| 966 | // FIXME: Implement filtering on the result. Not critical since |
| 967 | // filtering takes place on the update notifications already. This |
| 968 | // would be when all the metadata are fetch and a filter is set. |
| 969 | |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 970 | // Everything is fine, update the metadata length. |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 971 | metadata.updateLength(); |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 972 | return OK; |
Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 973 | } |
| 974 | |
Andreas Huber | 4e92c7e | 2010-02-12 12:35:58 -0800 | [diff] [blame] | 975 | status_t MediaPlayerService::Client::suspend() { |
| 976 | sp<MediaPlayerBase> p = getPlayer(); |
| 977 | if (p == 0) return UNKNOWN_ERROR; |
| 978 | |
| 979 | return p->suspend(); |
| 980 | } |
| 981 | |
| 982 | status_t MediaPlayerService::Client::resume() { |
| 983 | sp<MediaPlayerBase> p = getPlayer(); |
| 984 | if (p == 0) return UNKNOWN_ERROR; |
| 985 | |
| 986 | return p->resume(); |
| 987 | } |
| 988 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 989 | status_t MediaPlayerService::Client::prepareAsync() |
| 990 | { |
| 991 | LOGV("[%d] prepareAsync", mConnId); |
| 992 | sp<MediaPlayerBase> p = getPlayer(); |
| 993 | if (p == 0) return UNKNOWN_ERROR; |
| 994 | status_t ret = p->prepareAsync(); |
| 995 | #if CALLBACK_ANTAGONIZER |
| 996 | LOGD("start Antagonizer"); |
| 997 | if (ret == NO_ERROR) mAntagonizer->start(); |
| 998 | #endif |
| 999 | return ret; |
| 1000 | } |
| 1001 | |
| 1002 | status_t MediaPlayerService::Client::start() |
| 1003 | { |
| 1004 | LOGV("[%d] start", mConnId); |
| 1005 | sp<MediaPlayerBase> p = getPlayer(); |
| 1006 | if (p == 0) return UNKNOWN_ERROR; |
| 1007 | p->setLooping(mLoop); |
| 1008 | return p->start(); |
| 1009 | } |
| 1010 | |
| 1011 | status_t MediaPlayerService::Client::stop() |
| 1012 | { |
| 1013 | LOGV("[%d] stop", mConnId); |
| 1014 | sp<MediaPlayerBase> p = getPlayer(); |
| 1015 | if (p == 0) return UNKNOWN_ERROR; |
| 1016 | return p->stop(); |
| 1017 | } |
| 1018 | |
| 1019 | status_t MediaPlayerService::Client::pause() |
| 1020 | { |
| 1021 | LOGV("[%d] pause", mConnId); |
| 1022 | sp<MediaPlayerBase> p = getPlayer(); |
| 1023 | if (p == 0) return UNKNOWN_ERROR; |
| 1024 | return p->pause(); |
| 1025 | } |
| 1026 | |
| 1027 | status_t MediaPlayerService::Client::isPlaying(bool* state) |
| 1028 | { |
| 1029 | *state = false; |
| 1030 | sp<MediaPlayerBase> p = getPlayer(); |
| 1031 | if (p == 0) return UNKNOWN_ERROR; |
| 1032 | *state = p->isPlaying(); |
| 1033 | LOGV("[%d] isPlaying: %d", mConnId, *state); |
| 1034 | return NO_ERROR; |
| 1035 | } |
| 1036 | |
| 1037 | status_t MediaPlayerService::Client::getCurrentPosition(int *msec) |
| 1038 | { |
| 1039 | LOGV("getCurrentPosition"); |
| 1040 | sp<MediaPlayerBase> p = getPlayer(); |
| 1041 | if (p == 0) return UNKNOWN_ERROR; |
| 1042 | status_t ret = p->getCurrentPosition(msec); |
| 1043 | if (ret == NO_ERROR) { |
| 1044 | LOGV("[%d] getCurrentPosition = %d", mConnId, *msec); |
| 1045 | } else { |
| 1046 | LOGE("getCurrentPosition returned %d", ret); |
| 1047 | } |
| 1048 | return ret; |
| 1049 | } |
| 1050 | |
| 1051 | status_t MediaPlayerService::Client::getDuration(int *msec) |
| 1052 | { |
| 1053 | LOGV("getDuration"); |
| 1054 | sp<MediaPlayerBase> p = getPlayer(); |
| 1055 | if (p == 0) return UNKNOWN_ERROR; |
| 1056 | status_t ret = p->getDuration(msec); |
| 1057 | if (ret == NO_ERROR) { |
| 1058 | LOGV("[%d] getDuration = %d", mConnId, *msec); |
| 1059 | } else { |
| 1060 | LOGE("getDuration returned %d", ret); |
| 1061 | } |
| 1062 | return ret; |
| 1063 | } |
| 1064 | |
| 1065 | status_t MediaPlayerService::Client::seekTo(int msec) |
| 1066 | { |
| 1067 | LOGV("[%d] seekTo(%d)", mConnId, msec); |
| 1068 | sp<MediaPlayerBase> p = getPlayer(); |
| 1069 | if (p == 0) return UNKNOWN_ERROR; |
| 1070 | return p->seekTo(msec); |
| 1071 | } |
| 1072 | |
| 1073 | status_t MediaPlayerService::Client::reset() |
| 1074 | { |
| 1075 | LOGV("[%d] reset", mConnId); |
| 1076 | sp<MediaPlayerBase> p = getPlayer(); |
| 1077 | if (p == 0) return UNKNOWN_ERROR; |
| 1078 | return p->reset(); |
| 1079 | } |
| 1080 | |
| 1081 | status_t MediaPlayerService::Client::setAudioStreamType(int type) |
| 1082 | { |
| 1083 | LOGV("[%d] setAudioStreamType(%d)", mConnId, type); |
| 1084 | // TODO: for hardware output, call player instead |
| 1085 | Mutex::Autolock l(mLock); |
| 1086 | if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type); |
| 1087 | return NO_ERROR; |
| 1088 | } |
| 1089 | |
| 1090 | status_t MediaPlayerService::Client::setLooping(int loop) |
| 1091 | { |
| 1092 | LOGV("[%d] setLooping(%d)", mConnId, loop); |
| 1093 | mLoop = loop; |
| 1094 | sp<MediaPlayerBase> p = getPlayer(); |
| 1095 | if (p != 0) return p->setLooping(loop); |
| 1096 | return NO_ERROR; |
| 1097 | } |
| 1098 | |
| 1099 | status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume) |
| 1100 | { |
| 1101 | LOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume); |
| 1102 | // TODO: for hardware output, call player instead |
| 1103 | Mutex::Autolock l(mLock); |
| 1104 | if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume); |
| 1105 | return NO_ERROR; |
| 1106 | } |
| 1107 | |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1108 | status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level) |
| 1109 | { |
| 1110 | LOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level); |
| 1111 | Mutex::Autolock l(mLock); |
| 1112 | if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level); |
| 1113 | return NO_ERROR; |
| 1114 | } |
| 1115 | |
| 1116 | status_t MediaPlayerService::Client::attachAuxEffect(int effectId) |
| 1117 | { |
| 1118 | LOGV("[%d] attachAuxEffect(%d)", mConnId, effectId); |
| 1119 | Mutex::Autolock l(mLock); |
| 1120 | if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId); |
| 1121 | return NO_ERROR; |
| 1122 | } |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1123 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1124 | void MediaPlayerService::Client::notify(void* cookie, int msg, int ext1, int ext2) |
| 1125 | { |
| 1126 | Client* client = static_cast<Client*>(cookie); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1127 | |
| 1128 | if (MEDIA_INFO == msg && |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1129 | MEDIA_INFO_METADATA_UPDATE == ext1) { |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1130 | const media::Metadata::Type metadata_type = ext2; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1131 | |
| 1132 | if(client->shouldDropMetadata(metadata_type)) { |
| 1133 | return; |
| 1134 | } |
| 1135 | |
| 1136 | // Update the list of metadata that have changed. getMetadata |
| 1137 | // also access mMetadataUpdated and clears it. |
| 1138 | client->addNewMetadataUpdate(metadata_type); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1139 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1140 | LOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2); |
| 1141 | client->mClient->notify(msg, ext1, ext2); |
| 1142 | } |
| 1143 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1144 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1145 | bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1146 | { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1147 | Mutex::Autolock lock(mLock); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1148 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1149 | if (findMetadata(mMetadataDrop, code)) { |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1150 | return true; |
| 1151 | } |
| 1152 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1153 | if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) { |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1154 | return false; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1155 | } else { |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1156 | return true; |
| 1157 | } |
| 1158 | } |
| 1159 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1160 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1161 | void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1162 | Mutex::Autolock lock(mLock); |
| 1163 | if (mMetadataUpdated.indexOf(metadata_type) < 0) { |
| 1164 | mMetadataUpdated.add(metadata_type); |
| 1165 | } |
| 1166 | } |
| 1167 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1168 | #if CALLBACK_ANTAGONIZER |
| 1169 | const int Antagonizer::interval = 10000; // 10 msecs |
| 1170 | |
| 1171 | Antagonizer::Antagonizer(notify_callback_f cb, void* client) : |
| 1172 | mExit(false), mActive(false), mClient(client), mCb(cb) |
| 1173 | { |
| 1174 | createThread(callbackThread, this); |
| 1175 | } |
| 1176 | |
| 1177 | void Antagonizer::kill() |
| 1178 | { |
| 1179 | Mutex::Autolock _l(mLock); |
| 1180 | mActive = false; |
| 1181 | mExit = true; |
| 1182 | mCondition.wait(mLock); |
| 1183 | } |
| 1184 | |
| 1185 | int Antagonizer::callbackThread(void* user) |
| 1186 | { |
| 1187 | LOGD("Antagonizer started"); |
| 1188 | Antagonizer* p = reinterpret_cast<Antagonizer*>(user); |
| 1189 | while (!p->mExit) { |
| 1190 | if (p->mActive) { |
| 1191 | LOGV("send event"); |
| 1192 | p->mCb(p->mClient, 0, 0, 0); |
| 1193 | } |
| 1194 | usleep(interval); |
| 1195 | } |
| 1196 | Mutex::Autolock _l(p->mLock); |
| 1197 | p->mCondition.signal(); |
| 1198 | LOGD("Antagonizer stopped"); |
| 1199 | return 0; |
| 1200 | } |
| 1201 | #endif |
| 1202 | |
| 1203 | static size_t kDefaultHeapSize = 1024 * 1024; // 1MB |
| 1204 | |
| 1205 | sp<IMemory> MediaPlayerService::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat) |
| 1206 | { |
| 1207 | LOGV("decode(%s)", url); |
| 1208 | sp<MemoryBase> mem; |
| 1209 | sp<MediaPlayerBase> player; |
| 1210 | |
| 1211 | // Protect our precious, precious DRMd ringtones by only allowing |
| 1212 | // decoding of http, but not filesystem paths or content Uris. |
| 1213 | // If the application wants to decode those, it should open a |
| 1214 | // filedescriptor for them and use that. |
| 1215 | if (url != NULL && strncmp(url, "http://", 7) != 0) { |
| 1216 | LOGD("Can't decode %s by path, use filedescriptor instead", url); |
| 1217 | return mem; |
| 1218 | } |
| 1219 | |
| 1220 | player_type playerType = getPlayerType(url); |
| 1221 | LOGV("player type = %d", playerType); |
| 1222 | |
| 1223 | // create the right type of player |
| 1224 | sp<AudioCache> cache = new AudioCache(url); |
| 1225 | player = android::createPlayer(playerType, cache.get(), cache->notify); |
| 1226 | if (player == NULL) goto Exit; |
| 1227 | if (player->hardwareOutput()) goto Exit; |
| 1228 | |
| 1229 | static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache); |
| 1230 | |
| 1231 | // set data source |
| 1232 | if (player->setDataSource(url) != NO_ERROR) goto Exit; |
| 1233 | |
| 1234 | LOGV("prepare"); |
| 1235 | player->prepareAsync(); |
| 1236 | |
| 1237 | LOGV("wait for prepare"); |
| 1238 | if (cache->wait() != NO_ERROR) goto Exit; |
| 1239 | |
| 1240 | LOGV("start"); |
| 1241 | player->start(); |
| 1242 | |
| 1243 | LOGV("wait for playback complete"); |
| 1244 | if (cache->wait() != NO_ERROR) goto Exit; |
| 1245 | |
| 1246 | mem = new MemoryBase(cache->getHeap(), 0, cache->size()); |
| 1247 | *pSampleRate = cache->sampleRate(); |
| 1248 | *pNumChannels = cache->channelCount(); |
| 1249 | *pFormat = cache->format(); |
| 1250 | LOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat); |
| 1251 | |
| 1252 | Exit: |
| 1253 | if (player != 0) player->reset(); |
| 1254 | return mem; |
| 1255 | } |
| 1256 | |
| 1257 | sp<IMemory> MediaPlayerService::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat) |
| 1258 | { |
| 1259 | LOGV("decode(%d, %lld, %lld)", fd, offset, length); |
| 1260 | sp<MemoryBase> mem; |
| 1261 | sp<MediaPlayerBase> player; |
| 1262 | |
| 1263 | player_type playerType = getPlayerType(fd, offset, length); |
| 1264 | LOGV("player type = %d", playerType); |
| 1265 | |
| 1266 | // create the right type of player |
| 1267 | sp<AudioCache> cache = new AudioCache("decode_fd"); |
| 1268 | player = android::createPlayer(playerType, cache.get(), cache->notify); |
| 1269 | if (player == NULL) goto Exit; |
| 1270 | if (player->hardwareOutput()) goto Exit; |
| 1271 | |
| 1272 | static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache); |
| 1273 | |
| 1274 | // set data source |
| 1275 | if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit; |
| 1276 | |
| 1277 | LOGV("prepare"); |
| 1278 | player->prepareAsync(); |
| 1279 | |
| 1280 | LOGV("wait for prepare"); |
| 1281 | if (cache->wait() != NO_ERROR) goto Exit; |
| 1282 | |
| 1283 | LOGV("start"); |
| 1284 | player->start(); |
| 1285 | |
| 1286 | LOGV("wait for playback complete"); |
| 1287 | if (cache->wait() != NO_ERROR) goto Exit; |
| 1288 | |
| 1289 | mem = new MemoryBase(cache->getHeap(), 0, cache->size()); |
| 1290 | *pSampleRate = cache->sampleRate(); |
| 1291 | *pNumChannels = cache->channelCount(); |
| 1292 | *pFormat = cache->format(); |
| 1293 | LOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat); |
| 1294 | |
| 1295 | Exit: |
| 1296 | if (player != 0) player->reset(); |
| 1297 | ::close(fd); |
| 1298 | return mem; |
| 1299 | } |
| 1300 | |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1301 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1302 | #undef LOG_TAG |
| 1303 | #define LOG_TAG "AudioSink" |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 1304 | MediaPlayerService::AudioOutput::AudioOutput(int sessionId) |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1305 | : mCallback(NULL), |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 1306 | mCallbackCookie(NULL), |
| 1307 | mSessionId(sessionId) { |
| 1308 | LOGV("AudioOutput(%d)", sessionId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1309 | mTrack = 0; |
| 1310 | mStreamType = AudioSystem::MUSIC; |
| 1311 | mLeftVolume = 1.0; |
| 1312 | mRightVolume = 1.0; |
| 1313 | mLatency = 0; |
| 1314 | mMsecsPerFrame = 0; |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1315 | mAuxEffectId = 0; |
| 1316 | mSendLevel = 0.0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1317 | setMinBufferCount(); |
| 1318 | } |
| 1319 | |
| 1320 | MediaPlayerService::AudioOutput::~AudioOutput() |
| 1321 | { |
| 1322 | close(); |
| 1323 | } |
| 1324 | |
| 1325 | void MediaPlayerService::AudioOutput::setMinBufferCount() |
| 1326 | { |
| 1327 | char value[PROPERTY_VALUE_MAX]; |
| 1328 | if (property_get("ro.kernel.qemu", value, 0)) { |
| 1329 | mIsOnEmulator = true; |
| 1330 | mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | bool MediaPlayerService::AudioOutput::isOnEmulator() |
| 1335 | { |
| 1336 | setMinBufferCount(); |
| 1337 | return mIsOnEmulator; |
| 1338 | } |
| 1339 | |
| 1340 | int MediaPlayerService::AudioOutput::getMinBufferCount() |
| 1341 | { |
| 1342 | setMinBufferCount(); |
| 1343 | return mMinBufferCount; |
| 1344 | } |
| 1345 | |
| 1346 | ssize_t MediaPlayerService::AudioOutput::bufferSize() const |
| 1347 | { |
| 1348 | if (mTrack == 0) return NO_INIT; |
| 1349 | return mTrack->frameCount() * frameSize(); |
| 1350 | } |
| 1351 | |
| 1352 | ssize_t MediaPlayerService::AudioOutput::frameCount() const |
| 1353 | { |
| 1354 | if (mTrack == 0) return NO_INIT; |
| 1355 | return mTrack->frameCount(); |
| 1356 | } |
| 1357 | |
| 1358 | ssize_t MediaPlayerService::AudioOutput::channelCount() const |
| 1359 | { |
| 1360 | if (mTrack == 0) return NO_INIT; |
| 1361 | return mTrack->channelCount(); |
| 1362 | } |
| 1363 | |
| 1364 | ssize_t MediaPlayerService::AudioOutput::frameSize() const |
| 1365 | { |
| 1366 | if (mTrack == 0) return NO_INIT; |
| 1367 | return mTrack->frameSize(); |
| 1368 | } |
| 1369 | |
| 1370 | uint32_t MediaPlayerService::AudioOutput::latency () const |
| 1371 | { |
| 1372 | return mLatency; |
| 1373 | } |
| 1374 | |
| 1375 | float MediaPlayerService::AudioOutput::msecsPerFrame() const |
| 1376 | { |
| 1377 | return mMsecsPerFrame; |
| 1378 | } |
| 1379 | |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 1380 | status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) |
| 1381 | { |
| 1382 | if (mTrack == 0) return NO_INIT; |
| 1383 | return mTrack->getPosition(position); |
| 1384 | } |
| 1385 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1386 | status_t MediaPlayerService::AudioOutput::open( |
| 1387 | uint32_t sampleRate, int channelCount, int format, int bufferCount, |
| 1388 | AudioCallback cb, void *cookie) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1389 | { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1390 | mCallback = cb; |
| 1391 | mCallbackCookie = cookie; |
| 1392 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1393 | // Check argument "bufferCount" against the mininum buffer count |
| 1394 | if (bufferCount < mMinBufferCount) { |
| 1395 | LOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount); |
| 1396 | bufferCount = mMinBufferCount; |
| 1397 | |
| 1398 | } |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 1399 | LOGV("open(%u, %d, %d, %d, %d)", sampleRate, channelCount, format, bufferCount,mSessionId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1400 | if (mTrack) close(); |
| 1401 | int afSampleRate; |
| 1402 | int afFrameCount; |
| 1403 | int frameCount; |
| 1404 | |
| 1405 | if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) { |
| 1406 | return NO_INIT; |
| 1407 | } |
| 1408 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) { |
| 1409 | return NO_INIT; |
| 1410 | } |
| 1411 | |
| 1412 | frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1413 | |
| 1414 | AudioTrack *t; |
| 1415 | if (mCallback != NULL) { |
| 1416 | t = new AudioTrack( |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1417 | mStreamType, |
| 1418 | sampleRate, |
| 1419 | format, |
| 1420 | (channelCount == 2) ? AudioSystem::CHANNEL_OUT_STEREO : AudioSystem::CHANNEL_OUT_MONO, |
| 1421 | frameCount, |
| 1422 | 0 /* flags */, |
| 1423 | CallbackWrapper, |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 1424 | this, |
| 1425 | 0, |
| 1426 | mSessionId); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1427 | } else { |
| 1428 | t = new AudioTrack( |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1429 | mStreamType, |
| 1430 | sampleRate, |
| 1431 | format, |
| 1432 | (channelCount == 2) ? AudioSystem::CHANNEL_OUT_STEREO : AudioSystem::CHANNEL_OUT_MONO, |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 1433 | frameCount, |
| 1434 | 0, |
| 1435 | NULL, |
| 1436 | NULL, |
| 1437 | 0, |
| 1438 | mSessionId); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1439 | } |
| 1440 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1441 | if ((t == 0) || (t->initCheck() != NO_ERROR)) { |
| 1442 | LOGE("Unable to create audio track"); |
| 1443 | delete t; |
| 1444 | return NO_INIT; |
| 1445 | } |
| 1446 | |
| 1447 | LOGV("setVolume"); |
| 1448 | t->setVolume(mLeftVolume, mRightVolume); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1449 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1450 | mMsecsPerFrame = 1.e3 / (float) sampleRate; |
Dave Sparks | 1d711f6 | 2009-12-03 10:13:32 -0800 | [diff] [blame] | 1451 | mLatency = t->latency(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1452 | mTrack = t; |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1453 | |
| 1454 | t->setAuxEffectSendLevel(mSendLevel); |
| 1455 | return t->attachAuxEffect(mAuxEffectId);; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | void MediaPlayerService::AudioOutput::start() |
| 1459 | { |
| 1460 | LOGV("start"); |
| 1461 | if (mTrack) { |
| 1462 | mTrack->setVolume(mLeftVolume, mRightVolume); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1463 | mTrack->setAuxEffectSendLevel(mSendLevel); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1464 | mTrack->start(); |
| 1465 | } |
| 1466 | } |
| 1467 | |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 1468 | |
| 1469 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1470 | ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size) |
| 1471 | { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1472 | LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback."); |
| 1473 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1474 | //LOGV("write(%p, %u)", buffer, size); |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1475 | if (mTrack) { |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1476 | ssize_t ret = mTrack->write(buffer, size); |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1477 | return ret; |
| 1478 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1479 | return NO_INIT; |
| 1480 | } |
| 1481 | |
| 1482 | void MediaPlayerService::AudioOutput::stop() |
| 1483 | { |
| 1484 | LOGV("stop"); |
| 1485 | if (mTrack) mTrack->stop(); |
| 1486 | } |
| 1487 | |
| 1488 | void MediaPlayerService::AudioOutput::flush() |
| 1489 | { |
| 1490 | LOGV("flush"); |
| 1491 | if (mTrack) mTrack->flush(); |
| 1492 | } |
| 1493 | |
| 1494 | void MediaPlayerService::AudioOutput::pause() |
| 1495 | { |
| 1496 | LOGV("pause"); |
| 1497 | if (mTrack) mTrack->pause(); |
| 1498 | } |
| 1499 | |
| 1500 | void MediaPlayerService::AudioOutput::close() |
| 1501 | { |
| 1502 | LOGV("close"); |
| 1503 | delete mTrack; |
| 1504 | mTrack = 0; |
| 1505 | } |
| 1506 | |
| 1507 | void MediaPlayerService::AudioOutput::setVolume(float left, float right) |
| 1508 | { |
| 1509 | LOGV("setVolume(%f, %f)", left, right); |
| 1510 | mLeftVolume = left; |
| 1511 | mRightVolume = right; |
| 1512 | if (mTrack) { |
| 1513 | mTrack->setVolume(left, right); |
| 1514 | } |
| 1515 | } |
| 1516 | |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1517 | status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level) |
| 1518 | { |
| 1519 | LOGV("setAuxEffectSendLevel(%f)", level); |
| 1520 | mSendLevel = level; |
| 1521 | if (mTrack) { |
| 1522 | return mTrack->setAuxEffectSendLevel(level); |
| 1523 | } |
| 1524 | return NO_ERROR; |
| 1525 | } |
| 1526 | |
| 1527 | status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId) |
| 1528 | { |
| 1529 | LOGV("attachAuxEffect(%d)", effectId); |
| 1530 | mAuxEffectId = effectId; |
| 1531 | if (mTrack) { |
| 1532 | return mTrack->attachAuxEffect(effectId); |
| 1533 | } |
| 1534 | return NO_ERROR; |
| 1535 | } |
| 1536 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1537 | // static |
| 1538 | void MediaPlayerService::AudioOutput::CallbackWrapper( |
| 1539 | int event, void *cookie, void *info) { |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 1540 | //LOGV("callbackwrapper"); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1541 | if (event != AudioTrack::EVENT_MORE_DATA) { |
| 1542 | return; |
| 1543 | } |
| 1544 | |
| 1545 | AudioOutput *me = (AudioOutput *)cookie; |
| 1546 | AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info; |
| 1547 | |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 1548 | size_t actualSize = (*me->mCallback)( |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1549 | me, buffer->raw, buffer->size, me->mCallbackCookie); |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 1550 | |
Andreas Huber | 2e8ffaf | 2010-02-18 16:45:13 -0800 | [diff] [blame] | 1551 | buffer->size = actualSize; |
| 1552 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1553 | } |
| 1554 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1555 | #undef LOG_TAG |
| 1556 | #define LOG_TAG "AudioCache" |
| 1557 | MediaPlayerService::AudioCache::AudioCache(const char* name) : |
| 1558 | mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0), |
| 1559 | mError(NO_ERROR), mCommandComplete(false) |
| 1560 | { |
| 1561 | // create ashmem heap |
| 1562 | mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name); |
| 1563 | } |
| 1564 | |
| 1565 | uint32_t MediaPlayerService::AudioCache::latency () const |
| 1566 | { |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
| 1570 | float MediaPlayerService::AudioCache::msecsPerFrame() const |
| 1571 | { |
| 1572 | return mMsecsPerFrame; |
| 1573 | } |
| 1574 | |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 1575 | status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) |
| 1576 | { |
| 1577 | if (position == 0) return BAD_VALUE; |
| 1578 | *position = mSize; |
| 1579 | return NO_ERROR; |
| 1580 | } |
| 1581 | |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 1582 | //////////////////////////////////////////////////////////////////////////////// |
| 1583 | |
| 1584 | struct CallbackThread : public Thread { |
| 1585 | CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink, |
| 1586 | MediaPlayerBase::AudioSink::AudioCallback cb, |
| 1587 | void *cookie); |
| 1588 | |
| 1589 | protected: |
| 1590 | virtual ~CallbackThread(); |
| 1591 | |
| 1592 | virtual bool threadLoop(); |
| 1593 | |
| 1594 | private: |
| 1595 | wp<MediaPlayerBase::AudioSink> mSink; |
| 1596 | MediaPlayerBase::AudioSink::AudioCallback mCallback; |
| 1597 | void *mCookie; |
| 1598 | void *mBuffer; |
| 1599 | size_t mBufferSize; |
| 1600 | |
| 1601 | CallbackThread(const CallbackThread &); |
| 1602 | CallbackThread &operator=(const CallbackThread &); |
| 1603 | }; |
| 1604 | |
| 1605 | CallbackThread::CallbackThread( |
| 1606 | const wp<MediaPlayerBase::AudioSink> &sink, |
| 1607 | MediaPlayerBase::AudioSink::AudioCallback cb, |
| 1608 | void *cookie) |
| 1609 | : mSink(sink), |
| 1610 | mCallback(cb), |
| 1611 | mCookie(cookie), |
| 1612 | mBuffer(NULL), |
| 1613 | mBufferSize(0) { |
| 1614 | } |
| 1615 | |
| 1616 | CallbackThread::~CallbackThread() { |
| 1617 | if (mBuffer) { |
| 1618 | free(mBuffer); |
| 1619 | mBuffer = NULL; |
| 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | bool CallbackThread::threadLoop() { |
| 1624 | sp<MediaPlayerBase::AudioSink> sink = mSink.promote(); |
| 1625 | if (sink == NULL) { |
| 1626 | return false; |
| 1627 | } |
| 1628 | |
| 1629 | if (mBuffer == NULL) { |
| 1630 | mBufferSize = sink->bufferSize(); |
| 1631 | mBuffer = malloc(mBufferSize); |
| 1632 | } |
| 1633 | |
| 1634 | size_t actualSize = |
| 1635 | (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie); |
| 1636 | |
| 1637 | if (actualSize > 0) { |
| 1638 | sink->write(mBuffer, actualSize); |
| 1639 | } |
| 1640 | |
| 1641 | return true; |
| 1642 | } |
| 1643 | |
| 1644 | //////////////////////////////////////////////////////////////////////////////// |
| 1645 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1646 | status_t MediaPlayerService::AudioCache::open( |
| 1647 | uint32_t sampleRate, int channelCount, int format, int bufferCount, |
| 1648 | AudioCallback cb, void *cookie) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1649 | { |
Dave Sparks | 8eb8011 | 2009-12-09 20:20:26 -0800 | [diff] [blame] | 1650 | LOGV("open(%u, %d, %d, %d)", sampleRate, channelCount, format, bufferCount); |
Dave Sparks | 8eb8011 | 2009-12-09 20:20:26 -0800 | [diff] [blame] | 1651 | if (mHeap->getHeapID() < 0) { |
| 1652 | return NO_INIT; |
| 1653 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1654 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1655 | mSampleRate = sampleRate; |
| 1656 | mChannelCount = (uint16_t)channelCount; |
| 1657 | mFormat = (uint16_t)format; |
| 1658 | mMsecsPerFrame = 1.e3 / (float) sampleRate; |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 1659 | |
| 1660 | if (cb != NULL) { |
| 1661 | mCallbackThread = new CallbackThread(this, cb, cookie); |
| 1662 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1663 | return NO_ERROR; |
| 1664 | } |
| 1665 | |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 1666 | void MediaPlayerService::AudioCache::start() { |
| 1667 | if (mCallbackThread != NULL) { |
| 1668 | mCallbackThread->run("AudioCache callback"); |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | void MediaPlayerService::AudioCache::stop() { |
| 1673 | if (mCallbackThread != NULL) { |
| 1674 | mCallbackThread->requestExitAndWait(); |
| 1675 | } |
| 1676 | } |
| 1677 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1678 | ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size) |
| 1679 | { |
| 1680 | LOGV("write(%p, %u)", buffer, size); |
| 1681 | if ((buffer == 0) || (size == 0)) return size; |
| 1682 | |
| 1683 | uint8_t* p = static_cast<uint8_t*>(mHeap->getBase()); |
| 1684 | if (p == NULL) return NO_INIT; |
| 1685 | p += mSize; |
| 1686 | LOGV("memcpy(%p, %p, %u)", p, buffer, size); |
| 1687 | if (mSize + size > mHeap->getSize()) { |
| 1688 | LOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize()); |
| 1689 | size = mHeap->getSize() - mSize; |
| 1690 | } |
| 1691 | memcpy(p, buffer, size); |
| 1692 | mSize += size; |
| 1693 | return size; |
| 1694 | } |
| 1695 | |
| 1696 | // call with lock held |
| 1697 | status_t MediaPlayerService::AudioCache::wait() |
| 1698 | { |
| 1699 | Mutex::Autolock lock(mLock); |
Dave Sparks | 4bbc0ba | 2010-03-01 19:29:58 -0800 | [diff] [blame] | 1700 | while (!mCommandComplete) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1701 | mSignal.wait(mLock); |
| 1702 | } |
| 1703 | mCommandComplete = false; |
| 1704 | |
| 1705 | if (mError == NO_ERROR) { |
| 1706 | LOGV("wait - success"); |
| 1707 | } else { |
| 1708 | LOGV("wait - error"); |
| 1709 | } |
| 1710 | return mError; |
| 1711 | } |
| 1712 | |
| 1713 | void MediaPlayerService::AudioCache::notify(void* cookie, int msg, int ext1, int ext2) |
| 1714 | { |
| 1715 | LOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2); |
| 1716 | AudioCache* p = static_cast<AudioCache*>(cookie); |
| 1717 | |
| 1718 | // ignore buffering messages |
Dave Sparks | 8eb8011 | 2009-12-09 20:20:26 -0800 | [diff] [blame] | 1719 | switch (msg) |
| 1720 | { |
| 1721 | case MEDIA_ERROR: |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1722 | LOGE("Error %d, %d occurred", ext1, ext2); |
| 1723 | p->mError = ext1; |
Dave Sparks | 8eb8011 | 2009-12-09 20:20:26 -0800 | [diff] [blame] | 1724 | break; |
| 1725 | case MEDIA_PREPARED: |
| 1726 | LOGV("prepared"); |
| 1727 | break; |
| 1728 | case MEDIA_PLAYBACK_COMPLETE: |
| 1729 | LOGV("playback complete"); |
| 1730 | break; |
| 1731 | default: |
| 1732 | LOGV("ignored"); |
| 1733 | return; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | // wake up thread |
Dave Sparks | fe4c6f0 | 2010-03-02 12:56:37 -0800 | [diff] [blame] | 1737 | Mutex::Autolock lock(p->mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1738 | p->mCommandComplete = true; |
| 1739 | p->mSignal.signal(); |
| 1740 | } |
| 1741 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1742 | } // namespace android |