blob: c8cb7ed2310a3710a9469d78f97295e997af6a31 [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>
Nicolas Catania1d187f12009-05-12 23:25:55 -070046#include <utils/Vector.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080047
Andreas Huber1b86fe02014-01-29 11:13:26 -080048#include <media/IMediaHTTPService.h>
Jeff Brown2013a542012-09-04 21:38:42 -070049#include <media/IRemoteDisplay.h>
50#include <media/IRemoteDisplayClient.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080051#include <media/MediaPlayerInterface.h>
52#include <media/mediarecorder.h>
53#include <media/MediaMetadataRetrieverInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070054#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080055#include <media/AudioTrack.h>
James Dong8635b7b2011-03-14 17:01:38 -070056#include <media/MemoryLeakTrackUtil.h>
Lajos Molnar1381d4b2014-08-07 15:18:35 -070057#include <media/stagefright/MediaCodecList.h>
Eric Laurent9cb839a2011-09-27 09:48:56 -070058#include <media/stagefright/MediaErrors.h>
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +010059#include <media/stagefright/AudioPlayer.h>
60#include <media/stagefright/foundation/ADebug.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080061
Dima Zavin64760242011-05-11 14:15:23 -070062#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070063
Gloria Wang7cf180c2011-02-19 18:37:57 -080064#include <private/android_filesystem_config.h>
65
James Dong559bf282012-03-28 10:29:14 -070066#include "ActivityManager.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080067#include "MediaRecorderClient.h"
68#include "MediaPlayerService.h"
69#include "MetadataRetrieverClient.h"
John Grossman44a7e422012-06-21 17:29:24 -070070#include "MediaPlayerFactory.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080071
72#include "MidiFile.h"
Nicolas Catania14d27472009-07-13 14:37:49 -070073#include "TestPlayerStub.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070074#include "StagefrightPlayer.h"
Andreas Huberf9334412010-12-15 15:17:42 -080075#include "nuplayer/NuPlayerDriver.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070076
Andreas Huber20111aa2009-07-14 16:56:47 -070077#include <OMX.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070078
Andreas Hubered3e3e02012-03-26 11:13:27 -070079#include "Crypto.h"
Jeff Tinkercc82dc62013-02-08 10:18:35 -080080#include "Drm.h"
Andreas Huber59451f82012-09-18 10:36:32 -070081#include "HDCP.h"
Andreas Huberb7319a72013-05-29 14:20:52 -070082#include "HTTPBase.h"
Andreas Huber35213f12012-08-29 11:41:50 -070083#include "RemoteDisplay.h"
Andreas Hubered3e3e02012-03-26 11:13:27 -070084
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070085namespace {
nikoa64c8c72009-07-20 15:07:26 -070086using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070087using android::status_t;
88using android::OK;
89using android::BAD_VALUE;
90using android::NOT_ENOUGH_DATA;
91using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070092
93// Max number of entries in the filter.
94const int kMaxFilterSize = 64; // I pulled that out of thin air.
95
nikoa64c8c72009-07-20 15:07:26 -070096// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070097
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070098
99// Unmarshall a filter from a Parcel.
100// Filter format in a parcel:
101//
102// 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
103// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104// | number of entries (n) |
105// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106// | metadata type 1 |
107// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108// | metadata type 2 |
109// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110// ....
111// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112// | metadata type n |
113// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114//
115// @param p Parcel that should start with a filter.
116// @param[out] filter On exit contains the list of metadata type to be
117// filtered.
118// @param[out] status On exit contains the status code to be returned.
119// @return true if the parcel starts with a valid filter.
120bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700121 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700122 status_t *status)
123{
Nicolas Catania48290382009-07-10 13:53:06 -0700124 int32_t val;
125 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700126 {
Steve Block29357bc2012-01-06 19:20:56 +0000127 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700128 *status = NOT_ENOUGH_DATA;
129 return false;
130 }
131
Nicolas Catania48290382009-07-10 13:53:06 -0700132 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700133 {
Steve Block29357bc2012-01-06 19:20:56 +0000134 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700135 *status = BAD_VALUE;
136 return false;
137 }
138
Nicolas Catania48290382009-07-10 13:53:06 -0700139 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700140
141 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700142 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700143
nikoa64c8c72009-07-20 15:07:26 -0700144 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700145
Nicolas Catania48290382009-07-10 13:53:06 -0700146
147 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700148 {
Steve Block29357bc2012-01-06 19:20:56 +0000149 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700150 *status = NOT_ENOUGH_DATA;
151 return false;
152 }
153
nikoa64c8c72009-07-20 15:07:26 -0700154 const Metadata::Type *data =
155 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700156
Nicolas Catania48290382009-07-10 13:53:06 -0700157 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700158 {
Steve Block29357bc2012-01-06 19:20:56 +0000159 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700160 *status = BAD_VALUE;
161 return false;
162 }
163
164 // TODO: The stl impl of vector would be more efficient here
165 // because it degenerates into a memcpy on pod types. Try to
166 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700167 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700168 {
Nicolas Catania48290382009-07-10 13:53:06 -0700169 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700170 ++data;
171 }
172 *status = OK;
173 return true;
174}
175
Nicolas Catania48290382009-07-10 13:53:06 -0700176// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700177// @param val To be searched.
178// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700179bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700180{
181 // Deal with empty and ANY right away
182 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700183 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700184
Nicolas Catania48290382009-07-10 13:53:06 -0700185 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700186}
187
188} // anonymous namespace
189
190
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700191namespace {
192using android::Parcel;
193using android::String16;
194
195// marshalling tag indicating flattened utf16 tags
196// keep in sync with frameworks/base/media/java/android/media/AudioAttributes.java
197const int32_t kAudioAttributesMarshallTagFlattenTags = 1;
198
199// Audio attributes format in a parcel:
200//
201// 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
202// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
203// | usage |
204// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
205// | content_type |
206// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Hyejin Kim4f418f92014-09-05 15:50:03 +0900207// | source |
208// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700209// | flags |
210// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
211// | kAudioAttributesMarshallTagFlattenTags | // ignore tags if not found
212// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
213// | flattened tags in UTF16 |
214// | ... |
215// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
216//
217// @param p Parcel that contains audio attributes.
218// @param[out] attributes On exit points to an initialized audio_attributes_t structure
219// @param[out] status On exit contains the status code to be returned.
220void unmarshallAudioAttributes(const Parcel& parcel, audio_attributes_t *attributes)
221{
222 attributes->usage = (audio_usage_t) parcel.readInt32();
223 attributes->content_type = (audio_content_type_t) parcel.readInt32();
Hyejin Kim4f418f92014-09-05 15:50:03 +0900224 attributes->source = (audio_source_t) parcel.readInt32();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700225 attributes->flags = (audio_flags_mask_t) parcel.readInt32();
226 const bool hasFlattenedTag = (parcel.readInt32() == kAudioAttributesMarshallTagFlattenTags);
227 if (hasFlattenedTag) {
228 // the tags are UTF16, convert to UTF8
229 String16 tags = parcel.readString16();
230 ssize_t realTagSize = utf16_to_utf8_length(tags.string(), tags.size());
231 if (realTagSize <= 0) {
232 strcpy(attributes->tags, "");
233 } else {
234 // copy the flattened string into the attributes as the destination for the conversion:
235 // copying array size -1, array for tags was calloc'd, no need to NULL-terminate it
236 size_t tagSize = realTagSize > AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 ?
237 AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 : realTagSize;
238 utf16_to_utf8(tags.string(), tagSize, attributes->tags);
239 }
240 } else {
241 ALOGE("unmarshallAudioAttributes() received unflattened tags, ignoring tag values");
242 strcpy(attributes->tags, "");
243 }
244}
245} // anonymous namespace
246
247
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248namespace android {
249
Dave Burked681bbb2011-08-30 14:39:17 +0100250static bool checkPermission(const char* permissionString) {
251#ifndef HAVE_ANDROID_OS
252 return true;
253#endif
254 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
255 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000256 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100257 return ok;
258}
259
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800260// 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 -0800261/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
262/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
263
264void MediaPlayerService::instantiate() {
265 defaultServiceManager()->addService(
266 String16("media.player"), new MediaPlayerService());
267}
268
269MediaPlayerService::MediaPlayerService()
270{
Steve Block3856b092011-10-20 11:56:00 +0100271 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800272 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800273
274 mBatteryAudio.refCount = 0;
275 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
276 mBatteryAudio.deviceOn[i] = 0;
277 mBatteryAudio.lastTime[i] = 0;
278 mBatteryAudio.totalTime[i] = 0;
279 }
280 // speaker is on by default
281 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700282
Chong Zhang0b30fd42014-07-23 14:46:05 -0700283 // reset battery stats
284 // if the mediaserver has crashed, battery stats could be left
285 // in bad state, reset the state upon service start.
286 const sp<IServiceManager> sm(defaultServiceManager());
287 if (sm != NULL) {
288 const String16 name("batterystats");
289 sp<IBatteryStats> batteryStats =
290 interface_cast<IBatteryStats>(sm->getService(name));
291 if (batteryStats != NULL) {
292 batteryStats->noteResetVideo();
293 batteryStats->noteResetAudio();
294 }
295 }
296
John Grossman44a7e422012-06-21 17:29:24 -0700297 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800298}
299
300MediaPlayerService::~MediaPlayerService()
301{
Steve Block3856b092011-10-20 11:56:00 +0100302 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303}
304
Glenn Kastenf37971f2012-02-03 11:06:53 -0800305sp<IMediaRecorder> MediaPlayerService::createMediaRecorder()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800306{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800307 pid_t pid = IPCThreadState::self()->getCallingPid();
Gloria Wangdac6a312009-10-29 15:46:37 -0700308 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
309 wp<MediaRecorderClient> w = recorder;
310 Mutex::Autolock lock(mLock);
311 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100312 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800313 return recorder;
314}
315
Gloria Wangdac6a312009-10-29 15:46:37 -0700316void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
317{
318 Mutex::Autolock lock(mLock);
319 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100320 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700321}
322
Glenn Kastenf37971f2012-02-03 11:06:53 -0800323sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800324{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800325 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800326 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100327 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800328 return retriever;
329}
330
Glenn Kastenf37971f2012-02-03 11:06:53 -0800331sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100332 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800333{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800334 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800335 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700336
337 sp<Client> c = new Client(
338 this, pid, connId, client, audioSessionId,
339 IPCThreadState::self()->getCallingUid());
340
Steve Block3856b092011-10-20 11:56:00 +0100341 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100342 IPCThreadState::self()->getCallingUid());
343
344 wp<Client> w = c;
345 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800346 Mutex::Autolock lock(mLock);
347 mClients.add(w);
348 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800349 return c;
350}
351
Lajos Molnar1381d4b2014-08-07 15:18:35 -0700352sp<IMediaCodecList> MediaPlayerService::getCodecList() const {
353 return MediaCodecList::getLocalInstance();
354}
355
Andreas Huber318ad9c2009-10-15 13:46:54 -0700356sp<IOMX> MediaPlayerService::getOMX() {
357 Mutex::Autolock autoLock(mLock);
358
359 if (mOMX.get() == NULL) {
360 mOMX = new OMX;
361 }
362
363 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700364}
365
Andreas Hubered3e3e02012-03-26 11:13:27 -0700366sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700367 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700368}
369
Jeff Tinkercc82dc62013-02-08 10:18:35 -0800370sp<IDrm> MediaPlayerService::makeDrm() {
371 return new Drm;
372}
373
Andreas Huber279dcd82013-01-30 10:41:25 -0800374sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
375 return new HDCP(createEncryptionModule);
Andreas Huber59451f82012-09-18 10:36:32 -0700376}
377
Jeff Brown2013a542012-09-04 21:38:42 -0700378sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
379 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownaba33d52012-09-07 17:38:58 -0700380 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
381 return NULL;
382 }
383
Jeff Brownced24b32012-09-05 17:48:03 -0700384 return new RemoteDisplay(client, iface.string());
Jeff Brown2013a542012-09-04 21:38:42 -0700385}
386
Mark Salyzyn77342f72014-06-18 16:31:32 -0700387status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& /*args*/) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388{
389 const size_t SIZE = 256;
390 char buffer[SIZE];
391 String8 result;
392
393 result.append(" AudioCache\n");
394 if (mHeap != 0) {
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000395 snprintf(buffer, 255, " heap base(%p), size(%zu), flags(%d)\n",
Eric Laurent3d00aa62013-09-24 09:53:27 -0700396 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800397 result.append(buffer);
398 }
Scott Fan7d409692013-04-28 10:13:54 +0800399 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 -0800400 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
401 result.append(buffer);
402 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
403 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
404 result.append(buffer);
405 ::write(fd, result.string(), result.size());
406 return NO_ERROR;
407}
408
409status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
410{
411 const size_t SIZE = 256;
412 char buffer[SIZE];
413 String8 result;
414
415 result.append(" AudioOutput\n");
416 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
417 mStreamType, mLeftVolume, mRightVolume);
418 result.append(buffer);
419 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800420 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800421 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700422 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
423 mAuxEffectId, mSendLevel);
424 result.append(buffer);
425
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800426 ::write(fd, result.string(), result.size());
427 if (mTrack != 0) {
428 mTrack->dump(fd, args);
429 }
430 return NO_ERROR;
431}
432
433status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
434{
435 const size_t SIZE = 256;
436 char buffer[SIZE];
437 String8 result;
438 result.append(" Client\n");
439 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
440 mPid, mConnId, mStatus, mLoop?"true": "false");
441 result.append(buffer);
442 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700443 if (mPlayer != NULL) {
444 mPlayer->dump(fd, args);
445 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800446 if (mAudioOutput != 0) {
447 mAudioOutput->dump(fd, args);
448 }
449 write(fd, "\n", 1);
450 return NO_ERROR;
451}
452
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800453status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
454{
455 const size_t SIZE = 256;
456 char buffer[SIZE];
457 String8 result;
458 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
459 snprintf(buffer, SIZE, "Permission Denial: "
460 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
461 IPCThreadState::self()->getCallingPid(),
462 IPCThreadState::self()->getCallingUid());
463 result.append(buffer);
464 } else {
465 Mutex::Autolock lock(mLock);
466 for (int i = 0, n = mClients.size(); i < n; ++i) {
467 sp<Client> c = mClients[i].promote();
468 if (c != 0) c->dump(fd, args);
469 }
James Dongb9141222010-07-08 11:16:11 -0700470 if (mMediaRecorderClients.size() == 0) {
471 result.append(" No media recorder client\n\n");
472 } else {
473 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
474 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700475 if (c != 0) {
476 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
477 result.append(buffer);
478 write(fd, result.string(), result.size());
479 result = "\n";
480 c->dump(fd, args);
481 }
James Dongb9141222010-07-08 11:16:11 -0700482 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700483 }
484
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700486 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800487 FILE *f = fopen(buffer, "r");
488 if (f) {
489 while (!feof(f)) {
490 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700491 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800492 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700493 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800494 strstr(buffer, " /system/media/")) {
495 result.append(" ");
496 result.append(buffer);
497 }
498 }
499 fclose(f);
500 } else {
501 result.append("couldn't open ");
502 result.append(buffer);
503 result.append("\n");
504 }
505
Glenn Kasten0512ab52011-05-04 17:58:57 -0700506 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800507 DIR *d = opendir(buffer);
508 if (d) {
509 struct dirent *ent;
510 while((ent = readdir(d)) != NULL) {
511 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700512 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800513 struct stat s;
514 if (lstat(buffer, &s) == 0) {
515 if ((s.st_mode & S_IFMT) == S_IFLNK) {
516 char linkto[256];
517 int len = readlink(buffer, linkto, sizeof(linkto));
518 if(len > 0) {
519 if(len > 255) {
520 linkto[252] = '.';
521 linkto[253] = '.';
522 linkto[254] = '.';
523 linkto[255] = 0;
524 } else {
525 linkto[len] = 0;
526 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700527 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800528 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700529 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800530 strstr(linkto, "/system/media/") == linkto) {
531 result.append(" ");
532 result.append(buffer);
533 result.append(" -> ");
534 result.append(linkto);
535 result.append("\n");
536 }
537 }
538 } else {
539 result.append(" unexpected type for ");
540 result.append(buffer);
541 result.append("\n");
542 }
543 }
544 }
545 }
546 closedir(d);
547 } else {
548 result.append("couldn't open ");
549 result.append(buffer);
550 result.append("\n");
551 }
552
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800553 bool dumpMem = false;
554 for (size_t i = 0; i < args.size(); i++) {
555 if (args[i] == String16("-m")) {
556 dumpMem = true;
557 }
558 }
559 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700560 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800561 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800562 }
563 write(fd, result.string(), result.size());
564 return NO_ERROR;
565}
566
567void MediaPlayerService::removeClient(wp<Client> client)
568{
569 Mutex::Autolock lock(mLock);
570 mClients.remove(client);
571}
572
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700573MediaPlayerService::Client::Client(
574 const sp<MediaPlayerService>& service, pid_t pid,
575 int32_t connId, const sp<IMediaPlayerClient>& client,
576 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800577{
Steve Block3856b092011-10-20 11:56:00 +0100578 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800579 mPid = pid;
580 mConnId = connId;
581 mService = service;
582 mClient = client;
583 mLoop = false;
584 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700585 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700586 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800587 mRetransmitEndpointValid = false;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700588 mAudioAttributes = NULL;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700589
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800590#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000591 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800592 mAntagonizer = new Antagonizer(notify, this);
593#endif
594}
595
596MediaPlayerService::Client::~Client()
597{
Steve Block3856b092011-10-20 11:56:00 +0100598 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800599 mAudioOutput.clear();
600 wp<Client> client(this);
601 disconnect();
602 mService->removeClient(client);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700603 if (mAudioAttributes != NULL) {
604 free(mAudioAttributes);
605 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800606}
607
608void MediaPlayerService::Client::disconnect()
609{
Steve Block3856b092011-10-20 11:56:00 +0100610 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800611 // grab local reference and clear main reference to prevent future
612 // access to object
613 sp<MediaPlayerBase> p;
614 {
615 Mutex::Autolock l(mLock);
616 p = mPlayer;
beanzdcfefde2012-11-05 09:51:43 +0800617 mClient.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800618 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700619
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800620 mPlayer.clear();
621
622 // clear the notification to prevent callbacks to dead client
623 // and reset the player. We assume the player will serialize
624 // access to itself if necessary.
625 if (p != 0) {
626 p->setNotifyCallback(0, 0);
627#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000628 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800629 mAntagonizer->kill();
630#endif
631 p->reset();
632 }
633
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700634 disconnectNativeWindow();
635
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800636 IPCThreadState::self()->flushCommands();
637}
638
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800639sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
640{
641 // determine if we have the right player type
642 sp<MediaPlayerBase> p = mPlayer;
643 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100644 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800645 p.clear();
646 }
647 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700648 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800649 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700650
Jason Simmonsdb29e522011-08-12 13:46:55 -0700651 if (p != NULL) {
652 p->setUID(mUID);
653 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700654
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800655 return p;
656}
657
John Grossmanc795b642012-02-22 15:38:35 -0800658sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
659 player_type playerType)
660{
661 ALOGV("player type = %d", playerType);
662
663 // create the right type of player
664 sp<MediaPlayerBase> p = createPlayer(playerType);
665 if (p == NULL) {
666 return p;
667 }
668
669 if (!p->hardwareOutput()) {
Marco Nelissend457c972014-02-11 08:47:07 -0800670 mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(),
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700671 mPid, mAudioAttributes);
John Grossmanc795b642012-02-22 15:38:35 -0800672 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
673 }
674
675 return p;
676}
677
678void MediaPlayerService::Client::setDataSource_post(
679 const sp<MediaPlayerBase>& p,
680 status_t status)
681{
682 ALOGV(" setDataSource");
683 mStatus = status;
684 if (mStatus != OK) {
685 ALOGE(" error: %d", mStatus);
686 return;
687 }
688
689 // Set the re-transmission endpoint if one was chosen.
690 if (mRetransmitEndpointValid) {
691 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
692 if (mStatus != NO_ERROR) {
693 ALOGE("setRetransmitEndpoint error: %d", mStatus);
694 }
695 }
696
697 if (mStatus == OK) {
698 mPlayer = p;
699 }
700}
701
Andreas Huber2db84552010-01-28 11:19:57 -0800702status_t MediaPlayerService::Client::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800703 const sp<IMediaHTTPService> &httpService,
704 const char *url,
705 const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800706{
Steve Block3856b092011-10-20 11:56:00 +0100707 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800708 if (url == NULL)
709 return UNKNOWN_ERROR;
710
Dave Burked681bbb2011-08-30 14:39:17 +0100711 if ((strncmp(url, "http://", 7) == 0) ||
712 (strncmp(url, "https://", 8) == 0) ||
713 (strncmp(url, "rtsp://", 7) == 0)) {
714 if (!checkPermission("android.permission.INTERNET")) {
715 return PERMISSION_DENIED;
716 }
717 }
718
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800719 if (strncmp(url, "content://", 10) == 0) {
720 // get a filedescriptor for the content Uri and
721 // pass it to the setDataSource(fd) method
722
723 String16 url16(url);
724 int fd = android::openContentProviderFile(url16);
725 if (fd < 0)
726 {
Steve Block29357bc2012-01-06 19:20:56 +0000727 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800728 return UNKNOWN_ERROR;
729 }
730 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
731 close(fd);
732 return mStatus;
733 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700734 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800735 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
736 if (p == NULL) {
737 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800738 }
739
Andreas Huber1b86fe02014-01-29 11:13:26 -0800740 setDataSource_post(p, p->setDataSource(httpService, url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800741 return mStatus;
742 }
743}
744
745status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
746{
Steve Block3856b092011-10-20 11:56:00 +0100747 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800748 struct stat sb;
749 int ret = fstat(fd, &sb);
750 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000751 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800752 return UNKNOWN_ERROR;
753 }
754
Steve Block3856b092011-10-20 11:56:00 +0100755 ALOGV("st_dev = %llu", sb.st_dev);
756 ALOGV("st_mode = %u", sb.st_mode);
Mark Salyzyn77342f72014-06-18 16:31:32 -0700757 ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
758 ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
Steve Block3856b092011-10-20 11:56:00 +0100759 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800760
761 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000762 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800763 ::close(fd);
764 return UNKNOWN_ERROR;
765 }
766 if (offset + length > sb.st_size) {
767 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100768 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800769 }
770
John Grossman44a7e422012-06-21 17:29:24 -0700771 player_type playerType = MediaPlayerFactory::getPlayerType(this,
772 fd,
773 offset,
774 length);
John Grossmanc795b642012-02-22 15:38:35 -0800775 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
776 if (p == NULL) {
777 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800778 }
779
780 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800781 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800782 return mStatus;
783}
784
Andreas Hubere2b10282010-11-23 11:41:34 -0800785status_t MediaPlayerService::Client::setDataSource(
786 const sp<IStreamSource> &source) {
787 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700788 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800789 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800790 if (p == NULL) {
791 return NO_INIT;
792 }
793
Andreas Hubere2b10282010-11-23 11:41:34 -0800794 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800795 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800796 return mStatus;
797}
798
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700799void MediaPlayerService::Client::disconnectNativeWindow() {
800 if (mConnectedWindow != NULL) {
801 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
802 NATIVE_WINDOW_API_MEDIA);
803
804 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000805 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700806 strerror(-err), err);
807 }
808 }
809 mConnectedWindow.clear();
810}
811
Glenn Kasten11731182011-02-08 17:26:17 -0800812status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800813 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800814{
Andy McFadden484566c2012-12-18 09:46:54 -0800815 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800816 sp<MediaPlayerBase> p = getPlayer();
817 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700818
Andy McFadden484566c2012-12-18 09:46:54 -0800819 sp<IBinder> binder(bufferProducer == NULL ? NULL :
820 bufferProducer->asBinder());
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700821 if (mConnectedWindowBinder == binder) {
822 return OK;
823 }
824
825 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -0800826 if (bufferProducer != NULL) {
Marco Nelissenee08f7e2013-09-16 13:30:01 -0700827 anw = new Surface(bufferProducer, true /* controlledByApp */);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700828 status_t err = native_window_api_connect(anw.get(),
829 NATIVE_WINDOW_API_MEDIA);
830
831 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000832 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700833 // Note that we must do the reset before disconnecting from the ANW.
834 // Otherwise queue/dequeue calls could be made on the disconnected
835 // ANW, which may result in errors.
836 reset();
837
838 disconnectNativeWindow();
839
840 return err;
841 }
842 }
843
Andy McFadden484566c2012-12-18 09:46:54 -0800844 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700845 // disconnecting the old one. Otherwise queue/dequeue calls could be made
846 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -0800847 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700848
849 disconnectNativeWindow();
850
851 mConnectedWindow = anw;
852
853 if (err == OK) {
854 mConnectedWindowBinder = binder;
855 } else {
856 disconnectNativeWindow();
857 }
858
859 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800860}
861
Nicolas Catania1d187f12009-05-12 23:25:55 -0700862status_t MediaPlayerService::Client::invoke(const Parcel& request,
863 Parcel *reply)
864{
865 sp<MediaPlayerBase> p = getPlayer();
866 if (p == NULL) return UNKNOWN_ERROR;
867 return p->invoke(request, reply);
868}
869
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700870// This call doesn't need to access the native player.
871status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
872{
873 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700874 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700875
Nicolas Catania48290382009-07-10 13:53:06 -0700876 if (unmarshallFilter(filter, &allow, &status) &&
877 unmarshallFilter(filter, &drop, &status)) {
878 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700879
880 mMetadataAllow = allow;
881 mMetadataDrop = drop;
882 }
883 return status;
884}
885
Nicolas Catania48290382009-07-10 13:53:06 -0700886status_t MediaPlayerService::Client::getMetadata(
Mark Salyzyn77342f72014-06-18 16:31:32 -0700887 bool update_only, bool /*apply_filter*/, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700888{
nikoa64c8c72009-07-20 15:07:26 -0700889 sp<MediaPlayerBase> player = getPlayer();
890 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700891
nikod608a812009-07-16 16:39:53 -0700892 status_t status;
893 // Placeholder for the return code, updated by the caller.
894 reply->writeInt32(-1);
895
nikoa64c8c72009-07-20 15:07:26 -0700896 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700897
898 // We don't block notifications while we fetch the data. We clear
899 // mMetadataUpdated first so we don't lose notifications happening
900 // during the rest of this call.
901 {
902 Mutex::Autolock lock(mLock);
903 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700904 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700905 }
906 mMetadataUpdated.clear();
907 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700908
nikoa64c8c72009-07-20 15:07:26 -0700909 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700910
nikoa64c8c72009-07-20 15:07:26 -0700911 metadata.appendHeader();
912 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700913
914 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700915 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000916 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700917 return status;
918 }
919
920 // FIXME: Implement filtering on the result. Not critical since
921 // filtering takes place on the update notifications already. This
922 // would be when all the metadata are fetch and a filter is set.
923
nikod608a812009-07-16 16:39:53 -0700924 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700925 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700926 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700927}
928
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800929status_t MediaPlayerService::Client::prepareAsync()
930{
Steve Block3856b092011-10-20 11:56:00 +0100931 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800932 sp<MediaPlayerBase> p = getPlayer();
933 if (p == 0) return UNKNOWN_ERROR;
934 status_t ret = p->prepareAsync();
935#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000936 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800937 if (ret == NO_ERROR) mAntagonizer->start();
938#endif
939 return ret;
940}
941
942status_t MediaPlayerService::Client::start()
943{
Steve Block3856b092011-10-20 11:56:00 +0100944 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800945 sp<MediaPlayerBase> p = getPlayer();
946 if (p == 0) return UNKNOWN_ERROR;
947 p->setLooping(mLoop);
948 return p->start();
949}
950
951status_t MediaPlayerService::Client::stop()
952{
Steve Block3856b092011-10-20 11:56:00 +0100953 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800954 sp<MediaPlayerBase> p = getPlayer();
955 if (p == 0) return UNKNOWN_ERROR;
956 return p->stop();
957}
958
959status_t MediaPlayerService::Client::pause()
960{
Steve Block3856b092011-10-20 11:56:00 +0100961 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800962 sp<MediaPlayerBase> p = getPlayer();
963 if (p == 0) return UNKNOWN_ERROR;
964 return p->pause();
965}
966
967status_t MediaPlayerService::Client::isPlaying(bool* state)
968{
969 *state = false;
970 sp<MediaPlayerBase> p = getPlayer();
971 if (p == 0) return UNKNOWN_ERROR;
972 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100973 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800974 return NO_ERROR;
975}
976
977status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
978{
Steve Block3856b092011-10-20 11:56:00 +0100979 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800980 sp<MediaPlayerBase> p = getPlayer();
981 if (p == 0) return UNKNOWN_ERROR;
982 status_t ret = p->getCurrentPosition(msec);
983 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100984 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800985 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000986 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800987 }
988 return ret;
989}
990
991status_t MediaPlayerService::Client::getDuration(int *msec)
992{
Steve Block3856b092011-10-20 11:56:00 +0100993 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800994 sp<MediaPlayerBase> p = getPlayer();
995 if (p == 0) return UNKNOWN_ERROR;
996 status_t ret = p->getDuration(msec);
997 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100998 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800999 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001000 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001001 }
1002 return ret;
1003}
1004
Marco Nelissen6b74d672012-02-28 16:07:44 -08001005status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
1006 ALOGV("setNextPlayer");
1007 Mutex::Autolock l(mLock);
1008 sp<Client> c = static_cast<Client*>(player.get());
1009 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -07001010
1011 if (c != NULL) {
1012 if (mAudioOutput != NULL) {
1013 mAudioOutput->setNextOutput(c->mAudioOutput);
1014 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
1015 ALOGE("no current audio output");
1016 }
1017
1018 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
1019 mPlayer->setNextPlayer(mNextClient->getPlayer());
1020 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001021 }
John Grossman5f7e55e2012-08-24 14:47:25 -07001022
Marco Nelissen6b74d672012-02-28 16:07:44 -08001023 return OK;
1024}
1025
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001026status_t MediaPlayerService::Client::seekTo(int msec)
1027{
Steve Block3856b092011-10-20 11:56:00 +01001028 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001029 sp<MediaPlayerBase> p = getPlayer();
1030 if (p == 0) return UNKNOWN_ERROR;
1031 return p->seekTo(msec);
1032}
1033
1034status_t MediaPlayerService::Client::reset()
1035{
Steve Block3856b092011-10-20 11:56:00 +01001036 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -08001037 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001038 sp<MediaPlayerBase> p = getPlayer();
1039 if (p == 0) return UNKNOWN_ERROR;
1040 return p->reset();
1041}
1042
Glenn Kastenfff6d712012-01-12 16:38:12 -08001043status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001044{
Steve Block3856b092011-10-20 11:56:00 +01001045 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001046 // TODO: for hardware output, call player instead
1047 Mutex::Autolock l(mLock);
1048 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1049 return NO_ERROR;
1050}
1051
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001052status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel)
1053{
1054 if (mAudioAttributes != NULL) { free(mAudioAttributes); }
1055 mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1056 unmarshallAudioAttributes(parcel, mAudioAttributes);
1057
1058 ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
1059 mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
1060 mAudioAttributes->tags);
1061
1062 if (mAudioOutput != 0) {
1063 mAudioOutput->setAudioAttributes(mAudioAttributes);
1064 }
1065 return NO_ERROR;
1066}
1067
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001068status_t MediaPlayerService::Client::setLooping(int loop)
1069{
Steve Block3856b092011-10-20 11:56:00 +01001070 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001071 mLoop = loop;
1072 sp<MediaPlayerBase> p = getPlayer();
1073 if (p != 0) return p->setLooping(loop);
1074 return NO_ERROR;
1075}
1076
1077status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1078{
Steve Block3856b092011-10-20 11:56:00 +01001079 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -08001080
1081 // for hardware output, call player instead
1082 sp<MediaPlayerBase> p = getPlayer();
1083 {
1084 Mutex::Autolock l(mLock);
1085 if (p != 0 && p->hardwareOutput()) {
1086 MediaPlayerHWInterface* hwp =
1087 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1088 return hwp->setVolume(leftVolume, rightVolume);
1089 } else {
1090 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1091 return NO_ERROR;
1092 }
1093 }
1094
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001095 return NO_ERROR;
1096}
1097
Eric Laurent2beeb502010-07-16 07:43:46 -07001098status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1099{
Steve Block3856b092011-10-20 11:56:00 +01001100 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001101 Mutex::Autolock l(mLock);
1102 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1103 return NO_ERROR;
1104}
1105
1106status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1107{
Steve Block3856b092011-10-20 11:56:00 +01001108 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001109 Mutex::Autolock l(mLock);
1110 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1111 return NO_ERROR;
1112}
Nicolas Catania48290382009-07-10 13:53:06 -07001113
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001114status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001115 ALOGV("[%d] setParameter(%d)", mConnId, key);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001116 switch (key) {
1117 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
1118 {
1119 Mutex::Autolock l(mLock);
1120 return setAudioAttributes_l(request);
1121 }
1122 default:
1123 sp<MediaPlayerBase> p = getPlayer();
1124 if (p == 0) { return UNKNOWN_ERROR; }
1125 return p->setParameter(key, request);
1126 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001127}
1128
1129status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001130 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001131 sp<MediaPlayerBase> p = getPlayer();
1132 if (p == 0) return UNKNOWN_ERROR;
1133 return p->getParameter(key, reply);
1134}
1135
John Grossmanc795b642012-02-22 15:38:35 -08001136status_t MediaPlayerService::Client::setRetransmitEndpoint(
1137 const struct sockaddr_in* endpoint) {
1138
1139 if (NULL != endpoint) {
1140 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1141 uint16_t p = ntohs(endpoint->sin_port);
1142 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1143 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1144 } else {
1145 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1146 }
1147
1148 sp<MediaPlayerBase> p = getPlayer();
1149
1150 // Right now, the only valid time to set a retransmit endpoint is before
1151 // player selection has been made (since the presence or absence of a
1152 // retransmit endpoint is going to determine which player is selected during
1153 // setDataSource).
1154 if (p != 0) return INVALID_OPERATION;
1155
1156 if (NULL != endpoint) {
1157 mRetransmitEndpoint = *endpoint;
1158 mRetransmitEndpointValid = true;
1159 } else {
1160 mRetransmitEndpointValid = false;
1161 }
1162
1163 return NO_ERROR;
1164}
1165
John Grossman44a7e422012-06-21 17:29:24 -07001166status_t MediaPlayerService::Client::getRetransmitEndpoint(
1167 struct sockaddr_in* endpoint)
1168{
1169 if (NULL == endpoint)
1170 return BAD_VALUE;
1171
1172 sp<MediaPlayerBase> p = getPlayer();
1173
1174 if (p != NULL)
1175 return p->getRetransmitEndpoint(endpoint);
1176
1177 if (!mRetransmitEndpointValid)
1178 return NO_INIT;
1179
1180 *endpoint = mRetransmitEndpoint;
1181
1182 return NO_ERROR;
1183}
1184
Gloria Wangb483c472011-04-11 17:23:27 -07001185void MediaPlayerService::Client::notify(
1186 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001187{
1188 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001189 if (client == NULL) {
1190 return;
1191 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001192
James Dongb8a98252012-08-26 16:13:03 -07001193 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001194 {
1195 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001196 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001197 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001198 if (client->mAudioOutput != NULL)
1199 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001200 client->mNextClient->start();
1201 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1202 }
1203 }
1204
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001205 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001206 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001207 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001208
1209 if(client->shouldDropMetadata(metadata_type)) {
1210 return;
1211 }
1212
1213 // Update the list of metadata that have changed. getMetadata
1214 // also access mMetadataUpdated and clears it.
1215 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001216 }
James Dongb8a98252012-08-26 16:13:03 -07001217
1218 if (c != NULL) {
1219 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1220 c->notify(msg, ext1, ext2, obj);
1221 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001222}
1223
Nicolas Catania48290382009-07-10 13:53:06 -07001224
nikoa64c8c72009-07-20 15:07:26 -07001225bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001226{
Nicolas Catania48290382009-07-10 13:53:06 -07001227 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001228
Nicolas Catania48290382009-07-10 13:53:06 -07001229 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001230 return true;
1231 }
1232
Nicolas Catania48290382009-07-10 13:53:06 -07001233 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001234 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001235 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001236 return true;
1237 }
1238}
1239
Nicolas Catania48290382009-07-10 13:53:06 -07001240
nikoa64c8c72009-07-20 15:07:26 -07001241void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001242 Mutex::Autolock lock(mLock);
1243 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1244 mMetadataUpdated.add(metadata_type);
1245 }
1246}
1247
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001248#if CALLBACK_ANTAGONIZER
1249const int Antagonizer::interval = 10000; // 10 msecs
1250
1251Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1252 mExit(false), mActive(false), mClient(client), mCb(cb)
1253{
1254 createThread(callbackThread, this);
1255}
1256
1257void Antagonizer::kill()
1258{
1259 Mutex::Autolock _l(mLock);
1260 mActive = false;
1261 mExit = true;
1262 mCondition.wait(mLock);
1263}
1264
1265int Antagonizer::callbackThread(void* user)
1266{
Steve Blockb8a80522011-12-20 16:23:08 +00001267 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001268 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1269 while (!p->mExit) {
1270 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001271 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001272 p->mCb(p->mClient, 0, 0, 0);
1273 }
1274 usleep(interval);
1275 }
1276 Mutex::Autolock _l(p->mLock);
1277 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001278 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001279 return 0;
1280}
1281#endif
1282
Andreas Huber1b86fe02014-01-29 11:13:26 -08001283status_t MediaPlayerService::decode(
1284 const sp<IMediaHTTPService> &httpService,
1285 const char* url,
1286 uint32_t *pSampleRate,
1287 int* pNumChannels,
1288 audio_format_t* pFormat,
1289 const sp<IMemoryHeap>& heap,
1290 size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001291{
Steve Block3856b092011-10-20 11:56:00 +01001292 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001293 sp<MediaPlayerBase> player;
Eric Laurent3d00aa62013-09-24 09:53:27 -07001294 status_t status = BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001295
1296 // Protect our precious, precious DRMd ringtones by only allowing
1297 // decoding of http, but not filesystem paths or content Uris.
1298 // If the application wants to decode those, it should open a
1299 // filedescriptor for them and use that.
1300 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001301 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
Eric Laurent3d00aa62013-09-24 09:53:27 -07001302 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001303 }
1304
John Grossman44a7e422012-06-21 17:29:24 -07001305 player_type playerType =
1306 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +01001307 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001308
1309 // create the right type of player
Eric Laurent3d00aa62013-09-24 09:53:27 -07001310 sp<AudioCache> cache = new AudioCache(heap);
John Grossman44a7e422012-06-21 17:29:24 -07001311 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001312 if (player == NULL) goto Exit;
1313 if (player->hardwareOutput()) goto Exit;
1314
1315 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1316
1317 // set data source
Andreas Huber1b86fe02014-01-29 11:13:26 -08001318 if (player->setDataSource(httpService, url) != NO_ERROR) goto Exit;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001319
Steve Block3856b092011-10-20 11:56:00 +01001320 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001321 player->prepareAsync();
1322
Steve Block3856b092011-10-20 11:56:00 +01001323 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001324 if (cache->wait() != NO_ERROR) goto Exit;
1325
Steve Block3856b092011-10-20 11:56:00 +01001326 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001327 player->start();
1328
Steve Block3856b092011-10-20 11:56:00 +01001329 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001330 cache->wait();
1331 // in case of error, return what was successfully decoded.
1332 if (cache->size() == 0) {
1333 goto Exit;
1334 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001335
Eric Laurent3d00aa62013-09-24 09:53:27 -07001336 *pSize = cache->size();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001337 *pSampleRate = cache->sampleRate();
1338 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001339 *pFormat = cache->format();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001340 ALOGV("return size %d sampleRate=%u, channelCount = %d, format = %d",
1341 *pSize, *pSampleRate, *pNumChannels, *pFormat);
1342 status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001343
1344Exit:
1345 if (player != 0) player->reset();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001346 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001347}
1348
Eric Laurent3d00aa62013-09-24 09:53:27 -07001349status_t MediaPlayerService::decode(int fd, int64_t offset, int64_t length,
1350 uint32_t *pSampleRate, int* pNumChannels,
1351 audio_format_t* pFormat,
1352 const sp<IMemoryHeap>& heap, size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001353{
Steve Block3856b092011-10-20 11:56:00 +01001354 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001355 sp<MediaPlayerBase> player;
Eric Laurent3d00aa62013-09-24 09:53:27 -07001356 status_t status = BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001357
John Grossman44a7e422012-06-21 17:29:24 -07001358 player_type playerType = MediaPlayerFactory::getPlayerType(NULL /* client */,
1359 fd,
1360 offset,
1361 length);
Steve Block3856b092011-10-20 11:56:00 +01001362 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001363
1364 // create the right type of player
Eric Laurent3d00aa62013-09-24 09:53:27 -07001365 sp<AudioCache> cache = new AudioCache(heap);
John Grossman44a7e422012-06-21 17:29:24 -07001366 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001367 if (player == NULL) goto Exit;
1368 if (player->hardwareOutput()) goto Exit;
1369
1370 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1371
1372 // set data source
1373 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1374
Steve Block3856b092011-10-20 11:56:00 +01001375 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001376 player->prepareAsync();
1377
Steve Block3856b092011-10-20 11:56:00 +01001378 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001379 if (cache->wait() != NO_ERROR) goto Exit;
1380
Steve Block3856b092011-10-20 11:56:00 +01001381 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001382 player->start();
1383
Steve Block3856b092011-10-20 11:56:00 +01001384 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001385 cache->wait();
1386 // in case of error, return what was successfully decoded.
1387 if (cache->size() == 0) {
1388 goto Exit;
1389 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001390
Eric Laurent3d00aa62013-09-24 09:53:27 -07001391 *pSize = cache->size();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001392 *pSampleRate = cache->sampleRate();
1393 *pNumChannels = cache->channelCount();
1394 *pFormat = cache->format();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001395 ALOGV("return size %d, sampleRate=%u, channelCount = %d, format = %d",
1396 *pSize, *pSampleRate, *pNumChannels, *pFormat);
1397 status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001398
1399Exit:
1400 if (player != 0) player->reset();
1401 ::close(fd);
Eric Laurent3d00aa62013-09-24 09:53:27 -07001402 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001403}
1404
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001405
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001406#undef LOG_TAG
1407#define LOG_TAG "AudioSink"
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001408MediaPlayerService::AudioOutput::AudioOutput(int sessionId, int uid, int pid,
1409 const audio_attributes_t* attr)
Andreas Huber20111aa2009-07-14 16:56:47 -07001410 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001411 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001412 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001413 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001414 mSessionId(sessionId),
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001415 mUid(uid),
Marco Nelissend457c972014-02-11 08:47:07 -08001416 mPid(pid),
Eric Laurent1948eb32012-04-13 16:50:19 -07001417 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001418 ALOGV("AudioOutput(%d)", sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -07001419 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001420 mLeftVolume = 1.0;
1421 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001422 mPlaybackRatePermille = 1000;
1423 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001424 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001425 mAuxEffectId = 0;
1426 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001427 setMinBufferCount();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001428 mAttributes = attr;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001429}
1430
1431MediaPlayerService::AudioOutput::~AudioOutput()
1432{
1433 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001434 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001435}
1436
1437void MediaPlayerService::AudioOutput::setMinBufferCount()
1438{
1439 char value[PROPERTY_VALUE_MAX];
1440 if (property_get("ro.kernel.qemu", value, 0)) {
1441 mIsOnEmulator = true;
1442 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1443 }
1444}
1445
1446bool MediaPlayerService::AudioOutput::isOnEmulator()
1447{
1448 setMinBufferCount();
1449 return mIsOnEmulator;
1450}
1451
1452int MediaPlayerService::AudioOutput::getMinBufferCount()
1453{
1454 setMinBufferCount();
1455 return mMinBufferCount;
1456}
1457
1458ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1459{
1460 if (mTrack == 0) return NO_INIT;
1461 return mTrack->frameCount() * frameSize();
1462}
1463
1464ssize_t MediaPlayerService::AudioOutput::frameCount() const
1465{
1466 if (mTrack == 0) return NO_INIT;
1467 return mTrack->frameCount();
1468}
1469
1470ssize_t MediaPlayerService::AudioOutput::channelCount() const
1471{
1472 if (mTrack == 0) return NO_INIT;
1473 return mTrack->channelCount();
1474}
1475
1476ssize_t MediaPlayerService::AudioOutput::frameSize() const
1477{
1478 if (mTrack == 0) return NO_INIT;
1479 return mTrack->frameSize();
1480}
1481
1482uint32_t MediaPlayerService::AudioOutput::latency () const
1483{
Eric Laurentdb354e52012-03-05 17:27:11 -08001484 if (mTrack == 0) return 0;
1485 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001486}
1487
1488float MediaPlayerService::AudioOutput::msecsPerFrame() const
1489{
1490 return mMsecsPerFrame;
1491}
1492
Marco Nelissen4110c102012-03-29 09:31:28 -07001493status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001494{
1495 if (mTrack == 0) return NO_INIT;
1496 return mTrack->getPosition(position);
1497}
1498
Marco Nelissen4110c102012-03-29 09:31:28 -07001499status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1500{
1501 if (mTrack == 0) return NO_INIT;
1502 *frameswritten = mBytesWritten / frameSize();
1503 return OK;
1504}
1505
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001506status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
1507{
1508 if (mTrack == 0) return NO_INIT;
1509 return mTrack->setParameters(keyValuePairs);
1510}
1511
1512String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
1513{
1514 if (mTrack == 0) return String8::empty();
1515 return mTrack->getParameters(keys);
1516}
1517
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001518void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
1519 mAttributes = attributes;
1520}
1521
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001522void MediaPlayerService::AudioOutput::deleteRecycledTrack()
1523{
1524 ALOGV("deleteRecycledTrack");
1525
1526 if (mRecycledTrack != 0) {
1527
1528 if (mCallbackData != NULL) {
1529 mCallbackData->setOutput(NULL);
1530 mCallbackData->endTrackSwitch();
1531 }
1532
1533 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1534 mRecycledTrack->flush();
1535 }
1536 // An offloaded track isn't flushed because the STREAM_END is reported
1537 // slightly prematurely to allow time for the gapless track switch
1538 // but this means that if we decide not to recycle the track there
1539 // could be a small amount of residual data still playing. We leave
1540 // AudioFlinger to drain the track.
1541
1542 mRecycledTrack.clear();
1543 delete mCallbackData;
1544 mCallbackData = NULL;
1545 close();
1546 }
1547}
1548
Andreas Huber20111aa2009-07-14 16:56:47 -07001549status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001550 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1551 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001552 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001553 audio_output_flags_t flags,
1554 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001555{
Andreas Huber20111aa2009-07-14 16:56:47 -07001556 mCallback = cb;
1557 mCallbackCookie = cookie;
1558
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001559 // Check argument "bufferCount" against the mininum buffer count
1560 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001561 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001562 bufferCount = mMinBufferCount;
1563
1564 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001565 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1566 format, bufferCount, mSessionId, flags);
Glenn Kasten1127d652012-11-14 08:44:39 -08001567 uint32_t afSampleRate;
Glenn Kasten7da35f22012-11-14 12:54:39 -08001568 size_t afFrameCount;
Glenn Kastenbce50bf2014-02-27 15:29:51 -08001569 size_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001570
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001571 // offloading is only supported in callback mode for now.
1572 // offloadInfo must be present if offload flag is set
1573 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1574 ((cb == NULL) || (offloadInfo == NULL))) {
1575 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001576 }
1577
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001578 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1579 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1580 } else {
1581 uint32_t afSampleRate;
1582 size_t afFrameCount;
1583
1584 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1585 return NO_INIT;
1586 }
1587 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1588 return NO_INIT;
1589 }
1590
1591 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
1592 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001593
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001594 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001595 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001596 if (0 == channelMask) {
1597 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1598 return NO_INIT;
1599 }
1600 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001601
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001602 // Check whether we can recycle the track
1603 bool reuse = false;
1604 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001605
Glenn Kasten2799d742013-05-30 14:33:29 -07001606 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001607 // check whether we are switching between two offloaded tracks
1608 bothOffloaded = (flags & mRecycledTrack->getFlags()
1609 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07001610
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001611 // check if the existing track can be reused as-is, or if a new track needs to be created.
1612 reuse = true;
1613
Marco Nelissen67295b52012-06-11 14:52:53 -07001614 if ((mCallbackData == NULL && mCallback != NULL) ||
1615 (mCallbackData != NULL && mCallback == NULL)) {
1616 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1617 ALOGV("can't chain callback and write");
1618 reuse = false;
1619 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001620 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1621 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07001622 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001623 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07001624 reuse = false;
1625 } else if (flags != mFlags) {
1626 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1627 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001628 } else if (mRecycledTrack->format() != format) {
1629 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001630 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001631 } else {
1632 ALOGV("no track available to recycle");
1633 }
1634
1635 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1636
1637 // If we can't recycle and both tracks are offloaded
1638 // we must close the previous output before opening a new one
1639 if (bothOffloaded && !reuse) {
1640 ALOGV("both offloaded and not recycling");
1641 deleteRecycledTrack();
1642 }
1643
1644 sp<AudioTrack> t;
1645 CallbackData *newcbd = NULL;
1646
1647 // We don't attempt to create a new track if we are recycling an
1648 // offloaded track. But, if we are recycling a non-offloaded or we
1649 // are switching where one is offloaded and one isn't then we create
1650 // the new track in advance so that we can read additional stream info
1651
1652 if (!(reuse && bothOffloaded)) {
1653 ALOGV("creating new AudioTrack");
1654
1655 if (mCallback != NULL) {
1656 newcbd = new CallbackData(this);
1657 t = new AudioTrack(
1658 mStreamType,
1659 sampleRate,
1660 format,
1661 channelMask,
1662 frameCount,
1663 flags,
1664 CallbackWrapper,
1665 newcbd,
1666 0, // notification frames
1667 mSessionId,
1668 AudioTrack::TRANSFER_CALLBACK,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001669 offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -08001670 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001671 mPid,
1672 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001673 } else {
1674 t = new AudioTrack(
1675 mStreamType,
1676 sampleRate,
1677 format,
1678 channelMask,
1679 frameCount,
1680 flags,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001681 NULL, // callback
1682 NULL, // user data
1683 0, // notification frames
1684 mSessionId,
1685 AudioTrack::TRANSFER_DEFAULT,
1686 NULL, // offload info
Marco Nelissend457c972014-02-11 08:47:07 -08001687 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001688 mPid,
1689 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001690 }
1691
1692 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1693 ALOGE("Unable to create audio track");
1694 delete newcbd;
1695 return NO_INIT;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001696 } else {
1697 // successful AudioTrack initialization implies a legacy stream type was generated
1698 // from the audio attributes
1699 mStreamType = t->streamType();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001700 }
1701 }
1702
1703 if (reuse) {
1704 CHECK(mRecycledTrack != NULL);
1705
1706 if (!bothOffloaded) {
1707 if (mRecycledTrack->frameCount() != t->frameCount()) {
1708 ALOGV("framecount differs: %u/%u frames",
1709 mRecycledTrack->frameCount(), t->frameCount());
1710 reuse = false;
1711 }
1712 }
1713
Marco Nelissen67295b52012-06-11 14:52:53 -07001714 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001715 ALOGV("chaining to next output and recycling track");
Marco Nelissen67295b52012-06-11 14:52:53 -07001716 close();
1717 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001718 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001719 if (mCallbackData != NULL) {
1720 mCallbackData->setOutput(this);
1721 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001722 delete newcbd;
1723 return OK;
1724 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001725 }
1726
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001727 // we're not going to reuse the track, unblock and flush it
1728 // this was done earlier if both tracks are offloaded
1729 if (!bothOffloaded) {
1730 deleteRecycledTrack();
1731 }
1732
1733 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1734
Marco Nelissen67295b52012-06-11 14:52:53 -07001735 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001736 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001737 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001738
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001739 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001740 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001741 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001742 uint32_t pos;
1743 if (t->getPosition(&pos) == OK) {
1744 mBytesWritten = uint64_t(pos) * t->frameSize();
1745 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001746 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001747
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001748 status_t res = NO_ERROR;
1749 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1750 res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1751 if (res == NO_ERROR) {
1752 t->setAuxEffectSendLevel(mSendLevel);
1753 res = t->attachAuxEffect(mAuxEffectId);
1754 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001755 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001756 ALOGV("open() DONE status %d", res);
1757 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001758}
1759
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001760status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001761{
Steve Block3856b092011-10-20 11:56:00 +01001762 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001763 if (mCallbackData != NULL) {
1764 mCallbackData->endTrackSwitch();
1765 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001766 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001767 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001768 mTrack->setAuxEffectSendLevel(mSendLevel);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001769 return mTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001770 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001771 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001772}
1773
Marco Nelissen6b74d672012-02-28 16:07:44 -08001774void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1775 mNextOutput = nextOutput;
1776}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001777
1778
Marco Nelissen6b74d672012-02-28 16:07:44 -08001779void MediaPlayerService::AudioOutput::switchToNextOutput() {
1780 ALOGV("switchToNextOutput");
1781 if (mNextOutput != NULL) {
1782 if (mCallbackData != NULL) {
1783 mCallbackData->beginTrackSwitch();
1784 }
1785 delete mNextOutput->mCallbackData;
1786 mNextOutput->mCallbackData = mCallbackData;
1787 mCallbackData = NULL;
1788 mNextOutput->mRecycledTrack = mTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001789 mTrack.clear();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001790 mNextOutput->mSampleRateHz = mSampleRateHz;
1791 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001792 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001793 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001794 }
1795}
1796
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001797ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1798{
Glenn Kastenadad3d72014-02-21 14:51:43 -08001799 LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
Andreas Huber20111aa2009-07-14 16:56:47 -07001800
Steve Block3856b092011-10-20 11:56:00 +01001801 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07001802 if (mTrack != 0) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001803 ssize_t ret = mTrack->write(buffer, size);
Andy Hunga31335a2014-08-20 17:37:59 -07001804 if (ret >= 0) {
1805 mBytesWritten += ret;
1806 }
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001807 return ret;
1808 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001809 return NO_INIT;
1810}
1811
1812void MediaPlayerService::AudioOutput::stop()
1813{
Steve Block3856b092011-10-20 11:56:00 +01001814 ALOGV("stop");
Glenn Kasten2799d742013-05-30 14:33:29 -07001815 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001816}
1817
1818void MediaPlayerService::AudioOutput::flush()
1819{
Steve Block3856b092011-10-20 11:56:00 +01001820 ALOGV("flush");
Glenn Kasten2799d742013-05-30 14:33:29 -07001821 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001822}
1823
1824void MediaPlayerService::AudioOutput::pause()
1825{
Steve Block3856b092011-10-20 11:56:00 +01001826 ALOGV("pause");
Glenn Kasten2799d742013-05-30 14:33:29 -07001827 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001828}
1829
1830void MediaPlayerService::AudioOutput::close()
1831{
Steve Block3856b092011-10-20 11:56:00 +01001832 ALOGV("close");
Glenn Kasten2799d742013-05-30 14:33:29 -07001833 mTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001834}
1835
1836void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1837{
Steve Block3856b092011-10-20 11:56:00 +01001838 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001839 mLeftVolume = left;
1840 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07001841 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001842 mTrack->setVolume(left, right);
1843 }
1844}
1845
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001846status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1847{
1848 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1849 status_t res = NO_ERROR;
Glenn Kasten2799d742013-05-30 14:33:29 -07001850 if (mTrack != 0) {
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001851 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1852 } else {
1853 res = NO_INIT;
1854 }
1855 mPlaybackRatePermille = ratePermille;
1856 if (mSampleRateHz != 0) {
1857 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1858 }
1859 return res;
1860}
1861
Eric Laurent2beeb502010-07-16 07:43:46 -07001862status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1863{
Steve Block3856b092011-10-20 11:56:00 +01001864 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001865 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07001866 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001867 return mTrack->setAuxEffectSendLevel(level);
1868 }
1869 return NO_ERROR;
1870}
1871
1872status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1873{
Steve Block3856b092011-10-20 11:56:00 +01001874 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001875 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07001876 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001877 return mTrack->attachAuxEffect(effectId);
1878 }
1879 return NO_ERROR;
1880}
1881
Andreas Huber20111aa2009-07-14 16:56:47 -07001882// static
1883void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001884 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001885 //ALOGV("callbackwrapper");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001886 CallbackData *data = (CallbackData*)cookie;
1887 data->lock();
1888 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001889 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001890 if (me == NULL) {
1891 // no output set, likely because the track was scheduled to be reused
1892 // by another player, but the format turned out to be incompatible.
1893 data->unlock();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001894 if (buffer != NULL) {
1895 buffer->size = 0;
1896 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001897 return;
1898 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001899
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001900 switch(event) {
1901 case AudioTrack::EVENT_MORE_DATA: {
1902 size_t actualSize = (*me->mCallback)(
1903 me, buffer->raw, buffer->size, me->mCallbackCookie,
1904 CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001905
aarti jadhav-gaikwadf2575572014-08-13 15:04:39 +05301906 if ((me->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0 &&
1907 actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001908 // We've reached EOS but the audio track is not stopped yet,
1909 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001910
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001911 memset(buffer->raw, 0, buffer->size);
1912 actualSize = buffer->size;
1913 }
1914
1915 buffer->size = actualSize;
1916 } break;
1917
1918
1919 case AudioTrack::EVENT_STREAM_END:
1920 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
1921 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1922 me->mCallbackCookie, CB_EVENT_STREAM_END);
1923 break;
1924
1925 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
1926 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
1927 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1928 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
1929 break;
1930
1931 default:
1932 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001933 }
1934
Marco Nelissen6b74d672012-02-28 16:07:44 -08001935 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001936}
1937
Marco Nelissen4110c102012-03-29 09:31:28 -07001938int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001939{
1940 return mSessionId;
1941}
1942
Eric Laurent6f59db12013-07-26 17:16:50 -07001943uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
1944{
1945 if (mTrack == 0) return 0;
1946 return mTrack->getSampleRate();
1947}
1948
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001949#undef LOG_TAG
1950#define LOG_TAG "AudioCache"
Eric Laurent3d00aa62013-09-24 09:53:27 -07001951MediaPlayerService::AudioCache::AudioCache(const sp<IMemoryHeap>& heap) :
1952 mHeap(heap), mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
Andy Hunga31335a2014-08-20 17:37:59 -07001953 mFrameSize(1), mError(NO_ERROR), mCommandComplete(false)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001954{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001955}
1956
1957uint32_t MediaPlayerService::AudioCache::latency () const
1958{
1959 return 0;
1960}
1961
1962float MediaPlayerService::AudioCache::msecsPerFrame() const
1963{
1964 return mMsecsPerFrame;
1965}
1966
Marco Nelissen4110c102012-03-29 09:31:28 -07001967status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001968{
1969 if (position == 0) return BAD_VALUE;
Andy Hunga31335a2014-08-20 17:37:59 -07001970 *position = mSize / mFrameSize;
Eric Laurent342e9cf2010-01-19 17:37:09 -08001971 return NO_ERROR;
1972}
1973
Marco Nelissen4110c102012-03-29 09:31:28 -07001974status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
1975{
1976 if (written == 0) return BAD_VALUE;
Andy Hunga31335a2014-08-20 17:37:59 -07001977 *written = mSize / mFrameSize;
Marco Nelissen4110c102012-03-29 09:31:28 -07001978 return NO_ERROR;
1979}
1980
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001981////////////////////////////////////////////////////////////////////////////////
1982
1983struct CallbackThread : public Thread {
1984 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1985 MediaPlayerBase::AudioSink::AudioCallback cb,
1986 void *cookie);
1987
1988protected:
1989 virtual ~CallbackThread();
1990
1991 virtual bool threadLoop();
1992
1993private:
1994 wp<MediaPlayerBase::AudioSink> mSink;
1995 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1996 void *mCookie;
1997 void *mBuffer;
1998 size_t mBufferSize;
1999
2000 CallbackThread(const CallbackThread &);
2001 CallbackThread &operator=(const CallbackThread &);
2002};
2003
2004CallbackThread::CallbackThread(
2005 const wp<MediaPlayerBase::AudioSink> &sink,
2006 MediaPlayerBase::AudioSink::AudioCallback cb,
2007 void *cookie)
2008 : mSink(sink),
2009 mCallback(cb),
2010 mCookie(cookie),
2011 mBuffer(NULL),
2012 mBufferSize(0) {
2013}
2014
2015CallbackThread::~CallbackThread() {
2016 if (mBuffer) {
2017 free(mBuffer);
2018 mBuffer = NULL;
2019 }
2020}
2021
2022bool CallbackThread::threadLoop() {
2023 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
2024 if (sink == NULL) {
2025 return false;
2026 }
2027
2028 if (mBuffer == NULL) {
2029 mBufferSize = sink->bufferSize();
2030 mBuffer = malloc(mBufferSize);
2031 }
2032
2033 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002034 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
2035 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002036
2037 if (actualSize > 0) {
2038 sink->write(mBuffer, actualSize);
Andy Hunga31335a2014-08-20 17:37:59 -07002039 // Could return false on sink->write() error or short count.
2040 // Not necessarily appropriate but would work for AudioCache behavior.
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002041 }
2042
2043 return true;
2044}
2045
2046////////////////////////////////////////////////////////////////////////////////
2047
Andreas Huber20111aa2009-07-14 16:56:47 -07002048status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08002049 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
2050 audio_format_t format, int bufferCount,
Mark Salyzyn77342f72014-06-18 16:31:32 -07002051 AudioCallback cb, void *cookie, audio_output_flags_t /*flags*/,
2052 const audio_offload_info_t* /*offloadInfo*/)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002053{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08002054 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08002055 if (mHeap->getHeapID() < 0) {
2056 return NO_INIT;
2057 }
Andreas Huber20111aa2009-07-14 16:56:47 -07002058
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002059 mSampleRate = sampleRate;
2060 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08002061 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002062 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andy Hunga31335a2014-08-20 17:37:59 -07002063 mFrameSize = audio_is_linear_pcm(mFormat)
2064 ? mChannelCount * audio_bytes_per_sample(mFormat) : 1;
2065 mFrameCount = mHeap->getSize() / mFrameSize;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002066
2067 if (cb != NULL) {
2068 mCallbackThread = new CallbackThread(this, cb, cookie);
2069 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002070 return NO_ERROR;
2071}
2072
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002073status_t MediaPlayerService::AudioCache::start() {
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002074 if (mCallbackThread != NULL) {
2075 mCallbackThread->run("AudioCache callback");
2076 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002077 return NO_ERROR;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002078}
2079
2080void MediaPlayerService::AudioCache::stop() {
2081 if (mCallbackThread != NULL) {
2082 mCallbackThread->requestExitAndWait();
2083 }
2084}
2085
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002086ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
2087{
Steve Block3856b092011-10-20 11:56:00 +01002088 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002089 if ((buffer == 0) || (size == 0)) return size;
2090
2091 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
2092 if (p == NULL) return NO_INIT;
2093 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01002094 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
Andy Hunga31335a2014-08-20 17:37:59 -07002095
2096 bool overflow = mSize + size > mHeap->getSize();
2097 if (overflow) {
Steve Block29357bc2012-01-06 19:20:56 +00002098 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002099 size = mHeap->getSize() - mSize;
2100 }
Andy Hunga31335a2014-08-20 17:37:59 -07002101 size -= size % mFrameSize; // consume only integral amounts of frame size
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002102 memcpy(p, buffer, size);
2103 mSize += size;
Andy Hunga31335a2014-08-20 17:37:59 -07002104
2105 if (overflow) {
2106 // Signal heap filled here (last frame may be truncated).
2107 // After this point, no more data should be written as the
2108 // heap is filled and the AudioCache should be effectively
2109 // immutable with respect to future writes.
2110 //
2111 // It is thus safe for another thread to read the AudioCache.
2112 mCommandComplete = true;
2113 mSignal.signal();
2114 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002115 return size;
2116}
2117
2118// call with lock held
2119status_t MediaPlayerService::AudioCache::wait()
2120{
2121 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08002122 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002123 mSignal.wait(mLock);
2124 }
2125 mCommandComplete = false;
2126
2127 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01002128 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002129 } else {
Steve Block3856b092011-10-20 11:56:00 +01002130 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002131 }
2132 return mError;
2133}
2134
Gloria Wangb483c472011-04-11 17:23:27 -07002135void MediaPlayerService::AudioCache::notify(
Mark Salyzyn77342f72014-06-18 16:31:32 -07002136 void* cookie, int msg, int ext1, int ext2, const Parcel* /*obj*/)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002137{
Steve Block3856b092011-10-20 11:56:00 +01002138 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002139 AudioCache* p = static_cast<AudioCache*>(cookie);
2140
2141 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08002142 switch (msg)
2143 {
2144 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00002145 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002146 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08002147 break;
2148 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01002149 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08002150 break;
2151 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01002152 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08002153 break;
2154 default:
Steve Block3856b092011-10-20 11:56:00 +01002155 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08002156 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002157 }
2158
2159 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08002160 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002161 p->mCommandComplete = true;
2162 p->mSignal.signal();
2163}
2164
Marco Nelissen4110c102012-03-29 09:31:28 -07002165int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07002166{
2167 return 0;
2168}
2169
Eric Laurent6f59db12013-07-26 17:16:50 -07002170uint32_t MediaPlayerService::AudioCache::getSampleRate() const
2171{
2172 if (mMsecsPerFrame == 0) {
2173 return 0;
2174 }
2175 return (uint32_t)(1.e3 / mMsecsPerFrame);
2176}
2177
Gloria Wang7cf180c2011-02-19 18:37:57 -08002178void MediaPlayerService::addBatteryData(uint32_t params)
2179{
2180 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002181
2182 int32_t time = systemTime() / 1000000L;
2183
2184 // change audio output devices. This notification comes from AudioFlinger
2185 if ((params & kBatteryDataSpeakerOn)
2186 || (params & kBatteryDataOtherAudioDeviceOn)) {
2187
2188 int deviceOn[NUM_AUDIO_DEVICES];
2189 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2190 deviceOn[i] = 0;
2191 }
2192
2193 if ((params & kBatteryDataSpeakerOn)
2194 && (params & kBatteryDataOtherAudioDeviceOn)) {
2195 deviceOn[SPEAKER_AND_OTHER] = 1;
2196 } else if (params & kBatteryDataSpeakerOn) {
2197 deviceOn[SPEAKER] = 1;
2198 } else {
2199 deviceOn[OTHER_AUDIO_DEVICE] = 1;
2200 }
2201
2202 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2203 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
2204
2205 if (mBatteryAudio.refCount > 0) { // if playing audio
2206 if (!deviceOn[i]) {
2207 mBatteryAudio.lastTime[i] += time;
2208 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2209 mBatteryAudio.lastTime[i] = 0;
2210 } else {
2211 mBatteryAudio.lastTime[i] = 0 - time;
2212 }
2213 }
2214
2215 mBatteryAudio.deviceOn[i] = deviceOn[i];
2216 }
2217 }
2218 return;
2219 }
2220
2221 // an sudio stream is started
2222 if (params & kBatteryDataAudioFlingerStart) {
2223 // record the start time only if currently no other audio
2224 // is being played
2225 if (mBatteryAudio.refCount == 0) {
2226 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2227 if (mBatteryAudio.deviceOn[i]) {
2228 mBatteryAudio.lastTime[i] -= time;
2229 }
2230 }
2231 }
2232
2233 mBatteryAudio.refCount ++;
2234 return;
2235
2236 } else if (params & kBatteryDataAudioFlingerStop) {
2237 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002238 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002239 return;
2240 }
2241
2242 // record the stop time only if currently this is the only
2243 // audio being played
2244 if (mBatteryAudio.refCount == 1) {
2245 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2246 if (mBatteryAudio.deviceOn[i]) {
2247 mBatteryAudio.lastTime[i] += time;
2248 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2249 mBatteryAudio.lastTime[i] = 0;
2250 }
2251 }
2252 }
2253
2254 mBatteryAudio.refCount --;
2255 return;
2256 }
2257
Gloria Wang7cf180c2011-02-19 18:37:57 -08002258 int uid = IPCThreadState::self()->getCallingUid();
2259 if (uid == AID_MEDIA) {
2260 return;
2261 }
2262 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08002263
2264 if (index < 0) { // create a new entry for this UID
2265 BatteryUsageInfo info;
2266 info.audioTotalTime = 0;
2267 info.videoTotalTime = 0;
2268 info.audioLastTime = 0;
2269 info.videoLastTime = 0;
2270 info.refCount = 0;
2271
Gloria Wang9ee159b2011-02-24 14:51:45 -08002272 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00002273 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002274 return;
2275 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08002276 }
2277
2278 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
2279
2280 if (params & kBatteryDataCodecStarted) {
2281 if (params & kBatteryDataTrackAudio) {
2282 info.audioLastTime -= time;
2283 info.refCount ++;
2284 }
2285 if (params & kBatteryDataTrackVideo) {
2286 info.videoLastTime -= time;
2287 info.refCount ++;
2288 }
2289 } else {
2290 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002291 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002292 return;
2293 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002294 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002295 mBatteryData.removeItem(uid);
2296 return;
2297 }
2298
2299 if (params & kBatteryDataTrackAudio) {
2300 info.audioLastTime += time;
2301 info.refCount --;
2302 }
2303 if (params & kBatteryDataTrackVideo) {
2304 info.videoLastTime += time;
2305 info.refCount --;
2306 }
2307
2308 // no stream is being played by this UID
2309 if (info.refCount == 0) {
2310 info.audioTotalTime += info.audioLastTime;
2311 info.audioLastTime = 0;
2312 info.videoTotalTime += info.videoLastTime;
2313 info.videoLastTime = 0;
2314 }
2315 }
2316}
2317
2318status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2319 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002320
2321 // audio output devices usage
2322 int32_t time = systemTime() / 1000000L; //in ms
2323 int32_t totalTime;
2324
2325 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2326 totalTime = mBatteryAudio.totalTime[i];
2327
2328 if (mBatteryAudio.deviceOn[i]
2329 && (mBatteryAudio.lastTime[i] != 0)) {
2330 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2331 totalTime += tmpTime;
2332 }
2333
2334 reply->writeInt32(totalTime);
2335 // reset the total time
2336 mBatteryAudio.totalTime[i] = 0;
2337 }
2338
2339 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002340 BatteryUsageInfo info;
2341 int size = mBatteryData.size();
2342
2343 reply->writeInt32(size);
2344 int i = 0;
2345
2346 while (i < size) {
2347 info = mBatteryData.valueAt(i);
2348
2349 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2350 reply->writeInt32(info.audioTotalTime);
2351 reply->writeInt32(info.videoTotalTime);
2352
2353 info.audioTotalTime = 0;
2354 info.videoTotalTime = 0;
2355
2356 // remove the UID entry where no stream is being played
2357 if (info.refCount <= 0) {
2358 mBatteryData.removeItemsAt(i);
2359 size --;
2360 i --;
2361 }
2362 i++;
2363 }
2364 return NO_ERROR;
2365}
nikoa64c8c72009-07-20 15:07:26 -07002366} // namespace android