forked from ZLMediaKit/ZLMediaKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
128 changed files
with
1,032 additions
and
63,695 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
find_path(SWRESAMPLE_INCLUDE_DIR | ||
NAMES libswresample/swresample.h) | ||
|
||
find_library(SWRESAMPLE_LIBRARY | ||
NAMES swresample) | ||
|
||
set(SWRESAMPLE_LIBRARIES ${SWRESAMPLE_LIBRARY}) | ||
set(SWRESAMPLE_INCLUDE_DIRS ${SWRESAMPLE_INCLUDE_DIR}) | ||
|
||
include(FindPackageHandleStandardArgs) | ||
|
||
find_package_handle_standard_args(SWRESAMPLE DEFAULT_MSG SWRESAMPLE_LIBRARY SWRESAMPLE_INCLUDE_DIR) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved. | ||
* | ||
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit). | ||
* | ||
* Use of this source code is governed by MIT license that can be found in the | ||
* LICENSE file in the root of the source tree. All contributing project authors | ||
* may be found in the AUTHORS file in the root of the source tree. | ||
*/ | ||
|
||
#include "Util/logger.h" | ||
#include "AudioSRC.h" | ||
#include "SDLAudioDevice.h" | ||
|
||
using namespace std; | ||
using namespace toolkit; | ||
|
||
AudioSRC::AudioSRC(AudioSRCDelegate *del) { | ||
_delegate = del; | ||
} | ||
|
||
AudioSRC::~AudioSRC() {} | ||
|
||
void AudioSRC::setOutputAudioConfig(const SDL_AudioSpec &cfg) { | ||
int freq = _delegate->getPCMSampleRate(); | ||
int format = _delegate->getPCMFormat(); | ||
int channels = _delegate->getPCMChannel(); | ||
if (-1 == SDL_BuildAudioCVT(&_audio_cvt, format, channels, freq, cfg.format, cfg.channels, cfg.freq)) { | ||
throw std::runtime_error("the format conversion is not supported"); | ||
} | ||
InfoL << "audio cvt origin format, freq:" << freq << ", format:" << hex << format << dec << ", channels:" << channels; | ||
InfoL << "audio cvt info, " | ||
<< "needed:" << _audio_cvt.needed | ||
<< ", src_format:" << hex << _audio_cvt.src_format | ||
<< ", dst_format:" << _audio_cvt.dst_format << dec | ||
<< ", rate_incr:" << _audio_cvt.rate_incr | ||
<< ", len_mult:" << _audio_cvt.len_mult | ||
<< ", len_ratio:" << _audio_cvt.len_ratio; | ||
} | ||
|
||
void AudioSRC::setEnableMix(bool flag) { | ||
_enabled = flag; | ||
} | ||
|
||
int AudioSRC::getPCMData(char *buf, int size) { | ||
if (!_enabled) { | ||
return 0; | ||
} | ||
if (!_audio_cvt.needed) { | ||
//获取原始数据,不需要频率转换 | ||
return _delegate->getPCMData(buf, size); | ||
} | ||
|
||
if ((int)(size / _audio_cvt.len_ratio) != _origin_size) { | ||
_origin_size = size / _audio_cvt.len_ratio; | ||
_origin_buf.reset(new char[std::max(_origin_size, size)], [](char *ptr) { | ||
delete[] ptr; | ||
}); | ||
InfoL << "origin pcm buffer size is:" << _origin_size << ", target pcm buffer size is:" << size; | ||
} | ||
|
||
auto origin_size = _delegate->getPCMData(_origin_buf.get(), _origin_size); | ||
if (!origin_size) { | ||
//获取数据失败 | ||
TraceL << "get empty pcm data"; | ||
return 0; | ||
} | ||
|
||
_audio_cvt.buf = (Uint8 *) _origin_buf.get(); | ||
_audio_cvt.len = origin_size; | ||
if (0 != SDL_ConvertAudio(&_audio_cvt)) { | ||
WarnL << "SDL_ConvertAudio failed!"; | ||
_audio_cvt.len_cvt = 0; | ||
} | ||
if (_audio_cvt.len_cvt) { | ||
_target_buf.append(_origin_buf.get(), _audio_cvt.len_cvt); | ||
} | ||
if (_target_buf.size() < size) { | ||
return 0; | ||
} | ||
memcpy(buf, _target_buf.data(), size); | ||
_target_buf.erase(0, size); | ||
return size; | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////// | ||
|
||
AudioPlayer::AudioPlayer() : AudioSRC(this) { | ||
_device = SDLAudioDevice::Instance().shared_from_this(); | ||
} | ||
|
||
AudioPlayer::~AudioPlayer() { | ||
_device->delChannel(this); | ||
} | ||
|
||
void AudioPlayer::setup(int sample_rate, int channel, SDL_AudioFormat format) { | ||
_sample_rate = sample_rate; | ||
_channel = channel; | ||
_format = format; | ||
_device->addChannel(this); | ||
} | ||
|
||
SDL_AudioFormat AudioPlayer::getPCMFormat() { | ||
return _format; | ||
} | ||
|
||
int AudioPlayer::getPCMSampleRate() { | ||
return _sample_rate; | ||
} | ||
|
||
int AudioPlayer::getPCMChannel() { | ||
return _channel; | ||
} | ||
|
||
int AudioPlayer::getPCMData(char *buf, int size) { | ||
if (_buffer.size() < size) { | ||
return 0; | ||
} | ||
memcpy(buf, _buffer.data(), size); | ||
_buffer.erase(0, size); | ||
return size; | ||
} | ||
|
||
void AudioPlayer::inputFrame(const char *data, size_t size){ | ||
_buffer.append(data, size); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved. | ||
* | ||
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit). | ||
* | ||
* Use of this source code is governed by MIT license that can be found in the | ||
* LICENSE file in the root of the source tree. All contributing project authors | ||
* may be found in the AUTHORS file in the root of the source tree. | ||
*/ | ||
|
||
#ifndef AUDIOSRC_H_ | ||
#define AUDIOSRC_H_ | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
#include "SDL2/SDL.h" | ||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#if defined(_WIN32) | ||
#pragma comment(lib,"SDL2.lib") | ||
#endif //defined(_WIN32) | ||
|
||
#include "Network/Buffer.h" | ||
#include "SDLAudioDevice.h" | ||
#include "FFMpegDecoder.h" | ||
|
||
using namespace std; | ||
using namespace toolkit; | ||
|
||
class AudioSRCDelegate { | ||
public: | ||
virtual ~AudioSRCDelegate() {}; | ||
virtual SDL_AudioFormat getPCMFormat() = 0; | ||
virtual int getPCMSampleRate() = 0; | ||
virtual int getPCMChannel() = 0; | ||
virtual int getPCMData(char *buf, int size) = 0; | ||
}; | ||
|
||
//该类实现pcm的重采样 | ||
class AudioSRC { | ||
public: | ||
typedef std::shared_ptr<AudioSRC> Ptr; | ||
AudioSRC(AudioSRCDelegate *); | ||
virtual ~AudioSRC(); | ||
|
||
void setEnableMix(bool flag); | ||
void setOutputAudioConfig(const SDL_AudioSpec &cfg); | ||
int getPCMData(char *buf, int size); | ||
|
||
private: | ||
bool _enabled = true; | ||
int _origin_size = 0; | ||
std::shared_ptr<char> _origin_buf; | ||
AudioSRCDelegate *_delegate = nullptr; | ||
BufferLikeString _target_buf; | ||
SDL_AudioCVT _audio_cvt; | ||
}; | ||
|
||
class AudioPlayer : public AudioSRC, private AudioSRCDelegate{ | ||
public: | ||
AudioPlayer(); | ||
~AudioPlayer() override; | ||
|
||
void setup(int sample_rate, int channel, SDL_AudioFormat format); | ||
void inputFrame(const char *data, size_t size); | ||
|
||
private: | ||
SDL_AudioFormat getPCMFormat() override; | ||
int getPCMSampleRate() override; | ||
int getPCMChannel() override; | ||
int getPCMData(char *buf, int size) override; | ||
|
||
private: | ||
int _sample_rate, _channel; | ||
SDL_AudioFormat _format; | ||
BufferLikeString _buffer; | ||
SDLAudioDevice::Ptr _device; | ||
}; | ||
|
||
#endif /* AUDIOSRC_H_ */ |
Oops, something went wrong.