| 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 <list> |
| 24 |
|
| 25 |
#include <VideoToolbox/VTCompressionSession.h> |
| 26 |
|
| 27 |
#include "h26x-encoder.h" |
| 28 |
|
| 29 |
namespace mediastreamer { |
| 30 |
|
| 31 |
class VideoToolboxEncoder : public H26xEncoder { |
| 32 |
public: |
| 33 |
VideoToolboxEncoder(const std::string &mime, int maxPayloadSize); |
| 34 |
~VideoToolboxEncoder() { |
| 35 |
if (_session) CFRelease(_session); |
| 36 |
} |
| 37 |
|
| 38 |
MSVideoSize getVideoSize() const override { |
| 39 |
return _vsize; |
| 40 |
} |
| 41 |
void setVideoSize(const MSVideoSize &vsize) override { |
| 42 |
_vsize = vsize; |
| 43 |
} |
| 44 |
|
| 45 |
float getFps() const override { |
| 46 |
return _framerate; |
| 47 |
} |
| 48 |
void setFps(float fps) override; |
| 49 |
|
| 50 |
int getBitrate() const override { |
| 51 |
return _bitrate; |
| 52 |
} |
| 53 |
void setBitrate(int bitrate) override; |
| 54 |
|
| 55 |
bool isRunning() override { |
| 56 |
return _session != nullptr; |
| 57 |
} |
| 58 |
void start() override; |
| 59 |
void stop() override; |
| 60 |
|
| 61 |
void feed(mblk_t *rawData, uint64_t time, bool requestIFrame = false) override; |
| 62 |
bool fetch(MSQueue *encodedData) override; |
| 63 |
|
| 64 |
private: |
| 65 |
class Frame { |
| 66 |
public: |
| 67 |
Frame() { |
| 68 |
ms_queue_init(&_nalus); |
| 69 |
} |
| 70 |
Frame(Frame &&src); |
| 71 |
~Frame() { |
| 72 |
ms_queue_flush(&_nalus); |
| 73 |
} |
| 74 |
|
| 75 |
void put(mblk_t *m) { |
| 76 |
ms_queue_put(&_nalus, m); |
| 77 |
} |
| 78 |
void insert(MSQueue *q); |
| 79 |
mblk_t *get() { |
| 80 |
return ms_queue_get(&_nalus); |
| 81 |
} |
| 82 |
|
| 83 |
MSQueue *getQueue() { |
| 84 |
return &_nalus; |
| 85 |
} |
| 86 |
|
| 87 |
private: |
| 88 |
MSQueue _nalus; |
| 89 |
}; |
| 90 |
|
| 91 |
void applyFramerate(); |
| 92 |
void applyBitrate(); |
| 93 |
static void outputCb(void *outputCallbackRefCon, |
| 94 |
void *sourceFrameRefCon, |
| 95 |
OSStatus status, |
| 96 |
VTEncodeInfoFlags infoFlags, |
| 97 |
CMSampleBufferRef sampleBuffer); |
| 98 |
|
| 99 |
int _payloadMaxSize = 1460; |
| 100 |
MSVideoSize _vsize; |
| 101 |
float _framerate = 0.0f; |
| 102 |
int _bitrate = 0; |
| 103 |
VTCompressionSessionRef _session = nullptr; |
| 104 |
ms_mutex_t _mutex; |
| 105 |
std::list<Frame> _encodedFrames; |
| 106 |
}; |
| 107 |
|
| 108 |
} // namespace mediastreamer |