1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "media/filters/in_memory_url_protocol.h"
6
7 #include "media/ffmpeg/ffmpeg_common.h"
8
9 namespace media {
10
InMemoryUrlProtocol(const uint8_t * data,int64_t size,bool streaming)11 InMemoryUrlProtocol::InMemoryUrlProtocol(const uint8_t* data,
12 int64_t size,
13 bool streaming)
14 : data_(data),
15 size_(size >= 0 ? size : 0),
16 position_(0),
17 streaming_(streaming) {}
18
19 InMemoryUrlProtocol::~InMemoryUrlProtocol() = default;
20
Read(int size,uint8_t * data)21 int InMemoryUrlProtocol::Read(int size, uint8_t* data) {
22 // Not sure this can happen, but it's unclear from the ffmpeg code, so guard
23 // against it.
24 if (size < 0)
25 return AVERROR(EIO);
26 if (!size)
27 return 0;
28
29 const int64_t available_bytes = size_ - position_;
30 if (available_bytes <= 0)
31 return AVERROR_EOF;
32
33 if (size > available_bytes)
34 size = available_bytes;
35
36 if (size > 0) {
37 memcpy(data, data_ + position_, size);
38 position_ += size;
39 }
40
41 return size;
42 }
43
GetPosition(int64_t * position_out)44 bool InMemoryUrlProtocol::GetPosition(int64_t* position_out) {
45 if (!position_out)
46 return false;
47
48 *position_out = position_;
49 return true;
50 }
51
SetPosition(int64_t position)52 bool InMemoryUrlProtocol::SetPosition(int64_t position) {
53 if (position < 0 || position > size_)
54 return false;
55 position_ = position;
56 return true;
57 }
58
GetSize(int64_t * size_out)59 bool InMemoryUrlProtocol::GetSize(int64_t* size_out) {
60 if (!size_out)
61 return false;
62
63 *size_out = size_;
64 return true;
65 }
66
IsStreaming()67 bool InMemoryUrlProtocol::IsStreaming() {
68 return streaming_;
69 }
70
71 } // namespace media
72