1a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber/*
2a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber * Copyright 2013, The Android Open Source Project
3a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber *
4a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
5a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber * you may not use this file except in compliance with the License.
6a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber * You may obtain a copy of the License at
7a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber *
8a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber *     http://www.apache.org/licenses/LICENSE-2.0
9a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber *
10a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber * Unless required by applicable law or agreed to in writing, software
11a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
12a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber * See the License for the specific language governing permissions and
14a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber * limitations under the License.
15a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber */
16a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
17a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber#ifndef MEDIA_SENDER_H_
18a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
19a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber#define MEDIA_SENDER_H_
20a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
21a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber#include "rtp/RTPSender.h"
22a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
23a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber#include <media/stagefright/foundation/ABase.h>
24a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber#include <media/stagefright/foundation/AHandler.h>
25a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber#include <utils/Errors.h>
26a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber#include <utils/Vector.h>
27a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
28a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Hubernamespace android {
29a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
30a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huberstruct ABuffer;
31a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huberstruct ANetworkSession;
32a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huberstruct AMessage;
33a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huberstruct IHDCP;
34a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huberstruct TSPacketizer;
35a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
36a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber// This class facilitates sending of data from one or more media tracks
37a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber// through one or more RTP channels, either providing a 1:1 mapping from
38a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber// track to RTP channel or muxing all tracks into a single RTP channel and
39a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber// using transport stream encapsulation.
40a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber// Optionally the (video) data is encrypted using the provided hdcp object.
41a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huberstruct MediaSender : public AHandler {
42a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    enum {
43a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber        kWhatInitDone,
44a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber        kWhatError,
45126568c7aeeb5570789e70a310477f44dbdbd885Andreas Huber        kWhatNetworkStall,
46c86ef45279185b474bd6af0a7ae407f8ab577f13Andreas Huber        kWhatInformSender,
47a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    };
48a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
49a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    MediaSender(
50a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber            const sp<ANetworkSession> &netSession,
51a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber            const sp<AMessage> &notify);
52a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
53a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    status_t setHDCP(const sp<IHDCP> &hdcp);
54a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
55a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    enum FlagBits {
56a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber        FLAG_MANUALLY_PREPEND_SPS_PPS = 1,
57a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    };
58a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    ssize_t addTrack(const sp<AMessage> &format, uint32_t flags);
59a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
60a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    // If trackIndex == -1, initialize for transport stream muxing.
61a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    status_t initAsync(
62a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber            ssize_t trackIndex,
63a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber            const char *remoteHost,
64a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber            int32_t remoteRTPPort,
652aea9552aeba92bbaf9e56c666049ea2d14057b5Andreas Huber            RTPSender::TransportMode rtpMode,
66a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber            int32_t remoteRTCPPort,
672aea9552aeba92bbaf9e56c666049ea2d14057b5Andreas Huber            RTPSender::TransportMode rtcpMode,
68a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber            int32_t *localRTPPort);
69a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
70a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    status_t queueAccessUnit(
71a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber            size_t trackIndex, const sp<ABuffer> &accessUnit);
72a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
73a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huberprotected:
74a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    virtual void onMessageReceived(const sp<AMessage> &msg);
75a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    virtual ~MediaSender();
76a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
77a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huberprivate:
78a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    enum {
79a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber        kWhatSenderNotify,
80a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    };
81a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
82a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    enum Mode {
83a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber        MODE_UNDEFINED,
84a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber        MODE_TRANSPORT_STREAM,
85a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber        MODE_ELEMENTARY_STREAMS,
86a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    };
87a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
88a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    struct TrackInfo {
89a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber        sp<AMessage> mFormat;
90a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber        uint32_t mFlags;
91a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber        sp<RTPSender> mSender;
92a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber        List<sp<ABuffer> > mAccessUnits;
93a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber        ssize_t mPacketizerTrackIndex;
94a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber        bool mIsAudio;
95a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    };
96a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
97a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    sp<ANetworkSession> mNetSession;
98a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    sp<AMessage> mNotify;
99a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
100a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    sp<IHDCP> mHDCP;
101a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
102a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    Mode mMode;
103a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    int32_t mGeneration;
104a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
105a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    Vector<TrackInfo> mTrackInfos;
106a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
107a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    sp<TSPacketizer> mTSPacketizer;
108a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    sp<RTPSender> mTSSender;
109a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    int64_t mPrevTimeUs;
110a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
111a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    size_t mInitDoneCount;
112a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
113bfd79f2a8e795f304062e22756c72d995af7a0e6Andreas Huber    FILE *mLogFile;
114bfd79f2a8e795f304062e22756c72d995af7a0e6Andreas Huber
115a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    void onSenderNotify(const sp<AMessage> &msg);
116a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
117a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    void notifyInitDone(status_t err);
118a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    void notifyError(status_t err);
119126568c7aeeb5570789e70a310477f44dbdbd885Andreas Huber    void notifyNetworkStall(size_t numBytesQueued);
120a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
121a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    status_t packetizeAccessUnit(
122a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber            size_t trackIndex,
123a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber            sp<ABuffer> accessUnit,
124a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber            sp<ABuffer> *tsPackets);
125a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
126a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    DISALLOW_EVIL_CONSTRUCTORS(MediaSender);
127a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber};
128a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
129a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber}  // namespace android
130a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
131a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber#endif  // MEDIA_SENDER_H_
132a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
133