@@ -30,23 +30,23 @@ static std::string base64Encode(const std::vector<uint8_t>& data) {
3030
3131static std::string getThumbnailBase64 (GlobalSystemMediaTransportControlsSessionMediaProperties const & mediaProps) {
3232 try {
33- auto thumbnail = mediaProps.Thumbnail ();
33+ const auto thumbnail = mediaProps.Thumbnail ();
3434 if (!thumbnail) return " " ;
3535
36- auto stream = thumbnail.OpenReadAsync ().get ();
36+ const auto stream = thumbnail.OpenReadAsync ().get ();
3737 if (!stream) return " " ;
3838
39- auto size = stream.Size ();
39+ const auto size = stream.Size ();
4040 if (size == 0 ) return " " ;
4141
4242 std::vector<uint8_t > buffer (size);
43- DataReader reader (stream);
43+ const DataReader reader (stream);
4444 reader.LoadAsync (static_cast <uint32_t >(size)).get ();
4545 reader.ReadBytes (buffer);
4646
4747 return base64Encode (buffer);
4848 } catch (...) {
49- return " " ; // return empty string on any error
49+ return " " ;
5050 }
5151}
5252
@@ -60,12 +60,12 @@ Java_dev_codeman_smtc4j_SMTC4J_getPlaybackState(JNIEnv* env, jclass) {
6060 } catch (const winrt::hresult_invalid_argument&) {
6161 }
6262
63- auto manager = GlobalSystemMediaTransportControlsSessionManager::RequestAsync ().get ();
64- auto session = manager.GetCurrentSession ();
63+ const auto manager = GlobalSystemMediaTransportControlsSessionManager::RequestAsync ().get ();
64+ const auto session = manager.GetCurrentSession ();
6565 if (!session) return env->NewStringUTF (" {}" );
6666
67- auto info = session.GetPlaybackInfo ();
68- auto status = info.PlaybackStatus ();
67+ const auto info = session.GetPlaybackInfo ();
68+ const auto status = info.PlaybackStatus ();
6969
7070 int stateCode = 0 ;
7171 switch (status) {
@@ -75,40 +75,28 @@ Java_dev_codeman_smtc4j_SMTC4J_getPlaybackState(JNIEnv* env, jclass) {
7575 default : stateCode = -1 ; break ;
7676 }
7777
78- auto timeline = session.GetTimelineProperties ();
79- double smtcPosition = timeline.Position ().count () / 10'000'000.0 ; // seconds
78+ const auto timeline = session.GetTimelineProperties ();
79+ double positionSec = timeline.Position ().count () / 10'000'000.0 ; // to seconds
8080
81- static auto cachedLastTimeLineUpdate = timeline.LastUpdatedTime ().time_since_epoch ();
82- auto lastTimeLineUpdate = timeline.LastUpdatedTime ().time_since_epoch ();
83-
84- static double cachedPosition = smtcPosition;
85- static auto lastTime = std::chrono::steady_clock::now ();
86- auto now = std::chrono::steady_clock::now ();
87-
88- double delta = std::chrono::duration<double >(now - lastTime).count (); // seconds
89- lastTime = now;
90-
91- if (lastTimeLineUpdate != cachedLastTimeLineUpdate || stateCode != 2 ) {
92- cachedPosition = smtcPosition;
93- cachedLastTimeLineUpdate = lastTimeLineUpdate;
94- } else {
95- cachedPosition += delta;
81+ if (stateCode == 2 ) { // update position based on elapsed time while playing
82+ const auto lastUpdated = timeline.LastUpdatedTime ().time_since_epoch ();
83+ const auto now = winrt::clock::now ().time_since_epoch ();
84+ const auto deltaTicks = now.count () - lastUpdated.count ();
85+ positionSec += deltaTicks / 10'000'000.0 ; // to seconds
9686 }
9787
98- double positionSec = cachedPosition;
99-
100- std::string json = " {"
88+ const std::string json = " {"
10189 " \" stateCode\" :" + std::to_string (stateCode) + " ,"
10290 " \" position\" :" + std::to_string (positionSec) +
10391 " }" ;
10492
10593 return env->NewStringUTF (json.c_str ());
10694
10795 } catch (const winrt::hresult_error& e) {
108- std::string err = std::string (" { \ " error\" : \" " ) + winrt::to_string (e.message ()) + " \" }" ;
96+ std::string err = std::string (R"( { "error":" ) " ) + winrt::to_string (e.message ()) + " \" }" ;
10997 return env->NewStringUTF (err.c_str ());
11098 } catch (...) {
111- return env->NewStringUTF (" { \ " error\" : \ " unknown exception\" } " );
99+ return env->NewStringUTF (R"( { "error": "unknown exception"} ) " );
112100 }
113101}
114102
@@ -120,18 +108,18 @@ Java_dev_codeman_smtc4j_SMTC4J_getMediaInfo(JNIEnv* env, jclass) {
120108 } catch (const winrt::hresult_invalid_argument&) {
121109 }
122110
123- auto manager = GlobalSystemMediaTransportControlsSessionManager::RequestAsync ().get ();
124- auto session = manager.GetCurrentSession ();
111+ const auto manager = GlobalSystemMediaTransportControlsSessionManager::RequestAsync ().get ();
112+ const auto session = manager.GetCurrentSession ();
125113 if (!session) return env->NewStringUTF (" {}" );
126114
127- auto mediaProps = session.TryGetMediaPropertiesAsync ().get ();
128- auto timeline = session.GetTimelineProperties ();
115+ const auto mediaProps = session.TryGetMediaPropertiesAsync ().get ();
116+ const auto timeline = session.GetTimelineProperties ();
129117
130- double durationSec = timeline.EndTime ().count () / 10'000'000.0 ;
131- std::string thumbnailBase64 = getThumbnailBase64 (mediaProps);
132- auto sourceApp = winrt::to_string (session.SourceAppUserModelId ());
118+ const double durationSec = timeline.EndTime ().count () / 10'000'000.0 ;
119+ const std::string thumbnailBase64 = getThumbnailBase64 (mediaProps);
120+ const auto sourceApp = winrt::to_string (session.SourceAppUserModelId ());
133121
134- std::string json = " {"
122+ const std::string json = " {"
135123 " \" title\" :\" " + winrt::to_string (mediaProps.Title ()) + " \" ,"
136124 " \" artist\" :\" " + winrt::to_string (mediaProps.Artist ()) + " \" ,"
137125 " \" album\" :\" " + winrt::to_string (mediaProps.AlbumTitle ()) + " \" ,"
@@ -143,10 +131,10 @@ Java_dev_codeman_smtc4j_SMTC4J_getMediaInfo(JNIEnv* env, jclass) {
143131 return env->NewStringUTF (json.c_str ());
144132
145133 } catch (const winrt::hresult_error& e) {
146- std::string err = std::string (" { \ " error\" : \" " ) + winrt::to_string (e.message ()) + " \" }" ;
134+ const std::string err = std::string (R"( { "error":" ) " ) + winrt::to_string (e.message ()) + " \" }" ;
147135 return env->NewStringUTF (err.c_str ());
148136 } catch (...) {
149- return env->NewStringUTF (" { \ " error\" : \ " unknown exception\" } " );
137+ return env->NewStringUTF (R"( { "error": "unknown exception"} ) " );
150138 }
151139}
152140
0 commit comments