189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project/*
289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project**
389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** Copyright 2008, The Android Open Source Project
489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project**
589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");
689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** you may not use this file except in compliance with the License.
789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** You may obtain a copy of the License at
889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project**
989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project**     http://www.apache.org/licenses/LICENSE-2.0
1089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project**
1189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** Unless required by applicable law or agreed to in writing, software
1289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** distributed under the License is distributed on an "AS IS" BASIS,
1389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** See the License for the specific language governing permissions and
1589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** limitations under the License.
1689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project*/
1789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
1889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// Proxy for media player implementations
1989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
2089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project//#define LOG_NDEBUG 0
2189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#define LOG_TAG "MediaPlayerService"
2289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <utils/Log.h>
2389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
2489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <sys/types.h>
2589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <sys/stat.h>
267cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang#include <sys/time.h>
2789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <dirent.h>
2889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <unistd.h>
2989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
3089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <string.h>
316f74b0cc490a3b8523252ded00f7ca55160effd1Mathias Agopian
3289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <cutils/atomic.h>
3314d2747c7e54037e267bcff78b29e65b2181f0faNicolas Catania#include <cutils/properties.h> // for property_get
346f74b0cc490a3b8523252ded00f7ca55160effd1Mathias Agopian
356f74b0cc490a3b8523252ded00f7ca55160effd1Mathias Agopian#include <utils/misc.h>
3689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
377562408b2261d38415453378b6188f74fda99d88Mathias Agopian#include <binder/IPCThreadState.h>
387562408b2261d38415453378b6188f74fda99d88Mathias Agopian#include <binder/IServiceManager.h>
397562408b2261d38415453378b6188f74fda99d88Mathias Agopian#include <binder/MemoryHeapBase.h>
407562408b2261d38415453378b6188f74fda99d88Mathias Agopian#include <binder/MemoryBase.h>
417dae00baa6e8957be15523c46bb948bd1dde64c3Jamie Gennis#include <gui/SurfaceTextureClient.h>
421d187f1a86855f5f0694d7ec30efc9833bf7c589Nicolas Catania#include <utils/Errors.h>  // for status_t
431d187f1a86855f5f0694d7ec30efc9833bf7c589Nicolas Catania#include <utils/String8.h>
4410dbb8e97e7a81ca4867663b5517f048820b3094Marco Nelissen#include <utils/SystemClock.h>
451d187f1a86855f5f0694d7ec30efc9833bf7c589Nicolas Catania#include <utils/Vector.h>
4689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
47e104596061b219e9bce6d4db49a9d15242f8d2e5Jeff Brown#include <media/IRemoteDisplay.h>
48e104596061b219e9bce6d4db49a9d15242f8d2e5Jeff Brown#include <media/IRemoteDisplayClient.h>
4989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <media/MediaPlayerInterface.h>
5089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <media/mediarecorder.h>
5189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <media/MediaMetadataRetrieverInterface.h>
52a64c8c79af1a15911c55306d83a797fa50969f77niko#include <media/Metadata.h>
5389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <media/AudioTrack.h>
548635b7b095fbf7ffc63d3ce791891a9116ace1f6James Dong#include <media/MemoryLeakTrackUtil.h>
559cb839a0fcc98fe4278b39afb8b6d664c04f1673Eric Laurent#include <media/stagefright/MediaErrors.h>
5689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
5764760240f931714858a59c1579f07264d7182ba2Dima Zavin#include <system/audio.h>
58fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
597cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang#include <private/android_filesystem_config.h>
607cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang
61559bf2836f5da25b75bfb229fec0d20d540ee426James Dong#include "ActivityManager.h"
6289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include "MediaRecorderClient.h"
6389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include "MediaPlayerService.h"
6489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include "MetadataRetrieverClient.h"
6544a7e42f0310831e6a846d1b6bb40bf3a399bf6dJohn Grossman#include "MediaPlayerFactory.h"
6689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
6789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include "MidiFile.h"
6814d2747c7e54037e267bcff78b29e65b2181f0faNicolas Catania#include "TestPlayerStub.h"
6920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include "StagefrightPlayer.h"
70f933441648ef6a71dee783d733aac17b9508b452Andreas Huber#include "nuplayer/NuPlayerDriver.h"
7120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
7220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <OMX.h>
7314d2747c7e54037e267bcff78b29e65b2181f0faNicolas Catania
74ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber#include "Crypto.h"
75efbb781c15abf8e085f5ab8d39710bd58ea0b6e4Andreas Huber#include "HDCP.h"
76d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber#include "RemoteDisplay.h"
77ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber
78a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catanianamespace {
79a64c8c79af1a15911c55306d83a797fa50969f77nikousing android::media::Metadata;
80a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Cataniausing android::status_t;
81a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Cataniausing android::OK;
82a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Cataniausing android::BAD_VALUE;
83a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Cataniausing android::NOT_ENOUGH_DATA;
84a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Cataniausing android::Parcel;
85a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania
86a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania// Max number of entries in the filter.
87a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Cataniaconst int kMaxFilterSize = 64;  // I pulled that out of thin air.
88a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania
89a64c8c79af1a15911c55306d83a797fa50969f77niko// FIXME: Move all the metadata related function in the Metadata.cpp
90d608a813a9d2cbc6e2a5ea81d78d4a9044090c4cniko
91a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania
92a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania// Unmarshall a filter from a Parcel.
93a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania// Filter format in a parcel:
94a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania//
95a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania//  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
96a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania// |                       number of entries (n)                   |
98a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania// |                       metadata type 1                         |
100a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania// |                       metadata type 2                         |
102a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania//  ....
104a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania// |                       metadata type n                         |
106a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania//
108a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania// @param p Parcel that should start with a filter.
109a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania// @param[out] filter On exit contains the list of metadata type to be
110a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania//                    filtered.
111a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania// @param[out] status On exit contains the status code to be returned.
112a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania// @return true if the parcel starts with a valid filter.
113a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Cataniabool unmarshallFilter(const Parcel& p,
114a64c8c79af1a15911c55306d83a797fa50969f77niko                      Metadata::Filter *filter,
115a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania                      status_t *status)
116a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania{
1174829038419910aa6e75ce8992d45a223452d5c67Nicolas Catania    int32_t val;
1184829038419910aa6e75ce8992d45a223452d5c67Nicolas Catania    if (p.readInt32(&val) != OK)
119a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania    {
12029357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("Failed to read filter's length");
121a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania        *status = NOT_ENOUGH_DATA;
122a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania        return false;
123a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania    }
124a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania
1254829038419910aa6e75ce8992d45a223452d5c67Nicolas Catania    if( val > kMaxFilterSize || val < 0)
126a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania    {
12729357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("Invalid filter len %d", val);
128a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania        *status = BAD_VALUE;
129a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania        return false;
130a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania    }
131a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania
1324829038419910aa6e75ce8992d45a223452d5c67Nicolas Catania    const size_t num = val;
133a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania
134a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania    filter->clear();
1354829038419910aa6e75ce8992d45a223452d5c67Nicolas Catania    filter->setCapacity(num);
136a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania
137a64c8c79af1a15911c55306d83a797fa50969f77niko    size_t size = num * sizeof(Metadata::Type);
138a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania
1394829038419910aa6e75ce8992d45a223452d5c67Nicolas Catania
1404829038419910aa6e75ce8992d45a223452d5c67Nicolas Catania    if (p.dataAvail() < size)
141a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania    {
14229357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
143a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania        *status = NOT_ENOUGH_DATA;
144a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania        return false;
145a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania    }
146a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania
147a64c8c79af1a15911c55306d83a797fa50969f77niko    const Metadata::Type *data =
148a64c8c79af1a15911c55306d83a797fa50969f77niko            static_cast<const Metadata::Type*>(p.readInplace(size));
149a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania
1504829038419910aa6e75ce8992d45a223452d5c67Nicolas Catania    if (NULL == data)
151a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania    {
15229357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("Filter had no data");
153a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania        *status = BAD_VALUE;
154a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania        return false;
155a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania    }
156a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania
157a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania    // TODO: The stl impl of vector would be more efficient here
158a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania    // because it degenerates into a memcpy on pod types. Try to
159a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania    // replace later or use stl::set.
1604829038419910aa6e75ce8992d45a223452d5c67Nicolas Catania    for (size_t i = 0; i < num; ++i)
161a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania    {
1624829038419910aa6e75ce8992d45a223452d5c67Nicolas Catania        filter->add(*data);
163a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania        ++data;
164a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania    }
165a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania    *status = OK;
166a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania    return true;
167a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania}
168a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania
1694829038419910aa6e75ce8992d45a223452d5c67Nicolas Catania// @param filter Of metadata type.
170a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania// @param val To be searched.
171a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania// @return true if a match was found.
172a64c8c79af1a15911c55306d83a797fa50969f77nikobool findMetadata(const Metadata::Filter& filter, const int32_t val)
173a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania{
174a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania    // Deal with empty and ANY right away
175a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania    if (filter.isEmpty()) return false;
176a64c8c79af1a15911c55306d83a797fa50969f77niko    if (filter[0] == Metadata::kAny) return true;
177a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania
1784829038419910aa6e75ce8992d45a223452d5c67Nicolas Catania    return filter.indexOf(val) >= 0;
179a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania}
180a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania
181a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania}  // anonymous namespace
182a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania
183a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania
18489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectnamespace android {
18589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
186d681bbb1767bed09415e050ba78975df214bcd68Dave Burkestatic bool checkPermission(const char* permissionString) {
187d681bbb1767bed09415e050ba78975df214bcd68Dave Burke#ifndef HAVE_ANDROID_OS
188d681bbb1767bed09415e050ba78975df214bcd68Dave Burke    return true;
189d681bbb1767bed09415e050ba78975df214bcd68Dave Burke#endif
190d681bbb1767bed09415e050ba78975df214bcd68Dave Burke    if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
191d681bbb1767bed09415e050ba78975df214bcd68Dave Burke    bool ok = checkCallingPermission(String16(permissionString));
19229357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block    if (!ok) ALOGE("Request requires %s", permissionString);
193d681bbb1767bed09415e050ba78975df214bcd68Dave Burke    return ok;
194d681bbb1767bed09415e050ba78975df214bcd68Dave Burke}
195d681bbb1767bed09415e050ba78975df214bcd68Dave Burke
19689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// TODO: Find real cause of Audio/Video delay in PV framework and remove this workaround
19789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
19889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
19989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
20089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectvoid MediaPlayerService::instantiate() {
20189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    defaultServiceManager()->addService(
20289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            String16("media.player"), new MediaPlayerService());
20389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
20489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
20589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source ProjectMediaPlayerService::MediaPlayerService()
20689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
2073856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("MediaPlayerService created");
20889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mNextConnId = 1;
2099ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang
2109ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang    mBatteryAudio.refCount = 0;
2119ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang    for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2129ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang        mBatteryAudio.deviceOn[i] = 0;
2139ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang        mBatteryAudio.lastTime[i] = 0;
2149ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang        mBatteryAudio.totalTime[i] = 0;
2159ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang    }
2169ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang    // speaker is on by default
2179ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang    mBatteryAudio.deviceOn[SPEAKER] = 1;
21844a7e42f0310831e6a846d1b6bb40bf3a399bf6dJohn Grossman
21944a7e42f0310831e6a846d1b6bb40bf3a399bf6dJohn Grossman    MediaPlayerFactory::registerBuiltinFactories();
22089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
22189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
22289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source ProjectMediaPlayerService::~MediaPlayerService()
22389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
2243856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("MediaPlayerService destroyed");
22589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
22689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
22789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectsp<IMediaRecorder> MediaPlayerService::createMediaRecorder(pid_t pid)
22889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
229dac6a31a33ba53fb93850670cdddd1e6515dadceGloria Wang    sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
230dac6a31a33ba53fb93850670cdddd1e6515dadceGloria Wang    wp<MediaRecorderClient> w = recorder;
231dac6a31a33ba53fb93850670cdddd1e6515dadceGloria Wang    Mutex::Autolock lock(mLock);
232dac6a31a33ba53fb93850670cdddd1e6515dadceGloria Wang    mMediaRecorderClients.add(w);
2333856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Create new media recorder client from pid %d", pid);
23489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return recorder;
23589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
23689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
237dac6a31a33ba53fb93850670cdddd1e6515dadceGloria Wangvoid MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
238dac6a31a33ba53fb93850670cdddd1e6515dadceGloria Wang{
239dac6a31a33ba53fb93850670cdddd1e6515dadceGloria Wang    Mutex::Autolock lock(mLock);
240dac6a31a33ba53fb93850670cdddd1e6515dadceGloria Wang    mMediaRecorderClients.remove(client);
2413856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Delete media recorder client");
242dac6a31a33ba53fb93850670cdddd1e6515dadceGloria Wang}
243dac6a31a33ba53fb93850670cdddd1e6515dadceGloria Wang
24489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectsp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever(pid_t pid)
24589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
24689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
2473856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Create new media retriever from pid %d", pid);
24889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return retriever;
24989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
25089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
25189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectsp<IMediaPlayer> MediaPlayerService::create(pid_t pid, const sp<IMediaPlayerClient>& client,
252d681bbb1767bed09415e050ba78975df214bcd68Dave Burke        int audioSessionId)
25389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
25489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    int32_t connId = android_atomic_inc(&mNextConnId);
2559b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber
2569b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber    sp<Client> c = new Client(
2579b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber            this, pid, connId, client, audioSessionId,
2589b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber            IPCThreadState::self()->getCallingUid());
2599b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber
2603856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
261d681bbb1767bed09415e050ba78975df214bcd68Dave Burke         IPCThreadState::self()->getCallingUid());
262e2b1028852120bcfded33b8f06f66b780437fe92Andreas Huber
263d681bbb1767bed09415e050ba78975df214bcd68Dave Burke    wp<Client> w = c;
264d681bbb1767bed09415e050ba78975df214bcd68Dave Burke    {
265e2b1028852120bcfded33b8f06f66b780437fe92Andreas Huber        Mutex::Autolock lock(mLock);
266e2b1028852120bcfded33b8f06f66b780437fe92Andreas Huber        mClients.add(w);
267e2b1028852120bcfded33b8f06f66b780437fe92Andreas Huber    }
268e2b1028852120bcfded33b8f06f66b780437fe92Andreas Huber    return c;
269e2b1028852120bcfded33b8f06f66b780437fe92Andreas Huber}
270e2b1028852120bcfded33b8f06f66b780437fe92Andreas Huber
271318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Hubersp<IOMX> MediaPlayerService::getOMX() {
272318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    Mutex::Autolock autoLock(mLock);
273318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
274318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    if (mOMX.get() == NULL) {
275318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber        mOMX = new OMX;
276318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    }
277318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
278318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    return mOMX;
27920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
28020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
281ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Hubersp<ICrypto> MediaPlayerService::makeCrypto() {
2821bd139a2a68690e80398b70b27ca59550fea0e65Andreas Huber    return new Crypto;
283ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber}
284ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber
285efbb781c15abf8e085f5ab8d39710bd58ea0b6e4Andreas Hubersp<IHDCP> MediaPlayerService::makeHDCP() {
286efbb781c15abf8e085f5ab8d39710bd58ea0b6e4Andreas Huber    return new HDCP;
287efbb781c15abf8e085f5ab8d39710bd58ea0b6e4Andreas Huber}
288efbb781c15abf8e085f5ab8d39710bd58ea0b6e4Andreas Huber
289e104596061b219e9bce6d4db49a9d15242f8d2e5Jeff Brownsp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
290e104596061b219e9bce6d4db49a9d15242f8d2e5Jeff Brown        const sp<IRemoteDisplayClient>& client, const String8& iface) {
291b019250df516e094b0cb648851f9a132b045378aJeff Brown    if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
292b019250df516e094b0cb648851f9a132b045378aJeff Brown        return NULL;
293b019250df516e094b0cb648851f9a132b045378aJeff Brown    }
294b019250df516e094b0cb648851f9a132b045378aJeff Brown
295455d02eca342d8159637af317a5d83219d267812Jeff Brown    return new RemoteDisplay(client, iface.string());
296e104596061b219e9bce6d4db49a9d15242f8d2e5Jeff Brown}
297e104596061b219e9bce6d4db49a9d15242f8d2e5Jeff Brown
29889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
29989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
30089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    const size_t SIZE = 256;
30189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    char buffer[SIZE];
30289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    String8 result;
30389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
30489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    result.append(" AudioCache\n");
30589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (mHeap != 0) {
30689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        snprintf(buffer, 255, "  heap base(%p), size(%d), flags(%d), device(%s)\n",
30789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice());
30889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        result.append(buffer);
30989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
31089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    snprintf(buffer, 255, "  msec per frame(%f), channel count(%d), format(%d), frame count(%ld)\n",
31189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
31289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    result.append(buffer);
31389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    snprintf(buffer, 255, "  sample rate(%d), size(%d), error(%d), command complete(%s)\n",
31489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            mSampleRate, mSize, mError, mCommandComplete?"true":"false");
31589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    result.append(buffer);
31689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    ::write(fd, result.string(), result.size());
31789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
31889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
31989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
32089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
32189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
32289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    const size_t SIZE = 256;
32389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    char buffer[SIZE];
32489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    String8 result;
32589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
32689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    result.append(" AudioOutput\n");
32789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    snprintf(buffer, 255, "  stream type(%d), left - right volume(%f, %f)\n",
32889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            mStreamType, mLeftVolume, mRightVolume);
32989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    result.append(buffer);
33089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    snprintf(buffer, 255, "  msec per frame(%f), latency (%d)\n",
331db354e58e65592777aa17caa47933e14838b8b35Eric Laurent            mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
33289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    result.append(buffer);
3332beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent    snprintf(buffer, 255, "  aux effect id(%d), send level (%f)\n",
3342beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent            mAuxEffectId, mSendLevel);
3352beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent    result.append(buffer);
3362beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent
33789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    ::write(fd, result.string(), result.size());
33889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (mTrack != 0) {
33989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        mTrack->dump(fd, args);
34089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
34189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
34289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
34389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
34489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
34589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
34689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    const size_t SIZE = 256;
34789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    char buffer[SIZE];
34889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    String8 result;
34989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    result.append(" Client\n");
35089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    snprintf(buffer, 255, "  pid(%d), connId(%d), status(%d), looping(%s)\n",
35189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            mPid, mConnId, mStatus, mLoop?"true": "false");
35289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    result.append(buffer);
35389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    write(fd, result.string(), result.size());
354a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber    if (mPlayer != NULL) {
355a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber        mPlayer->dump(fd, args);
356a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber    }
35789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (mAudioOutput != 0) {
35889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        mAudioOutput->dump(fd, args);
35989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
36089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    write(fd, "\n", 1);
36189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
36289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
36389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
36489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
36589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
36689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    const size_t SIZE = 256;
36789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    char buffer[SIZE];
36889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    String8 result;
36989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
37089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        snprintf(buffer, SIZE, "Permission Denial: "
37189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                "can't dump MediaPlayerService from pid=%d, uid=%d\n",
37289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                IPCThreadState::self()->getCallingPid(),
37389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                IPCThreadState::self()->getCallingUid());
37489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        result.append(buffer);
37589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    } else {
37689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        Mutex::Autolock lock(mLock);
37789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        for (int i = 0, n = mClients.size(); i < n; ++i) {
37889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            sp<Client> c = mClients[i].promote();
37989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            if (c != 0) c->dump(fd, args);
38089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
381b914122eb9cb54bbeae4ec03bfebb194aecdccbdJames Dong        if (mMediaRecorderClients.size() == 0) {
382b914122eb9cb54bbeae4ec03bfebb194aecdccbdJames Dong                result.append(" No media recorder client\n\n");
383b914122eb9cb54bbeae4ec03bfebb194aecdccbdJames Dong        } else {
384b914122eb9cb54bbeae4ec03bfebb194aecdccbdJames Dong            for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
385b914122eb9cb54bbeae4ec03bfebb194aecdccbdJames Dong                sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
386e579e28089c6b86f56d257030b778c5e22b1a5a5James Dong                if (c != 0) {
387e579e28089c6b86f56d257030b778c5e22b1a5a5James Dong                    snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
388e579e28089c6b86f56d257030b778c5e22b1a5a5James Dong                    result.append(buffer);
389e579e28089c6b86f56d257030b778c5e22b1a5a5James Dong                    write(fd, result.string(), result.size());
390e579e28089c6b86f56d257030b778c5e22b1a5a5James Dong                    result = "\n";
391e579e28089c6b86f56d257030b778c5e22b1a5a5James Dong                    c->dump(fd, args);
392e579e28089c6b86f56d257030b778c5e22b1a5a5James Dong                }
393b914122eb9cb54bbeae4ec03bfebb194aecdccbdJames Dong            }
394dac6a31a33ba53fb93850670cdddd1e6515dadceGloria Wang        }
395dac6a31a33ba53fb93850670cdddd1e6515dadceGloria Wang
39689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        result.append(" Files opened and/or mapped:\n");
3970512ab559d4670c2204078470d7ef5d376811c57Glenn Kasten        snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
39889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        FILE *f = fopen(buffer, "r");
39989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        if (f) {
40089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            while (!feof(f)) {
40189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                fgets(buffer, SIZE, f);
40273ac1eebff620714c6977f7342ff8dcc4858b3cfMarco Nelissen                if (strstr(buffer, " /storage/") ||
40389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                    strstr(buffer, " /system/sounds/") ||
40402fa834249a01144d6f23137951538600aa5d611Dave Sparks                    strstr(buffer, " /data/") ||
40589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                    strstr(buffer, " /system/media/")) {
40689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                    result.append("  ");
40789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                    result.append(buffer);
40889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                }
40989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            }
41089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            fclose(f);
41189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        } else {
41289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            result.append("couldn't open ");
41389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            result.append(buffer);
41489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            result.append("\n");
41589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
41689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
4170512ab559d4670c2204078470d7ef5d376811c57Glenn Kasten        snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
41889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        DIR *d = opendir(buffer);
41989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        if (d) {
42089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            struct dirent *ent;
42189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            while((ent = readdir(d)) != NULL) {
42289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
4230512ab559d4670c2204078470d7ef5d376811c57Glenn Kasten                    snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
42489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                    struct stat s;
42589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                    if (lstat(buffer, &s) == 0) {
42689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                        if ((s.st_mode & S_IFMT) == S_IFLNK) {
42789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                            char linkto[256];
42889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                            int len = readlink(buffer, linkto, sizeof(linkto));
42989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                            if(len > 0) {
43089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                                if(len > 255) {
43189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                                    linkto[252] = '.';
43289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                                    linkto[253] = '.';
43389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                                    linkto[254] = '.';
43489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                                    linkto[255] = 0;
43589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                                } else {
43689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                                    linkto[len] = 0;
43789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                                }
43873ac1eebff620714c6977f7342ff8dcc4858b3cfMarco Nelissen                                if (strstr(linkto, "/storage/") == linkto ||
43989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                                    strstr(linkto, "/system/sounds/") == linkto ||
44002fa834249a01144d6f23137951538600aa5d611Dave Sparks                                    strstr(linkto, "/data/") == linkto ||
44189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                                    strstr(linkto, "/system/media/") == linkto) {
44289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                                    result.append("  ");
44389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                                    result.append(buffer);
44489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                                    result.append(" -> ");
44589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                                    result.append(linkto);
44689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                                    result.append("\n");
44789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                                }
44889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                            }
44989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                        } else {
45089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                            result.append("  unexpected type for ");
45189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                            result.append(buffer);
45289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                            result.append("\n");
45389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                        }
45489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                    }
45589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                }
45689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            }
45789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            closedir(d);
45889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        } else {
45989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            result.append("couldn't open ");
46089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            result.append(buffer);
46189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            result.append("\n");
46289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
46389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
46489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        bool dumpMem = false;
46589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        for (size_t i = 0; i < args.size(); i++) {
46689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            if (args[i] == String16("-m")) {
46789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                dumpMem = true;
46889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            }
46989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
47089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        if (dumpMem) {
4718635b7b095fbf7ffc63d3ce791891a9116ace1f6James Dong            dumpMemoryAddresses(fd);
47289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
47389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
47489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    write(fd, result.string(), result.size());
47589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
47689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
47789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
47889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectvoid MediaPlayerService::removeClient(wp<Client> client)
47989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
48089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    Mutex::Autolock lock(mLock);
48189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mClients.remove(client);
48289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
48389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
4849b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas HuberMediaPlayerService::Client::Client(
4859b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber        const sp<MediaPlayerService>& service, pid_t pid,
4869b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber        int32_t connId, const sp<IMediaPlayerClient>& client,
4879b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber        int audioSessionId, uid_t uid)
48889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
4893856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Client(%d) constructor", connId);
49089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mPid = pid;
49189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mConnId = connId;
49289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mService = service;
49389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mClient = client;
49489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mLoop = false;
49589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mStatus = NO_INIT;
496a514bdb58b5de4986679f72b7204b4764f7a2778Eric Laurent    mAudioSessionId = audioSessionId;
4979b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber    mUID = uid;
498c795b64060c3af9d7961fc1371e4ccfa8ee3e450John Grossman    mRetransmitEndpointValid = false;
499a514bdb58b5de4986679f72b7204b4764f7a2778Eric Laurent
50089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#if CALLBACK_ANTAGONIZER
501b8a805261bf0282e992d3608035e47d05a898710Steve Block    ALOGD("create Antagonizer");
50289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mAntagonizer = new Antagonizer(notify, this);
50389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#endif
50489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
50589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
50689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source ProjectMediaPlayerService::Client::~Client()
50789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
5083856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
50989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mAudioOutput.clear();
51089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    wp<Client> client(this);
51189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    disconnect();
51289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mService->removeClient(client);
51389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
51489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
51589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectvoid MediaPlayerService::Client::disconnect()
51689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
5173856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
51889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // grab local reference and clear main reference to prevent future
51989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // access to object
52089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    sp<MediaPlayerBase> p;
52189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    {
52289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        Mutex::Autolock l(mLock);
52389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        p = mPlayer;
52489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
525795fa5848276bab87445fa64fd0941c13ef62398Dave Sparks    mClient.clear();
52620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
52789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mPlayer.clear();
52889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
52989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // clear the notification to prevent callbacks to dead client
53089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // and reset the player. We assume the player will serialize
53189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // access to itself if necessary.
53289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (p != 0) {
53389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        p->setNotifyCallback(0, 0);
53489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#if CALLBACK_ANTAGONIZER
535b8a805261bf0282e992d3608035e47d05a898710Steve Block        ALOGD("kill Antagonizer");
53689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        mAntagonizer->kill();
53789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#endif
53889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        p->reset();
53989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
54089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
5417dae00baa6e8957be15523c46bb948bd1dde64c3Jamie Gennis    disconnectNativeWindow();
5427dae00baa6e8957be15523c46bb948bd1dde64c3Jamie Gennis
54389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    IPCThreadState::self()->flushCommands();
54489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
54589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
54689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectsp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
54789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
54889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // determine if we have the right player type
54989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    sp<MediaPlayerBase> p = mPlayer;
55089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if ((p != NULL) && (p->playerType() != playerType)) {
5513856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("delete player");
55289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        p.clear();
55389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
55489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (p == NULL) {
55544a7e42f0310831e6a846d1b6bb40bf3a399bf6dJohn Grossman        p = MediaPlayerFactory::createPlayer(playerType, this, notify);
55689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
5579b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber
558db29e5238e28d59978755a2ff2e7e0f05393abdfJason Simmons    if (p != NULL) {
559db29e5238e28d59978755a2ff2e7e0f05393abdfJason Simmons        p->setUID(mUID);
560db29e5238e28d59978755a2ff2e7e0f05393abdfJason Simmons    }
5619b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber
56289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return p;
56389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
56489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
565c795b64060c3af9d7961fc1371e4ccfa8ee3e450John Grossmansp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
566c795b64060c3af9d7961fc1371e4ccfa8ee3e450John Grossman        player_type playerType)
567c795b64060c3af9d7961fc1371e4ccfa8ee3e450John Grossman{
568c795b64060c3af9d7961fc1371e4ccfa8ee3e450John Grossman    ALOGV("player type = %d", playerType);
569c795b64060c3af9d7961fc1371e4ccfa8ee3e450John Grossman
570c795b64060c3af9d7961fc1371e4ccfa8ee3e450John Grossman    // create the right type of player
571c795b64060c3af9d7961fc1371e4ccfa8ee3e450John Grossman    sp<MediaPlayerBase> p = createPlayer(playerType);
572c795b64060c3af9d7961fc1371e4ccfa8ee3e450John Grossman    if (p == NULL) {
573c795b64060c3af9d7961fc1371e4ccfa8ee3e450John Grossman        return p;
574