~abreu-alexandre/oxide/add-ua-to-downloadrequested

« back to all changes in this revision

Viewing changes to shared/media/mediahub_player_shim.cc

  • Committer: Chris Coulson
  • Date: 2015-01-16 20:53:04 UTC
  • mfrom: (664.1.36 mediahub)
  • Revision ID: chris.coulson@canonical.com-20150116205304-i5wisclpxg0f8ljz
Add experimental support for playing audio via mediahub on mobile

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "mediahub_player_shim.h"
 
2
 
 
3
#include <map>
 
4
#include <ostream>
 
5
#include <core/media/service.h>
 
6
#include <core/media/player.h>
 
7
 
 
8
using namespace core::ubuntu::media;
 
9
 
 
10
namespace {
 
11
 
 
12
std::map<int, std::shared_ptr<Player> > g_mediahub_players;
 
13
 
 
14
void setup_delegate(MediaHubDelegate *delegate, std::shared_ptr<Player>& player)
 
15
{
 
16
  player->seeked_to().connect([delegate](int64_t pos) {
 
17
        delegate->seeked_to(pos);
 
18
      });
 
19
 
 
20
  player->end_of_stream().connect([delegate]() {
 
21
        delegate->end_of_stream();
 
22
      });
 
23
 
 
24
  player->playback_status_changed().connect([player, delegate](Player::PlaybackStatus status) {
 
25
        delegate->playback_status_changed(
 
26
                    static_cast<MediaHubDelegate::Status>(status),
 
27
                    player->duration().get()
 
28
                  );
 
29
      });
 
30
}
 
31
}
 
32
 
 
33
 
 
34
MediaHubClientHandle
 
35
mediahub_create_player(int player_id, MediaHubDelegate *delegate)
 
36
{
 
37
  try {
 
38
    if (player_id < 0) {
 
39
      return MediaHubClientHandle();
 
40
    }
 
41
 
 
42
    if (g_mediahub_players.find(player_id) != g_mediahub_players.end()) {
 
43
      return MediaHubClientHandle();
 
44
    }
 
45
 
 
46
    std::shared_ptr<Player> player =
 
47
        Service::Client::instance()->create_session(
 
48
            Player::Client::default_configuration());
 
49
 
 
50
    if (delegate != NULL) {
 
51
      setup_delegate(delegate, player);
 
52
    }
 
53
 
 
54
    g_mediahub_players[player_id] = player;
 
55
 
 
56
    return MediaHubClientHandle(static_cast<void*>(player.get()));
 
57
  } catch (std::runtime_error& error) {
 
58
    std::cerr << __PRETTY_FUNCTION__ << " " << error.what() << std::endl;
 
59
  }
 
60
  return MediaHubClientHandle();
 
61
}
 
62
 
 
63
MediaHubClientHandle
 
64
mediahub_create_fixed_player(int player_id, const std::string& domain, MediaHubDelegate *delegate)
 
65
{
 
66
  try {
 
67
    if (player_id < 0) {
 
68
      return MediaHubClientHandle();
 
69
    }
 
70
 
 
71
    if (g_mediahub_players.find(player_id) != g_mediahub_players.end()) {
 
72
      return MediaHubClientHandle();
 
73
    }
 
74
 
 
75
    std::shared_ptr<Player> player =
 
76
        Service::Client::instance()->create_fixed_session(domain,
 
77
            Player::Client::default_configuration());
 
78
 
 
79
    if (delegate != NULL) {
 
80
      setup_delegate(delegate, player);
 
81
 
 
82
      delegate->playback_status_changed(
 
83
                    MediaHubDelegate::Status(player->playback_status().get()),
 
84
                    player->duration().get()
 
85
                  );
 
86
    }
 
87
 
 
88
    g_mediahub_players[player_id] = player;
 
89
 
 
90
    return MediaHubClientHandle(static_cast<void*>(player.get()));
 
91
  } catch (std::runtime_error& error) {
 
92
    std::cerr << __PRETTY_FUNCTION__ << " " << error.what() << std::endl;
 
93
  }
 
94
  return MediaHubClientHandle();
 
95
}
 
96
 
 
97
MediaHubClientHandle
 
98
mediahub_resume_player(int player_id, int player_key)
 
99
{
 
100
  try {
 
101
    if (player_id < 0) {
 
102
      return MediaHubClientHandle();
 
103
    }
 
104
 
 
105
    if (g_mediahub_players.find(player_id) != g_mediahub_players.end()) {
 
106
      return MediaHubClientHandle();
 
107
    }
 
108
 
 
109
    std::shared_ptr<Player> player =
 
110
        Service::Client::instance()->resume_session(player_key);
 
111
 
 
112
    g_mediahub_players[player_id] = player;
 
113
 
 
114
    return MediaHubClientHandle(static_cast<void*>(player.get()));
 
115
  } catch (std::runtime_error& error) {
 
116
    std::cerr << __PRETTY_FUNCTION__ << " " << error.what() << std::endl;
 
117
  }
 
118
  return MediaHubClientHandle();
 
119
}
 
120
 
 
121
bool
 
122
mediahub_open_uri(MediaHubClientHandle handle,
 
123
                  const std::string& uri,
 
124
                  const std::string& cookies,
 
125
                  const std::string& user_agent)
 
126
{
 
127
  try {
 
128
    Player* player = static_cast<Player*>(handle);
 
129
    if (player != 0) {
 
130
      if (player->playback_status().get() == Player::playing);
 
131
        player->stop();
 
132
 
 
133
      core::ubuntu::media::Player::HeadersType headers;
 
134
      if (!cookies.empty()) {
 
135
        headers["Cookie"] = cookies;
 
136
      }
 
137
      if (!user_agent.empty()) {
 
138
        headers["User-Agent"] = user_agent;
 
139
      }
 
140
      return player->open_uri(uri, headers);
 
141
    }
 
142
  } catch (std::runtime_error& error) {
 
143
    std::cerr << __PRETTY_FUNCTION__ << " " << error.what() << std::endl;
 
144
  }
 
145
  return false;
 
146
}
 
147
 
 
148
 
 
149
void
 
150
mediahub_play(MediaHubClientHandle handle)
 
151
{
 
152
  try {
 
153
    Player* player = static_cast<Player*>(handle);
 
154
    if (player) {
 
155
      player->play();
 
156
    }
 
157
  } catch (std::runtime_error& error) {
 
158
    std::cerr << __PRETTY_FUNCTION__ << " " << error.what() << std::endl;
 
159
  }
 
160
}
 
161
 
 
162
void
 
163
mediahub_pause(MediaHubClientHandle handle)
 
164
{
 
165
  try {
 
166
    Player* player = static_cast<Player*>(handle);
 
167
    if (player) {
 
168
      player->pause();
 
169
    }
 
170
  } catch (std::runtime_error& error) {
 
171
    std::cerr << __PRETTY_FUNCTION__ << " " << error.what() << std::endl;
 
172
  }
 
173
}
 
174
 
 
175
void
 
176
mediahub_stop(MediaHubClientHandle handle)
 
177
{
 
178
  try {
 
179
    Player* player = static_cast<Player*>(handle);
 
180
    if (player) {
 
181
      player->stop();
 
182
    }
 
183
  } catch (std::runtime_error& error) {
 
184
    std::cerr << __PRETTY_FUNCTION__ << " " << error.what() << std::endl;
 
185
  }
 
186
}
 
187
 
 
188
unsigned long long
 
189
mediahub_get_duration(MediaHubClientHandle handle)
 
190
{
 
191
  try {
 
192
    Player* player = static_cast<Player*>(handle);
 
193
    if (player) {
 
194
      return player->duration().get();
 
195
    }
 
196
  } catch (std::runtime_error& error) {
 
197
    std::cerr << __PRETTY_FUNCTION__ << " " << error.what() << std::endl;
 
198
  }
 
199
  return 0;
 
200
}
 
201
 
 
202
unsigned long long
 
203
mediahub_get_position(MediaHubClientHandle handle)
 
204
{
 
205
  try {
 
206
    Player* player = static_cast<Player*>(handle);
 
207
    if (player) {
 
208
      return player->position().get();
 
209
    }
 
210
  } catch (std::runtime_error& error) {
 
211
    std::cerr << __PRETTY_FUNCTION__ << " " << error.what() << std::endl;
 
212
  }
 
213
  return 0;
 
214
}
 
215
 
 
216
void
 
217
mediahub_release(MediaHubClientHandle handle)
 
218
{
 
219
  try {
 
220
    auto it = g_mediahub_players.begin();
 
221
    for (; it != g_mediahub_players.end(); it++) {
 
222
      if (it->second.get() == handle) {
 
223
        break;
 
224
      }
 
225
    }
 
226
    if (it != g_mediahub_players.end()) {
 
227
      g_mediahub_players.erase(it);
 
228
    }
 
229
  } catch (std::runtime_error& error) {
 
230
    std::cerr << __PRETTY_FUNCTION__ << " " << error.what() << std::endl;
 
231
  }
 
232
}
 
233
 
 
234
int
 
235
mediahub_is_playing(MediaHubClientHandle handle)
 
236
{
 
237
  try {
 
238
    Player* player = static_cast<Player*>(handle);
 
239
    if (player) {
 
240
      return player->playback_status().get() != Player::playing;
 
241
    }
 
242
  } catch (std::runtime_error& error) {
 
243
    std::cerr << __PRETTY_FUNCTION__ << " " << error.what() << std::endl;
 
244
  }
 
245
  return 0;
 
246
}
 
247
 
 
248
int
 
249
mediahub_can_seek_forward(MediaHubClientHandle handle)
 
250
{
 
251
  try {
 
252
    Player* player = static_cast<Player*>(handle);
 
253
    if (player) {
 
254
      return player->can_seek().get();
 
255
    }
 
256
  } catch (std::runtime_error& error) {
 
257
    std::cerr << __PRETTY_FUNCTION__ << " " << error.what() << std::endl;
 
258
  }
 
259
  return 0;
 
260
}
 
261
 
 
262
int
 
263
mediahub_can_seek_backward(MediaHubClientHandle handle)
 
264
{
 
265
  try {
 
266
    Player* player = static_cast<Player*>(handle);
 
267
    if (player) {
 
268
      return player->can_seek().get();
 
269
    }
 
270
  } catch (std::runtime_error& error) {
 
271
    std::cerr << __PRETTY_FUNCTION__ << " " << error.what() << std::endl;
 
272
  }
 
273
  return 0;
 
274
}
 
275
 
 
276
void
 
277
mediahub_seek_to(MediaHubClientHandle handle, int64_t offset)
 
278
{
 
279
  try {
 
280
    Player* player = static_cast<Player*>(handle);
 
281
    if (player) {
 
282
      return player->seek_to(std::chrono::microseconds(offset));
 
283
    }
 
284
  } catch (std::runtime_error& error) {
 
285
    std::cerr << __PRETTY_FUNCTION__ << " " << error.what() << std::endl;
 
286
  }
 
287
}
 
288
 
 
289
int
 
290
mediahub_can_pause(MediaHubClientHandle handle)
 
291
{
 
292
  try {
 
293
    Player* player = static_cast<Player*>(handle);
 
294
    if (player) {
 
295
      return player->can_pause().get();
 
296
    }
 
297
  } catch (std::runtime_error& error) {
 
298
    std::cerr << __PRETTY_FUNCTION__ << " " << error.what() << std::endl;
 
299
  }
 
300
  return 0;
 
301
}
 
302
 
 
303
int
 
304
mediahub_is_player_ready(MediaHubClientHandle handle)
 
305
{
 
306
  try {
 
307
    Player* player = static_cast<Player*>(handle);
 
308
    if (player) {
 
309
      return player->playback_status().get() != Player::null;
 
310
    }
 
311
  } catch (std::runtime_error& error) {
 
312
    std::cerr << __PRETTY_FUNCTION__ << " " << error.what() << std::endl;
 
313
  }
 
314
  return 0;
 
315
}
 
316
 
 
317
void
 
318
mediahub_set_volume(MediaHubClientHandle handle, double volume)
 
319
{
 
320
  try {
 
321
    Player* player = static_cast<Player*>(handle);
 
322
    if (player) {
 
323
      player->volume().set(volume);
 
324
    }
 
325
  } catch (std::runtime_error& error) {
 
326
    std::cerr << __PRETTY_FUNCTION__ << " " << error.what() << std::endl;
 
327
  }
 
328
}
 
329
 
 
330
void
 
331
mediahub_set_player_lifetime(MediaHubClientHandle handle, Lifetime lifetime)
 
332
{
 
333
  try {
 
334
    Player* player = static_cast<Player*>(handle);
 
335
    if (player) {
 
336
      player->lifetime().set(Player::Lifetime(lifetime));
 
337
    }
 
338
  } catch (std::runtime_error& error) {
 
339
    std::cerr << __PRETTY_FUNCTION__ << " " << error.what() << std::endl;
 
340
  }
 
341
}
 
342