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