Skip to content

Commit

Permalink
October 2022 Release of the APL 2022.2 compliant APL Client Library
Browse files Browse the repository at this point in the history
For more details on this release refer to CHANGELOG.md

To learn about APL see: https://developer.amazon.com/docs/alexa-presentation-language/understand-apl.html
  • Loading branch information
zhamingc committed Oct 13, 2022
1 parent 02f3020 commit 71877ed
Show file tree
Hide file tree
Showing 50 changed files with 1,759 additions and 290 deletions.
120 changes: 120 additions & 0 deletions APLClient/include/APLClient/AplCoreAudioPlayer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

#ifndef APLCLIENT_APL_APLCOREAUDIOPLAYER_H
#define APLCLIENT_APL_APLCOREAUDIOPLAYER_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreorder"
#pragma push_macro("DEBUG")
#pragma push_macro("TRUE")
#pragma push_macro("FALSE")
#undef DEBUG
#undef TRUE
#undef FALSE
#include <apl/apl.h>
#pragma pop_macro("DEBUG")
#pragma pop_macro("TRUE")
#pragma pop_macro("FALSE")
#pragma GCC diagnostic pop

#include "AplConfiguration.h"
#include "AplCoreConnectionManager.h"

namespace APLClient {

/**
* AudioPlayer adapter. Controls browser-owned player.
*/
class AplCoreAudioPlayer : public apl::AudioPlayer {
public:
/**
* Constructor
*
* @param aplCoreConnectionManager Pointer to the APL Core connection manager
* @param config Pointer to APL configuration
* @param playerId PLayer ID.
* @param playerCallback Player events callback.
* @param speechMarkCallback TTS speechmarks callback.
*/
AplCoreAudioPlayer(
AplCoreConnectionManagerWPtr aplCoreConnectionManager,
AplConfigurationPtr config,
const std::string& playerId,
apl::AudioPlayerCallback&& playerCallback,
apl::SpeechMarkCallback&& speechMarkCallback);

/**
* @param aplCoreConnectionManager Pointer to the APL Core connection manager
* @param config Pointer to APL configuration
* @param playerId PLayer ID.
* @param playerCallback Player events callback.
* @param speechMarkCallback TTS speechmarks callback.
*/
static std::shared_ptr<AplCoreAudioPlayer> create(
AplCoreConnectionManagerWPtr aplCoreConnectionManager,
AplConfigurationPtr config,
const std::string& playerId,
apl::AudioPlayerCallback&& playerCallback,
apl::SpeechMarkCallback&& speechMarkCallback);

/// @name apl::AudioPlayer Functions
/// @{
void release() override;
void setTrack(apl::MediaTrack track) override;
void play(apl::ActionRef actionRef) override;
void pause() override;
/// @}

/**
* Process AudioPlayer event received from the browser.
* @param payload message payload.
*/
void onEvent(const rapidjson::Value& payload);

/**
* Process SpeechMarks received from the browser.
* @param payload message payload.
*/
void onSpeechMarks(const rapidjson::Value& payload);

/**
* Simulate time update.
*
* @param connectionManager AplCoreConnectionManager
*/
void tick(const AplCoreConnectionManager& connectionManager);

private:
void sendAudioPlayerCommand(const std::string& command, std::string optionalUrl = "");
void resolveExistingAction();
void onEventInternal(apl::AudioPlayerEventType eventType, const apl::AudioState& audioState, int currentTime);

private:
AplCoreConnectionManagerWPtr m_aplCoreConnectionManager;
AplConfigurationPtr m_aplConfiguration;

apl::ActionRef m_PlayRef = apl::ActionRef(nullptr);
bool m_Playing = false;
bool m_Prepared = false;
int m_PlaybackStartTime = 0;
std::string m_playerId;
};

using AplCoreAudioPlayerPtr = std::shared_ptr<AplCoreAudioPlayer>;

} // namespace APLClient

#endif // APLCLIENT_APL_APLCOREAUDIOPLAYER_H
82 changes: 82 additions & 0 deletions APLClient/include/APLClient/AplCoreAudioPlayerFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

#ifndef APLCLIENT_APL_APLCOREAUDIOPLAYERFACTORY_H
#define APLCLIENT_APL_APLCOREAUDIOPLAYERFACTORY_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreorder"
#pragma push_macro("DEBUG")
#pragma push_macro("TRUE")
#pragma push_macro("FALSE")
#undef DEBUG
#undef TRUE
#undef FALSE
#include <apl/apl.h>
#pragma pop_macro("DEBUG")
#pragma pop_macro("TRUE")
#pragma pop_macro("FALSE")
#pragma GCC diagnostic pop

#include "AplConfiguration.h"
#include "AplCoreConnectionManager.h"
#include "AplCoreAudioPlayer.h"

namespace APLClient {

/**
* AudioPlayer factory.
*/
class AplCoreAudioPlayerFactory : public apl::AudioPlayerFactory {
public:
/**
* Constructor
*
* @param aplCoreConnectionManager Pointer to the APL Core connection manager
* @param config Pointer to APL configuration
*/
AplCoreAudioPlayerFactory(AplCoreConnectionManagerPtr aplCoreConnectionManager, AplConfigurationPtr config);

/**
* @param aplCoreConnectionManager Pointer to the APL Core connection manager
* @param config Pointer to APL configuration
*/
static std::shared_ptr<AplCoreAudioPlayerFactory> create(AplCoreConnectionManagerPtr aplCoreConnectionManager, AplConfigurationPtr config);

/**
* @param playerId Player ID
* @return Corresponding player.
*/
AplCoreAudioPlayerPtr getPlayer(const std::string& playerId) const;

void tick(const AplCoreConnectionManager& connectionManager);

/// @name apl::AudioPlayerFactory Functions
/// @{
apl::AudioPlayerPtr createPlayer(apl::AudioPlayerCallback playerCallback,
apl::SpeechMarkCallback speechMarkCallback) override;
/// @}

private:
std::weak_ptr<AplCoreConnectionManager> m_aplCoreConnectionManager;
AplConfigurationPtr m_aplConfiguration;
std::map<std::string, AplCoreAudioPlayerPtr> m_Players;
};

using AplCoreAudioPlayerFactoryPtr = std::shared_ptr<AplCoreAudioPlayerFactory>;

} // namespace APLClient

#endif // APLCLIENT_APL_APLCOREAUDIOPLAYERFACTORY_H
58 changes: 42 additions & 16 deletions APLClient/include/APLClient/AplCoreConnectionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ class AplCoreConnectionManager
*/
void interruptCommandSequence();

/**
* Send a message to the view host
* @param message The message to send
* @return The sequence number of this message
*/
unsigned int send(AplCoreViewhostMessage& message);

/**
* Send a message to the view host and block until you get a reply
* @param message The message to send
Expand Down Expand Up @@ -210,6 +217,15 @@ class AplCoreConnectionManager
*/
void onDocumentRendered(const std::chrono::steady_clock::time_point &renderTime, uint64_t complexityScore);

/**
* Retrieves the current time
* @return The time
*/
std::chrono::milliseconds getCurrentTime() const {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch());
}

private:
/**
* Sends viewport scaling information to the client
Expand Down Expand Up @@ -320,6 +336,30 @@ class AplCoreConnectionManager
*/
void mediaLoadFailed(const rapidjson::Value& payload);

/**
* Handle the audioPlayerCallback message received from the viewhost
* @param payload
*/
void audioPlayerCallback(const rapidjson::Value& payload);

/**
* Handle the audioPlayerSpeechMarks message received from the viewhost
* @param payload
*/
void audioPlayerSpeechMarks(const rapidjson::Value& payload);

/**
* Handle the mediaPlayerUpdateMediaState message received from the viewhost
* @param payload
*/
void mediaPlayerUpdateMediaState(const rapidjson::Value& payload);

/**
* Handle the mediaPlayerDoCallback message received from the viewhost
* @param payload
*/
void mediaPlayerDoCallback(const rapidjson::Value& payload);

/**
* Handle the getFocusableAreas message received from the viewhost
* @param payload
Expand Down Expand Up @@ -402,6 +442,7 @@ class AplCoreConnectionManager
void sendHierarchy(const std::string& messageKey, bool blocking = false);

rapidjson::Value buildDisplayedChildrenHierarchy(const apl::ComponentPtr& component, AplCoreViewhostMessage& message);

/**
* Process set of dirty components and send out dirty properties as required.
* @param dirty dirty components set.
Expand All @@ -419,28 +460,12 @@ class AplCoreConnectionManager
*/
void coreFrameUpdate();

/**
* Send a message to the view host
* @param message The message to send
* @return The sequence number of this message
*/
unsigned int send(AplCoreViewhostMessage& message);

/**
* Sends an error message to the view host
* @param message The message to send to the view hsot
*/
void sendError(const std::string& message);

/**
* Retrieves the current time
* @return The time
*/
std::chrono::milliseconds getCurrentTime() {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch());
}

/**
* Get optional value from Json.
* @param jsonNode json data
Expand Down Expand Up @@ -580,6 +605,7 @@ class AplCoreConnectionManager
};

using AplCoreConnectionManagerPtr = std::shared_ptr<AplCoreConnectionManager>;
using AplCoreConnectionManagerWPtr = std::weak_ptr<AplCoreConnectionManager>;

} // namespace APLClient

Expand Down

0 comments on commit 71877ed

Please sign in to comment.