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