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