OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "examples/media_test/media_test.h" | 5 #include "examples/media_test/media_test.h" |
6 #include "mojo/public/cpp/application/connect.h" | 6 #include "mojo/public/cpp/application/connect.h" |
7 #include "mojo/services/media/common/cpp/linear_transform.h" | 7 #include "mojo/services/media/common/cpp/timeline.h" |
8 #include "mojo/services/media/common/cpp/local_time.h" | 8 #include "mojo/services/media/common/cpp/timeline_function.h" |
9 #include "mojo/services/media/control/interfaces/media_factory.mojom.h" | 9 #include "mojo/services/media/control/interfaces/media_factory.mojom.h" |
10 | 10 |
11 namespace mojo { | 11 namespace mojo { |
12 namespace media { | 12 namespace media { |
13 namespace examples { | 13 namespace examples { |
14 | 14 |
15 // static | 15 // static |
16 std::unique_ptr<MediaTest> MediaTest::Create( | 16 std::unique_ptr<MediaTest> MediaTest::Create( |
17 mojo::ApplicationImpl* app, | 17 mojo::ApplicationImpl* app, |
18 const std::string& input_file_name) { | 18 const std::string& input_file_name) { |
19 return std::unique_ptr<MediaTest>(new MediaTest(app, input_file_name)); | 19 return std::unique_ptr<MediaTest>(new MediaTest(app, input_file_name)); |
20 } | 20 } |
21 | 21 |
22 MediaTest::MediaTest(mojo::ApplicationImpl* app, | 22 MediaTest::MediaTest(mojo::ApplicationImpl* app, |
23 const std::string& input_file_name) | 23 const std::string& input_file_name) |
24 : state_(MediaState::UNPREPARED) { | 24 : state_(MediaState::UNPREPARED) { |
25 MediaFactoryPtr factory; | 25 MediaFactoryPtr factory; |
26 ConnectToService(app->shell(), "mojo:media_factory", GetProxy(&factory)); | 26 ConnectToService(app->shell(), "mojo:media_factory", GetProxy(&factory)); |
27 | 27 |
28 SeekingReaderPtr reader; | 28 SeekingReaderPtr reader; |
29 factory->CreateNetworkReader(input_file_name, GetProxy(&reader)); | 29 factory->CreateNetworkReader(input_file_name, GetProxy(&reader)); |
30 factory->CreatePlayer(reader.Pass(), GetProxy(&media_player_)); | 30 factory->CreatePlayer(reader.Pass(), GetProxy(&media_player_)); |
31 | 31 |
32 HandleStatusUpdates(); | 32 HandleStatusUpdates(); |
33 } | 33 } |
34 | 34 |
35 MediaTest::~MediaTest() {} | 35 MediaTest::~MediaTest() {} |
36 | 36 |
37 int64_t MediaTest::position_ns() const { | 37 int64_t MediaTest::position_ns() const { |
38 // Apply the transform to the current time. | 38 // Apply the timeline function to the current time. |
39 int64_t position; | 39 int64_t position = timeline_function_(Timeline::local_now()); |
40 transform_.DoForwardTransform(LocalClock::now().time_since_epoch().count(), | |
41 &position); | |
42 | 40 |
43 if (position < 0) { | 41 if (position < 0) { |
44 position = 0; | 42 position = 0; |
45 } | 43 } |
46 | 44 |
47 if (metadata_ && static_cast<uint64_t>(position) > metadata_->duration) { | 45 if (metadata_ && static_cast<uint64_t>(position) > metadata_->duration) { |
48 position = metadata_->duration; | 46 position = metadata_->duration; |
49 } | 47 } |
50 | 48 |
51 return position; | 49 return position; |
52 } | 50 } |
53 | 51 |
54 const MediaMetadataPtr& MediaTest::metadata() const { | 52 const MediaMetadataPtr& MediaTest::metadata() const { |
55 return metadata_; | 53 return metadata_; |
56 } | 54 } |
57 | 55 |
58 void MediaTest::HandleStatusUpdates(uint64_t version, | 56 void MediaTest::HandleStatusUpdates(uint64_t version, |
59 MediaPlayerStatusPtr status) { | 57 MediaPlayerStatusPtr status) { |
60 if (status) { | 58 if (status) { |
61 // Process status received from the player. | 59 // Process status received from the player. |
62 previous_state_ = state_; | 60 previous_state_ = state_; |
63 state_ = status->state; | 61 state_ = status->state; |
64 | 62 |
65 // Create a linear transform that translates local time to presentation | |
66 // time. Note that 'reference' here refers to the presentation time, and | |
67 // 'target' refers to the local time. | |
68 if (status->timeline_transform) { | 63 if (status->timeline_transform) { |
69 transform_ = | 64 timeline_function_ = status->timeline_transform.To<TimelineFunction>(); |
70 LinearTransform(status->timeline_transform->quad->target_offset, | |
71 status->timeline_transform->quad->reference_delta, | |
72 status->timeline_transform->quad->target_delta, | |
73 status->timeline_transform->quad->reference_offset); | |
74 } | 65 } |
75 | 66 |
76 metadata_ = status->metadata.Pass(); | 67 metadata_ = status->metadata.Pass(); |
77 | 68 |
78 if (update_callback_ != nullptr) { | 69 if (update_callback_ != nullptr) { |
79 update_callback_(); | 70 update_callback_(); |
80 } | 71 } |
81 } | 72 } |
82 | 73 |
83 // Request a status update. | 74 // Request a status update. |
84 media_player_->GetStatus( | 75 media_player_->GetStatus( |
85 version, [this](uint64_t version, MediaPlayerStatusPtr status) { | 76 version, [this](uint64_t version, MediaPlayerStatusPtr status) { |
86 HandleStatusUpdates(version, status.Pass()); | 77 HandleStatusUpdates(version, status.Pass()); |
87 }); | 78 }); |
88 } | 79 } |
89 | 80 |
90 } // namespace examples | 81 } // namespace examples |
91 } // namespace media | 82 } // namespace media |
92 } // namespace mojo | 83 } // namespace mojo |
OLD | NEW |