AwesomePlayer.h revision 5561ccf4a8db88a2e44eac1b3ed13b4ff53a7f20
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef AWESOME_PLAYER_H_
18
19#define AWESOME_PLAYER_H_
20
21#include "TimedEventQueue.h"
22
23#include <media/MediaPlayerInterface.h>
24#include <media/stagefright/OMXClient.h>
25#include <utils/threads.h>
26
27namespace android {
28
29struct AudioPlayer;
30struct MediaBuffer;
31struct MediaExtractor;
32struct MediaSource;
33struct Prefetcher;
34struct TimeSource;
35
36struct AwesomeRenderer : public RefBase {
37    AwesomeRenderer() {}
38
39    virtual void render(MediaBuffer *buffer) = 0;
40
41private:
42    AwesomeRenderer(const AwesomeRenderer &);
43    AwesomeRenderer &operator=(const AwesomeRenderer &);
44};
45
46struct AwesomePlayer {
47    AwesomePlayer();
48    ~AwesomePlayer();
49
50    void setListener(const wp<MediaPlayerBase> &listener);
51
52    status_t setDataSource(
53            const char *uri,
54            const KeyedVector<String8, String8> *headers = NULL);
55
56    status_t setDataSource(int fd, int64_t offset, int64_t length);
57
58    void reset();
59
60    status_t play();
61    status_t pause();
62
63    bool isPlaying() const;
64
65    void setISurface(const sp<ISurface> &isurface);
66    void setAudioSink(const sp<MediaPlayerBase::AudioSink> &audioSink);
67    status_t setLooping(bool shouldLoop);
68
69    status_t getDuration(int64_t *durationUs);
70    status_t getPosition(int64_t *positionUs);
71
72    status_t seekTo(int64_t timeUs);
73
74    status_t getVideoDimensions(int32_t *width, int32_t *height) const;
75
76private:
77    friend struct AwesomeEvent;
78
79    enum Flags {
80        PLAYING     = 1,
81        LOOPING     = 2,
82        FIRST_FRAME = 4,
83    };
84
85    mutable Mutex mLock;
86
87    OMXClient mClient;
88    TimedEventQueue mQueue;
89    wp<MediaPlayerBase> mListener;
90
91    sp<ISurface> mISurface;
92    sp<MediaPlayerBase::AudioSink> mAudioSink;
93
94    TimeSource *mTimeSource;
95
96    sp<MediaSource> mVideoSource;
97    sp<AwesomeRenderer> mVideoRenderer;
98
99    sp<MediaSource> mAudioSource;
100    AudioPlayer *mAudioPlayer;
101    int64_t mDurationUs;
102
103    uint32_t mFlags;
104
105    int32_t mVideoWidth, mVideoHeight;
106    int64_t mTimeSourceDeltaUs;
107    int64_t mVideoTimeUs;
108
109    bool mSeeking;
110    int64_t mSeekTimeUs;
111
112    sp<TimedEventQueue::Event> mVideoEvent;
113    bool mVideoEventPending;
114    sp<TimedEventQueue::Event> mStreamDoneEvent;
115    bool mStreamDoneEventPending;
116    sp<TimedEventQueue::Event> mBufferingEvent;
117    bool mBufferingEventPending;
118
119    void postVideoEvent_l(int64_t delayUs = -1);
120    void postBufferingEvent_l();
121    void postStreamDoneEvent_l();
122
123    MediaBuffer *mLastVideoBuffer;
124    MediaBuffer *mVideoBuffer;
125
126    sp<Prefetcher> mPrefetcher;
127
128    status_t setDataSource_l(const sp<MediaExtractor> &extractor);
129    void reset_l();
130    status_t seekTo_l(int64_t timeUs);
131    status_t pause_l();
132    void initRenderer_l();
133    void seekAudioIfNecessary_l();
134
135    void cancelPlayerEvents(bool keepBufferingGoing = false);
136
137    status_t setAudioSource(sp<MediaSource> source);
138    status_t setVideoSource(sp<MediaSource> source);
139
140    void onEvent(int32_t code);
141
142    static void AudioNotify(void *me, int what);
143    void onStreamDone();
144
145    void notifyListener_l(int msg, int ext1 = 0);
146
147    void onBufferingUpdate();
148
149    AwesomePlayer(const AwesomePlayer &);
150    AwesomePlayer &operator=(const AwesomePlayer &);
151};
152
153}  // namespace android
154
155#endif  // AWESOME_PLAYER_H_
156
157