Added song streamingh endpoint and removed redundant making oatpp::String shared when it already is a shared object #61

This commit is contained in:
kdeng00
2019-09-08 18:48:14 -04:00
parent 949abf8244
commit 19ddb9be0b
6 changed files with 83 additions and 102 deletions
+2
View File
@@ -6,6 +6,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(SOURCES set(SOURCES
src/callback/StreamCallback.cpp
src/database/AlbumRepository.cpp src/database/AlbumRepository.cpp
src/database/ArtistRepository.cpp src/database/ArtistRepository.cpp
src/database/BaseRepository.cpp src/database/BaseRepository.cpp
@@ -26,6 +27,7 @@ set(SOURCES
src/utility/MetadataRetriever.cpp src/utility/MetadataRetriever.cpp
) )
set(HEADERS set(HEADERS
include/callback/StreamCallback.h
include/component/AppComponent.hpp include/component/AppComponent.hpp
include/controller/AlbumController.hpp include/controller/AlbumController.hpp
include/controller/ArtistController.hpp include/controller/ArtistController.hpp
+27
View File
@@ -0,0 +1,27 @@
#ifndef STREAMCALLBACK_H_
#define STREAMCALLBACK_H_
#include <string>
#include "oatpp/core/data/stream/FileStream.hpp"
#include "oatpp/web/protocol/http/outgoing/ChunkedBody.hpp"
namespace callback
{
class StreamCallback : public oatpp::data::stream::ReadCallback
{
public:
StreamCallback();
StreamCallback(const std::string&);
oatpp::data::v_io_size read(void*, oatpp::data::v_io_size);
private:
std::string m_songPath;
long m_bytesRead;
long m_counter;
long m_fileSize;
};
}
#endif
+9 -99
View File
@@ -9,15 +9,16 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "oatpp/web/protocol/http/outgoing/ChunkedBody.hpp"
#include "oatpp/core/data/stream/ChunkedBuffer.hpp" #include "oatpp/core/data/stream/ChunkedBuffer.hpp"
#include "oatpp/core/data/stream/FileStream.hpp" #include "oatpp/core/data/stream/FileStream.hpp"
#include "oatpp/core/macro/codegen.hpp" #include "oatpp/core/macro/codegen.hpp"
#include "oatpp/core/macro/component.hpp" #include "oatpp/core/macro/component.hpp"
#include "oatpp/web/mime/multipart/InMemoryPartReader.hpp" #include "oatpp/web/mime/multipart/InMemoryPartReader.hpp"
#include "oatpp/web/mime/multipart/Reader.hpp" #include "oatpp/web/mime/multipart/Reader.hpp"
#include "oatpp/web/protocol/http/outgoing/ChunkedBody.hpp"
#include "oatpp/web/server/api/ApiController.hpp" #include "oatpp/web/server/api/ApiController.hpp"
#include "callback/StreamCallback.h"
#include "database/SongRepository.h" #include "database/SongRepository.h"
#include "dto/SongDto.hpp" #include "dto/SongDto.hpp"
#include "manager/SongManager.h" #include "manager/SongManager.h"
@@ -30,93 +31,6 @@ namespace fs = std::filesystem;
namespace controller namespace controller
{ {
class myCallback : public oatpp::data::stream::ReadCallback
{
public:
myCallback()
{
std::cout << "the non desired constructor chosen" << std::endl;
std::exit(-1);
}
myCallback(const std::string& songPath)
: m_songPath(songPath.c_str()),
m_counter(0)
{
std::cout << "the desired constructor chosen" << std::endl;
auto b = oatpp::base::StrBuffer::loadFromFile(m_songPath.c_str());
m_fileSize = b->getSize();
}
myCallback(myCallback& c) = default;
// I know, bad naming convention
oatpp::data::v_io_size demoStreamZero(void *buff, oatpp::data::v_io_size count)
{
oatpp::data::stream::FileInputStream rs(m_songPath.c_str());
return rs.read(buff, count);
}
oatpp::data::v_io_size demoStreamOne(void *buff, oatpp::data::v_io_size count)
{
std::cout << "file size " << m_fileSize << std::endl;
std::cout << "getting file input stream" << std::endl;
oatpp::data::stream::FileInputStream rs(m_songPath.c_str());
std::cout << "retrieving file" << std::endl;
auto f = rs.getFile();
std::cout << "file retrieved" << std::endl;
std::cout << "starting m_counter val " << m_counter << std::endl;;
auto newPos = m_counter * count;
auto bytesLeft = m_fileSize - m_bytesRead;
std::cout << "bytes left " << bytesLeft << std::endl;
//if (newPos >= m_fileSize) {
if (bytesLeft <= 0) {
std::cout << "pos " << newPos << std::endl;
std::cout << "done reading" << std::endl;
return 0;
}
auto fPos = std::ftell(f);
std::cout << "current file position indicator " << fPos << std::endl;
std::fseek(f, newPos, SEEK_SET);
fPos = std::ftell(f);
std::cout << "current file position indicator " << fPos << std::endl;
buff = malloc(count);
std::fgets((char*)buff, count, f);
std::cout << "ending m_counter val " << m_counter << std::endl;
m_bytesRead += count;
++m_counter;
return count;
}
oatpp::data::v_io_size read(void *buff, oatpp::data::v_io_size count)
{
return demoStreamZero(buff, count);
}
private:
std::string m_songPath;
long m_fileSize;
long m_bytesRead = 0;
long m_counter;
int m_iterations;
};
class SongController : public oatpp::web::server::api::ApiController class SongController : public oatpp::web::server::api::ApiController
{ {
public: public:
@@ -242,9 +156,7 @@ namespace controller
// TODO: work on this to handle the database records // TODO: work on this to handle the database records
ENDPOINT("DELETE", "api/v1/song/data/{id}", songDelete, PATH(Int32, id)) { ENDPOINT("DELETE", "api/v1/song/data/{id}", songDelete, PATH(Int32, id)) {
model::Song song(id);
model::Song song;
song.id = id;
manager::SongManager sngMgr(m_bConf); manager::SongManager sngMgr(m_bConf);
sngMgr.deleteSong(song); sngMgr.deleteSong(song);
@@ -255,21 +167,17 @@ namespace controller
// TODO: create endpoint for streaming songs // TODO: create endpoint for streaming songs
ENDPOINT("GET", "/api/v1/song/stream/{id}", streamSong, ENDPOINT("GET", "/api/v1/song/stream/{id}", streamSong,
PATH(Int32, id)) { PATH(Int32, id)) {
database::SongRepository songRepo(m_bConf); database::SongRepository songRepo(m_bConf);
model::Song songDb(id); model::Song songDb(id);
songDb = songRepo.retrieveRecord(songDb, type::SongFilter::id); songDb = songRepo.retrieveRecord(songDb, type::SongFilter::id);
myCallback cb(songDb.songPath); oatpp::data::v_io_size dSize = 1024;
auto cb_sh = std::make_shared<myCallback>(cb);
auto dSize = 1024; auto db = std::make_shared<oatpp::web::protocol::http::outgoing::ChunkedBody>(
dSize = 100000; std::make_shared<callback::StreamCallback>(songDb.songPath), nullptr, dSize);
auto db = std::make_shared<oatpp::web::protocol::http::outgoing::ChunkedBody>(oatpp::web::protocol::http::outgoing::ChunkedBody(cb_sh, nullptr, dSize));
auto response = OutgoingResponse::createShared(Status::CODE_200, db); auto response = OutgoingResponse::createShared(Status::CODE_200, db);
//auto response = createResponse(Status::CODE_200, "");
response->putHeader(Header::CONNECTION, Header::Value::CONNECTION_KEEP_ALIVE); response->putHeader(Header::CONNECTION, Header::Value::CONNECTION_KEEP_ALIVE);
response->putHeader("Accept-Ranges", "bytes");
response->putHeader(Header::CONTENT_TYPE, "audio/mpeg"); response->putHeader(Header::CONTENT_TYPE, "audio/mpeg");
return response; return response;
@@ -278,7 +186,9 @@ namespace controller
#include OATPP_CODEGEN_END(ApiController) #include OATPP_CODEGEN_END(ApiController)
private: private:
std::string m_exe_path; std::string m_exe_path;
model::BinaryPath m_bConf; model::BinaryPath m_bConf;
const long m_dataSize = std::numeric_limits<long long int>::max(); const long m_dataSize = std::numeric_limits<long long int>::max();
}; };
} }
+2 -2
View File
@@ -112,8 +112,8 @@ namespace model
{ {
BinaryPath() = default; BinaryPath() = default;
BinaryPath(const char *p) : path(std::move(p)) { } BinaryPath(const char *p) : path(std::move(p)) { }
BinaryPath(std::string& p) : path(std::move(p)) { } BinaryPath(std::string& p) : path(p) { }
BinaryPath(const std::string& p) : path(std::move(p)) { } BinaryPath(const std::string& p) : path(p) { }
std::string path; std::string path;
}; };
+1 -1
View File
@@ -5,7 +5,7 @@
#include <string> #include <string>
#include <mysql/mysql.h> #include <mysql/mysql.h>
#include "oatpp/network/server/Server.hpp" #include <oatpp/network/server/Server.hpp>
#include "oatpp/network/server/SimpleTCPConnectionProvider.hpp" #include "oatpp/network/server/SimpleTCPConnectionProvider.hpp"
#include "oatpp/web/server/HttpConnectionHandler.hpp" #include "oatpp/web/server/HttpConnectionHandler.hpp"
+42
View File
@@ -0,0 +1,42 @@
#include "callback/StreamCallback.h"
#include <iostream>
#include <fstream>
#include <memory>
callback::StreamCallback::StreamCallback() :
m_counter(0) { }
callback::StreamCallback::StreamCallback(const std::string& songPath) :
m_songPath(songPath),
m_counter(0),
m_bytesRead(0)
{
// getting file size
std::ifstream song(m_songPath.c_str(), std::ios::binary | std::ios::in | std::ios::ate);
m_fileSize = song.tellg();
std::cout << "file size is " << m_fileSize << " bytes" << std::endl;
}
oatpp::data::v_io_size callback::StreamCallback::read(void *buff, oatpp::data::v_io_size count)
{
if (m_counter >= m_fileSize) {
std::cout << "done streaming song" << std::endl;
return 0;
}
std::fstream song(m_songPath.c_str(), std::ios::binary | std::ios::in);
song.seekg(m_counter);
std::unique_ptr<char[]> data(new char[count]);
song.read(data.get(), count);
std::memcpy(buff, data.get(), count);
m_counter += count;
song.close();
return count;
}