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