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