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