Home | History | Annotate | Download | only in stagefright
      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 AUDIO_PLAYER_H_
     18 
     19 #define AUDIO_PLAYER_H_
     20 
     21 #include <media/MediaPlayerInterface.h>
     22 #include <media/stagefright/MediaBuffer.h>
     23 #include <media/stagefright/TimeSource.h>
     24 #include <utils/threads.h>
     25 
     26 namespace android {
     27 
     28 struct AudioPlaybackRate;
     29 class AudioTrack;
     30 struct AwesomePlayer;
     31 class MediaSource;
     32 
     33 class AudioPlayer : public TimeSource {
     34 public:
     35     enum {
     36         REACHED_EOS,
     37         SEEK_COMPLETE
     38     };
     39 
     40     enum {
     41         ALLOW_DEEP_BUFFERING = 0x01,
     42         USE_OFFLOAD = 0x02,
     43         HAS_VIDEO   = 0x1000,
     44         IS_STREAMING = 0x2000
     45 
     46     };
     47 
     48     AudioPlayer(const sp<MediaPlayerBase::AudioSink> &audioSink,
     49                 uint32_t flags = 0,
     50                 AwesomePlayer *audioObserver = NULL);
     51 
     52     virtual ~AudioPlayer();
     53 
     54     // Caller retains ownership of "source".
     55     void setSource(const sp<MediaSource> &source);
     56 
     57     // Return time in us.
     58     virtual int64_t getRealTimeUs();
     59 
     60     status_t start(bool sourceAlreadyStarted = false);
     61 
     62     void pause(bool playPendingSamples = false);
     63     status_t resume();
     64 
     65     // Returns the timestamp of the last buffer played (in us).
     66     int64_t getMediaTimeUs();
     67 
     68     // Returns true iff a mapping is established, i.e. the AudioPlayer
     69     // has played at least one frame of audio.
     70     bool getMediaTimeMapping(int64_t *realtime_us, int64_t *mediatime_us);
     71 
     72     status_t seekTo(int64_t time_us);
     73 
     74     bool isSeeking();
     75     bool reachedEOS(status_t *finalStatus);
     76 
     77     status_t setPlaybackRate(const AudioPlaybackRate &rate);
     78     status_t getPlaybackRate(AudioPlaybackRate *rate /* nonnull */);
     79 
     80     void notifyAudioEOS();
     81 
     82 private:
     83     friend class VideoEditorAudioPlayer;
     84     sp<MediaSource> mSource;
     85     sp<AudioTrack> mAudioTrack;
     86 
     87     MediaBuffer *mInputBuffer;
     88 
     89     int mSampleRate;
     90     int64_t mLatencyUs;
     91     size_t mFrameSize;
     92 
     93     Mutex mLock;
     94     int64_t mNumFramesPlayed;
     95     int64_t mNumFramesPlayedSysTimeUs;
     96 
     97     int64_t mPositionTimeMediaUs;
     98     int64_t mPositionTimeRealUs;
     99 
    100     bool mSeeking;
    101     bool mReachedEOS;
    102     status_t mFinalStatus;
    103     int64_t mSeekTimeUs;
    104 
    105     bool mStarted;
    106 
    107     bool mIsFirstBuffer;
    108     status_t mFirstBufferResult;
    109     MediaBuffer *mFirstBuffer;
    110 
    111     sp<MediaPlayerBase::AudioSink> mAudioSink;
    112     AwesomePlayer *mObserver;
    113     int64_t mPinnedTimeUs;
    114 
    115     bool mPlaying;
    116     int64_t mStartPosUs;
    117     const uint32_t mCreateFlags;
    118 
    119     static void AudioCallback(int event, void *user, void *info);
    120     void AudioCallback(int event, void *info);
    121 
    122     static size_t AudioSinkCallback(
    123             MediaPlayerBase::AudioSink *audioSink,
    124             void *data, size_t size, void *me,
    125             MediaPlayerBase::AudioSink::cb_event_t event);
    126 
    127     size_t fillBuffer(void *data, size_t size);
    128 
    129     int64_t getRealTimeUsLocked() const;
    130 
    131     void reset();
    132 
    133     uint32_t getNumFramesPendingPlayout() const;
    134     int64_t getOutputPlayPositionUs_l();
    135 
    136     bool allowDeepBuffering() const { return (mCreateFlags & ALLOW_DEEP_BUFFERING) != 0; }
    137     bool useOffload() const { return (mCreateFlags & USE_OFFLOAD) != 0; }
    138 
    139     AudioPlayer(const AudioPlayer &);
    140     AudioPlayer &operator=(const AudioPlayer &);
    141 };
    142 
    143 }  // namespace android
    144 
    145 #endif  // AUDIO_PLAYER_H_
    146