1/*
2 * Copyright 2012, 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 PLAYBACK_SESSION_H_
18
19#define PLAYBACK_SESSION_H_
20
21#include "MediaSender.h"
22#include "VideoFormats.h"
23#include "WifiDisplaySource.h"
24
25namespace android {
26
27struct ABuffer;
28struct BufferQueue;
29struct IHDCP;
30struct IGraphicBufferProducer;
31struct MediaPuller;
32struct MediaSource;
33struct MediaSender;
34struct NuMediaExtractor;
35
36// Encapsulates the state of an RTP/RTCP session in the context of wifi
37// display.
38struct WifiDisplaySource::PlaybackSession : public AHandler {
39    PlaybackSession(
40            const sp<ANetworkSession> &netSession,
41            const sp<AMessage> &notify,
42            const struct in_addr &interfaceAddr,
43            const sp<IHDCP> &hdcp,
44            const char *path = NULL);
45
46    status_t init(
47            const char *clientIP,
48            int32_t clientRtp,
49            RTPSender::TransportMode rtpMode,
50            int32_t clientRtcp,
51            RTPSender::TransportMode rtcpMode,
52            bool enableAudio,
53            bool usePCMAudio,
54            bool enableVideo,
55            VideoFormats::ResolutionType videoResolutionType,
56            size_t videoResolutionIndex,
57            VideoFormats::ProfileType videoProfileType,
58            VideoFormats::LevelType videoLevelType);
59
60    void destroyAsync();
61
62    int32_t getRTPPort() const;
63
64    int64_t getLastLifesignUs() const;
65    void updateLiveness();
66
67    status_t play();
68    status_t finishPlay();
69    status_t pause();
70
71    sp<IGraphicBufferProducer> getSurfaceTexture();
72
73    void requestIDRFrame();
74
75    enum {
76        kWhatSessionDead,
77        kWhatBinaryData,
78        kWhatSessionEstablished,
79        kWhatSessionDestroyed,
80    };
81
82protected:
83    virtual void onMessageReceived(const sp<AMessage> &msg);
84    virtual ~PlaybackSession();
85
86private:
87    struct Track;
88
89    enum {
90        kWhatMediaPullerNotify,
91        kWhatConverterNotify,
92        kWhatTrackNotify,
93        kWhatUpdateSurface,
94        kWhatPause,
95        kWhatResume,
96        kWhatMediaSenderNotify,
97        kWhatPullExtractorSample,
98    };
99
100    sp<ANetworkSession> mNetSession;
101    sp<AMessage> mNotify;
102    in_addr mInterfaceAddr;
103    sp<IHDCP> mHDCP;
104    AString mMediaPath;
105
106    sp<MediaSender> mMediaSender;
107    int32_t mLocalRTPPort;
108
109    bool mWeAreDead;
110    bool mPaused;
111
112    int64_t mLastLifesignUs;
113
114    sp<BufferQueue> mBufferQueue;
115
116    KeyedVector<size_t, sp<Track> > mTracks;
117    ssize_t mVideoTrackIndex;
118
119    int64_t mPrevTimeUs;
120
121    sp<NuMediaExtractor> mExtractor;
122    KeyedVector<size_t, size_t> mExtractorTrackToInternalTrack;
123    bool mPullExtractorPending;
124    int32_t mPullExtractorGeneration;
125    int64_t mFirstSampleTimeRealUs;
126    int64_t mFirstSampleTimeUs;
127
128    status_t setupMediaPacketizer(bool enableAudio, bool enableVideo);
129
130    status_t setupPacketizer(
131            bool enableAudio,
132            bool usePCMAudio,
133            bool enableVideo,
134            VideoFormats::ResolutionType videoResolutionType,
135            size_t videoResolutionIndex,
136            VideoFormats::ProfileType videoProfileType,
137            VideoFormats::LevelType videoLevelType);
138
139    status_t addSource(
140            bool isVideo,
141            const sp<MediaSource> &source,
142            bool isRepeaterSource,
143            bool usePCMAudio,
144            unsigned profileIdc,
145            unsigned levelIdc,
146            unsigned contraintSet,
147            size_t *numInputBuffers);
148
149    status_t addVideoSource(
150            VideoFormats::ResolutionType videoResolutionType,
151            size_t videoResolutionIndex,
152            VideoFormats::ProfileType videoProfileType,
153            VideoFormats::LevelType videoLevelType);
154
155    status_t addAudioSource(bool usePCMAudio);
156
157    status_t onMediaSenderInitialized();
158
159    void notifySessionDead();
160
161    void schedulePullExtractor();
162    void onPullExtractor();
163
164    void onSinkFeedback(const sp<AMessage> &msg);
165
166    DISALLOW_EVIL_CONSTRUCTORS(PlaybackSession);
167};
168
169}  // namespace android
170
171#endif  // PLAYBACK_SESSION_H_
172
173