AudioSRC.h 2.01 KB
Newer Older
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 * 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"

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:
44
    using Ptr = std::shared_ptr<AudioSRC>;
45 46 47 48 49 50 51 52 53
    AudioSRC(AudioSRCDelegate *);
    virtual ~AudioSRC();

    void setEnableMix(bool flag);
    void setOutputAudioConfig(const SDL_AudioSpec &cfg);
    int getPCMData(char *buf, int size);

private:
    bool _enabled = true;
54 55
    int _buf_size = 0;
    std::shared_ptr<char> _buf;
56
    AudioSRCDelegate *_delegate = nullptr;
夏楚 committed
57
    toolkit::BufferLikeString _target_buf;
58 59 60 61 62 63 64 65 66
    SDL_AudioCVT _audio_cvt;
};

class AudioPlayer : public AudioSRC, private AudioSRCDelegate{
public:
    AudioPlayer();
    ~AudioPlayer() override;

    void setup(int sample_rate, int channel, SDL_AudioFormat format);
ziyue committed
67
    void playPCM(const char *data, size_t size);
68 69 70 71 72 73 74 75 76 77

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;
夏楚 committed
78 79
    std::mutex _mtx;
    toolkit::BufferLikeString _buffer;
80 81 82 83
    SDLAudioDevice::Ptr _device;
};

#endif /* AUDIOSRC_H_ */