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