1//#define LOG_NDEBUG 0
2#define LOG_TAG "StagefrightPlayer"
3#include <utils/Log.h>
4
5#include "StagefrightPlayer.h"
6
7#include "AwesomePlayer.h"
8
9#include <media/Metadata.h>
10#include <media/stagefright/MediaExtractor.h>
11
12namespace android {
13
14StagefrightPlayer::StagefrightPlayer()
15    : mPlayer(new AwesomePlayer) {
16    LOGV("StagefrightPlayer");
17
18    mPlayer->setListener(this);
19}
20
21StagefrightPlayer::~StagefrightPlayer() {
22    LOGV("~StagefrightPlayer");
23    reset();
24
25    delete mPlayer;
26    mPlayer = NULL;
27}
28
29status_t StagefrightPlayer::initCheck() {
30    LOGV("initCheck");
31    return OK;
32}
33
34status_t StagefrightPlayer::setDataSource(
35        const char *url, const KeyedVector<String8, String8> *headers) {
36    LOGI("setDataSource('%s')", url);
37    return mPlayer->setDataSource(url, headers);
38}
39
40// Warning: The filedescriptor passed into this method will only be valid until
41// the method returns, if you want to keep it, dup it!
42status_t StagefrightPlayer::setDataSource(int fd, int64_t offset, int64_t length) {
43    LOGV("setDataSource(%d, %lld, %lld)", fd, offset, length);
44    return mPlayer->setDataSource(dup(fd), offset, length);
45}
46
47status_t StagefrightPlayer::setVideoSurface(const sp<ISurface> &surface) {
48    LOGV("setVideoSurface");
49
50    mPlayer->setISurface(surface);
51    return OK;
52}
53
54status_t StagefrightPlayer::prepare() {
55    return mPlayer->prepare();
56}
57
58status_t StagefrightPlayer::prepareAsync() {
59    return mPlayer->prepareAsync();
60}
61
62status_t StagefrightPlayer::start() {
63    LOGV("start");
64
65    return mPlayer->play();
66}
67
68status_t StagefrightPlayer::stop() {
69    LOGV("stop");
70
71    return pause();  // what's the difference?
72}
73
74status_t StagefrightPlayer::pause() {
75    LOGV("pause");
76
77    return mPlayer->pause();
78}
79
80bool StagefrightPlayer::isPlaying() {
81    LOGV("isPlaying");
82    return mPlayer->isPlaying();
83}
84
85status_t StagefrightPlayer::seekTo(int msec) {
86    LOGV("seekTo");
87
88    status_t err = mPlayer->seekTo((int64_t)msec * 1000);
89
90    return err;
91}
92
93status_t StagefrightPlayer::getCurrentPosition(int *msec) {
94    LOGV("getCurrentPosition");
95
96    int64_t positionUs;
97    status_t err = mPlayer->getPosition(&positionUs);
98
99    if (err != OK) {
100        return err;
101    }
102
103    *msec = (positionUs + 500) / 1000;
104
105    return OK;
106}
107
108status_t StagefrightPlayer::getDuration(int *msec) {
109    LOGV("getDuration");
110
111    int64_t durationUs;
112    status_t err = mPlayer->getDuration(&durationUs);
113
114    if (err != OK) {
115        *msec = 0;
116        return OK;
117    }
118
119    *msec = (durationUs + 500) / 1000;
120
121    return OK;
122}
123
124status_t StagefrightPlayer::reset() {
125    LOGV("reset");
126
127    mPlayer->reset();
128
129    return OK;
130}
131
132status_t StagefrightPlayer::setLooping(int loop) {
133    LOGV("setLooping");
134
135    return mPlayer->setLooping(loop);
136}
137
138player_type StagefrightPlayer::playerType() {
139    LOGV("playerType");
140    return STAGEFRIGHT_PLAYER;
141}
142
143status_t StagefrightPlayer::suspend() {
144    LOGV("suspend");
145    return mPlayer->suspend();
146}
147
148status_t StagefrightPlayer::resume() {
149    LOGV("resume");
150    return mPlayer->resume();
151}
152
153status_t StagefrightPlayer::invoke(const Parcel &request, Parcel *reply) {
154    return INVALID_OPERATION;
155}
156
157void StagefrightPlayer::setAudioSink(const sp<AudioSink> &audioSink) {
158    MediaPlayerInterface::setAudioSink(audioSink);
159
160    mPlayer->setAudioSink(audioSink);
161}
162
163status_t StagefrightPlayer::getMetadata(
164        const media::Metadata::Filter& ids, Parcel *records) {
165    using media::Metadata;
166
167    uint32_t flags = mPlayer->flags();
168
169    Metadata metadata(records);
170
171    metadata.appendBool(
172            Metadata::kPauseAvailable,
173            flags & MediaExtractor::CAN_PAUSE);
174
175    metadata.appendBool(
176            Metadata::kSeekBackwardAvailable,
177            flags & MediaExtractor::CAN_SEEK_BACKWARD);
178
179    metadata.appendBool(
180            Metadata::kSeekForwardAvailable,
181            flags & MediaExtractor::CAN_SEEK_FORWARD);
182
183    metadata.appendBool(
184            Metadata::kSeekAvailable,
185            flags & MediaExtractor::CAN_SEEK);
186
187    return OK;
188}
189
190}  // namespace android
191