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> |
Chong Zhang | 181fd9b | 2017-02-16 15:53:03 -0800 | [diff] [blame] | 63 | #include <media/stagefright/SurfaceUtils.h> |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 64 | #include <mediautils/BatteryNotifier.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | |
Andy Hung | 5354129 | 2016-04-13 16:48:51 -0700 | [diff] [blame] | 66 | #include <memunreachable/memunreachable.h> |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 67 | #include <system/audio.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 68 | |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 69 | #include <private/android_filesystem_config.h> |
| 70 | |
James Dong | 559bf28 | 2012-03-28 10:29:14 -0700 | [diff] [blame] | 71 | #include "ActivityManager.h" |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | #include "MediaRecorderClient.h" |
| 73 | #include "MediaPlayerService.h" |
| 74 | #include "MetadataRetrieverClient.h" |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 75 | #include "MediaPlayerFactory.h" |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | |
Nicolas Catania | 14d2747 | 2009-07-13 14:37:49 -0700 | [diff] [blame] | 77 | #include "TestPlayerStub.h" |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 78 | #include "nuplayer/NuPlayerDriver.h" |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 79 | |
Andreas Huber | b7319a7 | 2013-05-29 14:20:52 -0700 | [diff] [blame] | 80 | #include "HTTPBase.h" |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 81 | |
Wei Jia | 502c2f4 | 2017-07-13 17:47:56 -0700 | [diff] [blame] | 82 | static const int kDumpLockRetries = 50; |
| 83 | static const int kDumpLockSleepUs = 20000; |
| 84 | |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 85 | namespace { |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 86 | using android::media::Metadata; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 87 | using android::status_t; |
| 88 | using android::OK; |
| 89 | using android::BAD_VALUE; |
| 90 | using android::NOT_ENOUGH_DATA; |
| 91 | using android::Parcel; |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 92 | using android::media::VolumeShaper; |
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 | |
Andy Hung | ff874dc | 2016-04-11 16:49:09 -0700 | [diff] [blame] | 97 | const float kMaxRequiredSpeed = 8.0f; // for PCM tracks allow up to 8x speedup. |
| 98 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 99 | // FIXME: Move all the metadata related function in the Metadata.cpp |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 100 | |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 101 | |
| 102 | // Unmarshall a filter from a Parcel. |
| 103 | // Filter format in a parcel: |
| 104 | // |
| 105 | // 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 |
| 106 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 107 | // | number of entries (n) | |
| 108 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 109 | // | metadata type 1 | |
| 110 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 111 | // | metadata type 2 | |
| 112 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 113 | // .... |
| 114 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 115 | // | metadata type n | |
| 116 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 117 | // |
| 118 | // @param p Parcel that should start with a filter. |
| 119 | // @param[out] filter On exit contains the list of metadata type to be |
| 120 | // filtered. |
| 121 | // @param[out] status On exit contains the status code to be returned. |
| 122 | // @return true if the parcel starts with a valid filter. |
| 123 | bool unmarshallFilter(const Parcel& p, |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 124 | Metadata::Filter *filter, |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 125 | status_t *status) |
| 126 | { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 127 | int32_t val; |
| 128 | if (p.readInt32(&val) != OK) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 129 | { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 130 | ALOGE("Failed to read filter's length"); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 131 | *status = NOT_ENOUGH_DATA; |
| 132 | return false; |
| 133 | } |
| 134 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 135 | if( val > kMaxFilterSize || val < 0) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 136 | { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 137 | ALOGE("Invalid filter len %d", val); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 138 | *status = BAD_VALUE; |
| 139 | return false; |
| 140 | } |
| 141 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 142 | const size_t num = val; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 143 | |
| 144 | filter->clear(); |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 145 | filter->setCapacity(num); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 146 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 147 | size_t size = num * sizeof(Metadata::Type); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 148 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 149 | |
| 150 | if (p.dataAvail() < size) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 151 | { |
Andy Hung | 833b475 | 2016-04-04 17:15:48 -0700 | [diff] [blame] | 152 | ALOGE("Filter too short expected %zu but got %zu", size, p.dataAvail()); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 153 | *status = NOT_ENOUGH_DATA; |
| 154 | return false; |
| 155 | } |
| 156 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 157 | const Metadata::Type *data = |
| 158 | static_cast<const Metadata::Type*>(p.readInplace(size)); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 159 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 160 | if (NULL == data) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 161 | { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 162 | ALOGE("Filter had no data"); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 163 | *status = BAD_VALUE; |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | // TODO: The stl impl of vector would be more efficient here |
| 168 | // because it degenerates into a memcpy on pod types. Try to |
| 169 | // replace later or use stl::set. |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 170 | for (size_t i = 0; i < num; ++i) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 171 | { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 172 | filter->add(*data); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 173 | ++data; |
| 174 | } |
| 175 | *status = OK; |
| 176 | return true; |
| 177 | } |
| 178 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 179 | // @param filter Of metadata type. |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 180 | // @param val To be searched. |
| 181 | // @return true if a match was found. |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 182 | bool findMetadata(const Metadata::Filter& filter, const int32_t val) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 183 | { |
| 184 | // Deal with empty and ANY right away |
| 185 | if (filter.isEmpty()) return false; |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 186 | if (filter[0] == Metadata::kAny) return true; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 187 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 188 | return filter.indexOf(val) >= 0; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | } // anonymous namespace |
| 192 | |
| 193 | |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 194 | namespace { |
| 195 | using android::Parcel; |
| 196 | using android::String16; |
| 197 | |
| 198 | // marshalling tag indicating flattened utf16 tags |
| 199 | // keep in sync with frameworks/base/media/java/android/media/AudioAttributes.java |
| 200 | const int32_t kAudioAttributesMarshallTagFlattenTags = 1; |
| 201 | |
| 202 | // Audio attributes format in a parcel: |
| 203 | // |
| 204 | // 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 |
| 205 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 206 | // | usage | |
| 207 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 208 | // | content_type | |
| 209 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Hyejin Kim | 4f418f9 | 2014-09-05 15:50:03 +0900 | [diff] [blame] | 210 | // | source | |
| 211 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 212 | // | flags | |
| 213 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 214 | // | kAudioAttributesMarshallTagFlattenTags | // ignore tags if not found |
| 215 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 216 | // | flattened tags in UTF16 | |
| 217 | // | ... | |
| 218 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 219 | // |
| 220 | // @param p Parcel that contains audio attributes. |
| 221 | // @param[out] attributes On exit points to an initialized audio_attributes_t structure |
| 222 | // @param[out] status On exit contains the status code to be returned. |
| 223 | void unmarshallAudioAttributes(const Parcel& parcel, audio_attributes_t *attributes) |
| 224 | { |
| 225 | attributes->usage = (audio_usage_t) parcel.readInt32(); |
| 226 | attributes->content_type = (audio_content_type_t) parcel.readInt32(); |
Hyejin Kim | 4f418f9 | 2014-09-05 15:50:03 +0900 | [diff] [blame] | 227 | attributes->source = (audio_source_t) parcel.readInt32(); |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 228 | attributes->flags = (audio_flags_mask_t) parcel.readInt32(); |
| 229 | const bool hasFlattenedTag = (parcel.readInt32() == kAudioAttributesMarshallTagFlattenTags); |
| 230 | if (hasFlattenedTag) { |
| 231 | // the tags are UTF16, convert to UTF8 |
| 232 | String16 tags = parcel.readString16(); |
| 233 | ssize_t realTagSize = utf16_to_utf8_length(tags.string(), tags.size()); |
| 234 | if (realTagSize <= 0) { |
| 235 | strcpy(attributes->tags, ""); |
| 236 | } else { |
| 237 | // copy the flattened string into the attributes as the destination for the conversion: |
| 238 | // copying array size -1, array for tags was calloc'd, no need to NULL-terminate it |
| 239 | size_t tagSize = realTagSize > AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 ? |
| 240 | AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 : realTagSize; |
Sergio Giro | 1d3f427 | 2016-06-28 18:24:52 +0100 | [diff] [blame] | 241 | utf16_to_utf8(tags.string(), tagSize, attributes->tags, |
| 242 | sizeof(attributes->tags) / sizeof(attributes->tags[0])); |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 243 | } |
| 244 | } else { |
| 245 | ALOGE("unmarshallAudioAttributes() received unflattened tags, ignoring tag values"); |
| 246 | strcpy(attributes->tags, ""); |
| 247 | } |
| 248 | } |
| 249 | } // anonymous namespace |
| 250 | |
| 251 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | namespace android { |
| 253 | |
Marco Nelissen | f09611f | 2015-02-13 14:12:42 -0800 | [diff] [blame] | 254 | extern ALooperRoster gLooperRoster; |
| 255 | |
| 256 | |
Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 257 | static bool checkPermission(const char* permissionString) { |
Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 258 | if (getpid() == IPCThreadState::self()->getCallingPid()) return true; |
| 259 | bool ok = checkCallingPermission(String16(permissionString)); |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 260 | if (!ok) ALOGE("Request requires %s", permissionString); |
Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 261 | return ok; |
| 262 | } |
| 263 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | // TODO: Find real cause of Audio/Video delay in PV framework and remove this workaround |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | /* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4; |
| 266 | /* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false; |
| 267 | |
| 268 | void MediaPlayerService::instantiate() { |
| 269 | defaultServiceManager()->addService( |
| 270 | String16("media.player"), new MediaPlayerService()); |
| 271 | } |
| 272 | |
| 273 | MediaPlayerService::MediaPlayerService() |
| 274 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 275 | ALOGV("MediaPlayerService created"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | mNextConnId = 1; |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 277 | |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 278 | MediaPlayerFactory::registerBuiltinFactories(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | MediaPlayerService::~MediaPlayerService() |
| 282 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 283 | ALOGV("MediaPlayerService destroyed"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 286 | sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(const String16 &opPackageName) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | { |
Glenn Kasten | f37971f | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 288 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 289 | sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid, opPackageName); |
Gloria Wang | dac6a31 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 290 | wp<MediaRecorderClient> w = recorder; |
| 291 | Mutex::Autolock lock(mLock); |
| 292 | mMediaRecorderClients.add(w); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 293 | 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] | 294 | return recorder; |
| 295 | } |
| 296 | |
Chih-Hung Hsieh | 36d0ca1 | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 297 | void MediaPlayerService::removeMediaRecorderClient(const wp<MediaRecorderClient>& client) |
Gloria Wang | dac6a31 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 298 | { |
| 299 | Mutex::Autolock lock(mLock); |
| 300 | mMediaRecorderClients.remove(client); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 301 | ALOGV("Delete media recorder client"); |
Gloria Wang | dac6a31 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Glenn Kasten | f37971f | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 304 | sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever() |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | { |
Glenn Kasten | f37971f | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 306 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 308 | ALOGV("Create new media retriever from pid %d", pid); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | return retriever; |
| 310 | } |
| 311 | |
Glenn Kasten | f37971f | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 312 | sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 313 | audio_session_t audioSessionId) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | { |
Glenn Kasten | f37971f | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 315 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | int32_t connId = android_atomic_inc(&mNextConnId); |
Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 317 | |
| 318 | sp<Client> c = new Client( |
| 319 | this, pid, connId, client, audioSessionId, |
| 320 | IPCThreadState::self()->getCallingUid()); |
| 321 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 322 | ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid, |
Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 323 | IPCThreadState::self()->getCallingUid()); |
| 324 | |
| 325 | wp<Client> w = c; |
| 326 | { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | Mutex::Autolock lock(mLock); |
| 328 | mClients.add(w); |
| 329 | } |
Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 330 | return c; |
| 331 | } |
| 332 | |
Lajos Molnar | 1381d4b | 2014-08-07 15:18:35 -0700 | [diff] [blame] | 333 | sp<IMediaCodecList> MediaPlayerService::getCodecList() const { |
| 334 | return MediaCodecList::getLocalInstance(); |
| 335 | } |
| 336 | |
Jeff Brown | 2013a54 | 2012-09-04 21:38:42 -0700 | [diff] [blame] | 337 | sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay( |
Chong Zhang | d0a98fa | 2017-09-14 17:43:10 -0700 | [diff] [blame] | 338 | const String16 &/*opPackageName*/, |
| 339 | const sp<IRemoteDisplayClient>& /*client*/, |
| 340 | const String8& /*iface*/) { |
| 341 | ALOGE("listenForRemoteDisplay is no longer supported!"); |
Jeff Brown | aba33d5 | 2012-09-07 17:38:58 -0700 | [diff] [blame] | 342 | |
Chong Zhang | d0a98fa | 2017-09-14 17:43:10 -0700 | [diff] [blame] | 343 | return NULL; |
Jeff Brown | 2013a54 | 2012-09-04 21:38:42 -0700 | [diff] [blame] | 344 | } |
| 345 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const |
| 347 | { |
| 348 | const size_t SIZE = 256; |
| 349 | char buffer[SIZE]; |
| 350 | String8 result; |
| 351 | |
| 352 | result.append(" AudioOutput\n"); |
| 353 | snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", |
| 354 | mStreamType, mLeftVolume, mRightVolume); |
| 355 | result.append(buffer); |
| 356 | snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n", |
Eric Laurent | db354e5 | 2012-03-05 17:27:11 -0800 | [diff] [blame] | 357 | mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | result.append(buffer); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 359 | snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n", |
| 360 | mAuxEffectId, mSendLevel); |
| 361 | result.append(buffer); |
| 362 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | ::write(fd, result.string(), result.size()); |
| 364 | if (mTrack != 0) { |
| 365 | mTrack->dump(fd, args); |
| 366 | } |
| 367 | return NO_ERROR; |
| 368 | } |
| 369 | |
Lajos Molnar | 6d339f1 | 2015-04-17 16:15:53 -0700 | [diff] [blame] | 370 | 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] | 371 | { |
| 372 | const size_t SIZE = 256; |
| 373 | char buffer[SIZE]; |
| 374 | String8 result; |
| 375 | result.append(" Client\n"); |
| 376 | snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n", |
| 377 | mPid, mConnId, mStatus, mLoop?"true": "false"); |
| 378 | result.append(buffer); |
Wei Jia | 502c2f4 | 2017-07-13 17:47:56 -0700 | [diff] [blame] | 379 | |
| 380 | sp<MediaPlayerBase> p; |
| 381 | sp<AudioOutput> audioOutput; |
| 382 | bool locked = false; |
| 383 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 384 | if (mLock.tryLock() == NO_ERROR) { |
| 385 | locked = true; |
| 386 | break; |
| 387 | } |
| 388 | usleep(kDumpLockSleepUs); |
Andreas Huber | a0b1d4b | 2011-06-07 15:52:25 -0700 | [diff] [blame] | 389 | } |
Wei Jia | 502c2f4 | 2017-07-13 17:47:56 -0700 | [diff] [blame] | 390 | |
| 391 | if (locked) { |
| 392 | p = mPlayer; |
| 393 | audioOutput = mAudioOutput; |
| 394 | mLock.unlock(); |
| 395 | } else { |
| 396 | result.append(" lock is taken, no dump from player and audio output\n"); |
| 397 | } |
| 398 | write(fd, result.string(), result.size()); |
| 399 | |
| 400 | if (p != NULL) { |
| 401 | p->dump(fd, args); |
| 402 | } |
| 403 | if (audioOutput != 0) { |
| 404 | audioOutput->dump(fd, args); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | } |
| 406 | write(fd, "\n", 1); |
| 407 | return NO_ERROR; |
| 408 | } |
| 409 | |
Marco Nelissen | f09611f | 2015-02-13 14:12:42 -0800 | [diff] [blame] | 410 | /** |
| 411 | * The only arguments this understands right now are -c, -von and -voff, |
| 412 | * which are parsed by ALooperRoster::dump() |
| 413 | */ |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | status_t MediaPlayerService::dump(int fd, const Vector<String16>& args) |
| 415 | { |
| 416 | const size_t SIZE = 256; |
| 417 | char buffer[SIZE]; |
| 418 | String8 result; |
Ravi Kumar Alamanda | 330c8e3 | 2014-12-22 10:05:29 +0530 | [diff] [blame] | 419 | SortedVector< sp<Client> > clients; //to serialise the mutex unlock & client destruction. |
| 420 | SortedVector< sp<MediaRecorderClient> > mediaRecorderClients; |
| 421 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 423 | snprintf(buffer, SIZE, "Permission Denial: " |
| 424 | "can't dump MediaPlayerService from pid=%d, uid=%d\n", |
| 425 | IPCThreadState::self()->getCallingPid(), |
| 426 | IPCThreadState::self()->getCallingUid()); |
| 427 | result.append(buffer); |
| 428 | } else { |
| 429 | Mutex::Autolock lock(mLock); |
| 430 | for (int i = 0, n = mClients.size(); i < n; ++i) { |
| 431 | sp<Client> c = mClients[i].promote(); |
| 432 | if (c != 0) c->dump(fd, args); |
Ravi Kumar Alamanda | 330c8e3 | 2014-12-22 10:05:29 +0530 | [diff] [blame] | 433 | clients.add(c); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | } |
James Dong | b914122 | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 435 | if (mMediaRecorderClients.size() == 0) { |
| 436 | result.append(" No media recorder client\n\n"); |
| 437 | } else { |
| 438 | for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) { |
| 439 | sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote(); |
James Dong | e579e28 | 2011-10-18 22:29:20 -0700 | [diff] [blame] | 440 | if (c != 0) { |
| 441 | snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid); |
| 442 | result.append(buffer); |
| 443 | write(fd, result.string(), result.size()); |
| 444 | result = "\n"; |
| 445 | c->dump(fd, args); |
Ravi Kumar Alamanda | 330c8e3 | 2014-12-22 10:05:29 +0530 | [diff] [blame] | 446 | mediaRecorderClients.add(c); |
James Dong | e579e28 | 2011-10-18 22:29:20 -0700 | [diff] [blame] | 447 | } |
James Dong | b914122 | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 448 | } |
Gloria Wang | dac6a31 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 449 | } |
| 450 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | result.append(" Files opened and/or mapped:\n"); |
Marco Nelissen | f09611f | 2015-02-13 14:12:42 -0800 | [diff] [blame] | 452 | snprintf(buffer, SIZE, "/proc/%d/maps", getpid()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | FILE *f = fopen(buffer, "r"); |
| 454 | if (f) { |
| 455 | while (!feof(f)) { |
| 456 | fgets(buffer, SIZE, f); |
Marco Nelissen | 73ac1ee | 2012-05-07 15:36:32 -0700 | [diff] [blame] | 457 | if (strstr(buffer, " /storage/") || |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | strstr(buffer, " /system/sounds/") || |
Dave Sparks | 02fa834 | 2010-09-27 16:55:18 -0700 | [diff] [blame] | 459 | strstr(buffer, " /data/") || |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | strstr(buffer, " /system/media/")) { |
| 461 | result.append(" "); |
| 462 | result.append(buffer); |
| 463 | } |
| 464 | } |
| 465 | fclose(f); |
| 466 | } else { |
| 467 | result.append("couldn't open "); |
| 468 | result.append(buffer); |
| 469 | result.append("\n"); |
| 470 | } |
| 471 | |
Marco Nelissen | f09611f | 2015-02-13 14:12:42 -0800 | [diff] [blame] | 472 | snprintf(buffer, SIZE, "/proc/%d/fd", getpid()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | DIR *d = opendir(buffer); |
| 474 | if (d) { |
| 475 | struct dirent *ent; |
| 476 | while((ent = readdir(d)) != NULL) { |
| 477 | if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) { |
Marco Nelissen | f09611f | 2015-02-13 14:12:42 -0800 | [diff] [blame] | 478 | 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] | 479 | struct stat s; |
| 480 | if (lstat(buffer, &s) == 0) { |
| 481 | if ((s.st_mode & S_IFMT) == S_IFLNK) { |
| 482 | char linkto[256]; |
| 483 | int len = readlink(buffer, linkto, sizeof(linkto)); |
| 484 | if(len > 0) { |
| 485 | if(len > 255) { |
| 486 | linkto[252] = '.'; |
| 487 | linkto[253] = '.'; |
| 488 | linkto[254] = '.'; |
| 489 | linkto[255] = 0; |
| 490 | } else { |
| 491 | linkto[len] = 0; |
| 492 | } |
Marco Nelissen | 73ac1ee | 2012-05-07 15:36:32 -0700 | [diff] [blame] | 493 | if (strstr(linkto, "/storage/") == linkto || |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | strstr(linkto, "/system/sounds/") == linkto || |
Dave Sparks | 02fa834 | 2010-09-27 16:55:18 -0700 | [diff] [blame] | 495 | strstr(linkto, "/data/") == linkto || |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | strstr(linkto, "/system/media/") == linkto) { |
| 497 | result.append(" "); |
| 498 | result.append(buffer); |
| 499 | result.append(" -> "); |
| 500 | result.append(linkto); |
| 501 | result.append("\n"); |
| 502 | } |
| 503 | } |
| 504 | } else { |
| 505 | result.append(" unexpected type for "); |
| 506 | result.append(buffer); |
| 507 | result.append("\n"); |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | closedir(d); |
| 513 | } else { |
| 514 | result.append("couldn't open "); |
| 515 | result.append(buffer); |
| 516 | result.append("\n"); |
| 517 | } |
| 518 | |
Marco Nelissen | f09611f | 2015-02-13 14:12:42 -0800 | [diff] [blame] | 519 | gLooperRoster.dump(fd, args); |
| 520 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 521 | bool dumpMem = false; |
Andy Hung | 5354129 | 2016-04-13 16:48:51 -0700 | [diff] [blame] | 522 | bool unreachableMemory = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | for (size_t i = 0; i < args.size(); i++) { |
| 524 | if (args[i] == String16("-m")) { |
| 525 | dumpMem = true; |
Andy Hung | 5354129 | 2016-04-13 16:48:51 -0700 | [diff] [blame] | 526 | } else if (args[i] == String16("--unreachable")) { |
| 527 | unreachableMemory = true; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | if (dumpMem) { |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 531 | result.append("\nDumping memory:\n"); |
| 532 | std::string s = dumpMemoryAddresses(100 /* limit */); |
| 533 | result.append(s.c_str(), s.size()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | } |
Andy Hung | 5354129 | 2016-04-13 16:48:51 -0700 | [diff] [blame] | 535 | if (unreachableMemory) { |
| 536 | result.append("\nDumping unreachable memory:\n"); |
| 537 | // TODO - should limit be an argument parameter? |
| 538 | std::string s = GetUnreachableMemoryString(true /* contents */, 10000 /* limit */); |
| 539 | result.append(s.c_str(), s.size()); |
| 540 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | } |
| 542 | write(fd, result.string(), result.size()); |
| 543 | return NO_ERROR; |
| 544 | } |
| 545 | |
Chih-Hung Hsieh | 36d0ca1 | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 546 | void MediaPlayerService::removeClient(const wp<Client>& client) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | { |
| 548 | Mutex::Autolock lock(mLock); |
| 549 | mClients.remove(client); |
| 550 | } |
| 551 | |
Robert Shih | ee0a0e3 | 2016-08-16 16:50:54 -0700 | [diff] [blame] | 552 | bool MediaPlayerService::hasClient(wp<Client> client) |
| 553 | { |
| 554 | Mutex::Autolock lock(mLock); |
| 555 | return mClients.indexOf(client) != NAME_NOT_FOUND; |
| 556 | } |
| 557 | |
Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 558 | MediaPlayerService::Client::Client( |
| 559 | const sp<MediaPlayerService>& service, pid_t pid, |
| 560 | int32_t connId, const sp<IMediaPlayerClient>& client, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 561 | audio_session_t audioSessionId, uid_t uid) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 563 | ALOGV("Client(%d) constructor", connId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 564 | mPid = pid; |
| 565 | mConnId = connId; |
| 566 | mService = service; |
| 567 | mClient = client; |
| 568 | mLoop = false; |
| 569 | mStatus = NO_INIT; |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 570 | mAudioSessionId = audioSessionId; |
Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 571 | mUid = uid; |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 572 | mRetransmitEndpointValid = false; |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 573 | mAudioAttributes = NULL; |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 574 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | #if CALLBACK_ANTAGONIZER |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 576 | ALOGD("create Antagonizer"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | mAntagonizer = new Antagonizer(notify, this); |
| 578 | #endif |
| 579 | } |
| 580 | |
| 581 | MediaPlayerService::Client::~Client() |
| 582 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 583 | ALOGV("Client(%d) destructor pid = %d", mConnId, mPid); |
Wei Jia | 502c2f4 | 2017-07-13 17:47:56 -0700 | [diff] [blame] | 584 | { |
| 585 | Mutex::Autolock l(mLock); |
| 586 | mAudioOutput.clear(); |
| 587 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 588 | wp<Client> client(this); |
| 589 | disconnect(); |
| 590 | mService->removeClient(client); |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 591 | if (mAudioAttributes != NULL) { |
| 592 | free(mAudioAttributes); |
| 593 | } |
Pawin Vongmasa | 9c47c97 | 2017-02-08 04:09:38 -0800 | [diff] [blame] | 594 | clearDeathNotifiers(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | void MediaPlayerService::Client::disconnect() |
| 598 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 599 | ALOGV("disconnect(%d) from pid %d", mConnId, mPid); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 600 | // grab local reference and clear main reference to prevent future |
| 601 | // access to object |
| 602 | sp<MediaPlayerBase> p; |
| 603 | { |
| 604 | Mutex::Autolock l(mLock); |
| 605 | p = mPlayer; |
beanz | dcfefde | 2012-11-05 09:51:43 +0800 | [diff] [blame] | 606 | mClient.clear(); |
Wei Jia | 502c2f4 | 2017-07-13 17:47:56 -0700 | [diff] [blame] | 607 | mPlayer.clear(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 609 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 610 | // clear the notification to prevent callbacks to dead client |
| 611 | // and reset the player. We assume the player will serialize |
| 612 | // access to itself if necessary. |
| 613 | if (p != 0) { |
| 614 | p->setNotifyCallback(0, 0); |
| 615 | #if CALLBACK_ANTAGONIZER |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 616 | ALOGD("kill Antagonizer"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | mAntagonizer->kill(); |
| 618 | #endif |
| 619 | p->reset(); |
| 620 | } |
| 621 | |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 622 | disconnectNativeWindow(); |
| 623 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | IPCThreadState::self()->flushCommands(); |
| 625 | } |
| 626 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 627 | sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType) |
| 628 | { |
| 629 | // determine if we have the right player type |
Wei Jia | 502c2f4 | 2017-07-13 17:47:56 -0700 | [diff] [blame] | 630 | sp<MediaPlayerBase> p = getPlayer(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | if ((p != NULL) && (p->playerType() != playerType)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 632 | ALOGV("delete player"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | p.clear(); |
| 634 | } |
| 635 | if (p == NULL) { |
Ronghua Wu | 68845c1 | 2015-07-21 09:50:48 -0700 | [diff] [blame] | 636 | p = MediaPlayerFactory::createPlayer(playerType, this, notify, mPid); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | } |
Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 638 | |
Jason Simmons | db29e52 | 2011-08-12 13:46:55 -0700 | [diff] [blame] | 639 | if (p != NULL) { |
Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 640 | p->setUID(mUid); |
Jason Simmons | db29e52 | 2011-08-12 13:46:55 -0700 | [diff] [blame] | 641 | } |
Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 642 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 643 | return p; |
| 644 | } |
| 645 | |
Marco Nelissen | 6dc3a3e | 2016-04-29 15:42:06 -0700 | [diff] [blame] | 646 | MediaPlayerService::Client::ServiceDeathNotifier::ServiceDeathNotifier( |
| 647 | const sp<IBinder>& service, |
| 648 | const sp<MediaPlayerBase>& listener, |
| 649 | int which) { |
| 650 | mService = service; |
Pawin Vongmasa | 9c47c97 | 2017-02-08 04:09:38 -0800 | [diff] [blame] | 651 | mOmx = nullptr; |
| 652 | mListener = listener; |
| 653 | mWhich = which; |
| 654 | } |
| 655 | |
| 656 | MediaPlayerService::Client::ServiceDeathNotifier::ServiceDeathNotifier( |
| 657 | const sp<IOmx>& omx, |
| 658 | const sp<MediaPlayerBase>& listener, |
| 659 | int which) { |
| 660 | mService = nullptr; |
| 661 | mOmx = omx; |
Marco Nelissen | 6dc3a3e | 2016-04-29 15:42:06 -0700 | [diff] [blame] | 662 | mListener = listener; |
| 663 | mWhich = which; |
| 664 | } |
| 665 | |
| 666 | MediaPlayerService::Client::ServiceDeathNotifier::~ServiceDeathNotifier() { |
Marco Nelissen | 6dc3a3e | 2016-04-29 15:42:06 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | void MediaPlayerService::Client::ServiceDeathNotifier::binderDied(const wp<IBinder>& /*who*/) { |
| 670 | sp<MediaPlayerBase> listener = mListener.promote(); |
| 671 | if (listener != NULL) { |
| 672 | listener->sendEvent(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, mWhich); |
| 673 | } else { |
| 674 | ALOGW("listener for process %d death is gone", mWhich); |
| 675 | } |
| 676 | } |
| 677 | |
Pawin Vongmasa | 9c47c97 | 2017-02-08 04:09:38 -0800 | [diff] [blame] | 678 | void MediaPlayerService::Client::ServiceDeathNotifier::serviceDied( |
| 679 | uint64_t /* cookie */, |
| 680 | const wp<::android::hidl::base::V1_0::IBase>& /* who */) { |
| 681 | sp<MediaPlayerBase> listener = mListener.promote(); |
| 682 | if (listener != NULL) { |
| 683 | listener->sendEvent(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, mWhich); |
| 684 | } else { |
| 685 | ALOGW("listener for process %d death is gone", mWhich); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | void MediaPlayerService::Client::ServiceDeathNotifier::unlinkToDeath() { |
| 690 | if (mService != nullptr) { |
| 691 | mService->unlinkToDeath(this); |
| 692 | mService = nullptr; |
| 693 | } else if (mOmx != nullptr) { |
| 694 | mOmx->unlinkToDeath(this); |
| 695 | mOmx = nullptr; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | void MediaPlayerService::Client::clearDeathNotifiers() { |
| 700 | if (mExtractorDeathListener != nullptr) { |
| 701 | mExtractorDeathListener->unlinkToDeath(); |
| 702 | mExtractorDeathListener = nullptr; |
| 703 | } |
| 704 | if (mCodecDeathListener != nullptr) { |
| 705 | mCodecDeathListener->unlinkToDeath(); |
| 706 | mCodecDeathListener = nullptr; |
| 707 | } |
| 708 | } |
| 709 | |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 710 | sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre( |
| 711 | player_type playerType) |
| 712 | { |
| 713 | ALOGV("player type = %d", playerType); |
Pawin Vongmasa | 9c47c97 | 2017-02-08 04:09:38 -0800 | [diff] [blame] | 714 | clearDeathNotifiers(); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 715 | |
| 716 | // create the right type of player |
| 717 | sp<MediaPlayerBase> p = createPlayer(playerType); |
| 718 | if (p == NULL) { |
| 719 | return p; |
| 720 | } |
| 721 | |
Marco Nelissen | 6dc3a3e | 2016-04-29 15:42:06 -0700 | [diff] [blame] | 722 | sp<IServiceManager> sm = defaultServiceManager(); |
| 723 | sp<IBinder> binder = sm->getService(String16("media.extractor")); |
Marco Nelissen | 1b5a7ee | 2016-09-30 13:54:30 -0700 | [diff] [blame] | 724 | if (binder == NULL) { |
| 725 | ALOGE("extractor service not available"); |
| 726 | return NULL; |
| 727 | } |
Marco Nelissen | 6dc3a3e | 2016-04-29 15:42:06 -0700 | [diff] [blame] | 728 | mExtractorDeathListener = new ServiceDeathNotifier(binder, p, MEDIAEXTRACTOR_PROCESS_DEATH); |
| 729 | binder->linkToDeath(mExtractorDeathListener); |
| 730 | |
Pawin Vongmasa | d4e9ca4 | 2017-03-29 17:24:56 -0700 | [diff] [blame] | 731 | if (property_get_bool("persist.media.treble_omx", true)) { |
Pawin Vongmasa | 9c47c97 | 2017-02-08 04:09:38 -0800 | [diff] [blame] | 732 | // Treble IOmx |
| 733 | sp<IOmx> omx = IOmx::getService(); |
| 734 | if (omx == nullptr) { |
| 735 | ALOGE("Treble IOmx not available"); |
| 736 | return NULL; |
| 737 | } |
| 738 | mCodecDeathListener = new ServiceDeathNotifier(omx, p, MEDIACODEC_PROCESS_DEATH); |
| 739 | omx->linkToDeath(mCodecDeathListener, 0); |
| 740 | } else { |
| 741 | // Legacy IOMX |
| 742 | binder = sm->getService(String16("media.codec")); |
| 743 | if (binder == NULL) { |
| 744 | ALOGE("codec service not available"); |
| 745 | return NULL; |
| 746 | } |
| 747 | mCodecDeathListener = new ServiceDeathNotifier(binder, p, MEDIACODEC_PROCESS_DEATH); |
| 748 | binder->linkToDeath(mCodecDeathListener); |
Marco Nelissen | 1b5a7ee | 2016-09-30 13:54:30 -0700 | [diff] [blame] | 749 | } |
Marco Nelissen | 6dc3a3e | 2016-04-29 15:42:06 -0700 | [diff] [blame] | 750 | |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 751 | if (!p->hardwareOutput()) { |
Jean-Michel Trivi | 2650e96 | 2015-07-22 18:14:02 -0700 | [diff] [blame] | 752 | Mutex::Autolock l(mLock); |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 753 | mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(), |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 754 | mPid, mAudioAttributes); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 755 | static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput); |
| 756 | } |
| 757 | |
| 758 | return p; |
| 759 | } |
| 760 | |
| 761 | void MediaPlayerService::Client::setDataSource_post( |
| 762 | const sp<MediaPlayerBase>& p, |
| 763 | status_t status) |
| 764 | { |
| 765 | ALOGV(" setDataSource"); |
| 766 | mStatus = status; |
| 767 | if (mStatus != OK) { |
| 768 | ALOGE(" error: %d", mStatus); |
| 769 | return; |
| 770 | } |
| 771 | |
| 772 | // Set the re-transmission endpoint if one was chosen. |
| 773 | if (mRetransmitEndpointValid) { |
| 774 | mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint); |
| 775 | if (mStatus != NO_ERROR) { |
| 776 | ALOGE("setRetransmitEndpoint error: %d", mStatus); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | if (mStatus == OK) { |
Wei Jia | 502c2f4 | 2017-07-13 17:47:56 -0700 | [diff] [blame] | 781 | Mutex::Autolock l(mLock); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 782 | mPlayer = p; |
| 783 | } |
| 784 | } |
| 785 | |
Andreas Huber | 2db8455 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 786 | status_t MediaPlayerService::Client::setDataSource( |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 787 | const sp<IMediaHTTPService> &httpService, |
| 788 | const char *url, |
| 789 | const KeyedVector<String8, String8> *headers) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 790 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 791 | ALOGV("setDataSource(%s)", url); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 792 | if (url == NULL) |
| 793 | return UNKNOWN_ERROR; |
| 794 | |
Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 795 | if ((strncmp(url, "http://", 7) == 0) || |
| 796 | (strncmp(url, "https://", 8) == 0) || |
| 797 | (strncmp(url, "rtsp://", 7) == 0)) { |
| 798 | if (!checkPermission("android.permission.INTERNET")) { |
| 799 | return PERMISSION_DENIED; |
| 800 | } |
| 801 | } |
| 802 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 803 | if (strncmp(url, "content://", 10) == 0) { |
| 804 | // get a filedescriptor for the content Uri and |
| 805 | // pass it to the setDataSource(fd) method |
| 806 | |
| 807 | String16 url16(url); |
| 808 | int fd = android::openContentProviderFile(url16); |
| 809 | if (fd < 0) |
| 810 | { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 811 | ALOGE("Couldn't open fd for %s", url); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 812 | return UNKNOWN_ERROR; |
| 813 | } |
| 814 | setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus |
| 815 | close(fd); |
| 816 | return mStatus; |
| 817 | } else { |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 818 | player_type playerType = MediaPlayerFactory::getPlayerType(this, url); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 819 | sp<MediaPlayerBase> p = setDataSource_pre(playerType); |
| 820 | if (p == NULL) { |
| 821 | return NO_INIT; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 822 | } |
| 823 | |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 824 | setDataSource_post(p, p->setDataSource(httpService, url, headers)); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 825 | return mStatus; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length) |
| 830 | { |
Marco Nelissen | 83b0fd9 | 2015-09-16 13:48:07 -0700 | [diff] [blame] | 831 | ALOGV("setDataSource fd=%d (%s), offset=%lld, length=%lld", |
| 832 | 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] | 833 | struct stat sb; |
| 834 | int ret = fstat(fd, &sb); |
| 835 | if (ret != 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 836 | 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] | 837 | return UNKNOWN_ERROR; |
| 838 | } |
| 839 | |
Andy Hung | 833b475 | 2016-04-04 17:15:48 -0700 | [diff] [blame] | 840 | ALOGV("st_dev = %llu", static_cast<unsigned long long>(sb.st_dev)); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 841 | ALOGV("st_mode = %u", sb.st_mode); |
Mark Salyzyn | 77342f7 | 2014-06-18 16:31:32 -0700 | [diff] [blame] | 842 | ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid)); |
| 843 | ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid)); |
Andy Hung | 833b475 | 2016-04-04 17:15:48 -0700 | [diff] [blame] | 844 | ALOGV("st_size = %llu", static_cast<unsigned long long>(sb.st_size)); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 845 | |
| 846 | if (offset >= sb.st_size) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 847 | ALOGE("offset error"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | return UNKNOWN_ERROR; |
| 849 | } |
| 850 | if (offset + length > sb.st_size) { |
| 851 | length = sb.st_size - offset; |
Andy Hung | 833b475 | 2016-04-04 17:15:48 -0700 | [diff] [blame] | 852 | ALOGV("calculated length = %lld", (long long)length); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 853 | } |
| 854 | |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 855 | player_type playerType = MediaPlayerFactory::getPlayerType(this, |
| 856 | fd, |
| 857 | offset, |
| 858 | length); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 859 | sp<MediaPlayerBase> p = setDataSource_pre(playerType); |
| 860 | if (p == NULL) { |
| 861 | return NO_INIT; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | // now set data source |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 865 | setDataSource_post(p, p->setDataSource(fd, offset, length)); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 866 | return mStatus; |
| 867 | } |
| 868 | |
Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 869 | status_t MediaPlayerService::Client::setDataSource( |
| 870 | const sp<IStreamSource> &source) { |
| 871 | // create the right type of player |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 872 | player_type playerType = MediaPlayerFactory::getPlayerType(this, source); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 873 | sp<MediaPlayerBase> p = setDataSource_pre(playerType); |
Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 874 | if (p == NULL) { |
| 875 | return NO_INIT; |
| 876 | } |
| 877 | |
Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 878 | // now set data source |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 879 | setDataSource_post(p, p->setDataSource(source)); |
Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 880 | return mStatus; |
| 881 | } |
| 882 | |
Chris Watkins | 99f3160 | 2015-03-20 13:06:33 -0700 | [diff] [blame] | 883 | status_t MediaPlayerService::Client::setDataSource( |
| 884 | const sp<IDataSource> &source) { |
| 885 | sp<DataSource> dataSource = DataSource::CreateFromIDataSource(source); |
| 886 | player_type playerType = MediaPlayerFactory::getPlayerType(this, dataSource); |
| 887 | sp<MediaPlayerBase> p = setDataSource_pre(playerType); |
| 888 | if (p == NULL) { |
| 889 | return NO_INIT; |
| 890 | } |
| 891 | // now set data source |
| 892 | setDataSource_post(p, p->setDataSource(dataSource)); |
| 893 | return mStatus; |
| 894 | } |
| 895 | |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 896 | void MediaPlayerService::Client::disconnectNativeWindow() { |
| 897 | if (mConnectedWindow != NULL) { |
Chong Zhang | 181fd9b | 2017-02-16 15:53:03 -0800 | [diff] [blame] | 898 | status_t err = nativeWindowDisconnect( |
| 899 | mConnectedWindow.get(), "disconnectNativeWindow"); |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 900 | |
| 901 | if (err != OK) { |
Chong Zhang | 181fd9b | 2017-02-16 15:53:03 -0800 | [diff] [blame] | 902 | ALOGW("nativeWindowDisconnect returned an error: %s (%d)", |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 903 | strerror(-err), err); |
| 904 | } |
| 905 | } |
| 906 | mConnectedWindow.clear(); |
| 907 | } |
| 908 | |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 909 | status_t MediaPlayerService::Client::setVideoSurfaceTexture( |
Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 910 | const sp<IGraphicBufferProducer>& bufferProducer) |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 911 | { |
Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 912 | ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get()); |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 913 | sp<MediaPlayerBase> p = getPlayer(); |
| 914 | if (p == 0) return UNKNOWN_ERROR; |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 915 | |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 916 | sp<IBinder> binder(IInterface::asBinder(bufferProducer)); |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 917 | if (mConnectedWindowBinder == binder) { |
| 918 | return OK; |
| 919 | } |
| 920 | |
| 921 | sp<ANativeWindow> anw; |
Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 922 | if (bufferProducer != NULL) { |
Marco Nelissen | ee08f7e | 2013-09-16 13:30:01 -0700 | [diff] [blame] | 923 | anw = new Surface(bufferProducer, true /* controlledByApp */); |
Chong Zhang | 181fd9b | 2017-02-16 15:53:03 -0800 | [diff] [blame] | 924 | status_t err = nativeWindowConnect(anw.get(), "setVideoSurfaceTexture"); |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 925 | |
| 926 | if (err != OK) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 927 | ALOGE("setVideoSurfaceTexture failed: %d", err); |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 928 | // Note that we must do the reset before disconnecting from the ANW. |
| 929 | // Otherwise queue/dequeue calls could be made on the disconnected |
| 930 | // ANW, which may result in errors. |
| 931 | reset(); |
| 932 | |
| 933 | disconnectNativeWindow(); |
| 934 | |
| 935 | return err; |
| 936 | } |
| 937 | } |
| 938 | |
Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 939 | // Note that we must set the player's new GraphicBufferProducer before |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 940 | // disconnecting the old one. Otherwise queue/dequeue calls could be made |
| 941 | // on the disconnected ANW, which may result in errors. |
Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 942 | status_t err = p->setVideoSurfaceTexture(bufferProducer); |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 943 | |
| 944 | disconnectNativeWindow(); |
| 945 | |
| 946 | mConnectedWindow = anw; |
| 947 | |
| 948 | if (err == OK) { |
| 949 | mConnectedWindowBinder = binder; |
| 950 | } else { |
| 951 | disconnectNativeWindow(); |
| 952 | } |
| 953 | |
| 954 | return err; |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 955 | } |
| 956 | |
Nicolas Catania | 1d187f1 | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 957 | status_t MediaPlayerService::Client::invoke(const Parcel& request, |
| 958 | Parcel *reply) |
| 959 | { |
| 960 | sp<MediaPlayerBase> p = getPlayer(); |
| 961 | if (p == NULL) return UNKNOWN_ERROR; |
| 962 | return p->invoke(request, reply); |
| 963 | } |
| 964 | |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 965 | // This call doesn't need to access the native player. |
| 966 | status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter) |
| 967 | { |
| 968 | status_t status; |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 969 | media::Metadata::Filter allow, drop; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 970 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 971 | if (unmarshallFilter(filter, &allow, &status) && |
| 972 | unmarshallFilter(filter, &drop, &status)) { |
| 973 | Mutex::Autolock lock(mLock); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 974 | |
| 975 | mMetadataAllow = allow; |
| 976 | mMetadataDrop = drop; |
| 977 | } |
| 978 | return status; |
| 979 | } |
| 980 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 981 | status_t MediaPlayerService::Client::getMetadata( |
Mark Salyzyn | 77342f7 | 2014-06-18 16:31:32 -0700 | [diff] [blame] | 982 | bool update_only, bool /*apply_filter*/, Parcel *reply) |
Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 983 | { |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 984 | sp<MediaPlayerBase> player = getPlayer(); |
| 985 | if (player == 0) return UNKNOWN_ERROR; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 986 | |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 987 | status_t status; |
| 988 | // Placeholder for the return code, updated by the caller. |
| 989 | reply->writeInt32(-1); |
| 990 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 991 | media::Metadata::Filter ids; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 992 | |
| 993 | // We don't block notifications while we fetch the data. We clear |
| 994 | // mMetadataUpdated first so we don't lose notifications happening |
| 995 | // during the rest of this call. |
| 996 | { |
| 997 | Mutex::Autolock lock(mLock); |
| 998 | if (update_only) { |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 999 | ids = mMetadataUpdated; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1000 | } |
| 1001 | mMetadataUpdated.clear(); |
| 1002 | } |
Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 1003 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1004 | media::Metadata metadata(reply); |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1005 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1006 | metadata.appendHeader(); |
| 1007 | status = player->getMetadata(ids, reply); |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 1008 | |
| 1009 | if (status != OK) { |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1010 | metadata.resetParcel(); |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1011 | ALOGE("getMetadata failed %d", status); |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 1012 | return status; |
| 1013 | } |
| 1014 | |
| 1015 | // FIXME: Implement filtering on the result. Not critical since |
| 1016 | // filtering takes place on the update notifications already. This |
| 1017 | // would be when all the metadata are fetch and a filter is set. |
| 1018 | |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 1019 | // Everything is fine, update the metadata length. |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1020 | metadata.updateLength(); |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 1021 | return OK; |
Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
Wei Jia | d399e7e | 2016-10-26 15:49:11 -0700 | [diff] [blame] | 1024 | status_t MediaPlayerService::Client::setBufferingSettings( |
| 1025 | const BufferingSettings& buffering) |
| 1026 | { |
Wei Jia | dc6f340 | 2017-01-09 15:04:18 -0800 | [diff] [blame] | 1027 | ALOGV("[%d] setBufferingSettings{%s}", |
| 1028 | mConnId, buffering.toString().string()); |
Wei Jia | d399e7e | 2016-10-26 15:49:11 -0700 | [diff] [blame] | 1029 | sp<MediaPlayerBase> p = getPlayer(); |
| 1030 | if (p == 0) return UNKNOWN_ERROR; |
| 1031 | return p->setBufferingSettings(buffering); |
| 1032 | } |
| 1033 | |
| 1034 | status_t MediaPlayerService::Client::getDefaultBufferingSettings( |
| 1035 | BufferingSettings* buffering /* nonnull */) |
| 1036 | { |
| 1037 | sp<MediaPlayerBase> p = getPlayer(); |
| 1038 | // TODO: create mPlayer on demand. |
| 1039 | if (p == 0) return UNKNOWN_ERROR; |
| 1040 | status_t ret = p->getDefaultBufferingSettings(buffering); |
| 1041 | if (ret == NO_ERROR) { |
Wei Jia | dc6f340 | 2017-01-09 15:04:18 -0800 | [diff] [blame] | 1042 | ALOGV("[%d] getDefaultBufferingSettings{%s}", |
| 1043 | mConnId, buffering->toString().string()); |
Wei Jia | d399e7e | 2016-10-26 15:49:11 -0700 | [diff] [blame] | 1044 | } else { |
| 1045 | ALOGV("[%d] getDefaultBufferingSettings returned %d", mConnId, ret); |
| 1046 | } |
| 1047 | return ret; |
| 1048 | } |
| 1049 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1050 | status_t MediaPlayerService::Client::prepareAsync() |
| 1051 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1052 | ALOGV("[%d] prepareAsync", mConnId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1053 | sp<MediaPlayerBase> p = getPlayer(); |
| 1054 | if (p == 0) return UNKNOWN_ERROR; |
| 1055 | status_t ret = p->prepareAsync(); |
| 1056 | #if CALLBACK_ANTAGONIZER |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1057 | ALOGD("start Antagonizer"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1058 | if (ret == NO_ERROR) mAntagonizer->start(); |
| 1059 | #endif |
| 1060 | return ret; |
| 1061 | } |
| 1062 | |
| 1063 | status_t MediaPlayerService::Client::start() |
| 1064 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1065 | ALOGV("[%d] start", mConnId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1066 | sp<MediaPlayerBase> p = getPlayer(); |
| 1067 | if (p == 0) return UNKNOWN_ERROR; |
| 1068 | p->setLooping(mLoop); |
| 1069 | return p->start(); |
| 1070 | } |
| 1071 | |
| 1072 | status_t MediaPlayerService::Client::stop() |
| 1073 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1074 | ALOGV("[%d] stop", mConnId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1075 | sp<MediaPlayerBase> p = getPlayer(); |
| 1076 | if (p == 0) return UNKNOWN_ERROR; |
| 1077 | return p->stop(); |
| 1078 | } |
| 1079 | |
| 1080 | status_t MediaPlayerService::Client::pause() |
| 1081 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1082 | ALOGV("[%d] pause", mConnId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1083 | sp<MediaPlayerBase> p = getPlayer(); |
| 1084 | if (p == 0) return UNKNOWN_ERROR; |
| 1085 | return p->pause(); |
| 1086 | } |
| 1087 | |
| 1088 | status_t MediaPlayerService::Client::isPlaying(bool* state) |
| 1089 | { |
| 1090 | *state = false; |
| 1091 | sp<MediaPlayerBase> p = getPlayer(); |
| 1092 | if (p == 0) return UNKNOWN_ERROR; |
| 1093 | *state = p->isPlaying(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1094 | ALOGV("[%d] isPlaying: %d", mConnId, *state); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1095 | return NO_ERROR; |
| 1096 | } |
| 1097 | |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 1098 | status_t MediaPlayerService::Client::setPlaybackSettings(const AudioPlaybackRate& rate) |
Wei Jia | 9816016 | 2015-02-04 17:01:11 -0800 | [diff] [blame] | 1099 | { |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 1100 | ALOGV("[%d] setPlaybackSettings(%f, %f, %d, %d)", |
| 1101 | mConnId, rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode); |
Wei Jia | 9816016 | 2015-02-04 17:01:11 -0800 | [diff] [blame] | 1102 | sp<MediaPlayerBase> p = getPlayer(); |
| 1103 | if (p == 0) return UNKNOWN_ERROR; |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 1104 | return p->setPlaybackSettings(rate); |
| 1105 | } |
| 1106 | |
| 1107 | status_t MediaPlayerService::Client::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */) |
| 1108 | { |
| 1109 | sp<MediaPlayerBase> p = getPlayer(); |
| 1110 | if (p == 0) return UNKNOWN_ERROR; |
| 1111 | status_t ret = p->getPlaybackSettings(rate); |
| 1112 | if (ret == NO_ERROR) { |
| 1113 | ALOGV("[%d] getPlaybackSettings(%f, %f, %d, %d)", |
| 1114 | mConnId, rate->mSpeed, rate->mPitch, rate->mFallbackMode, rate->mStretchMode); |
| 1115 | } else { |
| 1116 | ALOGV("[%d] getPlaybackSettings returned %d", mConnId, ret); |
| 1117 | } |
| 1118 | return ret; |
| 1119 | } |
| 1120 | |
| 1121 | status_t MediaPlayerService::Client::setSyncSettings( |
| 1122 | const AVSyncSettings& sync, float videoFpsHint) |
| 1123 | { |
| 1124 | ALOGV("[%d] setSyncSettings(%u, %u, %f, %f)", |
| 1125 | mConnId, sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint); |
| 1126 | sp<MediaPlayerBase> p = getPlayer(); |
| 1127 | if (p == 0) return UNKNOWN_ERROR; |
| 1128 | return p->setSyncSettings(sync, videoFpsHint); |
| 1129 | } |
| 1130 | |
| 1131 | status_t MediaPlayerService::Client::getSyncSettings( |
| 1132 | AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */) |
| 1133 | { |
| 1134 | sp<MediaPlayerBase> p = getPlayer(); |
| 1135 | if (p == 0) return UNKNOWN_ERROR; |
| 1136 | status_t ret = p->getSyncSettings(sync, videoFps); |
| 1137 | if (ret == NO_ERROR) { |
| 1138 | ALOGV("[%d] getSyncSettings(%u, %u, %f, %f)", |
| 1139 | mConnId, sync->mSource, sync->mAudioAdjustMode, sync->mTolerance, *videoFps); |
| 1140 | } else { |
| 1141 | ALOGV("[%d] getSyncSettings returned %d", mConnId, ret); |
| 1142 | } |
| 1143 | return ret; |
Wei Jia | 9816016 | 2015-02-04 17:01:11 -0800 | [diff] [blame] | 1144 | } |
| 1145 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1146 | status_t MediaPlayerService::Client::getCurrentPosition(int *msec) |
| 1147 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1148 | ALOGV("getCurrentPosition"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1149 | sp<MediaPlayerBase> p = getPlayer(); |
| 1150 | if (p == 0) return UNKNOWN_ERROR; |
| 1151 | status_t ret = p->getCurrentPosition(msec); |
| 1152 | if (ret == NO_ERROR) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1153 | ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1154 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1155 | ALOGE("getCurrentPosition returned %d", ret); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1156 | } |
| 1157 | return ret; |
| 1158 | } |
| 1159 | |
| 1160 | status_t MediaPlayerService::Client::getDuration(int *msec) |
| 1161 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1162 | ALOGV("getDuration"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1163 | sp<MediaPlayerBase> p = getPlayer(); |
| 1164 | if (p == 0) return UNKNOWN_ERROR; |
| 1165 | status_t ret = p->getDuration(msec); |
| 1166 | if (ret == NO_ERROR) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1167 | ALOGV("[%d] getDuration = %d", mConnId, *msec); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1168 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1169 | ALOGE("getDuration returned %d", ret); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1170 | } |
| 1171 | return ret; |
| 1172 | } |
| 1173 | |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1174 | status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) { |
| 1175 | ALOGV("setNextPlayer"); |
| 1176 | Mutex::Autolock l(mLock); |
| 1177 | sp<Client> c = static_cast<Client*>(player.get()); |
Wei Jia | 2828412 | 2016-08-30 13:49:06 -0700 | [diff] [blame] | 1178 | if (c != NULL && !mService->hasClient(c)) { |
Robert Shih | ee0a0e3 | 2016-08-16 16:50:54 -0700 | [diff] [blame] | 1179 | return BAD_VALUE; |
| 1180 | } |
| 1181 | |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1182 | mNextClient = c; |
John Grossman | 5f7e55e | 2012-08-24 14:47:25 -0700 | [diff] [blame] | 1183 | |
| 1184 | if (c != NULL) { |
| 1185 | if (mAudioOutput != NULL) { |
| 1186 | mAudioOutput->setNextOutput(c->mAudioOutput); |
| 1187 | } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) { |
| 1188 | ALOGE("no current audio output"); |
| 1189 | } |
| 1190 | |
| 1191 | if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) { |
| 1192 | mPlayer->setNextPlayer(mNextClient->getPlayer()); |
| 1193 | } |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1194 | } |
John Grossman | 5f7e55e | 2012-08-24 14:47:25 -0700 | [diff] [blame] | 1195 | |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1196 | return OK; |
| 1197 | } |
| 1198 | |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 1199 | VolumeShaper::Status MediaPlayerService::Client::applyVolumeShaper( |
| 1200 | const sp<VolumeShaper::Configuration>& configuration, |
| 1201 | const sp<VolumeShaper::Operation>& operation) { |
| 1202 | // for hardware output, call player instead |
| 1203 | ALOGV("Client::applyVolumeShaper(%p)", this); |
| 1204 | sp<MediaPlayerBase> p = getPlayer(); |
| 1205 | { |
| 1206 | Mutex::Autolock l(mLock); |
| 1207 | if (p != 0 && p->hardwareOutput()) { |
| 1208 | // TODO: investigate internal implementation |
| 1209 | return VolumeShaper::Status(INVALID_OPERATION); |
| 1210 | } |
| 1211 | if (mAudioOutput.get() != nullptr) { |
| 1212 | return mAudioOutput->applyVolumeShaper(configuration, operation); |
| 1213 | } |
| 1214 | } |
| 1215 | return VolumeShaper::Status(INVALID_OPERATION); |
| 1216 | } |
| 1217 | |
| 1218 | sp<VolumeShaper::State> MediaPlayerService::Client::getVolumeShaperState(int id) { |
| 1219 | // for hardware output, call player instead |
| 1220 | ALOGV("Client::getVolumeShaperState(%p)", this); |
| 1221 | sp<MediaPlayerBase> p = getPlayer(); |
| 1222 | { |
| 1223 | Mutex::Autolock l(mLock); |
| 1224 | if (p != 0 && p->hardwareOutput()) { |
| 1225 | // TODO: investigate internal implementation. |
| 1226 | return nullptr; |
| 1227 | } |
| 1228 | if (mAudioOutput.get() != nullptr) { |
| 1229 | return mAudioOutput->getVolumeShaperState(id); |
| 1230 | } |
| 1231 | } |
| 1232 | return nullptr; |
| 1233 | } |
| 1234 | |
Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 1235 | status_t MediaPlayerService::Client::seekTo(int msec, MediaPlayerSeekMode mode) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1236 | { |
Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 1237 | ALOGV("[%d] seekTo(%d, %d)", mConnId, msec, mode); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1238 | sp<MediaPlayerBase> p = getPlayer(); |
| 1239 | if (p == 0) return UNKNOWN_ERROR; |
Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 1240 | return p->seekTo(msec, mode); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | status_t MediaPlayerService::Client::reset() |
| 1244 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1245 | ALOGV("[%d] reset", mConnId); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 1246 | mRetransmitEndpointValid = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1247 | sp<MediaPlayerBase> p = getPlayer(); |
| 1248 | if (p == 0) return UNKNOWN_ERROR; |
| 1249 | return p->reset(); |
| 1250 | } |
| 1251 | |
Wei Jia | 52c2851 | 2017-09-13 18:17:51 -0700 | [diff] [blame] | 1252 | status_t MediaPlayerService::Client::notifyAt(int64_t mediaTimeUs) |
| 1253 | { |
| 1254 | ALOGV("[%d] notifyAt(%lld)", mConnId, (long long)mediaTimeUs); |
| 1255 | sp<MediaPlayerBase> p = getPlayer(); |
| 1256 | if (p == 0) return UNKNOWN_ERROR; |
| 1257 | return p->notifyAt(mediaTimeUs); |
| 1258 | } |
| 1259 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1260 | 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] | 1261 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1262 | ALOGV("[%d] setAudioStreamType(%d)", mConnId, type); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1263 | // TODO: for hardware output, call player instead |
| 1264 | Mutex::Autolock l(mLock); |
| 1265 | if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type); |
| 1266 | return NO_ERROR; |
| 1267 | } |
| 1268 | |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1269 | status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel) |
| 1270 | { |
| 1271 | if (mAudioAttributes != NULL) { free(mAudioAttributes); } |
| 1272 | mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t)); |
Jean-Michel Trivi | 2650e96 | 2015-07-22 18:14:02 -0700 | [diff] [blame] | 1273 | if (mAudioAttributes == NULL) { |
| 1274 | return NO_MEMORY; |
| 1275 | } |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1276 | unmarshallAudioAttributes(parcel, mAudioAttributes); |
| 1277 | |
| 1278 | ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s", |
| 1279 | mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags, |
| 1280 | mAudioAttributes->tags); |
| 1281 | |
| 1282 | if (mAudioOutput != 0) { |
| 1283 | mAudioOutput->setAudioAttributes(mAudioAttributes); |
| 1284 | } |
| 1285 | return NO_ERROR; |
| 1286 | } |
| 1287 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1288 | status_t MediaPlayerService::Client::setLooping(int loop) |
| 1289 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1290 | ALOGV("[%d] setLooping(%d)", mConnId, loop); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1291 | mLoop = loop; |
| 1292 | sp<MediaPlayerBase> p = getPlayer(); |
| 1293 | if (p != 0) return p->setLooping(loop); |
| 1294 | return NO_ERROR; |
| 1295 | } |
| 1296 | |
| 1297 | status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume) |
| 1298 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1299 | ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume); |
John Grossman | 761defc | 2012-02-09 15:09:05 -0800 | [diff] [blame] | 1300 | |
| 1301 | // for hardware output, call player instead |
| 1302 | sp<MediaPlayerBase> p = getPlayer(); |
| 1303 | { |
| 1304 | Mutex::Autolock l(mLock); |
| 1305 | if (p != 0 && p->hardwareOutput()) { |
| 1306 | MediaPlayerHWInterface* hwp = |
| 1307 | reinterpret_cast<MediaPlayerHWInterface*>(p.get()); |
| 1308 | return hwp->setVolume(leftVolume, rightVolume); |
| 1309 | } else { |
| 1310 | if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume); |
| 1311 | return NO_ERROR; |
| 1312 | } |
| 1313 | } |
| 1314 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1315 | return NO_ERROR; |
| 1316 | } |
| 1317 | |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1318 | status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level) |
| 1319 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1320 | ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1321 | Mutex::Autolock l(mLock); |
| 1322 | if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level); |
| 1323 | return NO_ERROR; |
| 1324 | } |
| 1325 | |
| 1326 | status_t MediaPlayerService::Client::attachAuxEffect(int effectId) |
| 1327 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1328 | ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1329 | Mutex::Autolock l(mLock); |
| 1330 | if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId); |
| 1331 | return NO_ERROR; |
| 1332 | } |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1333 | |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 1334 | status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1335 | ALOGV("[%d] setParameter(%d)", mConnId, key); |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1336 | switch (key) { |
| 1337 | case KEY_PARAMETER_AUDIO_ATTRIBUTES: |
| 1338 | { |
| 1339 | Mutex::Autolock l(mLock); |
| 1340 | return setAudioAttributes_l(request); |
| 1341 | } |
| 1342 | default: |
| 1343 | sp<MediaPlayerBase> p = getPlayer(); |
| 1344 | if (p == 0) { return UNKNOWN_ERROR; } |
| 1345 | return p->setParameter(key, request); |
| 1346 | } |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1350 | ALOGV("[%d] getParameter(%d)", mConnId, key); |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 1351 | sp<MediaPlayerBase> p = getPlayer(); |
| 1352 | if (p == 0) return UNKNOWN_ERROR; |
| 1353 | return p->getParameter(key, reply); |
| 1354 | } |
| 1355 | |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 1356 | status_t MediaPlayerService::Client::setRetransmitEndpoint( |
| 1357 | const struct sockaddr_in* endpoint) { |
| 1358 | |
| 1359 | if (NULL != endpoint) { |
| 1360 | uint32_t a = ntohl(endpoint->sin_addr.s_addr); |
| 1361 | uint16_t p = ntohs(endpoint->sin_port); |
| 1362 | ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId, |
| 1363 | (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p); |
| 1364 | } else { |
| 1365 | ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId); |
| 1366 | } |
| 1367 | |
| 1368 | sp<MediaPlayerBase> p = getPlayer(); |
| 1369 | |
| 1370 | // Right now, the only valid time to set a retransmit endpoint is before |
| 1371 | // player selection has been made (since the presence or absence of a |
| 1372 | // retransmit endpoint is going to determine which player is selected during |
| 1373 | // setDataSource). |
| 1374 | if (p != 0) return INVALID_OPERATION; |
| 1375 | |
| 1376 | if (NULL != endpoint) { |
| 1377 | mRetransmitEndpoint = *endpoint; |
| 1378 | mRetransmitEndpointValid = true; |
| 1379 | } else { |
| 1380 | mRetransmitEndpointValid = false; |
| 1381 | } |
| 1382 | |
| 1383 | return NO_ERROR; |
| 1384 | } |
| 1385 | |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 1386 | status_t MediaPlayerService::Client::getRetransmitEndpoint( |
| 1387 | struct sockaddr_in* endpoint) |
| 1388 | { |
| 1389 | if (NULL == endpoint) |
| 1390 | return BAD_VALUE; |
| 1391 | |
| 1392 | sp<MediaPlayerBase> p = getPlayer(); |
| 1393 | |
| 1394 | if (p != NULL) |
| 1395 | return p->getRetransmitEndpoint(endpoint); |
| 1396 | |
| 1397 | if (!mRetransmitEndpointValid) |
| 1398 | return NO_INIT; |
| 1399 | |
| 1400 | *endpoint = mRetransmitEndpoint; |
| 1401 | |
| 1402 | return NO_ERROR; |
| 1403 | } |
| 1404 | |
Gloria Wang | b483c47 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 1405 | void MediaPlayerService::Client::notify( |
| 1406 | 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] | 1407 | { |
| 1408 | Client* client = static_cast<Client*>(cookie); |
James Dong | b8a9825 | 2012-08-26 16:13:03 -0700 | [diff] [blame] | 1409 | if (client == NULL) { |
| 1410 | return; |
| 1411 | } |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1412 | |
James Dong | b8a9825 | 2012-08-26 16:13:03 -0700 | [diff] [blame] | 1413 | sp<IMediaPlayerClient> c; |
Haynes Mathew George | 6cb3375 | 2015-06-22 14:16:42 -0700 | [diff] [blame] | 1414 | sp<Client> nextClient; |
| 1415 | status_t errStartNext = NO_ERROR; |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1416 | { |
| 1417 | Mutex::Autolock l(client->mLock); |
James Dong | b8a9825 | 2012-08-26 16:13:03 -0700 | [diff] [blame] | 1418 | c = client->mClient; |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1419 | if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) { |
Haynes Mathew George | 6cb3375 | 2015-06-22 14:16:42 -0700 | [diff] [blame] | 1420 | nextClient = client->mNextClient; |
| 1421 | |
John Grossman | cb0b755 | 2012-08-23 17:47:31 -0700 | [diff] [blame] | 1422 | if (client->mAudioOutput != NULL) |
| 1423 | client->mAudioOutput->switchToNextOutput(); |
Haynes Mathew George | 6cb3375 | 2015-06-22 14:16:42 -0700 | [diff] [blame] | 1424 | |
| 1425 | errStartNext = nextClient->start(); |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | if (nextClient != NULL) { |
| 1430 | sp<IMediaPlayerClient> nc; |
| 1431 | { |
| 1432 | Mutex::Autolock l(nextClient->mLock); |
| 1433 | nc = nextClient->mClient; |
| 1434 | } |
| 1435 | if (nc != NULL) { |
| 1436 | if (errStartNext == NO_ERROR) { |
| 1437 | nc->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj); |
| 1438 | } else { |
| 1439 | nc->notify(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN , 0, obj); |
| 1440 | ALOGE("gapless:start playback for next track failed, err(%d)", errStartNext); |
Wei Jia | 2afac0c | 2016-01-07 12:13:07 -0800 | [diff] [blame] | 1441 | } |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1442 | } |
| 1443 | } |
| 1444 | |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1445 | if (MEDIA_INFO == msg && |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1446 | MEDIA_INFO_METADATA_UPDATE == ext1) { |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1447 | const media::Metadata::Type metadata_type = ext2; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1448 | |
| 1449 | if(client->shouldDropMetadata(metadata_type)) { |
| 1450 | return; |
| 1451 | } |
| 1452 | |
| 1453 | // Update the list of metadata that have changed. getMetadata |
| 1454 | // also access mMetadataUpdated and clears it. |
| 1455 | client->addNewMetadataUpdate(metadata_type); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1456 | } |
James Dong | b8a9825 | 2012-08-26 16:13:03 -0700 | [diff] [blame] | 1457 | |
| 1458 | if (c != NULL) { |
| 1459 | ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2); |
| 1460 | c->notify(msg, ext1, ext2, obj); |
| 1461 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1462 | } |
| 1463 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1464 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1465 | bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1466 | { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1467 | Mutex::Autolock lock(mLock); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1468 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1469 | if (findMetadata(mMetadataDrop, code)) { |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1470 | return true; |
| 1471 | } |
| 1472 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1473 | if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) { |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1474 | return false; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1475 | } else { |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1476 | return true; |
| 1477 | } |
| 1478 | } |
| 1479 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1480 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1481 | void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1482 | Mutex::Autolock lock(mLock); |
| 1483 | if (mMetadataUpdated.indexOf(metadata_type) < 0) { |
| 1484 | mMetadataUpdated.add(metadata_type); |
| 1485 | } |
| 1486 | } |
| 1487 | |
Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 1488 | // Modular DRM |
| 1489 | status_t MediaPlayerService::Client::prepareDrm(const uint8_t uuid[16], |
| 1490 | const Vector<uint8_t>& drmSessionId) |
| 1491 | { |
| 1492 | ALOGV("[%d] prepareDrm", mConnId); |
| 1493 | sp<MediaPlayerBase> p = getPlayer(); |
| 1494 | if (p == 0) return UNKNOWN_ERROR; |
| 1495 | |
| 1496 | status_t ret = p->prepareDrm(uuid, drmSessionId); |
| 1497 | ALOGV("prepareDrm ret: %d", ret); |
| 1498 | |
| 1499 | return ret; |
| 1500 | } |
| 1501 | |
| 1502 | status_t MediaPlayerService::Client::releaseDrm() |
| 1503 | { |
| 1504 | ALOGV("[%d] releaseDrm", mConnId); |
| 1505 | sp<MediaPlayerBase> p = getPlayer(); |
| 1506 | if (p == 0) return UNKNOWN_ERROR; |
| 1507 | |
| 1508 | status_t ret = p->releaseDrm(); |
| 1509 | ALOGV("releaseDrm ret: %d", ret); |
| 1510 | |
| 1511 | return ret; |
| 1512 | } |
| 1513 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1514 | #if CALLBACK_ANTAGONIZER |
| 1515 | const int Antagonizer::interval = 10000; // 10 msecs |
| 1516 | |
| 1517 | Antagonizer::Antagonizer(notify_callback_f cb, void* client) : |
| 1518 | mExit(false), mActive(false), mClient(client), mCb(cb) |
| 1519 | { |
| 1520 | createThread(callbackThread, this); |
| 1521 | } |
| 1522 | |
| 1523 | void Antagonizer::kill() |
| 1524 | { |
| 1525 | Mutex::Autolock _l(mLock); |
| 1526 | mActive = false; |
| 1527 | mExit = true; |
| 1528 | mCondition.wait(mLock); |
| 1529 | } |
| 1530 | |
| 1531 | int Antagonizer::callbackThread(void* user) |
| 1532 | { |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1533 | ALOGD("Antagonizer started"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1534 | Antagonizer* p = reinterpret_cast<Antagonizer*>(user); |
| 1535 | while (!p->mExit) { |
| 1536 | if (p->mActive) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1537 | ALOGV("send event"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1538 | p->mCb(p->mClient, 0, 0, 0); |
| 1539 | } |
| 1540 | usleep(interval); |
| 1541 | } |
| 1542 | Mutex::Autolock _l(p->mLock); |
| 1543 | p->mCondition.signal(); |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1544 | ALOGD("Antagonizer stopped"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1545 | return 0; |
| 1546 | } |
| 1547 | #endif |
| 1548 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1549 | #undef LOG_TAG |
| 1550 | #define LOG_TAG "AudioSink" |
Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 1551 | MediaPlayerService::AudioOutput::AudioOutput(audio_session_t sessionId, uid_t uid, int pid, |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1552 | const audio_attributes_t* attr) |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1553 | : mCallback(NULL), |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 1554 | mCallbackCookie(NULL), |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1555 | mCallbackData(NULL), |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1556 | mStreamType(AUDIO_STREAM_MUSIC), |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1557 | mLeftVolume(1.0), |
| 1558 | mRightVolume(1.0), |
| 1559 | mPlaybackRate(AUDIO_PLAYBACK_RATE_DEFAULT), |
| 1560 | mSampleRateHz(0), |
| 1561 | mMsecsPerFrame(0), |
| 1562 | mFrameSize(0), |
Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1563 | mSessionId(sessionId), |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1564 | mUid(uid), |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 1565 | mPid(pid), |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1566 | mSendLevel(0.0), |
| 1567 | mAuxEffectId(0), |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 1568 | mFlags(AUDIO_OUTPUT_FLAG_NONE), |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 1569 | mVolumeHandler(new media::VolumeHandler()) |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1570 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1571 | ALOGV("AudioOutput(%d)", sessionId); |
Eric Laurent | 4356269 | 2015-07-15 16:49:07 -0700 | [diff] [blame] | 1572 | if (attr != NULL) { |
Jean-Michel Trivi | 2650e96 | 2015-07-22 18:14:02 -0700 | [diff] [blame] | 1573 | mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t)); |
| 1574 | if (mAttributes != NULL) { |
| 1575 | memcpy(mAttributes, attr, sizeof(audio_attributes_t)); |
| 1576 | mStreamType = audio_attributes_to_stream_type(attr); |
| 1577 | } |
| 1578 | } else { |
| 1579 | mAttributes = NULL; |
Eric Laurent | 4356269 | 2015-07-15 16:49:07 -0700 | [diff] [blame] | 1580 | } |
| 1581 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1582 | setMinBufferCount(); |
| 1583 | } |
| 1584 | |
| 1585 | MediaPlayerService::AudioOutput::~AudioOutput() |
| 1586 | { |
| 1587 | close(); |
Jean-Michel Trivi | 2650e96 | 2015-07-22 18:14:02 -0700 | [diff] [blame] | 1588 | free(mAttributes); |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1589 | delete mCallbackData; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1590 | } |
| 1591 | |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1592 | //static |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1593 | void MediaPlayerService::AudioOutput::setMinBufferCount() |
| 1594 | { |
| 1595 | char value[PROPERTY_VALUE_MAX]; |
| 1596 | if (property_get("ro.kernel.qemu", value, 0)) { |
| 1597 | mIsOnEmulator = true; |
| 1598 | mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator |
| 1599 | } |
| 1600 | } |
| 1601 | |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1602 | // static |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1603 | bool MediaPlayerService::AudioOutput::isOnEmulator() |
| 1604 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1605 | setMinBufferCount(); // benign race wrt other threads |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1606 | return mIsOnEmulator; |
| 1607 | } |
| 1608 | |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1609 | // static |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1610 | int MediaPlayerService::AudioOutput::getMinBufferCount() |
| 1611 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1612 | setMinBufferCount(); // benign race wrt other threads |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1613 | return mMinBufferCount; |
| 1614 | } |
| 1615 | |
| 1616 | ssize_t MediaPlayerService::AudioOutput::bufferSize() const |
| 1617 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1618 | Mutex::Autolock lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1619 | if (mTrack == 0) return NO_INIT; |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1620 | return mTrack->frameCount() * mFrameSize; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | ssize_t MediaPlayerService::AudioOutput::frameCount() const |
| 1624 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1625 | Mutex::Autolock lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1626 | if (mTrack == 0) return NO_INIT; |
| 1627 | return mTrack->frameCount(); |
| 1628 | } |
| 1629 | |
| 1630 | ssize_t MediaPlayerService::AudioOutput::channelCount() const |
| 1631 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1632 | Mutex::Autolock lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1633 | if (mTrack == 0) return NO_INIT; |
| 1634 | return mTrack->channelCount(); |
| 1635 | } |
| 1636 | |
| 1637 | ssize_t MediaPlayerService::AudioOutput::frameSize() const |
| 1638 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1639 | Mutex::Autolock lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1640 | if (mTrack == 0) return NO_INIT; |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1641 | return mFrameSize; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | uint32_t MediaPlayerService::AudioOutput::latency () const |
| 1645 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1646 | Mutex::Autolock lock(mLock); |
Eric Laurent | db354e5 | 2012-03-05 17:27:11 -0800 | [diff] [blame] | 1647 | if (mTrack == 0) return 0; |
| 1648 | return mTrack->latency(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | float MediaPlayerService::AudioOutput::msecsPerFrame() const |
| 1652 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1653 | Mutex::Autolock lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1654 | return mMsecsPerFrame; |
| 1655 | } |
| 1656 | |
Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1657 | status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 1658 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1659 | Mutex::Autolock lock(mLock); |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 1660 | if (mTrack == 0) return NO_INIT; |
| 1661 | return mTrack->getPosition(position); |
| 1662 | } |
| 1663 | |
Lajos Molnar | 06ad152 | 2014-08-28 07:27:44 -0700 | [diff] [blame] | 1664 | status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const |
| 1665 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1666 | Mutex::Autolock lock(mLock); |
Lajos Molnar | 06ad152 | 2014-08-28 07:27:44 -0700 | [diff] [blame] | 1667 | if (mTrack == 0) return NO_INIT; |
| 1668 | return mTrack->getTimestamp(ts); |
| 1669 | } |
| 1670 | |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1671 | // TODO: Remove unnecessary calls to getPlayedOutDurationUs() |
| 1672 | // as it acquires locks and may query the audio driver. |
| 1673 | // |
| 1674 | // Some calls could conceivably retrieve extrapolated data instead of |
| 1675 | // accessing getTimestamp() or getPosition() every time a data buffer with |
| 1676 | // a media time is received. |
| 1677 | // |
| 1678 | // Calculate duration of played samples if played at normal rate (i.e., 1.0). |
| 1679 | int64_t MediaPlayerService::AudioOutput::getPlayedOutDurationUs(int64_t nowUs) const |
| 1680 | { |
| 1681 | Mutex::Autolock lock(mLock); |
| 1682 | if (mTrack == 0 || mSampleRateHz == 0) { |
Wei Jia | 213f490 | 2015-10-22 08:55:25 -0700 | [diff] [blame] | 1683 | return 0; |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | uint32_t numFramesPlayed; |
Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1687 | int64_t numFramesPlayedAtUs; |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1688 | AudioTimestamp ts; |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1689 | |
| 1690 | status_t res = mTrack->getTimestamp(ts); |
| 1691 | if (res == OK) { // case 1: mixing audio tracks and offloaded tracks. |
| 1692 | numFramesPlayed = ts.mPosition; |
Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1693 | numFramesPlayedAtUs = ts.mTime.tv_sec * 1000000LL + ts.mTime.tv_nsec / 1000; |
| 1694 | //ALOGD("getTimestamp: OK %d %lld", numFramesPlayed, (long long)numFramesPlayedAtUs); |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1695 | } else if (res == WOULD_BLOCK) { // case 2: transitory state on start of a new track |
| 1696 | numFramesPlayed = 0; |
Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1697 | numFramesPlayedAtUs = nowUs; |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1698 | //ALOGD("getTimestamp: WOULD_BLOCK %d %lld", |
Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1699 | // numFramesPlayed, (long long)numFramesPlayedAtUs); |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1700 | } else { // case 3: transitory at new track or audio fast tracks. |
| 1701 | res = mTrack->getPosition(&numFramesPlayed); |
| 1702 | CHECK_EQ(res, (status_t)OK); |
Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1703 | numFramesPlayedAtUs = nowUs; |
| 1704 | numFramesPlayedAtUs += 1000LL * mTrack->latency() / 2; /* XXX */ |
| 1705 | //ALOGD("getPosition: %u %lld", numFramesPlayed, (long long)numFramesPlayedAtUs); |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | // CHECK_EQ(numFramesPlayed & (1 << 31), 0); // can't be negative until 12.4 hrs, test |
| 1709 | // TODO: remove the (int32_t) casting below as it may overflow at 12.4 hours. |
| 1710 | int64_t durationUs = (int64_t)((int32_t)numFramesPlayed * 1000000LL / mSampleRateHz) |
Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1711 | + nowUs - numFramesPlayedAtUs; |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1712 | if (durationUs < 0) { |
| 1713 | // Occurs when numFramesPlayed position is very small and the following: |
| 1714 | // (1) In case 1, the time nowUs is computed before getTimestamp() is called and |
Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1715 | // numFramesPlayedAtUs is greater than nowUs by time more than numFramesPlayed. |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1716 | // (2) In case 3, using getPosition and adding mAudioSink->latency() to |
Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1717 | // numFramesPlayedAtUs, by a time amount greater than numFramesPlayed. |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1718 | // |
| 1719 | // Both of these are transitory conditions. |
| 1720 | ALOGV("getPlayedOutDurationUs: negative duration %lld set to zero", (long long)durationUs); |
| 1721 | durationUs = 0; |
| 1722 | } |
| 1723 | ALOGV("getPlayedOutDurationUs(%lld) nowUs(%lld) frames(%u) framesAt(%lld)", |
Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1724 | (long long)durationUs, (long long)nowUs, |
| 1725 | numFramesPlayed, (long long)numFramesPlayedAtUs); |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1726 | return durationUs; |
| 1727 | } |
| 1728 | |
Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1729 | status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const |
| 1730 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1731 | Mutex::Autolock lock(mLock); |
Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1732 | if (mTrack == 0) return NO_INIT; |
Andy Hung | 2f6e73d | 2016-04-08 12:12:58 -0700 | [diff] [blame] | 1733 | ExtendedTimestamp ets; |
| 1734 | status_t status = mTrack->getTimestamp(&ets); |
| 1735 | if (status == OK || status == WOULD_BLOCK) { |
| 1736 | *frameswritten = (uint32_t)ets.mPosition[ExtendedTimestamp::LOCATION_CLIENT]; |
| 1737 | } |
| 1738 | return status; |
Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1739 | } |
| 1740 | |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1741 | status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs) |
| 1742 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1743 | Mutex::Autolock lock(mLock); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1744 | if (mTrack == 0) return NO_INIT; |
| 1745 | return mTrack->setParameters(keyValuePairs); |
| 1746 | } |
| 1747 | |
| 1748 | String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys) |
| 1749 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1750 | Mutex::Autolock lock(mLock); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1751 | if (mTrack == 0) return String8::empty(); |
| 1752 | return mTrack->getParameters(keys); |
| 1753 | } |
| 1754 | |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1755 | void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1756 | Mutex::Autolock lock(mLock); |
Jean-Michel Trivi | 2650e96 | 2015-07-22 18:14:02 -0700 | [diff] [blame] | 1757 | if (attributes == NULL) { |
| 1758 | free(mAttributes); |
| 1759 | mAttributes = NULL; |
| 1760 | } else { |
| 1761 | if (mAttributes == NULL) { |
| 1762 | mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t)); |
| 1763 | } |
| 1764 | memcpy(mAttributes, attributes, sizeof(audio_attributes_t)); |
Eric Laurent | 4356269 | 2015-07-15 16:49:07 -0700 | [diff] [blame] | 1765 | mStreamType = audio_attributes_to_stream_type(attributes); |
| 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | void MediaPlayerService::AudioOutput::setAudioStreamType(audio_stream_type_t streamType) |
| 1770 | { |
Jean-Michel Trivi | 2650e96 | 2015-07-22 18:14:02 -0700 | [diff] [blame] | 1771 | Mutex::Autolock lock(mLock); |
Eric Laurent | 4356269 | 2015-07-15 16:49:07 -0700 | [diff] [blame] | 1772 | // do not allow direct stream type modification if attributes have been set |
| 1773 | if (mAttributes == NULL) { |
| 1774 | mStreamType = streamType; |
| 1775 | } |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1776 | } |
| 1777 | |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1778 | void MediaPlayerService::AudioOutput::deleteRecycledTrack_l() |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1779 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1780 | ALOGV("deleteRecycledTrack_l"); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1781 | if (mRecycledTrack != 0) { |
| 1782 | |
| 1783 | if (mCallbackData != NULL) { |
| 1784 | mCallbackData->setOutput(NULL); |
| 1785 | mCallbackData->endTrackSwitch(); |
| 1786 | } |
| 1787 | |
| 1788 | if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) { |
Andy Hung | e13f8a6 | 2016-03-30 14:20:42 -0700 | [diff] [blame] | 1789 | int32_t msec = 0; |
| 1790 | if (!mRecycledTrack->stopped()) { // check if active |
| 1791 | (void)mRecycledTrack->pendingDuration(&msec); |
| 1792 | } |
| 1793 | mRecycledTrack->stop(); // ensure full data drain |
| 1794 | ALOGD("deleting recycled track, waiting for data drain (%d msec)", msec); |
| 1795 | if (msec > 0) { |
| 1796 | static const int32_t WAIT_LIMIT_MS = 3000; |
| 1797 | if (msec > WAIT_LIMIT_MS) { |
| 1798 | msec = WAIT_LIMIT_MS; |
| 1799 | } |
| 1800 | usleep(msec * 1000LL); |
| 1801 | } |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1802 | } |
| 1803 | // An offloaded track isn't flushed because the STREAM_END is reported |
| 1804 | // slightly prematurely to allow time for the gapless track switch |
| 1805 | // but this means that if we decide not to recycle the track there |
| 1806 | // could be a small amount of residual data still playing. We leave |
| 1807 | // AudioFlinger to drain the track. |
| 1808 | |
| 1809 | mRecycledTrack.clear(); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1810 | close_l(); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1811 | delete mCallbackData; |
| 1812 | mCallbackData = NULL; |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1813 | } |
| 1814 | } |
| 1815 | |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1816 | void MediaPlayerService::AudioOutput::close_l() |
| 1817 | { |
| 1818 | mTrack.clear(); |
| 1819 | } |
| 1820 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1821 | status_t MediaPlayerService::AudioOutput::open( |
Jean-Michel Trivi | 786618f | 2012-03-02 14:54:07 -0800 | [diff] [blame] | 1822 | uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask, |
| 1823 | audio_format_t format, int bufferCount, |
Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1824 | AudioCallback cb, void *cookie, |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1825 | audio_output_flags_t flags, |
Ronghua Wu | faeb0f2 | 2015-05-21 12:20:21 -0700 | [diff] [blame] | 1826 | const audio_offload_info_t *offloadInfo, |
Andy Hung | 179652e | 2015-05-31 22:49:46 -0700 | [diff] [blame] | 1827 | bool doNotReconnect, |
| 1828 | uint32_t suggestedFrameCount) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1829 | { |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1830 | ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask, |
| 1831 | format, bufferCount, mSessionId, flags); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1832 | |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1833 | // offloading is only supported in callback mode for now. |
| 1834 | // offloadInfo must be present if offload flag is set |
| 1835 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) && |
| 1836 | ((cb == NULL) || (offloadInfo == NULL))) { |
| 1837 | return BAD_VALUE; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1838 | } |
| 1839 | |
Andy Hung | 179652e | 2015-05-31 22:49:46 -0700 | [diff] [blame] | 1840 | // compute frame count for the AudioTrack internal buffer |
| 1841 | size_t frameCount; |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1842 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1843 | frameCount = 0; // AudioTrack will get frame count from AudioFlinger |
| 1844 | } else { |
Andy Hung | 179652e | 2015-05-31 22:49:46 -0700 | [diff] [blame] | 1845 | // try to estimate the buffer processing fetch size from AudioFlinger. |
| 1846 | // framesPerBuffer is approximate and generally correct, except when it's not :-). |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1847 | uint32_t afSampleRate; |
| 1848 | size_t afFrameCount; |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1849 | if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) { |
| 1850 | return NO_INIT; |
| 1851 | } |
| 1852 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) { |
| 1853 | return NO_INIT; |
| 1854 | } |
Andy Hung | 179652e | 2015-05-31 22:49:46 -0700 | [diff] [blame] | 1855 | const size_t framesPerBuffer = |
| 1856 | (unsigned long long)sampleRate * afFrameCount / afSampleRate; |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1857 | |
Andy Hung | 179652e | 2015-05-31 22:49:46 -0700 | [diff] [blame] | 1858 | if (bufferCount == 0) { |
| 1859 | // use suggestedFrameCount |
| 1860 | bufferCount = (suggestedFrameCount + framesPerBuffer - 1) / framesPerBuffer; |
| 1861 | } |
| 1862 | // Check argument bufferCount against the mininum buffer count |
| 1863 | if (bufferCount != 0 && bufferCount < mMinBufferCount) { |
| 1864 | ALOGV("bufferCount (%d) increased to %d", bufferCount, mMinBufferCount); |
| 1865 | bufferCount = mMinBufferCount; |
| 1866 | } |
| 1867 | // if frameCount is 0, then AudioTrack will get frame count from AudioFlinger |
| 1868 | // which will be the minimum size permitted. |
| 1869 | frameCount = bufferCount * framesPerBuffer; |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1870 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1871 | |
Jean-Michel Trivi | 786618f | 2012-03-02 14:54:07 -0800 | [diff] [blame] | 1872 | if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) { |
Glenn Kasten | ab334fd | 2012-03-14 12:56:06 -0700 | [diff] [blame] | 1873 | channelMask = audio_channel_out_mask_from_count(channelCount); |
Jean-Michel Trivi | 786618f | 2012-03-02 14:54:07 -0800 | [diff] [blame] | 1874 | if (0 == channelMask) { |
| 1875 | ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount); |
| 1876 | return NO_INIT; |
| 1877 | } |
| 1878 | } |
Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1879 | |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1880 | Mutex::Autolock lock(mLock); |
Andy Hung | 179652e | 2015-05-31 22:49:46 -0700 | [diff] [blame] | 1881 | mCallback = cb; |
| 1882 | mCallbackCookie = cookie; |
| 1883 | |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1884 | // Check whether we can recycle the track |
| 1885 | bool reuse = false; |
| 1886 | bool bothOffloaded = false; |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1887 | |
Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 1888 | if (mRecycledTrack != 0) { |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1889 | // check whether we are switching between two offloaded tracks |
| 1890 | bothOffloaded = (flags & mRecycledTrack->getFlags() |
| 1891 | & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0; |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1892 | |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1893 | // check if the existing track can be reused as-is, or if a new track needs to be created. |
| 1894 | reuse = true; |
| 1895 | |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1896 | if ((mCallbackData == NULL && mCallback != NULL) || |
| 1897 | (mCallbackData != NULL && mCallback == NULL)) { |
| 1898 | // recycled track uses callbacks but the caller wants to use writes, or vice versa |
| 1899 | ALOGV("can't chain callback and write"); |
| 1900 | reuse = false; |
| 1901 | } else if ((mRecycledTrack->getSampleRate() != sampleRate) || |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1902 | (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) { |
| 1903 | ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch", |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1904 | mRecycledTrack->getSampleRate(), sampleRate, |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1905 | mRecycledTrack->channelCount(), channelCount); |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1906 | reuse = false; |
| 1907 | } else if (flags != mFlags) { |
| 1908 | ALOGV("output flags differ %08x/%08x", flags, mFlags); |
| 1909 | reuse = false; |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1910 | } else if (mRecycledTrack->format() != format) { |
| 1911 | reuse = false; |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1912 | } |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1913 | } else { |
| 1914 | ALOGV("no track available to recycle"); |
| 1915 | } |
| 1916 | |
| 1917 | ALOGV_IF(bothOffloaded, "both tracks offloaded"); |
| 1918 | |
| 1919 | // If we can't recycle and both tracks are offloaded |
| 1920 | // we must close the previous output before opening a new one |
| 1921 | if (bothOffloaded && !reuse) { |
| 1922 | ALOGV("both offloaded and not recycling"); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1923 | deleteRecycledTrack_l(); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1924 | } |
| 1925 | |
| 1926 | sp<AudioTrack> t; |
| 1927 | CallbackData *newcbd = NULL; |
| 1928 | |
| 1929 | // We don't attempt to create a new track if we are recycling an |
| 1930 | // offloaded track. But, if we are recycling a non-offloaded or we |
| 1931 | // are switching where one is offloaded and one isn't then we create |
| 1932 | // the new track in advance so that we can read additional stream info |
| 1933 | |
| 1934 | if (!(reuse && bothOffloaded)) { |
| 1935 | ALOGV("creating new AudioTrack"); |
| 1936 | |
| 1937 | if (mCallback != NULL) { |
| 1938 | newcbd = new CallbackData(this); |
| 1939 | t = new AudioTrack( |
| 1940 | mStreamType, |
| 1941 | sampleRate, |
| 1942 | format, |
| 1943 | channelMask, |
| 1944 | frameCount, |
| 1945 | flags, |
| 1946 | CallbackWrapper, |
| 1947 | newcbd, |
| 1948 | 0, // notification frames |
| 1949 | mSessionId, |
| 1950 | AudioTrack::TRANSFER_CALLBACK, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1951 | offloadInfo, |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 1952 | mUid, |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1953 | mPid, |
Ronghua Wu | faeb0f2 | 2015-05-21 12:20:21 -0700 | [diff] [blame] | 1954 | mAttributes, |
| 1955 | doNotReconnect); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1956 | } else { |
Andy Hung | ff874dc | 2016-04-11 16:49:09 -0700 | [diff] [blame] | 1957 | // TODO: Due to buffer memory concerns, we use a max target playback speed |
| 1958 | // based on mPlaybackRate at the time of open (instead of kMaxRequiredSpeed), |
| 1959 | // also clamping the target speed to 1.0 <= targetSpeed <= kMaxRequiredSpeed. |
| 1960 | const float targetSpeed = |
| 1961 | std::min(std::max(mPlaybackRate.mSpeed, 1.0f), kMaxRequiredSpeed); |
| 1962 | ALOGW_IF(targetSpeed != mPlaybackRate.mSpeed, |
| 1963 | "track target speed:%f clamped from playback speed:%f", |
| 1964 | targetSpeed, mPlaybackRate.mSpeed); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1965 | t = new AudioTrack( |
| 1966 | mStreamType, |
| 1967 | sampleRate, |
| 1968 | format, |
| 1969 | channelMask, |
| 1970 | frameCount, |
| 1971 | flags, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1972 | NULL, // callback |
| 1973 | NULL, // user data |
| 1974 | 0, // notification frames |
| 1975 | mSessionId, |
| 1976 | AudioTrack::TRANSFER_DEFAULT, |
| 1977 | NULL, // offload info |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 1978 | mUid, |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1979 | mPid, |
Ronghua Wu | faeb0f2 | 2015-05-21 12:20:21 -0700 | [diff] [blame] | 1980 | mAttributes, |
Andy Hung | ff874dc | 2016-04-11 16:49:09 -0700 | [diff] [blame] | 1981 | doNotReconnect, |
| 1982 | targetSpeed); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | if ((t == 0) || (t->initCheck() != NO_ERROR)) { |
| 1986 | ALOGE("Unable to create audio track"); |
| 1987 | delete newcbd; |
Glenn Kasten | 3e98ecd | 2015-05-18 13:13:24 -0700 | [diff] [blame] | 1988 | // t goes out of scope, so reference count drops to zero |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1989 | return NO_INIT; |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1990 | } else { |
| 1991 | // successful AudioTrack initialization implies a legacy stream type was generated |
| 1992 | // from the audio attributes |
| 1993 | mStreamType = t->streamType(); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1994 | } |
| 1995 | } |
| 1996 | |
| 1997 | if (reuse) { |
| 1998 | CHECK(mRecycledTrack != NULL); |
| 1999 | |
| 2000 | if (!bothOffloaded) { |
| 2001 | if (mRecycledTrack->frameCount() != t->frameCount()) { |
Andy Hung | 833b475 | 2016-04-04 17:15:48 -0700 | [diff] [blame] | 2002 | ALOGV("framecount differs: %zu/%zu frames", |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2003 | mRecycledTrack->frameCount(), t->frameCount()); |
| 2004 | reuse = false; |
| 2005 | } |
| 2006 | } |
| 2007 | |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 2008 | if (reuse) { |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2009 | ALOGV("chaining to next output and recycling track"); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2010 | close_l(); |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 2011 | mTrack = mRecycledTrack; |
Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2012 | mRecycledTrack.clear(); |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 2013 | if (mCallbackData != NULL) { |
| 2014 | mCallbackData->setOutput(this); |
| 2015 | } |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 2016 | delete newcbd; |
Wei Jia | e0bbac9 | 2016-07-18 16:04:53 -0700 | [diff] [blame] | 2017 | return updateTrack(); |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 2018 | } |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 2019 | } |
| 2020 | |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2021 | // we're not going to reuse the track, unblock and flush it |
| 2022 | // this was done earlier if both tracks are offloaded |
| 2023 | if (!bothOffloaded) { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2024 | deleteRecycledTrack_l(); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL))); |
| 2028 | |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 2029 | mCallbackData = newcbd; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2030 | ALOGV("setVolume"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2031 | t->setVolume(mLeftVolume, mRightVolume); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2032 | |
Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2033 | // Restore VolumeShapers for the MediaPlayer in case the track was recreated |
| 2034 | // due to an output sink error (e.g. offload to non-offload switch). |
| 2035 | mVolumeHandler->forall([&t](const VolumeShaper &shaper) -> VolumeShaper::Status { |
| 2036 | sp<VolumeShaper::Operation> operationToEnd = |
| 2037 | new VolumeShaper::Operation(shaper.mOperation); |
| 2038 | // TODO: Ideally we would restore to the exact xOffset position |
| 2039 | // as returned by getVolumeShaperState(), but we don't have that |
| 2040 | // information when restoring at the client unless we periodically poll |
| 2041 | // the server or create shared memory state. |
| 2042 | // |
| 2043 | // For now, we simply advance to the end of the VolumeShaper effect |
| 2044 | // if it has been started. |
| 2045 | if (shaper.isStarted()) { |
Andy Hung | f370264 | 2017-05-05 17:33:32 -0700 | [diff] [blame] | 2046 | operationToEnd->setNormalizedTime(1.f); |
Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2047 | } |
| 2048 | return t->applyVolumeShaper(shaper.mConfiguration, operationToEnd); |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 2049 | }); |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 2050 | |
Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 2051 | mSampleRateHz = sampleRate; |
Preetam Singh Ranawat | 2e17eef | 2015-08-12 12:11:46 -0700 | [diff] [blame] | 2052 | mFlags = flags; |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2053 | mMsecsPerFrame = 1E3f / (mPlaybackRate.mSpeed * sampleRate); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2054 | mFrameSize = t->frameSize(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2055 | mTrack = t; |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2056 | |
Wei Jia | e0bbac9 | 2016-07-18 16:04:53 -0700 | [diff] [blame] | 2057 | return updateTrack(); |
| 2058 | } |
| 2059 | |
| 2060 | status_t MediaPlayerService::AudioOutput::updateTrack() { |
| 2061 | if (mTrack == NULL) { |
| 2062 | return NO_ERROR; |
| 2063 | } |
| 2064 | |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2065 | status_t res = NO_ERROR; |
Wei Jia | 0162d00 | 2015-06-09 11:59:33 -0700 | [diff] [blame] | 2066 | // Note some output devices may give us a direct track even though we don't specify it. |
| 2067 | // Example: Line application b/17459982. |
Wei Jia | e0bbac9 | 2016-07-18 16:04:53 -0700 | [diff] [blame] | 2068 | if ((mTrack->getFlags() |
| 2069 | & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT)) == 0) { |
| 2070 | res = mTrack->setPlaybackRate(mPlaybackRate); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2071 | if (res == NO_ERROR) { |
Wei Jia | e0bbac9 | 2016-07-18 16:04:53 -0700 | [diff] [blame] | 2072 | mTrack->setAuxEffectSendLevel(mSendLevel); |
| 2073 | res = mTrack->attachAuxEffect(mAuxEffectId); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2074 | } |
Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 2075 | } |
Wei Jia | e0bbac9 | 2016-07-18 16:04:53 -0700 | [diff] [blame] | 2076 | ALOGV("updateTrack() DONE status %d", res); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2077 | return res; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2078 | } |
| 2079 | |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2080 | status_t MediaPlayerService::AudioOutput::start() |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2081 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2082 | ALOGV("start"); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2083 | Mutex::Autolock lock(mLock); |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2084 | if (mCallbackData != NULL) { |
| 2085 | mCallbackData->endTrackSwitch(); |
| 2086 | } |
Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2087 | if (mTrack != 0) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2088 | mTrack->setVolume(mLeftVolume, mRightVolume); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2089 | mTrack->setAuxEffectSendLevel(mSendLevel); |
Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2090 | status_t status = mTrack->start(); |
| 2091 | if (status == NO_ERROR) { |
| 2092 | mVolumeHandler->setStarted(); |
| 2093 | } |
| 2094 | return status; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2095 | } |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2096 | return NO_INIT; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2097 | } |
| 2098 | |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2099 | void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2100 | Mutex::Autolock lock(mLock); |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2101 | mNextOutput = nextOutput; |
| 2102 | } |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 2103 | |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2104 | void MediaPlayerService::AudioOutput::switchToNextOutput() { |
| 2105 | ALOGV("switchToNextOutput"); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2106 | |
| 2107 | // Try to acquire the callback lock before moving track (without incurring deadlock). |
| 2108 | const unsigned kMaxSwitchTries = 100; |
| 2109 | Mutex::Autolock lock(mLock); |
| 2110 | for (unsigned tries = 0;;) { |
| 2111 | if (mTrack == 0) { |
| 2112 | return; |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2113 | } |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2114 | if (mNextOutput != NULL && mNextOutput != this) { |
| 2115 | if (mCallbackData != NULL) { |
| 2116 | // two alternative approaches |
| 2117 | #if 1 |
| 2118 | CallbackData *callbackData = mCallbackData; |
| 2119 | mLock.unlock(); |
| 2120 | // proper acquisition sequence |
| 2121 | callbackData->lock(); |
| 2122 | mLock.lock(); |
| 2123 | // Caution: it is unlikely that someone deleted our callback or changed our target |
| 2124 | if (callbackData != mCallbackData || mNextOutput == NULL || mNextOutput == this) { |
| 2125 | // fatal if we are starved out. |
| 2126 | LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries, |
| 2127 | "switchToNextOutput() cannot obtain correct lock sequence"); |
| 2128 | callbackData->unlock(); |
| 2129 | continue; |
| 2130 | } |
| 2131 | callbackData->mSwitching = true; // begin track switch |
Wei Jia | adee56a | 2016-08-05 15:40:34 -0700 | [diff] [blame] | 2132 | callbackData->setOutput(NULL); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2133 | #else |
| 2134 | // tryBeginTrackSwitch() returns false if the callback has the lock. |
| 2135 | if (!mCallbackData->tryBeginTrackSwitch()) { |
| 2136 | // fatal if we are starved out. |
| 2137 | LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries, |
| 2138 | "switchToNextOutput() cannot obtain callback lock"); |
| 2139 | mLock.unlock(); |
| 2140 | usleep(5 * 1000 /* usec */); // allow callback to use AudioOutput |
| 2141 | mLock.lock(); |
| 2142 | continue; |
| 2143 | } |
| 2144 | #endif |
| 2145 | } |
| 2146 | |
| 2147 | Mutex::Autolock nextLock(mNextOutput->mLock); |
| 2148 | |
| 2149 | // If the next output track is not NULL, then it has been |
| 2150 | // opened already for playback. |
| 2151 | // This is possible even without the next player being started, |
| 2152 | // for example, the next player could be prepared and seeked. |
| 2153 | // |
| 2154 | // Presuming it isn't advisable to force the track over. |
| 2155 | if (mNextOutput->mTrack == NULL) { |
| 2156 | ALOGD("Recycling track for gapless playback"); |
| 2157 | delete mNextOutput->mCallbackData; |
| 2158 | mNextOutput->mCallbackData = mCallbackData; |
| 2159 | mNextOutput->mRecycledTrack = mTrack; |
| 2160 | mNextOutput->mSampleRateHz = mSampleRateHz; |
| 2161 | mNextOutput->mMsecsPerFrame = mMsecsPerFrame; |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2162 | mNextOutput->mFlags = mFlags; |
| 2163 | mNextOutput->mFrameSize = mFrameSize; |
| 2164 | close_l(); |
| 2165 | mCallbackData = NULL; // destruction handled by mNextOutput |
| 2166 | } else { |
| 2167 | ALOGW("Ignoring gapless playback because next player has already started"); |
| 2168 | // remove track in case resource needed for future players. |
| 2169 | if (mCallbackData != NULL) { |
| 2170 | mCallbackData->endTrackSwitch(); // release lock for callbacks before close. |
| 2171 | } |
| 2172 | close_l(); |
| 2173 | } |
| 2174 | } |
| 2175 | break; |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2176 | } |
| 2177 | } |
| 2178 | |
Wei Jia | 7d3f4df | 2015-03-03 15:28:00 -0800 | [diff] [blame] | 2179 | 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] | 2180 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2181 | Mutex::Autolock lock(mLock); |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2182 | 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] | 2183 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2184 | //ALOGV("write(%p, %u)", buffer, size); |
Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2185 | if (mTrack != 0) { |
Andy Hung | 2f6e73d | 2016-04-08 12:12:58 -0700 | [diff] [blame] | 2186 | return mTrack->write(buffer, size, blocking); |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 2187 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2188 | return NO_INIT; |
| 2189 | } |
| 2190 | |
| 2191 | void MediaPlayerService::AudioOutput::stop() |
| 2192 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2193 | ALOGV("stop"); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2194 | Mutex::Autolock lock(mLock); |
Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2195 | if (mTrack != 0) mTrack->stop(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | void MediaPlayerService::AudioOutput::flush() |
| 2199 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2200 | ALOGV("flush"); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2201 | Mutex::Autolock lock(mLock); |
Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2202 | if (mTrack != 0) mTrack->flush(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2203 | } |
| 2204 | |
| 2205 | void MediaPlayerService::AudioOutput::pause() |
| 2206 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2207 | ALOGV("pause"); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2208 | Mutex::Autolock lock(mLock); |
Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2209 | if (mTrack != 0) mTrack->pause(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | void MediaPlayerService::AudioOutput::close() |
| 2213 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2214 | ALOGV("close"); |
Erik Wolsheimer | 7845a1f | 2015-10-30 12:07:52 -0700 | [diff] [blame] | 2215 | sp<AudioTrack> track; |
| 2216 | { |
| 2217 | Mutex::Autolock lock(mLock); |
| 2218 | track = mTrack; |
| 2219 | close_l(); // clears mTrack |
| 2220 | } |
| 2221 | // destruction of the track occurs outside of mutex. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2222 | } |
| 2223 | |
| 2224 | void MediaPlayerService::AudioOutput::setVolume(float left, float right) |
| 2225 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2226 | ALOGV("setVolume(%f, %f)", left, right); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2227 | Mutex::Autolock lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2228 | mLeftVolume = left; |
| 2229 | mRightVolume = right; |
Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2230 | if (mTrack != 0) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2231 | mTrack->setVolume(left, right); |
| 2232 | } |
| 2233 | } |
| 2234 | |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2235 | status_t MediaPlayerService::AudioOutput::setPlaybackRate(const AudioPlaybackRate &rate) |
Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 2236 | { |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2237 | ALOGV("setPlaybackRate(%f %f %d %d)", |
| 2238 | rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2239 | Mutex::Autolock lock(mLock); |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2240 | if (mTrack == 0) { |
| 2241 | // remember rate so that we can set it when the track is opened |
| 2242 | mPlaybackRate = rate; |
| 2243 | return OK; |
Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 2244 | } |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2245 | status_t res = mTrack->setPlaybackRate(rate); |
| 2246 | if (res != NO_ERROR) { |
| 2247 | return res; |
| 2248 | } |
| 2249 | // rate.mSpeed is always greater than 0 if setPlaybackRate succeeded |
| 2250 | CHECK_GT(rate.mSpeed, 0.f); |
| 2251 | mPlaybackRate = rate; |
Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 2252 | if (mSampleRateHz != 0) { |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2253 | mMsecsPerFrame = 1E3f / (rate.mSpeed * mSampleRateHz); |
Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 2254 | } |
| 2255 | return res; |
| 2256 | } |
| 2257 | |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2258 | status_t MediaPlayerService::AudioOutput::getPlaybackRate(AudioPlaybackRate *rate) |
| 2259 | { |
| 2260 | ALOGV("setPlaybackRate"); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2261 | Mutex::Autolock lock(mLock); |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2262 | if (mTrack == 0) { |
| 2263 | return NO_INIT; |
| 2264 | } |
| 2265 | *rate = mTrack->getPlaybackRate(); |
| 2266 | return NO_ERROR; |
| 2267 | } |
| 2268 | |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2269 | status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level) |
| 2270 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2271 | ALOGV("setAuxEffectSendLevel(%f)", level); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2272 | Mutex::Autolock lock(mLock); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2273 | mSendLevel = level; |
Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2274 | if (mTrack != 0) { |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2275 | return mTrack->setAuxEffectSendLevel(level); |
| 2276 | } |
| 2277 | return NO_ERROR; |
| 2278 | } |
| 2279 | |
| 2280 | status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId) |
| 2281 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2282 | ALOGV("attachAuxEffect(%d)", effectId); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2283 | Mutex::Autolock lock(mLock); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2284 | mAuxEffectId = effectId; |
Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2285 | if (mTrack != 0) { |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2286 | return mTrack->attachAuxEffect(effectId); |
| 2287 | } |
| 2288 | return NO_ERROR; |
| 2289 | } |
| 2290 | |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 2291 | VolumeShaper::Status MediaPlayerService::AudioOutput::applyVolumeShaper( |
| 2292 | const sp<VolumeShaper::Configuration>& configuration, |
| 2293 | const sp<VolumeShaper::Operation>& operation) |
| 2294 | { |
| 2295 | Mutex::Autolock lock(mLock); |
| 2296 | ALOGV("AudioOutput::applyVolumeShaper"); |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 2297 | |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 2298 | mVolumeHandler->setIdIfNecessary(configuration); |
Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2299 | |
| 2300 | VolumeShaper::Status status; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 2301 | if (mTrack != 0) { |
Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2302 | status = mTrack->applyVolumeShaper(configuration, operation); |
| 2303 | if (status >= 0) { |
| 2304 | (void)mVolumeHandler->applyVolumeShaper(configuration, operation); |
Andy Hung | f370264 | 2017-05-05 17:33:32 -0700 | [diff] [blame] | 2305 | if (mTrack->isPlaying()) { // match local AudioTrack to properly restore. |
| 2306 | mVolumeHandler->setStarted(); |
| 2307 | } |
Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2308 | } |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 2309 | } else { |
Andy Hung | c01eddb | 2017-07-21 16:42:41 -0700 | [diff] [blame] | 2310 | // VolumeShapers are not affected when a track moves between players for |
| 2311 | // gapless playback (setNextMediaPlayer). |
| 2312 | // We forward VolumeShaper operations that do not change configuration |
| 2313 | // to the new player so that unducking may occur as expected. |
| 2314 | // Unducking is an idempotent operation, same if applied back-to-back. |
| 2315 | if (configuration->getType() == VolumeShaper::Configuration::TYPE_ID |
| 2316 | && mNextOutput != nullptr) { |
| 2317 | ALOGV("applyVolumeShaper: Attempting to forward missed operation: %s %s", |
| 2318 | configuration->toString().c_str(), operation->toString().c_str()); |
| 2319 | Mutex::Autolock nextLock(mNextOutput->mLock); |
| 2320 | |
| 2321 | // recycled track should be forwarded from this AudioSink by switchToNextOutput |
| 2322 | sp<AudioTrack> track = mNextOutput->mRecycledTrack; |
| 2323 | if (track != nullptr) { |
| 2324 | ALOGD("Forward VolumeShaper operation to recycled track %p", track.get()); |
| 2325 | (void)track->applyVolumeShaper(configuration, operation); |
| 2326 | } else { |
| 2327 | // There is a small chance that the unduck occurs after the next |
| 2328 | // player has already started, but before it is registered to receive |
| 2329 | // the unduck command. |
| 2330 | track = mNextOutput->mTrack; |
| 2331 | if (track != nullptr) { |
| 2332 | ALOGD("Forward VolumeShaper operation to track %p", track.get()); |
| 2333 | (void)track->applyVolumeShaper(configuration, operation); |
| 2334 | } |
| 2335 | } |
| 2336 | } |
Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2337 | status = mVolumeHandler->applyVolumeShaper(configuration, operation); |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 2338 | } |
Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2339 | return status; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | sp<VolumeShaper::State> MediaPlayerService::AudioOutput::getVolumeShaperState(int id) |
| 2343 | { |
| 2344 | Mutex::Autolock lock(mLock); |
| 2345 | if (mTrack != 0) { |
| 2346 | return mTrack->getVolumeShaperState(id); |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 2347 | } else { |
| 2348 | return mVolumeHandler->getVolumeShaperState(id); |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 2349 | } |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 2350 | } |
| 2351 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2352 | // static |
| 2353 | void MediaPlayerService::AudioOutput::CallbackWrapper( |
Glenn Kasten | d217a8c | 2011-06-01 15:20:35 -0700 | [diff] [blame] | 2354 | int event, void *cookie, void *info) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2355 | //ALOGV("callbackwrapper"); |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2356 | CallbackData *data = (CallbackData*)cookie; |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2357 | // 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] | 2358 | data->lock(); |
| 2359 | AudioOutput *me = data->getOutput(); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2360 | AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info; |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2361 | if (me == NULL) { |
| 2362 | // no output set, likely because the track was scheduled to be reused |
| 2363 | // by another player, but the format turned out to be incompatible. |
| 2364 | data->unlock(); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2365 | if (buffer != NULL) { |
| 2366 | buffer->size = 0; |
| 2367 | } |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2368 | return; |
| 2369 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2370 | |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2371 | switch(event) { |
| 2372 | case AudioTrack::EVENT_MORE_DATA: { |
| 2373 | size_t actualSize = (*me->mCallback)( |
| 2374 | me, buffer->raw, buffer->size, me->mCallbackCookie, |
| 2375 | CB_EVENT_FILL_BUFFER); |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 2376 | |
Andy Hung | 719b46b | 2015-05-31 22:18:25 -0700 | [diff] [blame] | 2377 | // Log when no data is returned from the callback. |
| 2378 | // (1) We may have no data (especially with network streaming sources). |
| 2379 | // (2) We may have reached the EOS and the audio track is not stopped yet. |
| 2380 | // Note that AwesomePlayer/AudioPlayer will only return zero size when it reaches the EOS. |
| 2381 | // NuPlayerRenderer will return zero when it doesn't have data (it doesn't block to fill). |
| 2382 | // |
| 2383 | // This is a benign busy-wait, with the next data request generated 10 ms or more later; |
| 2384 | // 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] | 2385 | |
Andy Hung | 719b46b | 2015-05-31 22:18:25 -0700 | [diff] [blame] | 2386 | ALOGV_IF(actualSize == 0 && buffer->size > 0, "callbackwrapper: empty buffer returned"); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2387 | |
| 2388 | buffer->size = actualSize; |
| 2389 | } break; |
| 2390 | |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2391 | case AudioTrack::EVENT_STREAM_END: |
Andy Hung | 719b46b | 2015-05-31 22:18:25 -0700 | [diff] [blame] | 2392 | // currently only occurs for offloaded callbacks |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2393 | ALOGV("callbackwrapper: deliver EVENT_STREAM_END"); |
| 2394 | (*me->mCallback)(me, NULL /* buffer */, 0 /* size */, |
| 2395 | me->mCallbackCookie, CB_EVENT_STREAM_END); |
| 2396 | break; |
| 2397 | |
| 2398 | case AudioTrack::EVENT_NEW_IAUDIOTRACK : |
| 2399 | ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN"); |
| 2400 | (*me->mCallback)(me, NULL /* buffer */, 0 /* size */, |
| 2401 | me->mCallbackCookie, CB_EVENT_TEAR_DOWN); |
| 2402 | break; |
| 2403 | |
Glenn Kasten | 421743b | 2015-06-01 08:18:08 -0700 | [diff] [blame] | 2404 | case AudioTrack::EVENT_UNDERRUN: |
Andy Hung | 719b46b | 2015-05-31 22:18:25 -0700 | [diff] [blame] | 2405 | // This occurs when there is no data available, typically |
Glenn Kasten | 421743b | 2015-06-01 08:18:08 -0700 | [diff] [blame] | 2406 | // when there is a failure to supply data to the AudioTrack. It can also |
| 2407 | // occur in non-offloaded mode when the audio device comes out of standby. |
| 2408 | // |
Andy Hung | 719b46b | 2015-05-31 22:18:25 -0700 | [diff] [blame] | 2409 | // If an AudioTrack underruns it outputs silence. Since this happens suddenly |
| 2410 | // it may sound like an audible pop or glitch. |
| 2411 | // |
| 2412 | // The underrun event is sent once per track underrun; the condition is reset |
| 2413 | // when more data is sent to the AudioTrack. |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2414 | ALOGD("callbackwrapper: EVENT_UNDERRUN (discarded)"); |
Glenn Kasten | 421743b | 2015-06-01 08:18:08 -0700 | [diff] [blame] | 2415 | break; |
| 2416 | |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2417 | default: |
| 2418 | ALOGE("received unknown event type: %d inside CallbackWrapper !", event); |
Andreas Huber | 51c1e0e | 2011-04-04 11:43:40 -0700 | [diff] [blame] | 2419 | } |
| 2420 | |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2421 | data->unlock(); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2422 | } |
| 2423 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2424 | audio_session_t MediaPlayerService::AudioOutput::getSessionId() const |
Eric Laurent | 8c563ed | 2010-10-07 18:23:03 -0700 | [diff] [blame] | 2425 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2426 | Mutex::Autolock lock(mLock); |
Eric Laurent | 8c563ed | 2010-10-07 18:23:03 -0700 | [diff] [blame] | 2427 | return mSessionId; |
| 2428 | } |
| 2429 | |
Eric Laurent | 6f59db1 | 2013-07-26 17:16:50 -0700 | [diff] [blame] | 2430 | uint32_t MediaPlayerService::AudioOutput::getSampleRate() const |
| 2431 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2432 | Mutex::Autolock lock(mLock); |
Eric Laurent | 6f59db1 | 2013-07-26 17:16:50 -0700 | [diff] [blame] | 2433 | if (mTrack == 0) return 0; |
| 2434 | return mTrack->getSampleRate(); |
| 2435 | } |
| 2436 | |
Andy Hung | f2c87b3 | 2016-04-07 19:49:29 -0700 | [diff] [blame] | 2437 | int64_t MediaPlayerService::AudioOutput::getBufferDurationInUs() const |
| 2438 | { |
| 2439 | Mutex::Autolock lock(mLock); |
| 2440 | if (mTrack == 0) { |
| 2441 | return 0; |
| 2442 | } |
| 2443 | int64_t duration; |
| 2444 | if (mTrack->getBufferDurationInUs(&duration) != OK) { |
| 2445 | return 0; |
| 2446 | } |
| 2447 | return duration; |
| 2448 | } |
| 2449 | |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 2450 | //////////////////////////////////////////////////////////////////////////////// |
| 2451 | |
| 2452 | struct CallbackThread : public Thread { |
| 2453 | CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink, |
| 2454 | MediaPlayerBase::AudioSink::AudioCallback cb, |
| 2455 | void *cookie); |
| 2456 | |
| 2457 | protected: |
| 2458 | virtual ~CallbackThread(); |
| 2459 | |
| 2460 | virtual bool threadLoop(); |
| 2461 | |
| 2462 | private: |
| 2463 | wp<MediaPlayerBase::AudioSink> mSink; |
| 2464 | MediaPlayerBase::AudioSink::AudioCallback mCallback; |
| 2465 | void *mCookie; |
| 2466 | void *mBuffer; |
| 2467 | size_t mBufferSize; |
| 2468 | |
| 2469 | CallbackThread(const CallbackThread &); |
| 2470 | CallbackThread &operator=(const CallbackThread &); |
| 2471 | }; |
| 2472 | |
| 2473 | CallbackThread::CallbackThread( |
| 2474 | const wp<MediaPlayerBase::AudioSink> &sink, |
| 2475 | MediaPlayerBase::AudioSink::AudioCallback cb, |
| 2476 | void *cookie) |
| 2477 | : mSink(sink), |
| 2478 | mCallback(cb), |
| 2479 | mCookie(cookie), |
| 2480 | mBuffer(NULL), |
| 2481 | mBufferSize(0) { |
| 2482 | } |
| 2483 | |
| 2484 | CallbackThread::~CallbackThread() { |
| 2485 | if (mBuffer) { |
| 2486 | free(mBuffer); |
| 2487 | mBuffer = NULL; |
| 2488 | } |
| 2489 | } |
| 2490 | |
| 2491 | bool CallbackThread::threadLoop() { |
| 2492 | sp<MediaPlayerBase::AudioSink> sink = mSink.promote(); |
| 2493 | if (sink == NULL) { |
| 2494 | return false; |
| 2495 | } |
| 2496 | |
| 2497 | if (mBuffer == NULL) { |
| 2498 | mBufferSize = sink->bufferSize(); |
| 2499 | mBuffer = malloc(mBufferSize); |
| 2500 | } |
| 2501 | |
| 2502 | size_t actualSize = |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 2503 | (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie, |
| 2504 | MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER); |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 2505 | |
| 2506 | if (actualSize > 0) { |
| 2507 | sink->write(mBuffer, actualSize); |
Andy Hung | a31335a | 2014-08-20 17:37:59 -0700 | [diff] [blame] | 2508 | // Could return false on sink->write() error or short count. |
| 2509 | // Not necessarily appropriate but would work for AudioCache behavior. |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 2510 | } |
| 2511 | |
| 2512 | return true; |
| 2513 | } |
| 2514 | |
| 2515 | //////////////////////////////////////////////////////////////////////////////// |
| 2516 | |
Chong Zhang | e5b9b69 | 2017-05-11 11:44:56 -0700 | [diff] [blame] | 2517 | void MediaPlayerService::addBatteryData(uint32_t params) { |
| 2518 | mBatteryTracker.addBatteryData(params); |
| 2519 | } |
| 2520 | |
| 2521 | status_t MediaPlayerService::pullBatteryData(Parcel* reply) { |
| 2522 | return mBatteryTracker.pullBatteryData(reply); |
| 2523 | } |
| 2524 | |
| 2525 | MediaPlayerService::BatteryTracker::BatteryTracker() { |
| 2526 | mBatteryAudio.refCount = 0; |
| 2527 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 2528 | mBatteryAudio.deviceOn[i] = 0; |
| 2529 | mBatteryAudio.lastTime[i] = 0; |
| 2530 | mBatteryAudio.totalTime[i] = 0; |
| 2531 | } |
| 2532 | // speaker is on by default |
| 2533 | mBatteryAudio.deviceOn[SPEAKER] = 1; |
| 2534 | |
| 2535 | // reset battery stats |
| 2536 | // if the mediaserver has crashed, battery stats could be left |
| 2537 | // in bad state, reset the state upon service start. |
| 2538 | BatteryNotifier::getInstance().noteResetVideo(); |
| 2539 | } |
| 2540 | |
| 2541 | void MediaPlayerService::BatteryTracker::addBatteryData(uint32_t params) |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2542 | { |
| 2543 | Mutex::Autolock lock(mLock); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2544 | |
| 2545 | int32_t time = systemTime() / 1000000L; |
| 2546 | |
| 2547 | // change audio output devices. This notification comes from AudioFlinger |
| 2548 | if ((params & kBatteryDataSpeakerOn) |
| 2549 | || (params & kBatteryDataOtherAudioDeviceOn)) { |
| 2550 | |
| 2551 | int deviceOn[NUM_AUDIO_DEVICES]; |
| 2552 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 2553 | deviceOn[i] = 0; |
| 2554 | } |
| 2555 | |
| 2556 | if ((params & kBatteryDataSpeakerOn) |
| 2557 | && (params & kBatteryDataOtherAudioDeviceOn)) { |
| 2558 | deviceOn[SPEAKER_AND_OTHER] = 1; |
| 2559 | } else if (params & kBatteryDataSpeakerOn) { |
| 2560 | deviceOn[SPEAKER] = 1; |
| 2561 | } else { |
| 2562 | deviceOn[OTHER_AUDIO_DEVICE] = 1; |
| 2563 | } |
| 2564 | |
| 2565 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 2566 | if (mBatteryAudio.deviceOn[i] != deviceOn[i]){ |
| 2567 | |
| 2568 | if (mBatteryAudio.refCount > 0) { // if playing audio |
| 2569 | if (!deviceOn[i]) { |
| 2570 | mBatteryAudio.lastTime[i] += time; |
| 2571 | mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i]; |
| 2572 | mBatteryAudio.lastTime[i] = 0; |
| 2573 | } else { |
| 2574 | mBatteryAudio.lastTime[i] = 0 - time; |
| 2575 | } |
| 2576 | } |
| 2577 | |
| 2578 | mBatteryAudio.deviceOn[i] = deviceOn[i]; |
| 2579 | } |
| 2580 | } |
| 2581 | return; |
| 2582 | } |
| 2583 | |
Marco Nelissen | b7848f1 | 2014-12-04 08:57:56 -0800 | [diff] [blame] | 2584 | // an audio stream is started |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2585 | if (params & kBatteryDataAudioFlingerStart) { |
| 2586 | // record the start time only if currently no other audio |
| 2587 | // is being played |
| 2588 | if (mBatteryAudio.refCount == 0) { |
| 2589 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 2590 | if (mBatteryAudio.deviceOn[i]) { |
| 2591 | mBatteryAudio.lastTime[i] -= time; |
| 2592 | } |
| 2593 | } |
| 2594 | } |
| 2595 | |
| 2596 | mBatteryAudio.refCount ++; |
| 2597 | return; |
| 2598 | |
| 2599 | } else if (params & kBatteryDataAudioFlingerStop) { |
| 2600 | if (mBatteryAudio.refCount <= 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2601 | ALOGW("Battery track warning: refCount is <= 0"); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2602 | return; |
| 2603 | } |
| 2604 | |
| 2605 | // record the stop time only if currently this is the only |
| 2606 | // audio being played |
| 2607 | if (mBatteryAudio.refCount == 1) { |
| 2608 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 2609 | if (mBatteryAudio.deviceOn[i]) { |
| 2610 | mBatteryAudio.lastTime[i] += time; |
| 2611 | mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i]; |
| 2612 | mBatteryAudio.lastTime[i] = 0; |
| 2613 | } |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | mBatteryAudio.refCount --; |
| 2618 | return; |
| 2619 | } |
| 2620 | |
Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 2621 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2622 | if (uid == AID_MEDIA) { |
| 2623 | return; |
| 2624 | } |
| 2625 | int index = mBatteryData.indexOfKey(uid); |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2626 | |
| 2627 | if (index < 0) { // create a new entry for this UID |
| 2628 | BatteryUsageInfo info; |
| 2629 | info.audioTotalTime = 0; |
| 2630 | info.videoTotalTime = 0; |
| 2631 | info.audioLastTime = 0; |
| 2632 | info.videoLastTime = 0; |
| 2633 | info.refCount = 0; |
| 2634 | |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2635 | if (mBatteryData.add(uid, info) == NO_MEMORY) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 2636 | ALOGE("Battery track error: no memory for new app"); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2637 | return; |
| 2638 | } |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2639 | } |
| 2640 | |
| 2641 | BatteryUsageInfo &info = mBatteryData.editValueFor(uid); |
| 2642 | |
| 2643 | if (params & kBatteryDataCodecStarted) { |
| 2644 | if (params & kBatteryDataTrackAudio) { |
| 2645 | info.audioLastTime -= time; |
| 2646 | info.refCount ++; |
| 2647 | } |
| 2648 | if (params & kBatteryDataTrackVideo) { |
| 2649 | info.videoLastTime -= time; |
| 2650 | info.refCount ++; |
| 2651 | } |
| 2652 | } else { |
| 2653 | if (info.refCount == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2654 | ALOGW("Battery track warning: refCount is already 0"); |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2655 | return; |
| 2656 | } else if (info.refCount < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 2657 | ALOGE("Battery track error: refCount < 0"); |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2658 | mBatteryData.removeItem(uid); |
| 2659 | return; |
| 2660 | } |
| 2661 | |
| 2662 | if (params & kBatteryDataTrackAudio) { |
| 2663 | info.audioLastTime += time; |
| 2664 | info.refCount --; |
| 2665 | } |
| 2666 | if (params & kBatteryDataTrackVideo) { |
| 2667 | info.videoLastTime += time; |
| 2668 | info.refCount --; |
| 2669 | } |
| 2670 | |
| 2671 | // no stream is being played by this UID |
| 2672 | if (info.refCount == 0) { |
| 2673 | info.audioTotalTime += info.audioLastTime; |
| 2674 | info.audioLastTime = 0; |
| 2675 | info.videoTotalTime += info.videoLastTime; |
| 2676 | info.videoLastTime = 0; |
| 2677 | } |
| 2678 | } |
| 2679 | } |
| 2680 | |
Chong Zhang | e5b9b69 | 2017-05-11 11:44:56 -0700 | [diff] [blame] | 2681 | status_t MediaPlayerService::BatteryTracker::pullBatteryData(Parcel* reply) { |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2682 | Mutex::Autolock lock(mLock); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2683 | |
| 2684 | // audio output devices usage |
| 2685 | int32_t time = systemTime() / 1000000L; //in ms |
| 2686 | int32_t totalTime; |
| 2687 | |
| 2688 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 2689 | totalTime = mBatteryAudio.totalTime[i]; |
| 2690 | |
| 2691 | if (mBatteryAudio.deviceOn[i] |
| 2692 | && (mBatteryAudio.lastTime[i] != 0)) { |
| 2693 | int32_t tmpTime = mBatteryAudio.lastTime[i] + time; |
| 2694 | totalTime += tmpTime; |
| 2695 | } |
| 2696 | |
| 2697 | reply->writeInt32(totalTime); |
| 2698 | // reset the total time |
| 2699 | mBatteryAudio.totalTime[i] = 0; |
| 2700 | } |
| 2701 | |
| 2702 | // codec usage |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2703 | BatteryUsageInfo info; |
| 2704 | int size = mBatteryData.size(); |
| 2705 | |
| 2706 | reply->writeInt32(size); |
| 2707 | int i = 0; |
| 2708 | |
| 2709 | while (i < size) { |
| 2710 | info = mBatteryData.valueAt(i); |
| 2711 | |
| 2712 | reply->writeInt32(mBatteryData.keyAt(i)); //UID |
| 2713 | reply->writeInt32(info.audioTotalTime); |
| 2714 | reply->writeInt32(info.videoTotalTime); |
| 2715 | |
| 2716 | info.audioTotalTime = 0; |
| 2717 | info.videoTotalTime = 0; |
| 2718 | |
| 2719 | // remove the UID entry where no stream is being played |
| 2720 | if (info.refCount <= 0) { |
| 2721 | mBatteryData.removeItemsAt(i); |
| 2722 | size --; |
| 2723 | i --; |
| 2724 | } |
| 2725 | i++; |
| 2726 | } |
| 2727 | return NO_ERROR; |
| 2728 | } |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 2729 | } // namespace android |