127c174483a8ae9688d5d4897c19074f62c7f1701James Dong/*
227c174483a8ae9688d5d4897c19074f62c7f1701James Dong * Copyright (C) 2009 The Android Open Source Project
327c174483a8ae9688d5d4897c19074f62c7f1701James Dong *
427c174483a8ae9688d5d4897c19074f62c7f1701James Dong * Licensed under the Apache License, Version 2.0 (the "License");
527c174483a8ae9688d5d4897c19074f62c7f1701James Dong * you may not use this file except in compliance with the License.
627c174483a8ae9688d5d4897c19074f62c7f1701James Dong * You may obtain a copy of the License at
727c174483a8ae9688d5d4897c19074f62c7f1701James Dong *
827c174483a8ae9688d5d4897c19074f62c7f1701James Dong *      http://www.apache.org/licenses/LICENSE-2.0
927c174483a8ae9688d5d4897c19074f62c7f1701James Dong *
1027c174483a8ae9688d5d4897c19074f62c7f1701James Dong * Unless required by applicable law or agreed to in writing, software
1127c174483a8ae9688d5d4897c19074f62c7f1701James Dong * distributed under the License is distributed on an "AS IS" BASIS,
1227c174483a8ae9688d5d4897c19074f62c7f1701James Dong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1327c174483a8ae9688d5d4897c19074f62c7f1701James Dong * See the License for the specific language governing permissions and
1427c174483a8ae9688d5d4897c19074f62c7f1701James Dong * limitations under the License.
1527c174483a8ae9688d5d4897c19074f62c7f1701James Dong */
1627c174483a8ae9688d5d4897c19074f62c7f1701James Dong
1720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber//#define LOG_NDEBUG 0
1820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#define LOG_TAG "StagefrightPlayer"
1920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <utils/Log.h>
2020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
2120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include "StagefrightPlayer.h"
22bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
23bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber#include "AwesomePlayer.h"
2420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
25acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber#include <media/Metadata.h>
26acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber#include <media/stagefright/MediaExtractor.h>
27acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber
2820111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubernamespace android {
2920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
3020111aa043c5f404472bc63b90bc5aad906b1101Andreas HuberStagefrightPlayer::StagefrightPlayer()
31bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    : mPlayer(new AwesomePlayer) {
323856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("StagefrightPlayer");
33bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
34bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    mPlayer->setListener(this);
3520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
3620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
3720111aa043c5f404472bc63b90bc5aad906b1101Andreas HuberStagefrightPlayer::~StagefrightPlayer() {
383856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("~StagefrightPlayer");
3920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    reset();
40bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
41bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    delete mPlayer;
42bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    mPlayer = NULL;
4320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
4420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
4520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberstatus_t StagefrightPlayer::initCheck() {
463856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("initCheck");
4720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    return OK;
4820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
4920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
509b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huberstatus_t StagefrightPlayer::setUID(uid_t uid) {
519b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber    mPlayer->setUID(uid);
529b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber
539b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber    return OK;
549b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber}
559b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber
562db8455d8f4468a637109d31f319ce02d9d743ecAndreas Huberstatus_t StagefrightPlayer::setDataSource(
575561ccf4a8db88a2e44eac1b3ed13b4ff53a7f20Andreas Huber        const char *url, const KeyedVector<String8, String8> *headers) {
585561ccf4a8db88a2e44eac1b3ed13b4ff53a7f20Andreas Huber    return mPlayer->setDataSource(url, headers);
5920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
6020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
61a75e9fb4876139fff4bb836fb0ebd5f80c8b20b3Andreas Huber// Warning: The filedescriptor passed into this method will only be valid until
62a75e9fb4876139fff4bb836fb0ebd5f80c8b20b3Andreas Huber// the method returns, if you want to keep it, dup it!
6320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberstatus_t StagefrightPlayer::setDataSource(int fd, int64_t offset, int64_t length) {
643856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("setDataSource(%d, %lld, %lld)", fd, offset, length);
65bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    return mPlayer->setDataSource(dup(fd), offset, length);
6620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
6720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
68e2b1028852120bcfded33b8f06f66b780437fe92Andreas Huberstatus_t StagefrightPlayer::setDataSource(const sp<IStreamSource> &source) {
69e2b1028852120bcfded33b8f06f66b780437fe92Andreas Huber    return mPlayer->setDataSource(source);
70e2b1028852120bcfded33b8f06f66b780437fe92Andreas Huber}
71e2b1028852120bcfded33b8f06f66b780437fe92Andreas Huber
721173118eace0e9e347cb007f0da817cee87579edGlenn Kastenstatus_t StagefrightPlayer::setVideoSurfaceTexture(
731173118eace0e9e347cb007f0da817cee87579edGlenn Kasten        const sp<ISurfaceTexture> &surfaceTexture) {
743856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("setVideoSurfaceTexture");
751173118eace0e9e347cb007f0da817cee87579edGlenn Kasten
7663970b42f101c87db7cfd26d43b0d300260b1582Andreas Huber    return mPlayer->setSurfaceTexture(surfaceTexture);
771173118eace0e9e347cb007f0da817cee87579edGlenn Kasten}
781173118eace0e9e347cb007f0da817cee87579edGlenn Kasten
7920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberstatus_t StagefrightPlayer::prepare() {
80c23f12af0394aa2f6651968a3c8840f1af317aa1Andreas Huber    return mPlayer->prepare();
8120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
8220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
8320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberstatus_t StagefrightPlayer::prepareAsync() {
84c23f12af0394aa2f6651968a3c8840f1af317aa1Andreas Huber    return mPlayer->prepareAsync();
8520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
8620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
8720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberstatus_t StagefrightPlayer::start() {
883856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("start");
8920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
90bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    return mPlayer->play();
9120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
9220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
9320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberstatus_t StagefrightPlayer::stop() {
943856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("stop");
9520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
96bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    return pause();  // what's the difference?
9720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
9820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
9920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberstatus_t StagefrightPlayer::pause() {
1003856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("pause");
10120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
102bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    return mPlayer->pause();
10320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
10420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
10520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberbool StagefrightPlayer::isPlaying() {
1063856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("isPlaying");
107bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    return mPlayer->isPlaying();
10820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
10920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
11020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberstatus_t StagefrightPlayer::seekTo(int msec) {
1113856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("seekTo %.2f secs", msec / 1E3);
11220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
11320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    status_t err = mPlayer->seekTo((int64_t)msec * 1000);
11420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
11520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    return err;
11620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
11720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
11820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberstatus_t StagefrightPlayer::getCurrentPosition(int *msec) {
1193856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("getCurrentPosition");
12020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
121bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    int64_t positionUs;
122bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    status_t err = mPlayer->getPosition(&positionUs);
123bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
124bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    if (err != OK) {
125bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber        return err;
12620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    }
12720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
128bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    *msec = (positionUs + 500) / 1000;
129bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
13020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    return OK;
13120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
13220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
13320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberstatus_t StagefrightPlayer::getDuration(int *msec) {
1343856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("getDuration");
13520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
136bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    int64_t durationUs;
137bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    status_t err = mPlayer->getDuration(&durationUs);
138bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
139bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    if (err != OK) {
140acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber        *msec = 0;
141acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber        return OK;
14220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    }
14320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
144bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    *msec = (durationUs + 500) / 1000;
145bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
14620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    return OK;
14720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
14820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
14920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberstatus_t StagefrightPlayer::reset() {
1503856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("reset");
15120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
152bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    mPlayer->reset();
15320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
15420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    return OK;
15520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
15620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
15720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberstatus_t StagefrightPlayer::setLooping(int loop) {
1583856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("setLooping");
159bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
160bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    return mPlayer->setLooping(loop);
16120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
16220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
16320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberplayer_type StagefrightPlayer::playerType() {
1643856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("playerType");
16520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    return STAGEFRIGHT_PLAYER;
16620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
16720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
16820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberstatus_t StagefrightPlayer::invoke(const Parcel &request, Parcel *reply) {
169f9d660a5e0196240add5daf0199f128d471e592cInsun Kang    ALOGV("invoke()");
170f9d660a5e0196240add5daf0199f128d471e592cInsun Kang    return mPlayer->invoke(request, reply);
17120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
17220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
17320111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubervoid StagefrightPlayer::setAudioSink(const sp<AudioSink> &audioSink) {
17420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    MediaPlayerInterface::setAudioSink(audioSink);
17520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
176bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    mPlayer->setAudioSink(audioSink);
17720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
17820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
1794f9e47f2c03ce36261c4717cd7e131d7940bb068Gloria Wangstatus_t StagefrightPlayer::setParameter(int key, const Parcel &request) {
1807a8b0ed6419e57dd8b41d3806893d63d3df91aabJean-Michel Trivi    ALOGV("setParameter(key=%d)", key);
1814f9e47f2c03ce36261c4717cd7e131d7940bb068Gloria Wang    return mPlayer->setParameter(key, request);
1824f9e47f2c03ce36261c4717cd7e131d7940bb068Gloria Wang}
1834f9e47f2c03ce36261c4717cd7e131d7940bb068Gloria Wang
1844f9e47f2c03ce36261c4717cd7e131d7940bb068Gloria Wangstatus_t StagefrightPlayer::getParameter(int key, Parcel *reply) {
1853856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("getParameter");
1864f9e47f2c03ce36261c4717cd7e131d7940bb068Gloria Wang    return mPlayer->getParameter(key, reply);
1874f9e47f2c03ce36261c4717cd7e131d7940bb068Gloria Wang}
1884f9e47f2c03ce36261c4717cd7e131d7940bb068Gloria Wang
189acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huberstatus_t StagefrightPlayer::getMetadata(
190acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber        const media::Metadata::Filter& ids, Parcel *records) {
191acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber    using media::Metadata;
192acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber
193acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber    uint32_t flags = mPlayer->flags();
194acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber
195acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber    Metadata metadata(records);
196acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber
197acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber    metadata.appendBool(
198acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber            Metadata::kPauseAvailable,
199acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber            flags & MediaExtractor::CAN_PAUSE);
200acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber
201acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber    metadata.appendBool(
202acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber            Metadata::kSeekBackwardAvailable,
203acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber            flags & MediaExtractor::CAN_SEEK_BACKWARD);
204acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber
205acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber    metadata.appendBool(
206acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber            Metadata::kSeekForwardAvailable,
207acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber            flags & MediaExtractor::CAN_SEEK_FORWARD);
208acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber
20970f521de2675ce6eedf4b22beed94ea1289b0f38Andreas Huber    metadata.appendBool(
21070f521de2675ce6eedf4b22beed94ea1289b0f38Andreas Huber            Metadata::kSeekAvailable,
21170f521de2675ce6eedf4b22beed94ea1289b0f38Andreas Huber            flags & MediaExtractor::CAN_SEEK);
21270f521de2675ce6eedf4b22beed94ea1289b0f38Andreas Huber
213acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber    return OK;
214acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber}
215acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber
216a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huberstatus_t StagefrightPlayer::dump(int fd, const Vector<String16> &args) const {
217a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber    return mPlayer->dump(fd, args);
218a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber}
219a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber
22020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}  // namespace android
221