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