blob: fde317c370a11a860c3a4c461e7535cab40c8a83 [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
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 Wang7cf180c2011-02-19 18:37:57 -080026#include <sys/time.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080027#include <dirent.h>
28#include <unistd.h>
29
30#include <string.h>
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070031
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080032#include <cutils/atomic.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070033#include <cutils/properties.h> // for property_get
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070034
35#include <utils/misc.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080036
Chong Zhang0b30fd42014-07-23 14:46:05 -070037#include <binder/IBatteryStats.h>
Mathias Agopian75624082009-05-19 19:08:10 -070038#include <binder/IPCThreadState.h>
39#include <binder/IServiceManager.h>
40#include <binder/MemoryHeapBase.h>
41#include <binder/MemoryBase.h>
Mathias Agopianb1e7cd12013-02-14 17:11:27 -080042#include <gui/Surface.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070043#include <utils/Errors.h> // for status_t
44#include <utils/String8.h>
Marco Nelissen10dbb8e2009-09-20 10:42:13 -070045#include <utils/SystemClock.h>
Lajos Molnar06ad1522014-08-28 07:27:44 -070046#include <utils/Timers.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070047#include <utils/Vector.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080048
Andreas Huber1b86fe02014-01-29 11:13:26 -080049#include <media/IMediaHTTPService.h>
Jeff Brown2013a542012-09-04 21:38:42 -070050#include <media/IRemoteDisplay.h>
51#include <media/IRemoteDisplayClient.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080052#include <media/MediaPlayerInterface.h>
53#include <media/mediarecorder.h>
54#include <media/MediaMetadataRetrieverInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070055#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080056#include <media/AudioTrack.h>
James Dong8635b7b2011-03-14 17:01:38 -070057#include <media/MemoryLeakTrackUtil.h>
Lajos Molnar1381d4b2014-08-07 15:18:35 -070058#include <media/stagefright/MediaCodecList.h>
Eric Laurent9cb839a2011-09-27 09:48:56 -070059#include <media/stagefright/MediaErrors.h>
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +010060#include <media/stagefright/AudioPlayer.h>
61#include <media/stagefright/foundation/ADebug.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080062
Dima Zavin64760242011-05-11 14:15:23 -070063#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070064
Gloria Wang7cf180c2011-02-19 18:37:57 -080065#include <private/android_filesystem_config.h>
66
James Dong559bf282012-03-28 10:29:14 -070067#include "ActivityManager.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080068#include "MediaRecorderClient.h"
69#include "MediaPlayerService.h"
70#include "MetadataRetrieverClient.h"
John Grossman44a7e422012-06-21 17:29:24 -070071#include "MediaPlayerFactory.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080072
73#include "MidiFile.h"
Nicolas Catania14d27472009-07-13 14:37:49 -070074#include "TestPlayerStub.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070075#include "StagefrightPlayer.h"
Andreas Huberf9334412010-12-15 15:17:42 -080076#include "nuplayer/NuPlayerDriver.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070077
Andreas Huber20111aa2009-07-14 16:56:47 -070078#include <OMX.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070079
Andreas Hubered3e3e02012-03-26 11:13:27 -070080#include "Crypto.h"
Jeff Tinkercc82dc62013-02-08 10:18:35 -080081#include "Drm.h"
Andreas Huber59451f82012-09-18 10:36:32 -070082#include "HDCP.h"
Andreas Huberb7319a72013-05-29 14:20:52 -070083#include "HTTPBase.h"
Andreas Huber35213f12012-08-29 11:41:50 -070084#include "RemoteDisplay.h"
Andreas Hubered3e3e02012-03-26 11:13:27 -070085
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070086namespace {
nikoa64c8c72009-07-20 15:07:26 -070087using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070088using android::status_t;
89using android::OK;
90using android::BAD_VALUE;
91using android::NOT_ENOUGH_DATA;
92using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070093
94// Max number of entries in the filter.
95const int kMaxFilterSize = 64; // I pulled that out of thin air.
96
nikoa64c8c72009-07-20 15:07:26 -070097// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070098
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070099
100// Unmarshall a filter from a Parcel.
101// Filter format in a parcel:
102//
103// 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
104// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105// | number of entries (n) |
106// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107// | metadata type 1 |
108// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109// | metadata type 2 |
110// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111// ....
112// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113// | metadata type n |
114// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115//
116// @param p Parcel that should start with a filter.
117// @param[out] filter On exit contains the list of metadata type to be
118// filtered.
119// @param[out] status On exit contains the status code to be returned.
120// @return true if the parcel starts with a valid filter.
121bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700122 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700123 status_t *status)
124{
Nicolas Catania48290382009-07-10 13:53:06 -0700125 int32_t val;
126 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700127 {
Steve Block29357bc2012-01-06 19:20:56 +0000128 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700129 *status = NOT_ENOUGH_DATA;
130 return false;
131 }
132
Nicolas Catania48290382009-07-10 13:53:06 -0700133 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700134 {
Steve Block29357bc2012-01-06 19:20:56 +0000135 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700136 *status = BAD_VALUE;
137 return false;
138 }
139
Nicolas Catania48290382009-07-10 13:53:06 -0700140 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700141
142 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700143 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700144
nikoa64c8c72009-07-20 15:07:26 -0700145 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700146
Nicolas Catania48290382009-07-10 13:53:06 -0700147
148 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700149 {
Steve Block29357bc2012-01-06 19:20:56 +0000150 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700151 *status = NOT_ENOUGH_DATA;
152 return false;
153 }
154
nikoa64c8c72009-07-20 15:07:26 -0700155 const Metadata::Type *data =
156 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700157
Nicolas Catania48290382009-07-10 13:53:06 -0700158 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700159 {
Steve Block29357bc2012-01-06 19:20:56 +0000160 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700161 *status = BAD_VALUE;
162 return false;
163 }
164
165 // TODO: The stl impl of vector would be more efficient here
166 // because it degenerates into a memcpy on pod types. Try to
167 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700168 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700169 {
Nicolas Catania48290382009-07-10 13:53:06 -0700170 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700171 ++data;
172 }
173 *status = OK;
174 return true;
175}
176
Nicolas Catania48290382009-07-10 13:53:06 -0700177// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700178// @param val To be searched.
179// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700180bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700181{
182 // Deal with empty and ANY right away
183 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700184 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700185
Nicolas Catania48290382009-07-10 13:53:06 -0700186 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700187}
188
189} // anonymous namespace
190
191
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700192namespace {
193using android::Parcel;
194using android::String16;
195
196// marshalling tag indicating flattened utf16 tags
197// keep in sync with frameworks/base/media/java/android/media/AudioAttributes.java
198const int32_t kAudioAttributesMarshallTagFlattenTags = 1;
199
200// Audio attributes format in a parcel:
201//
202// 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
203// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
204// | usage |
205// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
206// | content_type |
207// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Hyejin Kim4f418f92014-09-05 15:50:03 +0900208// | source |
209// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700210// | flags |
211// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
212// | kAudioAttributesMarshallTagFlattenTags | // ignore tags if not found
213// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
214// | flattened tags in UTF16 |
215// | ... |
216// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
217//
218// @param p Parcel that contains audio attributes.
219// @param[out] attributes On exit points to an initialized audio_attributes_t structure
220// @param[out] status On exit contains the status code to be returned.
221void unmarshallAudioAttributes(const Parcel& parcel, audio_attributes_t *attributes)
222{
223 attributes->usage = (audio_usage_t) parcel.readInt32();
224 attributes->content_type = (audio_content_type_t) parcel.readInt32();
Hyejin Kim4f418f92014-09-05 15:50:03 +0900225 attributes->source = (audio_source_t) parcel.readInt32();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700226 attributes->flags = (audio_flags_mask_t) parcel.readInt32();
227 const bool hasFlattenedTag = (parcel.readInt32() == kAudioAttributesMarshallTagFlattenTags);
228 if (hasFlattenedTag) {
229 // the tags are UTF16, convert to UTF8
230 String16 tags = parcel.readString16();
231 ssize_t realTagSize = utf16_to_utf8_length(tags.string(), tags.size());
232 if (realTagSize <= 0) {
233 strcpy(attributes->tags, "");
234 } else {
235 // copy the flattened string into the attributes as the destination for the conversion:
236 // copying array size -1, array for tags was calloc'd, no need to NULL-terminate it
237 size_t tagSize = realTagSize > AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 ?
238 AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 : realTagSize;
Sergio Giro804c3b52016-06-28 18:24:52 +0100239 utf16_to_utf8(tags.string(), tagSize, attributes->tags,
240 sizeof(attributes->tags) / sizeof(attributes->tags[0]));
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700241 }
242 } else {
243 ALOGE("unmarshallAudioAttributes() received unflattened tags, ignoring tag values");
244 strcpy(attributes->tags, "");
245 }
246}
247} // anonymous namespace
248
249
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800250namespace android {
251
Dave Burked681bbb2011-08-30 14:39:17 +0100252static bool checkPermission(const char* permissionString) {
253#ifndef HAVE_ANDROID_OS
254 return true;
255#endif
256 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
257 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000258 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100259 return ok;
260}
261
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800262// TODO: Find real cause of Audio/Video delay in PV framework and remove this workaround
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800263/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
264/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
265
266void MediaPlayerService::instantiate() {
267 defaultServiceManager()->addService(
268 String16("media.player"), new MediaPlayerService());
269}
270
271MediaPlayerService::MediaPlayerService()
272{
Steve Block3856b092011-10-20 11:56:00 +0100273 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800274 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800275
276 mBatteryAudio.refCount = 0;
277 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
278 mBatteryAudio.deviceOn[i] = 0;
279 mBatteryAudio.lastTime[i] = 0;
280 mBatteryAudio.totalTime[i] = 0;
281 }
282 // speaker is on by default
283 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700284
Chong Zhang0b30fd42014-07-23 14:46:05 -0700285 // reset battery stats
286 // if the mediaserver has crashed, battery stats could be left
287 // in bad state, reset the state upon service start.
288 const sp<IServiceManager> sm(defaultServiceManager());
289 if (sm != NULL) {
290 const String16 name("batterystats");
291 sp<IBatteryStats> batteryStats =
292 interface_cast<IBatteryStats>(sm->getService(name));
293 if (batteryStats != NULL) {
294 batteryStats->noteResetVideo();
295 batteryStats->noteResetAudio();
296 }
297 }
298
John Grossman44a7e422012-06-21 17:29:24 -0700299 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800300}
301
302MediaPlayerService::~MediaPlayerService()
303{
Steve Block3856b092011-10-20 11:56:00 +0100304 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800305}
306
Glenn Kastenf37971f2012-02-03 11:06:53 -0800307sp<IMediaRecorder> MediaPlayerService::createMediaRecorder()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800308{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800309 pid_t pid = IPCThreadState::self()->getCallingPid();
Gloria Wangdac6a312009-10-29 15:46:37 -0700310 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
311 wp<MediaRecorderClient> w = recorder;
312 Mutex::Autolock lock(mLock);
313 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100314 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800315 return recorder;
316}
317
Gloria Wangdac6a312009-10-29 15:46:37 -0700318void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
319{
320 Mutex::Autolock lock(mLock);
321 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100322 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700323}
324
Glenn Kastenf37971f2012-02-03 11:06:53 -0800325sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800326{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800327 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800328 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100329 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800330 return retriever;
331}
332
Glenn Kastenf37971f2012-02-03 11:06:53 -0800333sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100334 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800335{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800336 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800337 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700338
339 sp<Client> c = new Client(
340 this, pid, connId, client, audioSessionId,
341 IPCThreadState::self()->getCallingUid());
342
Steve Block3856b092011-10-20 11:56:00 +0100343 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100344 IPCThreadState::self()->getCallingUid());
345
346 wp<Client> w = c;
347 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800348 Mutex::Autolock lock(mLock);
349 mClients.add(w);
350 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800351 return c;
352}
353
Lajos Molnar1381d4b2014-08-07 15:18:35 -0700354sp<IMediaCodecList> MediaPlayerService::getCodecList() const {
355 return MediaCodecList::getLocalInstance();
356}
357
Andreas Huber318ad9c2009-10-15 13:46:54 -0700358sp<IOMX> MediaPlayerService::getOMX() {
359 Mutex::Autolock autoLock(mLock);
360
361 if (mOMX.get() == NULL) {
362 mOMX = new OMX;
363 }
364
365 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700366}
367
Andreas Hubered3e3e02012-03-26 11:13:27 -0700368sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700369 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700370}
371
Jeff Tinkercc82dc62013-02-08 10:18:35 -0800372sp<IDrm> MediaPlayerService::makeDrm() {
373 return new Drm;
374}
375
Andreas Huber279dcd82013-01-30 10:41:25 -0800376sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
377 return new HDCP(createEncryptionModule);
Andreas Huber59451f82012-09-18 10:36:32 -0700378}
379
Jeff Brown2013a542012-09-04 21:38:42 -0700380sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
381 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownaba33d52012-09-07 17:38:58 -0700382 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
383 return NULL;
384 }
385
Jeff Brownced24b32012-09-05 17:48:03 -0700386 return new RemoteDisplay(client, iface.string());
Jeff Brown2013a542012-09-04 21:38:42 -0700387}
388
Mark Salyzyn77342f72014-06-18 16:31:32 -0700389status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& /*args*/) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800390{
391 const size_t SIZE = 256;
392 char buffer[SIZE];
393 String8 result;
394
395 result.append(" AudioCache\n");
396 if (mHeap != 0) {
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000397 snprintf(buffer, 255, " heap base(%p), size(%zu), flags(%d)\n",
Eric Laurent3d00aa62013-09-24 09:53:27 -0700398 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800399 result.append(buffer);
400 }
Scott Fan7d409692013-04-28 10:13:54 +0800401 snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%zd)\n",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800402 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
403 result.append(buffer);
404 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
405 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
406 result.append(buffer);
407 ::write(fd, result.string(), result.size());
408 return NO_ERROR;
409}
410
411status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
412{
413 const size_t SIZE = 256;
414 char buffer[SIZE];
415 String8 result;
416
417 result.append(" AudioOutput\n");
418 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
419 mStreamType, mLeftVolume, mRightVolume);
420 result.append(buffer);
421 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800422 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800423 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700424 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
425 mAuxEffectId, mSendLevel);
426 result.append(buffer);
427
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800428 ::write(fd, result.string(), result.size());
429 if (mTrack != 0) {
430 mTrack->dump(fd, args);
431 }
432 return NO_ERROR;
433}
434
435status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
436{
437 const size_t SIZE = 256;
438 char buffer[SIZE];
439 String8 result;
440 result.append(" Client\n");
441 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
442 mPid, mConnId, mStatus, mLoop?"true": "false");
443 result.append(buffer);
444 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700445 if (mPlayer != NULL) {
446 mPlayer->dump(fd, args);
447 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800448 if (mAudioOutput != 0) {
449 mAudioOutput->dump(fd, args);
450 }
451 write(fd, "\n", 1);
452 return NO_ERROR;
453}
454
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800455status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
456{
457 const size_t SIZE = 256;
458 char buffer[SIZE];
459 String8 result;
460 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
461 snprintf(buffer, SIZE, "Permission Denial: "
462 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
463 IPCThreadState::self()->getCallingPid(),
464 IPCThreadState::self()->getCallingUid());
465 result.append(buffer);
466 } else {
467 Mutex::Autolock lock(mLock);
468 for (int i = 0, n = mClients.size(); i < n; ++i) {
469 sp<Client> c = mClients[i].promote();
470 if (c != 0) c->dump(fd, args);
471 }
James Dongb9141222010-07-08 11:16:11 -0700472 if (mMediaRecorderClients.size() == 0) {
473 result.append(" No media recorder client\n\n");
474 } else {
475 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
476 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700477 if (c != 0) {
478 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
479 result.append(buffer);
480 write(fd, result.string(), result.size());
481 result = "\n";
482 c->dump(fd, args);
483 }
James Dongb9141222010-07-08 11:16:11 -0700484 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700485 }
486
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800487 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700488 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800489 FILE *f = fopen(buffer, "r");
490 if (f) {
491 while (!feof(f)) {
492 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700493 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800494 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700495 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800496 strstr(buffer, " /system/media/")) {
497 result.append(" ");
498 result.append(buffer);
499 }
500 }
501 fclose(f);
502 } else {
503 result.append("couldn't open ");
504 result.append(buffer);
505 result.append("\n");
506 }
507
Glenn Kasten0512ab52011-05-04 17:58:57 -0700508 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800509 DIR *d = opendir(buffer);
510 if (d) {
511 struct dirent *ent;
512 while((ent = readdir(d)) != NULL) {
513 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700514 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515 struct stat s;
516 if (lstat(buffer, &s) == 0) {
517 if ((s.st_mode & S_IFMT) == S_IFLNK) {
518 char linkto[256];
519 int len = readlink(buffer, linkto, sizeof(linkto));
520 if(len > 0) {
521 if(len > 255) {
522 linkto[252] = '.';
523 linkto[253] = '.';
524 linkto[254] = '.';
525 linkto[255] = 0;
526 } else {
527 linkto[len] = 0;
528 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700529 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800530 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700531 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800532 strstr(linkto, "/system/media/") == linkto) {
533 result.append(" ");
534 result.append(buffer);
535 result.append(" -> ");
536 result.append(linkto);
537 result.append("\n");
538 }
539 }
540 } else {
541 result.append(" unexpected type for ");
542 result.append(buffer);
543 result.append("\n");
544 }
545 }
546 }
547 }
548 closedir(d);
549 } else {
550 result.append("couldn't open ");
551 result.append(buffer);
552 result.append("\n");
553 }
554
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555 bool dumpMem = false;
556 for (size_t i = 0; i < args.size(); i++) {
557 if (args[i] == String16("-m")) {
558 dumpMem = true;
559 }
560 }
561 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700562 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800563 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800564 }
565 write(fd, result.string(), result.size());
566 return NO_ERROR;
567}
568
569void MediaPlayerService::removeClient(wp<Client> client)
570{
571 Mutex::Autolock lock(mLock);
572 mClients.remove(client);
573}
574
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700575MediaPlayerService::Client::Client(
576 const sp<MediaPlayerService>& service, pid_t pid,
577 int32_t connId, const sp<IMediaPlayerClient>& client,
578 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800579{
Steve Block3856b092011-10-20 11:56:00 +0100580 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800581 mPid = pid;
582 mConnId = connId;
583 mService = service;
584 mClient = client;
585 mLoop = false;
586 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700587 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700588 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800589 mRetransmitEndpointValid = false;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700590 mAudioAttributes = NULL;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700591
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800592#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000593 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800594 mAntagonizer = new Antagonizer(notify, this);
595#endif
596}
597
598MediaPlayerService::Client::~Client()
599{
Steve Block3856b092011-10-20 11:56:00 +0100600 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800601 mAudioOutput.clear();
602 wp<Client> client(this);
603 disconnect();
604 mService->removeClient(client);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700605 if (mAudioAttributes != NULL) {
606 free(mAudioAttributes);
607 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800608}
609
610void MediaPlayerService::Client::disconnect()
611{
Steve Block3856b092011-10-20 11:56:00 +0100612 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800613 // grab local reference and clear main reference to prevent future
614 // access to object
615 sp<MediaPlayerBase> p;
616 {
617 Mutex::Autolock l(mLock);
618 p = mPlayer;
beanzdcfefde2012-11-05 09:51:43 +0800619 mClient.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800620 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700621
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800622 mPlayer.clear();
623
624 // clear the notification to prevent callbacks to dead client
625 // and reset the player. We assume the player will serialize
626 // access to itself if necessary.
627 if (p != 0) {
628 p->setNotifyCallback(0, 0);
629#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000630 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800631 mAntagonizer->kill();
632#endif
633 p->reset();
634 }
635
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700636 disconnectNativeWindow();
637
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800638 IPCThreadState::self()->flushCommands();
639}
640
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800641sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
642{
643 // determine if we have the right player type
644 sp<MediaPlayerBase> p = mPlayer;
645 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100646 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800647 p.clear();
648 }
649 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700650 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800651 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700652
Jason Simmonsdb29e522011-08-12 13:46:55 -0700653 if (p != NULL) {
654 p->setUID(mUID);
655 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700656
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800657 return p;
658}
659
John Grossmanc795b642012-02-22 15:38:35 -0800660sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
661 player_type playerType)
662{
663 ALOGV("player type = %d", playerType);
664
665 // create the right type of player
666 sp<MediaPlayerBase> p = createPlayer(playerType);
667 if (p == NULL) {
668 return p;
669 }
670
671 if (!p->hardwareOutput()) {
Marco Nelissend457c972014-02-11 08:47:07 -0800672 mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(),
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700673 mPid, mAudioAttributes);
John Grossmanc795b642012-02-22 15:38:35 -0800674 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
675 }
676
677 return p;
678}
679
680void MediaPlayerService::Client::setDataSource_post(
681 const sp<MediaPlayerBase>& p,
682 status_t status)
683{
684 ALOGV(" setDataSource");
685 mStatus = status;
686 if (mStatus != OK) {
687 ALOGE(" error: %d", mStatus);
688 return;
689 }
690
691 // Set the re-transmission endpoint if one was chosen.
692 if (mRetransmitEndpointValid) {
693 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
694 if (mStatus != NO_ERROR) {
695 ALOGE("setRetransmitEndpoint error: %d", mStatus);
696 }
697 }
698
699 if (mStatus == OK) {
700 mPlayer = p;
701 }
702}
703
Andreas Huber2db84552010-01-28 11:19:57 -0800704status_t MediaPlayerService::Client::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800705 const sp<IMediaHTTPService> &httpService,
706 const char *url,
707 const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800708{
Steve Block3856b092011-10-20 11:56:00 +0100709 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800710 if (url == NULL)
711 return UNKNOWN_ERROR;
712
Dave Burked681bbb2011-08-30 14:39:17 +0100713 if ((strncmp(url, "http://", 7) == 0) ||
714 (strncmp(url, "https://", 8) == 0) ||
715 (strncmp(url, "rtsp://", 7) == 0)) {
716 if (!checkPermission("android.permission.INTERNET")) {
717 return PERMISSION_DENIED;
718 }
719 }
720
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800721 if (strncmp(url, "content://", 10) == 0) {
722 // get a filedescriptor for the content Uri and
723 // pass it to the setDataSource(fd) method
724
725 String16 url16(url);
726 int fd = android::openContentProviderFile(url16);
727 if (fd < 0)
728 {
Steve Block29357bc2012-01-06 19:20:56 +0000729 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800730 return UNKNOWN_ERROR;
731 }
732 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
733 close(fd);
734 return mStatus;
735 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700736 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800737 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
738 if (p == NULL) {
739 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800740 }
741
Andreas Huber1b86fe02014-01-29 11:13:26 -0800742 setDataSource_post(p, p->setDataSource(httpService, url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800743 return mStatus;
744 }
745}
746
747status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
748{
Steve Block3856b092011-10-20 11:56:00 +0100749 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800750 struct stat sb;
751 int ret = fstat(fd, &sb);
752 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000753 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800754 return UNKNOWN_ERROR;
755 }
756
Steve Block3856b092011-10-20 11:56:00 +0100757 ALOGV("st_dev = %llu", sb.st_dev);
758 ALOGV("st_mode = %u", sb.st_mode);
Mark Salyzyn77342f72014-06-18 16:31:32 -0700759 ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
760 ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
Steve Block3856b092011-10-20 11:56:00 +0100761 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800762
763 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000764 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800765 ::close(fd);
766 return UNKNOWN_ERROR;
767 }
768 if (offset + length > sb.st_size) {
769 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100770 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800771 }
772
John Grossman44a7e422012-06-21 17:29:24 -0700773 player_type playerType = MediaPlayerFactory::getPlayerType(this,
774 fd,
775 offset,
776 length);
John Grossmanc795b642012-02-22 15:38:35 -0800777 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
778 if (p == NULL) {
779 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800780 }
781
782 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800783 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800784 return mStatus;
785}
786
Andreas Hubere2b10282010-11-23 11:41:34 -0800787status_t MediaPlayerService::Client::setDataSource(
788 const sp<IStreamSource> &source) {
789 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700790 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800791 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800792 if (p == NULL) {
793 return NO_INIT;
794 }
795
Andreas Hubere2b10282010-11-23 11:41:34 -0800796 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800797 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800798 return mStatus;
799}
800
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700801void MediaPlayerService::Client::disconnectNativeWindow() {
802 if (mConnectedWindow != NULL) {
803 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
804 NATIVE_WINDOW_API_MEDIA);
805
806 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000807 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700808 strerror(-err), err);
809 }
810 }
811 mConnectedWindow.clear();
812}
813
Glenn Kasten11731182011-02-08 17:26:17 -0800814status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800815 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800816{
Andy McFadden484566c2012-12-18 09:46:54 -0800817 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800818 sp<MediaPlayerBase> p = getPlayer();
819 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700820
Andy McFadden484566c2012-12-18 09:46:54 -0800821 sp<IBinder> binder(bufferProducer == NULL ? NULL :
822 bufferProducer->asBinder());
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700823 if (mConnectedWindowBinder == binder) {
824 return OK;
825 }
826
827 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -0800828 if (bufferProducer != NULL) {
Marco Nelissenee08f7e2013-09-16 13:30:01 -0700829 anw = new Surface(bufferProducer, true /* controlledByApp */);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700830 status_t err = native_window_api_connect(anw.get(),
831 NATIVE_WINDOW_API_MEDIA);
832
833 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000834 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700835 // Note that we must do the reset before disconnecting from the ANW.
836 // Otherwise queue/dequeue calls could be made on the disconnected
837 // ANW, which may result in errors.
838 reset();
839
840 disconnectNativeWindow();
841
842 return err;
843 }
844 }
845
Andy McFadden484566c2012-12-18 09:46:54 -0800846 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700847 // disconnecting the old one. Otherwise queue/dequeue calls could be made
848 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -0800849 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700850
851 disconnectNativeWindow();
852
853 mConnectedWindow = anw;
854
855 if (err == OK) {
856 mConnectedWindowBinder = binder;
857 } else {
858 disconnectNativeWindow();
859 }
860
861 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800862}
863
Nicolas Catania1d187f12009-05-12 23:25:55 -0700864status_t MediaPlayerService::Client::invoke(const Parcel& request,
865 Parcel *reply)
866{
867 sp<MediaPlayerBase> p = getPlayer();
868 if (p == NULL) return UNKNOWN_ERROR;
869 return p->invoke(request, reply);
870}
871
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700872// This call doesn't need to access the native player.
873status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
874{
875 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700876 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700877
Nicolas Catania48290382009-07-10 13:53:06 -0700878 if (unmarshallFilter(filter, &allow, &status) &&
879 unmarshallFilter(filter, &drop, &status)) {
880 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700881
882 mMetadataAllow = allow;
883 mMetadataDrop = drop;
884 }
885 return status;
886}
887
Nicolas Catania48290382009-07-10 13:53:06 -0700888status_t MediaPlayerService::Client::getMetadata(
Mark Salyzyn77342f72014-06-18 16:31:32 -0700889 bool update_only, bool /*apply_filter*/, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700890{
nikoa64c8c72009-07-20 15:07:26 -0700891 sp<MediaPlayerBase> player = getPlayer();
892 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700893
nikod608a812009-07-16 16:39:53 -0700894 status_t status;
895 // Placeholder for the return code, updated by the caller.
896 reply->writeInt32(-1);
897
nikoa64c8c72009-07-20 15:07:26 -0700898 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700899
900 // We don't block notifications while we fetch the data. We clear
901 // mMetadataUpdated first so we don't lose notifications happening
902 // during the rest of this call.
903 {
904 Mutex::Autolock lock(mLock);
905 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700906 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700907 }
908 mMetadataUpdated.clear();
909 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700910
nikoa64c8c72009-07-20 15:07:26 -0700911 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700912
nikoa64c8c72009-07-20 15:07:26 -0700913 metadata.appendHeader();
914 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700915
916 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700917 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000918 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700919 return status;
920 }
921
922 // FIXME: Implement filtering on the result. Not critical since
923 // filtering takes place on the update notifications already. This
924 // would be when all the metadata are fetch and a filter is set.
925
nikod608a812009-07-16 16:39:53 -0700926 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700927 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700928 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700929}
930
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800931status_t MediaPlayerService::Client::prepareAsync()
932{
Steve Block3856b092011-10-20 11:56:00 +0100933 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800934 sp<MediaPlayerBase> p = getPlayer();
935 if (p == 0) return UNKNOWN_ERROR;
936 status_t ret = p->prepareAsync();
937#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000938 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800939 if (ret == NO_ERROR) mAntagonizer->start();
940#endif
941 return ret;
942}
943
944status_t MediaPlayerService::Client::start()
945{
Steve Block3856b092011-10-20 11:56:00 +0100946 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800947 sp<MediaPlayerBase> p = getPlayer();
948 if (p == 0) return UNKNOWN_ERROR;
949 p->setLooping(mLoop);
950 return p->start();
951}
952
953status_t MediaPlayerService::Client::stop()
954{
Steve Block3856b092011-10-20 11:56:00 +0100955 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800956 sp<MediaPlayerBase> p = getPlayer();
957 if (p == 0) return UNKNOWN_ERROR;
958 return p->stop();
959}
960
961status_t MediaPlayerService::Client::pause()
962{
Steve Block3856b092011-10-20 11:56:00 +0100963 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800964 sp<MediaPlayerBase> p = getPlayer();
965 if (p == 0) return UNKNOWN_ERROR;
966 return p->pause();
967}
968
969status_t MediaPlayerService::Client::isPlaying(bool* state)
970{
971 *state = false;
972 sp<MediaPlayerBase> p = getPlayer();
973 if (p == 0) return UNKNOWN_ERROR;
974 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100975 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800976 return NO_ERROR;
977}
978
979status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
980{
Steve Block3856b092011-10-20 11:56:00 +0100981 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800982 sp<MediaPlayerBase> p = getPlayer();
983 if (p == 0) return UNKNOWN_ERROR;
984 status_t ret = p->getCurrentPosition(msec);
985 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100986 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800987 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000988 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800989 }
990 return ret;
991}
992
993status_t MediaPlayerService::Client::getDuration(int *msec)
994{
Steve Block3856b092011-10-20 11:56:00 +0100995 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800996 sp<MediaPlayerBase> p = getPlayer();
997 if (p == 0) return UNKNOWN_ERROR;
998 status_t ret = p->getDuration(msec);
999 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001000 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001001 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001002 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001003 }
1004 return ret;
1005}
1006
Marco Nelissen6b74d672012-02-28 16:07:44 -08001007status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
1008 ALOGV("setNextPlayer");
1009 Mutex::Autolock l(mLock);
1010 sp<Client> c = static_cast<Client*>(player.get());
1011 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -07001012
1013 if (c != NULL) {
1014 if (mAudioOutput != NULL) {
1015 mAudioOutput->setNextOutput(c->mAudioOutput);
1016 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
1017 ALOGE("no current audio output");
1018 }
1019
1020 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
1021 mPlayer->setNextPlayer(mNextClient->getPlayer());
1022 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001023 }
John Grossman5f7e55e2012-08-24 14:47:25 -07001024
Marco Nelissen6b74d672012-02-28 16:07:44 -08001025 return OK;
1026}
1027
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001028status_t MediaPlayerService::Client::seekTo(int msec)
1029{
Steve Block3856b092011-10-20 11:56:00 +01001030 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001031 sp<MediaPlayerBase> p = getPlayer();
1032 if (p == 0) return UNKNOWN_ERROR;
1033 return p->seekTo(msec);
1034}
1035
1036status_t MediaPlayerService::Client::reset()
1037{
Steve Block3856b092011-10-20 11:56:00 +01001038 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -08001039 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001040 sp<MediaPlayerBase> p = getPlayer();
1041 if (p == 0) return UNKNOWN_ERROR;
1042 return p->reset();
1043}
1044
Glenn Kastenfff6d712012-01-12 16:38:12 -08001045status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001046{
Steve Block3856b092011-10-20 11:56:00 +01001047 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001048 // TODO: for hardware output, call player instead
1049 Mutex::Autolock l(mLock);
1050 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1051 return NO_ERROR;
1052}
1053
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001054status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel)
1055{
1056 if (mAudioAttributes != NULL) { free(mAudioAttributes); }
1057 mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1058 unmarshallAudioAttributes(parcel, mAudioAttributes);
1059
1060 ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
1061 mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
1062 mAudioAttributes->tags);
1063
1064 if (mAudioOutput != 0) {
1065 mAudioOutput->setAudioAttributes(mAudioAttributes);
1066 }
1067 return NO_ERROR;
1068}
1069
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001070status_t MediaPlayerService::Client::setLooping(int loop)
1071{
Steve Block3856b092011-10-20 11:56:00 +01001072 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001073 mLoop = loop;
1074 sp<MediaPlayerBase> p = getPlayer();
1075 if (p != 0) return p->setLooping(loop);
1076 return NO_ERROR;
1077}
1078
1079status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1080{
Steve Block3856b092011-10-20 11:56:00 +01001081 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -08001082
1083 // for hardware output, call player instead
1084 sp<MediaPlayerBase> p = getPlayer();
1085 {
1086 Mutex::Autolock l(mLock);
1087 if (p != 0 && p->hardwareOutput()) {
1088 MediaPlayerHWInterface* hwp =
1089 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1090 return hwp->setVolume(leftVolume, rightVolume);
1091 } else {
1092 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1093 return NO_ERROR;
1094 }
1095 }
1096
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001097 return NO_ERROR;
1098}
1099
Eric Laurent2beeb502010-07-16 07:43:46 -07001100status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1101{
Steve Block3856b092011-10-20 11:56:00 +01001102 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001103 Mutex::Autolock l(mLock);
1104 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1105 return NO_ERROR;
1106}
1107
1108status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1109{
Steve Block3856b092011-10-20 11:56:00 +01001110 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001111 Mutex::Autolock l(mLock);
1112 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1113 return NO_ERROR;
1114}
Nicolas Catania48290382009-07-10 13:53:06 -07001115
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001116status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001117 ALOGV("[%d] setParameter(%d)", mConnId, key);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001118 switch (key) {
1119 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
1120 {
1121 Mutex::Autolock l(mLock);
1122 return setAudioAttributes_l(request);
1123 }
1124 default:
1125 sp<MediaPlayerBase> p = getPlayer();
1126 if (p == 0) { return UNKNOWN_ERROR; }
1127 return p->setParameter(key, request);
1128 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001129}
1130
1131status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001132 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001133 sp<MediaPlayerBase> p = getPlayer();
1134 if (p == 0) return UNKNOWN_ERROR;
1135 return p->getParameter(key, reply);
1136}
1137
John Grossmanc795b642012-02-22 15:38:35 -08001138status_t MediaPlayerService::Client::setRetransmitEndpoint(
1139 const struct sockaddr_in* endpoint) {
1140
1141 if (NULL != endpoint) {
1142 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1143 uint16_t p = ntohs(endpoint->sin_port);
1144 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1145 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1146 } else {
1147 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1148 }
1149
1150 sp<MediaPlayerBase> p = getPlayer();
1151
1152 // Right now, the only valid time to set a retransmit endpoint is before
1153 // player selection has been made (since the presence or absence of a
1154 // retransmit endpoint is going to determine which player is selected during
1155 // setDataSource).
1156 if (p != 0) return INVALID_OPERATION;
1157
1158 if (NULL != endpoint) {
1159 mRetransmitEndpoint = *endpoint;
1160 mRetransmitEndpointValid = true;
1161 } else {
1162 mRetransmitEndpointValid = false;
1163 }
1164
1165 return NO_ERROR;
1166}
1167
John Grossman44a7e422012-06-21 17:29:24 -07001168status_t MediaPlayerService::Client::getRetransmitEndpoint(
1169 struct sockaddr_in* endpoint)
1170{
1171 if (NULL == endpoint)
1172 return BAD_VALUE;
1173
1174 sp<MediaPlayerBase> p = getPlayer();
1175
1176 if (p != NULL)
1177 return p->getRetransmitEndpoint(endpoint);
1178
1179 if (!mRetransmitEndpointValid)
1180 return NO_INIT;
1181
1182 *endpoint = mRetransmitEndpoint;
1183
1184 return NO_ERROR;
1185}
1186
Gloria Wangb483c472011-04-11 17:23:27 -07001187void MediaPlayerService::Client::notify(
1188 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001189{
1190 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001191 if (client == NULL) {
1192 return;
1193 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001194
James Dongb8a98252012-08-26 16:13:03 -07001195 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001196 {
1197 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001198 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001199 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001200 if (client->mAudioOutput != NULL)
1201 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001202 client->mNextClient->start();
1203 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1204 }
1205 }
1206
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001207 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001208 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001209 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001210
1211 if(client->shouldDropMetadata(metadata_type)) {
1212 return;
1213 }
1214
1215 // Update the list of metadata that have changed. getMetadata
1216 // also access mMetadataUpdated and clears it.
1217 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001218 }
James Dongb8a98252012-08-26 16:13:03 -07001219
1220 if (c != NULL) {
1221 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1222 c->notify(msg, ext1, ext2, obj);
1223 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001224}
1225
Nicolas Catania48290382009-07-10 13:53:06 -07001226
nikoa64c8c72009-07-20 15:07:26 -07001227bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001228{
Nicolas Catania48290382009-07-10 13:53:06 -07001229 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001230
Nicolas Catania48290382009-07-10 13:53:06 -07001231 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001232 return true;
1233 }
1234
Nicolas Catania48290382009-07-10 13:53:06 -07001235 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001236 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001237 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001238 return true;
1239 }
1240}
1241
Nicolas Catania48290382009-07-10 13:53:06 -07001242
nikoa64c8c72009-07-20 15:07:26 -07001243void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001244 Mutex::Autolock lock(mLock);
1245 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1246 mMetadataUpdated.add(metadata_type);
1247 }
1248}
1249
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001250#if CALLBACK_ANTAGONIZER
1251const int Antagonizer::interval = 10000; // 10 msecs
1252
1253Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1254 mExit(false), mActive(false), mClient(client), mCb(cb)
1255{
1256 createThread(callbackThread, this);
1257}
1258
1259void Antagonizer::kill()
1260{
1261 Mutex::Autolock _l(mLock);
1262 mActive = false;
1263 mExit = true;
1264 mCondition.wait(mLock);
1265}
1266
1267int Antagonizer::callbackThread(void* user)
1268{
Steve Blockb8a80522011-12-20 16:23:08 +00001269 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001270 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1271 while (!p->mExit) {
1272 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001273 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001274 p->mCb(p->mClient, 0, 0, 0);
1275 }
1276 usleep(interval);
1277 }
1278 Mutex::Autolock _l(p->mLock);
1279 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001280 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001281 return 0;
1282}
1283#endif
1284
Andreas Huber1b86fe02014-01-29 11:13:26 -08001285status_t MediaPlayerService::decode(
1286 const sp<IMediaHTTPService> &httpService,
1287 const char* url,
1288 uint32_t *pSampleRate,
1289 int* pNumChannels,
1290 audio_format_t* pFormat,
1291 const sp<IMemoryHeap>& heap,
1292 size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001293{
Steve Block3856b092011-10-20 11:56:00 +01001294 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001295 sp<MediaPlayerBase> player;
Eric Laurent3d00aa62013-09-24 09:53:27 -07001296 status_t status = BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001297
1298 // Protect our precious, precious DRMd ringtones by only allowing
1299 // decoding of http, but not filesystem paths or content Uris.
1300 // If the application wants to decode those, it should open a
1301 // filedescriptor for them and use that.
1302 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001303 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
Eric Laurent3d00aa62013-09-24 09:53:27 -07001304 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001305 }
1306
John Grossman44a7e422012-06-21 17:29:24 -07001307 player_type playerType =
1308 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +01001309 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001310
1311 // create the right type of player
Eric Laurent3d00aa62013-09-24 09:53:27 -07001312 sp<AudioCache> cache = new AudioCache(heap);
John Grossman44a7e422012-06-21 17:29:24 -07001313 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001314 if (player == NULL) goto Exit;
1315 if (player->hardwareOutput()) goto Exit;
1316
1317 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1318
1319 // set data source
Andreas Huber1b86fe02014-01-29 11:13:26 -08001320 if (player->setDataSource(httpService, url) != NO_ERROR) goto Exit;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001321
Steve Block3856b092011-10-20 11:56:00 +01001322 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001323 player->prepareAsync();
1324
Steve Block3856b092011-10-20 11:56:00 +01001325 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001326 if (cache->wait() != NO_ERROR) goto Exit;
1327
Steve Block3856b092011-10-20 11:56:00 +01001328 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001329 player->start();
1330
Steve Block3856b092011-10-20 11:56:00 +01001331 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001332 cache->wait();
1333 // in case of error, return what was successfully decoded.
1334 if (cache->size() == 0) {
1335 goto Exit;
1336 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001337
Eric Laurent3d00aa62013-09-24 09:53:27 -07001338 *pSize = cache->size();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001339 *pSampleRate = cache->sampleRate();
1340 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001341 *pFormat = cache->format();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001342 ALOGV("return size %d sampleRate=%u, channelCount = %d, format = %d",
1343 *pSize, *pSampleRate, *pNumChannels, *pFormat);
1344 status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001345
1346Exit:
1347 if (player != 0) player->reset();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001348 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001349}
1350
Eric Laurent3d00aa62013-09-24 09:53:27 -07001351status_t MediaPlayerService::decode(int fd, int64_t offset, int64_t length,
1352 uint32_t *pSampleRate, int* pNumChannels,
1353 audio_format_t* pFormat,
1354 const sp<IMemoryHeap>& heap, size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001355{
Steve Block3856b092011-10-20 11:56:00 +01001356 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001357 sp<MediaPlayerBase> player;
Eric Laurent3d00aa62013-09-24 09:53:27 -07001358 status_t status = BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001359
John Grossman44a7e422012-06-21 17:29:24 -07001360 player_type playerType = MediaPlayerFactory::getPlayerType(NULL /* client */,
1361 fd,
1362 offset,
1363 length);
Steve Block3856b092011-10-20 11:56:00 +01001364 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001365
1366 // create the right type of player
Eric Laurent3d00aa62013-09-24 09:53:27 -07001367 sp<AudioCache> cache = new AudioCache(heap);
John Grossman44a7e422012-06-21 17:29:24 -07001368 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001369 if (player == NULL) goto Exit;
1370 if (player->hardwareOutput()) goto Exit;
1371
1372 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1373
1374 // set data source
1375 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1376
Steve Block3856b092011-10-20 11:56:00 +01001377 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001378 player->prepareAsync();
1379
Steve Block3856b092011-10-20 11:56:00 +01001380 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001381 if (cache->wait() != NO_ERROR) goto Exit;
1382
Steve Block3856b092011-10-20 11:56:00 +01001383 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001384 player->start();
1385
Steve Block3856b092011-10-20 11:56:00 +01001386 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001387 cache->wait();
1388 // in case of error, return what was successfully decoded.
1389 if (cache->size() == 0) {
1390 goto Exit;
1391 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001392
Eric Laurent3d00aa62013-09-24 09:53:27 -07001393 *pSize = cache->size();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001394 *pSampleRate = cache->sampleRate();
1395 *pNumChannels = cache->channelCount();
1396 *pFormat = cache->format();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001397 ALOGV("return size %d, sampleRate=%u, channelCount = %d, format = %d",
1398 *pSize, *pSampleRate, *pNumChannels, *pFormat);
1399 status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001400
1401Exit:
1402 if (player != 0) player->reset();
1403 ::close(fd);
Eric Laurent3d00aa62013-09-24 09:53:27 -07001404 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001405}
1406
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001407
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001408#undef LOG_TAG
1409#define LOG_TAG "AudioSink"
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001410MediaPlayerService::AudioOutput::AudioOutput(int sessionId, int uid, int pid,
1411 const audio_attributes_t* attr)
Andreas Huber20111aa2009-07-14 16:56:47 -07001412 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001413 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001414 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001415 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001416 mSessionId(sessionId),
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001417 mUid(uid),
Marco Nelissend457c972014-02-11 08:47:07 -08001418 mPid(pid),
Eric Laurent1948eb32012-04-13 16:50:19 -07001419 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001420 ALOGV("AudioOutput(%d)", sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -07001421 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001422 mLeftVolume = 1.0;
1423 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001424 mPlaybackRatePermille = 1000;
1425 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001426 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001427 mAuxEffectId = 0;
1428 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001429 setMinBufferCount();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001430 mAttributes = attr;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001431}
1432
1433MediaPlayerService::AudioOutput::~AudioOutput()
1434{
1435 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001436 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001437}
1438
1439void MediaPlayerService::AudioOutput::setMinBufferCount()
1440{
1441 char value[PROPERTY_VALUE_MAX];
1442 if (property_get("ro.kernel.qemu", value, 0)) {
1443 mIsOnEmulator = true;
1444 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1445 }
1446}
1447
1448bool MediaPlayerService::AudioOutput::isOnEmulator()
1449{
1450 setMinBufferCount();
1451 return mIsOnEmulator;
1452}
1453
1454int MediaPlayerService::AudioOutput::getMinBufferCount()
1455{
1456 setMinBufferCount();
1457 return mMinBufferCount;
1458}
1459
1460ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1461{
1462 if (mTrack == 0) return NO_INIT;
1463 return mTrack->frameCount() * frameSize();
1464}
1465
1466ssize_t MediaPlayerService::AudioOutput::frameCount() const
1467{
1468 if (mTrack == 0) return NO_INIT;
1469 return mTrack->frameCount();
1470}
1471
1472ssize_t MediaPlayerService::AudioOutput::channelCount() const
1473{
1474 if (mTrack == 0) return NO_INIT;
1475 return mTrack->channelCount();
1476}
1477
1478ssize_t MediaPlayerService::AudioOutput::frameSize() const
1479{
1480 if (mTrack == 0) return NO_INIT;
1481 return mTrack->frameSize();
1482}
1483
1484uint32_t MediaPlayerService::AudioOutput::latency () const
1485{
Eric Laurentdb354e52012-03-05 17:27:11 -08001486 if (mTrack == 0) return 0;
1487 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001488}
1489
1490float MediaPlayerService::AudioOutput::msecsPerFrame() const
1491{
1492 return mMsecsPerFrame;
1493}
1494
Marco Nelissen4110c102012-03-29 09:31:28 -07001495status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001496{
1497 if (mTrack == 0) return NO_INIT;
1498 return mTrack->getPosition(position);
1499}
1500
Lajos Molnar06ad1522014-08-28 07:27:44 -07001501status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const
1502{
1503 if (mTrack == 0) return NO_INIT;
1504 return mTrack->getTimestamp(ts);
1505}
1506
Marco Nelissen4110c102012-03-29 09:31:28 -07001507status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1508{
1509 if (mTrack == 0) return NO_INIT;
1510 *frameswritten = mBytesWritten / frameSize();
1511 return OK;
1512}
1513
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001514status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
1515{
1516 if (mTrack == 0) return NO_INIT;
1517 return mTrack->setParameters(keyValuePairs);
1518}
1519
1520String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
1521{
1522 if (mTrack == 0) return String8::empty();
1523 return mTrack->getParameters(keys);
1524}
1525
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001526void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
1527 mAttributes = attributes;
1528}
1529
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001530void MediaPlayerService::AudioOutput::deleteRecycledTrack()
1531{
1532 ALOGV("deleteRecycledTrack");
1533
1534 if (mRecycledTrack != 0) {
1535
1536 if (mCallbackData != NULL) {
1537 mCallbackData->setOutput(NULL);
1538 mCallbackData->endTrackSwitch();
1539 }
1540
1541 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1542 mRecycledTrack->flush();
1543 }
1544 // An offloaded track isn't flushed because the STREAM_END is reported
1545 // slightly prematurely to allow time for the gapless track switch
1546 // but this means that if we decide not to recycle the track there
1547 // could be a small amount of residual data still playing. We leave
1548 // AudioFlinger to drain the track.
1549
1550 mRecycledTrack.clear();
1551 delete mCallbackData;
1552 mCallbackData = NULL;
1553 close();
1554 }
1555}
1556
Andreas Huber20111aa2009-07-14 16:56:47 -07001557status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001558 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1559 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001560 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001561 audio_output_flags_t flags,
1562 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001563{
Andreas Huber20111aa2009-07-14 16:56:47 -07001564 mCallback = cb;
1565 mCallbackCookie = cookie;
1566
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001567 // Check argument "bufferCount" against the mininum buffer count
1568 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001569 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001570 bufferCount = mMinBufferCount;
1571
1572 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001573 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1574 format, bufferCount, mSessionId, flags);
Glenn Kasten1127d652012-11-14 08:44:39 -08001575 uint32_t afSampleRate;
Glenn Kasten7da35f22012-11-14 12:54:39 -08001576 size_t afFrameCount;
Glenn Kastenbce50bf2014-02-27 15:29:51 -08001577 size_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001578
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001579 // offloading is only supported in callback mode for now.
1580 // offloadInfo must be present if offload flag is set
1581 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1582 ((cb == NULL) || (offloadInfo == NULL))) {
1583 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001584 }
1585
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001586 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1587 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1588 } else {
1589 uint32_t afSampleRate;
1590 size_t afFrameCount;
1591
1592 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1593 return NO_INIT;
1594 }
1595 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1596 return NO_INIT;
1597 }
1598
1599 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
1600 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001601
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001602 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001603 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001604 if (0 == channelMask) {
1605 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1606 return NO_INIT;
1607 }
1608 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001609
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001610 // Check whether we can recycle the track
1611 bool reuse = false;
1612 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001613
Glenn Kasten2799d742013-05-30 14:33:29 -07001614 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001615 // check whether we are switching between two offloaded tracks
1616 bothOffloaded = (flags & mRecycledTrack->getFlags()
1617 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07001618
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001619 // check if the existing track can be reused as-is, or if a new track needs to be created.
1620 reuse = true;
1621
Marco Nelissen67295b52012-06-11 14:52:53 -07001622 if ((mCallbackData == NULL && mCallback != NULL) ||
1623 (mCallbackData != NULL && mCallback == NULL)) {
1624 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1625 ALOGV("can't chain callback and write");
1626 reuse = false;
1627 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001628 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1629 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07001630 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001631 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07001632 reuse = false;
1633 } else if (flags != mFlags) {
1634 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1635 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001636 } else if (mRecycledTrack->format() != format) {
1637 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001638 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001639 } else {
1640 ALOGV("no track available to recycle");
1641 }
1642
1643 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1644
1645 // If we can't recycle and both tracks are offloaded
1646 // we must close the previous output before opening a new one
1647 if (bothOffloaded && !reuse) {
1648 ALOGV("both offloaded and not recycling");
1649 deleteRecycledTrack();
1650 }
1651
1652 sp<AudioTrack> t;
1653 CallbackData *newcbd = NULL;
1654
1655 // We don't attempt to create a new track if we are recycling an
1656 // offloaded track. But, if we are recycling a non-offloaded or we
1657 // are switching where one is offloaded and one isn't then we create
1658 // the new track in advance so that we can read additional stream info
1659
1660 if (!(reuse && bothOffloaded)) {
1661 ALOGV("creating new AudioTrack");
1662
1663 if (mCallback != NULL) {
1664 newcbd = new CallbackData(this);
1665 t = new AudioTrack(
1666 mStreamType,
1667 sampleRate,
1668 format,
1669 channelMask,
1670 frameCount,
1671 flags,
1672 CallbackWrapper,
1673 newcbd,
1674 0, // notification frames
1675 mSessionId,
1676 AudioTrack::TRANSFER_CALLBACK,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001677 offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -08001678 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001679 mPid,
1680 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001681 } else {
1682 t = new AudioTrack(
1683 mStreamType,
1684 sampleRate,
1685 format,
1686 channelMask,
1687 frameCount,
1688 flags,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001689 NULL, // callback
1690 NULL, // user data
1691 0, // notification frames
1692 mSessionId,
1693 AudioTrack::TRANSFER_DEFAULT,
1694 NULL, // offload info
Marco Nelissend457c972014-02-11 08:47:07 -08001695 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001696 mPid,
1697 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001698 }
1699
1700 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1701 ALOGE("Unable to create audio track");
1702 delete newcbd;
1703 return NO_INIT;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001704 } else {
1705 // successful AudioTrack initialization implies a legacy stream type was generated
1706 // from the audio attributes
1707 mStreamType = t->streamType();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001708 }
1709 }
1710
1711 if (reuse) {
1712 CHECK(mRecycledTrack != NULL);
1713
1714 if (!bothOffloaded) {
1715 if (mRecycledTrack->frameCount() != t->frameCount()) {
1716 ALOGV("framecount differs: %u/%u frames",
1717 mRecycledTrack->frameCount(), t->frameCount());
1718 reuse = false;
1719 }
1720 }
1721
Marco Nelissen67295b52012-06-11 14:52:53 -07001722 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001723 ALOGV("chaining to next output and recycling track");
Marco Nelissen67295b52012-06-11 14:52:53 -07001724 close();
1725 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001726 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001727 if (mCallbackData != NULL) {
1728 mCallbackData->setOutput(this);
1729 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001730 delete newcbd;
1731 return OK;
1732 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001733 }
1734
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001735 // we're not going to reuse the track, unblock and flush it
1736 // this was done earlier if both tracks are offloaded
1737 if (!bothOffloaded) {
1738 deleteRecycledTrack();
1739 }
1740
1741 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1742
Marco Nelissen67295b52012-06-11 14:52:53 -07001743 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001744 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001745 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001746
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001747 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001748 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001749 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001750 uint32_t pos;
1751 if (t->getPosition(&pos) == OK) {
1752 mBytesWritten = uint64_t(pos) * t->frameSize();
1753 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001754 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001755
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001756 status_t res = NO_ERROR;
1757 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1758 res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1759 if (res == NO_ERROR) {
1760 t->setAuxEffectSendLevel(mSendLevel);
1761 res = t->attachAuxEffect(mAuxEffectId);
1762 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001763 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001764 ALOGV("open() DONE status %d", res);
1765 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001766}
1767
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001768status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001769{
Steve Block3856b092011-10-20 11:56:00 +01001770 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001771 if (mCallbackData != NULL) {
1772 mCallbackData->endTrackSwitch();
1773 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001774 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001775 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001776 mTrack->setAuxEffectSendLevel(mSendLevel);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001777 return mTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001778 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001779 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001780}
1781
Marco Nelissen6b74d672012-02-28 16:07:44 -08001782void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1783 mNextOutput = nextOutput;
1784}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001785
1786
Marco Nelissen6b74d672012-02-28 16:07:44 -08001787void MediaPlayerService::AudioOutput::switchToNextOutput() {
1788 ALOGV("switchToNextOutput");
1789 if (mNextOutput != NULL) {
1790 if (mCallbackData != NULL) {
1791 mCallbackData->beginTrackSwitch();
1792 }
1793 delete mNextOutput->mCallbackData;
1794 mNextOutput->mCallbackData = mCallbackData;
1795 mCallbackData = NULL;
1796 mNextOutput->mRecycledTrack = mTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001797 mTrack.clear();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001798 mNextOutput->mSampleRateHz = mSampleRateHz;
1799 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001800 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001801 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001802 }
1803}
1804
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001805ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1806{
Glenn Kastenadad3d72014-02-21 14:51:43 -08001807 LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
Andreas Huber20111aa2009-07-14 16:56:47 -07001808
Steve Block3856b092011-10-20 11:56:00 +01001809 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07001810 if (mTrack != 0) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001811 ssize_t ret = mTrack->write(buffer, size);
Andy Hunga31335a2014-08-20 17:37:59 -07001812 if (ret >= 0) {
1813 mBytesWritten += ret;
1814 }
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001815 return ret;
1816 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001817 return NO_INIT;
1818}
1819
1820void MediaPlayerService::AudioOutput::stop()
1821{
Steve Block3856b092011-10-20 11:56:00 +01001822 ALOGV("stop");
Glenn Kasten2799d742013-05-30 14:33:29 -07001823 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001824}
1825
1826void MediaPlayerService::AudioOutput::flush()
1827{
Steve Block3856b092011-10-20 11:56:00 +01001828 ALOGV("flush");
Glenn Kasten2799d742013-05-30 14:33:29 -07001829 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001830}
1831
1832void MediaPlayerService::AudioOutput::pause()
1833{
Steve Block3856b092011-10-20 11:56:00 +01001834 ALOGV("pause");
Glenn Kasten2799d742013-05-30 14:33:29 -07001835 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001836}
1837
1838void MediaPlayerService::AudioOutput::close()
1839{
Steve Block3856b092011-10-20 11:56:00 +01001840 ALOGV("close");
Glenn Kasten2799d742013-05-30 14:33:29 -07001841 mTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001842}
1843
1844void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1845{
Steve Block3856b092011-10-20 11:56:00 +01001846 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001847 mLeftVolume = left;
1848 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07001849 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001850 mTrack->setVolume(left, right);
1851 }
1852}
1853
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001854status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1855{
1856 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1857 status_t res = NO_ERROR;
Glenn Kasten2799d742013-05-30 14:33:29 -07001858 if (mTrack != 0) {
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001859 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1860 } else {
1861 res = NO_INIT;
1862 }
1863 mPlaybackRatePermille = ratePermille;
1864 if (mSampleRateHz != 0) {
1865 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1866 }
1867 return res;
1868}
1869
Eric Laurent2beeb502010-07-16 07:43:46 -07001870status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1871{
Steve Block3856b092011-10-20 11:56:00 +01001872 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001873 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07001874 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001875 return mTrack->setAuxEffectSendLevel(level);
1876 }
1877 return NO_ERROR;
1878}
1879
1880status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1881{
Steve Block3856b092011-10-20 11:56:00 +01001882 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001883 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07001884 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001885 return mTrack->attachAuxEffect(effectId);
1886 }
1887 return NO_ERROR;
1888}
1889
Andreas Huber20111aa2009-07-14 16:56:47 -07001890// static
1891void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001892 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001893 //ALOGV("callbackwrapper");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001894 CallbackData *data = (CallbackData*)cookie;
1895 data->lock();
1896 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001897 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001898 if (me == NULL) {
1899 // no output set, likely because the track was scheduled to be reused
1900 // by another player, but the format turned out to be incompatible.
1901 data->unlock();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001902 if (buffer != NULL) {
1903 buffer->size = 0;
1904 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001905 return;
1906 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001907
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001908 switch(event) {
1909 case AudioTrack::EVENT_MORE_DATA: {
1910 size_t actualSize = (*me->mCallback)(
1911 me, buffer->raw, buffer->size, me->mCallbackCookie,
1912 CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001913
aarti jadhav-gaikwadf2575572014-08-13 15:04:39 +05301914 if ((me->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0 &&
1915 actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001916 // We've reached EOS but the audio track is not stopped yet,
1917 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001918
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001919 memset(buffer->raw, 0, buffer->size);
1920 actualSize = buffer->size;
1921 }
1922
1923 buffer->size = actualSize;
1924 } break;
1925
1926
1927 case AudioTrack::EVENT_STREAM_END:
1928 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
1929 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1930 me->mCallbackCookie, CB_EVENT_STREAM_END);
1931 break;
1932
1933 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
1934 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
1935 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1936 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
1937 break;
1938
1939 default:
1940 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001941 }
1942
Marco Nelissen6b74d672012-02-28 16:07:44 -08001943 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001944}
1945
Marco Nelissen4110c102012-03-29 09:31:28 -07001946int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001947{
1948 return mSessionId;
1949}
1950
Eric Laurent6f59db12013-07-26 17:16:50 -07001951uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
1952{
1953 if (mTrack == 0) return 0;
1954 return mTrack->getSampleRate();
1955}
1956
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001957#undef LOG_TAG
1958#define LOG_TAG "AudioCache"
Eric Laurent3d00aa62013-09-24 09:53:27 -07001959MediaPlayerService::AudioCache::AudioCache(const sp<IMemoryHeap>& heap) :
1960 mHeap(heap), mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
Andy Hunga31335a2014-08-20 17:37:59 -07001961 mFrameSize(1), mError(NO_ERROR), mCommandComplete(false)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001962{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001963}
1964
1965uint32_t MediaPlayerService::AudioCache::latency () const
1966{
1967 return 0;
1968}
1969
1970float MediaPlayerService::AudioCache::msecsPerFrame() const
1971{
1972 return mMsecsPerFrame;
1973}
1974
Marco Nelissen4110c102012-03-29 09:31:28 -07001975status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001976{
1977 if (position == 0) return BAD_VALUE;
Andy Hunga31335a2014-08-20 17:37:59 -07001978 *position = mSize / mFrameSize;
Eric Laurent342e9cf2010-01-19 17:37:09 -08001979 return NO_ERROR;
1980}
1981
Lajos Molnar06ad1522014-08-28 07:27:44 -07001982status_t MediaPlayerService::AudioCache::getTimestamp(AudioTimestamp &ts) const
1983{
1984 ts.mPosition = mSize / mFrameSize;
1985 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
1986 ts.mTime.tv_sec = now / 1000000000LL;
1987 ts.mTime.tv_nsec = now - (1000000000LL * ts.mTime.tv_sec);
1988 return NO_ERROR;
1989}
1990
Marco Nelissen4110c102012-03-29 09:31:28 -07001991status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
1992{
1993 if (written == 0) return BAD_VALUE;
Andy Hunga31335a2014-08-20 17:37:59 -07001994 *written = mSize / mFrameSize;
Marco Nelissen4110c102012-03-29 09:31:28 -07001995 return NO_ERROR;
1996}
1997
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001998////////////////////////////////////////////////////////////////////////////////
1999
2000struct CallbackThread : public Thread {
2001 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
2002 MediaPlayerBase::AudioSink::AudioCallback cb,
2003 void *cookie);
2004
2005protected:
2006 virtual ~CallbackThread();
2007
2008 virtual bool threadLoop();
2009
2010private:
2011 wp<MediaPlayerBase::AudioSink> mSink;
2012 MediaPlayerBase::AudioSink::AudioCallback mCallback;
2013 void *mCookie;
2014 void *mBuffer;
2015 size_t mBufferSize;
2016
2017 CallbackThread(const CallbackThread &);
2018 CallbackThread &operator=(const CallbackThread &);
2019};
2020
2021CallbackThread::CallbackThread(
2022 const wp<MediaPlayerBase::AudioSink> &sink,
2023 MediaPlayerBase::AudioSink::AudioCallback cb,
2024 void *cookie)
2025 : mSink(sink),
2026 mCallback(cb),
2027 mCookie(cookie),
2028 mBuffer(NULL),
2029 mBufferSize(0) {
2030}
2031
2032CallbackThread::~CallbackThread() {
2033 if (mBuffer) {
2034 free(mBuffer);
2035 mBuffer = NULL;
2036 }
2037}
2038
2039bool CallbackThread::threadLoop() {
2040 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
2041 if (sink == NULL) {
2042 return false;
2043 }
2044
2045 if (mBuffer == NULL) {
2046 mBufferSize = sink->bufferSize();
2047 mBuffer = malloc(mBufferSize);
2048 }
2049
2050 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002051 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
2052 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002053
2054 if (actualSize > 0) {
2055 sink->write(mBuffer, actualSize);
Andy Hunga31335a2014-08-20 17:37:59 -07002056 // Could return false on sink->write() error or short count.
2057 // Not necessarily appropriate but would work for AudioCache behavior.
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002058 }
2059
2060 return true;
2061}
2062
2063////////////////////////////////////////////////////////////////////////////////
2064
Andreas Huber20111aa2009-07-14 16:56:47 -07002065status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08002066 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
2067 audio_format_t format, int bufferCount,
Mark Salyzyn77342f72014-06-18 16:31:32 -07002068 AudioCallback cb, void *cookie, audio_output_flags_t /*flags*/,
2069 const audio_offload_info_t* /*offloadInfo*/)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002070{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08002071 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08002072 if (mHeap->getHeapID() < 0) {
2073 return NO_INIT;
2074 }
Andreas Huber20111aa2009-07-14 16:56:47 -07002075
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002076 mSampleRate = sampleRate;
2077 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08002078 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002079 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andy Hunga31335a2014-08-20 17:37:59 -07002080 mFrameSize = audio_is_linear_pcm(mFormat)
2081 ? mChannelCount * audio_bytes_per_sample(mFormat) : 1;
2082 mFrameCount = mHeap->getSize() / mFrameSize;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002083
2084 if (cb != NULL) {
2085 mCallbackThread = new CallbackThread(this, cb, cookie);
2086 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002087 return NO_ERROR;
2088}
2089
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002090status_t MediaPlayerService::AudioCache::start() {
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002091 if (mCallbackThread != NULL) {
2092 mCallbackThread->run("AudioCache callback");
2093 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002094 return NO_ERROR;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002095}
2096
2097void MediaPlayerService::AudioCache::stop() {
2098 if (mCallbackThread != NULL) {
2099 mCallbackThread->requestExitAndWait();
2100 }
2101}
2102
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002103ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
2104{
Steve Block3856b092011-10-20 11:56:00 +01002105 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002106 if ((buffer == 0) || (size == 0)) return size;
2107
2108 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
2109 if (p == NULL) return NO_INIT;
2110 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01002111 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
Andy Hunga31335a2014-08-20 17:37:59 -07002112
2113 bool overflow = mSize + size > mHeap->getSize();
2114 if (overflow) {
Steve Block29357bc2012-01-06 19:20:56 +00002115 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002116 size = mHeap->getSize() - mSize;
2117 }
Andy Hunga31335a2014-08-20 17:37:59 -07002118 size -= size % mFrameSize; // consume only integral amounts of frame size
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002119 memcpy(p, buffer, size);
2120 mSize += size;
Andy Hunga31335a2014-08-20 17:37:59 -07002121
2122 if (overflow) {
2123 // Signal heap filled here (last frame may be truncated).
2124 // After this point, no more data should be written as the
2125 // heap is filled and the AudioCache should be effectively
2126 // immutable with respect to future writes.
2127 //
2128 // It is thus safe for another thread to read the AudioCache.
2129 mCommandComplete = true;
2130 mSignal.signal();
2131 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002132 return size;
2133}
2134
2135// call with lock held
2136status_t MediaPlayerService::AudioCache::wait()
2137{
2138 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08002139 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002140 mSignal.wait(mLock);
2141 }
2142 mCommandComplete = false;
2143
2144 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01002145 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002146 } else {
Steve Block3856b092011-10-20 11:56:00 +01002147 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002148 }
2149 return mError;
2150}
2151
Gloria Wangb483c472011-04-11 17:23:27 -07002152void MediaPlayerService::AudioCache::notify(
Mark Salyzyn77342f72014-06-18 16:31:32 -07002153 void* cookie, int msg, int ext1, int ext2, const Parcel* /*obj*/)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002154{
Steve Block3856b092011-10-20 11:56:00 +01002155 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002156 AudioCache* p = static_cast<AudioCache*>(cookie);
2157
2158 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08002159 switch (msg)
2160 {
2161 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00002162 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002163 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08002164 break;
2165 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01002166 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08002167 break;
2168 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01002169 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08002170 break;
2171 default:
Steve Block3856b092011-10-20 11:56:00 +01002172 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08002173 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002174 }
2175
2176 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08002177 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002178 p->mCommandComplete = true;
2179 p->mSignal.signal();
2180}
2181
Marco Nelissen4110c102012-03-29 09:31:28 -07002182int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07002183{
2184 return 0;
2185}
2186
Eric Laurent6f59db12013-07-26 17:16:50 -07002187uint32_t MediaPlayerService::AudioCache::getSampleRate() const
2188{
2189 if (mMsecsPerFrame == 0) {
2190 return 0;
2191 }
2192 return (uint32_t)(1.e3 / mMsecsPerFrame);
2193}
2194
Gloria Wang7cf180c2011-02-19 18:37:57 -08002195void MediaPlayerService::addBatteryData(uint32_t params)
2196{
2197 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002198
2199 int32_t time = systemTime() / 1000000L;
2200
2201 // change audio output devices. This notification comes from AudioFlinger
2202 if ((params & kBatteryDataSpeakerOn)
2203 || (params & kBatteryDataOtherAudioDeviceOn)) {
2204
2205 int deviceOn[NUM_AUDIO_DEVICES];
2206 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2207 deviceOn[i] = 0;
2208 }
2209
2210 if ((params & kBatteryDataSpeakerOn)
2211 && (params & kBatteryDataOtherAudioDeviceOn)) {
2212 deviceOn[SPEAKER_AND_OTHER] = 1;
2213 } else if (params & kBatteryDataSpeakerOn) {
2214 deviceOn[SPEAKER] = 1;
2215 } else {
2216 deviceOn[OTHER_AUDIO_DEVICE] = 1;
2217 }
2218
2219 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2220 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
2221
2222 if (mBatteryAudio.refCount > 0) { // if playing audio
2223 if (!deviceOn[i]) {
2224 mBatteryAudio.lastTime[i] += time;
2225 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2226 mBatteryAudio.lastTime[i] = 0;
2227 } else {
2228 mBatteryAudio.lastTime[i] = 0 - time;
2229 }
2230 }
2231
2232 mBatteryAudio.deviceOn[i] = deviceOn[i];
2233 }
2234 }
2235 return;
2236 }
2237
2238 // an sudio stream is started
2239 if (params & kBatteryDataAudioFlingerStart) {
2240 // record the start time only if currently no other audio
2241 // is being played
2242 if (mBatteryAudio.refCount == 0) {
2243 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2244 if (mBatteryAudio.deviceOn[i]) {
2245 mBatteryAudio.lastTime[i] -= time;
2246 }
2247 }
2248 }
2249
2250 mBatteryAudio.refCount ++;
2251 return;
2252
2253 } else if (params & kBatteryDataAudioFlingerStop) {
2254 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002255 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002256 return;
2257 }
2258
2259 // record the stop time only if currently this is the only
2260 // audio being played
2261 if (mBatteryAudio.refCount == 1) {
2262 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2263 if (mBatteryAudio.deviceOn[i]) {
2264 mBatteryAudio.lastTime[i] += time;
2265 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2266 mBatteryAudio.lastTime[i] = 0;
2267 }
2268 }
2269 }
2270
2271 mBatteryAudio.refCount --;
2272 return;
2273 }
2274
Gloria Wang7cf180c2011-02-19 18:37:57 -08002275 int uid = IPCThreadState::self()->getCallingUid();
2276 if (uid == AID_MEDIA) {
2277 return;
2278 }
2279 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08002280
2281 if (index < 0) { // create a new entry for this UID
2282 BatteryUsageInfo info;
2283 info.audioTotalTime = 0;
2284 info.videoTotalTime = 0;
2285 info.audioLastTime = 0;
2286 info.videoLastTime = 0;
2287 info.refCount = 0;
2288
Gloria Wang9ee159b2011-02-24 14:51:45 -08002289 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00002290 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002291 return;
2292 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08002293 }
2294
2295 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
2296
2297 if (params & kBatteryDataCodecStarted) {
2298 if (params & kBatteryDataTrackAudio) {
2299 info.audioLastTime -= time;
2300 info.refCount ++;
2301 }
2302 if (params & kBatteryDataTrackVideo) {
2303 info.videoLastTime -= time;
2304 info.refCount ++;
2305 }
2306 } else {
2307 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002308 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002309 return;
2310 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002311 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002312 mBatteryData.removeItem(uid);
2313 return;
2314 }
2315
2316 if (params & kBatteryDataTrackAudio) {
2317 info.audioLastTime += time;
2318 info.refCount --;
2319 }
2320 if (params & kBatteryDataTrackVideo) {
2321 info.videoLastTime += time;
2322 info.refCount --;
2323 }
2324
2325 // no stream is being played by this UID
2326 if (info.refCount == 0) {
2327 info.audioTotalTime += info.audioLastTime;
2328 info.audioLastTime = 0;
2329 info.videoTotalTime += info.videoLastTime;
2330 info.videoLastTime = 0;
2331 }
2332 }
2333}
2334
2335status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2336 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002337
2338 // audio output devices usage
2339 int32_t time = systemTime() / 1000000L; //in ms
2340 int32_t totalTime;
2341
2342 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2343 totalTime = mBatteryAudio.totalTime[i];
2344
2345 if (mBatteryAudio.deviceOn[i]
2346 && (mBatteryAudio.lastTime[i] != 0)) {
2347 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2348 totalTime += tmpTime;
2349 }
2350
2351 reply->writeInt32(totalTime);
2352 // reset the total time
2353 mBatteryAudio.totalTime[i] = 0;
2354 }
2355
2356 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002357 BatteryUsageInfo info;
2358 int size = mBatteryData.size();
2359
2360 reply->writeInt32(size);
2361 int i = 0;
2362
2363 while (i < size) {
2364 info = mBatteryData.valueAt(i);
2365
2366 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2367 reply->writeInt32(info.audioTotalTime);
2368 reply->writeInt32(info.videoTotalTime);
2369
2370 info.audioTotalTime = 0;
2371 info.videoTotalTime = 0;
2372
2373 // remove the UID entry where no stream is being played
2374 if (info.refCount <= 0) {
2375 mBatteryData.removeItemsAt(i);
2376 size --;
2377 i --;
2378 }
2379 i++;
2380 }
2381 return NO_ERROR;
2382}
nikoa64c8c72009-07-20 15:07:26 -07002383} // namespace android