blob: c120898baae779b1d6b9410cce2ceeea9d0048d5 [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
2**
3** Copyright 2008, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18// Proxy for media player implementations
19
20//#define LOG_NDEBUG 0
21#define LOG_TAG "MediaPlayerService"
22#include <utils/Log.h>
23
24#include <sys/types.h>
25#include <sys/stat.h>
Gloria Wang7cf180c2011-02-19 18:37:57 -080026#include <sys/time.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080027#include <dirent.h>
28#include <unistd.h>
29
30#include <string.h>
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070031
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080032#include <cutils/atomic.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070033#include <cutils/properties.h> // for property_get
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070034
35#include <utils/misc.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080036
Chong Zhang0b30fd42014-07-23 14:46:05 -070037#include <binder/IBatteryStats.h>
Mathias Agopian75624082009-05-19 19:08:10 -070038#include <binder/IPCThreadState.h>
39#include <binder/IServiceManager.h>
40#include <binder/MemoryHeapBase.h>
41#include <binder/MemoryBase.h>
Mathias Agopianb1e7cd12013-02-14 17:11:27 -080042#include <gui/Surface.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070043#include <utils/Errors.h> // for status_t
44#include <utils/String8.h>
Marco Nelissen10dbb8e2009-09-20 10:42:13 -070045#include <utils/SystemClock.h>
Lajos Molnar06ad1522014-08-28 07:27:44 -070046#include <utils/Timers.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070047#include <utils/Vector.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080048
Andreas Huber1b86fe02014-01-29 11:13:26 -080049#include <media/IMediaHTTPService.h>
Jeff Brown2013a542012-09-04 21:38:42 -070050#include <media/IRemoteDisplay.h>
51#include <media/IRemoteDisplayClient.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080052#include <media/MediaPlayerInterface.h>
53#include <media/mediarecorder.h>
54#include <media/MediaMetadataRetrieverInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070055#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080056#include <media/AudioTrack.h>
James Dong8635b7b2011-03-14 17:01:38 -070057#include <media/MemoryLeakTrackUtil.h>
Lajos Molnar1381d4b2014-08-07 15:18:35 -070058#include <media/stagefright/MediaCodecList.h>
Eric Laurent9cb839a2011-09-27 09:48:56 -070059#include <media/stagefright/MediaErrors.h>
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +010060#include <media/stagefright/AudioPlayer.h>
61#include <media/stagefright/foundation/ADebug.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080062
Dima Zavin64760242011-05-11 14:15:23 -070063#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070064
Gloria Wang7cf180c2011-02-19 18:37:57 -080065#include <private/android_filesystem_config.h>
66
James Dong559bf282012-03-28 10:29:14 -070067#include "ActivityManager.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080068#include "MediaRecorderClient.h"
69#include "MediaPlayerService.h"
70#include "MetadataRetrieverClient.h"
John Grossman44a7e422012-06-21 17:29:24 -070071#include "MediaPlayerFactory.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080072
73#include "MidiFile.h"
Nicolas Catania14d27472009-07-13 14:37:49 -070074#include "TestPlayerStub.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070075#include "StagefrightPlayer.h"
Andreas Huberf9334412010-12-15 15:17:42 -080076#include "nuplayer/NuPlayerDriver.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070077
Andreas Huber20111aa2009-07-14 16:56:47 -070078#include <OMX.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070079
Andreas Hubered3e3e02012-03-26 11:13:27 -070080#include "Crypto.h"
Jeff Tinkercc82dc62013-02-08 10:18:35 -080081#include "Drm.h"
Andreas Huber59451f82012-09-18 10:36:32 -070082#include "HDCP.h"
Andreas Huberb7319a72013-05-29 14:20:52 -070083#include "HTTPBase.h"
Andreas Huber35213f12012-08-29 11:41:50 -070084#include "RemoteDisplay.h"
Andreas Hubered3e3e02012-03-26 11:13:27 -070085
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070086namespace {
nikoa64c8c72009-07-20 15:07:26 -070087using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070088using android::status_t;
89using android::OK;
90using android::BAD_VALUE;
91using android::NOT_ENOUGH_DATA;
92using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070093
94// Max number of entries in the filter.
95const int kMaxFilterSize = 64; // I pulled that out of thin air.
96
nikoa64c8c72009-07-20 15:07:26 -070097// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070098
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070099
100// Unmarshall a filter from a Parcel.
101// Filter format in a parcel:
102//
103// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
104// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105// | number of entries (n) |
106// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107// | metadata type 1 |
108// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109// | metadata type 2 |
110// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111// ....
112// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113// | metadata type n |
114// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115//
116// @param p Parcel that should start with a filter.
117// @param[out] filter On exit contains the list of metadata type to be
118// filtered.
119// @param[out] status On exit contains the status code to be returned.
120// @return true if the parcel starts with a valid filter.
121bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700122 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700123 status_t *status)
124{
Nicolas Catania48290382009-07-10 13:53:06 -0700125 int32_t val;
126 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700127 {
Steve Block29357bc2012-01-06 19:20:56 +0000128 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700129 *status = NOT_ENOUGH_DATA;
130 return false;
131 }
132
Nicolas Catania48290382009-07-10 13:53:06 -0700133 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700134 {
Steve Block29357bc2012-01-06 19:20:56 +0000135 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700136 *status = BAD_VALUE;
137 return false;
138 }
139
Nicolas Catania48290382009-07-10 13:53:06 -0700140 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700141
142 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700143 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700144
nikoa64c8c72009-07-20 15:07:26 -0700145 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700146
Nicolas Catania48290382009-07-10 13:53:06 -0700147
148 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700149 {
Steve Block29357bc2012-01-06 19:20:56 +0000150 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700151 *status = NOT_ENOUGH_DATA;
152 return false;
153 }
154
nikoa64c8c72009-07-20 15:07:26 -0700155 const Metadata::Type *data =
156 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700157
Nicolas Catania48290382009-07-10 13:53:06 -0700158 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700159 {
Steve Block29357bc2012-01-06 19:20:56 +0000160 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700161 *status = BAD_VALUE;
162 return false;
163 }
164
165 // TODO: The stl impl of vector would be more efficient here
166 // because it degenerates into a memcpy on pod types. Try to
167 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700168 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700169 {
Nicolas Catania48290382009-07-10 13:53:06 -0700170 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700171 ++data;
172 }
173 *status = OK;
174 return true;
175}
176
Nicolas Catania48290382009-07-10 13:53:06 -0700177// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700178// @param val To be searched.
179// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700180bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700181{
182 // Deal with empty and ANY right away
183 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700184 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700185
Nicolas Catania48290382009-07-10 13:53:06 -0700186 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700187}
188
189} // anonymous namespace
190
191
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700192namespace {
193using android::Parcel;
194using android::String16;
195
196// marshalling tag indicating flattened utf16 tags
197// keep in sync with frameworks/base/media/java/android/media/AudioAttributes.java
198const int32_t kAudioAttributesMarshallTagFlattenTags = 1;
199
200// Audio attributes format in a parcel:
201//
202// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
203// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
204// | usage |
205// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
206// | content_type |
207// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Hyejin Kim4f418f92014-09-05 15:50:03 +0900208// | source |
209// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700210// | flags |
211// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
212// | kAudioAttributesMarshallTagFlattenTags | // ignore tags if not found
213// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
214// | flattened tags in UTF16 |
215// | ... |
216// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
217//
218// @param p Parcel that contains audio attributes.
219// @param[out] attributes On exit points to an initialized audio_attributes_t structure
220// @param[out] status On exit contains the status code to be returned.
221void unmarshallAudioAttributes(const Parcel& parcel, audio_attributes_t *attributes)
222{
223 attributes->usage = (audio_usage_t) parcel.readInt32();
224 attributes->content_type = (audio_content_type_t) parcel.readInt32();
Hyejin Kim4f418f92014-09-05 15:50:03 +0900225 attributes->source = (audio_source_t) parcel.readInt32();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700226 attributes->flags = (audio_flags_mask_t) parcel.readInt32();
227 const bool hasFlattenedTag = (parcel.readInt32() == kAudioAttributesMarshallTagFlattenTags);
228 if (hasFlattenedTag) {
229 // the tags are UTF16, convert to UTF8
230 String16 tags = parcel.readString16();
231 ssize_t realTagSize = utf16_to_utf8_length(tags.string(), tags.size());
232 if (realTagSize <= 0) {
233 strcpy(attributes->tags, "");
234 } else {
235 // copy the flattened string into the attributes as the destination for the conversion:
236 // copying array size -1, array for tags was calloc'd, no need to NULL-terminate it
237 size_t tagSize = realTagSize > AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 ?
238 AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 : realTagSize;
239 utf16_to_utf8(tags.string(), tagSize, attributes->tags);
240 }
241 } else {
242 ALOGE("unmarshallAudioAttributes() received unflattened tags, ignoring tag values");
243 strcpy(attributes->tags, "");
244 }
245}
246} // anonymous namespace
247
248
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800249namespace android {
250
Dave Burked681bbb2011-08-30 14:39:17 +0100251static bool checkPermission(const char* permissionString) {
252#ifndef HAVE_ANDROID_OS
253 return true;
254#endif
255 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
256 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000257 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100258 return ok;
259}
260
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800261// 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 -0800262/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
263/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
264
265void MediaPlayerService::instantiate() {
266 defaultServiceManager()->addService(
267 String16("media.player"), new MediaPlayerService());
268}
269
270MediaPlayerService::MediaPlayerService()
271{
Steve Block3856b092011-10-20 11:56:00 +0100272 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800273 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800274
275 mBatteryAudio.refCount = 0;
276 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
277 mBatteryAudio.deviceOn[i] = 0;
278 mBatteryAudio.lastTime[i] = 0;
279 mBatteryAudio.totalTime[i] = 0;
280 }
281 // speaker is on by default
282 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700283
Chong Zhang0b30fd42014-07-23 14:46:05 -0700284 // reset battery stats
285 // if the mediaserver has crashed, battery stats could be left
286 // in bad state, reset the state upon service start.
287 const sp<IServiceManager> sm(defaultServiceManager());
288 if (sm != NULL) {
289 const String16 name("batterystats");
290 sp<IBatteryStats> batteryStats =
291 interface_cast<IBatteryStats>(sm->getService(name));
292 if (batteryStats != NULL) {
293 batteryStats->noteResetVideo();
294 batteryStats->noteResetAudio();
295 }
296 }
297
John Grossman44a7e422012-06-21 17:29:24 -0700298 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800299}
300
301MediaPlayerService::~MediaPlayerService()
302{
Steve Block3856b092011-10-20 11:56:00 +0100303 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800304}
305
Glenn Kastenf37971f2012-02-03 11:06:53 -0800306sp<IMediaRecorder> MediaPlayerService::createMediaRecorder()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800307{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800308 pid_t pid = IPCThreadState::self()->getCallingPid();
Gloria Wangdac6a312009-10-29 15:46:37 -0700309 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
310 wp<MediaRecorderClient> w = recorder;
311 Mutex::Autolock lock(mLock);
312 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100313 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800314 return recorder;
315}
316
Gloria Wangdac6a312009-10-29 15:46:37 -0700317void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
318{
319 Mutex::Autolock lock(mLock);
320 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100321 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700322}
323
Glenn Kastenf37971f2012-02-03 11:06:53 -0800324sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800325{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800326 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800327 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100328 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800329 return retriever;
330}
331
Glenn Kastenf37971f2012-02-03 11:06:53 -0800332sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100333 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800334{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800335 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800336 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700337
338 sp<Client> c = new Client(
339 this, pid, connId, client, audioSessionId,
340 IPCThreadState::self()->getCallingUid());
341
Steve Block3856b092011-10-20 11:56:00 +0100342 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100343 IPCThreadState::self()->getCallingUid());
344
345 wp<Client> w = c;
346 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800347 Mutex::Autolock lock(mLock);
348 mClients.add(w);
349 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800350 return c;
351}
352
Lajos Molnar1381d4b2014-08-07 15:18:35 -0700353sp<IMediaCodecList> MediaPlayerService::getCodecList() const {
354 return MediaCodecList::getLocalInstance();
355}
356
Andreas Huber318ad9c2009-10-15 13:46:54 -0700357sp<IOMX> MediaPlayerService::getOMX() {
358 Mutex::Autolock autoLock(mLock);
359
360 if (mOMX.get() == NULL) {
361 mOMX = new OMX;
362 }
363
364 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700365}
366
Andreas Hubered3e3e02012-03-26 11:13:27 -0700367sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700368 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700369}
370
Jeff Tinkercc82dc62013-02-08 10:18:35 -0800371sp<IDrm> MediaPlayerService::makeDrm() {
372 return new Drm;
373}
374
Andreas Huber279dcd82013-01-30 10:41:25 -0800375sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
376 return new HDCP(createEncryptionModule);
Andreas Huber59451f82012-09-18 10:36:32 -0700377}
378
Jeff Brown2013a542012-09-04 21:38:42 -0700379sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
380 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownaba33d52012-09-07 17:38:58 -0700381 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
382 return NULL;
383 }
384
Jeff Brownced24b32012-09-05 17:48:03 -0700385 return new RemoteDisplay(client, iface.string());
Jeff Brown2013a542012-09-04 21:38:42 -0700386}
387
Mark Salyzyn77342f72014-06-18 16:31:32 -0700388status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& /*args*/) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800389{
390 const size_t SIZE = 256;
391 char buffer[SIZE];
392 String8 result;
393
394 result.append(" AudioCache\n");
395 if (mHeap != 0) {
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000396 snprintf(buffer, 255, " heap base(%p), size(%zu), flags(%d)\n",
Eric Laurent3d00aa62013-09-24 09:53:27 -0700397 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800398 result.append(buffer);
399 }
Scott Fan7d409692013-04-28 10:13:54 +0800400 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 -0800401 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
402 result.append(buffer);
403 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
404 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
405 result.append(buffer);
406 ::write(fd, result.string(), result.size());
407 return NO_ERROR;
408}
409
410status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
411{
412 const size_t SIZE = 256;
413 char buffer[SIZE];
414 String8 result;
415
416 result.append(" AudioOutput\n");
417 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
418 mStreamType, mLeftVolume, mRightVolume);
419 result.append(buffer);
420 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800421 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800422 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700423 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
424 mAuxEffectId, mSendLevel);
425 result.append(buffer);
426
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800427 ::write(fd, result.string(), result.size());
428 if (mTrack != 0) {
429 mTrack->dump(fd, args);
430 }
431 return NO_ERROR;
432}
433
434status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
435{
436 const size_t SIZE = 256;
437 char buffer[SIZE];
438 String8 result;
439 result.append(" Client\n");
440 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
441 mPid, mConnId, mStatus, mLoop?"true": "false");
442 result.append(buffer);
443 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700444 if (mPlayer != NULL) {
445 mPlayer->dump(fd, args);
446 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800447 if (mAudioOutput != 0) {
448 mAudioOutput->dump(fd, args);
449 }
450 write(fd, "\n", 1);
451 return NO_ERROR;
452}
453
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800454status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
455{
456 const size_t SIZE = 256;
457 char buffer[SIZE];
458 String8 result;
459 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
460 snprintf(buffer, SIZE, "Permission Denial: "
461 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
462 IPCThreadState::self()->getCallingPid(),
463 IPCThreadState::self()->getCallingUid());
464 result.append(buffer);
465 } else {
466 Mutex::Autolock lock(mLock);
467 for (int i = 0, n = mClients.size(); i < n; ++i) {
468 sp<Client> c = mClients[i].promote();
469 if (c != 0) c->dump(fd, args);
470 }
James Dongb9141222010-07-08 11:16:11 -0700471 if (mMediaRecorderClients.size() == 0) {
472 result.append(" No media recorder client\n\n");
473 } else {
474 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
475 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700476 if (c != 0) {
477 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
478 result.append(buffer);
479 write(fd, result.string(), result.size());
480 result = "\n";
481 c->dump(fd, args);
482 }
James Dongb9141222010-07-08 11:16:11 -0700483 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700484 }
485
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800486 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700487 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800488 FILE *f = fopen(buffer, "r");
489 if (f) {
490 while (!feof(f)) {
491 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700492 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800493 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700494 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800495 strstr(buffer, " /system/media/")) {
496 result.append(" ");
497 result.append(buffer);
498 }
499 }
500 fclose(f);
501 } else {
502 result.append("couldn't open ");
503 result.append(buffer);
504 result.append("\n");
505 }
506
Glenn Kasten0512ab52011-05-04 17:58:57 -0700507 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800508 DIR *d = opendir(buffer);
509 if (d) {
510 struct dirent *ent;
511 while((ent = readdir(d)) != NULL) {
512 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700513 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800514 struct stat s;
515 if (lstat(buffer, &s) == 0) {
516 if ((s.st_mode & S_IFMT) == S_IFLNK) {
517 char linkto[256];
518 int len = readlink(buffer, linkto, sizeof(linkto));
519 if(len > 0) {
520 if(len > 255) {
521 linkto[252] = '.';
522 linkto[253] = '.';
523 linkto[254] = '.';
524 linkto[255] = 0;
525 } else {
526 linkto[len] = 0;
527 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700528 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800529 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700530 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800531 strstr(linkto, "/system/media/") == linkto) {
532 result.append(" ");
533 result.append(buffer);
534 result.append(" -> ");
535 result.append(linkto);
536 result.append("\n");
537 }
538 }
539 } else {
540 result.append(" unexpected type for ");
541 result.append(buffer);
542 result.append("\n");
543 }
544 }
545 }
546 }
547 closedir(d);
548 } else {
549 result.append("couldn't open ");
550 result.append(buffer);
551 result.append("\n");
552 }
553
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800554 bool dumpMem = false;
555 for (size_t i = 0; i < args.size(); i++) {
556 if (args[i] == String16("-m")) {
557 dumpMem = true;
558 }
559 }
560 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700561 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800562 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800563 }
564 write(fd, result.string(), result.size());
565 return NO_ERROR;
566}
567
568void MediaPlayerService::removeClient(wp<Client> client)
569{
570 Mutex::Autolock lock(mLock);
571 mClients.remove(client);
572}
573
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700574MediaPlayerService::Client::Client(
575 const sp<MediaPlayerService>& service, pid_t pid,
576 int32_t connId, const sp<IMediaPlayerClient>& client,
577 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800578{
Steve Block3856b092011-10-20 11:56:00 +0100579 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800580 mPid = pid;
581 mConnId = connId;
582 mService = service;
583 mClient = client;
584 mLoop = false;
585 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700586 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700587 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800588 mRetransmitEndpointValid = false;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700589 mAudioAttributes = NULL;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700590
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800591#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000592 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800593 mAntagonizer = new Antagonizer(notify, this);
594#endif
595}
596
597MediaPlayerService::Client::~Client()
598{
Steve Block3856b092011-10-20 11:56:00 +0100599 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800600 mAudioOutput.clear();
601 wp<Client> client(this);
602 disconnect();
603 mService->removeClient(client);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700604 if (mAudioAttributes != NULL) {
605 free(mAudioAttributes);
606 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800607}
608
609void MediaPlayerService::Client::disconnect()
610{
Steve Block3856b092011-10-20 11:56:00 +0100611 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800612 // grab local reference and clear main reference to prevent future
613 // access to object
614 sp<MediaPlayerBase> p;
615 {
616 Mutex::Autolock l(mLock);
617 p = mPlayer;
beanzdcfefde2012-11-05 09:51:43 +0800618 mClient.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800619 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700620
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800621 mPlayer.clear();
622
623 // clear the notification to prevent callbacks to dead client
624 // and reset the player. We assume the player will serialize
625 // access to itself if necessary.
626 if (p != 0) {
627 p->setNotifyCallback(0, 0);
628#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000629 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800630 mAntagonizer->kill();
631#endif
632 p->reset();
633 }
634
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700635 disconnectNativeWindow();
636
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800637 IPCThreadState::self()->flushCommands();
638}
639
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800640sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
641{
642 // determine if we have the right player type
643 sp<MediaPlayerBase> p = mPlayer;
644 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100645 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800646 p.clear();
647 }
648 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700649 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800650 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700651
Jason Simmonsdb29e522011-08-12 13:46:55 -0700652 if (p != NULL) {
653 p->setUID(mUID);
654 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700655
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800656 return p;
657}
658
John Grossmanc795b642012-02-22 15:38:35 -0800659sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
660 player_type playerType)
661{
662 ALOGV("player type = %d", playerType);
663
664 // create the right type of player
665 sp<MediaPlayerBase> p = createPlayer(playerType);
666 if (p == NULL) {
667 return p;
668 }
669
670 if (!p->hardwareOutput()) {
Marco Nelissend457c972014-02-11 08:47:07 -0800671 mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(),
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700672 mPid, mAudioAttributes);
John Grossmanc795b642012-02-22 15:38:35 -0800673 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
674 }
675
676 return p;
677}
678
679void MediaPlayerService::Client::setDataSource_post(
680 const sp<MediaPlayerBase>& p,
681 status_t status)
682{
683 ALOGV(" setDataSource");
684 mStatus = status;
685 if (mStatus != OK) {
686 ALOGE(" error: %d", mStatus);
687 return;
688 }
689
690 // Set the re-transmission endpoint if one was chosen.
691 if (mRetransmitEndpointValid) {
692 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
693 if (mStatus != NO_ERROR) {
694 ALOGE("setRetransmitEndpoint error: %d", mStatus);
695 }
696 }
697
698 if (mStatus == OK) {
699 mPlayer = p;
700 }
701}
702
Andreas Huber2db84552010-01-28 11:19:57 -0800703status_t MediaPlayerService::Client::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800704 const sp<IMediaHTTPService> &httpService,
705 const char *url,
706 const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800707{
Steve Block3856b092011-10-20 11:56:00 +0100708 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800709 if (url == NULL)
710 return UNKNOWN_ERROR;
711
Dave Burked681bbb2011-08-30 14:39:17 +0100712 if ((strncmp(url, "http://", 7) == 0) ||
713 (strncmp(url, "https://", 8) == 0) ||
714 (strncmp(url, "rtsp://", 7) == 0)) {
715 if (!checkPermission("android.permission.INTERNET")) {
716 return PERMISSION_DENIED;
717 }
718 }
719
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800720 if (strncmp(url, "content://", 10) == 0) {
721 // get a filedescriptor for the content Uri and
722 // pass it to the setDataSource(fd) method
723
724 String16 url16(url);
725 int fd = android::openContentProviderFile(url16);
726 if (fd < 0)
727 {
Steve Block29357bc2012-01-06 19:20:56 +0000728 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800729 return UNKNOWN_ERROR;
730 }
731 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
732 close(fd);
733 return mStatus;
734 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700735 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800736 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
737 if (p == NULL) {
738 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800739 }
740
Andreas Huber1b86fe02014-01-29 11:13:26 -0800741 setDataSource_post(p, p->setDataSource(httpService, url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800742 return mStatus;
743 }
744}
745
746status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
747{
Steve Block3856b092011-10-20 11:56:00 +0100748 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800749 struct stat sb;
750 int ret = fstat(fd, &sb);
751 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000752 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800753 return UNKNOWN_ERROR;
754 }
755
Steve Block3856b092011-10-20 11:56:00 +0100756 ALOGV("st_dev = %llu", sb.st_dev);
757 ALOGV("st_mode = %u", sb.st_mode);
Mark Salyzyn77342f72014-06-18 16:31:32 -0700758 ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
759 ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
Steve Block3856b092011-10-20 11:56:00 +0100760 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800761
762 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000763 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800764 ::close(fd);
765 return UNKNOWN_ERROR;
766 }
767 if (offset + length > sb.st_size) {
768 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100769 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800770 }
771
John Grossman44a7e422012-06-21 17:29:24 -0700772 player_type playerType = MediaPlayerFactory::getPlayerType(this,
773 fd,
774 offset,
775 length);
John Grossmanc795b642012-02-22 15:38:35 -0800776 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
777 if (p == NULL) {
778 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800779 }
780
781 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800782 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800783 return mStatus;
784}
785
Andreas Hubere2b10282010-11-23 11:41:34 -0800786status_t MediaPlayerService::Client::setDataSource(
787 const sp<IStreamSource> &source) {
788 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700789 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800790 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800791 if (p == NULL) {
792 return NO_INIT;
793 }
794
Andreas Hubere2b10282010-11-23 11:41:34 -0800795 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800796 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800797 return mStatus;
798}
799
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700800void MediaPlayerService::Client::disconnectNativeWindow() {
801 if (mConnectedWindow != NULL) {
802 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
803 NATIVE_WINDOW_API_MEDIA);
804
805 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000806 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700807 strerror(-err), err);
808 }
809 }
810 mConnectedWindow.clear();
811}
812
Glenn Kasten11731182011-02-08 17:26:17 -0800813status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800814 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800815{
Andy McFadden484566c2012-12-18 09:46:54 -0800816 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800817 sp<MediaPlayerBase> p = getPlayer();
818 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700819
Andy McFadden484566c2012-12-18 09:46:54 -0800820 sp<IBinder> binder(bufferProducer == NULL ? NULL :
821 bufferProducer->asBinder());
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700822 if (mConnectedWindowBinder == binder) {
823 return OK;
824 }
825
826 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -0800827 if (bufferProducer != NULL) {
Marco Nelissenee08f7e2013-09-16 13:30:01 -0700828 anw = new Surface(bufferProducer, true /* controlledByApp */);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700829 status_t err = native_window_api_connect(anw.get(),
830 NATIVE_WINDOW_API_MEDIA);
831
832 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000833 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700834 // Note that we must do the reset before disconnecting from the ANW.
835 // Otherwise queue/dequeue calls could be made on the disconnected
836 // ANW, which may result in errors.
837 reset();
838
839 disconnectNativeWindow();
840
841 return err;
842 }
843 }
844
Andy McFadden484566c2012-12-18 09:46:54 -0800845 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700846 // disconnecting the old one. Otherwise queue/dequeue calls could be made
847 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -0800848 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700849
850 disconnectNativeWindow();
851
852 mConnectedWindow = anw;
853
854 if (err == OK) {
855 mConnectedWindowBinder = binder;
856 } else {
857 disconnectNativeWindow();
858 }
859
860 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800861}
862
Nicolas Catania1d187f12009-05-12 23:25:55 -0700863status_t MediaPlayerService::Client::invoke(const Parcel& request,
864 Parcel *reply)
865{
866 sp<MediaPlayerBase> p = getPlayer();
867 if (p == NULL) return UNKNOWN_ERROR;
868 return p->invoke(request, reply);
869}
870
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700871// This call doesn't need to access the native player.
872status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
873{
874 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700875 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700876
Nicolas Catania48290382009-07-10 13:53:06 -0700877 if (unmarshallFilter(filter, &allow, &status) &&
878 unmarshallFilter(filter, &drop, &status)) {
879 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700880
881 mMetadataAllow = allow;
882 mMetadataDrop = drop;
883 }
884 return status;
885}
886
Nicolas Catania48290382009-07-10 13:53:06 -0700887status_t MediaPlayerService::Client::getMetadata(
Mark Salyzyn77342f72014-06-18 16:31:32 -0700888 bool update_only, bool /*apply_filter*/, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700889{
nikoa64c8c72009-07-20 15:07:26 -0700890 sp<MediaPlayerBase> player = getPlayer();
891 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700892
nikod608a812009-07-16 16:39:53 -0700893 status_t status;
894 // Placeholder for the return code, updated by the caller.
895 reply->writeInt32(-1);
896
nikoa64c8c72009-07-20 15:07:26 -0700897 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700898
899 // We don't block notifications while we fetch the data. We clear
900 // mMetadataUpdated first so we don't lose notifications happening
901 // during the rest of this call.
902 {
903 Mutex::Autolock lock(mLock);
904 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700905 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700906 }
907 mMetadataUpdated.clear();
908 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700909
nikoa64c8c72009-07-20 15:07:26 -0700910 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700911
nikoa64c8c72009-07-20 15:07:26 -0700912 metadata.appendHeader();
913 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700914
915 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700916 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000917 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700918 return status;
919 }
920
921 // FIXME: Implement filtering on the result. Not critical since
922 // filtering takes place on the update notifications already. This
923 // would be when all the metadata are fetch and a filter is set.
924
nikod608a812009-07-16 16:39:53 -0700925 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700926 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700927 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700928}
929
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800930status_t MediaPlayerService::Client::prepareAsync()
931{
Steve Block3856b092011-10-20 11:56:00 +0100932 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800933 sp<MediaPlayerBase> p = getPlayer();
934 if (p == 0) return UNKNOWN_ERROR;
935 status_t ret = p->prepareAsync();
936#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000937 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800938 if (ret == NO_ERROR) mAntagonizer->start();
939#endif
940 return ret;
941}
942
943status_t MediaPlayerService::Client::start()
944{
Steve Block3856b092011-10-20 11:56:00 +0100945 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800946 sp<MediaPlayerBase> p = getPlayer();
947 if (p == 0) return UNKNOWN_ERROR;
948 p->setLooping(mLoop);
949 return p->start();
950}
951
952status_t MediaPlayerService::Client::stop()
953{
Steve Block3856b092011-10-20 11:56:00 +0100954 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800955 sp<MediaPlayerBase> p = getPlayer();
956 if (p == 0) return UNKNOWN_ERROR;
957 return p->stop();
958}
959
960status_t MediaPlayerService::Client::pause()
961{
Steve Block3856b092011-10-20 11:56:00 +0100962 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800963 sp<MediaPlayerBase> p = getPlayer();
964 if (p == 0) return UNKNOWN_ERROR;
965 return p->pause();
966}
967
968status_t MediaPlayerService::Client::isPlaying(bool* state)
969{
970 *state = false;
971 sp<MediaPlayerBase> p = getPlayer();
972 if (p == 0) return UNKNOWN_ERROR;
973 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100974 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800975 return NO_ERROR;
976}
977
978status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
979{
Steve Block3856b092011-10-20 11:56:00 +0100980 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800981 sp<MediaPlayerBase> p = getPlayer();
982 if (p == 0) return UNKNOWN_ERROR;
983 status_t ret = p->getCurrentPosition(msec);
984 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100985 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800986 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000987 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800988 }
989 return ret;
990}
991
992status_t MediaPlayerService::Client::getDuration(int *msec)
993{
Steve Block3856b092011-10-20 11:56:00 +0100994 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800995 sp<MediaPlayerBase> p = getPlayer();
996 if (p == 0) return UNKNOWN_ERROR;
997 status_t ret = p->getDuration(msec);
998 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100999 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001000 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001001 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001002 }
1003 return ret;
1004}
1005
Marco Nelissen6b74d672012-02-28 16:07:44 -08001006status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
1007 ALOGV("setNextPlayer");
1008 Mutex::Autolock l(mLock);
1009 sp<Client> c = static_cast<Client*>(player.get());
1010 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -07001011
1012 if (c != NULL) {
1013 if (mAudioOutput != NULL) {
1014 mAudioOutput->setNextOutput(c->mAudioOutput);
1015 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
1016 ALOGE("no current audio output");
1017 }
1018
1019 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
1020 mPlayer->setNextPlayer(mNextClient->getPlayer());
1021 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001022 }
John Grossman5f7e55e2012-08-24 14:47:25 -07001023
Marco Nelissen6b74d672012-02-28 16:07:44 -08001024 return OK;
1025}
1026
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001027status_t MediaPlayerService::Client::seekTo(int msec)
1028{
Steve Block3856b092011-10-20 11:56:00 +01001029 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001030 sp<MediaPlayerBase> p = getPlayer();
1031 if (p == 0) return UNKNOWN_ERROR;
1032 return p->seekTo(msec);
1033}
1034
1035status_t MediaPlayerService::Client::reset()
1036{
Steve Block3856b092011-10-20 11:56:00 +01001037 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -08001038 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001039 sp<MediaPlayerBase> p = getPlayer();
1040 if (p == 0) return UNKNOWN_ERROR;
1041 return p->reset();
1042}
1043
Glenn Kastenfff6d712012-01-12 16:38:12 -08001044status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001045{
Steve Block3856b092011-10-20 11:56:00 +01001046 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001047 // TODO: for hardware output, call player instead
1048 Mutex::Autolock l(mLock);
1049 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1050 return NO_ERROR;
1051}
1052
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001053status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel)
1054{
1055 if (mAudioAttributes != NULL) { free(mAudioAttributes); }
1056 mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1057 unmarshallAudioAttributes(parcel, mAudioAttributes);
1058
1059 ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
1060 mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
1061 mAudioAttributes->tags);
1062
1063 if (mAudioOutput != 0) {
1064 mAudioOutput->setAudioAttributes(mAudioAttributes);
1065 }
1066 return NO_ERROR;
1067}
1068
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001069status_t MediaPlayerService::Client::setLooping(int loop)
1070{
Steve Block3856b092011-10-20 11:56:00 +01001071 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001072 mLoop = loop;
1073 sp<MediaPlayerBase> p = getPlayer();
1074 if (p != 0) return p->setLooping(loop);
1075 return NO_ERROR;
1076}
1077
1078status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1079{
Steve Block3856b092011-10-20 11:56:00 +01001080 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -08001081
1082 // for hardware output, call player instead
1083 sp<MediaPlayerBase> p = getPlayer();
1084 {
1085 Mutex::Autolock l(mLock);
1086 if (p != 0 && p->hardwareOutput()) {
1087 MediaPlayerHWInterface* hwp =
1088 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1089 return hwp->setVolume(leftVolume, rightVolume);
1090 } else {
1091 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1092 return NO_ERROR;
1093 }
1094 }
1095
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001096 return NO_ERROR;
1097}
1098
Eric Laurent2beeb502010-07-16 07:43:46 -07001099status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1100{
Steve Block3856b092011-10-20 11:56:00 +01001101 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001102 Mutex::Autolock l(mLock);
1103 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1104 return NO_ERROR;
1105}
1106
1107status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1108{
Steve Block3856b092011-10-20 11:56:00 +01001109 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001110 Mutex::Autolock l(mLock);
1111 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1112 return NO_ERROR;
1113}
Nicolas Catania48290382009-07-10 13:53:06 -07001114
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001115status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001116 ALOGV("[%d] setParameter(%d)", mConnId, key);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001117 switch (key) {
1118 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
1119 {
1120 Mutex::Autolock l(mLock);
1121 return setAudioAttributes_l(request);
1122 }
1123 default:
1124 sp<MediaPlayerBase> p = getPlayer();
1125 if (p == 0) { return UNKNOWN_ERROR; }
1126 return p->setParameter(key, request);
1127 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001128}
1129
1130status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001131 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001132 sp<MediaPlayerBase> p = getPlayer();
1133 if (p == 0) return UNKNOWN_ERROR;
1134 return p->getParameter(key, reply);
1135}
1136
John Grossmanc795b642012-02-22 15:38:35 -08001137status_t MediaPlayerService::Client::setRetransmitEndpoint(
1138 const struct sockaddr_in* endpoint) {
1139
1140 if (NULL != endpoint) {
1141 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1142 uint16_t p = ntohs(endpoint->sin_port);
1143 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1144 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1145 } else {
1146 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1147 }
1148
1149 sp<MediaPlayerBase> p = getPlayer();
1150
1151 // Right now, the only valid time to set a retransmit endpoint is before
1152 // player selection has been made (since the presence or absence of a
1153 // retransmit endpoint is going to determine which player is selected during
1154 // setDataSource).
1155 if (p != 0) return INVALID_OPERATION;
1156
1157 if (NULL != endpoint) {
1158 mRetransmitEndpoint = *endpoint;
1159 mRetransmitEndpointValid = true;
1160 } else {
1161 mRetransmitEndpointValid = false;
1162 }
1163
1164 return NO_ERROR;
1165}
1166
John Grossman44a7e422012-06-21 17:29:24 -07001167status_t MediaPlayerService::Client::getRetransmitEndpoint(
1168 struct sockaddr_in* endpoint)
1169{
1170 if (NULL == endpoint)
1171 return BAD_VALUE;
1172
1173 sp<MediaPlayerBase> p = getPlayer();
1174
1175 if (p != NULL)
1176 return p->getRetransmitEndpoint(endpoint);
1177
1178 if (!mRetransmitEndpointValid)
1179 return NO_INIT;
1180
1181 *endpoint = mRetransmitEndpoint;
1182
1183 return NO_ERROR;
1184}
1185
Gloria Wangb483c472011-04-11 17:23:27 -07001186void MediaPlayerService::Client::notify(
1187 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001188{
1189 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001190 if (client == NULL) {
1191 return;
1192 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001193
James Dongb8a98252012-08-26 16:13:03 -07001194 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001195 {
1196 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001197 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001198 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001199 if (client->mAudioOutput != NULL)
1200 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001201 client->mNextClient->start();
1202 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1203 }
1204 }
1205
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001206 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001207 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001208 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001209
1210 if(client->shouldDropMetadata(metadata_type)) {
1211 return;
1212 }
1213
1214 // Update the list of metadata that have changed. getMetadata
1215 // also access mMetadataUpdated and clears it.
1216 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001217 }
James Dongb8a98252012-08-26 16:13:03 -07001218
1219 if (c != NULL) {
1220 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1221 c->notify(msg, ext1, ext2, obj);
1222 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001223}
1224
Nicolas Catania48290382009-07-10 13:53:06 -07001225
nikoa64c8c72009-07-20 15:07:26 -07001226bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001227{
Nicolas Catania48290382009-07-10 13:53:06 -07001228 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001229
Nicolas Catania48290382009-07-10 13:53:06 -07001230 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001231 return true;
1232 }
1233
Nicolas Catania48290382009-07-10 13:53:06 -07001234 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001235 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001236 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001237 return true;
1238 }
1239}
1240
Nicolas Catania48290382009-07-10 13:53:06 -07001241
nikoa64c8c72009-07-20 15:07:26 -07001242void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001243 Mutex::Autolock lock(mLock);
1244 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1245 mMetadataUpdated.add(metadata_type);
1246 }
1247}
1248
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001249#if CALLBACK_ANTAGONIZER
1250const int Antagonizer::interval = 10000; // 10 msecs
1251
1252Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1253 mExit(false), mActive(false), mClient(client), mCb(cb)
1254{
1255 createThread(callbackThread, this);
1256}
1257
1258void Antagonizer::kill()
1259{
1260 Mutex::Autolock _l(mLock);
1261 mActive = false;
1262 mExit = true;
1263 mCondition.wait(mLock);
1264}
1265
1266int Antagonizer::callbackThread(void* user)
1267{
Steve Blockb8a80522011-12-20 16:23:08 +00001268 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001269 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1270 while (!p->mExit) {
1271 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001272 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001273 p->mCb(p->mClient, 0, 0, 0);
1274 }
1275 usleep(interval);
1276 }
1277 Mutex::Autolock _l(p->mLock);
1278 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001279 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001280 return 0;
1281}
1282#endif
1283
Andreas Huber1b86fe02014-01-29 11:13:26 -08001284status_t MediaPlayerService::decode(
1285 const sp<IMediaHTTPService> &httpService,
1286 const char* url,
1287 uint32_t *pSampleRate,
1288 int* pNumChannels,
1289 audio_format_t* pFormat,
1290 const sp<IMemoryHeap>& heap,
1291 size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001292{
Steve Block3856b092011-10-20 11:56:00 +01001293 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001294 sp<MediaPlayerBase> player;
Eric Laurent3d00aa62013-09-24 09:53:27 -07001295 status_t status = BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001296
1297 // Protect our precious, precious DRMd ringtones by only allowing
1298 // decoding of http, but not filesystem paths or content Uris.
1299 // If the application wants to decode those, it should open a
1300 // filedescriptor for them and use that.
1301 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001302 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
Eric Laurent3d00aa62013-09-24 09:53:27 -07001303 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001304 }
1305
John Grossman44a7e422012-06-21 17:29:24 -07001306 player_type playerType =
1307 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +01001308 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001309
1310 // create the right type of player
Eric Laurent3d00aa62013-09-24 09:53:27 -07001311 sp<AudioCache> cache = new AudioCache(heap);
John Grossman44a7e422012-06-21 17:29:24 -07001312 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001313 if (player == NULL) goto Exit;
1314 if (player->hardwareOutput()) goto Exit;
1315
1316 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1317
1318 // set data source
Andreas Huber1b86fe02014-01-29 11:13:26 -08001319 if (player->setDataSource(httpService, url) != NO_ERROR) goto Exit;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001320
Steve Block3856b092011-10-20 11:56:00 +01001321 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001322 player->prepareAsync();
1323
Steve Block3856b092011-10-20 11:56:00 +01001324 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001325 if (cache->wait() != NO_ERROR) goto Exit;
1326
Steve Block3856b092011-10-20 11:56:00 +01001327 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001328 player->start();
1329
Steve Block3856b092011-10-20 11:56:00 +01001330 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001331 cache->wait();
1332 // in case of error, return what was successfully decoded.
1333 if (cache->size() == 0) {
1334 goto Exit;
1335 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001336
Eric Laurent3d00aa62013-09-24 09:53:27 -07001337 *pSize = cache->size();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001338 *pSampleRate = cache->sampleRate();
1339 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001340 *pFormat = cache->format();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001341 ALOGV("return size %d sampleRate=%u, channelCount = %d, format = %d",
1342 *pSize, *pSampleRate, *pNumChannels, *pFormat);
1343 status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001344
1345Exit:
1346 if (player != 0) player->reset();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001347 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001348}
1349
Eric Laurent3d00aa62013-09-24 09:53:27 -07001350status_t MediaPlayerService::decode(int fd, int64_t offset, int64_t length,
1351 uint32_t *pSampleRate, int* pNumChannels,
1352 audio_format_t* pFormat,
1353 const sp<IMemoryHeap>& heap, size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001354{
Steve Block3856b092011-10-20 11:56:00 +01001355 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001356 sp<MediaPlayerBase> player;
Eric Laurent3d00aa62013-09-24 09:53:27 -07001357 status_t status = BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001358
John Grossman44a7e422012-06-21 17:29:24 -07001359 player_type playerType = MediaPlayerFactory::getPlayerType(NULL /* client */,
1360 fd,
1361 offset,
1362 length);
Steve Block3856b092011-10-20 11:56:00 +01001363 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001364
1365 // create the right type of player
Eric Laurent3d00aa62013-09-24 09:53:27 -07001366 sp<AudioCache> cache = new AudioCache(heap);
John Grossman44a7e422012-06-21 17:29:24 -07001367 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001368 if (player == NULL) goto Exit;
1369 if (player->hardwareOutput()) goto Exit;
1370
1371 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1372
1373 // set data source
1374 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1375
Steve Block3856b092011-10-20 11:56:00 +01001376 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001377 player->prepareAsync();
1378
Steve Block3856b092011-10-20 11:56:00 +01001379 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001380 if (cache->wait() != NO_ERROR) goto Exit;
1381
Steve Block3856b092011-10-20 11:56:00 +01001382 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001383 player->start();
1384
Steve Block3856b092011-10-20 11:56:00 +01001385 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001386 cache->wait();
1387 // in case of error, return what was successfully decoded.
1388 if (cache->size() == 0) {
1389 goto Exit;
1390 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001391
Eric Laurent3d00aa62013-09-24 09:53:27 -07001392 *pSize = cache->size();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001393 *pSampleRate = cache->sampleRate();
1394 *pNumChannels = cache->channelCount();
1395 *pFormat = cache->format();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001396 ALOGV("return size %d, sampleRate=%u, channelCount = %d, format = %d",
1397 *pSize, *pSampleRate, *pNumChannels, *pFormat);
1398 status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001399
1400Exit:
1401 if (player != 0) player->reset();
1402 ::close(fd);
Eric Laurent3d00aa62013-09-24 09:53:27 -07001403 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001404}
1405
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001406
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001407#undef LOG_TAG
1408#define LOG_TAG "AudioSink"
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001409MediaPlayerService::AudioOutput::AudioOutput(int sessionId, int uid, int pid,
1410 const audio_attributes_t* attr)
Andreas Huber20111aa2009-07-14 16:56:47 -07001411 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001412 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001413 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001414 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001415 mSessionId(sessionId),
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001416 mUid(uid),
Marco Nelissend457c972014-02-11 08:47:07 -08001417 mPid(pid),
Eric Laurent1948eb32012-04-13 16:50:19 -07001418 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001419 ALOGV("AudioOutput(%d)", sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -07001420 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001421 mLeftVolume = 1.0;
1422 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001423 mPlaybackRatePermille = 1000;
1424 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001425 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001426 mAuxEffectId = 0;
1427 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001428 setMinBufferCount();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001429 mAttributes = attr;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001430}
1431
1432MediaPlayerService::AudioOutput::~AudioOutput()
1433{
1434 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001435 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001436}
1437
1438void MediaPlayerService::AudioOutput::setMinBufferCount()
1439{
1440 char value[PROPERTY_VALUE_MAX];
1441 if (property_get("ro.kernel.qemu", value, 0)) {
1442 mIsOnEmulator = true;
1443 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1444 }
1445}
1446
1447bool MediaPlayerService::AudioOutput::isOnEmulator()
1448{
1449 setMinBufferCount();
1450 return mIsOnEmulator;
1451}
1452
1453int MediaPlayerService::AudioOutput::getMinBufferCount()
1454{
1455 setMinBufferCount();
1456 return mMinBufferCount;
1457}
1458
1459ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1460{
1461 if (mTrack == 0) return NO_INIT;
1462 return mTrack->frameCount() * frameSize();
1463}
1464
1465ssize_t MediaPlayerService::AudioOutput::frameCount() const
1466{
1467 if (mTrack == 0) return NO_INIT;
1468 return mTrack->frameCount();
1469}
1470
1471ssize_t MediaPlayerService::AudioOutput::channelCount() const
1472{
1473 if (mTrack == 0) return NO_INIT;
1474 return mTrack->channelCount();
1475}
1476
1477ssize_t MediaPlayerService::AudioOutput::frameSize() const
1478{
1479 if (mTrack == 0) return NO_INIT;
1480 return mTrack->frameSize();
1481}
1482
1483uint32_t MediaPlayerService::AudioOutput::latency () const
1484{
Eric Laurentdb354e52012-03-05 17:27:11 -08001485 if (mTrack == 0) return 0;
1486 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001487}
1488
1489float MediaPlayerService::AudioOutput::msecsPerFrame() const
1490{
1491 return mMsecsPerFrame;
1492}
1493
Marco Nelissen4110c102012-03-29 09:31:28 -07001494status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001495{
1496 if (mTrack == 0) return NO_INIT;
1497 return mTrack->getPosition(position);
1498}
1499
Lajos Molnar06ad1522014-08-28 07:27:44 -07001500status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const
1501{
1502 if (mTrack == 0) return NO_INIT;
1503 return mTrack->getTimestamp(ts);
1504}
1505
Marco Nelissen4110c102012-03-29 09:31:28 -07001506status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1507{
1508 if (mTrack == 0) return NO_INIT;
1509 *frameswritten = mBytesWritten / frameSize();
1510 return OK;
1511}
1512
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001513status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
1514{
1515 if (mTrack == 0) return NO_INIT;
1516 return mTrack->setParameters(keyValuePairs);
1517}
1518
1519String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
1520{
1521 if (mTrack == 0) return String8::empty();
1522 return mTrack->getParameters(keys);
1523}
1524
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001525void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
1526 mAttributes = attributes;
1527}
1528
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001529void MediaPlayerService::AudioOutput::deleteRecycledTrack()
1530{
1531 ALOGV("deleteRecycledTrack");
1532
1533 if (mRecycledTrack != 0) {
1534
1535 if (mCallbackData != NULL) {
1536 mCallbackData->setOutput(NULL);
1537 mCallbackData->endTrackSwitch();
1538 }
1539
1540 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1541 mRecycledTrack->flush();
1542 }
1543 // An offloaded track isn't flushed because the STREAM_END is reported
1544 // slightly prematurely to allow time for the gapless track switch
1545 // but this means that if we decide not to recycle the track there
1546 // could be a small amount of residual data still playing. We leave
1547 // AudioFlinger to drain the track.
1548
1549 mRecycledTrack.clear();
1550 delete mCallbackData;
1551 mCallbackData = NULL;
1552 close();
1553 }
1554}
1555
Andreas Huber20111aa2009-07-14 16:56:47 -07001556status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001557 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1558 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001559 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001560 audio_output_flags_t flags,
1561 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001562{
Andreas Huber20111aa2009-07-14 16:56:47 -07001563 mCallback = cb;
1564 mCallbackCookie = cookie;
1565
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001566 // Check argument "bufferCount" against the mininum buffer count
1567 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001568 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001569 bufferCount = mMinBufferCount;
1570
1571 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001572 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1573 format, bufferCount, mSessionId, flags);
Glenn Kasten1127d652012-11-14 08:44:39 -08001574 uint32_t afSampleRate;
Glenn Kasten7da35f22012-11-14 12:54:39 -08001575 size_t afFrameCount;
Glenn Kastenbce50bf2014-02-27 15:29:51 -08001576 size_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001577
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001578 // offloading is only supported in callback mode for now.
1579 // offloadInfo must be present if offload flag is set
1580 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1581 ((cb == NULL) || (offloadInfo == NULL))) {
1582 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001583 }
1584
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001585 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1586 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1587 } else {
1588 uint32_t afSampleRate;
1589 size_t afFrameCount;
1590
1591 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1592 return NO_INIT;
1593 }
1594 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1595 return NO_INIT;
1596 }
1597
1598 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
1599 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001600
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001601 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001602 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001603 if (0 == channelMask) {
1604 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1605 return NO_INIT;
1606 }
1607 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001608
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001609 // Check whether we can recycle the track
1610 bool reuse = false;
1611 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001612
Glenn Kasten2799d742013-05-30 14:33:29 -07001613 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001614 // check whether we are switching between two offloaded tracks
1615 bothOffloaded = (flags & mRecycledTrack->getFlags()
1616 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07001617
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001618 // check if the existing track can be reused as-is, or if a new track needs to be created.
1619 reuse = true;
1620
Marco Nelissen67295b52012-06-11 14:52:53 -07001621 if ((mCallbackData == NULL && mCallback != NULL) ||
1622 (mCallbackData != NULL && mCallback == NULL)) {
1623 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1624 ALOGV("can't chain callback and write");
1625 reuse = false;
1626 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001627 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1628 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07001629 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001630 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07001631 reuse = false;
1632 } else if (flags != mFlags) {
1633 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1634 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001635 } else if (mRecycledTrack->format() != format) {
1636 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001637 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001638 } else {
1639 ALOGV("no track available to recycle");
1640 }
1641
1642 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1643
1644 // If we can't recycle and both tracks are offloaded
1645 // we must close the previous output before opening a new one
1646 if (bothOffloaded && !reuse) {
1647 ALOGV("both offloaded and not recycling");
1648 deleteRecycledTrack();
1649 }
1650
1651 sp<AudioTrack> t;
1652 CallbackData *newcbd = NULL;
1653
1654 // We don't attempt to create a new track if we are recycling an
1655 // offloaded track. But, if we are recycling a non-offloaded or we
1656 // are switching where one is offloaded and one isn't then we create
1657 // the new track in advance so that we can read additional stream info
1658
1659 if (!(reuse && bothOffloaded)) {
1660 ALOGV("creating new AudioTrack");
1661
1662 if (mCallback != NULL) {
1663 newcbd = new CallbackData(this);
1664 t = new AudioTrack(
1665 mStreamType,
1666 sampleRate,
1667 format,
1668 channelMask,
1669 frameCount,
1670 flags,
1671 CallbackWrapper,
1672 newcbd,
1673 0, // notification frames
1674 mSessionId,
1675 AudioTrack::TRANSFER_CALLBACK,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001676 offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -08001677 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001678 mPid,
1679 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001680 } else {
1681 t = new AudioTrack(
1682 mStreamType,
1683 sampleRate,
1684 format,
1685 channelMask,
1686 frameCount,
1687 flags,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001688 NULL, // callback
1689 NULL, // user data
1690 0, // notification frames
1691 mSessionId,
1692 AudioTrack::TRANSFER_DEFAULT,
1693 NULL, // offload info
Marco Nelissend457c972014-02-11 08:47:07 -08001694 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001695 mPid,
1696 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001697 }
1698
1699 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1700 ALOGE("Unable to create audio track");
1701 delete newcbd;
1702 return NO_INIT;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001703 } else {
1704 // successful AudioTrack initialization implies a legacy stream type was generated
1705 // from the audio attributes
1706 mStreamType = t->streamType();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001707 }
1708 }
1709
1710 if (reuse) {
1711 CHECK(mRecycledTrack != NULL);
1712
1713 if (!bothOffloaded) {
1714 if (mRecycledTrack->frameCount() != t->frameCount()) {
1715 ALOGV("framecount differs: %u/%u frames",
1716 mRecycledTrack->frameCount(), t->frameCount());
1717 reuse = false;
1718 }
1719 }
1720
Marco Nelissen67295b52012-06-11 14:52:53 -07001721 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001722 ALOGV("chaining to next output and recycling track");
Marco Nelissen67295b52012-06-11 14:52:53 -07001723 close();
1724 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001725 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001726 if (mCallbackData != NULL) {
1727 mCallbackData->setOutput(this);
1728 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001729 delete newcbd;
1730 return OK;
1731 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001732 }
1733
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001734 // we're not going to reuse the track, unblock and flush it
1735 // this was done earlier if both tracks are offloaded
1736 if (!bothOffloaded) {
1737 deleteRecycledTrack();
1738 }
1739
1740 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1741
Marco Nelissen67295b52012-06-11 14:52:53 -07001742 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001743 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001744 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001745
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001746 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001747 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001748 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001749 uint32_t pos;
1750 if (t->getPosition(&pos) == OK) {
1751 mBytesWritten = uint64_t(pos) * t->frameSize();
1752 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001753 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001754
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001755 status_t res = NO_ERROR;
1756 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1757 res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1758 if (res == NO_ERROR) {
1759 t->setAuxEffectSendLevel(mSendLevel);
1760 res = t->attachAuxEffect(mAuxEffectId);
1761 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001762 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001763 ALOGV("open() DONE status %d", res);
1764 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001765}
1766
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001767status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001768{
Steve Block3856b092011-10-20 11:56:00 +01001769 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001770 if (mCallbackData != NULL) {
1771 mCallbackData->endTrackSwitch();
1772 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001773 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001774 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001775 mTrack->setAuxEffectSendLevel(mSendLevel);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001776 return mTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001777 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001778 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001779}
1780
Marco Nelissen6b74d672012-02-28 16:07:44 -08001781void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1782 mNextOutput = nextOutput;
1783}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001784
1785
Marco Nelissen6b74d672012-02-28 16:07:44 -08001786void MediaPlayerService::AudioOutput::switchToNextOutput() {
1787 ALOGV("switchToNextOutput");
1788 if (mNextOutput != NULL) {
1789 if (mCallbackData != NULL) {
1790 mCallbackData->beginTrackSwitch();
1791 }
1792 delete mNextOutput->mCallbackData;
1793 mNextOutput->mCallbackData = mCallbackData;
1794 mCallbackData = NULL;
1795 mNextOutput->mRecycledTrack = mTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001796 mTrack.clear();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001797 mNextOutput->mSampleRateHz = mSampleRateHz;
1798 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001799 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001800 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001801 }
1802}
1803
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001804ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1805{
Glenn Kastenadad3d72014-02-21 14:51:43 -08001806 LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
Andreas Huber20111aa2009-07-14 16:56:47 -07001807
Steve Block3856b092011-10-20 11:56:00 +01001808 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07001809 if (mTrack != 0) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001810 ssize_t ret = mTrack->write(buffer, size);
Andy Hunga31335a2014-08-20 17:37:59 -07001811 if (ret >= 0) {
1812 mBytesWritten += ret;
1813 }
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001814 return ret;
1815 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001816 return NO_INIT;
1817}
1818
1819void MediaPlayerService::AudioOutput::stop()
1820{
Steve Block3856b092011-10-20 11:56:00 +01001821 ALOGV("stop");
Glenn Kasten2799d742013-05-30 14:33:29 -07001822 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001823}
1824
1825void MediaPlayerService::AudioOutput::flush()
1826{
Steve Block3856b092011-10-20 11:56:00 +01001827 ALOGV("flush");
Glenn Kasten2799d742013-05-30 14:33:29 -07001828 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001829}
1830
1831void MediaPlayerService::AudioOutput::pause()
1832{
Steve Block3856b092011-10-20 11:56:00 +01001833 ALOGV("pause");
Glenn Kasten2799d742013-05-30 14:33:29 -07001834 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001835}
1836
1837void MediaPlayerService::AudioOutput::close()
1838{
Steve Block3856b092011-10-20 11:56:00 +01001839 ALOGV("close");
Glenn Kasten2799d742013-05-30 14:33:29 -07001840 mTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001841}
1842
1843void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1844{
Steve Block3856b092011-10-20 11:56:00 +01001845 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001846 mLeftVolume = left;
1847 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07001848 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001849 mTrack->setVolume(left, right);
1850 }
1851}
1852
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001853status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1854{
1855 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1856 status_t res = NO_ERROR;
Glenn Kasten2799d742013-05-30 14:33:29 -07001857 if (mTrack != 0) {
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001858 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1859 } else {
1860 res = NO_INIT;
1861 }
1862 mPlaybackRatePermille = ratePermille;
1863 if (mSampleRateHz != 0) {
1864 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1865 }
1866 return res;
1867}
1868
Eric Laurent2beeb502010-07-16 07:43:46 -07001869status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1870{
Steve Block3856b092011-10-20 11:56:00 +01001871 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001872 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07001873 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001874 return mTrack->setAuxEffectSendLevel(level);
1875 }
1876 return NO_ERROR;
1877}
1878
1879status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1880{
Steve Block3856b092011-10-20 11:56:00 +01001881 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001882 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07001883 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001884 return mTrack->attachAuxEffect(effectId);
1885 }
1886 return NO_ERROR;
1887}
1888
Andreas Huber20111aa2009-07-14 16:56:47 -07001889// static
1890void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001891 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001892 //ALOGV("callbackwrapper");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001893 CallbackData *data = (CallbackData*)cookie;
1894 data->lock();
1895 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001896 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001897 if (me == NULL) {
1898 // no output set, likely because the track was scheduled to be reused
1899 // by another player, but the format turned out to be incompatible.
1900 data->unlock();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001901 if (buffer != NULL) {
1902 buffer->size = 0;
1903 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001904 return;
1905 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001906
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001907 switch(event) {
1908 case AudioTrack::EVENT_MORE_DATA: {
1909 size_t actualSize = (*me->mCallback)(
1910 me, buffer->raw, buffer->size, me->mCallbackCookie,
1911 CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001912
aarti jadhav-gaikwadf2575572014-08-13 15:04:39 +05301913 if ((me->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0 &&
1914 actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001915 // We've reached EOS but the audio track is not stopped yet,
1916 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001917
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001918 memset(buffer->raw, 0, buffer->size);
1919 actualSize = buffer->size;
1920 }
1921
1922 buffer->size = actualSize;
1923 } break;
1924
1925
1926 case AudioTrack::EVENT_STREAM_END:
1927 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
1928 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1929 me->mCallbackCookie, CB_EVENT_STREAM_END);
1930 break;
1931
1932 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
1933 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
1934 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1935 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
1936 break;
1937
1938 default:
1939 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001940 }
1941
Marco Nelissen6b74d672012-02-28 16:07:44 -08001942 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001943}
1944
Marco Nelissen4110c102012-03-29 09:31:28 -07001945int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001946{
1947 return mSessionId;
1948}
1949
Eric Laurent6f59db12013-07-26 17:16:50 -07001950uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
1951{
1952 if (mTrack == 0) return 0;
1953 return mTrack->getSampleRate();
1954}
1955
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001956#undef LOG_TAG
1957#define LOG_TAG "AudioCache"
Eric Laurent3d00aa62013-09-24 09:53:27 -07001958MediaPlayerService::AudioCache::AudioCache(const sp<IMemoryHeap>& heap) :
1959 mHeap(heap), mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
Andy Hunga31335a2014-08-20 17:37:59 -07001960 mFrameSize(1), mError(NO_ERROR), mCommandComplete(false)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001961{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001962}
1963
1964uint32_t MediaPlayerService::AudioCache::latency () const
1965{
1966 return 0;
1967}
1968
1969float MediaPlayerService::AudioCache::msecsPerFrame() const
1970{
1971 return mMsecsPerFrame;
1972}
1973
Marco Nelissen4110c102012-03-29 09:31:28 -07001974status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001975{
1976 if (position == 0) return BAD_VALUE;
Andy Hunga31335a2014-08-20 17:37:59 -07001977 *position = mSize / mFrameSize;
Eric Laurent342e9cf2010-01-19 17:37:09 -08001978 return NO_ERROR;
1979}
1980
Lajos Molnar06ad1522014-08-28 07:27:44 -07001981status_t MediaPlayerService::AudioCache::getTimestamp(AudioTimestamp &ts) const
1982{
1983 ts.mPosition = mSize / mFrameSize;
1984 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
1985 ts.mTime.tv_sec = now / 1000000000LL;
1986 ts.mTime.tv_nsec = now - (1000000000LL * ts.mTime.tv_sec);
1987 return NO_ERROR;
1988}
1989
Marco Nelissen4110c102012-03-29 09:31:28 -07001990status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
1991{
1992 if (written == 0) return BAD_VALUE;
Andy Hunga31335a2014-08-20 17:37:59 -07001993 *written = mSize / mFrameSize;
Marco Nelissen4110c102012-03-29 09:31:28 -07001994 return NO_ERROR;
1995}
1996
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001997////////////////////////////////////////////////////////////////////////////////
1998
1999struct CallbackThread : public Thread {
2000 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
2001 MediaPlayerBase::AudioSink::AudioCallback cb,
2002 void *cookie);
2003
2004protected:
2005 virtual ~CallbackThread();
2006
2007 virtual bool threadLoop();
2008
2009private:
2010 wp<MediaPlayerBase::AudioSink> mSink;
2011 MediaPlayerBase::AudioSink::AudioCallback mCallback;
2012 void *mCookie;
2013 void *mBuffer;
2014 size_t mBufferSize;
2015
2016 CallbackThread(const CallbackThread &);
2017 CallbackThread &operator=(const CallbackThread &);
2018};
2019
2020CallbackThread::CallbackThread(
2021 const wp<MediaPlayerBase::AudioSink> &sink,
2022 MediaPlayerBase::AudioSink::AudioCallback cb,
2023 void *cookie)
2024 : mSink(sink),
2025 mCallback(cb),
2026 mCookie(cookie),
2027 mBuffer(NULL),
2028 mBufferSize(0) {
2029}
2030
2031CallbackThread::~CallbackThread() {
2032 if (mBuffer) {
2033 free(mBuffer);
2034 mBuffer = NULL;
2035 }
2036}
2037
2038bool CallbackThread::threadLoop() {
2039 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
2040 if (sink == NULL) {
2041 return false;
2042 }
2043
2044 if (mBuffer == NULL) {
2045 mBufferSize = sink->bufferSize();
2046 mBuffer = malloc(mBufferSize);
2047 }
2048
2049 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002050 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
2051 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002052
2053 if (actualSize > 0) {
2054 sink->write(mBuffer, actualSize);
Andy Hunga31335a2014-08-20 17:37:59 -07002055 // Could return false on sink->write() error or short count.
2056 // Not necessarily appropriate but would work for AudioCache behavior.
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002057 }
2058
2059 return true;
2060}
2061
2062////////////////////////////////////////////////////////////////////////////////
2063
Andreas Huber20111aa2009-07-14 16:56:47 -07002064status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08002065 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
2066 audio_format_t format, int bufferCount,
Mark Salyzyn77342f72014-06-18 16:31:32 -07002067 AudioCallback cb, void *cookie, audio_output_flags_t /*flags*/,
2068 const audio_offload_info_t* /*offloadInfo*/)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002069{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08002070 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08002071 if (mHeap->getHeapID() < 0) {
2072 return NO_INIT;
2073 }
Andreas Huber20111aa2009-07-14 16:56:47 -07002074
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002075 mSampleRate = sampleRate;
2076 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08002077 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002078 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andy Hunga31335a2014-08-20 17:37:59 -07002079 mFrameSize = audio_is_linear_pcm(mFormat)
2080 ? mChannelCount * audio_bytes_per_sample(mFormat) : 1;
2081 mFrameCount = mHeap->getSize() / mFrameSize;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002082
2083 if (cb != NULL) {
2084 mCallbackThread = new CallbackThread(this, cb, cookie);
2085 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002086 return NO_ERROR;
2087}
2088
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002089status_t MediaPlayerService::AudioCache::start() {
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002090 if (mCallbackThread != NULL) {
2091 mCallbackThread->run("AudioCache callback");
2092 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002093 return NO_ERROR;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002094}
2095
2096void MediaPlayerService::AudioCache::stop() {
2097 if (mCallbackThread != NULL) {
2098 mCallbackThread->requestExitAndWait();
2099 }
2100}
2101
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002102ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
2103{
Steve Block3856b092011-10-20 11:56:00 +01002104 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002105 if ((buffer == 0) || (size == 0)) return size;
2106
2107 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
2108 if (p == NULL) return NO_INIT;
2109 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01002110 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
Andy Hunga31335a2014-08-20 17:37:59 -07002111
2112 bool overflow = mSize + size > mHeap->getSize();
2113 if (overflow) {
Steve Block29357bc2012-01-06 19:20:56 +00002114 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002115 size = mHeap->getSize() - mSize;
2116 }
Andy Hunga31335a2014-08-20 17:37:59 -07002117 size -= size % mFrameSize; // consume only integral amounts of frame size
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002118 memcpy(p, buffer, size);
2119 mSize += size;
Andy Hunga31335a2014-08-20 17:37:59 -07002120
2121 if (overflow) {
2122 // Signal heap filled here (last frame may be truncated).
2123 // After this point, no more data should be written as the
2124 // heap is filled and the AudioCache should be effectively
2125 // immutable with respect to future writes.
2126 //
2127 // It is thus safe for another thread to read the AudioCache.
2128 mCommandComplete = true;
2129 mSignal.signal();
2130 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002131 return size;
2132}
2133
2134// call with lock held
2135status_t MediaPlayerService::AudioCache::wait()
2136{
2137 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08002138 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002139 mSignal.wait(mLock);
2140 }
2141 mCommandComplete = false;
2142
2143 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01002144 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002145 } else {
Steve Block3856b092011-10-20 11:56:00 +01002146 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002147 }
2148 return mError;
2149}
2150
Gloria Wangb483c472011-04-11 17:23:27 -07002151void MediaPlayerService::AudioCache::notify(
Mark Salyzyn77342f72014-06-18 16:31:32 -07002152 void* cookie, int msg, int ext1, int ext2, const Parcel* /*obj*/)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002153{
Steve Block3856b092011-10-20 11:56:00 +01002154 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002155 AudioCache* p = static_cast<AudioCache*>(cookie);
2156
2157 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08002158 switch (msg)
2159 {
2160 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00002161 ALOGE("Error %d, %d occurred", ext1, ext2);
Dave Sparks8eb80112009-12-09 20:20:26 -08002162 break;
2163 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01002164 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08002165 break;
2166 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01002167 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08002168 break;
2169 default:
Steve Block3856b092011-10-20 11:56:00 +01002170 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08002171 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002172 }
2173
2174 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08002175 Mutex::Autolock lock(p->mLock);
Chong Zhangdf2592b2014-10-24 18:53:44 -07002176 if (msg == MEDIA_ERROR) {
2177 p->mError = ext1;
2178 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002179 p->mCommandComplete = true;
2180 p->mSignal.signal();
2181}
2182
Marco Nelissen4110c102012-03-29 09:31:28 -07002183int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07002184{
2185 return 0;
2186}
2187
Eric Laurent6f59db12013-07-26 17:16:50 -07002188uint32_t MediaPlayerService::AudioCache::getSampleRate() const
2189{
2190 if (mMsecsPerFrame == 0) {
2191 return 0;
2192 }
2193 return (uint32_t)(1.e3 / mMsecsPerFrame);
2194}
2195
Gloria Wang7cf180c2011-02-19 18:37:57 -08002196void MediaPlayerService::addBatteryData(uint32_t params)
2197{
2198 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002199
2200 int32_t time = systemTime() / 1000000L;
2201
2202 // change audio output devices. This notification comes from AudioFlinger
2203 if ((params & kBatteryDataSpeakerOn)
2204 || (params & kBatteryDataOtherAudioDeviceOn)) {
2205
2206 int deviceOn[NUM_AUDIO_DEVICES];
2207 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2208 deviceOn[i] = 0;
2209 }
2210
2211 if ((params & kBatteryDataSpeakerOn)
2212 && (params & kBatteryDataOtherAudioDeviceOn)) {
2213 deviceOn[SPEAKER_AND_OTHER] = 1;
2214 } else if (params & kBatteryDataSpeakerOn) {
2215 deviceOn[SPEAKER] = 1;
2216 } else {
2217 deviceOn[OTHER_AUDIO_DEVICE] = 1;
2218 }
2219
2220 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2221 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
2222
2223 if (mBatteryAudio.refCount > 0) { // if playing audio
2224 if (!deviceOn[i]) {
2225 mBatteryAudio.lastTime[i] += time;
2226 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2227 mBatteryAudio.lastTime[i] = 0;
2228 } else {
2229 mBatteryAudio.lastTime[i] = 0 - time;
2230 }
2231 }
2232
2233 mBatteryAudio.deviceOn[i] = deviceOn[i];
2234 }
2235 }
2236 return;
2237 }
2238
2239 // an sudio stream is started
2240 if (params & kBatteryDataAudioFlingerStart) {
2241 // record the start time only if currently no other audio
2242 // is being played
2243 if (mBatteryAudio.refCount == 0) {
2244 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2245 if (mBatteryAudio.deviceOn[i]) {
2246 mBatteryAudio.lastTime[i] -= time;
2247 }
2248 }
2249 }
2250
2251 mBatteryAudio.refCount ++;
2252 return;
2253
2254 } else if (params & kBatteryDataAudioFlingerStop) {
2255 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002256 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002257 return;
2258 }
2259
2260 // record the stop time only if currently this is the only
2261 // audio being played
2262 if (mBatteryAudio.refCount == 1) {
2263 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2264 if (mBatteryAudio.deviceOn[i]) {
2265 mBatteryAudio.lastTime[i] += time;
2266 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2267 mBatteryAudio.lastTime[i] = 0;
2268 }
2269 }
2270 }
2271
2272 mBatteryAudio.refCount --;
2273 return;
2274 }
2275
Gloria Wang7cf180c2011-02-19 18:37:57 -08002276 int uid = IPCThreadState::self()->getCallingUid();
2277 if (uid == AID_MEDIA) {
2278 return;
2279 }
2280 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08002281
2282 if (index < 0) { // create a new entry for this UID
2283 BatteryUsageInfo info;
2284 info.audioTotalTime = 0;
2285 info.videoTotalTime = 0;
2286 info.audioLastTime = 0;
2287 info.videoLastTime = 0;
2288 info.refCount = 0;
2289
Gloria Wang9ee159b2011-02-24 14:51:45 -08002290 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00002291 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002292 return;
2293 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08002294 }
2295
2296 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
2297
2298 if (params & kBatteryDataCodecStarted) {
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 } else {
2308 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002309 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002310 return;
2311 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002312 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002313 mBatteryData.removeItem(uid);
2314 return;
2315 }
2316
2317 if (params & kBatteryDataTrackAudio) {
2318 info.audioLastTime += time;
2319 info.refCount --;
2320 }
2321 if (params & kBatteryDataTrackVideo) {
2322 info.videoLastTime += time;
2323 info.refCount --;
2324 }
2325
2326 // no stream is being played by this UID
2327 if (info.refCount == 0) {
2328 info.audioTotalTime += info.audioLastTime;
2329 info.audioLastTime = 0;
2330 info.videoTotalTime += info.videoLastTime;
2331 info.videoLastTime = 0;
2332 }
2333 }
2334}
2335
2336status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2337 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002338
2339 // audio output devices usage
2340 int32_t time = systemTime() / 1000000L; //in ms
2341 int32_t totalTime;
2342
2343 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2344 totalTime = mBatteryAudio.totalTime[i];
2345
2346 if (mBatteryAudio.deviceOn[i]
2347 && (mBatteryAudio.lastTime[i] != 0)) {
2348 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2349 totalTime += tmpTime;
2350 }
2351
2352 reply->writeInt32(totalTime);
2353 // reset the total time
2354 mBatteryAudio.totalTime[i] = 0;
2355 }
2356
2357 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002358 BatteryUsageInfo info;
2359 int size = mBatteryData.size();
2360
2361 reply->writeInt32(size);
2362 int i = 0;
2363
2364 while (i < size) {
2365 info = mBatteryData.valueAt(i);
2366
2367 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2368 reply->writeInt32(info.audioTotalTime);
2369 reply->writeInt32(info.videoTotalTime);
2370
2371 info.audioTotalTime = 0;
2372 info.videoTotalTime = 0;
2373
2374 // remove the UID entry where no stream is being played
2375 if (info.refCount <= 0) {
2376 mBatteryData.removeItemsAt(i);
2377 size --;
2378 i --;
2379 }
2380 i++;
2381 }
2382 return NO_ERROR;
2383}
nikoa64c8c72009-07-20 15:07:26 -07002384} // namespace android