| 1 |
/* |
| 2 |
* Copyright (c) 2010-2022 Belledonne Communications SARL. |
| 3 |
* |
| 4 |
* This file is part of mediastreamer2 |
| 5 |
* (see https://gitlab.linphone.org/BC/public/mediastreamer2). |
| 6 |
* |
| 7 |
* This program is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Affero General Public License as |
| 9 |
* published by the Free Software Foundation, either version 3 of the |
| 10 |
* License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Affero General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Affero General Public License |
| 18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#pragma once |
| 22 |
|
| 23 |
#include <memory> |
| 24 |
|
| 25 |
#include "h26x-utils.h" |
| 26 |
#include "media/NdkMediaCodec.h" |
| 27 |
|
| 28 |
#include "h26x-encoder.h" |
| 29 |
|
| 30 |
namespace mediastreamer { |
| 31 |
|
| 32 |
class MediaCodecEncoder : public H26xEncoder { |
| 33 |
public: |
| 34 |
~MediaCodecEncoder(); |
| 35 |
|
| 36 |
const std::string &getMime() const { |
| 37 |
return _mime; |
| 38 |
} |
| 39 |
|
| 40 |
MSVideoSize getVideoSize() const override { |
| 41 |
return _vsize; |
| 42 |
} |
| 43 |
void setVideoSize(const MSVideoSize &vsize) override { |
| 44 |
_vsize = vsize; |
| 45 |
} |
| 46 |
|
| 47 |
float getFps() const override { |
| 48 |
return _fps; |
| 49 |
} |
| 50 |
void setFps(float fps) override; |
| 51 |
|
| 52 |
int getBitrate() const override { |
| 53 |
return _bitrate; |
| 54 |
} |
| 55 |
void setBitrate(int bitrate) override; |
| 56 |
|
| 57 |
bool pixelFormatConversionEnabled() const { |
| 58 |
return _pixelFormatConvertionEnabled; |
| 59 |
} |
| 60 |
void enablePixelFormatConversion(bool enable) { |
| 61 |
_pixelFormatConvertionEnabled = enable; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Controls whether we should avoid perform continous polling with dequeueOutputBuffer(). |
| 66 |
* Some drivers don't want this - for no good reason - and stop working. |
| 67 |
* When _hasOutbufferDequeueLimit is set to true, we'll insure that there is no more |
| 68 |
* dequeueOutputBuffer() than queueInputBuffer(). |
| 69 |
* This is generally not a good idea and is activated as workaround for these annoying drivers. |
| 70 |
* Indeed, an encoder may generate more output buffers than the number of input frames they are processing. |
| 71 |
**/ |
| 72 |
void enableOutbufferDequeueLimit(bool enable); |
| 73 |
|
| 74 |
bool isRunning() override { |
| 75 |
return _isRunning; |
| 76 |
} |
| 77 |
void start() override; |
| 78 |
void stop() override; |
| 79 |
|
| 80 |
void feed(mblk_t *rawData, uint64_t time, bool requestIFrame = false) override; |
| 81 |
bool fetch(MSQueue *encodedData) override; |
| 82 |
|
| 83 |
protected: |
| 84 |
MediaCodecEncoder(const std::string &mime); |
| 85 |
void createImpl(); |
| 86 |
void configureImpl(); |
| 87 |
virtual AMediaFormat *createMediaFormat() const; |
| 88 |
|
| 89 |
std::unique_ptr<H26xParameterSetsInserter> _psInserter; |
| 90 |
MSVideoSize _vsize; |
| 91 |
float _fps = 0; |
| 92 |
int _bitrate = 0; |
| 93 |
AMediaCodec *_impl = nullptr; |
| 94 |
int _pendingFrames = 0; |
| 95 |
bool _isRunning = false; |
| 96 |
bool _recoveryMode = false; |
| 97 |
bool _pixelFormatConvertionEnabled = true; |
| 98 |
bool _firstImageQueued = false; |
| 99 |
bool _hasOutbufferDequeueLimit = false; |
| 100 |
|
| 101 |
static const int _timeoutUs = 0; |
| 102 |
static const int32_t _colorFormat = 0x7f420888; // COLOR_FormatYUV420Flexible |
| 103 |
static const int32_t _bitrateMode = 1; // VBR mode |
| 104 |
static const int32_t _iFrameInterval = 20; // 20 seconds |
| 105 |
static const int32_t _encodingLatency = 1; |
| 106 |
static const int32_t _priority = 0; // real-time priority |
| 107 |
}; |
| 108 |
|
| 109 |
} // namespace mediastreamer |