blob: f113e21d814fc8b2a32c9e2f277d40c1295a5ce8 [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>
Marco Nelissenf09611f2015-02-13 14:12:42 -080062#include <media/stagefright/foundation/ALooperRoster.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080063
Dima Zavin64760242011-05-11 14:15:23 -070064#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070065
Gloria Wang7cf180c2011-02-19 18:37:57 -080066#include <private/android_filesystem_config.h>
67
James Dong559bf282012-03-28 10:29:14 -070068#include "ActivityManager.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080069#include "MediaRecorderClient.h"
70#include "MediaPlayerService.h"
71#include "MetadataRetrieverClient.h"
John Grossman44a7e422012-06-21 17:29:24 -070072#include "MediaPlayerFactory.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080073
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
Marco Nelissenf09611f2015-02-13 14:12:42 -0800251extern ALooperRoster gLooperRoster;
252
253
Dave Burked681bbb2011-08-30 14:39:17 +0100254static bool checkPermission(const char* permissionString) {
255#ifndef HAVE_ANDROID_OS
256 return true;
257#endif
258 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
259 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000260 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100261 return ok;
262}
263
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800264// 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 -0800265/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
266/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
267
268void MediaPlayerService::instantiate() {
269 defaultServiceManager()->addService(
270 String16("media.player"), new MediaPlayerService());
271}
272
273MediaPlayerService::MediaPlayerService()
274{
Steve Block3856b092011-10-20 11:56:00 +0100275 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800276 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800277
278 mBatteryAudio.refCount = 0;
279 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
280 mBatteryAudio.deviceOn[i] = 0;
281 mBatteryAudio.lastTime[i] = 0;
282 mBatteryAudio.totalTime[i] = 0;
283 }
284 // speaker is on by default
285 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700286
Chong Zhang0b30fd42014-07-23 14:46:05 -0700287 // reset battery stats
288 // if the mediaserver has crashed, battery stats could be left
289 // in bad state, reset the state upon service start.
290 const sp<IServiceManager> sm(defaultServiceManager());
291 if (sm != NULL) {
292 const String16 name("batterystats");
Warren Rehmaned849b82015-03-05 15:26:12 +0900293 // use checkService() to avoid blocking if service is not up yet
Chong Zhang0b30fd42014-07-23 14:46:05 -0700294 sp<IBatteryStats> batteryStats =
Warren Rehmaned849b82015-03-05 15:26:12 +0900295 interface_cast<IBatteryStats>(sm->checkService(name));
Chong Zhang0b30fd42014-07-23 14:46:05 -0700296 if (batteryStats != NULL) {
297 batteryStats->noteResetVideo();
298 batteryStats->noteResetAudio();
299 }
300 }
301
John Grossman44a7e422012-06-21 17:29:24 -0700302 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303}
304
305MediaPlayerService::~MediaPlayerService()
306{
Steve Block3856b092011-10-20 11:56:00 +0100307 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800308}
309
Glenn Kastenf37971f2012-02-03 11:06:53 -0800310sp<IMediaRecorder> MediaPlayerService::createMediaRecorder()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800311{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800312 pid_t pid = IPCThreadState::self()->getCallingPid();
Gloria Wangdac6a312009-10-29 15:46:37 -0700313 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
314 wp<MediaRecorderClient> w = recorder;
315 Mutex::Autolock lock(mLock);
316 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100317 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800318 return recorder;
319}
320
Gloria Wangdac6a312009-10-29 15:46:37 -0700321void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
322{
323 Mutex::Autolock lock(mLock);
324 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100325 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700326}
327
Glenn Kastenf37971f2012-02-03 11:06:53 -0800328sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800329{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800330 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800331 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100332 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800333 return retriever;
334}
335
Glenn Kastenf37971f2012-02-03 11:06:53 -0800336sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100337 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800338{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800339 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800340 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700341
342 sp<Client> c = new Client(
343 this, pid, connId, client, audioSessionId,
344 IPCThreadState::self()->getCallingUid());
345
Steve Block3856b092011-10-20 11:56:00 +0100346 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100347 IPCThreadState::self()->getCallingUid());
348
349 wp<Client> w = c;
350 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800351 Mutex::Autolock lock(mLock);
352 mClients.add(w);
353 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800354 return c;
355}
356
Lajos Molnar1381d4b2014-08-07 15:18:35 -0700357sp<IMediaCodecList> MediaPlayerService::getCodecList() const {
358 return MediaCodecList::getLocalInstance();
359}
360
Andreas Huber318ad9c2009-10-15 13:46:54 -0700361sp<IOMX> MediaPlayerService::getOMX() {
362 Mutex::Autolock autoLock(mLock);
363
364 if (mOMX.get() == NULL) {
365 mOMX = new OMX;
366 }
367
368 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700369}
370
Andreas Hubered3e3e02012-03-26 11:13:27 -0700371sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700372 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700373}
374
Jeff Tinkercc82dc62013-02-08 10:18:35 -0800375sp<IDrm> MediaPlayerService::makeDrm() {
376 return new Drm;
377}
378
Andreas Huber279dcd82013-01-30 10:41:25 -0800379sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
380 return new HDCP(createEncryptionModule);
Andreas Huber59451f82012-09-18 10:36:32 -0700381}
382
Jeff Brown2013a542012-09-04 21:38:42 -0700383sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
384 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownaba33d52012-09-07 17:38:58 -0700385 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
386 return NULL;
387 }
388
Jeff Brownced24b32012-09-05 17:48:03 -0700389 return new RemoteDisplay(client, iface.string());
Jeff Brown2013a542012-09-04 21:38:42 -0700390}
391
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800392status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
393{
394 const size_t SIZE = 256;
395 char buffer[SIZE];
396 String8 result;
397
398 result.append(" AudioOutput\n");
399 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
400 mStreamType, mLeftVolume, mRightVolume);
401 result.append(buffer);
402 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800403 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800404 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700405 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
406 mAuxEffectId, mSendLevel);
407 result.append(buffer);
408
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800409 ::write(fd, result.string(), result.size());
410 if (mTrack != 0) {
411 mTrack->dump(fd, args);
412 }
413 return NO_ERROR;
414}
415
416status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
417{
418 const size_t SIZE = 256;
419 char buffer[SIZE];
420 String8 result;
421 result.append(" Client\n");
422 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
423 mPid, mConnId, mStatus, mLoop?"true": "false");
424 result.append(buffer);
425 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700426 if (mPlayer != NULL) {
427 mPlayer->dump(fd, args);
428 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800429 if (mAudioOutput != 0) {
430 mAudioOutput->dump(fd, args);
431 }
432 write(fd, "\n", 1);
433 return NO_ERROR;
434}
435
Marco Nelissenf09611f2015-02-13 14:12:42 -0800436/**
437 * The only arguments this understands right now are -c, -von and -voff,
438 * which are parsed by ALooperRoster::dump()
439 */
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800440status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
441{
442 const size_t SIZE = 256;
443 char buffer[SIZE];
444 String8 result;
Ravi Kumar Alamanda330c8e32014-12-22 10:05:29 +0530445 SortedVector< sp<Client> > clients; //to serialise the mutex unlock & client destruction.
446 SortedVector< sp<MediaRecorderClient> > mediaRecorderClients;
447
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800448 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
449 snprintf(buffer, SIZE, "Permission Denial: "
450 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
451 IPCThreadState::self()->getCallingPid(),
452 IPCThreadState::self()->getCallingUid());
453 result.append(buffer);
454 } else {
455 Mutex::Autolock lock(mLock);
456 for (int i = 0, n = mClients.size(); i < n; ++i) {
457 sp<Client> c = mClients[i].promote();
458 if (c != 0) c->dump(fd, args);
Ravi Kumar Alamanda330c8e32014-12-22 10:05:29 +0530459 clients.add(c);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800460 }
James Dongb9141222010-07-08 11:16:11 -0700461 if (mMediaRecorderClients.size() == 0) {
462 result.append(" No media recorder client\n\n");
463 } else {
464 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
465 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700466 if (c != 0) {
467 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
468 result.append(buffer);
469 write(fd, result.string(), result.size());
470 result = "\n";
471 c->dump(fd, args);
Ravi Kumar Alamanda330c8e32014-12-22 10:05:29 +0530472 mediaRecorderClients.add(c);
James Donge579e282011-10-18 22:29:20 -0700473 }
James Dongb9141222010-07-08 11:16:11 -0700474 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700475 }
476
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800477 result.append(" Files opened and/or mapped:\n");
Marco Nelissenf09611f2015-02-13 14:12:42 -0800478 snprintf(buffer, SIZE, "/proc/%d/maps", getpid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800479 FILE *f = fopen(buffer, "r");
480 if (f) {
481 while (!feof(f)) {
482 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700483 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800484 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700485 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800486 strstr(buffer, " /system/media/")) {
487 result.append(" ");
488 result.append(buffer);
489 }
490 }
491 fclose(f);
492 } else {
493 result.append("couldn't open ");
494 result.append(buffer);
495 result.append("\n");
496 }
497
Marco Nelissenf09611f2015-02-13 14:12:42 -0800498 snprintf(buffer, SIZE, "/proc/%d/fd", getpid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800499 DIR *d = opendir(buffer);
500 if (d) {
501 struct dirent *ent;
502 while((ent = readdir(d)) != NULL) {
503 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Marco Nelissenf09611f2015-02-13 14:12:42 -0800504 snprintf(buffer, SIZE, "/proc/%d/fd/%s", getpid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800505 struct stat s;
506 if (lstat(buffer, &s) == 0) {
507 if ((s.st_mode & S_IFMT) == S_IFLNK) {
508 char linkto[256];
509 int len = readlink(buffer, linkto, sizeof(linkto));
510 if(len > 0) {
511 if(len > 255) {
512 linkto[252] = '.';
513 linkto[253] = '.';
514 linkto[254] = '.';
515 linkto[255] = 0;
516 } else {
517 linkto[len] = 0;
518 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700519 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800520 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700521 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800522 strstr(linkto, "/system/media/") == linkto) {
523 result.append(" ");
524 result.append(buffer);
525 result.append(" -> ");
526 result.append(linkto);
527 result.append("\n");
528 }
529 }
530 } else {
531 result.append(" unexpected type for ");
532 result.append(buffer);
533 result.append("\n");
534 }
535 }
536 }
537 }
538 closedir(d);
539 } else {
540 result.append("couldn't open ");
541 result.append(buffer);
542 result.append("\n");
543 }
544
Marco Nelissenf09611f2015-02-13 14:12:42 -0800545 gLooperRoster.dump(fd, args);
546
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800547 bool dumpMem = false;
548 for (size_t i = 0; i < args.size(); i++) {
549 if (args[i] == String16("-m")) {
550 dumpMem = true;
551 }
552 }
553 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700554 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800556 }
557 write(fd, result.string(), result.size());
558 return NO_ERROR;
559}
560
561void MediaPlayerService::removeClient(wp<Client> client)
562{
563 Mutex::Autolock lock(mLock);
564 mClients.remove(client);
565}
566
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700567MediaPlayerService::Client::Client(
568 const sp<MediaPlayerService>& service, pid_t pid,
569 int32_t connId, const sp<IMediaPlayerClient>& client,
570 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800571{
Steve Block3856b092011-10-20 11:56:00 +0100572 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800573 mPid = pid;
574 mConnId = connId;
575 mService = service;
576 mClient = client;
577 mLoop = false;
578 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700579 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700580 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800581 mRetransmitEndpointValid = false;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700582 mAudioAttributes = NULL;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700583
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800584#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000585 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800586 mAntagonizer = new Antagonizer(notify, this);
587#endif
588}
589
590MediaPlayerService::Client::~Client()
591{
Steve Block3856b092011-10-20 11:56:00 +0100592 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800593 mAudioOutput.clear();
594 wp<Client> client(this);
595 disconnect();
596 mService->removeClient(client);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700597 if (mAudioAttributes != NULL) {
598 free(mAudioAttributes);
599 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800600}
601
602void MediaPlayerService::Client::disconnect()
603{
Steve Block3856b092011-10-20 11:56:00 +0100604 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800605 // grab local reference and clear main reference to prevent future
606 // access to object
607 sp<MediaPlayerBase> p;
608 {
609 Mutex::Autolock l(mLock);
610 p = mPlayer;
beanzdcfefde2012-11-05 09:51:43 +0800611 mClient.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800612 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700613
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800614 mPlayer.clear();
615
616 // clear the notification to prevent callbacks to dead client
617 // and reset the player. We assume the player will serialize
618 // access to itself if necessary.
619 if (p != 0) {
620 p->setNotifyCallback(0, 0);
621#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000622 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800623 mAntagonizer->kill();
624#endif
625 p->reset();
626 }
627
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700628 disconnectNativeWindow();
629
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800630 IPCThreadState::self()->flushCommands();
631}
632
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800633sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
634{
635 // determine if we have the right player type
636 sp<MediaPlayerBase> p = mPlayer;
637 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100638 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800639 p.clear();
640 }
641 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700642 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800643 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700644
Jason Simmonsdb29e522011-08-12 13:46:55 -0700645 if (p != NULL) {
646 p->setUID(mUID);
647 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700648
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800649 return p;
650}
651
John Grossmanc795b642012-02-22 15:38:35 -0800652sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
653 player_type playerType)
654{
655 ALOGV("player type = %d", playerType);
656
657 // create the right type of player
658 sp<MediaPlayerBase> p = createPlayer(playerType);
659 if (p == NULL) {
660 return p;
661 }
662
663 if (!p->hardwareOutput()) {
Marco Nelissend457c972014-02-11 08:47:07 -0800664 mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(),
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700665 mPid, mAudioAttributes);
John Grossmanc795b642012-02-22 15:38:35 -0800666 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
667 }
668
669 return p;
670}
671
672void MediaPlayerService::Client::setDataSource_post(
673 const sp<MediaPlayerBase>& p,
674 status_t status)
675{
676 ALOGV(" setDataSource");
677 mStatus = status;
678 if (mStatus != OK) {
679 ALOGE(" error: %d", mStatus);
680 return;
681 }
682
683 // Set the re-transmission endpoint if one was chosen.
684 if (mRetransmitEndpointValid) {
685 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
686 if (mStatus != NO_ERROR) {
687 ALOGE("setRetransmitEndpoint error: %d", mStatus);
688 }
689 }
690
691 if (mStatus == OK) {
692 mPlayer = p;
693 }
694}
695
Andreas Huber2db84552010-01-28 11:19:57 -0800696status_t MediaPlayerService::Client::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800697 const sp<IMediaHTTPService> &httpService,
698 const char *url,
699 const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800700{
Steve Block3856b092011-10-20 11:56:00 +0100701 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800702 if (url == NULL)
703 return UNKNOWN_ERROR;
704
Dave Burked681bbb2011-08-30 14:39:17 +0100705 if ((strncmp(url, "http://", 7) == 0) ||
706 (strncmp(url, "https://", 8) == 0) ||
707 (strncmp(url, "rtsp://", 7) == 0)) {
708 if (!checkPermission("android.permission.INTERNET")) {
709 return PERMISSION_DENIED;
710 }
711 }
712
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800713 if (strncmp(url, "content://", 10) == 0) {
714 // get a filedescriptor for the content Uri and
715 // pass it to the setDataSource(fd) method
716
717 String16 url16(url);
718 int fd = android::openContentProviderFile(url16);
719 if (fd < 0)
720 {
Steve Block29357bc2012-01-06 19:20:56 +0000721 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800722 return UNKNOWN_ERROR;
723 }
724 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
725 close(fd);
726 return mStatus;
727 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700728 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800729 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
730 if (p == NULL) {
731 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800732 }
733
Andreas Huber1b86fe02014-01-29 11:13:26 -0800734 setDataSource_post(p, p->setDataSource(httpService, url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800735 return mStatus;
736 }
737}
738
739status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
740{
Steve Block3856b092011-10-20 11:56:00 +0100741 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800742 struct stat sb;
743 int ret = fstat(fd, &sb);
744 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000745 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800746 return UNKNOWN_ERROR;
747 }
748
Steve Block3856b092011-10-20 11:56:00 +0100749 ALOGV("st_dev = %llu", sb.st_dev);
750 ALOGV("st_mode = %u", sb.st_mode);
Mark Salyzyn77342f72014-06-18 16:31:32 -0700751 ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
752 ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
Steve Block3856b092011-10-20 11:56:00 +0100753 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800754
755 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000756 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800757 ::close(fd);
758 return UNKNOWN_ERROR;
759 }
760 if (offset + length > sb.st_size) {
761 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100762 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800763 }
764
John Grossman44a7e422012-06-21 17:29:24 -0700765 player_type playerType = MediaPlayerFactory::getPlayerType(this,
766 fd,
767 offset,
768 length);
John Grossmanc795b642012-02-22 15:38:35 -0800769 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
770 if (p == NULL) {
771 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800772 }
773
774 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800775 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800776 return mStatus;
777}
778
Andreas Hubere2b10282010-11-23 11:41:34 -0800779status_t MediaPlayerService::Client::setDataSource(
780 const sp<IStreamSource> &source) {
781 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700782 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800783 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800784 if (p == NULL) {
785 return NO_INIT;
786 }
787
Andreas Hubere2b10282010-11-23 11:41:34 -0800788 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800789 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800790 return mStatus;
791}
792
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700793void MediaPlayerService::Client::disconnectNativeWindow() {
794 if (mConnectedWindow != NULL) {
795 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
796 NATIVE_WINDOW_API_MEDIA);
797
798 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000799 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700800 strerror(-err), err);
801 }
802 }
803 mConnectedWindow.clear();
804}
805
Glenn Kasten11731182011-02-08 17:26:17 -0800806status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800807 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800808{
Andy McFadden484566c2012-12-18 09:46:54 -0800809 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800810 sp<MediaPlayerBase> p = getPlayer();
811 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700812
Marco Nelissenf8880202014-11-14 07:58:25 -0800813 sp<IBinder> binder(IInterface::asBinder(bufferProducer));
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700814 if (mConnectedWindowBinder == binder) {
815 return OK;
816 }
817
818 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -0800819 if (bufferProducer != NULL) {
Marco Nelissenee08f7e2013-09-16 13:30:01 -0700820 anw = new Surface(bufferProducer, true /* controlledByApp */);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700821 status_t err = native_window_api_connect(anw.get(),
822 NATIVE_WINDOW_API_MEDIA);
823
824 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000825 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700826 // Note that we must do the reset before disconnecting from the ANW.
827 // Otherwise queue/dequeue calls could be made on the disconnected
828 // ANW, which may result in errors.
829 reset();
830
831 disconnectNativeWindow();
832
833 return err;
834 }
835 }
836
Andy McFadden484566c2012-12-18 09:46:54 -0800837 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700838 // disconnecting the old one. Otherwise queue/dequeue calls could be made
839 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -0800840 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700841
842 disconnectNativeWindow();
843
844 mConnectedWindow = anw;
845
846 if (err == OK) {
847 mConnectedWindowBinder = binder;
848 } else {
849 disconnectNativeWindow();
850 }
851
852 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800853}
854
Nicolas Catania1d187f12009-05-12 23:25:55 -0700855status_t MediaPlayerService::Client::invoke(const Parcel& request,
856 Parcel *reply)
857{
858 sp<MediaPlayerBase> p = getPlayer();
859 if (p == NULL) return UNKNOWN_ERROR;
860 return p->invoke(request, reply);
861}
862
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700863// This call doesn't need to access the native player.
864status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
865{
866 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700867 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700868
Nicolas Catania48290382009-07-10 13:53:06 -0700869 if (unmarshallFilter(filter, &allow, &status) &&
870 unmarshallFilter(filter, &drop, &status)) {
871 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700872
873 mMetadataAllow = allow;
874 mMetadataDrop = drop;
875 }
876 return status;
877}
878
Nicolas Catania48290382009-07-10 13:53:06 -0700879status_t MediaPlayerService::Client::getMetadata(
Mark Salyzyn77342f72014-06-18 16:31:32 -0700880 bool update_only, bool /*apply_filter*/, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700881{
nikoa64c8c72009-07-20 15:07:26 -0700882 sp<MediaPlayerBase> player = getPlayer();
883 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700884
nikod608a812009-07-16 16:39:53 -0700885 status_t status;
886 // Placeholder for the return code, updated by the caller.
887 reply->writeInt32(-1);
888
nikoa64c8c72009-07-20 15:07:26 -0700889 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700890
891 // We don't block notifications while we fetch the data. We clear
892 // mMetadataUpdated first so we don't lose notifications happening
893 // during the rest of this call.
894 {
895 Mutex::Autolock lock(mLock);
896 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700897 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700898 }
899 mMetadataUpdated.clear();
900 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700901
nikoa64c8c72009-07-20 15:07:26 -0700902 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700903
nikoa64c8c72009-07-20 15:07:26 -0700904 metadata.appendHeader();
905 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700906
907 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700908 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000909 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700910 return status;
911 }
912
913 // FIXME: Implement filtering on the result. Not critical since
914 // filtering takes place on the update notifications already. This
915 // would be when all the metadata are fetch and a filter is set.
916
nikod608a812009-07-16 16:39:53 -0700917 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700918 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700919 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700920}
921
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800922status_t MediaPlayerService::Client::prepareAsync()
923{
Steve Block3856b092011-10-20 11:56:00 +0100924 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800925 sp<MediaPlayerBase> p = getPlayer();
926 if (p == 0) return UNKNOWN_ERROR;
927 status_t ret = p->prepareAsync();
928#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000929 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800930 if (ret == NO_ERROR) mAntagonizer->start();
931#endif
932 return ret;
933}
934
935status_t MediaPlayerService::Client::start()
936{
Steve Block3856b092011-10-20 11:56:00 +0100937 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800938 sp<MediaPlayerBase> p = getPlayer();
939 if (p == 0) return UNKNOWN_ERROR;
940 p->setLooping(mLoop);
941 return p->start();
942}
943
944status_t MediaPlayerService::Client::stop()
945{
Steve Block3856b092011-10-20 11:56:00 +0100946 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800947 sp<MediaPlayerBase> p = getPlayer();
948 if (p == 0) return UNKNOWN_ERROR;
949 return p->stop();
950}
951
952status_t MediaPlayerService::Client::pause()
953{
Steve Block3856b092011-10-20 11:56:00 +0100954 ALOGV("[%d] pause", 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->pause();
958}
959
960status_t MediaPlayerService::Client::isPlaying(bool* state)
961{
962 *state = false;
963 sp<MediaPlayerBase> p = getPlayer();
964 if (p == 0) return UNKNOWN_ERROR;
965 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100966 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800967 return NO_ERROR;
968}
969
Wei Jia98160162015-02-04 17:01:11 -0800970status_t MediaPlayerService::Client::setPlaybackRate(float rate)
971{
972 ALOGV("[%d] setPlaybackRate(%f)", mConnId, rate);
973 sp<MediaPlayerBase> p = getPlayer();
974 if (p == 0) return UNKNOWN_ERROR;
975 return p->setPlaybackRate(rate);
976}
977
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800978status_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
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001284#undef LOG_TAG
1285#define LOG_TAG "AudioSink"
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001286MediaPlayerService::AudioOutput::AudioOutput(int sessionId, int uid, int pid,
1287 const audio_attributes_t* attr)
Andreas Huber20111aa2009-07-14 16:56:47 -07001288 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001289 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001290 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001291 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001292 mSessionId(sessionId),
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001293 mUid(uid),
Marco Nelissend457c972014-02-11 08:47:07 -08001294 mPid(pid),
Eric Laurent1948eb32012-04-13 16:50:19 -07001295 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001296 ALOGV("AudioOutput(%d)", sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -07001297 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001298 mLeftVolume = 1.0;
1299 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001300 mPlaybackRatePermille = 1000;
1301 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001302 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001303 mAuxEffectId = 0;
1304 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001305 setMinBufferCount();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001306 mAttributes = attr;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001307}
1308
1309MediaPlayerService::AudioOutput::~AudioOutput()
1310{
1311 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001312 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001313}
1314
1315void MediaPlayerService::AudioOutput::setMinBufferCount()
1316{
1317 char value[PROPERTY_VALUE_MAX];
1318 if (property_get("ro.kernel.qemu", value, 0)) {
1319 mIsOnEmulator = true;
1320 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1321 }
1322}
1323
1324bool MediaPlayerService::AudioOutput::isOnEmulator()
1325{
1326 setMinBufferCount();
1327 return mIsOnEmulator;
1328}
1329
1330int MediaPlayerService::AudioOutput::getMinBufferCount()
1331{
1332 setMinBufferCount();
1333 return mMinBufferCount;
1334}
1335
1336ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1337{
1338 if (mTrack == 0) return NO_INIT;
1339 return mTrack->frameCount() * frameSize();
1340}
1341
1342ssize_t MediaPlayerService::AudioOutput::frameCount() const
1343{
1344 if (mTrack == 0) return NO_INIT;
1345 return mTrack->frameCount();
1346}
1347
1348ssize_t MediaPlayerService::AudioOutput::channelCount() const
1349{
1350 if (mTrack == 0) return NO_INIT;
1351 return mTrack->channelCount();
1352}
1353
1354ssize_t MediaPlayerService::AudioOutput::frameSize() const
1355{
1356 if (mTrack == 0) return NO_INIT;
1357 return mTrack->frameSize();
1358}
1359
1360uint32_t MediaPlayerService::AudioOutput::latency () const
1361{
Eric Laurentdb354e52012-03-05 17:27:11 -08001362 if (mTrack == 0) return 0;
1363 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001364}
1365
1366float MediaPlayerService::AudioOutput::msecsPerFrame() const
1367{
1368 return mMsecsPerFrame;
1369}
1370
Marco Nelissen4110c102012-03-29 09:31:28 -07001371status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001372{
1373 if (mTrack == 0) return NO_INIT;
1374 return mTrack->getPosition(position);
1375}
1376
Lajos Molnar06ad1522014-08-28 07:27:44 -07001377status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const
1378{
1379 if (mTrack == 0) return NO_INIT;
1380 return mTrack->getTimestamp(ts);
1381}
1382
Marco Nelissen4110c102012-03-29 09:31:28 -07001383status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1384{
1385 if (mTrack == 0) return NO_INIT;
1386 *frameswritten = mBytesWritten / frameSize();
1387 return OK;
1388}
1389
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001390status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
1391{
1392 if (mTrack == 0) return NO_INIT;
1393 return mTrack->setParameters(keyValuePairs);
1394}
1395
1396String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
1397{
1398 if (mTrack == 0) return String8::empty();
1399 return mTrack->getParameters(keys);
1400}
1401
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001402void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
1403 mAttributes = attributes;
1404}
1405
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001406void MediaPlayerService::AudioOutput::deleteRecycledTrack()
1407{
1408 ALOGV("deleteRecycledTrack");
1409
1410 if (mRecycledTrack != 0) {
1411
1412 if (mCallbackData != NULL) {
1413 mCallbackData->setOutput(NULL);
1414 mCallbackData->endTrackSwitch();
1415 }
1416
1417 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1418 mRecycledTrack->flush();
1419 }
1420 // An offloaded track isn't flushed because the STREAM_END is reported
1421 // slightly prematurely to allow time for the gapless track switch
1422 // but this means that if we decide not to recycle the track there
1423 // could be a small amount of residual data still playing. We leave
1424 // AudioFlinger to drain the track.
1425
1426 mRecycledTrack.clear();
1427 delete mCallbackData;
1428 mCallbackData = NULL;
1429 close();
1430 }
1431}
1432
Andreas Huber20111aa2009-07-14 16:56:47 -07001433status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001434 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1435 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001436 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001437 audio_output_flags_t flags,
1438 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001439{
Andreas Huber20111aa2009-07-14 16:56:47 -07001440 mCallback = cb;
1441 mCallbackCookie = cookie;
1442
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001443 // Check argument "bufferCount" against the mininum buffer count
1444 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001445 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001446 bufferCount = mMinBufferCount;
1447
1448 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001449 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1450 format, bufferCount, mSessionId, flags);
Glenn Kasten1127d652012-11-14 08:44:39 -08001451 uint32_t afSampleRate;
Glenn Kasten7da35f22012-11-14 12:54:39 -08001452 size_t afFrameCount;
Glenn Kastenbce50bf2014-02-27 15:29:51 -08001453 size_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001454
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001455 // offloading is only supported in callback mode for now.
1456 // offloadInfo must be present if offload flag is set
1457 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1458 ((cb == NULL) || (offloadInfo == NULL))) {
1459 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001460 }
1461
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001462 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1463 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1464 } else {
1465 uint32_t afSampleRate;
1466 size_t afFrameCount;
1467
1468 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1469 return NO_INIT;
1470 }
1471 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1472 return NO_INIT;
1473 }
1474
1475 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
1476 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001477
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001478 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001479 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001480 if (0 == channelMask) {
1481 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1482 return NO_INIT;
1483 }
1484 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001485
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001486 // Check whether we can recycle the track
1487 bool reuse = false;
1488 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001489
Glenn Kasten2799d742013-05-30 14:33:29 -07001490 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001491 // check whether we are switching between two offloaded tracks
1492 bothOffloaded = (flags & mRecycledTrack->getFlags()
1493 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07001494
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001495 // check if the existing track can be reused as-is, or if a new track needs to be created.
1496 reuse = true;
1497
Marco Nelissen67295b52012-06-11 14:52:53 -07001498 if ((mCallbackData == NULL && mCallback != NULL) ||
1499 (mCallbackData != NULL && mCallback == NULL)) {
1500 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1501 ALOGV("can't chain callback and write");
1502 reuse = false;
1503 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001504 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1505 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07001506 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001507 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07001508 reuse = false;
1509 } else if (flags != mFlags) {
1510 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1511 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001512 } else if (mRecycledTrack->format() != format) {
1513 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001514 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001515 } else {
1516 ALOGV("no track available to recycle");
1517 }
1518
1519 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1520
1521 // If we can't recycle and both tracks are offloaded
1522 // we must close the previous output before opening a new one
1523 if (bothOffloaded && !reuse) {
1524 ALOGV("both offloaded and not recycling");
1525 deleteRecycledTrack();
1526 }
1527
1528 sp<AudioTrack> t;
1529 CallbackData *newcbd = NULL;
1530
1531 // We don't attempt to create a new track if we are recycling an
1532 // offloaded track. But, if we are recycling a non-offloaded or we
1533 // are switching where one is offloaded and one isn't then we create
1534 // the new track in advance so that we can read additional stream info
1535
1536 if (!(reuse && bothOffloaded)) {
1537 ALOGV("creating new AudioTrack");
1538
1539 if (mCallback != NULL) {
1540 newcbd = new CallbackData(this);
1541 t = new AudioTrack(
1542 mStreamType,
1543 sampleRate,
1544 format,
1545 channelMask,
1546 frameCount,
1547 flags,
1548 CallbackWrapper,
1549 newcbd,
1550 0, // notification frames
1551 mSessionId,
1552 AudioTrack::TRANSFER_CALLBACK,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001553 offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -08001554 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001555 mPid,
1556 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001557 } else {
1558 t = new AudioTrack(
1559 mStreamType,
1560 sampleRate,
1561 format,
1562 channelMask,
1563 frameCount,
1564 flags,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001565 NULL, // callback
1566 NULL, // user data
1567 0, // notification frames
1568 mSessionId,
1569 AudioTrack::TRANSFER_DEFAULT,
1570 NULL, // offload info
Marco Nelissend457c972014-02-11 08:47:07 -08001571 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001572 mPid,
1573 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001574 }
1575
1576 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1577 ALOGE("Unable to create audio track");
1578 delete newcbd;
1579 return NO_INIT;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001580 } else {
1581 // successful AudioTrack initialization implies a legacy stream type was generated
1582 // from the audio attributes
1583 mStreamType = t->streamType();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001584 }
1585 }
1586
1587 if (reuse) {
1588 CHECK(mRecycledTrack != NULL);
1589
1590 if (!bothOffloaded) {
1591 if (mRecycledTrack->frameCount() != t->frameCount()) {
1592 ALOGV("framecount differs: %u/%u frames",
1593 mRecycledTrack->frameCount(), t->frameCount());
1594 reuse = false;
1595 }
1596 }
1597
Marco Nelissen67295b52012-06-11 14:52:53 -07001598 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001599 ALOGV("chaining to next output and recycling track");
Marco Nelissen67295b52012-06-11 14:52:53 -07001600 close();
1601 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001602 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001603 if (mCallbackData != NULL) {
1604 mCallbackData->setOutput(this);
1605 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001606 delete newcbd;
1607 return OK;
1608 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001609 }
1610
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001611 // we're not going to reuse the track, unblock and flush it
1612 // this was done earlier if both tracks are offloaded
1613 if (!bothOffloaded) {
1614 deleteRecycledTrack();
1615 }
1616
1617 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1618
Marco Nelissen67295b52012-06-11 14:52:53 -07001619 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001620 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001621 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001622
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001623 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001624 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001625 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001626 uint32_t pos;
1627 if (t->getPosition(&pos) == OK) {
1628 mBytesWritten = uint64_t(pos) * t->frameSize();
1629 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001630 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001631
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001632 status_t res = NO_ERROR;
1633 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1634 res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1635 if (res == NO_ERROR) {
1636 t->setAuxEffectSendLevel(mSendLevel);
1637 res = t->attachAuxEffect(mAuxEffectId);
1638 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001639 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001640 ALOGV("open() DONE status %d", res);
1641 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001642}
1643
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001644status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001645{
Steve Block3856b092011-10-20 11:56:00 +01001646 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001647 if (mCallbackData != NULL) {
1648 mCallbackData->endTrackSwitch();
1649 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001650 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001651 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001652 mTrack->setAuxEffectSendLevel(mSendLevel);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001653 return mTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001654 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001655 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001656}
1657
Marco Nelissen6b74d672012-02-28 16:07:44 -08001658void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1659 mNextOutput = nextOutput;
1660}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001661
1662
Marco Nelissen6b74d672012-02-28 16:07:44 -08001663void MediaPlayerService::AudioOutput::switchToNextOutput() {
1664 ALOGV("switchToNextOutput");
1665 if (mNextOutput != NULL) {
1666 if (mCallbackData != NULL) {
1667 mCallbackData->beginTrackSwitch();
1668 }
1669 delete mNextOutput->mCallbackData;
1670 mNextOutput->mCallbackData = mCallbackData;
1671 mCallbackData = NULL;
1672 mNextOutput->mRecycledTrack = mTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001673 mTrack.clear();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001674 mNextOutput->mSampleRateHz = mSampleRateHz;
1675 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001676 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001677 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001678 }
1679}
1680
Wei Jia7d3f4df2015-03-03 15:28:00 -08001681ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size, bool blocking)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001682{
Glenn Kastenadad3d72014-02-21 14:51:43 -08001683 LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
Andreas Huber20111aa2009-07-14 16:56:47 -07001684
Steve Block3856b092011-10-20 11:56:00 +01001685 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07001686 if (mTrack != 0) {
Wei Jia7d3f4df2015-03-03 15:28:00 -08001687 ssize_t ret = mTrack->write(buffer, size, blocking);
Andy Hunga31335a2014-08-20 17:37:59 -07001688 if (ret >= 0) {
1689 mBytesWritten += ret;
1690 }
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001691 return ret;
1692 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001693 return NO_INIT;
1694}
1695
1696void MediaPlayerService::AudioOutput::stop()
1697{
Steve Block3856b092011-10-20 11:56:00 +01001698 ALOGV("stop");
Glenn Kasten2799d742013-05-30 14:33:29 -07001699 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001700}
1701
1702void MediaPlayerService::AudioOutput::flush()
1703{
Steve Block3856b092011-10-20 11:56:00 +01001704 ALOGV("flush");
Glenn Kasten2799d742013-05-30 14:33:29 -07001705 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001706}
1707
1708void MediaPlayerService::AudioOutput::pause()
1709{
Steve Block3856b092011-10-20 11:56:00 +01001710 ALOGV("pause");
Glenn Kasten2799d742013-05-30 14:33:29 -07001711 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001712}
1713
1714void MediaPlayerService::AudioOutput::close()
1715{
Steve Block3856b092011-10-20 11:56:00 +01001716 ALOGV("close");
Glenn Kasten2799d742013-05-30 14:33:29 -07001717 mTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001718}
1719
1720void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1721{
Steve Block3856b092011-10-20 11:56:00 +01001722 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001723 mLeftVolume = left;
1724 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07001725 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001726 mTrack->setVolume(left, right);
1727 }
1728}
1729
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001730status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1731{
1732 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1733 status_t res = NO_ERROR;
Glenn Kasten2799d742013-05-30 14:33:29 -07001734 if (mTrack != 0) {
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001735 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1736 } else {
1737 res = NO_INIT;
1738 }
1739 mPlaybackRatePermille = ratePermille;
1740 if (mSampleRateHz != 0) {
1741 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1742 }
1743 return res;
1744}
1745
Eric Laurent2beeb502010-07-16 07:43:46 -07001746status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1747{
Steve Block3856b092011-10-20 11:56:00 +01001748 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001749 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07001750 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001751 return mTrack->setAuxEffectSendLevel(level);
1752 }
1753 return NO_ERROR;
1754}
1755
1756status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1757{
Steve Block3856b092011-10-20 11:56:00 +01001758 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001759 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07001760 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001761 return mTrack->attachAuxEffect(effectId);
1762 }
1763 return NO_ERROR;
1764}
1765
Andreas Huber20111aa2009-07-14 16:56:47 -07001766// static
1767void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001768 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001769 //ALOGV("callbackwrapper");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001770 CallbackData *data = (CallbackData*)cookie;
1771 data->lock();
1772 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001773 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001774 if (me == NULL) {
1775 // no output set, likely because the track was scheduled to be reused
1776 // by another player, but the format turned out to be incompatible.
1777 data->unlock();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001778 if (buffer != NULL) {
1779 buffer->size = 0;
1780 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001781 return;
1782 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001783
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001784 switch(event) {
1785 case AudioTrack::EVENT_MORE_DATA: {
1786 size_t actualSize = (*me->mCallback)(
1787 me, buffer->raw, buffer->size, me->mCallbackCookie,
1788 CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001789
aarti jadhav-gaikwadf2575572014-08-13 15:04:39 +05301790 if ((me->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0 &&
1791 actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001792 // We've reached EOS but the audio track is not stopped yet,
1793 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001794
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001795 memset(buffer->raw, 0, buffer->size);
1796 actualSize = buffer->size;
1797 }
1798
1799 buffer->size = actualSize;
1800 } break;
1801
1802
1803 case AudioTrack::EVENT_STREAM_END:
1804 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
1805 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1806 me->mCallbackCookie, CB_EVENT_STREAM_END);
1807 break;
1808
1809 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
1810 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
1811 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1812 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
1813 break;
1814
1815 default:
1816 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001817 }
1818
Marco Nelissen6b74d672012-02-28 16:07:44 -08001819 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001820}
1821
Marco Nelissen4110c102012-03-29 09:31:28 -07001822int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001823{
1824 return mSessionId;
1825}
1826
Eric Laurent6f59db12013-07-26 17:16:50 -07001827uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
1828{
1829 if (mTrack == 0) return 0;
1830 return mTrack->getSampleRate();
1831}
1832
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001833////////////////////////////////////////////////////////////////////////////////
1834
1835struct CallbackThread : public Thread {
1836 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1837 MediaPlayerBase::AudioSink::AudioCallback cb,
1838 void *cookie);
1839
1840protected:
1841 virtual ~CallbackThread();
1842
1843 virtual bool threadLoop();
1844
1845private:
1846 wp<MediaPlayerBase::AudioSink> mSink;
1847 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1848 void *mCookie;
1849 void *mBuffer;
1850 size_t mBufferSize;
1851
1852 CallbackThread(const CallbackThread &);
1853 CallbackThread &operator=(const CallbackThread &);
1854};
1855
1856CallbackThread::CallbackThread(
1857 const wp<MediaPlayerBase::AudioSink> &sink,
1858 MediaPlayerBase::AudioSink::AudioCallback cb,
1859 void *cookie)
1860 : mSink(sink),
1861 mCallback(cb),
1862 mCookie(cookie),
1863 mBuffer(NULL),
1864 mBufferSize(0) {
1865}
1866
1867CallbackThread::~CallbackThread() {
1868 if (mBuffer) {
1869 free(mBuffer);
1870 mBuffer = NULL;
1871 }
1872}
1873
1874bool CallbackThread::threadLoop() {
1875 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1876 if (sink == NULL) {
1877 return false;
1878 }
1879
1880 if (mBuffer == NULL) {
1881 mBufferSize = sink->bufferSize();
1882 mBuffer = malloc(mBufferSize);
1883 }
1884
1885 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001886 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
1887 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001888
1889 if (actualSize > 0) {
1890 sink->write(mBuffer, actualSize);
Andy Hunga31335a2014-08-20 17:37:59 -07001891 // Could return false on sink->write() error or short count.
1892 // Not necessarily appropriate but would work for AudioCache behavior.
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001893 }
1894
1895 return true;
1896}
1897
1898////////////////////////////////////////////////////////////////////////////////
1899
Gloria Wang7cf180c2011-02-19 18:37:57 -08001900void MediaPlayerService::addBatteryData(uint32_t params)
1901{
1902 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001903
1904 int32_t time = systemTime() / 1000000L;
1905
1906 // change audio output devices. This notification comes from AudioFlinger
1907 if ((params & kBatteryDataSpeakerOn)
1908 || (params & kBatteryDataOtherAudioDeviceOn)) {
1909
1910 int deviceOn[NUM_AUDIO_DEVICES];
1911 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1912 deviceOn[i] = 0;
1913 }
1914
1915 if ((params & kBatteryDataSpeakerOn)
1916 && (params & kBatteryDataOtherAudioDeviceOn)) {
1917 deviceOn[SPEAKER_AND_OTHER] = 1;
1918 } else if (params & kBatteryDataSpeakerOn) {
1919 deviceOn[SPEAKER] = 1;
1920 } else {
1921 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1922 }
1923
1924 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1925 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1926
1927 if (mBatteryAudio.refCount > 0) { // if playing audio
1928 if (!deviceOn[i]) {
1929 mBatteryAudio.lastTime[i] += time;
1930 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1931 mBatteryAudio.lastTime[i] = 0;
1932 } else {
1933 mBatteryAudio.lastTime[i] = 0 - time;
1934 }
1935 }
1936
1937 mBatteryAudio.deviceOn[i] = deviceOn[i];
1938 }
1939 }
1940 return;
1941 }
1942
Marco Nelissenb7848f12014-12-04 08:57:56 -08001943 // an audio stream is started
Gloria Wang9ee159b2011-02-24 14:51:45 -08001944 if (params & kBatteryDataAudioFlingerStart) {
1945 // record the start time only if currently no other audio
1946 // is being played
1947 if (mBatteryAudio.refCount == 0) {
1948 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1949 if (mBatteryAudio.deviceOn[i]) {
1950 mBatteryAudio.lastTime[i] -= time;
1951 }
1952 }
1953 }
1954
1955 mBatteryAudio.refCount ++;
1956 return;
1957
1958 } else if (params & kBatteryDataAudioFlingerStop) {
1959 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001960 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001961 return;
1962 }
1963
1964 // record the stop time only if currently this is the only
1965 // audio being played
1966 if (mBatteryAudio.refCount == 1) {
1967 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1968 if (mBatteryAudio.deviceOn[i]) {
1969 mBatteryAudio.lastTime[i] += time;
1970 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1971 mBatteryAudio.lastTime[i] = 0;
1972 }
1973 }
1974 }
1975
1976 mBatteryAudio.refCount --;
1977 return;
1978 }
1979
Gloria Wang7cf180c2011-02-19 18:37:57 -08001980 int uid = IPCThreadState::self()->getCallingUid();
1981 if (uid == AID_MEDIA) {
1982 return;
1983 }
1984 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001985
1986 if (index < 0) { // create a new entry for this UID
1987 BatteryUsageInfo info;
1988 info.audioTotalTime = 0;
1989 info.videoTotalTime = 0;
1990 info.audioLastTime = 0;
1991 info.videoLastTime = 0;
1992 info.refCount = 0;
1993
Gloria Wang9ee159b2011-02-24 14:51:45 -08001994 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001995 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001996 return;
1997 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08001998 }
1999
2000 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
2001
2002 if (params & kBatteryDataCodecStarted) {
2003 if (params & kBatteryDataTrackAudio) {
2004 info.audioLastTime -= time;
2005 info.refCount ++;
2006 }
2007 if (params & kBatteryDataTrackVideo) {
2008 info.videoLastTime -= time;
2009 info.refCount ++;
2010 }
2011 } else {
2012 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002013 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002014 return;
2015 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002016 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002017 mBatteryData.removeItem(uid);
2018 return;
2019 }
2020
2021 if (params & kBatteryDataTrackAudio) {
2022 info.audioLastTime += time;
2023 info.refCount --;
2024 }
2025 if (params & kBatteryDataTrackVideo) {
2026 info.videoLastTime += time;
2027 info.refCount --;
2028 }
2029
2030 // no stream is being played by this UID
2031 if (info.refCount == 0) {
2032 info.audioTotalTime += info.audioLastTime;
2033 info.audioLastTime = 0;
2034 info.videoTotalTime += info.videoLastTime;
2035 info.videoLastTime = 0;
2036 }
2037 }
2038}
2039
2040status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2041 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002042
2043 // audio output devices usage
2044 int32_t time = systemTime() / 1000000L; //in ms
2045 int32_t totalTime;
2046
2047 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2048 totalTime = mBatteryAudio.totalTime[i];
2049
2050 if (mBatteryAudio.deviceOn[i]
2051 && (mBatteryAudio.lastTime[i] != 0)) {
2052 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2053 totalTime += tmpTime;
2054 }
2055
2056 reply->writeInt32(totalTime);
2057 // reset the total time
2058 mBatteryAudio.totalTime[i] = 0;
2059 }
2060
2061 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002062 BatteryUsageInfo info;
2063 int size = mBatteryData.size();
2064
2065 reply->writeInt32(size);
2066 int i = 0;
2067
2068 while (i < size) {
2069 info = mBatteryData.valueAt(i);
2070
2071 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2072 reply->writeInt32(info.audioTotalTime);
2073 reply->writeInt32(info.videoTotalTime);
2074
2075 info.audioTotalTime = 0;
2076 info.videoTotalTime = 0;
2077
2078 // remove the UID entry where no stream is being played
2079 if (info.refCount <= 0) {
2080 mBatteryData.removeItemsAt(i);
2081 size --;
2082 i --;
2083 }
2084 i++;
2085 }
2086 return NO_ERROR;
2087}
nikoa64c8c72009-07-20 15:07:26 -07002088} // namespace android