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