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