Skip to content

Commit

Permalink
read_seek implement for libyk.
Browse files Browse the repository at this point in the history
Signed-off-by: Jack <[email protected]>
  • Loading branch information
Jackarain committed Jul 4, 2013
1 parent 3a8589f commit 718e336
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
5 changes: 5 additions & 0 deletions libyk/libyk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ bool youku::read_data(char* data, std::size_t size, std::size_t &read_size)
return m_impl->read_data(data, size, read_size);
}

boost::int64_t youku::read_seek(boost::uint64_t offset, int whence)
{
return m_impl->read_seek(offset, whence);
}

}
3 changes: 3 additions & 0 deletions libyk/libyk.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ namespace libyk
// 读取当前下载的index对应视频的数据.
bool read_data(char* data, std::size_t size, std::size_t &read_size);

// seek文件位置.
boost::int64_t read_seek(boost::uint64_t offset, int whence);

///停止下载.
void stop();

Expand Down
62 changes: 61 additions & 1 deletion libyk/youku_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include <boost/thread/condition.hpp>
#include "youku_impl.h"

#ifndef AVSEEK_SIZE
#define AVSEEK_SIZE 0x10000
#endif

namespace libyk {

using boost::property_tree::wptree;
Expand Down Expand Up @@ -279,7 +283,7 @@ bool youku_impl::read_data(char* data, std::size_t size, std::size_t &read_size)
return false;

// 通知读取数据.
boost::asio::mutable_buffers_1 bufs = boost::asio::buffer(data, size);
boost::asio::mutable_buffers_1 bufs = boost::asio::buffer(data, size);
m_io_service.post(boost::bind(
&youku_impl::handle_read_data<boost::asio::mutable_buffers_1>, this,
boost::ref(cond), boost::cref(bufs), boost::ref(read_size)));
Expand Down Expand Up @@ -526,6 +530,62 @@ duration_info youku_impl::current_duration_info()
return ret;
}

boost::int64_t youku_impl::read_seek(boost::uint64_t offset, int whence)
{
boost::condition cond;
boost::mutex::scoped_lock lock(m_mutex);

// 通知下载位置改变.
m_io_service.post(boost::bind(&youku_impl::handle_read_seek,
this, boost::ref(cond), boost::ref(offset), whence));

// 等待完成操作.
cond.wait(lock);

return offset;
}

void youku_impl::handle_read_seek(boost::condition &cond, boost::uint64_t &offset, int whence)
{
if (!m_multi_http)
{
switch (whence)
{
case SEEK_SET: // 文件起始位置计算.
{
// 只更新下载地址.
m_offset = offset;
char buf[1];
m_multi_http->fetch_data(boost::asio::buffer(&buf[0], 0), m_offset);
}
break;
case SEEK_CUR: // 文件指针当前位置开始计算.
{
m_offset += offset;
char buf[1];
m_multi_http->fetch_data(boost::asio::buffer(&buf[0], 0), m_offset);
offset = m_offset;
}
break;
case SEEK_END: // 文件尾开始计算.
{
m_offset = m_multi_http->file_size() - offset;
char buf[1];
m_multi_http->fetch_data(boost::asio::buffer(&buf[0], 0), m_offset);
offset = m_offset;
}
break;
case AVSEEK_SIZE: // 文件大小.
{
offset = m_multi_http->file_size();
}
break;
}
}

cond.notify_one();
}

}


Expand Down
5 changes: 5 additions & 0 deletions libyk/youku_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class youku_impl : public boost::noncopyable
// 读取当前下载的index对应视频的数据.
bool read_data(char* data, std::size_t size, std::size_t &read_size);

// seek文件位置.
boost::int64_t read_seek(boost::uint64_t offset, int whence);

// 停止下载.
void stop();

Expand All @@ -55,6 +58,8 @@ class youku_impl : public boost::noncopyable
template <typename MutableBufferSequence>
void handle_read_data(boost::condition &cond,
const MutableBufferSequence &buffers, std::size_t &read_size);
void handle_read_seek(boost::condition &cond,
boost::uint64_t &offset, int whence);
video_type query_quality();

private:
Expand Down

0 comments on commit 718e336

Please sign in to comment.