说明
核心原子变量是pDeviceStatus,该变量的夸线程修改,就不需要使用锁了;
std::atomic<IPDeviceStatus*> pDeviceStatus = new IPDeviceStatus();
代码功能
某公共场所使用的IP喇叭,类似物联网设备,基于嵌入式的设备的应用,应用层通过http协议推送语音文件进行播放,这是客户的项目,不变全部公开
核心代码
// IPSound.cpp: 定义应用程序的入口点。
//
#include <iostream>
#include "IPSound.h"
#include "IPSoundDeviceModel.h"
#include "httplib.h"
#include "json.hpp"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <vector>
#include <sys/time.h>
#define MAX_FILE_LEN 1024
#define MP3_PLAY_PRG "mpg123"
#define WAV_PLAY_PRG "aplay"
#define MP3_AUDIO "mp3"
#define WAV_AUDIO "wav"
using json = nlohmann::json;
// std::atomic<IPDeviceStatus*> pDeviceStatus = new IPDeviceStatus();
std::atomic<IPDeviceStatus*> pDeviceStatus = new IPDeviceStatus();
std::atomic<IPSoundDeviceModel*> pDevModel = 0;
bool gForceExit = false;
void* stopPlay(void *param)
{
std::cout << "receive force stop command --- Start" << endl;
...
std::cout << "receive force stop command --- End" << endl;
return 0;
}
void* playAudio(void *param)
{
char command[MAX_FILE_LEN *2 ] = {0};
string subfix = (*pDeviceStatus).getCurrentFileSubfix();
...
// 如果没有需要播放的文件
(*pDeviceStatus).mStatus = 0;
// 重置当前文件信息
(*pDeviceStatus).reset();
return 0;
}
void handle_post(const httplib::Request& req, httplib::Response& res)
{
// 处理POST请求的逻辑
std::cout << "Received a POST request!" << std::endl;
std::string postData = req.body;
std::cout << postData << endl;
auto size = req.files.size();
std::cout << "--0-->" << size << endl;
auto ret = req.has_file("file");
int playStatus = 0;
// 如果是停止播放
if (!ret){
std::cout << "hell01" << endl;
// 启动一个线程执行任务
pthread_t t;
pthread_create(&t,nullptr,stopPlay,nullptr);
std::cout << "hell02" << endl;
// stopPlay();
// stop Play
// 设置播放状态
(*pDeviceStatus).mStatus = 0;
std::cout << "hell03" << endl;
}
else{
const auto& file = req.get_file_value("file");
std::cout << "--1-->" << file.filename << endl;
std::cout << "--2-->" << file.content_type<< endl;
// // 将上传的图像文件写入本地文件系统中
char filePath[MAX_FILE_LEN] = {0};
struct timeval tv;
if (gettimeofday(&tv, NULL) == -1) {
perror("Failed to get current time");
exit(EXIT_FAILURE);
}
unsigned long milliseconds = tv.tv_sec * 1000 + tv.tv_usec / 1000;
sprintf(filePath, "./tmp/%lu_%s",milliseconds, file.filename.c_str());
//strcpy(filePath,"/home/user/aaa.mp3");
cout << "save file path = " << filePath << endl;
FILE* fp = fopen(filePath, "wb");
if (!fp) {
cout << "file cache failed" << endl;
res.status = 500;
return;
}
fwrite(file.content.data(), sizeof(char), file.content.size(), fp);
fclose(fp);
// 如果当前正在播放,则进入队列
if ((*pDeviceStatus).mStatus == 1){
std::cout << "==============设备忙,添加到等待队列==============" << endl;
AudioTask audioTaks ;
audioTaks.mTaskID = "aaa0001";
audioTaks.mAudioFile = filePath;
(*pDeviceStatus).mPlayList.push_back(audioTaks);
playStatus = 2;
}
else{
(*pDeviceStatus).mCurrentFile.mTaskID = "aaa0001";
(*pDeviceStatus).mCurrentFile.mAudioFile = filePath;
playStatus = 1;
// 设置播放状态
(*pDeviceStatus).mStatus = 1;
// 调用播放函数
// 启动一个线程执行任务
pthread_t t;
pthread_create(&t,nullptr,playAudio,nullptr);
//playAudio();
}
}
json rtnJson;
rtnJson["code"] = "00000000";
if (playStatus == 1){
rtnJson["msg"] = "添加成功立即播放";
}
else if (playStatus == 0){
rtnJson["msg"] = "取消播放成功";
}
else{
rtnJson["msg"] = "设备忙,已排队";
}
// 返回响应内容
res.set_content(rtnJson.dump(), "application/json;charset=utf-8");
}
int main(void)
{
using namespace httplib;
pDevModel = new IPSoundDeviceModel();
Server svr;
// curl http://192.168.7.103:8888/api/v1/player/deviceInfo
// 查询设备信息
svr.Get("/api/v1/player/deviceInfo", [&](const Request& req, Response& res) {
std::cout << "/api/v1/player/deviceInfo " << endl;
res.set_content((*pDevModel).toJsonString(), "application/json;charset=utf-8");
});
// curl http://192.168.7.103:8888/api/v1/player/status
// 查询设备状态
svr.Get("/api/v1/player/status", [&](const Request& req, Response& res) {
std::cout << "/api/v1/player/status" << endl;
res.set_content((*pDeviceStatus).toJsonString(), "application/json;charset=utf-8");
});
// curl -X POST "audioId=&1&volume=30" -F "file=@/home/user/Work/9.mp3" http://192.168.7.103:8888/api/v1/player/audio
// 播放音乐
svr.Post("/api/v1/player/audio", [&](const auto& req, auto& res) {
std::cout << "multipart invoke " << endl;
handle_post(req,res);
std::cout << "multipart invoke END----------------" << endl;
});
// 升级
// curl -X POST "audioId=&1&volume=30" http://192.168.7.103:8888/api/v1/player/audio
svr.Post("/api/v1/player/upGrade", [&](const Request& req, Response& res) {
std::cout << "/api/v1/player/upGrade" << endl;
res.set_content((*pDeviceStatus).toJsonString(), "application/json;charset=utf-8");
});
std::cout << "Server on :" << DEFAULT_PORT << endl;
svr.listen("0.0.0.0", DEFAULT_PORT);
}
我公司承接各类技术服务,主要聚焦于:stm32、单片机、嵌入式、QT应用开发、Web+Python+Django应用开发。欢迎合作。