-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement partial responses support in MercuryManager (#171)
* fixes for esp32 * fixes for esp32 * reuploaded sdkconfig.defaults * added gzip compatible mercury decoder (failed if message were trucated) - updated from main-branch * fix: clang-format * fix: Minor style fixes * fix: clear partials on sub res and reconnect * fix: Restore bell version * fix: Update bell --------- Co-authored-by: Filip <[email protected]>
- Loading branch information
1 parent
1b07a9c
commit 5c35a33
Showing
11 changed files
with
571 additions
and
404 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
Header.uri max_size:256, fixed_length:false | ||
Header.method max_size:64, fixed_length:false | ||
UserField.key type:FT_POINTER | ||
UserField.value type:FT_POINTER | ||
Header.user_fields max_count:64, fixed_count:false |
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 |
---|---|---|
@@ -1,4 +1,12 @@ | ||
message Header { | ||
optional string uri = 0x01; | ||
optional string content_type = 0x02; | ||
optional string method = 0x03; | ||
optional int32 status_code = 0x04; | ||
repeated UserField user_fields = 0x06; | ||
} | ||
|
||
message UserField { | ||
optional string key = 0x01; | ||
optional string value = 0x02; | ||
} |
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,134 @@ | ||
#include "EspPlayer.h" | ||
|
||
#include <cstdint> // for uint8_t | ||
#include <functional> // for __base | ||
#include <iostream> // for operator<<, basic_ostream, endl, cout | ||
#include <memory> // for shared_ptr, make_shared, make_unique | ||
#include <mutex> // for scoped_lock | ||
#include <string_view> // for hash, string_view | ||
#include <type_traits> // for remove_extent_t | ||
#include <utility> // for move | ||
#include <variant> // for get | ||
#include <vector> // for vector | ||
|
||
#include "BellUtils.h" // for BELL_SLEEP_MS | ||
#include "CircularBuffer.h" | ||
#include "Logger.h" | ||
#include "SpircHandler.h" // for SpircHandler, SpircHandler::EventType | ||
#include "StreamInfo.h" // for BitWidth, BitWidth::BW_16 | ||
#include "TrackPlayer.h" // for TrackPlayer | ||
|
||
EspPlayer::EspPlayer(std::unique_ptr<AudioSink> sink, | ||
std::shared_ptr<cspot::SpircHandler> handler) | ||
: bell::Task("player", 32 * 1024, 0, 1) { | ||
this->handler = handler; | ||
this->audioSink = std::move(sink); | ||
|
||
this->circularBuffer = std::make_shared<bell::CircularBuffer>(1024 * 128); | ||
|
||
auto hashFunc = std::hash<std::string_view>(); | ||
|
||
this->handler->getTrackPlayer()->setDataCallback( | ||
[this, &hashFunc](uint8_t* data, size_t bytes, std::string_view trackId) { | ||
auto hash = hashFunc(trackId); | ||
this->feedData(data, bytes, hash); | ||
return bytes; | ||
}); | ||
|
||
this->isPaused = false; | ||
|
||
this->handler->setEventHandler( | ||
[this, &hashFunc](std::unique_ptr<cspot::SpircHandler::Event> event) { | ||
switch (event->eventType) { | ||
case cspot::SpircHandler::EventType::PLAY_PAUSE: | ||
if (std::get<bool>(event->data)) { | ||
this->pauseRequested = true; | ||
} else { | ||
this->isPaused = false; | ||
this->pauseRequested = false; | ||
} | ||
break; | ||
case cspot::SpircHandler::EventType::DISC: | ||
this->circularBuffer->emptyBuffer(); | ||
break; | ||
case cspot::SpircHandler::EventType::FLUSH: | ||
this->circularBuffer->emptyBuffer(); | ||
break; | ||
case cspot::SpircHandler::EventType::SEEK: | ||
this->circularBuffer->emptyBuffer(); | ||
break; | ||
case cspot::SpircHandler::EventType::PLAYBACK_START: | ||
this->isPaused = true; | ||
this->playlistEnd = false; | ||
this->circularBuffer->emptyBuffer(); | ||
break; | ||
case cspot::SpircHandler::EventType::DEPLETED: | ||
this->playlistEnd = true; | ||
break; | ||
case cspot::SpircHandler::EventType::VOLUME: { | ||
int volume = std::get<int>(event->data); | ||
break; | ||
} | ||
default: | ||
break; | ||
} | ||
}); | ||
startTask(); | ||
} | ||
|
||
void EspPlayer::feedData(uint8_t* data, size_t len, size_t trackId) { | ||
size_t toWrite = len; | ||
|
||
while (toWrite > 0) { | ||
this->current_hash = trackId; | ||
size_t written = | ||
this->circularBuffer->write(data + (len - toWrite), toWrite); | ||
if (written == 0) { | ||
BELL_SLEEP_MS(10); | ||
} | ||
|
||
toWrite -= written; | ||
} | ||
} | ||
|
||
void EspPlayer::runTask() { | ||
std::vector<uint8_t> outBuf = std::vector<uint8_t>(1024); | ||
|
||
std::scoped_lock lock(runningMutex); | ||
|
||
size_t lastHash = 0; | ||
|
||
while (isRunning) { | ||
if (!this->isPaused) { | ||
size_t read = this->circularBuffer->read(outBuf.data(), outBuf.size()); | ||
if (this->pauseRequested) { | ||
this->pauseRequested = false; | ||
std::cout << "Pause requested!" << std::endl; | ||
this->isPaused = true; | ||
} | ||
|
||
this->audioSink->feedPCMFrames(outBuf.data(), read); | ||
|
||
if (read == 0) { | ||
if (this->playlistEnd) { | ||
this->handler->notifyAudioEnded(); | ||
this->playlistEnd = false; | ||
} | ||
BELL_SLEEP_MS(10); | ||
continue; | ||
} else { | ||
if (lastHash != current_hash) { | ||
lastHash = current_hash; | ||
this->handler->notifyAudioReachedPlayback(); | ||
} | ||
} | ||
} else { | ||
BELL_SLEEP_MS(100); | ||
} | ||
} | ||
} | ||
|
||
void EspPlayer::disconnect() { | ||
isRunning = false; | ||
std::scoped_lock lock(runningMutex); | ||
} |
Oops, something went wrong.