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