Skip to content

Commit c5101d2

Browse files
committed
Remove Tizen API from the plugin class
1 parent d17f42c commit c5101d2

File tree

3 files changed

+65
-68
lines changed

3 files changed

+65
-68
lines changed

packages/audioplayers/tizen/src/audio_player.cc

+38-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void AudioPlayer::Play() {
6060
ret = player_start(player_);
6161
HandleResult("player_start", ret);
6262
should_play_ = false;
63-
start_playing_listener_(player_id_);
63+
EmitPositionUpdates();
6464
break;
6565
default:
6666
// "player is already playing audio.
@@ -98,6 +98,10 @@ void AudioPlayer::Release() {
9898
player_destroy(player_);
9999
player_ = nullptr;
100100
}
101+
if (timer_) {
102+
ecore_timer_del(timer_);
103+
timer_ = nullptr;
104+
}
101105
}
102106

103107
void AudioPlayer::Seek(int position) {
@@ -236,6 +240,10 @@ void AudioPlayer::PreparePlayer() {
236240
seeking_ = false;
237241
}
238242

243+
void AudioPlayer::EmitPositionUpdates() {
244+
ecore_main_loop_thread_safe_call_async(StartPositionUpdates, (void *)this);
245+
}
246+
239247
void AudioPlayer::ResetPlayer() {
240248
int ret;
241249
player_state_e state = GetPlayerState();
@@ -287,7 +295,7 @@ void AudioPlayer::OnPrepared(void *data) {
287295
if (player->should_play_) {
288296
ret = player_start(player->player_);
289297
if (ret == PLAYER_ERROR_NONE) {
290-
player->start_playing_listener_(player->player_id_);
298+
player->EmitPositionUpdates();
291299
}
292300
player->should_play_ = false;
293301
}
@@ -330,3 +338,31 @@ void AudioPlayer::OnErrorOccurred(int code, void *data) {
330338
AudioPlayer *player = (AudioPlayer *)data;
331339
player->error_listener_(player->player_id_, "error occurred: " + error);
332340
}
341+
342+
void AudioPlayer::StartPositionUpdates(void *data) {
343+
AudioPlayer *player = (AudioPlayer *)data;
344+
if (!player->timer_) {
345+
const double kTimeInterval = 0.2;
346+
player->timer_ = ecore_timer_add(kTimeInterval, OnPositionUpdate, data);
347+
if (player->timer_ == nullptr) {
348+
LOG_ERROR("failed to add timer for UpdatePosition");
349+
}
350+
}
351+
}
352+
353+
Eina_Bool AudioPlayer::OnPositionUpdate(void *data) {
354+
AudioPlayer *player = (AudioPlayer *)data;
355+
std::string player_id = player->GetPlayerId();
356+
try {
357+
if (player->IsPlaying()) {
358+
int duration = player->GetDuration();
359+
int position = player->GetCurrentPosition();
360+
player->start_playing_listener_(player_id, duration, position);
361+
return ECORE_CALLBACK_RENEW;
362+
}
363+
} catch (...) {
364+
LOG_ERROR("failed to update position for player %s", player_id.c_str());
365+
}
366+
player->timer_ = nullptr;
367+
return ECORE_CALLBACK_CANCEL;
368+
}

packages/audioplayers/tizen/src/audio_player.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef AUDIO_PLAYER_H_
22
#define AUDIO_PLAYER_H_
33

4+
#include <Ecore.h>
45
#include <player.h>
56

67
#include <functional>
@@ -11,7 +12,8 @@ enum class ReleaseMode { kRelease, kLoop, kStop };
1112

1213
using PreparedListener =
1314
std::function<void(const std::string &player_id, int duration)>;
14-
using StartPlayingListener = std::function<void(const std::string &player_id)>;
15+
using StartPlayingListener = std::function<void(
16+
const std::string &player_id, const int duration, const int position)>;
1517
using SeekCompletedListener = std::function<void(const std::string &player_id)>;
1618
using PlayCompletedListener = std::function<void(const std::string &player_id)>;
1719
using ErrorListener = std::function<void(const std::string &player_id,
@@ -51,13 +53,16 @@ class AudioPlayer {
5153
// The player state should be idle before calling this function.
5254
void PreparePlayer();
5355
void ResetPlayer();
56+
void EmitPositionUpdates();
5457
player_state_e GetPlayerState();
5558

5659
static void OnPrepared(void *data);
5760
static void OnSeekCompleted(void *data);
5861
static void OnPlayCompleted(void *data);
5962
static void OnInterrupted(player_interrupted_code_e code, void *data);
6063
static void OnErrorOccurred(int code, void *data);
64+
static void StartPositionUpdates(void *data);
65+
static Eina_Bool OnPositionUpdate(void *data);
6166

6267
player_h player_ = nullptr;
6368
std::string player_id_;
@@ -71,6 +76,7 @@ class AudioPlayer {
7176
bool preparing_ = false;
7277
bool seeking_ = false;
7378
bool should_play_ = false;
79+
Ecore_Timer *timer_ = nullptr;
7480
PreparedListener prepared_listener_;
7581
StartPlayingListener start_playing_listener_;
7682
SeekCompletedListener seek_completed_listener_;

packages/audioplayers/tizen/src/audioplayers_tizen_plugin.cc

+20-65
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "audioplayers_tizen_plugin.h"
22

3-
#include <Ecore.h>
43
#include <flutter/encodable_value.h>
54
#include <flutter/method_channel.h>
65
#include <flutter/plugin_registrar.h>
@@ -15,8 +14,6 @@
1514

1615
namespace {
1716

18-
const double kTimeInterval = 0.2;
19-
2017
template <typename T>
2118
static bool GetValueFromEncodableMap(const flutter::EncodableMap *map,
2219
const char *key, T &out) {
@@ -51,12 +48,7 @@ class AudioplayersTizenPlugin : public flutter::Plugin {
5148

5249
AudioplayersTizenPlugin() {}
5350

54-
virtual ~AudioplayersTizenPlugin() {
55-
if (timer_) {
56-
ecore_timer_del(timer_);
57-
timer_ = nullptr;
58-
}
59-
}
51+
virtual ~AudioplayersTizenPlugin() {}
6052

6153
private:
6254
void HandleMethodCall(
@@ -208,9 +200,25 @@ class AudioplayersTizenPlugin : public flutter::Plugin {
208200
};
209201

210202
StartPlayingListener start_playing_listener =
211-
[plugin = this](const std::string &player_id) {
212-
ecore_main_loop_thread_safe_call_async(StartPositionUpdates,
213-
(void *)plugin);
203+
[channel = channel_.get()](const std::string &player_id,
204+
const int duration, const int position) {
205+
flutter::EncodableMap durationWrapped = {
206+
{flutter::EncodableValue("playerId"),
207+
flutter::EncodableValue(player_id)},
208+
{flutter::EncodableValue("value"),
209+
flutter::EncodableValue(duration)}};
210+
channel->InvokeMethod(
211+
"audio.onDuration",
212+
std::make_unique<flutter::EncodableValue>(durationWrapped));
213+
214+
flutter::EncodableMap positionWrapped = {
215+
{flutter::EncodableValue("playerId"),
216+
flutter::EncodableValue(player_id)},
217+
{flutter::EncodableValue("value"),
218+
flutter::EncodableValue(position)}};
219+
channel->InvokeMethod(
220+
"audio.onCurrentPosition",
221+
std::make_unique<flutter::EncodableValue>(positionWrapped));
214222
};
215223

216224
PlayCompletedListener play_completed_listener =
@@ -242,59 +250,6 @@ class AudioplayersTizenPlugin : public flutter::Plugin {
242250
return audio_players_[player_id].get();
243251
}
244252

245-
static void StartPositionUpdates(void *data) {
246-
AudioplayersTizenPlugin *plugin = (AudioplayersTizenPlugin *)data;
247-
if (!plugin->timer_) {
248-
plugin->timer_ = ecore_timer_add(kTimeInterval, UpdatePosition, data);
249-
if (plugin->timer_ == nullptr) {
250-
LOG_ERROR("failed to add timer for UpdatePosition");
251-
}
252-
}
253-
}
254-
255-
static Eina_Bool UpdatePosition(void *data) {
256-
AudioplayersTizenPlugin *plugin = (AudioplayersTizenPlugin *)data;
257-
bool none_playing = true;
258-
auto iter = plugin->audio_players_.begin();
259-
while (iter != plugin->audio_players_.end()) {
260-
try {
261-
std::string player_id = iter->second->GetPlayerId();
262-
if (iter->second->IsPlaying()) {
263-
none_playing = false;
264-
flutter::EncodableMap duration = {
265-
{flutter::EncodableValue("playerId"),
266-
flutter::EncodableValue(player_id)},
267-
{flutter::EncodableValue("value"),
268-
flutter::EncodableValue(iter->second->GetDuration())}};
269-
plugin->channel_->InvokeMethod(
270-
"audio.onDuration",
271-
std::make_unique<flutter::EncodableValue>(duration));
272-
273-
flutter::EncodableMap position = {
274-
{flutter::EncodableValue("playerId"),
275-
flutter::EncodableValue(player_id)},
276-
{flutter::EncodableValue("value"),
277-
flutter::EncodableValue(iter->second->GetCurrentPosition())}};
278-
plugin->channel_->InvokeMethod(
279-
"audio.onCurrentPosition",
280-
std::make_unique<flutter::EncodableValue>(position));
281-
}
282-
iter++;
283-
} catch (...) {
284-
LOG_ERROR("failed to update position for player %s",
285-
iter->second->GetPlayerId().c_str());
286-
}
287-
}
288-
289-
if (none_playing) {
290-
plugin->timer_ = nullptr;
291-
return ECORE_CALLBACK_CANCEL;
292-
} else {
293-
return ECORE_CALLBACK_RENEW;
294-
}
295-
}
296-
297-
Ecore_Timer *timer_ = nullptr;
298253
std::unique_ptr<flutter::MethodChannel<flutter::EncodableValue>> channel_;
299254
std::map<std::string, std::unique_ptr<AudioPlayer>> audio_players_;
300255
};

0 commit comments

Comments
 (0)