1 /* 2 * Copyright (C) 2019 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 #pragma once 18 19 #include <stdint.h> 20 21 #include <chrono> 22 #include <memory> 23 #include <vector> 24 25 #include <https/RunLoop.h> 26 #include <source/StreamingSource.h> 27 28 struct RTPSender; 29 30 struct Packetizer : public std::enable_shared_from_this<Packetizer> { 31 32 using StreamingSource = android::StreamingSource; 33 34 explicit Packetizer(std::shared_ptr<RunLoop> runLoop, 35 std::shared_ptr<StreamingSource> source); 36 virtual ~Packetizer(); 37 38 virtual void run(); 39 virtual uint32_t rtpNow() const = 0; 40 int32_t requestIDRFrame(); 41 42 virtual void queueRTPDatagram(std::vector<uint8_t> *packet); 43 44 virtual void addSender(std::shared_ptr<RTPSender> sender); 45 46 virtual void onFrame(const std::shared_ptr<android::SBuffer>& accessUnit); 47 48 protected: 49 virtual void packetize(const std::shared_ptr<android::SBuffer>& accessUnit, 50 int64_t timeUs) = 0; 51 52 uint32_t timeSinceStart() const; 53 mediaStartTimePacketizer54 int64_t mediaStartTime() const { return mStartTimeMedia; } 55 56 private: 57 size_t mNumSamplesRead; 58 std::chrono::time_point<std::chrono::steady_clock> mStartTimeReal; 59 int64_t mStartTimeMedia; 60 61 std::shared_ptr<RunLoop> mRunLoop; 62 63 std::shared_ptr<StreamingSource> mStreamingSource; 64 65 std::vector<std::weak_ptr<RTPSender>> mSenders; 66 }; 67