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