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