1/*
2 * Copyright 2013, 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 MEDIA_SENDER_H_
18
19#define MEDIA_SENDER_H_
20
21#include "rtp/RTPSender.h"
22
23#include <media/stagefright/foundation/ABase.h>
24#include <media/stagefright/foundation/AHandler.h>
25#include <utils/Errors.h>
26#include <utils/Vector.h>
27
28namespace android {
29
30struct ABuffer;
31struct ANetworkSession;
32struct AMessage;
33struct IHDCP;
34struct TSPacketizer;
35
36// This class facilitates sending of data from one or more media tracks
37// through one or more RTP channels, either providing a 1:1 mapping from
38// track to RTP channel or muxing all tracks into a single RTP channel and
39// using transport stream encapsulation.
40// Optionally the (video) data is encrypted using the provided hdcp object.
41struct MediaSender : public AHandler {
42    enum {
43        kWhatInitDone,
44        kWhatError,
45        kWhatNetworkStall,
46        kWhatInformSender,
47    };
48
49    MediaSender(
50            const sp<ANetworkSession> &netSession,
51            const sp<AMessage> &notify);
52
53    status_t setHDCP(const sp<IHDCP> &hdcp);
54
55    enum FlagBits {
56        FLAG_MANUALLY_PREPEND_SPS_PPS = 1,
57    };
58    ssize_t addTrack(const sp<AMessage> &format, uint32_t flags);
59
60    // If trackIndex == -1, initialize for transport stream muxing.
61    status_t initAsync(
62            ssize_t trackIndex,
63            const char *remoteHost,
64            int32_t remoteRTPPort,
65            RTPSender::TransportMode rtpMode,
66            int32_t remoteRTCPPort,
67            RTPSender::TransportMode rtcpMode,
68            int32_t *localRTPPort);
69
70    status_t queueAccessUnit(
71            size_t trackIndex, const sp<ABuffer> &accessUnit);
72
73protected:
74    virtual void onMessageReceived(const sp<AMessage> &msg);
75    virtual ~MediaSender();
76
77private:
78    enum {
79        kWhatSenderNotify,
80    };
81
82    enum Mode {
83        MODE_UNDEFINED,
84        MODE_TRANSPORT_STREAM,
85        MODE_ELEMENTARY_STREAMS,
86    };
87
88    struct TrackInfo {
89        sp<AMessage> mFormat;
90        uint32_t mFlags;
91        sp<RTPSender> mSender;
92        List<sp<ABuffer> > mAccessUnits;
93        ssize_t mPacketizerTrackIndex;
94        bool mIsAudio;
95    };
96
97    sp<ANetworkSession> mNetSession;
98    sp<AMessage> mNotify;
99
100    sp<IHDCP> mHDCP;
101
102    Mode mMode;
103    int32_t mGeneration;
104
105    Vector<TrackInfo> mTrackInfos;
106
107    sp<TSPacketizer> mTSPacketizer;
108    sp<RTPSender> mTSSender;
109    int64_t mPrevTimeUs;
110
111    size_t mInitDoneCount;
112
113    FILE *mLogFile;
114
115    void onSenderNotify(const sp<AMessage> &msg);
116
117    void notifyInitDone(status_t err);
118    void notifyError(status_t err);
119    void notifyNetworkStall(size_t numBytesQueued);
120
121    status_t packetizeAccessUnit(
122            size_t trackIndex,
123            sp<ABuffer> accessUnit,
124            sp<ABuffer> *tsPackets);
125
126    DISALLOW_EVIL_CONSTRUCTORS(MediaSender);
127};
128
129}  // namespace android
130
131#endif  // MEDIA_SENDER_H_
132
133