blob: 65080b047132cacb31446067fd603597b3c3290d [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
Wei Jia502c2f42017-07-13 17:47:56 -070086static const int kDumpLockRetries = 50;
87static const int kDumpLockSleepUs = 20000;
88
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070089namespace {
nikoa64c8c72009-07-20 15:07:26 -070090using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070091using android::status_t;
92using android::OK;
93using android::BAD_VALUE;
94using android::NOT_ENOUGH_DATA;
95using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070096
97// Max number of entries in the filter.
98const int kMaxFilterSize = 64; // I pulled that out of thin air.
99
nikoa64c8c72009-07-20 15:07:26 -0700100// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -0700101
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700102
103// Unmarshall a filter from a Parcel.
104// Filter format in a parcel:
105//
106// 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
107// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108// | number of entries (n) |
109// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110// | metadata type 1 |
111// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112// | metadata type 2 |
113// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114// ....
115// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116// | metadata type n |
117// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
118//
119// @param p Parcel that should start with a filter.
120// @param[out] filter On exit contains the list of metadata type to be
121// filtered.
122// @param[out] status On exit contains the status code to be returned.
123// @return true if the parcel starts with a valid filter.
124bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700125 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700126 status_t *status)
127{
Nicolas Catania48290382009-07-10 13:53:06 -0700128 int32_t val;
129 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700130 {
Steve Block29357bc2012-01-06 19:20:56 +0000131 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700132 *status = NOT_ENOUGH_DATA;
133 return false;
134 }
135
Nicolas Catania48290382009-07-10 13:53:06 -0700136 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700137 {
Steve Block29357bc2012-01-06 19:20:56 +0000138 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700139 *status = BAD_VALUE;
140 return false;
141 }
142
Nicolas Catania48290382009-07-10 13:53:06 -0700143 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700144
145 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700146 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700147
nikoa64c8c72009-07-20 15:07:26 -0700148 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700149
Nicolas Catania48290382009-07-10 13:53:06 -0700150
151 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700152 {
Steve Block29357bc2012-01-06 19:20:56 +0000153 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700154 *status = NOT_ENOUGH_DATA;
155 return false;
156 }
157
nikoa64c8c72009-07-20 15:07:26 -0700158 const Metadata::Type *data =
159 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700160
Nicolas Catania48290382009-07-10 13:53:06 -0700161 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700162 {
Steve Block29357bc2012-01-06 19:20:56 +0000163 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700164 *status = BAD_VALUE;
165 return false;
166 }
167
168 // TODO: The stl impl of vector would be more efficient here
169 // because it degenerates into a memcpy on pod types. Try to
170 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700171 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700172 {
Nicolas Catania48290382009-07-10 13:53:06 -0700173 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700174 ++data;
175 }
176 *status = OK;
177 return true;
178}
179
Nicolas Catania48290382009-07-10 13:53:06 -0700180// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700181// @param val To be searched.
182// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700183bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700184{
185 // Deal with empty and ANY right away
186 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700187 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700188
Nicolas Catania48290382009-07-10 13:53:06 -0700189 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700190}
191
192} // anonymous namespace
193
194
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700195namespace {
196using android::Parcel;
197using android::String16;
198
199// marshalling tag indicating flattened utf16 tags
200// keep in sync with frameworks/base/media/java/android/media/AudioAttributes.java
201const int32_t kAudioAttributesMarshallTagFlattenTags = 1;
202
203// Audio attributes format in a parcel:
204//
205// 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
206// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
207// | usage |
208// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
209// | content_type |
210// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Hyejin Kim4f418f92014-09-05 15:50:03 +0900211// | source |
212// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700213// | flags |
214// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
215// | kAudioAttributesMarshallTagFlattenTags | // ignore tags if not found
216// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
217// | flattened tags in UTF16 |
218// | ... |
219// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
220//
221// @param p Parcel that contains audio attributes.
222// @param[out] attributes On exit points to an initialized audio_attributes_t structure
223// @param[out] status On exit contains the status code to be returned.
224void unmarshallAudioAttributes(const Parcel& parcel, audio_attributes_t *attributes)
225{
226 attributes->usage = (audio_usage_t) parcel.readInt32();
227 attributes->content_type = (audio_content_type_t) parcel.readInt32();
Hyejin Kim4f418f92014-09-05 15:50:03 +0900228 attributes->source = (audio_source_t) parcel.readInt32();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700229 attributes->flags = (audio_flags_mask_t) parcel.readInt32();
230 const bool hasFlattenedTag = (parcel.readInt32() == kAudioAttributesMarshallTagFlattenTags);
231 if (hasFlattenedTag) {
232 // the tags are UTF16, convert to UTF8
233 String16 tags = parcel.readString16();
234 ssize_t realTagSize = utf16_to_utf8_length(tags.string(), tags.size());
235 if (realTagSize <= 0) {
236 strcpy(attributes->tags, "");
237 } else {
238 // copy the flattened string into the attributes as the destination for the conversion:
239 // copying array size -1, array for tags was calloc'd, no need to NULL-terminate it
240 size_t tagSize = realTagSize > AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 ?
241 AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 : realTagSize;
Sergio Giro804c3b52016-06-28 18:24:52 +0100242 utf16_to_utf8(tags.string(), tagSize, attributes->tags,
243 sizeof(attributes->tags) / sizeof(attributes->tags[0]));
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700244 }
245 } else {
246 ALOGE("unmarshallAudioAttributes() received unflattened tags, ignoring tag values");
247 strcpy(attributes->tags, "");
248 }
249}
250} // anonymous namespace
251
252
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800253namespace android {
254
Dave Burked681bbb2011-08-30 14:39:17 +0100255static bool checkPermission(const char* permissionString) {
256#ifndef HAVE_ANDROID_OS
257 return true;
258#endif
259 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
260 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000261 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100262 return ok;
263}
264
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800265// 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 -0800266/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
267/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
268
269void MediaPlayerService::instantiate() {
270 defaultServiceManager()->addService(
271 String16("media.player"), new MediaPlayerService());
272}
273
274MediaPlayerService::MediaPlayerService()
275{
Steve Block3856b092011-10-20 11:56:00 +0100276 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800277 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800278
279 mBatteryAudio.refCount = 0;
280 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
281 mBatteryAudio.deviceOn[i] = 0;
282 mBatteryAudio.lastTime[i] = 0;
283 mBatteryAudio.totalTime[i] = 0;
284 }
285 // speaker is on by default
286 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700287
Chong Zhang0b30fd42014-07-23 14:46:05 -0700288 // reset battery stats
289 // if the mediaserver has crashed, battery stats could be left
290 // in bad state, reset the state upon service start.
291 const sp<IServiceManager> sm(defaultServiceManager());
292 if (sm != NULL) {
293 const String16 name("batterystats");
294 sp<IBatteryStats> batteryStats =
295 interface_cast<IBatteryStats>(sm->getService(name));
296 if (batteryStats != NULL) {
297 batteryStats->noteResetVideo();
298 batteryStats->noteResetAudio();
299 }
300 }
301
John Grossman44a7e422012-06-21 17:29:24 -0700302 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303}
304
305MediaPlayerService::~MediaPlayerService()
306{
Steve Block3856b092011-10-20 11:56:00 +0100307 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800308}
309
Glenn Kastenf37971f2012-02-03 11:06:53 -0800310sp<IMediaRecorder> MediaPlayerService::createMediaRecorder()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800311{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800312 pid_t pid = IPCThreadState::self()->getCallingPid();
Gloria Wangdac6a312009-10-29 15:46:37 -0700313 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
314 wp<MediaRecorderClient> w = recorder;
315 Mutex::Autolock lock(mLock);
316 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100317 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800318 return recorder;
319}
320
Gloria Wangdac6a312009-10-29 15:46:37 -0700321void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
322{
323 Mutex::Autolock lock(mLock);
324 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100325 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700326}
327
Glenn Kastenf37971f2012-02-03 11:06:53 -0800328sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800329{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800330 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800331 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100332 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800333 return retriever;
334}
335
Glenn Kastenf37971f2012-02-03 11:06:53 -0800336sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100337 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800338{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800339 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800340 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700341
342 sp<Client> c = new Client(
343 this, pid, connId, client, audioSessionId,
344 IPCThreadState::self()->getCallingUid());
345
Steve Block3856b092011-10-20 11:56:00 +0100346 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100347 IPCThreadState::self()->getCallingUid());
348
349 wp<Client> w = c;
350 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800351 Mutex::Autolock lock(mLock);
352 mClients.add(w);
353 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800354 return c;
355}
356
Lajos Molnar1381d4b2014-08-07 15:18:35 -0700357sp<IMediaCodecList> MediaPlayerService::getCodecList() const {
358 return MediaCodecList::getLocalInstance();
359}
360
Andreas Huber318ad9c2009-10-15 13:46:54 -0700361sp<IOMX> MediaPlayerService::getOMX() {
362 Mutex::Autolock autoLock(mLock);
363
364 if (mOMX.get() == NULL) {
365 mOMX = new OMX;
366 }
367
368 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700369}
370
Andreas Hubered3e3e02012-03-26 11:13:27 -0700371sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700372 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700373}
374
Jeff Tinkercc82dc62013-02-08 10:18:35 -0800375sp<IDrm> MediaPlayerService::makeDrm() {
376 return new Drm;
377}
378
Andreas Huber279dcd82013-01-30 10:41:25 -0800379sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
380 return new HDCP(createEncryptionModule);
Andreas Huber59451f82012-09-18 10:36:32 -0700381}
382
Jeff Brown2013a542012-09-04 21:38:42 -0700383sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
384 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownaba33d52012-09-07 17:38:58 -0700385 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
386 return NULL;
387 }
388
Jeff Brownced24b32012-09-05 17:48:03 -0700389 return new RemoteDisplay(client, iface.string());
Jeff Brown2013a542012-09-04 21:38:42 -0700390}
391
Mark Salyzyn77342f72014-06-18 16:31:32 -0700392status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& /*args*/) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393{
394 const size_t SIZE = 256;
395 char buffer[SIZE];
396 String8 result;
397
398 result.append(" AudioCache\n");
399 if (mHeap != 0) {
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000400 snprintf(buffer, 255, " heap base(%p), size(%zu), flags(%d)\n",
Eric Laurent3d00aa62013-09-24 09:53:27 -0700401 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800402 result.append(buffer);
403 }
Scott Fan7d409692013-04-28 10:13:54 +0800404 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 -0800405 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
406 result.append(buffer);
407 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
408 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
409 result.append(buffer);
410 ::write(fd, result.string(), result.size());
411 return NO_ERROR;
412}
413
414status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
415{
416 const size_t SIZE = 256;
417 char buffer[SIZE];
418 String8 result;
419
420 result.append(" AudioOutput\n");
421 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
422 mStreamType, mLeftVolume, mRightVolume);
423 result.append(buffer);
424 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800425 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800426 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700427 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
428 mAuxEffectId, mSendLevel);
429 result.append(buffer);
430
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800431 ::write(fd, result.string(), result.size());
432 if (mTrack != 0) {
433 mTrack->dump(fd, args);
434 }
435 return NO_ERROR;
436}
437
438status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
439{
440 const size_t SIZE = 256;
441 char buffer[SIZE];
442 String8 result;
443 result.append(" Client\n");
444 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
445 mPid, mConnId, mStatus, mLoop?"true": "false");
446 result.append(buffer);
Wei Jia502c2f42017-07-13 17:47:56 -0700447
448 sp<MediaPlayerBase> p;
449 sp<AudioOutput> audioOutput;
450 bool locked = false;
451 for (int i = 0; i < kDumpLockRetries; ++i) {
452 if (mLock.tryLock() == NO_ERROR) {
453 locked = true;
454 break;
455 }
456 usleep(kDumpLockSleepUs);
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700457 }
Wei Jia502c2f42017-07-13 17:47:56 -0700458
459 if (locked) {
460 p = mPlayer;
461 audioOutput = mAudioOutput;
462 mLock.unlock();
463 } else {
464 result.append(" lock is taken, no dump from player and audio output\n");
465 }
466 write(fd, result.string(), result.size());
467
468 if (p != NULL) {
469 p->dump(fd, args);
470 }
471 if (audioOutput != 0) {
472 audioOutput->dump(fd, args);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800473 }
474 write(fd, "\n", 1);
475 return NO_ERROR;
476}
477
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800478status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
479{
480 const size_t SIZE = 256;
481 char buffer[SIZE];
482 String8 result;
483 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
484 snprintf(buffer, SIZE, "Permission Denial: "
485 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
486 IPCThreadState::self()->getCallingPid(),
487 IPCThreadState::self()->getCallingUid());
488 result.append(buffer);
489 } else {
490 Mutex::Autolock lock(mLock);
491 for (int i = 0, n = mClients.size(); i < n; ++i) {
492 sp<Client> c = mClients[i].promote();
493 if (c != 0) c->dump(fd, args);
494 }
James Dongb9141222010-07-08 11:16:11 -0700495 if (mMediaRecorderClients.size() == 0) {
496 result.append(" No media recorder client\n\n");
497 } else {
498 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
499 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700500 if (c != 0) {
501 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
502 result.append(buffer);
503 write(fd, result.string(), result.size());
504 result = "\n";
505 c->dump(fd, args);
506 }
James Dongb9141222010-07-08 11:16:11 -0700507 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700508 }
509
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800510 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700511 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800512 FILE *f = fopen(buffer, "r");
513 if (f) {
514 while (!feof(f)) {
515 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700516 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800517 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700518 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800519 strstr(buffer, " /system/media/")) {
520 result.append(" ");
521 result.append(buffer);
522 }
523 }
524 fclose(f);
525 } else {
526 result.append("couldn't open ");
527 result.append(buffer);
528 result.append("\n");
529 }
530
Glenn Kasten0512ab52011-05-04 17:58:57 -0700531 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800532 DIR *d = opendir(buffer);
533 if (d) {
534 struct dirent *ent;
535 while((ent = readdir(d)) != NULL) {
536 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700537 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800538 struct stat s;
539 if (lstat(buffer, &s) == 0) {
540 if ((s.st_mode & S_IFMT) == S_IFLNK) {
541 char linkto[256];
542 int len = readlink(buffer, linkto, sizeof(linkto));
543 if(len > 0) {
544 if(len > 255) {
545 linkto[252] = '.';
546 linkto[253] = '.';
547 linkto[254] = '.';
548 linkto[255] = 0;
549 } else {
550 linkto[len] = 0;
551 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700552 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800553 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700554 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555 strstr(linkto, "/system/media/") == linkto) {
556 result.append(" ");
557 result.append(buffer);
558 result.append(" -> ");
559 result.append(linkto);
560 result.append("\n");
561 }
562 }
563 } else {
564 result.append(" unexpected type for ");
565 result.append(buffer);
566 result.append("\n");
567 }
568 }
569 }
570 }
571 closedir(d);
572 } else {
573 result.append("couldn't open ");
574 result.append(buffer);
575 result.append("\n");
576 }
577
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800578 bool dumpMem = false;
579 for (size_t i = 0; i < args.size(); i++) {
580 if (args[i] == String16("-m")) {
581 dumpMem = true;
582 }
583 }
584 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700585 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800586 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800587 }
588 write(fd, result.string(), result.size());
589 return NO_ERROR;
590}
591
592void MediaPlayerService::removeClient(wp<Client> client)
593{
594 Mutex::Autolock lock(mLock);
595 mClients.remove(client);
596}
597
Robert Shihee0a0e32016-08-16 16:50:54 -0700598bool MediaPlayerService::hasClient(wp<Client> client)
599{
600 Mutex::Autolock lock(mLock);
601 return mClients.indexOf(client) != NAME_NOT_FOUND;
602}
603
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700604MediaPlayerService::Client::Client(
605 const sp<MediaPlayerService>& service, pid_t pid,
606 int32_t connId, const sp<IMediaPlayerClient>& client,
607 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800608{
Steve Block3856b092011-10-20 11:56:00 +0100609 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800610 mPid = pid;
611 mConnId = connId;
612 mService = service;
613 mClient = client;
614 mLoop = false;
615 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700616 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700617 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800618 mRetransmitEndpointValid = false;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700619 mAudioAttributes = NULL;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700620
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800621#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000622 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800623 mAntagonizer = new Antagonizer(notify, this);
624#endif
625}
626
627MediaPlayerService::Client::~Client()
628{
Steve Block3856b092011-10-20 11:56:00 +0100629 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
Wei Jia502c2f42017-07-13 17:47:56 -0700630 {
631 Mutex::Autolock l(mLock);
632 mAudioOutput.clear();
633 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800634 wp<Client> client(this);
635 disconnect();
636 mService->removeClient(client);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700637 if (mAudioAttributes != NULL) {
638 free(mAudioAttributes);
639 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800640}
641
642void MediaPlayerService::Client::disconnect()
643{
Steve Block3856b092011-10-20 11:56:00 +0100644 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800645 // grab local reference and clear main reference to prevent future
646 // access to object
647 sp<MediaPlayerBase> p;
648 {
649 Mutex::Autolock l(mLock);
650 p = mPlayer;
beanzdcfefde2012-11-05 09:51:43 +0800651 mClient.clear();
Wei Jia502c2f42017-07-13 17:47:56 -0700652 mPlayer.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800653 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700654
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800655 // clear the notification to prevent callbacks to dead client
656 // and reset the player. We assume the player will serialize
657 // access to itself if necessary.
658 if (p != 0) {
659 p->setNotifyCallback(0, 0);
660#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000661 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800662 mAntagonizer->kill();
663#endif
664 p->reset();
665 }
666
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700667 disconnectNativeWindow();
668
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800669 IPCThreadState::self()->flushCommands();
670}
671
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800672sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
673{
674 // determine if we have the right player type
Wei Jia502c2f42017-07-13 17:47:56 -0700675 sp<MediaPlayerBase> p = getPlayer();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800676 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100677 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800678 p.clear();
679 }
680 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700681 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800682 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700683
Jason Simmonsdb29e522011-08-12 13:46:55 -0700684 if (p != NULL) {
685 p->setUID(mUID);
686 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700687
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800688 return p;
689}
690
John Grossmanc795b642012-02-22 15:38:35 -0800691sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
692 player_type playerType)
693{
694 ALOGV("player type = %d", playerType);
695
696 // create the right type of player
697 sp<MediaPlayerBase> p = createPlayer(playerType);
698 if (p == NULL) {
699 return p;
700 }
701
702 if (!p->hardwareOutput()) {
Marco Nelissend457c972014-02-11 08:47:07 -0800703 mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(),
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700704 mPid, mAudioAttributes);
John Grossmanc795b642012-02-22 15:38:35 -0800705 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
706 }
707
708 return p;
709}
710
711void MediaPlayerService::Client::setDataSource_post(
712 const sp<MediaPlayerBase>& p,
713 status_t status)
714{
715 ALOGV(" setDataSource");
716 mStatus = status;
717 if (mStatus != OK) {
718 ALOGE(" error: %d", mStatus);
719 return;
720 }
721
722 // Set the re-transmission endpoint if one was chosen.
723 if (mRetransmitEndpointValid) {
724 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
725 if (mStatus != NO_ERROR) {
726 ALOGE("setRetransmitEndpoint error: %d", mStatus);
727 }
728 }
729
730 if (mStatus == OK) {
Wei Jia502c2f42017-07-13 17:47:56 -0700731 Mutex::Autolock l(mLock);
John Grossmanc795b642012-02-22 15:38:35 -0800732 mPlayer = p;
733 }
734}
735
Andreas Huber2db84552010-01-28 11:19:57 -0800736status_t MediaPlayerService::Client::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800737 const sp<IMediaHTTPService> &httpService,
738 const char *url,
739 const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800740{
Steve Block3856b092011-10-20 11:56:00 +0100741 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800742 if (url == NULL)
743 return UNKNOWN_ERROR;
744
Dave Burked681bbb2011-08-30 14:39:17 +0100745 if ((strncmp(url, "http://", 7) == 0) ||
746 (strncmp(url, "https://", 8) == 0) ||
747 (strncmp(url, "rtsp://", 7) == 0)) {
748 if (!checkPermission("android.permission.INTERNET")) {
749 return PERMISSION_DENIED;
750 }
751 }
752
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800753 if (strncmp(url, "content://", 10) == 0) {
754 // get a filedescriptor for the content Uri and
755 // pass it to the setDataSource(fd) method
756
757 String16 url16(url);
758 int fd = android::openContentProviderFile(url16);
759 if (fd < 0)
760 {
Steve Block29357bc2012-01-06 19:20:56 +0000761 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800762 return UNKNOWN_ERROR;
763 }
764 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
765 close(fd);
766 return mStatus;
767 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700768 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800769 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
770 if (p == NULL) {
771 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800772 }
773
Andreas Huber1b86fe02014-01-29 11:13:26 -0800774 setDataSource_post(p, p->setDataSource(httpService, url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800775 return mStatus;
776 }
777}
778
779status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
780{
Steve Block3856b092011-10-20 11:56:00 +0100781 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800782 struct stat sb;
783 int ret = fstat(fd, &sb);
784 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000785 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800786 return UNKNOWN_ERROR;
787 }
788
Steve Block3856b092011-10-20 11:56:00 +0100789 ALOGV("st_dev = %llu", sb.st_dev);
790 ALOGV("st_mode = %u", sb.st_mode);
Mark Salyzyn77342f72014-06-18 16:31:32 -0700791 ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
792 ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
Steve Block3856b092011-10-20 11:56:00 +0100793 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800794
795 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000796 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800797 ::close(fd);
798 return UNKNOWN_ERROR;
799 }
800 if (offset + length > sb.st_size) {
801 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100802 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800803 }
804
John Grossman44a7e422012-06-21 17:29:24 -0700805 player_type playerType = MediaPlayerFactory::getPlayerType(this,
806 fd,
807 offset,
808 length);
John Grossmanc795b642012-02-22 15:38:35 -0800809 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
810 if (p == NULL) {
811 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800812 }
813
814 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800815 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800816 return mStatus;
817}
818
Andreas Hubere2b10282010-11-23 11:41:34 -0800819status_t MediaPlayerService::Client::setDataSource(
820 const sp<IStreamSource> &source) {
821 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700822 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800823 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800824 if (p == NULL) {
825 return NO_INIT;
826 }
827
Andreas Hubere2b10282010-11-23 11:41:34 -0800828 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800829 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800830 return mStatus;
831}
832
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700833void MediaPlayerService::Client::disconnectNativeWindow() {
834 if (mConnectedWindow != NULL) {
835 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
836 NATIVE_WINDOW_API_MEDIA);
837
838 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000839 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700840 strerror(-err), err);
841 }
842 }
843 mConnectedWindow.clear();
844}
845
Glenn Kasten11731182011-02-08 17:26:17 -0800846status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800847 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800848{
Andy McFadden484566c2012-12-18 09:46:54 -0800849 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800850 sp<MediaPlayerBase> p = getPlayer();
851 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700852
Andy McFadden484566c2012-12-18 09:46:54 -0800853 sp<IBinder> binder(bufferProducer == NULL ? NULL :
854 bufferProducer->asBinder());
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700855 if (mConnectedWindowBinder == binder) {
856 return OK;
857 }
858
859 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -0800860 if (bufferProducer != NULL) {
Marco Nelissenee08f7e2013-09-16 13:30:01 -0700861 anw = new Surface(bufferProducer, true /* controlledByApp */);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700862 status_t err = native_window_api_connect(anw.get(),
863 NATIVE_WINDOW_API_MEDIA);
864
865 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000866 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700867 // Note that we must do the reset before disconnecting from the ANW.
868 // Otherwise queue/dequeue calls could be made on the disconnected
869 // ANW, which may result in errors.
870 reset();
871
872 disconnectNativeWindow();
873
874 return err;
875 }
876 }
877
Andy McFadden484566c2012-12-18 09:46:54 -0800878 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700879 // disconnecting the old one. Otherwise queue/dequeue calls could be made
880 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -0800881 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700882
883 disconnectNativeWindow();
884
885 mConnectedWindow = anw;
886
887 if (err == OK) {
888 mConnectedWindowBinder = binder;
889 } else {
890 disconnectNativeWindow();
891 }
892
893 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800894}
895
Nicolas Catania1d187f12009-05-12 23:25:55 -0700896status_t MediaPlayerService::Client::invoke(const Parcel& request,
897 Parcel *reply)
898{
899 sp<MediaPlayerBase> p = getPlayer();
900 if (p == NULL) return UNKNOWN_ERROR;
901 return p->invoke(request, reply);
902}
903
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700904// This call doesn't need to access the native player.
905status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
906{
907 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700908 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700909
Nicolas Catania48290382009-07-10 13:53:06 -0700910 if (unmarshallFilter(filter, &allow, &status) &&
911 unmarshallFilter(filter, &drop, &status)) {
912 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700913
914 mMetadataAllow = allow;
915 mMetadataDrop = drop;
916 }
917 return status;
918}
919
Nicolas Catania48290382009-07-10 13:53:06 -0700920status_t MediaPlayerService::Client::getMetadata(
Mark Salyzyn77342f72014-06-18 16:31:32 -0700921 bool update_only, bool /*apply_filter*/, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700922{
nikoa64c8c72009-07-20 15:07:26 -0700923 sp<MediaPlayerBase> player = getPlayer();
924 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700925
nikod608a812009-07-16 16:39:53 -0700926 status_t status;
927 // Placeholder for the return code, updated by the caller.
928 reply->writeInt32(-1);
929
nikoa64c8c72009-07-20 15:07:26 -0700930 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700931
932 // We don't block notifications while we fetch the data. We clear
933 // mMetadataUpdated first so we don't lose notifications happening
934 // during the rest of this call.
935 {
936 Mutex::Autolock lock(mLock);
937 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700938 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700939 }
940 mMetadataUpdated.clear();
941 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700942
nikoa64c8c72009-07-20 15:07:26 -0700943 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700944
nikoa64c8c72009-07-20 15:07:26 -0700945 metadata.appendHeader();
946 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700947
948 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700949 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000950 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700951 return status;
952 }
953
954 // FIXME: Implement filtering on the result. Not critical since
955 // filtering takes place on the update notifications already. This
956 // would be when all the metadata are fetch and a filter is set.
957
nikod608a812009-07-16 16:39:53 -0700958 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700959 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700960 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700961}
962
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800963status_t MediaPlayerService::Client::prepareAsync()
964{
Steve Block3856b092011-10-20 11:56:00 +0100965 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800966 sp<MediaPlayerBase> p = getPlayer();
967 if (p == 0) return UNKNOWN_ERROR;
968 status_t ret = p->prepareAsync();
969#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000970 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800971 if (ret == NO_ERROR) mAntagonizer->start();
972#endif
973 return ret;
974}
975
976status_t MediaPlayerService::Client::start()
977{
Steve Block3856b092011-10-20 11:56:00 +0100978 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800979 sp<MediaPlayerBase> p = getPlayer();
980 if (p == 0) return UNKNOWN_ERROR;
981 p->setLooping(mLoop);
982 return p->start();
983}
984
985status_t MediaPlayerService::Client::stop()
986{
Steve Block3856b092011-10-20 11:56:00 +0100987 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800988 sp<MediaPlayerBase> p = getPlayer();
989 if (p == 0) return UNKNOWN_ERROR;
990 return p->stop();
991}
992
993status_t MediaPlayerService::Client::pause()
994{
Steve Block3856b092011-10-20 11:56:00 +0100995 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800996 sp<MediaPlayerBase> p = getPlayer();
997 if (p == 0) return UNKNOWN_ERROR;
998 return p->pause();
999}
1000
1001status_t MediaPlayerService::Client::isPlaying(bool* state)
1002{
1003 *state = false;
1004 sp<MediaPlayerBase> p = getPlayer();
1005 if (p == 0) return UNKNOWN_ERROR;
1006 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +01001007 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001008 return NO_ERROR;
1009}
1010
1011status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
1012{
Steve Block3856b092011-10-20 11:56:00 +01001013 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001014 sp<MediaPlayerBase> p = getPlayer();
1015 if (p == 0) return UNKNOWN_ERROR;
1016 status_t ret = p->getCurrentPosition(msec);
1017 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001018 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001019 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001020 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001021 }
1022 return ret;
1023}
1024
1025status_t MediaPlayerService::Client::getDuration(int *msec)
1026{
Steve Block3856b092011-10-20 11:56:00 +01001027 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001028 sp<MediaPlayerBase> p = getPlayer();
1029 if (p == 0) return UNKNOWN_ERROR;
1030 status_t ret = p->getDuration(msec);
1031 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001032 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001033 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001034 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001035 }
1036 return ret;
1037}
1038
Marco Nelissen6b74d672012-02-28 16:07:44 -08001039status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
1040 ALOGV("setNextPlayer");
1041 Mutex::Autolock l(mLock);
1042 sp<Client> c = static_cast<Client*>(player.get());
Wei Jia28284122016-08-30 13:49:06 -07001043 if (c != NULL && !mService->hasClient(c)) {
Robert Shihee0a0e32016-08-16 16:50:54 -07001044 return BAD_VALUE;
1045 }
1046
Marco Nelissen6b74d672012-02-28 16:07:44 -08001047 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -07001048
1049 if (c != NULL) {
1050 if (mAudioOutput != NULL) {
1051 mAudioOutput->setNextOutput(c->mAudioOutput);
1052 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
1053 ALOGE("no current audio output");
1054 }
1055
1056 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
1057 mPlayer->setNextPlayer(mNextClient->getPlayer());
1058 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001059 }
John Grossman5f7e55e2012-08-24 14:47:25 -07001060
Marco Nelissen6b74d672012-02-28 16:07:44 -08001061 return OK;
1062}
1063
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001064status_t MediaPlayerService::Client::seekTo(int msec)
1065{
Steve Block3856b092011-10-20 11:56:00 +01001066 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001067 sp<MediaPlayerBase> p = getPlayer();
1068 if (p == 0) return UNKNOWN_ERROR;
1069 return p->seekTo(msec);
1070}
1071
1072status_t MediaPlayerService::Client::reset()
1073{
Steve Block3856b092011-10-20 11:56:00 +01001074 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -08001075 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001076 sp<MediaPlayerBase> p = getPlayer();
1077 if (p == 0) return UNKNOWN_ERROR;
1078 return p->reset();
1079}
1080
Glenn Kastenfff6d712012-01-12 16:38:12 -08001081status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001082{
Steve Block3856b092011-10-20 11:56:00 +01001083 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001084 // TODO: for hardware output, call player instead
1085 Mutex::Autolock l(mLock);
1086 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1087 return NO_ERROR;
1088}
1089
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001090status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel)
1091{
1092 if (mAudioAttributes != NULL) { free(mAudioAttributes); }
1093 mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1094 unmarshallAudioAttributes(parcel, mAudioAttributes);
1095
1096 ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
1097 mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
1098 mAudioAttributes->tags);
1099
1100 if (mAudioOutput != 0) {
1101 mAudioOutput->setAudioAttributes(mAudioAttributes);
1102 }
1103 return NO_ERROR;
1104}
1105
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001106status_t MediaPlayerService::Client::setLooping(int loop)
1107{
Steve Block3856b092011-10-20 11:56:00 +01001108 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001109 mLoop = loop;
1110 sp<MediaPlayerBase> p = getPlayer();
1111 if (p != 0) return p->setLooping(loop);
1112 return NO_ERROR;
1113}
1114
1115status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1116{
Steve Block3856b092011-10-20 11:56:00 +01001117 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -08001118
1119 // for hardware output, call player instead
1120 sp<MediaPlayerBase> p = getPlayer();
1121 {
1122 Mutex::Autolock l(mLock);
1123 if (p != 0 && p->hardwareOutput()) {
1124 MediaPlayerHWInterface* hwp =
1125 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1126 return hwp->setVolume(leftVolume, rightVolume);
1127 } else {
1128 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1129 return NO_ERROR;
1130 }
1131 }
1132
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001133 return NO_ERROR;
1134}
1135
Eric Laurent2beeb502010-07-16 07:43:46 -07001136status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1137{
Steve Block3856b092011-10-20 11:56:00 +01001138 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001139 Mutex::Autolock l(mLock);
1140 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1141 return NO_ERROR;
1142}
1143
1144status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1145{
Steve Block3856b092011-10-20 11:56:00 +01001146 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001147 Mutex::Autolock l(mLock);
1148 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1149 return NO_ERROR;
1150}
Nicolas Catania48290382009-07-10 13:53:06 -07001151
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001152status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001153 ALOGV("[%d] setParameter(%d)", mConnId, key);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001154 switch (key) {
1155 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
1156 {
1157 Mutex::Autolock l(mLock);
1158 return setAudioAttributes_l(request);
1159 }
1160 default:
1161 sp<MediaPlayerBase> p = getPlayer();
1162 if (p == 0) { return UNKNOWN_ERROR; }
1163 return p->setParameter(key, request);
1164 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001165}
1166
1167status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001168 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001169 sp<MediaPlayerBase> p = getPlayer();
1170 if (p == 0) return UNKNOWN_ERROR;
1171 return p->getParameter(key, reply);
1172}
1173
John Grossmanc795b642012-02-22 15:38:35 -08001174status_t MediaPlayerService::Client::setRetransmitEndpoint(
1175 const struct sockaddr_in* endpoint) {
1176
1177 if (NULL != endpoint) {
1178 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1179 uint16_t p = ntohs(endpoint->sin_port);
1180 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1181 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1182 } else {
1183 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1184 }
1185
1186 sp<MediaPlayerBase> p = getPlayer();
1187
1188 // Right now, the only valid time to set a retransmit endpoint is before
1189 // player selection has been made (since the presence or absence of a
1190 // retransmit endpoint is going to determine which player is selected during
1191 // setDataSource).
1192 if (p != 0) return INVALID_OPERATION;
1193
1194 if (NULL != endpoint) {
1195 mRetransmitEndpoint = *endpoint;
1196 mRetransmitEndpointValid = true;
1197 } else {
1198 mRetransmitEndpointValid = false;
1199 }
1200
1201 return NO_ERROR;
1202}
1203
John Grossman44a7e422012-06-21 17:29:24 -07001204status_t MediaPlayerService::Client::getRetransmitEndpoint(
1205 struct sockaddr_in* endpoint)
1206{
1207 if (NULL == endpoint)
1208 return BAD_VALUE;
1209
1210 sp<MediaPlayerBase> p = getPlayer();
1211
1212 if (p != NULL)
1213 return p->getRetransmitEndpoint(endpoint);
1214
1215 if (!mRetransmitEndpointValid)
1216 return NO_INIT;
1217
1218 *endpoint = mRetransmitEndpoint;
1219
1220 return NO_ERROR;
1221}
1222
Gloria Wangb483c472011-04-11 17:23:27 -07001223void MediaPlayerService::Client::notify(
1224 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001225{
1226 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001227 if (client == NULL) {
1228 return;
1229 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001230
James Dongb8a98252012-08-26 16:13:03 -07001231 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001232 {
1233 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001234 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001235 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001236 if (client->mAudioOutput != NULL)
1237 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001238 client->mNextClient->start();
1239 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1240 }
1241 }
1242
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001243 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001244 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001245 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001246
1247 if(client->shouldDropMetadata(metadata_type)) {
1248 return;
1249 }
1250
1251 // Update the list of metadata that have changed. getMetadata
1252 // also access mMetadataUpdated and clears it.
1253 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001254 }
James Dongb8a98252012-08-26 16:13:03 -07001255
1256 if (c != NULL) {
1257 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1258 c->notify(msg, ext1, ext2, obj);
1259 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001260}
1261
Nicolas Catania48290382009-07-10 13:53:06 -07001262
nikoa64c8c72009-07-20 15:07:26 -07001263bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001264{
Nicolas Catania48290382009-07-10 13:53:06 -07001265 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001266
Nicolas Catania48290382009-07-10 13:53:06 -07001267 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001268 return true;
1269 }
1270
Nicolas Catania48290382009-07-10 13:53:06 -07001271 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001272 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001273 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001274 return true;
1275 }
1276}
1277
Nicolas Catania48290382009-07-10 13:53:06 -07001278
nikoa64c8c72009-07-20 15:07:26 -07001279void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001280 Mutex::Autolock lock(mLock);
1281 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1282 mMetadataUpdated.add(metadata_type);
1283 }
1284}
1285
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001286#if CALLBACK_ANTAGONIZER
1287const int Antagonizer::interval = 10000; // 10 msecs
1288
1289Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1290 mExit(false), mActive(false), mClient(client), mCb(cb)
1291{
1292 createThread(callbackThread, this);
1293}
1294
1295void Antagonizer::kill()
1296{
1297 Mutex::Autolock _l(mLock);
1298 mActive = false;
1299 mExit = true;
1300 mCondition.wait(mLock);
1301}
1302
1303int Antagonizer::callbackThread(void* user)
1304{
Steve Blockb8a80522011-12-20 16:23:08 +00001305 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001306 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1307 while (!p->mExit) {
1308 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001309 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001310 p->mCb(p->mClient, 0, 0, 0);
1311 }
1312 usleep(interval);
1313 }
1314 Mutex::Autolock _l(p->mLock);
1315 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001316 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001317 return 0;
1318}
1319#endif
1320
Andreas Huber1b86fe02014-01-29 11:13:26 -08001321status_t MediaPlayerService::decode(
1322 const sp<IMediaHTTPService> &httpService,
1323 const char* url,
1324 uint32_t *pSampleRate,
1325 int* pNumChannels,
1326 audio_format_t* pFormat,
1327 const sp<IMemoryHeap>& heap,
1328 size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001329{
Steve Block3856b092011-10-20 11:56:00 +01001330 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001331 sp<MediaPlayerBase> player;
Eric Laurent3d00aa62013-09-24 09:53:27 -07001332 status_t status = BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001333
1334 // Protect our precious, precious DRMd ringtones by only allowing
1335 // decoding of http, but not filesystem paths or content Uris.
1336 // If the application wants to decode those, it should open a
1337 // filedescriptor for them and use that.
1338 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001339 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
Eric Laurent3d00aa62013-09-24 09:53:27 -07001340 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001341 }
1342
John Grossman44a7e422012-06-21 17:29:24 -07001343 player_type playerType =
1344 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +01001345 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001346
1347 // create the right type of player
Eric Laurent3d00aa62013-09-24 09:53:27 -07001348 sp<AudioCache> cache = new AudioCache(heap);
John Grossman44a7e422012-06-21 17:29:24 -07001349 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001350 if (player == NULL) goto Exit;
1351 if (player->hardwareOutput()) goto Exit;
1352
1353 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1354
1355 // set data source
Andreas Huber1b86fe02014-01-29 11:13:26 -08001356 if (player->setDataSource(httpService, url) != NO_ERROR) goto Exit;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001357
Steve Block3856b092011-10-20 11:56:00 +01001358 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001359 player->prepareAsync();
1360
Steve Block3856b092011-10-20 11:56:00 +01001361 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001362 if (cache->wait() != NO_ERROR) goto Exit;
1363
Steve Block3856b092011-10-20 11:56:00 +01001364 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001365 player->start();
1366
Steve Block3856b092011-10-20 11:56:00 +01001367 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001368 cache->wait();
1369 // in case of error, return what was successfully decoded.
1370 if (cache->size() == 0) {
1371 goto Exit;
1372 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001373
Eric Laurent3d00aa62013-09-24 09:53:27 -07001374 *pSize = cache->size();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001375 *pSampleRate = cache->sampleRate();
1376 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001377 *pFormat = cache->format();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001378 ALOGV("return size %d sampleRate=%u, channelCount = %d, format = %d",
1379 *pSize, *pSampleRate, *pNumChannels, *pFormat);
1380 status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001381
1382Exit:
1383 if (player != 0) player->reset();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001384 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001385}
1386
Eric Laurent3d00aa62013-09-24 09:53:27 -07001387status_t MediaPlayerService::decode(int fd, int64_t offset, int64_t length,
1388 uint32_t *pSampleRate, int* pNumChannels,
1389 audio_format_t* pFormat,
1390 const sp<IMemoryHeap>& heap, size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001391{
Steve Block3856b092011-10-20 11:56:00 +01001392 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001393 sp<MediaPlayerBase> player;
Eric Laurent3d00aa62013-09-24 09:53:27 -07001394 status_t status = BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001395
John Grossman44a7e422012-06-21 17:29:24 -07001396 player_type playerType = MediaPlayerFactory::getPlayerType(NULL /* client */,
1397 fd,
1398 offset,
1399 length);
Steve Block3856b092011-10-20 11:56:00 +01001400 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001401
1402 // create the right type of player
Eric Laurent3d00aa62013-09-24 09:53:27 -07001403 sp<AudioCache> cache = new AudioCache(heap);
John Grossman44a7e422012-06-21 17:29:24 -07001404 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001405 if (player == NULL) goto Exit;
1406 if (player->hardwareOutput()) goto Exit;
1407
1408 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1409
1410 // set data source
1411 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1412
Steve Block3856b092011-10-20 11:56:00 +01001413 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001414 player->prepareAsync();
1415
Steve Block3856b092011-10-20 11:56:00 +01001416 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001417 if (cache->wait() != NO_ERROR) goto Exit;
1418
Steve Block3856b092011-10-20 11:56:00 +01001419 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001420 player->start();
1421
Steve Block3856b092011-10-20 11:56:00 +01001422 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001423 cache->wait();
1424 // in case of error, return what was successfully decoded.
1425 if (cache->size() == 0) {
1426 goto Exit;
1427 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001428
Eric Laurent3d00aa62013-09-24 09:53:27 -07001429 *pSize = cache->size();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001430 *pSampleRate = cache->sampleRate();
1431 *pNumChannels = cache->channelCount();
1432 *pFormat = cache->format();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001433 ALOGV("return size %d, sampleRate=%u, channelCount = %d, format = %d",
1434 *pSize, *pSampleRate, *pNumChannels, *pFormat);
1435 status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001436
1437Exit:
1438 if (player != 0) player->reset();
1439 ::close(fd);
Eric Laurent3d00aa62013-09-24 09:53:27 -07001440 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001441}
1442
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001443
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001444#undef LOG_TAG
1445#define LOG_TAG "AudioSink"
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001446MediaPlayerService::AudioOutput::AudioOutput(int sessionId, int uid, int pid,
1447 const audio_attributes_t* attr)
Andreas Huber20111aa2009-07-14 16:56:47 -07001448 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001449 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001450 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001451 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001452 mSessionId(sessionId),
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001453 mUid(uid),
Marco Nelissend457c972014-02-11 08:47:07 -08001454 mPid(pid),
Eric Laurent1948eb32012-04-13 16:50:19 -07001455 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001456 ALOGV("AudioOutput(%d)", sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -07001457 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001458 mLeftVolume = 1.0;
1459 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001460 mPlaybackRatePermille = 1000;
1461 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001462 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001463 mAuxEffectId = 0;
1464 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001465 setMinBufferCount();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001466 mAttributes = attr;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001467}
1468
1469MediaPlayerService::AudioOutput::~AudioOutput()
1470{
1471 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001472 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001473}
1474
1475void MediaPlayerService::AudioOutput::setMinBufferCount()
1476{
1477 char value[PROPERTY_VALUE_MAX];
1478 if (property_get("ro.kernel.qemu", value, 0)) {
1479 mIsOnEmulator = true;
1480 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1481 }
1482}
1483
1484bool MediaPlayerService::AudioOutput::isOnEmulator()
1485{
1486 setMinBufferCount();
1487 return mIsOnEmulator;
1488}
1489
1490int MediaPlayerService::AudioOutput::getMinBufferCount()
1491{
1492 setMinBufferCount();
1493 return mMinBufferCount;
1494}
1495
1496ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1497{
1498 if (mTrack == 0) return NO_INIT;
1499 return mTrack->frameCount() * frameSize();
1500}
1501
1502ssize_t MediaPlayerService::AudioOutput::frameCount() const
1503{
1504 if (mTrack == 0) return NO_INIT;
1505 return mTrack->frameCount();
1506}
1507
1508ssize_t MediaPlayerService::AudioOutput::channelCount() const
1509{
1510 if (mTrack == 0) return NO_INIT;
1511 return mTrack->channelCount();
1512}
1513
1514ssize_t MediaPlayerService::AudioOutput::frameSize() const
1515{
1516 if (mTrack == 0) return NO_INIT;
1517 return mTrack->frameSize();
1518}
1519
1520uint32_t MediaPlayerService::AudioOutput::latency () const
1521{
Eric Laurentdb354e52012-03-05 17:27:11 -08001522 if (mTrack == 0) return 0;
1523 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001524}
1525
1526float MediaPlayerService::AudioOutput::msecsPerFrame() const
1527{
1528 return mMsecsPerFrame;
1529}
1530
Marco Nelissen4110c102012-03-29 09:31:28 -07001531status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001532{
1533 if (mTrack == 0) return NO_INIT;
1534 return mTrack->getPosition(position);
1535}
1536
Lajos Molnar06ad1522014-08-28 07:27:44 -07001537status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const
1538{
1539 if (mTrack == 0) return NO_INIT;
1540 return mTrack->getTimestamp(ts);
1541}
1542
Marco Nelissen4110c102012-03-29 09:31:28 -07001543status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1544{
1545 if (mTrack == 0) return NO_INIT;
1546 *frameswritten = mBytesWritten / frameSize();
1547 return OK;
1548}
1549
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001550status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
1551{
1552 if (mTrack == 0) return NO_INIT;
1553 return mTrack->setParameters(keyValuePairs);
1554}
1555
1556String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
1557{
1558 if (mTrack == 0) return String8::empty();
1559 return mTrack->getParameters(keys);
1560}
1561
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001562void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
1563 mAttributes = attributes;
1564}
1565
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001566void MediaPlayerService::AudioOutput::deleteRecycledTrack()
1567{
1568 ALOGV("deleteRecycledTrack");
1569
1570 if (mRecycledTrack != 0) {
1571
1572 if (mCallbackData != NULL) {
1573 mCallbackData->setOutput(NULL);
1574 mCallbackData->endTrackSwitch();
1575 }
1576
1577 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1578 mRecycledTrack->flush();
1579 }
1580 // An offloaded track isn't flushed because the STREAM_END is reported
1581 // slightly prematurely to allow time for the gapless track switch
1582 // but this means that if we decide not to recycle the track there
1583 // could be a small amount of residual data still playing. We leave
1584 // AudioFlinger to drain the track.
1585
1586 mRecycledTrack.clear();
1587 delete mCallbackData;
1588 mCallbackData = NULL;
1589 close();
1590 }
1591}
1592
Andreas Huber20111aa2009-07-14 16:56:47 -07001593status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001594 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1595 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001596 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001597 audio_output_flags_t flags,
1598 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001599{
Andreas Huber20111aa2009-07-14 16:56:47 -07001600 mCallback = cb;
1601 mCallbackCookie = cookie;
1602
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001603 // Check argument "bufferCount" against the mininum buffer count
1604 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001605 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001606 bufferCount = mMinBufferCount;
1607
1608 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001609 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1610 format, bufferCount, mSessionId, flags);
Glenn Kasten1127d652012-11-14 08:44:39 -08001611 uint32_t afSampleRate;
Glenn Kasten7da35f22012-11-14 12:54:39 -08001612 size_t afFrameCount;
Glenn Kastenbce50bf2014-02-27 15:29:51 -08001613 size_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001614
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001615 // offloading is only supported in callback mode for now.
1616 // offloadInfo must be present if offload flag is set
1617 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1618 ((cb == NULL) || (offloadInfo == NULL))) {
1619 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001620 }
1621
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001622 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1623 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1624 } else {
1625 uint32_t afSampleRate;
1626 size_t afFrameCount;
1627
1628 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1629 return NO_INIT;
1630 }
1631 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1632 return NO_INIT;
1633 }
1634
1635 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
1636 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001637
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001638 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001639 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001640 if (0 == channelMask) {
1641 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1642 return NO_INIT;
1643 }
1644 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001645
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001646 // Check whether we can recycle the track
1647 bool reuse = false;
1648 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001649
Glenn Kasten2799d742013-05-30 14:33:29 -07001650 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001651 // check whether we are switching between two offloaded tracks
1652 bothOffloaded = (flags & mRecycledTrack->getFlags()
1653 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07001654
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001655 // check if the existing track can be reused as-is, or if a new track needs to be created.
1656 reuse = true;
1657
Marco Nelissen67295b52012-06-11 14:52:53 -07001658 if ((mCallbackData == NULL && mCallback != NULL) ||
1659 (mCallbackData != NULL && mCallback == NULL)) {
1660 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1661 ALOGV("can't chain callback and write");
1662 reuse = false;
1663 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001664 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1665 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07001666 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001667 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07001668 reuse = false;
1669 } else if (flags != mFlags) {
1670 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1671 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001672 } else if (mRecycledTrack->format() != format) {
1673 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001674 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001675 } else {
1676 ALOGV("no track available to recycle");
1677 }
1678
1679 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1680
1681 // If we can't recycle and both tracks are offloaded
1682 // we must close the previous output before opening a new one
1683 if (bothOffloaded && !reuse) {
1684 ALOGV("both offloaded and not recycling");
1685 deleteRecycledTrack();
1686 }
1687
1688 sp<AudioTrack> t;
1689 CallbackData *newcbd = NULL;
1690
1691 // We don't attempt to create a new track if we are recycling an
1692 // offloaded track. But, if we are recycling a non-offloaded or we
1693 // are switching where one is offloaded and one isn't then we create
1694 // the new track in advance so that we can read additional stream info
1695
1696 if (!(reuse && bothOffloaded)) {
1697 ALOGV("creating new AudioTrack");
1698
1699 if (mCallback != NULL) {
1700 newcbd = new CallbackData(this);
1701 t = new AudioTrack(
1702 mStreamType,
1703 sampleRate,
1704 format,
1705 channelMask,
1706 frameCount,
1707 flags,
1708 CallbackWrapper,
1709 newcbd,
1710 0, // notification frames
1711 mSessionId,
1712 AudioTrack::TRANSFER_CALLBACK,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001713 offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -08001714 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001715 mPid,
1716 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001717 } else {
1718 t = new AudioTrack(
1719 mStreamType,
1720 sampleRate,
1721 format,
1722 channelMask,
1723 frameCount,
1724 flags,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001725 NULL, // callback
1726 NULL, // user data
1727 0, // notification frames
1728 mSessionId,
1729 AudioTrack::TRANSFER_DEFAULT,
1730 NULL, // offload info
Marco Nelissend457c972014-02-11 08:47:07 -08001731 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001732 mPid,
1733 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001734 }
1735
1736 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1737 ALOGE("Unable to create audio track");
1738 delete newcbd;
1739 return NO_INIT;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001740 } else {
1741 // successful AudioTrack initialization implies a legacy stream type was generated
1742 // from the audio attributes
1743 mStreamType = t->streamType();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001744 }
1745 }
1746
1747 if (reuse) {
1748 CHECK(mRecycledTrack != NULL);
1749
1750 if (!bothOffloaded) {
1751 if (mRecycledTrack->frameCount() != t->frameCount()) {
1752 ALOGV("framecount differs: %u/%u frames",
1753 mRecycledTrack->frameCount(), t->frameCount());
1754 reuse = false;
1755 }
1756 }
1757
Marco Nelissen67295b52012-06-11 14:52:53 -07001758 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001759 ALOGV("chaining to next output and recycling track");
Marco Nelissen67295b52012-06-11 14:52:53 -07001760 close();
1761 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001762 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001763 if (mCallbackData != NULL) {
1764 mCallbackData->setOutput(this);
1765 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001766 delete newcbd;
1767 return OK;
1768 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001769 }
1770
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001771 // we're not going to reuse the track, unblock and flush it
1772 // this was done earlier if both tracks are offloaded
1773 if (!bothOffloaded) {
1774 deleteRecycledTrack();
1775 }
1776
1777 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1778
Marco Nelissen67295b52012-06-11 14:52:53 -07001779 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001780 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001781 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001782
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001783 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001784 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001785 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001786 uint32_t pos;
1787 if (t->getPosition(&pos) == OK) {
1788 mBytesWritten = uint64_t(pos) * t->frameSize();
1789 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001790 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001791
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001792 status_t res = NO_ERROR;
1793 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1794 res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1795 if (res == NO_ERROR) {
1796 t->setAuxEffectSendLevel(mSendLevel);
1797 res = t->attachAuxEffect(mAuxEffectId);
1798 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001799 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001800 ALOGV("open() DONE status %d", res);
1801 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001802}
1803
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001804status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001805{
Steve Block3856b092011-10-20 11:56:00 +01001806 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001807 if (mCallbackData != NULL) {
1808 mCallbackData->endTrackSwitch();
1809 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001810 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001811 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001812 mTrack->setAuxEffectSendLevel(mSendLevel);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001813 return mTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001814 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001815 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001816}
1817
Marco Nelissen6b74d672012-02-28 16:07:44 -08001818void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1819 mNextOutput = nextOutput;
1820}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001821
1822
Marco Nelissen6b74d672012-02-28 16:07:44 -08001823void MediaPlayerService::AudioOutput::switchToNextOutput() {
1824 ALOGV("switchToNextOutput");
1825 if (mNextOutput != NULL) {
1826 if (mCallbackData != NULL) {
1827 mCallbackData->beginTrackSwitch();
1828 }
1829 delete mNextOutput->mCallbackData;
1830 mNextOutput->mCallbackData = mCallbackData;
1831 mCallbackData = NULL;
1832 mNextOutput->mRecycledTrack = mTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001833 mTrack.clear();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001834 mNextOutput->mSampleRateHz = mSampleRateHz;
1835 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001836 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001837 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001838 }
1839}
1840
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001841ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1842{
Glenn Kastenadad3d72014-02-21 14:51:43 -08001843 LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
Andreas Huber20111aa2009-07-14 16:56:47 -07001844
Steve Block3856b092011-10-20 11:56:00 +01001845 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07001846 if (mTrack != 0) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001847 ssize_t ret = mTrack->write(buffer, size);
Andy Hunga31335a2014-08-20 17:37:59 -07001848 if (ret >= 0) {
1849 mBytesWritten += ret;
1850 }
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001851 return ret;
1852 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001853 return NO_INIT;
1854}
1855
1856void MediaPlayerService::AudioOutput::stop()
1857{
Steve Block3856b092011-10-20 11:56:00 +01001858 ALOGV("stop");
Glenn Kasten2799d742013-05-30 14:33:29 -07001859 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001860}
1861
1862void MediaPlayerService::AudioOutput::flush()
1863{
Steve Block3856b092011-10-20 11:56:00 +01001864 ALOGV("flush");
Glenn Kasten2799d742013-05-30 14:33:29 -07001865 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001866}
1867
1868void MediaPlayerService::AudioOutput::pause()
1869{
Steve Block3856b092011-10-20 11:56:00 +01001870 ALOGV("pause");
Glenn Kasten2799d742013-05-30 14:33:29 -07001871 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001872}
1873
1874void MediaPlayerService::AudioOutput::close()
1875{
Steve Block3856b092011-10-20 11:56:00 +01001876 ALOGV("close");
Glenn Kasten2799d742013-05-30 14:33:29 -07001877 mTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001878}
1879
1880void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1881{
Steve Block3856b092011-10-20 11:56:00 +01001882 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001883 mLeftVolume = left;
1884 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07001885 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001886 mTrack->setVolume(left, right);
1887 }
1888}
1889
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001890status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1891{
1892 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1893 status_t res = NO_ERROR;
Glenn Kasten2799d742013-05-30 14:33:29 -07001894 if (mTrack != 0) {
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001895 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1896 } else {
1897 res = NO_INIT;
1898 }
1899 mPlaybackRatePermille = ratePermille;
1900 if (mSampleRateHz != 0) {
1901 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1902 }
1903 return res;
1904}
1905
Eric Laurent2beeb502010-07-16 07:43:46 -07001906status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1907{
Steve Block3856b092011-10-20 11:56:00 +01001908 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001909 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07001910 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001911 return mTrack->setAuxEffectSendLevel(level);
1912 }
1913 return NO_ERROR;
1914}
1915
1916status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1917{
Steve Block3856b092011-10-20 11:56:00 +01001918 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001919 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07001920 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001921 return mTrack->attachAuxEffect(effectId);
1922 }
1923 return NO_ERROR;
1924}
1925
Andreas Huber20111aa2009-07-14 16:56:47 -07001926// static
1927void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001928 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001929 //ALOGV("callbackwrapper");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001930 CallbackData *data = (CallbackData*)cookie;
1931 data->lock();
1932 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001933 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001934 if (me == NULL) {
1935 // no output set, likely because the track was scheduled to be reused
1936 // by another player, but the format turned out to be incompatible.
1937 data->unlock();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001938 if (buffer != NULL) {
1939 buffer->size = 0;
1940 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001941 return;
1942 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001943
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001944 switch(event) {
1945 case AudioTrack::EVENT_MORE_DATA: {
1946 size_t actualSize = (*me->mCallback)(
1947 me, buffer->raw, buffer->size, me->mCallbackCookie,
1948 CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001949
aarti jadhav-gaikwadf2575572014-08-13 15:04:39 +05301950 if ((me->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0 &&
1951 actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001952 // We've reached EOS but the audio track is not stopped yet,
1953 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001954
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001955 memset(buffer->raw, 0, buffer->size);
1956 actualSize = buffer->size;
1957 }
1958
1959 buffer->size = actualSize;
1960 } break;
1961
1962
1963 case AudioTrack::EVENT_STREAM_END:
1964 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
1965 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1966 me->mCallbackCookie, CB_EVENT_STREAM_END);
1967 break;
1968
1969 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
1970 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
1971 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1972 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
1973 break;
1974
1975 default:
1976 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001977 }
1978
Marco Nelissen6b74d672012-02-28 16:07:44 -08001979 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001980}
1981
Marco Nelissen4110c102012-03-29 09:31:28 -07001982int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001983{
1984 return mSessionId;
1985}
1986
Eric Laurent6f59db12013-07-26 17:16:50 -07001987uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
1988{
1989 if (mTrack == 0) return 0;
1990 return mTrack->getSampleRate();
1991}
1992
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001993#undef LOG_TAG
1994#define LOG_TAG "AudioCache"
Eric Laurent3d00aa62013-09-24 09:53:27 -07001995MediaPlayerService::AudioCache::AudioCache(const sp<IMemoryHeap>& heap) :
1996 mHeap(heap), mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
Andy Hunga31335a2014-08-20 17:37:59 -07001997 mFrameSize(1), mError(NO_ERROR), mCommandComplete(false)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001998{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001999}
2000
2001uint32_t MediaPlayerService::AudioCache::latency () const
2002{
2003 return 0;
2004}
2005
2006float MediaPlayerService::AudioCache::msecsPerFrame() const
2007{
2008 return mMsecsPerFrame;
2009}
2010
Marco Nelissen4110c102012-03-29 09:31:28 -07002011status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08002012{
2013 if (position == 0) return BAD_VALUE;
Andy Hunga31335a2014-08-20 17:37:59 -07002014 *position = mSize / mFrameSize;
Eric Laurent342e9cf2010-01-19 17:37:09 -08002015 return NO_ERROR;
2016}
2017
Lajos Molnar06ad1522014-08-28 07:27:44 -07002018status_t MediaPlayerService::AudioCache::getTimestamp(AudioTimestamp &ts) const
2019{
2020 ts.mPosition = mSize / mFrameSize;
2021 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
2022 ts.mTime.tv_sec = now / 1000000000LL;
2023 ts.mTime.tv_nsec = now - (1000000000LL * ts.mTime.tv_sec);
2024 return NO_ERROR;
2025}
2026
Marco Nelissen4110c102012-03-29 09:31:28 -07002027status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
2028{
2029 if (written == 0) return BAD_VALUE;
Andy Hunga31335a2014-08-20 17:37:59 -07002030 *written = mSize / mFrameSize;
Marco Nelissen4110c102012-03-29 09:31:28 -07002031 return NO_ERROR;
2032}
2033
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002034////////////////////////////////////////////////////////////////////////////////
2035
2036struct CallbackThread : public Thread {
2037 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
2038 MediaPlayerBase::AudioSink::AudioCallback cb,
2039 void *cookie);
2040
2041protected:
2042 virtual ~CallbackThread();
2043
2044 virtual bool threadLoop();
2045
2046private:
2047 wp<MediaPlayerBase::AudioSink> mSink;
2048 MediaPlayerBase::AudioSink::AudioCallback mCallback;
2049 void *mCookie;
2050 void *mBuffer;
2051 size_t mBufferSize;
2052
2053 CallbackThread(const CallbackThread &);
2054 CallbackThread &operator=(const CallbackThread &);
2055};
2056
2057CallbackThread::CallbackThread(
2058 const wp<MediaPlayerBase::AudioSink> &sink,
2059 MediaPlayerBase::AudioSink::AudioCallback cb,
2060 void *cookie)
2061 : mSink(sink),
2062 mCallback(cb),
2063 mCookie(cookie),
2064 mBuffer(NULL),
2065 mBufferSize(0) {
2066}
2067
2068CallbackThread::~CallbackThread() {
2069 if (mBuffer) {
2070 free(mBuffer);
2071 mBuffer = NULL;
2072 }
2073}
2074
2075bool CallbackThread::threadLoop() {
2076 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
2077 if (sink == NULL) {
2078 return false;
2079 }
2080
2081 if (mBuffer == NULL) {
2082 mBufferSize = sink->bufferSize();
2083 mBuffer = malloc(mBufferSize);
2084 }
2085
2086 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002087 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
2088 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002089
2090 if (actualSize > 0) {
2091 sink->write(mBuffer, actualSize);
Andy Hunga31335a2014-08-20 17:37:59 -07002092 // Could return false on sink->write() error or short count.
2093 // Not necessarily appropriate but would work for AudioCache behavior.
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002094 }
2095
2096 return true;
2097}
2098
2099////////////////////////////////////////////////////////////////////////////////
2100
Andreas Huber20111aa2009-07-14 16:56:47 -07002101status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08002102 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
2103 audio_format_t format, int bufferCount,
Mark Salyzyn77342f72014-06-18 16:31:32 -07002104 AudioCallback cb, void *cookie, audio_output_flags_t /*flags*/,
2105 const audio_offload_info_t* /*offloadInfo*/)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002106{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08002107 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08002108 if (mHeap->getHeapID() < 0) {
2109 return NO_INIT;
2110 }
Andreas Huber20111aa2009-07-14 16:56:47 -07002111
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002112 mSampleRate = sampleRate;
2113 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08002114 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002115 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andy Hunga31335a2014-08-20 17:37:59 -07002116 mFrameSize = audio_is_linear_pcm(mFormat)
2117 ? mChannelCount * audio_bytes_per_sample(mFormat) : 1;
2118 mFrameCount = mHeap->getSize() / mFrameSize;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002119
2120 if (cb != NULL) {
2121 mCallbackThread = new CallbackThread(this, cb, cookie);
2122 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002123 return NO_ERROR;
2124}
2125
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002126status_t MediaPlayerService::AudioCache::start() {
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002127 if (mCallbackThread != NULL) {
2128 mCallbackThread->run("AudioCache callback");
2129 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002130 return NO_ERROR;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002131}
2132
2133void MediaPlayerService::AudioCache::stop() {
2134 if (mCallbackThread != NULL) {
2135 mCallbackThread->requestExitAndWait();
2136 }
2137}
2138
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002139ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
2140{
Steve Block3856b092011-10-20 11:56:00 +01002141 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002142 if ((buffer == 0) || (size == 0)) return size;
2143
2144 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
2145 if (p == NULL) return NO_INIT;
2146 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01002147 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
Andy Hunga31335a2014-08-20 17:37:59 -07002148
2149 bool overflow = mSize + size > mHeap->getSize();
2150 if (overflow) {
Steve Block29357bc2012-01-06 19:20:56 +00002151 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002152 size = mHeap->getSize() - mSize;
2153 }
Andy Hunga31335a2014-08-20 17:37:59 -07002154 size -= size % mFrameSize; // consume only integral amounts of frame size
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002155 memcpy(p, buffer, size);
2156 mSize += size;
Andy Hunga31335a2014-08-20 17:37:59 -07002157
2158 if (overflow) {
2159 // Signal heap filled here (last frame may be truncated).
2160 // After this point, no more data should be written as the
2161 // heap is filled and the AudioCache should be effectively
2162 // immutable with respect to future writes.
2163 //
2164 // It is thus safe for another thread to read the AudioCache.
2165 mCommandComplete = true;
2166 mSignal.signal();
2167 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002168 return size;
2169}
2170
2171// call with lock held
2172status_t MediaPlayerService::AudioCache::wait()
2173{
2174 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08002175 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002176 mSignal.wait(mLock);
2177 }
2178 mCommandComplete = false;
2179
2180 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01002181 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002182 } else {
Steve Block3856b092011-10-20 11:56:00 +01002183 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002184 }
2185 return mError;
2186}
2187
Gloria Wangb483c472011-04-11 17:23:27 -07002188void MediaPlayerService::AudioCache::notify(
Mark Salyzyn77342f72014-06-18 16:31:32 -07002189 void* cookie, int msg, int ext1, int ext2, const Parcel* /*obj*/)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002190{
Steve Block3856b092011-10-20 11:56:00 +01002191 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002192 AudioCache* p = static_cast<AudioCache*>(cookie);
2193
2194 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08002195 switch (msg)
2196 {
2197 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00002198 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002199 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08002200 break;
2201 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01002202 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08002203 break;
2204 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01002205 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08002206 break;
2207 default:
Steve Block3856b092011-10-20 11:56:00 +01002208 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08002209 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002210 }
2211
2212 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08002213 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002214 p->mCommandComplete = true;
2215 p->mSignal.signal();
2216}
2217
Marco Nelissen4110c102012-03-29 09:31:28 -07002218int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07002219{
2220 return 0;
2221}
2222
Eric Laurent6f59db12013-07-26 17:16:50 -07002223uint32_t MediaPlayerService::AudioCache::getSampleRate() const
2224{
2225 if (mMsecsPerFrame == 0) {
2226 return 0;
2227 }
2228 return (uint32_t)(1.e3 / mMsecsPerFrame);
2229}
2230
Gloria Wang7cf180c2011-02-19 18:37:57 -08002231void MediaPlayerService::addBatteryData(uint32_t params)
2232{
2233 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002234
2235 int32_t time = systemTime() / 1000000L;
2236
2237 // change audio output devices. This notification comes from AudioFlinger
2238 if ((params & kBatteryDataSpeakerOn)
2239 || (params & kBatteryDataOtherAudioDeviceOn)) {
2240
2241 int deviceOn[NUM_AUDIO_DEVICES];
2242 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2243 deviceOn[i] = 0;
2244 }
2245
2246 if ((params & kBatteryDataSpeakerOn)
2247 && (params & kBatteryDataOtherAudioDeviceOn)) {
2248 deviceOn[SPEAKER_AND_OTHER] = 1;
2249 } else if (params & kBatteryDataSpeakerOn) {
2250 deviceOn[SPEAKER] = 1;
2251 } else {
2252 deviceOn[OTHER_AUDIO_DEVICE] = 1;
2253 }
2254
2255 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2256 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
2257
2258 if (mBatteryAudio.refCount > 0) { // if playing audio
2259 if (!deviceOn[i]) {
2260 mBatteryAudio.lastTime[i] += time;
2261 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2262 mBatteryAudio.lastTime[i] = 0;
2263 } else {
2264 mBatteryAudio.lastTime[i] = 0 - time;
2265 }
2266 }
2267
2268 mBatteryAudio.deviceOn[i] = deviceOn[i];
2269 }
2270 }
2271 return;
2272 }
2273
2274 // an sudio stream is started
2275 if (params & kBatteryDataAudioFlingerStart) {
2276 // record the start time only if currently no other audio
2277 // is being played
2278 if (mBatteryAudio.refCount == 0) {
2279 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2280 if (mBatteryAudio.deviceOn[i]) {
2281 mBatteryAudio.lastTime[i] -= time;
2282 }
2283 }
2284 }
2285
2286 mBatteryAudio.refCount ++;
2287 return;
2288
2289 } else if (params & kBatteryDataAudioFlingerStop) {
2290 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002291 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002292 return;
2293 }
2294
2295 // record the stop time only if currently this is the only
2296 // audio being played
2297 if (mBatteryAudio.refCount == 1) {
2298 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2299 if (mBatteryAudio.deviceOn[i]) {
2300 mBatteryAudio.lastTime[i] += time;
2301 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2302 mBatteryAudio.lastTime[i] = 0;
2303 }
2304 }
2305 }
2306
2307 mBatteryAudio.refCount --;
2308 return;
2309 }
2310
Gloria Wang7cf180c2011-02-19 18:37:57 -08002311 int uid = IPCThreadState::self()->getCallingUid();
2312 if (uid == AID_MEDIA) {
2313 return;
2314 }
2315 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08002316
2317 if (index < 0) { // create a new entry for this UID
2318 BatteryUsageInfo info;
2319 info.audioTotalTime = 0;
2320 info.videoTotalTime = 0;
2321 info.audioLastTime = 0;
2322 info.videoLastTime = 0;
2323 info.refCount = 0;
2324
Gloria Wang9ee159b2011-02-24 14:51:45 -08002325 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00002326 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002327 return;
2328 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08002329 }
2330
2331 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
2332
2333 if (params & kBatteryDataCodecStarted) {
2334 if (params & kBatteryDataTrackAudio) {
2335 info.audioLastTime -= time;
2336 info.refCount ++;
2337 }
2338 if (params & kBatteryDataTrackVideo) {
2339 info.videoLastTime -= time;
2340 info.refCount ++;
2341 }
2342 } else {
2343 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002344 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002345 return;
2346 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002347 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002348 mBatteryData.removeItem(uid);
2349 return;
2350 }
2351
2352 if (params & kBatteryDataTrackAudio) {
2353 info.audioLastTime += time;
2354 info.refCount --;
2355 }
2356 if (params & kBatteryDataTrackVideo) {
2357 info.videoLastTime += time;
2358 info.refCount --;
2359 }
2360
2361 // no stream is being played by this UID
2362 if (info.refCount == 0) {
2363 info.audioTotalTime += info.audioLastTime;
2364 info.audioLastTime = 0;
2365 info.videoTotalTime += info.videoLastTime;
2366 info.videoLastTime = 0;
2367 }
2368 }
2369}
2370
2371status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2372 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002373
2374 // audio output devices usage
2375 int32_t time = systemTime() / 1000000L; //in ms
2376 int32_t totalTime;
2377
2378 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2379 totalTime = mBatteryAudio.totalTime[i];
2380
2381 if (mBatteryAudio.deviceOn[i]
2382 && (mBatteryAudio.lastTime[i] != 0)) {
2383 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2384 totalTime += tmpTime;
2385 }
2386
2387 reply->writeInt32(totalTime);
2388 // reset the total time
2389 mBatteryAudio.totalTime[i] = 0;
2390 }
2391
2392 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002393 BatteryUsageInfo info;
2394 int size = mBatteryData.size();
2395
2396 reply->writeInt32(size);
2397 int i = 0;
2398
2399 while (i < size) {
2400 info = mBatteryData.valueAt(i);
2401
2402 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2403 reply->writeInt32(info.audioTotalTime);
2404 reply->writeInt32(info.videoTotalTime);
2405
2406 info.audioTotalTime = 0;
2407 info.videoTotalTime = 0;
2408
2409 // remove the UID entry where no stream is being played
2410 if (info.refCount <= 0) {
2411 mBatteryData.removeItemsAt(i);
2412 size --;
2413 i --;
2414 }
2415 i++;
2416 }
2417 return NO_ERROR;
2418}
nikoa64c8c72009-07-20 15:07:26 -07002419} // namespace android