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