1 /* 2 * Copyright 2016, Dario Casalinuovo 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "HTTPMediaIO.h" 8 9 #include <Handler.h> 10 #include <UrlProtocolRoster.h> 11 12 #include <stdio.h> 13 14 15 class FileListener : public BUrlProtocolAsynchronousListener 16 { 17 public: 18 FileListener(BAdapterIO* owner) 19 : 20 BUrlProtocolAsynchronousListener(true), 21 fRequest(NULL), 22 fAdapterIO(owner) 23 { 24 fInputAdapter = fAdapterIO->BuildInputAdapter(); 25 } 26 27 virtual ~FileListener() {}; 28 29 bool ConnectionSuccessful() const 30 { 31 printf("ConnectionSuccessful\n"); 32 return fRequest != NULL; 33 } 34 35 void ConnectionOpened(BUrlRequest* request) 36 { 37 printf("Connection opened\n"); 38 if (fRequest != NULL) 39 fRequest->Stop(); 40 41 fRequest = request; 42 } 43 44 void DataReceived(BUrlRequest* request, const char* data, 45 off_t position, ssize_t size) 46 { 47 printf("Data received\n"); 48 if (request != fRequest) 49 delete request; 50 51 fInputAdapter->Write(data, size); 52 } 53 54 void RequestCompleted(BUrlRequest* request, bool success) 55 { 56 printf("Request completed\n"); 57 printf("Success: %s\n", success ? "true" : "false"); 58 if (request != fRequest) 59 return; 60 61 fRequest = NULL; 62 delete request; 63 } 64 65 private: 66 BUrlRequest* fRequest; 67 BAdapterIO* fAdapterIO; 68 BInputAdapter* fInputAdapter; 69 }; 70 71 72 HTTPMediaIO::HTTPMediaIO(BUrl* url) 73 : 74 BAdapterIO( 75 B_MEDIA_STREAMING | B_MEDIA_MUTABLE_SIZE | B_MEDIA_SEEK_BACKWARD, 76 B_INFINITE_TIMEOUT), 77 fInitErr(B_OK) 78 { 79 fContext = new BUrlContext(); 80 fContext->AcquireReference(); 81 82 fListener = new FileListener(this); 83 84 fReq = BUrlProtocolRoster::MakeRequest(*url, 85 fListener, fContext); 86 87 fReq->Run(); 88 } 89 90 91 HTTPMediaIO::~HTTPMediaIO() 92 { 93 delete fReq; 94 delete fListener; 95 96 fContext->ReleaseReference(); 97 delete fContext; 98 } 99 100 101 status_t 102 HTTPMediaIO::InitCheck() const 103 { 104 return fInitErr; 105 } 106 107 108 ssize_t 109 HTTPMediaIO::WriteAt(off_t position, const void* buffer, size_t size) 110 { 111 return B_NOT_SUPPORTED; 112 } 113