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