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