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